2015-06-18 2 views
5

Ich bin ein paar Güte der Fit-Tests mit scipy.stats in Python 2.7.10.Getting ein pdf von scipy.stats in einer generischen Weise

for distrName in distrNameList: 
    distr = getattr(distributions, distrName) 
    param = distr.fit(sample) 
    pdf = distr.pdf(???) 

Was gebe ich in distr.pdf() auf die list von Abtastpunkten von Interesse, die Werte des Best-Fit-pdf zu bekommen, abscissas genannt?

+0

[Hier sind alle scipy.stats Verteilungen PDF-Dateien mit Beispielcode.] (http://stackoverflow.com/a/37559471/2087463) – tmthydvnprt

Antwort

4

Um das PDF unter abscissas zu bewerten, würden Sie abcissas als erstes Argument an pdf übergeben. Um die Parameter angeben, use the * operator entpacken Sie die param Tupel und diese Werte distr.pdf passieren:

pdf = distr.pdf(abscissas, *param) 

Zum Beispiel

import numpy as np 
import scipy.stats as stats 

distrNameList = ['beta', 'expon', 'gamma'] 
sample = stats.norm(0, 1).rvs(1000) 
abscissas = np.linspace(0,1, 10) 
for distrName in distrNameList: 
    distr = getattr(stats.distributions, distrName) 
    param = distr.fit(sample) 
    pdf = distr.pdf(abscissas, *param) 
    print(pdf) 
6

Aus der Dokumentation, die .fit() method kehrt:

Form, loc, Maßstab: Tupel von Schwimmern MLEs für beliebige Formstatistiken, gefolgt von denen für Ort und Maßstab.

und die .pdf() method nimmt:

x: array_like Quantile

arg1, arg2, arg3, ...: array_like der Formparameter (n) für die Verteilung (siehe docstring des Instanzobjekts für weitere Informationen)

loc: array_like, optional Positionsparameter (Standard = 0)

Skala: array_like, optional

So im Wesentlichen würden Sie so etwas tun:

import numpy as np 
from scipy import stats 
from matplotlib import pyplot as plt 


# some random variates drawn from a beta distribution 
rvs = stats.beta.rvs(2, 5, loc=0, scale=1, size=1000) 

# estimate distribution parameters, in this case (a, b, loc, scale) 
params = stats.beta.fit(rvs) 

# evaluate PDF 
x = np.linspace(0, 1, 1000) 
pdf = stats.beta.pdf(x, *params) 

# plot 
fig, ax = plt.subplots(1, 1) 
ax.hold(True) 
ax.hist(rvs, normed=True) 
ax.plot(x, pdf, '--r') 

enter image description here

+0

sehr nett, +1 für Grafiken und Ausarbeitung, aber im Wesentlichen hat dies nicht zur Antwort des unutbu hinzugefügt - oder irre ich mich? – gt6989b

+0

Nein, nicht wirklich - ich hatte keine Zeit, um meine Antwort zu tippen, bevor unutbu seine –

+0

geschrieben habe ich +1, aber leider kann ich nicht akzeptieren ... vielen Dank – gt6989b

Verwandte Themen