In [12]:
import numpy as np
import scipy.stats as ss
from scipy.stats import norm
from scipy.special import erf,erfc
import matplotlib.pyplot as plt
%matplotlib inline
From Reed $$f(x) = \frac{\alpha\beta}{\alpha+\beta}\left[A(\alpha,\nu,\tau^2)x^{-\alpha-1}\Phi\left(\frac{\log x-\nu-\alpha \tau^2}{\tau} \right)+A(-\beta,\nu,\tau^2)x^{\beta-1}\Phi^c\left(\frac{\log x-\nu+\beta \tau^2}{\tau} \right)\right]$$ where $A(\theta,\nu,\tau^2) = \exp\left(\theta \nu + \theta^2\tau^2/2\right)$. Parameter shapes are something like
In [13]:
def dpln(x,alpha=1,beta=0.5,nu=-1,tau2=1):
A1 = np.exp(alpha*nu+alpha**2*tau2/2)
A2 = np.exp(-beta*nu+beta**2*tau2/2)
term1 = A1*x**(-alpha-1)*norm.cdf((np.log(x)-nu-alpha*tau2)/tau2**0.5)
term2 = A2*x**(beta-1)*norm.sf((np.log(x)-nu+beta*tau2)/tau2**0.5)
fofx = alpha*beta/(alpha+beta)*(term2+term1)
return fofx
This also suggests the Power-log normal, which is just one side of the equation (and also likely needs a proper normalization). $$f(x) = \alpha x^{-\alpha-1}\Phi\left(\frac{\log x-\nu-\alpha \tau^2}{\tau} \right)$$
In [27]:
def pln(x,alpha=1,tau2=1,nu=1):
A1 = np.exp(alpha*nu+alpha**2*tau2/2)
fofx = alpha*x**(-alpha-1)*norm.cdf((np.log(x)-nu-alpha*tau2)/tau2**0.5)
return fofx
In [29]:
trialx = np.logspace(-2,3,100)
pdf = dpln(trialx,alpha=1,nu=-1, beta = 3.0, tau2=0.50)
pdf2 = pln(trialx, alpha=1,nu=-1,tau2=0.5)
plt.loglog(trialx,pdf,label='Double power lognorm')
plt.loglog(trialx,pdf2,label='Power lognorm')
plt.legend(loc=4)
Out[29]:
Using the scipy rv_continuous
class, we can define dPlN
as a subclass.
In [50]:
from dpln_distrib import dpln
Let's load in some data to test it...
In [51]:
from astropy.io.fits import getdata
img = getdata("../testingdata/hd22.13co.intintensity.fits")
data = img[np.isfinite(img)]
scipy's general definitions are a little weird, and dpln
will likely need to be adjusted to some degree (like doing away with nu
and tau2
in favour of the built-ins). For now, I've found it necessary to freeze the loc
parameter to zero. Also parameter constraints should assist with the fitting.
In [65]:
fit = dpln.fit(data, floc=0.0, fscale=1.0)
print(fit)
How did it do?
In [66]:
plt.hist(data, bins=100, normed=1)
print(dpln.ppf(0.05, *fit), dpln.ppf(0.95, *fit))
x = np.linspace(dpln.ppf(0.05, *fit), dpln.ppf(0.95, *fit), 1000)
plt.plot(x, dpln.pdf(x, *fit))
Out[66]:
Clearly, there's still some issues with this fit... Now try the single powerlaw form.
In [136]:
from pln_distrib import pln
In [143]:
fit2 = pln.fit(data, fscale=1.0, floc=0.0)
print(fit2)
In [144]:
plt.hist(data-data.min(), bins=100, normed=1)
print(pln.ppf(0.05, *fit2))
x2 = np.linspace(pln.ppf(0.05, *fit2), pln.ppf(0.95, *fit2), 1000)
plt.plot(x2, pln.pdf(x2, *fit2))
Out[144]:
In [145]:
percents = np.linspace(0.01, 1.0, 100)
plt.plot(percents, pln.ppf(percents, *fit2))
Out[145]:
In [147]:
rv = pln(3.0, 1.0, 1.0)
print(rv.ppf(0.01), rv.ppf(0.95))
x = np.linspace(rv.ppf(0.01), rv.ppf(0.99), 1000)
plt.plot(x, rv.pdf(x))
Out[147]:
In [148]:
plt.plot(x, rv.cdf(x))
Out[148]:
In [149]:
plt.plot(x, rv.logpdf(x))
Out[149]:
In [150]:
plt.plot(np.linspace(0.01,1.0,100), rv.ppf(np.linspace(0.01,1.0,100)))
Out[150]:
In [ ]: