none
Calculating Standard Deviation in Visual Basic (2015)

    Question

  • Hey guys, Eiji here, new to the forums!

    I'm having a lot of trouble here trying to figure out how I can calculate the Standard Deviation. I've looked all over google and other threads including many other sites, and found nothing but bickering, vague concepts, or code that is only applicable in other contexts.

    In my situation here, I am required to find the Lowest Value, Average, and Standard Deviation within the list of values (all inserted within a listbox of course).

    I've basically managed to get the average and lowest number working (through trial and error, i still lack understanding of overall concept of the code)

    Right now, I want to figure out how I can find Standard Deviation, but I don't know how I can line up a list of numbers separately to calculate. In this situation, I first want to take each number separately, subtract the average from each number, and then square the result FOR EACH NUMBER, SEPERATELY. Once I've squared each of the differences, I need to calculate the "variance" which is by adding up the squared differences and dividing them by the number count. Once I've found my variance, I must find the square root of my variance to find the Standard Deviation. This is to those who don't know what Standard Deviation is, but i'm pretty sure you all do, or have the ability to look it up.. So, how do I do that through code? 

    If possible, itd be great if we can work our way with the code I already have set, meaning I wouldnt have to touch any other line BEFORE Line 49, unless if it's just a small minor change like a Declaration Type or anything crucial of that sort. Because the Lowest Value and Average Value is already set stable, so if we can work our way around it and work from Line 49 down, that would be splendid. 

    Here's my code so far: (unfortunately i can't enter an image here yet lol, so here's the imgur link to my code)

    http://imgur.com/uDe8TLw

    I tested my program and all I know is that I'm not getting the standard deviation correctly. 

    Someone please help me, i've tried my best, and I'm really new to VB. My professor is foreign, she does not speak English too well and she likes to play 'blues clues'. I ask her and she LITERALLY would not want to help me, and tells me to figure it out completely on my own. Mind you I pay attention in class, and this material is not covered. I understand her methods and how it pushes us students to play around with vb, but this is far too complex for me for I haven't even covered the basic building blocks to how I can even use loops or arrays etc. 

    Thank you for your time guys!

    __________________________

    UPDATE

    Thanks to Cor Ligthert, dbasnett and Frank L. Smith, I've managed to find the solution! 

    Thread is closed on my end, but for anyone else who may have similar inquiries related to having to calculate Standard Deviation, perhaps in a different format, may also utilize this post for further questions. 

    Awesome forums!! :) 

    • Edited by error.Eiji Thursday, March 30, 2017 11:52 PM
    Thursday, March 30, 2017 9:26 AM

All replies

  • Help with Homework is not given, but you should be able to do it yourself if you know the VB ingredients. 

    This is your base for the description

    https://en.wikipedia.org/wiki/Standard_deviation

    This are the operators you need beside the standard operators (math)

    https://msdn.microsoft.com/en-us/library/b6ex274z.aspx


    And here the calculations you can use for summation using Linq

    https://msdn.microsoft.com/en-us/library/bb918069.aspx

    With this is should for an average student be a piece of cake.


    Success
    Cor


    Thursday, March 30, 2017 10:17 AM
  • This isn't based on what you have already so you'll have to adapt it, but it shows how this can be done.  It takes advantage of Lists.

        Private lbAsNum As List(Of Integer)
        Private lowValue As Integer
        Private highValue As Integer
        Private sumValues As Integer
        Private averageValue As Double
        Private variance As Double
        Private stdDevValue As Double
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'create list of numbers
            lbAsNum = New List(Of Integer)
            For Each itm As String In ListBox1.Items.Cast(Of String)()
                Dim i As Integer
                If Integer.TryParse(itm, i) Then
                    'valid number in listbox
                    lbAsNum.Add(i)
                Else
                    'invalid number
                    Stop 'add error code
                End If
            Next
    
            lowValue = lbAsNum.Min
            highValue = lbAsNum.Max
            sumValues = lbAsNum.Sum
            averageValue = lbAsNum.Average
    
            variance = 0
            For Each i As Integer In lbAsNum
                variance += (i - averageValue) ^ 2
            Next
            variance /= lbAsNum.Count
    
            stdDevValue = Math.Sqrt(variance)
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, March 30, 2017 11:15 AM
  • In addition to db's answer, you might also want to have a look at something that I posted a few days ago:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/a0b32b7c-48f8-470a-8f16-a2e2e7329825/creating-statistical-tests-for-users-to-use-on-an-application?forum=vbgeneral#437bb9b9-1398-4255-9f92-55bf62979493

    ***** EDIT *****

    I forgot: The screenshot in that other one won't work; it's from an older website so here's it is:


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Thursday, March 30, 2017 1:48 PM
  • Help with Homework is not given, but you should be able to do it yourself if you know the VB ingredients. 

    This is your base for the description

    https://en.wikipedia.org/wiki/Standard_deviation

    This are the operators you need beside the standard operators (math)

    https://msdn.microsoft.com/en-us/library/b6ex274z.aspx


    And here the calculations you can use for summation using Linq

    https://msdn.microsoft.com/en-us/library/bb918069.aspx

    With this is should for an average student be a piece of cake.


    Success
    Cor


    Ah, i understand the concept of not helping others with homework. I guess despite having the task for an assignment, the post can be considered for help of one's own curiousity and will to learn! 

    I appreciate  the links given it's helped me further my understanding in multiple concepts. 

    Points up, Cheers!

    Thursday, March 30, 2017 11:42 PM
  • This actually really helped a lot, I couldnt understand it at first because of how different the names were but silly ole me had to scan it a good 20 times to finally understand that all it took was another For statement and apply the same exact code you've supplied to me. I ended up using your given method. I appreciate it BIG TIME man. Points up for you, Cheers!!! 

    Thursday, March 30, 2017 11:44 PM
  • This is rather interesting. I'll be honest, it's kind of over my league, considering the immense amount of code.. I couldnt quite grasp it, haha. I tried following the instructions, pasted the code on a brand new form and hovering my mouse over ss, i couldnt quite get that particular window/menu list of simple statistics to show.. along with that, I got some errors. I fixed most but one (forgot what it was). 

    But I really love the concept of having to preset code for the sake of simplicity. 

    Does Visual basic accept some form of plugin or addons into the software where we can add our own commands into the system? if they do, I would definitely download a plugin that supplies the user with the simple statistics for it helps save a lot of time on the long run. 

    I appreciate your contribution big time, got you a point up as well. thanks Frank!

    Thursday, March 30, 2017 11:48 PM
  • This is rather interesting. I'll be honest, it's kind of over my league, considering the immense amount of code.. I couldnt quite grasp it, haha. I tried following the instructions, pasted the code on a brand new form and hovering my mouse over ss, i couldnt quite get that particular window/menu list of simple statistics to show.. along with that, I got some errors. I fixed most but one (forgot what it was). 

    But I really love the concept of having to preset code for the sake of simplicity. 

    Does Visual basic accept some form of plugin or addons into the software where we can add our own commands into the system? if they do, I would definitely download a plugin that supplies the user with the simple statistics for it helps save a lot of time on the long run. 

    I appreciate your contribution big time, got you a point up as well. thanks Frank!

    Oh, that's to me - I can't tell unless you make it obvious. ;-)

    *****

    I do realize this is an assignment, but I'll put something together tomorrow so that you can download, just so that you'll get an idea.

    Once you "get it", you'll see the power of using a class (or structure, but they're not the same).


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, March 30, 2017 11:56 PM
  • Hi error,

    It seems that you have solved this issue now, please remember to close your thread by marking useful posts as answer so that more community members who face the same issue will search this answer quickly.

    Best regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 31, 2017 5:17 AM
    Moderator
  • This is rather interesting. I'll be honest, it's kind of over my league, considering the immense amount of code.. I couldnt quite grasp it, haha. I tried following the instructions, pasted the code on a brand new form and hovering my mouse over ss, i couldnt quite get that particular window/menu list of simple statistics to show.. along with that, I got some errors. I fixed most but one (forgot what it was). 

    But I really love the concept of having to preset code for the sake of simplicity. 

    Does Visual basic accept some form of plugin or addons into the software where we can add our own commands into the system? if they do, I would definitely download a plugin that supplies the user with the simple statistics for it helps save a lot of time on the long run. 

    I appreciate your contribution big time, got you a point up as well. thanks Frank!

    Eiji,

    I have the project folder zipped up and uploaded here:

    https://fls.exavault.com/share/view/gu2x-fm7w3wva

    Once you download that, extract the zip file somewhere then open it in Visual Studio. When you do, VS will prompt you to convert it to your version because I use a much older version. Just follow the prompts and it should all work ok.

    If you'll then look at Solution Explorer, you'll see this:

    The class is in a different file but that's all there is to that. If you want to have a look at the code then right-click "SimpleStats.vb" and choose "View Code".

    You can run it in Form1 the same way that you'd run any other. I have only the first one set to run:

        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Test01()
            ' Test02()
            ' Test03()
            ' Test04()
    
            Stop
    
        End Sub


    The reason that I did that is because the class has two constructors. One of them uses a collection of nullables and I feel sure that you don't need it, so I didn't want to confuse things.

    Looking only at the first constructor (that is, Sub New), you'll see something that might look confusing but it's actually easy:

    Public Sub New(ByVal values As IEnumerable(Of Decimal))

    The "As IEnumerable(..." bit looks odd I'm sure but it just means this:

    Obviously the calculations only have meaning when you're dealing with more than one value (Decimal here). By making the parameter an IEnumerable(Of NumericTypeHere), I'm telling it to accept any generic collection that implements the IEnumerable interface.

    Clear as mud, eh? ;-)

    This is what I mean:

            ' This will work:
    
            Dim nums1 As New List(Of Decimal) From {1, 2, 3}
            Dim ss As New SimpleStats(nums1)
    
            Stop
    
            ' This will also work:
    
            Dim nums2() As Decimal = {1, 2, 3}
            ss = New SimpleStats(nums2)
    
            Stop
    
            ' This will also work:
    
            Dim nums3 As IEnumerable(Of Decimal) = Nothing
            nums3 = nums1.ToArray
    
            ss = New SimpleStats(nums3)
    
            Stop
    
            ' This will NOT work:
    
            Dim nums4 As New Collection From {1, 2, 3}
            ss = New SimpleStats(nums4)
    
            Stop

    The last one won't work because the collection (nums4) isn't generic and the constructor insists that it is.

    *****

    The best way to understand it is to put a breakpoint in and step into the code line at a time. I think that will give you a good idea of what's going on.

    Obviously it's not something you can use for a scholastic assignment but knowledge is always good. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, March 31, 2017 11:30 AM