DISTRIBUZIONI E PROBABILITA’

Di seguito vengono riportate, in linguaggio VB.NET, delle funzioni necessarie al calcolo delle distribuzioni di probabilità di alcune variabili aleatorie.

// NORMALE  
    Private Function Normale(ByVal mean As Double, ByVal stdDev As Double) As Double
        'metodo di Marseglia

        Static spare As Double
        Static hasSpare As Boolean = False

        If hasSpare Then
            hasSpare = False
            Return spare * stdDev + mean
        Else
            Dim u, v, s As Double

            Do
                u = rand.NextDouble * 2.0 - 1.0
                v = rand.NextDouble * 2.0 - 1.0
                s = u * u + v * v
            Loop While s >= 1.0 OrElse s = 0.0

            s = Math.Sqrt(-1.0 * Math.Log(s) / s)
            spare = v * s
            hasSpare = True
            Return mean + stdDev * u * s
        End If

    End Function


// LOGNORMNALE        https://www.encyclopediaofmath.org/index.php/Generating_random_variables
    Private Function LogNormale(mean As Double, stdDev As Double)
 As Double
        Return (Math.Exp(Me.Normale(mean, stdDev)))
    End Function

//ESPONENZIALE       https://www.encyclopediaofmath.org/index.php/Generating_random_variables
    Private Function Esponenziale(beta As Double)
 As Double
        Dim U As Double = rand.NextDouble
        Dim out As Double = -beta * Math.Log(U)
        Return out
    End Function

//CHI-QUADRO         https://www.encyclopediaofmath.org/index.php/Generating_random_variables
    Private Function ChiSquare(df As Integer)
 As Double
        Dim somma As Double = 0
        df = CInt(df / 2)

        For d = 1 To df
            somma += Math.Log(rand.NextDouble)
        Next

        Return (-2 * somma)
    End Function


//WEIBULL            https://www.encyclopediaofmath.org/index.php/Generating_random_variables
    Private Function Weibull(gamma As Double, beta As Double)
 As Double
        Return (Math.Pow(Me.Esponenziale(beta), 1 / gamma))
    End Function


//T DI STUDENT       https://www.encyclopediaofmath.org/index.php/Generating_random_variables
    Private Function tStudent(df As Integer)
 As Double
        Return (Me.Normale(0, 1) / Math.Sqrt(Me.ChiSquare(df) / df))
    End Function


//GAMMA              https://www.encyclopediaofmath.org/index.php/Generating_random_variables
    Private Function Gamma(a As Double, b As Double)
 As Double
        Dim somma As Double = 0

        For h As Double = 1 To a
            somma += Math.Log(rand.Next)
        Next

        Return -b * somma

    End Function


//F-FISHER            https://www.encyclopediaofmath.org/index.php/Generating_random_variables
    Private Function FFisher(df1 As Integer, df2 As Integer)
 As Double
        Dim Num As Double = Me.ChiSquare(df1) / df1
        Dim Den As Double = Me.ChiSquare(df2) / df2
        Return (Num / Den)
    End Function


//BERNOULLI
    Private Function Bernoulli(p As Double)
 As Integer
        Dim Risultato As Integer
        Risultato = If(rand.NextDouble > p, 1, 0)
        Return Risultato
    End Function


//IPERGEOMETRICA        https://peteroupc.github.io/randomfunc.html#Hypergeometric_Distribution
    Private Function Ipergeometrica(trials As Integer, ones As Integer, count As Integer)
 As Integer
        If ones < 0 OrElse count < 0 OrElse trials < 0 OrElse ones > count OrElse trials > count Then Me.RichTextBox1.Text = "Errore"

        If ones = 0 Then Return 0

        Dim i As Integer = 0
        Dim successi As Integer = 0
        Dim CountCorrente As Integer = count
        Dim OnesCorrente As Integer = ones

        While i < trials AndAlso OnesCorrente > 0
            If Me.Bernoulli(OnesCorrente / CountCorrente) = 1 Then
                OnesCorrente -= 1
                successi += 1
            End If
            CountCorrente -= 1
            i += 1
        End While
        Return successi
    End Function

//BINOMIALE     https://cs.stackexchange.com/questions/48669/how-to-improve-the-binomial-algorithm
    Private Function Binomiale(n As Integer, k As Integer, p As Double)
 As Double
        If (n = 0 AndAlso k = 0) Then Return 1
        If (n < 0 OrElse k < 0) Then Return 0

        Return (1 - p) * Binomiale(n - 1, k, p) + p * Binomiale(n - 1, k - 1, p)
    End Function

//POISSON        http://www.nrbook.com/devroye/Devroye_files/chapter_ten.pdf   pag504
    Private Function Poisson(beta As Double, lambda As Double) As Double
        Dim X As Double = 0
        Dim Somma As Double = 0
        While True
            Do
                Dim variabile As Double = Me.Esponenziale(beta)
                Somma += variabile
                If Somma < lambda Then
                    X += 1
                Else
                    Return (X)
                End If
            Loop
        End While
    End Function


//PROBABILITA' BINOMIALE
    Private Function ProbabilitaBinomiale(tentativi As Integer, p As Double)
        Dim cont As Integer = 0
        For h As Integer = 1 To tentativi
            If rand.NextDouble > p Then
                cont += 1
            End If
        Next
        Return CDbl(cont / tentativi)
    End Function


//PROBABILITA' POISSON          metodo di MonteCarlo
    Private Function ProbabilitaPoisson(x As Integer, ripetizioni As Integer, beta As Double, lambda As Double) As Double
        Dim contatore As Integer = 0
        For h As Integer = 1 To ripetizioni
            If CInt(Me.Poisson(beta, lambda)) = x Then contatore += 1
        Next

        Return (CDbl(contatore / ripetizioni))
    End Function


//PROBABILITA' IPERGEOMETRICA    metodo di MonteCarlo
    Private Function ProbabilitaIpergeometrica(x As Integer, ripetizioni As Integer, trials As Integer, ones As Integer, count As Integer) As Double
        Dim contatore As Integer = 0
        For h As Integer = 1 To ripetizioni
            If CInt(Me.Ipergeometrica(trials, ones, count)) = x Then contatore += 1
        Next

        Return (CDbl(contatore / ripetizioni))
    End Function

Lascia un commento

Progetta un sito come questo con WordPress.com
Comincia ora