SubName: Mean, variance, variance2
Author: [Wikipedia]
Description: Calculate the mean and the variance of a list of data elements.
The Standard Deviation is the root of the Variance.
If the data is a sample (not the whole population) then estimation for the variance is by deviding by n-1 instead of n (n = the size of the list).
Tags: mean, variance
Author: [Wikipedia]
Description: Calculate the mean and the variance of a list of data elements.
The Standard Deviation is the root of the Variance.
If the data is a sample (not the whole population) then estimation for the variance is by deviding by n-1 instead of n (n = the size of the list).
B4X:
Sub mean(data As List) As Double
Dim sum As Double = 0
For Each x As Double In data
sum = sum + x
Next
Return sum/data.size
End Sub
'calculate when the mean is not known
Sub variance(data As List) As Double
Dim Sum As Double = 0
Dim Sum_sqr As Double = 0
For Each x As Double In data
Sum = Sum + x
Sum_sqr = Sum_sqr + x*x
Next
Return (Sum_sqr - (Sum*Sum)/data.Size)/data.size
'Return (Sum_sqr - (Sum*Sum)/data.Size)/(data.size-1) ' for sample
End Sub
'calculate when the mean is known
Sub variance2(data As List, meanx As Double) As Double
Dim sum As Double = 0
For Each x As Double In data
sum = sum + (x-meanx)*(x-meanx)
Next
Return sum/data.size
'Return sum/(data.size-1) ' for sample
End Sub
Tags: mean, variance
Last edited: