Fit a distribution to values and probability

Viewed 35

I am trying to figure out how I can fit a normal distribution to values with known probability. Let's say I have three realisations:

Probability Value
0.1 1
0.5 4
0.9 7

I would like to fit a normal distribution using scipy.stats. Can someone point me to a code example how it can be done?

Thanks in advance.

---UPDATE---

I reckon I got an idea how it can be done. But the problem is that the normal distribution does not really fit the data. I decided to try to iterate via different distributions till I find best fit (i'd add RMSE as a criteria once I solve the problem below). I am coding something like below but curve_fit complains that it cannot determine number of fit parameters. If I try to use lambda (commented now) it is not happy with *arg. Anyone got an idea what I can do differently?

import numpy as np
import scipy
import scipy.stats as st
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import sys

size=10000

data = np.array([0.114, 0.28, 0.32])
prob = np.array([0.1, 0.5, 0.9])

DISTRIBUTIONS = [
    st.alpha,st.anglit,st.arcsine,st.beta,st.betaprime,st.bradford,st.burr,st.burr12, st.cauchy,st.chi,st.chi2,st.cosine,
    st.erlang,st.expon,st.exponnorm,st.exponweib,st.exponpow,st.f,st.fatiguelife,st.fisk,
    st.foldnorm,st.genlogistic,st.genexpon, st.genpareto,
    st.genextreme,st.gausshyper,st.gamma,st.gengamma,st.genhalflogistic,st.gilbrat,st.gompertz,st.gumbel_r,
    st.gumbel_l,st.halfcauchy,st.halflogistic,st.halfnorm,st.halfgennorm,st.hypsecant,st.invgamma,st.invgauss,
    st.invweibull,st.johnsonsb,st.johnsonsu,st.ksone,st.kstwobign,st.laplace, st.levy, st.levy_l,
    st.logistic,st.loggamma,st.loglaplace,st.lognorm,st.lomax,st.maxwell,st.mielke,st.nakagami,st.ncx2,st.ncf,
    st.nct,st.norm,st.pareto,st.pearson3,st.powerlaw,st.powerlognorm,st.powernorm,st.rdist,st.reciprocal, st.norminvgauss,
    st.rayleigh,st.rice,st.recipinvgauss,st.semicircular,st.t,st.triang,st.truncexpon,st.truncnorm,st.tukeylambda,
    st.uniform,st.vonmises,st.vonmises_line,st.wald,st.weibull_min,st.weibull_max,st.wrapcauchy, st.trapz
]

for distribution in DISTRIBUTIONS:
    
    # f = lambda x,loc,scale, *arg: distribution(loc=loc, scale=scale, *arg).cdf(x)
    params = scipy.optimize.curve_fit(distribution.cdf,data,prob)[0]
    print(params)
    
0 Answers
Related