Results 1 to 4 of 4

Thread: VB6 and the Sample Mean

  1. #1

    Thread Starter
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    VB6 and the Sample Mean

    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:
    Code:
    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.
    Doctor Ed

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 and the Sample Mean

    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
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: VB6 and the Sample Mean

    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.
    Doctor Ed

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VB6 and the Sample Mean

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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