Question Standard Deviation using DataGridview?

rnestor3

Member
Joined
Aug 3, 2013
Messages
14
Programming Experience
Beginner
I'm in a class college and I'm a beginner in programming. I'm trying to make a replica windows statistical calculator and I hit a road bump with the standard deviation button here is my code so far

Private Sub btnsigman1_Click(sender As System.Object, e As System.EventArgs) Handles btnsigman1.Click

Dim totalvalue As Integer
For Each dgvRow As DataGridViewRow In dgstat.Rows
If Not dgvRow.IsNewRow Then
totalvalue += CInt(dgvRow.Cells(1).Value)

End If
Next
txtinput.Text = totalvalue
End Sub
End Class

I've tried a lot from here and can't seem to come out right anyway i go, also there is a button on there Called F-E that i cannot get figured out. I have to turn this in Monday so any help would be really appreciated.
 
I'm not really sure what's wrong here, do you even know how to calculate a standard deviation? Wikipedia says:

For a finite set of numbers, the standard deviation is found by taking the square root of the average of the squared differences of the values from their average value. For example, consider a population consisting of the following eight values:
49f102b5180cc271551b24b6cf684e5c.png


These eight data points have the mean (average) of 5:
b959bb328932092baa95510e53687a8b.png


To calculate the population standard deviation, first compute the difference of each data point from the mean, and square the result of each:
97d5812ea4aa747bfa37805c1ffa35f0.png


Next, compute the average of these values, and take the square root:
09052af40c670479dacadf81b2f450e9.png

Show me some code that at least tries to implement this.
 
Ah, Herman just beat me to this, but here is some more info:-

Hi,

You are already half way there by knowing how to iterate a DataGridView. All you need to do now is to make the correct calculations for Standard Deviation. Have a look here:-

Simple Example of Calculating Standard Deviation

F-E means Fixed to Exponent and is used to display numbers in Scientific Notation. i.e:-

The number 1234 in Scientific Notation means 1.234 x 10 to the Power of 3 which is typically displayed as:-

1.234e+3

Hope that helps.

Cheers,

Ian
 
In laymen's terms:

Deviation = Math.Sqrt(valueSet.Select(Function(n) Math.Abs(n - valueSet.Average()) ^ 2).Average())
 
Last edited:
Sorry guys, I’m trying to find this part of the standarddeviation formula: (x-Avgx)^2
My code is as followed
Private Subbtnsigman1_Click(sender As System.Object, e AsSystem.EventArgs) Handlesbtnsigman1.Click

Dim totalvalue As Integer
For Each dgvRow As DataGridViewRowIn dgstat.Rows
If NotdgvRow.IsNewRow Then
totalvalue += CInt(dgvRow.Cells(1).Value)-rowvalues(0)

End If
Next
txtinput.Text = totalvalue


I tried this and it just gives me the sum of values –avg x.I need each value –avg x. I’m stuck and confused. Sorry for not being morespecific in my last post
 
Hi,

Based on your profile, please can you confirm that you are using the .NET2 Framework within your version of Visual Studio? I ask this since the answer that Herman gave is correct using the right Framework, but it may be that these methods were not available in .NET2. Just guessing here. As it is, you have not mentioned whether you tried Herman's solution or not and whether it worked or not, so what happened when you did so?

Aside from that, you have not shown any deviation from what you posted in post #1, so what did you try? I think I can guess this, but I will let you answer the question.

Cheers,

Ian
 
I don't know what framework I'm in. I don't know what that means. I took the values from the column of numbers I'm working with and divided by the count. Then I realized its taking the sum and just subtracting the average from the total sum. I can't figure out how to take each value and subtract the average. If I can get this to work I know the rest of it
 
I told you I have never did this before...it be like me asking someone that has never taken calculus to compute an integral. I thought this forum was to help people sorry for being stupid Herman
 
The point I was trying to make is in order to solve a higher level problem, you need to understand and apply lower level concepts. All the information you need is a few keywords away, most of it is right here in this thread.

As to your analogy, your question is more akin to you, speaking no Italian at all, asking us to help you write a poem in Italian. I never said you are stupid, I only tried to point you in the right direction.
 
alright, ill post what I've come up with in my code. I feel like im very close to getting what I need but there is something small im missing here.

Private
Sub btnsigman1_Click(sender As System.Object, e As System.EventArgs) Handles btnsigman1.Click

Dim totalValue As Integer 'totalvalue is the sum of the squares of the inputed data grid

For Each dgvRow As DataGridViewRow In dgstat.Rows

If Not dgvRow.IsNewRow Then
totalValue +=
CInt(dgvRow.Cells(1).Value - (dgvRow.Cells(1).Value / RowValues(0)) ^ 2)

End If

Next
txtinput.Text =
Math.Sqrt(totalValue / RowValues(0) - 1)

End Sub
 
Couple of points:

- If the DGV is databound, you should use the .Datasource property to count your aggregates, not the actual DataGridViewRows.

- Otherwise, start by getting an enumerable to work with, instead of trying to run operations directly on the DGV. It's not meant for that. The easiest and clearest way is with Linq.
        Dim colValues As IEnumerable(Of Integer) = From dgvRow As DataGridViewRow In dgstat.Rows
                                                   Where Not dgvRow.IsNewRow
                                                   Select CInt(dgvRow.Cells(1).Value)
If linq is not an option (if you are really stuck in .Net 2.0), then get a List(Of Integer).
        Dim colValues As New List(Of Integer)
        For Each dgvRow As DataGridViewRow In dgstat.Rows
            If Not dgvRow.IsNewRow Then
                colValues.Add(CInt(dgvRow.Cells(1).Value))
            End If
        Next


- Once you have your unadultered values you apply the previous formula:
        ' Squareroot of the average of squared absolute differences between original value and set average.
        Dim stdDeviation As Integer = Math.Sqrt(colValues.Select(Function(x) Math.Abs(x - colValues.Average()) ^ 2).Average())
or in the slightly longer and less correct way (because you enumerate the enumerable twice instead of just once):
        Dim setAverage As Integer = colValues.Average()
        Dim squareDifferences As IEnumerable(Of Integer) = colValues.Select(Function(x) Math.Abs(x - setAverage) ^ 2)
        Dim stdDeviation As Integer = Math.Sqrt(squareDifferences.Average())


That is ALL the code you need.
 
Last edited:
Back
Top