python - Custom PDF from scipy.stats.rv_continuous unwanted upper-bound -
i attempting generate random probability density function of qso's of luminosity form:
1/( (l/l_b^* )^alpha + (l/l_b^* )^beta )
where l_b^*, alpha, , beta constants. this, following code used:
import scipy.stats st loglbreak = 43.88 alpha = 3.4 beta = 1.6 class my_pdf(st.rv_continuous): def _pdf(self,l_l): #"l_l" in log l l = 10**(l_l/loglbreak) d = 1/(l**alpha + l**beta) return d dist_log_l = my_pdf(momtype = 0, = 0,name='l_l_dist') distro = dist_log_l.rvs(size = 10000)
(l/l^* rased power of 10 since being done in log scale)
the distribution supposed produce graph approximates this, trailing off infinity, in reality graph produces looks this (10,000 samples). upper bound same regardless of amount of samples used. there reason being restricted in way is?
your pdf not normalized. integral of pdf on domain must 1. pdf integrates approximately 3.4712:
in [72]: scipy.integrate import quad in [73]: quad(dist_log_l._pdf, 0, 100) out[73]: (3.4712183965415373, 2.0134487716044682e-11) in [74]: quad(dist_log_l._pdf, 0, 800) out[74]: (3.4712184965748905, 2.013626296581202e-11) in [75]: quad(dist_log_l._pdf, 0, 1000) out[75]: (3.47121849657489, 8.412130378805368e-10)
this break class's implementation of inverse transform sampling. generate samples domain integral of pdf 0 x first reaches 1.0, in case 2.325
in [81]: quad(dist_log_l._pdf, 0, 2.325) out[81]: (1.0000875374350238, 1.1103202107010366e-14)
that is, in fact, see in histogram.
as quick fix verify issue, modified return
statement of _pdf()
method to:
return d/3.47121849657489
and ran script again. (in real fix, value function of other parameters.) commands
in [85]: import matplotlib.pyplot plt in [86]: plt.hist(distro, bins=31)
generates plot:
Comments
Post a Comment