Code Doc
Sep 18th, 2008, 09:07 AM
I put this small program together and thought I would share it with you. For years I taught college statistics and a concept the students often had trouble with was understanding the behavior of the sample mean and the population mean. As the sample mean grows it approaches the population mean and the sampling error shrinks. However, on occasion even with random sampling, the error could increase temporarily. This program illustrates this behavior rather well:
Dim SampTot As Double, SampSize As Long, Value As Single
Dim MaxValue As Double, MinValue As Double, Power As Integer
Const Range = 200
Private Sub Command1_Click()
Do
SampSize = SampSize + 1
Value = Rnd * Range
SampTot = SampTot + Value
If SampSize = 1 Then
MinValue = Value
ElseIf Value < MinValue Then MinValue = Value
End If
If Value > MaxValue Then MaxValue = Value
If SampSize = 2 ^ (Power + 1) Then
Power = Power + 1
With Text1
.Text = .Text & "Population Mean = " & Range / 2 & vbCrLf
.Text = .Text & "Sample Size = " & SampSize & vbCrLf
.Text = .Text & "Sample Mean = " & SampTot / SampSize & vbCrLf
.Text = .Text & "% error = " & Format$(Abs(Range / 2 - SampTot / SampSize) / (Range / 2), "0.000000%") & vbCrLf
.Text = .Text & "Minimum Value = " & MinValue & vbCrLf
.Text = .Text & "Maximum Value = " & MaxValue & vbCrLf & vbCrLf
.SelStart = Len(.Text)
End With
Exit Do
End If
Loop
End Sub
Private Sub Form_Load()
Randomize
Text1.Text = vbNullString
End Sub
Build a form with a command button and text box with its multiline property set to true. Then use the command button to generate sample values within the range of 0 to 200. We know that the population mean is 100. Watch the sample mean approach it with subsequent command button presses. I welcome comments and discussion.
Dim SampTot As Double, SampSize As Long, Value As Single
Dim MaxValue As Double, MinValue As Double, Power As Integer
Const Range = 200
Private Sub Command1_Click()
Do
SampSize = SampSize + 1
Value = Rnd * Range
SampTot = SampTot + Value
If SampSize = 1 Then
MinValue = Value
ElseIf Value < MinValue Then MinValue = Value
End If
If Value > MaxValue Then MaxValue = Value
If SampSize = 2 ^ (Power + 1) Then
Power = Power + 1
With Text1
.Text = .Text & "Population Mean = " & Range / 2 & vbCrLf
.Text = .Text & "Sample Size = " & SampSize & vbCrLf
.Text = .Text & "Sample Mean = " & SampTot / SampSize & vbCrLf
.Text = .Text & "% error = " & Format$(Abs(Range / 2 - SampTot / SampSize) / (Range / 2), "0.000000%") & vbCrLf
.Text = .Text & "Minimum Value = " & MinValue & vbCrLf
.Text = .Text & "Maximum Value = " & MaxValue & vbCrLf & vbCrLf
.SelStart = Len(.Text)
End With
Exit Do
End If
Loop
End Sub
Private Sub Form_Load()
Randomize
Text1.Text = vbNullString
End Sub
Build a form with a command button and text box with its multiline property set to true. Then use the command button to generate sample values within the range of 0 to 200. We know that the population mean is 100. Watch the sample mean approach it with subsequent command button presses. I welcome comments and discussion.