In [3]:
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 [4]:
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)$$
If we consider the dpln distribution in the limit as $$\beta \rightarrow \infty,$$ we find the above since dpln may be written as mixture of the limiting cases (and fitted as such).
In [5]:
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 [6]:
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[6]:
Using the scipy rv_continuous
class, we can define dPlN
as a subclass.
In [7]:
from dpln_distrib import dpln
Let's load in some data to test it...
In [8]:
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 [9]:
fit = dpln.fit(data, floc=0.0, fscale=1.0)
print(fit)
How did it do?
In [10]:
plt.hist(data, bins=100, normed=1, alpha=0.1)
print(dpln.ppf(0.05, *fit), dpln.ppf(0.95, *fit))
x = np.linspace(dpln.ppf(0.001, *fit), dpln.ppf(0.95, *fit), 1000)
plt.plot(x, dpln.pdf(x, *fit))
Out[10]:
Clearly, there's still some issues with this fit... Now try the single powerlaw form.
In [11]:
from pln_distrib import pln
In [18]:
data = data[data<15]
fit2 = pln.fit(data, fscale=1.0, floc=0.0)
print(fit2)
In [19]:
plt.hist(data, bins=100, normed=1)
print(pln.ppf(0.01, *fit2))
x2 = np.linspace(pln.ppf(0.01, *fit2), pln.ppf(0.99, *fit2), 1000)
plt.plot(x2, pln.pdf(x2, *fit2))
print(pln.stats(*fit2, moments='mvsk'))
This seems to have worked fairly well. To ensure the code is working properly, let's try out some of the features.
In [20]:
rv = pln(fit2[0], fit2[1], fit2[2], loc=fit2[3], scale=fit2[4])
plt.hist(data, bins=100, color="b", alpha=0.3, normed=1)
plt.hist(rv.rvs(size=5000), bins=200, color="g", alpha=0.4, normed=1)
plt.plot(x2, rv.pdf(x2))
# plt.xlim([0,30])
Out[20]:
In [24]:
plt.plot(x2, rv.cdf(x2))
Out[24]:
This looks pretty good. In log-scale...
In [25]:
plt.hist(data, bins=100, log=True, normed=1, alpha=0.3)
plt.hist(rv.rvs(size=5000), bins=200, color="g", alpha=0.2, normed=1, log=True)
plt.plot(x2, rv.pdf(x2))
plt.xlim([0, 40])
Out[25]:
There's some obvious discrepancies here. The fit hasn't been able to pick up the proper power-law for the tail.
In [23]: