ADD:
ruff for code formatting BIC statistic AND BIC test implemented test_distributions.py for test new created dists with pytest REFACTOR: k_gen pdf changed from 2 params to generalized
This commit is contained in:
@@ -1,43 +1,51 @@
|
||||
from scipy.stats import rv_continuous
|
||||
from scipy.special import gamma, kv, gammaln
|
||||
from scipy.special import kv, gammaln
|
||||
from scipy.stats._distn_infrastructure import _ShapeInfo
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class k_gen(rv_continuous):
|
||||
"""K distribution for radar clutter modeling.
|
||||
"""Generalized K distribution for radar clutter modeling.
|
||||
|
||||
The probability density function is::
|
||||
|
||||
f(x; nu, b) = 2/Gamma(nu) * b^((nu+1)/2) * x^((nu-1)/2) * K_{nu-1}(2*sqrt(b*x))
|
||||
f(x; mu, alpha, beta) = 2 / (Gamma(alpha) * Gamma(beta))
|
||||
* (alpha*beta/mu)^((alpha+beta)/2)
|
||||
* x^((alpha+beta)/2 - 1)
|
||||
* K_{alpha-beta}(2*sqrt(alpha*beta*x/mu))
|
||||
|
||||
for x >= 0, where K_{nu-1} is the modified Bessel function of the second
|
||||
kind of order nu-1, nu > 0 is the shape parameter and b > 0 is the rate
|
||||
parameter.
|
||||
for x > 0, where K_{alpha-beta} is the modified Bessel function of the
|
||||
second kind of order alpha-beta, mu > 0 is the mean, and alpha > 0,
|
||||
beta > 0 are shape parameters.
|
||||
|
||||
The standard (2-parameter) K distribution is the special case beta=1,
|
||||
with nu=alpha and b=alpha/mu.
|
||||
"""
|
||||
|
||||
def _shape_info(self):
|
||||
return [_ShapeInfo("nu", domain=(0, np.inf), inclusive=(False, True)),
|
||||
_ShapeInfo("b", domain=(0, np.inf), inclusive=(False, True))]
|
||||
return [
|
||||
_ShapeInfo("mu", domain=(0, np.inf), inclusive=(False, True)),
|
||||
_ShapeInfo("alpha", domain=(0, np.inf), inclusive=(True, True)),
|
||||
_ShapeInfo("beta", domain=(0, np.inf), inclusive=(True, True)),
|
||||
]
|
||||
|
||||
def _argcheck(self, mu, alpha, beta):
|
||||
return (mu > 0) & (alpha > 0) & (beta > 0)
|
||||
|
||||
def _argcheck(self, nu, b):
|
||||
return (nu > 0) & (b > 0)
|
||||
def _pdf(self, x, mu, alpha, beta):
|
||||
return np.exp(self._logpdf(x, mu, alpha, beta))
|
||||
|
||||
def _pdf(self, x, nu, b):
|
||||
# k.pdf(x, nu, b) = 2/Gamma(nu) * b^((nu+1)/2) * x^((nu-1)/2) * K_{nu-1}(2*sqrt(b*x))
|
||||
return np.exp(self._logpdf(x, nu, b))
|
||||
|
||||
def _logpdf(self, x, nu, b):
|
||||
def _logpdf(self, x, mu, alpha, beta):
|
||||
z = 2.0 * np.sqrt(alpha * beta * x / mu)
|
||||
return (
|
||||
np.log(2.0)
|
||||
- gammaln(nu)
|
||||
+ (nu + 1) / 2.0 * np.log(b)
|
||||
+ (nu - 1) / 2.0 * np.log(x)
|
||||
+ np.log(kv(nu - 1, 2.0 * np.sqrt(b * x)))
|
||||
- gammaln(alpha)
|
||||
- gammaln(beta)
|
||||
+ (alpha + beta) / 2.0 * np.log(alpha * beta / mu)
|
||||
+ ((alpha + beta) / 2.0 - 1.0) * np.log(x)
|
||||
+ np.log(kv(alpha - beta, z))
|
||||
)
|
||||
|
||||
|
||||
k_dist = k_gen(a=0.0, name="k_distribution", shapes="nu, b")
|
||||
k_dist = k_gen(a=0.0, name="k_distribution", shapes="mu, alpha, beta")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user