PDA

Click to See Complete Forum and Search --> : VB6 and the Sample Mean


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.

RobDog888
Sep 18th, 2008, 09:50 AM
Moved to CodeBank

I moved your thread since it wasnt a question or problem over to our CodeBank for finished tips, tricks and other cool code.

Thanks for the entry :thumb:

Code Doc
Sep 18th, 2008, 12:48 PM
OK, RobDog, I wasn't sure if code that was not thoroughly tested was supposed to go to the Codebank thread, but I did run it through several iterations. Anyway, I kind of wish I had something like this to illustrate the principle when I was lecturing in the classroom.

Note that we are sampling essentially from an infinite population, which makes it a bit theoretical compared to actual practice, but there are real world examples of sampling from huge populations. It might also be interesting to count the number of times that the same value is selected at random more than once. However, that would require an array, be rather memory intensive with huge samples, and would definitely slow down the processing.

Another approach would be to first generate a large finite population of values and then extract samples from it without replacement until all are selected.

RobDog888
Sep 18th, 2008, 01:16 PM
No prob. Most entries are not 100% bug free. Its ok as when members test it out and find a bug they will post it in your thread. Sometimes they have suggestions and improvements so its ok to post replies like that too.