Seite 1 von 1

Normalverteilung Dichtefunktion

Verfasst: Donnerstag 1. Februar 2018, 16:12
von nono191
Mein Problem ist, dass ich ein Programm schreiben will dass die Dichte berechnet, jedoch gibt es immer falsche Ergebnisse und ich konnte keinen Fehler finden.

Code: Alles auswählen

import math
import scipy.stats as stats


class NormalDistribution:

    def __init__(self, mu, sigma_square):

        self.mu = mu
        self.sigma = math.sqrt(sigma_square)

    def pdf(self, x):
        return 1 / (self.sigma * math.sqrt(2 * math.pi)) * math.exp(-(math.pow(x - self.mu, 2) / (2 * math.pow(self.sigma, 2))))


print(NormalDistribution(0, 3).pdf(1))
print(stats.norm.pdf(1, 0, 3))

Code: Alles auswählen

Output:
0.19496965572274114 # Ausgabe des Programms
0.12579440923099774 # Korrekte Ausgabe
Ich wäre sehr dankbar wenn mir jemand auf die Sprünge helfen könnte.

LG nono

Re: Normalverteilung Dichtefunktion

Verfasst: Donnerstag 1. Februar 2018, 21:55
von nono191
P.S Die Formel ist Bild

Re: Normalverteilung Dichtefunktion

Verfasst: Donnerstag 1. Februar 2018, 22:40
von Sirius3
@nono191: wie kommst Du auf die Idee, dass Du sigma^2 angeben mußt?

Code: Alles auswählen

import math

def pdf(x, mu, sigma):
    return math.exp(-(x - mu)**2 / (2 * sigma**2)) / (sigma * (2 * math.pi)**0.5)

print(pdf(1, 0, 3))

Re: Normalverteilung Dichtefunktion

Verfasst: Freitag 2. Februar 2018, 13:05
von nono191
Hallo Sirius,
Erstmal danke für deine Antwort.

Ich habe gedacht, dass man die Varianz angeben muss.

Danke
nono