Click here to Skip to main content
15,894,907 members
Articles / Programming Languages / Visual Basic
Article

This class has some nice statistical functions

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
20 Mar 2007CPOL 36.8K   555   17   8
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionShould you have tried a different approach maybe? Pin
DJBrown14-Jan-11 13:30
DJBrown14-Jan-11 13:30 
AnswerRe: Should you have tried a different approach maybe? Pin
hossein narimani rad14-Jan-11 19:12
hossein narimani rad14-Jan-11 19:12 
Generalspelling Pin
Gwareth16-Apr-07 5:43
Gwareth16-Apr-07 5:43 
Good code. I converted it for use in VB6. One thing though, you should probably check your spelling. Dimention is actually spelled Dimension. Will help make it look better.

Also, I'd recommend a few comments in the code. No code should ever go fully uncommented. Its bad programming practice.

Keep up the good work otherwise!
AnswerRe: spelling Pin
Hossein Narimani Rad25-Apr-07 0:07
Hossein Narimani Rad25-Apr-07 0:07 
GeneralExceptions Pin
Kidan20-Mar-07 9:40
Kidan20-Mar-07 9:40 
AnswerRe: Exceptions Pin
Hossein Narimani Rad31-Mar-07 6:22
Hossein Narimani Rad31-Mar-07 6:22 
Generalbooo Pin
Thanks for all the fish20-Mar-07 8:48
Thanks for all the fish20-Mar-07 8:48 
AnswerRe: booo Pin
Hossein Narimani Rad31-Mar-07 6:23
Hossein Narimani Rad31-Mar-07 6:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.