Speed#

This benchmark focuses on comparing the speed of pyMultiFit’s statistical distribution generations compared to the well-established SciPy library. The focus is on ensuring that pyMultiFit provides reliably faster generation that closely match or are better than SciPy running time.

What is Tested?#

  • Probability Density Function (PDF): Evaluates the time taken to compute the PDF for a set of input values.

  • Cumulative Distribution Function (CDF): Measures the performance of computing cumulative probabilities.

Benchmark setup#

To test accuracy:

  • Both pyMultiFit and SciPy are run on the same input data, using distributions like Gaussian, Beta, and Laplace.

  • Results are compared using metrics such as absolute error, relative error, and visual plots.

  • A range of parameter values and edge cases are included to evaluate robustness and consistency.#%% md

Testing#

[1]:
import numpy as np
import scipy.stats as ss

import pymultifit.distributions as pd
from functions import cdf_pdf_plots
[2]:
np.random.seed(43)
n_points_list = np.logspace(0.1, 6, num=50, dtype=int)

normal(loc=0, scale=1)#

[3]:
custom_dist = pd.GaussianDistribution(normalize=True)
scipy_dist = ss.norm()

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Gausian N(0, 1)")
_images/_bm_speed_5_0.png
_images/_bm_speed_5_1.png

normal(loc=3, scale=0.1)#

[4]:
custom_dist = pd.GaussianDistribution(mean=3, std=0.1, normalize=True)
scipy_dist = ss.norm(loc=3, scale=0.1)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Gaussian N(3, 0.1)")
_images/_bm_speed_7_0.png
_images/_bm_speed_7_1.png

laplace(loc=0, scale=1)#

[5]:
custom_dist = pd.LaplaceDistribution(normalize=True)
scipy_dist = ss.laplace()

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Laplace L(0, 1)")
_images/_bm_speed_9_0.png
_images/_bm_speed_9_1.png

laplace(loc=-3, scale=3)#

[6]:
custom_dist = pd.LaplaceDistribution(mean=-3, diversity=3, normalize=True)
scipy_dist = ss.laplace(loc=-3, scale=3)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Laplace L(-3, 3)")
_images/_bm_speed_11_0.png
_images/_bm_speed_11_1.png

skewnorm(a=1, loc=0, scale=1)#

[7]:
custom_dist = pd.SkewNormalDistribution(normalize=True)
scipy_dist = ss.skewnorm(a=1)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "SkewNorm S(1, 0, 1)")
_images/_bm_speed_13_0.png
_images/_bm_speed_13_1.png

skewnorm(a=3, loc=-3, scale=0.5)#

[8]:
custom_dist = pd.SkewNormalDistribution(shape=3, location=-3, scale=0.5, normalize=True)
scipy_dist = ss.skewnorm(a=3, loc=-3, scale=0.5)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "SkewNorm S(3, -3, 0.5)")
_images/_bm_speed_15_0.png
_images/_bm_speed_15_1.png

lognorm(s=1, loc=0, scale=1)#

[9]:
custom_dist = pd.LogNormalDistribution(mean=1, std=1, loc=0, normalize=True)
scipy_dist = ss.lognorm(s=1, loc=0, scale=1)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "LogNorm L(1, 0, 1)")
_images/_bm_speed_17_0.png
_images/_bm_speed_17_1.png

lognorm(s=3, loc=-5, scale=0.5)#

[10]:
custom_dist = pd.LogNormalDistribution(mean=0.5, std=3, loc=-5, normalize=True)
scipy_dist = ss.lognorm(s=3, loc=-5, scale=0.5)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "LogNorm L(3, -5, 0.5)")
_images/_bm_speed_19_0.png
_images/_bm_speed_19_1.png

beta(a=1, b=1)#

[11]:
custom_dist = pd.BetaDistribution(alpha=1, beta=1, normalize=True)
scipy_dist = ss.beta(a=1, b=1)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Beta B(1, 1)")
_images/_bm_speed_21_0.png
_images/_bm_speed_21_1.png

beta(a=5, b=80, loc=-3, scale=5)#

[12]:
custom_dist = pd.BetaDistribution(alpha=5, beta=80, loc=-3, scale=5.6, normalize=True)
scipy_dist = ss.beta(a=5, b=80, loc=-3, scale=5.6)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Beta B(5, 80, -3, 5.6)")
_images/_bm_speed_23_0.png
_images/_bm_speed_23_1.png

arcsine()#

[13]:
custom_dist = pd.ArcSineDistribution(normalize=True)
scipy_dist = ss.arcsine()

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "ArcSine AS()")
/home/syedalimohsinbukhari/PycharmProjects/pyMultiFit/src/pymultifit/distributions/utilities_d.py:120: RuntimeWarning: invalid value encountered in power
  numerator = y**(alpha - 1) * (1 - y)**(beta - 1)
_images/_bm_speed_25_1.png
_images/_bm_speed_25_2.png

arcsine(loc=3, scale=2.2)#

[14]:
custom_dist = pd.ArcSineDistribution(loc=3, scale=2.2, normalize=True)
scipy_dist = ss.arcsine(loc=3, scale=2.2)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "AcrSine AS(3, 2.2)")
_images/_bm_speed_27_0.png
_images/_bm_speed_27_1.png

gamma(a=1, loc=0, scale=1)#

[15]:
custom_dist = pd.GammaDistributionSS(shape=1, scale=1, loc=0, normalize=True)
scipy_dist = ss.gamma(a=1, loc=0, scale=1)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Gamma G(1, 0, 1)")
_images/_bm_speed_29_0.png
_images/_bm_speed_29_1.png

gamma(a=1.27, loc=-3, scale=1.5)#

[16]:
custom_dist = pd.GammaDistributionSS(shape=1.27, scale=1.5, loc=-3, normalize=True)
scipy_dist = ss.gamma(a=1.27, loc=-3, scale=1.5)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Gamma G(1.27, -3, 1.5)")
_images/_bm_speed_31_0.png
_images/_bm_speed_31_1.png

\(\chi^2\)(dof=1)#

[17]:
custom_dist = pd.ChiSquareDistribution(degree_of_freedom=1, normalize=True)
scipy_dist = ss.chi2(df=1)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Chi2 C(1)")
_images/_bm_speed_33_0.png
_images/_bm_speed_33_1.png

\(\chi^2\)(dof=2, loc=-2.3, scale=0.3)#

[18]:
custom_dist = pd.ChiSquareDistribution(degree_of_freedom=2, loc=-2.3, scale=0.3, normalize=True)
scipy_dist = ss.chi2(df=2, loc=-2.3, scale=0.3)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Chi2 (2, -2.3, 0.3")
_images/_bm_speed_35_0.png
_images/_bm_speed_35_1.png

\(\chi^2\)(dof=2.5, loc=-2.3, scale=0.3)#

[19]:
custom_dist = pd.ChiSquareDistribution(degree_of_freedom=2.5, loc=-2.3, scale=0.3, normalize=True)
scipy_dist = ss.chi2(df=2.5, loc=-2.3, scale=0.3)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Chi2 (2.5, -2.3, 0.3")
_images/_bm_speed_37_0.png
_images/_bm_speed_37_1.png

foldnorm(mean=2)#

[20]:
custom_dist = pd.FoldedNormalDistribution(mean=2, normalize=True)
scipy_dist = ss.foldnorm(c=2)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "FoldNormal FN(2)")
_images/_bm_speed_39_0.png
_images/_bm_speed_39_1.png

foldnorm(mean=2, loc=1.4, scale=4)#

[21]:
custom_dist = pd.FoldedNormalDistribution(mean=2, sigma=4, loc=1.4, normalize=True)
scipy_dist = ss.foldnorm(c=2, loc=1.4, scale=4)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "FoldNormal FN(2, 1.4, 4)")
_images/_bm_speed_41_0.png
_images/_bm_speed_41_1.png

halfNormal(scale=2)#

[22]:
custom_dist = pd.HalfNormalDistribution(scale=2, normalize=True)
scipy_dist = ss.halfnorm(scale=2)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "HalfNormal HN(2)")
_images/_bm_speed_43_0.png
_images/_bm_speed_43_1.png

halfnorm(loc=-3, scale=1.3)#

[23]:
custom_dist = pd.HalfNormalDistribution(loc=-3, scale=1.3, normalize=True)
scipy_dist = ss.halfnorm(loc=-3, scale=1.3)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "HalfNormal HN(-3, 1.3)")
_images/_bm_speed_45_0.png
_images/_bm_speed_45_1.png

expon(scale=1.4)#

[24]:
custom_dist = pd.ExponentialDistribution(scale=1.4, normalize=True)
scipy_dist = ss.expon(scale=1/1.4)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Exponential Exp(1.4)")
_images/_bm_speed_47_0.png
_images/_bm_speed_47_1.png

expon(loc=-3, scale=1.4)#

[25]:
custom_dist = pd.ExponentialDistribution(scale=1.4, loc=-3, normalize=True)
scipy_dist = ss.expon(scale=1/1.4, loc=-3)

cdf_pdf_plots(custom_dist, scipy_dist, n_points_list, "Exponential Exp(-3, 1.4)")
_images/_bm_speed_49_0.png
_images/_bm_speed_49_1.png