giovedì 3 aprile 2014

ESEMPIO IN VB.NET PER CALCOLARE MEDIA E VARIANZA

Qui di seguito, è riportato un esempio di calcolo della running mean, della running variance e della running covariance.

Public Class Form1
        Dim Count As Integer
        Dim AvgL As Double
        Dim AvgK As Double
        Dim PrevAvgL As Double
        Dim PrevAvgK As Double
        Dim VarL As Double
        Dim VarK As Double
        Dim Cov As Double
        Dim L As New List(Of Integer) 'List of exams by L             
End Class
    Private Sub Button1_Click(sender As Object, e As EventArgs)Handles Button1.Click
        Dim K As New List(Of Integer)  'List of exams by K          
 
        L.Add(28)
        L.Add(30)
        L.Add(23)
        L.Add(27)
        L.Add(28)
        L.Add(23)
        L.Add(30)
L.Add(27)
L.Add(25)


        K.Add(18)
        K.Add(22)
        K.Add(23)
        K.Add(27)
        K.Add(24)
        K.Add(25)
        K.Add(28)
                K.Add(21)
                K.Add(19)




                For i As Integer = 0 To L.Count - 1
            PrevAvgL = AvgL
            PrevAvgK = AvgK
            Count = Count + 1
            AvgL = ((Count-1)*AvgL+L(i))/Count
            AvgK = ((Count-1)*AvgK+K(i))/Count
            VarL = ((Count-1)*VarL+(L(i)-PrevAvgL)*(L(i)-AvgL))/Count
            VarK = ((Count-1)*VarK+(K(i)-PrevAvgK)*(K(i)-AvgK))/Count
            Cov = ((Count-1)*Cov+(L(i)-PrevAvgL)*(K(i)*AvgK))/Count


    End Sub

Nessun commento:

Posta un commento