Statistics/Normal distribution: Difference between revisions

Added FreeBASIC
(added Haskell version)
(Added FreeBASIC)
Line 372:
2.8:
3.0:
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Const pi As Double = 3.141592653589793
Randomize
 
' Generates normally distributed random numbers with mean 0 and standard deviation 1
Function randomNormal() As Double
Return Cos(2.0 * pi * Rnd) * Sqr(-2.0 * Log(Rnd))
End Function
 
Sub normalStats(sampleSize As Integer)
If sampleSize < 1 Then Return
Dim r(1 To sampleSize) As Double
Dim h(-1 To 10) As Integer '' all zero by default
Dim sum As Double = 0.0
Dim hSum As Integer = 0
 
' Generate 'sampleSize' normally distributed random numbers with mean 0.5 and standard deviation 0.25
' calculate their sum
' and in which box they will fall when drawing the histogram
For i As Integer = 1 To sampleSize
r(i) = 0.5 + randomNormal / 4.0
sum += r(i)
If r(i) < 0.0 Then
h(-1) += 1
ElseIf r(i) >= 1.0 Then
h(10) += 1
Else
h(Int(r(i) * 10)) += 1
End If
Next
 
For i As Integer = -1 To 10 : hSum += h(i) : Next
' adjust one of the h() values if necessary to ensure hSum = sampleSize
Dim adj As Integer = sampleSize - hSum
If adj <> 0 Then
For i As Integer = -1 To 10
h(i) += adj
If h(i) >= 0 Then Exit For
h(i) -= adj
Next
End If
Dim mean As Double = sum / sampleSize
 
Dim sd As Double
sum = 0.0
' Now calculate their standard deviation
For i As Integer = 1 To sampleSize
sum += (r(i) - mean) ^ 2.0
Next
sd = Sqr(sum/sampleSize)
 
' Draw a histogram of the data with interval 0.1
Dim numStars As Integer
' If sample size > 300 then normalize histogram to 300
Dim scale As Double = 1.0
If sampleSize > 300 Then scale = 300.0 / sampleSize
Print "Sample size "; sampleSize
Print
Print Using " Mean #.######"; mean;
Print Using " SD #.######"; sd
Print
For i As Integer = -1 To 10
If i = -1 Then
Print Using "< 0.00 : ";
ElseIf i = 10 Then
Print Using ">=1.00 : ";
Else
Print Using " #.## : "; i/10.0;
End If
Print Using "##### " ; h(i);
numStars = Int(h(i) * scale + 0.5)
Print String(numStars, "*")
Next
End Sub
normalStats 100
Print
normalStats 1000
Print
normalStats 10000
Print
normalStats 100000
Print
Print "Press any key to quit"
Sleep</lang>
Sample output:
{{out}}
<pre>
Sample size 100
 
Mean 0.486977 SD 0.244147
 
< 0.00 : 2 **
0.00 : 5 *****
0.10 : 4 ****
0.20 : 14 **************
0.30 : 12 ************
0.40 : 15 ***************
0.50 : 17 *****************
0.60 : 11 ***********
0.70 : 9 *********
0.80 : 7 *******
0.90 : 1 *
>=1.00 : 3 ***
 
Sample size 1000
 
Mean 0.489234 SD 0.247606
 
< 0.00 : 18 *****
0.00 : 32 **********
0.10 : 73 **********************
0.20 : 111 *********************************
0.30 : 138 *****************************************
0.40 : 151 *********************************************
0.50 : 153 **********************************************
0.60 : 114 **********************************
0.70 : 101 ******************************
0.80 : 51 ***************
0.90 : 38 ***********
>=1.00 : 20 ******
 
Sample size 10000
 
Mean 0.498239 SD 0.249235
 
< 0.00 : 225 *******
0.00 : 333 **********
0.10 : 589 ******************
0.20 : 999 ******************************
0.30 : 1320 ****************************************
0.40 : 1542 **********************************************
0.50 : 1581 ***********************************************
0.60 : 1323 ****************************************
0.70 : 963 *****************************
0.80 : 591 ******************
0.90 : 314 *********
>=1.00 : 220 *******
 
Sample size 100000
 
Mean 0.500925 SD 0.248910
 
< 0.00 : 2173 *******
0.00 : 3192 **********
0.10 : 5938 ******************
0.20 : 9715 *****************************
0.30 : 13351 ****************************************
0.40 : 15399 **********************************************
0.50 : 15680 ***********************************************
0.60 : 13422 ****************************************
0.70 : 9633 *****************************
0.80 : 5993 ******************
0.90 : 3207 **********
>=1.00 : 2297 *******
</pre>
 
9,485

edits