Results 1 to 3 of 3

Thread: Variance Built In Function

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2001
    Location
    UK
    Posts
    158

    Variance Built In Function

    Is there a built in function to get the variance????
    [vbcode] On Error GoTo VBForums[/vbcode]
    www27.brinkster.com/muditha

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    there is however a Variance function in SQL ... I mean if your gonna calc. variance on data found in a db ....

    Return estimates of the variance for a population or a population sample represented as a set of values contained in a specified field on a query.

    Syntax

    Var(expr)
    VarP(expr)

    The expr placeholder represents a string expression identifying the field that contains the numeric data you want to evaluate or an expression that performs a calculation using the data in that field. Operands in expr can include the name of a table field, a constant, or a function (which can be either intrinsic or user-defined but not one of the other SQL aggregate functions).

    Remarks

    The VarP function evaluates a population, and the Var function evaluates a population sample.

    If the underlying query contains fewer than two records, the Var and VarP functions return a Null value, which indicates that a variance can't be calculated.

    You can use the Var and VarP functions in a query expression or in an SQL statement.
    -= a peet post =-

  3. #3
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    There isn't a built in function that I know of, but it's not difficult to do with a little work. Here's something that you might find useful. A long time ago I made a thing that will do some simple statistical analysis stuff on a set of data. My original isn't commented, so I went back and added that in. It does some minimal error checking, like not allowing you to pass anything other than an array (which would be useless), and not trying to analyze a set of data with less than 2 things in it. The optional boolean parameter of the function is for telling it what the data set is. If you give it true, it treats the data as the whole population; if false, it treats it like a sample of the population. The difference affects how variance is calculated. The function assumes the data is a population by default. The array you give the function can be 0 or 1 based since it uses LBound and UBound.
    VB Code:
    1. 'in a module
    2. Public Type STAT_ANALYSIS
    3.    N As Long
    4.    Sum As Double
    5.    Mean As Double
    6.    Min As Double
    7.    Max As Double
    8.    Range As Double
    9.    SumSquared As Double
    10.    SumOfXiSquared As Double
    11.    Variance As Double
    12.    StandardDeviation As Double
    13.    SourceData As Variant
    14.    Population As Boolean
    15. End Type
    16.  
    17. Public Function Analyze(varData As Variant, Optional ByVal blnPopulation As Boolean = True) As STAT_ANALYSIS
    18.    Dim blnFirstPass As Boolean 'flag to tell if it's the first pass
    19.    Dim i As Long
    20.    'passing anything other than an array is useless, so terminate if varData doesn't have an array
    21.    If Not CBool(VarType(varData) And vbArray) Then Exit Function
    22.    With Analyze
    23.       'total number of items
    24.       .N = UBound(varData) - LBound(varData) + 1
    25.       'statistical analysis needs at the very very least 2 items
    26.       If .N < 2 Then Exit Function
    27.       .Sum = 0
    28.       .SumOfXiSquared = 0
    29.       blnFirstPass = True
    30.       'go through each item in the data set
    31.       For i = LBound(varData) To UBound(varData)
    32.          'get the running sum of the items
    33.          .Sum = .Sum + varData(i)
    34.          'get the running sum of the square of each item
    35.          .SumOfXiSquared = .SumOfXiSquared + (varData(i) ^ 2)
    36.          'if this isn't the first pass, give the max and min a value to start off
    37.          If blnFirstPass Then
    38.             .Min = varData(i)
    39.             .Max = varData(i)
    40.             blnFirstPass = False
    41.          Else
    42.             'these two statements will eventually find the max and min of the data set
    43.             If varData(i) > .Max Then .Max = varData(i)
    44.             If varData(i) < .Min Then .Min = varData(i)
    45.          End If
    46.       Next i
    47.       'get the range of the data
    48.       .Range = .Max - .Min
    49.       'get the mean (average) of the data
    50.       .Mean = .Sum / .N
    51.       'get the sum of all items squared
    52.       .SumSquared = .Sum ^ 2
    53.       'get the variance, based on whether the data set is a population or a sample
    54.       .Variance = (.SumOfXiSquared - (.SumSquared / .N)) / IIf(blnPopulation, .N, .N - 1)
    55.       'get the standard deviation
    56.       .StandardDeviation = Sqr(.Variance)
    57.       'stick the source data into the struct in case you need it later
    58.       .SourceData = varData
    59.       'tell whether or not the source data was treated as a population
    60.       .Population = blnPopulation
    61.    End With
    62. End Function
    Last edited by Kaverin; Jul 14th, 2001 at 08:45 PM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width