Python package for statistical analysis of Normal Mean or/and Variance Mixtures.
Requirements:
pip install requirements.txt
For contributing:
pip install requirements.dev.txt
numpy~=1.26.4
scipy~=1.13.1
matplotlib~=3.8.4
numba~=0.59.0
mpmath~=1.3.0
Development:
mypy~=1.10.0
black~=24.4.2
isort~=5.13.2
mpmath~=1.3.0
pytest~=7.4.4
Problem with classical representation is that it is not unique. So usually it is more convenient to make some substitutions and get canonical representation of mixture.
where
Normal Mean or/and Variance Mixtures have useful applications in statistical analysis and financial math.
Classes are implemented in directory src.mixtures .
There are three classes of mixtures:
One can select mixture representation (classical or canonical) by specifying mixture_form parameter.
Example of mixture object creation:
from src.mixtures.nm_mixture import *
from scipy.stats import norm
mixture = NormalMeanVarianceMixtures("classical", alpha=1.2, beta=2.2, gamma=1, distribution=norm)
Example of mixture sample generation:
from src.generators.nm_generator import NMGenerator
from src.mixtures.nm_mixture import *
from scipy.stats import expon
generator = NMGenerator()
mixture = NormalMeanMixtures("classical", alpha=1.6, beta=4, gamma=1, distribution=expon)
sample = generator.classical_generate(mixture, 5000)
- Calculation of pobability density function
compute_pdf()
- Calculation of logarithm of pobability density function
compute_logpdf()
- Cumulative distribution function
compute_cdf()
- Moments calculation
compute_moment()
Computes given integral with the limits of integration being 0 and 1 using Randomized Quasi-Monte Carlo method.
func
: integrated function
error_tolerance
: pre-specified error tolerance
count
: number of rows of random values matrix
base_n
: number of columns of random values matrix
i_max
: allowed number of cycles
a
: parameter for quantile of normal distribution
from src.algorithms.support_algorithms.rqmc import RQMC
rqmc = RQMC(lambda x: x**3 - x**2 + 1, error_tolerance=1e-5)
So rqmc()[0]
is estimated integral value and rqmc()[1]
is current error tolerance.
-
Semiparametric estimation of parameter
in Normal Mean-Variance mixture. Parameter
estimation for
whereAlgorithm was created by Belomestny & Panov(2017)
Parameters
m
- Search area radius
tolerance
- Defines error tolerance to stop bisection algorithm
max_iterations
- Maximum allowed iterations for bisection algorithm
omega
- Lipschitz continuous odd function onwith compact support\ Algorithm returns estimated
value and estimation successfulness. Usage
from scipy.stats import halfnorm from src.estimators.semiparametric.nmv_semiparametric_estimator import NMVSemiParametricEstimator from src.generators.nmv_generator import NMVGenerator from src.mixtures.nmv_mixture import NormalMeanVarianceMixtures # Generate sample of NMV-mixture with parameter mu = 2 to test the algorithm mixture = NormalMeanVarianceMixtures("canonical", alpha=0, mu=2, distribution=halfnorm) sample = NMVGenerator().canonical_generate(mixture, 1000) # Create new estimator for NMV-mixture and select an algorithm by its name estimator = NMVSemiParametricEstimator("mu_estimation", {"m": 10, "tolerance": 10 ** -5}) estimate_result = estimator.estimate(sample) print("Estimated mu value: ", estimate_result.value, "\nEstimation is successful: ", estimate_result.success)
Output:
Estimated mu value: 2.0798295736312866 Estimation is successful: True
-
Estimation of mixing density for given
in Normal Mean-Variance mixture. Mixing density function
estimation for
whereAlgorithm was created by Belomestny & Panov(2017)
Parameters
mu
- Parameterof NMV-mixture
gmm
- Parameterfor Belomestny & Panov(2017) algorithm
u_value
- Parameterfor Belomestny & Panov(2017) algorithm
v_value
- Parameterfor Belomestny & Panov(2017) algorithm
x_data
- Points to estimate functionAlgorithm returns list of estimated g(x) values and estimation successfulness.
Usage:
For demonstration purposes to use the algorithm we can generate sample from NMV mixture with parameter
, size 25 values and mixing density function from scipy.stats import expon from src.generators.nmv_generator import NMVGenerator from src.mixtures.nmv_mixture import NormalMeanVarianceMixtures real_g = expon.pdf # Real g(x) mixture = NormalMeanVarianceMixtures("canonical", alpha=0, mu=1, distribution=expon) sample = NMVGenerator().canonical_generate(mixture, 25)
Now we assume that we know nothing about
, and try to estimate it in points 0.5, 1 and 2. from src.estimators.semiparametric.nmv_semiparametric_estimator import NMVSemiParametricEstimator x_data = [0.5, 1, 2] # x points, where to estimate g(x) estimator = NMVSemiParametricEstimator( "g_estimation_given_mu", {"x_data": x_data, "u_value": 7.6, "v_value": 0.9} ) est = estimator.estimate(sample) # est is EstimateResult object
Result of the estimation can be accesses by
est.list_value
x = 0.5, Real g(x) value: 0.6065 Estimated: 0.9727 x = 1, Real g(x) value: 0.3678 Estimated: 0.5227 x = 2, Real g(x) value: 0.1353 Estimated: 0.1866
- Hintz, E., Hofert, M., & Lemieux, C. (2019). Normal variance mixtures: Distribution, density and parameter estimation. Comput. Stat. Data Anal., 157, 107175.
- Belomestny, D., & Panov, V. (2017). Semiparametric estimation in the normal variance-mean mixture model. Statistics, 52, 571 - 589.
- Andreev Sergey - andreev-sergej
- Knyazev Dmitrii - Engelsgeduld