Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

This class has some nice statistical functions

0.00/5 (No votes)
20 Mar 2007 1  
It has some Statistical function such as mean , variance,...

Introduction

The class which is attached helps you to use statistical functions in your projects

It include these functinos :

1-Max

2-Min

3-Median

4-Mode

5-Mean

6-Variance

7-Covariance

8-VarCovarMatrix

9-Correlation

Using the code

Using the class is too easy.
Just try it. then enjoy it.

Something Nice

Have you ever tried to write a function that could calculate the Mode ?

It was really good experince i had writting this class

if you look at the function ( Mode ) you will find out, that it could count how many times an elemnt in an matrix of any type! with any dimension ( just less than 3) was repeated!

and finally it find the most repeated elements and return them as the Mode.

this is the code :

Public Shared Function Mode(ByVal InArray As Array) As Object()


        Dim Dimentions() As Integer = size(InArray)
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim NewInArray(InArray.Length - 1) As Object

        Select Case Dimentions.Length
            Case 1
                Array.Copy(InArray, NewInArray, InArray.Length)

            Case 2
                For i = 0 To Dimentions(0) - 1
                    For j = 0 To Dimentions(1) - 1
                        NewInArray(i * Dimentions(1) + j) = InArray(i, j)
                    Next
                Next

            Case 3
                For i = 0 To Dimentions(0) - 1
                    For j = 0 To Dimentions(1) - 1
                        For k = 0 To Dimentions(2) - 1
                            NewInArray(i * Dimentions(1) * Dimentions(2) + j * Dimentions(2) + k) = InArray(i, j, k)
                        Next
                    Next
                Next

            Case Else
                MsgBox("Matrix dimention must be less than 3", MsgBoxStyle.Critical, "Statistic")
                Return Nothing
        End Select

        Array.Sort(NewInArray)
        Dim Temp As Integer
        Dim Counter As Integer = 0
        Dim ModeArray(0) As ArrayValueRepetation

        i = 0

        While i < NewInArray.Length
            Temp = 0
            ReDim Preserve ModeArray(Counter)
            For j = i To NewInArray.Length - 1
                If NewInArray(i) = NewInArray(j) Then
                    Temp += 1
                    ModeArray(Counter).RepeatTimes = Temp
                    ModeArray(Counter).Value = NewInArray(i)
                ElseIf Temp = 1 Then
                    ModeArray(Counter).RepeatTimes = Temp
                    ModeArray(Counter).Value = NewInArray(i)
                    Exit For
                Else
                    Exit For
                End If
            Next
            Counter += 1
            i = j
        End While

        Dim RepeatTimes(Counter - 1) As Integer
        For i = 0 To Counter - 1
            RepeatTimes(i) = ModeArray(i).RepeatTimes
        Next

        Dim MaxRepeat As UInteger = Max(RepeatTimes)
        Dim OutVector(0) As Object
        Dim NumberOfModes As Integer = 0
        For i = 0 To Counter - 1
            If ModeArray(i).RepeatTimes = MaxRepeat Then
                ReDim Preserve OutVector(NumberOfModes)
                'OutVector.SetValue(ModeArray(i).Value, NumberOfModes)
                OutVector(NumberOfModes) = ModeArray(i).Value
                NumberOfModes += 1
            End If
        Next

        Return OutVector
    End Function
 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here