### Quickstart model fitting and prediction Source: https://erdogant.github.io/distfit/pages/html/Installation.html Demonstrates initializing the model, fitting empirical data, and predicting values for new data points. ```python from distfit import distfit import numpy as np X = np.random.normal(0, 2, 1000) y = [-8,-6,0,1,2,3,4,5,6] # Initialize model dfit = distfit() # Find best theoretical distribution for empirical data X dfit.fit_transform(X) dfit.plot() # Make prediction dfit.predict(y) dfit.plot() ``` -------------------------------- ### Initialize distfit with distribution selection Source: https://erdogant.github.io/distfit/pages/html/_sources/Parametric.rst.txt Examples of initializing the distfit model with manual, popular, or full distribution sets. ```python # Load library from distfit import distfit # Initialize model and test only for normal distribution dfit = distfit(distr='norm') # Set multiple distributions to test for dfit = distfit(distr=['norm','t']) ``` ```python # Initialize model and select popular distributions dfit = distfit(distr='popular') ``` ```python # Initialize model and select all distributions dfit = distfit(distr='full') ``` -------------------------------- ### import_example Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Imports a pre-defined example dataset from the GitHub source. ```APIDOC ## import_example(data='gas_spot_price') ### Description Imports an example dataset directly from the GitHub source. ### Parameters - **data** (str) - Optional - The name of the dataset to import. Options: 'gas_spot_price', 'tips', 'occupancy', 'predictive_maintenance', 'multi_normal', 'multi_t'. ``` -------------------------------- ### Install distfit via pip Source: https://erdogant.github.io/distfit/pages/html/Installation.html Provides commands to install the package from PyPI or directly from the GitHub repository. ```bash # The installation from pypi is disabled: pip install distfit # Install directly from github pip install git+https://github.com/erdogant/distfit ``` -------------------------------- ### Uninstall distfit environment Source: https://erdogant.github.io/distfit/pages/html/Installation.html Lists and removes the conda environment used for the distfit installation. ```bash # List all the active environments. distfit should be listed. conda env list # Remove the distfit environment conda env remove --name env_distfit # List all the active environments. distfit should be absent. conda env list ``` -------------------------------- ### Visualize Distributions with distfit Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Demonstrates how to initialize the distfit model, fit data, and generate CDF and PDF plots. ```python >>> from distfit import distfit >>> import numpy as np >>> >>> # Create dataset >>> X = np.random.normal(0, 2, 1000) >>> >>> # Initialize >>> dfit = distfit() >>> >>> # Fit >>> dfit.fit_transform(X) >>> >>> # Make CDF plot >>> fig, ax = dfit.plot(chart='cdf') >>> >>> # Append the PDF plot >>> dfit.plot(chart='pdf', fig=fig, ax=ax) >>> >>> # Plot the CDF of the top 10 fitted distributions. >>> fig, ax = dfit.plot(chart='cdf', n_top=10) >>> # Append the PDF plot >>> dfit.plot(chart='pdf', n_top=10, fig=fig, ax=ax) >>> ``` -------------------------------- ### Initialize and Fit Quantile Model Source: https://erdogant.github.io/distfit/pages/html/_sources/Quantile.rst.txt Demonstrates initializing the distfit model with the quantile method, fitting it to random normal data, and accessing the resulting confidence interval bounds. ```python # Load library from distfit import distfit # Initialize model with quantile method dfit = distfit(method='quantile') # Some random data X = np.random.normal(10, 3, 2000) y = [3,4,5,6,10,11,12,18,20] # Compute quantiles based on data dfit.fit_transform(X) # Some results about the CII print(dfit.model['CII_min_alpha']) # > 5.024718707579791 # Some results print(dfit.model['CII_max_alpha']) # > 15.01373120064936 # Plot dfit.plot() ``` -------------------------------- ### Initialize and Fit Distribution Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Demonstrates how to initialize the distfit model, fit it to a normal distribution dataset, and generate synthetic data. ```python >>> from distfit import distfit >>> import numpy as np >>> >>> # Create dataset >>> X = np.random.normal(0, 2, 1000) >>> y = [-8,-6,0,1,2,3,4,5,6] >>> >>> # Initialize >>> dfit = distfit() >>> # Fit >>> dfit.fit_transform(X) >>> >>> # Create syntethic data using fitted distribution. >>> Xnew = dfit.generate(10) >>> ``` -------------------------------- ### Perform distribution fitting and visualization Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Demonstrates initializing the distfit model, fitting data, and generating summary and PDF plots. ```python >>> from distfit import distfit >>> import numpy as np >>> >>> X = np.random.normal(0, 2, 1000) >>> y = [-8,-6,0,1,2,3,4,5,6] >>> >>> dfit = distfit() >>> results = dfit.fit_transform(X) >>> >>> # Plot summary >>> dfit.plot_summary() >>> >>> # PDF plot >>> dfit.plot() >>> >>> # Make prediction >>> results_proba = dfit.predict(y) >>> >>> # Plot PDF >>> fig, ax = dfit.plot(chart='pdf') >>> >>> # Add the CDF to the plot >>> fig, ax = dfit.plot(chart='cdf', n_top=1, ax=ax) >>> >>> # QQ-plot for top 10 fitted distributions >>> fig, ax = dfit.qqplot(X, n_top=10) >>> ``` -------------------------------- ### Initialize distfit with full distribution set Source: https://erdogant.github.io/distfit/pages/html/Parametric.html Use the 'full' keyword to test against the complete list of supported univariate distributions. ```python # Initialize model and select all distributions dfit = distfit(distr='full') ``` -------------------------------- ### Generate QQ-plots with distfit Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Demonstrates how to initialize the distfit model, fit it to a dataset, and generate QQ-plots for the best-fitted models. ```python >>> from distfit import distfit >>> import numpy as np >>> >>> # Create dataset >>> X = np.random.normal(0, 2, 1000) >>> >>> # Initialize >>> dfit = distfit() >>> >>> # Fit >>> dfit.fit_transform(X) >>> >>> # Make qq-plot >>> dfit.qqplot(X) >>> >>> # Make qq-plot for top 10 best fitted models. >>> dfit.qqplot(X, n_top=10) >>> ``` -------------------------------- ### Initialize distfit with popular distributions Source: https://erdogant.github.io/distfit/pages/html/Parametric.html Use the 'popular' keyword to test against a curated set of common probability distributions. ```python # Initialize model and select popular distributions dfit = distfit(distr='popular') ``` -------------------------------- ### distfit.__init__ Source: https://erdogant.github.io/distfit/pages/html/Parametric.html Initializes the distfit model with configuration parameters. ```APIDOC ## distfit(method='parametric', alpha=0.05, multtest='fdr_bh', bins=50, bound='both', distr='popular', n_perm=10000) ### Description Initializes the distfit model object with specific configuration parameters for distribution fitting. ### Parameters - **method** (str) - Optional - Specify the method type: 'parametric', 'empirical'. Default: 'parametric'. - **alpha** (float) - Optional - Significance alpha. Default: 0.05. - **multtest** (str) - Optional - Multiple test correction method. Default: 'fdr_bh'. - **bins** (int) - Optional - To determine the empirical histogram. Default: 50. - **bound** (int) - Optional - Directionality to test for significance. Default: 'both'. - **distr** (str) - Optional - The set of distributions to test. Default: 'popular'. - **n_perm** (int) - Optional - Number of permutations to model null-distribution if method is 'empirical'. Default: 10000. ``` -------------------------------- ### Initialize and Fit Percentile Model Source: https://erdogant.github.io/distfit/pages/html/Percentile.html Demonstrates initializing the distfit model with the percentile method, fitting it to random normal data, and accessing the calculated confidence interval bounds. ```python # Load library from distfit import distfit # Initialize model with percentile method dfit = distfit(method='percentile') # Some random data X = np.random.normal(10, 3, 2000) y = [3,4,5,6,10,11,12,18,20] # Compute Percentiles based on data dfit.fit_transform(X) # Some results about the CII print(dfit.model['CII_min_alpha']) # > 4.0714359161939235 # Some results print(dfit.model['CII_max_alpha']) # > 16.00598292777584 # Plot dfit.plot() ``` -------------------------------- ### Initialize distfit with manual distribution selection Source: https://erdogant.github.io/distfit/pages/html/Parametric.html Specify one or multiple distributions by passing a string or a list of strings to the distr parameter. ```python # Load library from distfit import distfit # Initialize model and test only for normal distribution dfit = distfit(distr='norm') # Set multiple distributions to test for dfit = distfit(distr=['norm','t']) ``` -------------------------------- ### Fit distributions and perform bootstrapping Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Initialize the distfit object, fit data, and optionally perform bootstrapping to evaluate distribution fit quality. ```python >>> # Import library >>> from distfit import distfit >>> >>> # Initialize without permutations >>> dfit = distfit() >>> >>> # Random data >>> # X = np.random.exponential(0.5, 10000) >>> # X = np.random.uniform(0, 1000, 10000) >>> X = np.random.normal(163, 10, 10000) >>> >>> # Fit without permutations >>> results = dfit.fit_transform(X) >>> >>> # Results are stored in summary >>> dfit.summary[['name', 'score', 'bootstrap_score', 'bootstrap_pass']] >>> >>> # Create summary plot (no bootstrap is present) >>> dfit.plot_summary() >>> >>> results = dfit.bootstrap(X, n_boots=100) >>> >>> # Create summary plot (the bootstrap is automatically added to the plot) >>> dfit.plot_summary() ``` -------------------------------- ### Validate distribution fit using bootstrapping Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Shows how to initialize the model with bootstrapping enabled and inspect the validation results. ```python >>> # Import library >>> from distfit import distfit >>> >>> # Initialize with 100 permutations >>> dfit = distfit(n_boots=100) >>> >>> # Random data >>> # X = np.random.exponential(0.5, 10000) >>> # X = np.random.uniform(0, 1000, 10000) >>> X = np.random.normal(163, 10, 10000) >>> >>> results = dfit.fit_transform(X) >>> >>> # Results are stored in summary >>> dfit.summary[['name', 'score', 'bootstrap_score', 'bootstrap_pass']] >>> >>> # Create summary plot >>> dfit.plot_summary() ``` -------------------------------- ### Configure Logger Verbosity Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Demonstrates how to set the logging level to 'warning' and trigger various log messages. ```python >>> # Set the logger to warning >>> set_logger(verbose='warning') >>> >>> # Test with different messages >>> logger.debug("Hello debug") >>> logger.info("Hello info") >>> logger.warning("Hello warning") >>> logger.critical("Hello critical") >>> ``` -------------------------------- ### Project file structure Source: https://erdogant.github.io/distfit/pages/html/Coding%20quality.html Displays the directory layout of the distfit package. ```text path/to/distfit/ ├── .gitignore ├── docs │   ├── conf.py │   ├── index.rst │   └── ... ├── LICENSE ├── MANIFEST.in ├── distfit │   ├── __init__.py │   ├── __version__.py │   └── distfit.py ├── README.md ├── requirements.txt ├── setup.py └── tests ├── __init__.py └── test_distfit.py ``` -------------------------------- ### Generate Basic PDF and CDF Plots Source: https://erdogant.github.io/distfit/pages/html/Plots.html Initializes the distfit model, fits data, and generates separate PDF and CDF charts. ```python # Import from distfit import distfit import numpy as np # Create dataset X = np.random.normal(0, 2, 10000) y = [-8,-6,0,1,2,3,4,5,6] # Initialize dfit = distfit(alpha=0.01) # Fit dfit.fit_transform(X) # Plot seperately fig, ax = dfit.plot(chart='pdf') fig, ax = dfit.plot(chart='cdf') ``` -------------------------------- ### Initialize and Fit Model Source: https://erdogant.github.io/distfit/pages/html/_sources/Examples.rst.txt Initialize the distfit object and perform a fit on empirical data to find the best theoretical distribution. ```python # Initialize dfit = distfit(todf=True) # Search for best theoretical fit on your empirical data dfit.fit_transform(X) # Make prediction on new datapoints based on the fit results = dfit.predict(y) ``` -------------------------------- ### Initialize Multivariate distfit Model Source: https://erdogant.github.io/distfit/pages/html/_sources/multivariate.rst.txt Configure the distfit object for multivariate analysis by setting the multivariate parameter to True. ```python dfit = distfit( multivariate=True, distr='norm', method='mle', bins=50, alpha=0.05 ) ``` -------------------------------- ### Fit distributions using different methods Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Demonstrates fitting data using parametric, quantile, or percentile methods and generating predictions. ```python >>> from distfit import distfit >>> import numpy as np >>> >>> # Create dataset >>> X = np.random.normal(0, 2, 1000) >>> y = [-8,-6,0,1,2,3,4,5,6] >>> >>> # Default method is parametric. >>> dfit = distfit() >>> >>> # In case of quantile >>> dfit = distfit(method='quantile') >>> >>> # In case of percentile >>> dfit = distfit(method='percentile') >>> >>> # Fit using method >>> model_results = dfit.fit_transform(X) >>> >>> dfit.plot() >>> >>> # Make prediction >>> results = dfit.predict(y) >>> >>> # Plot results with CII and predictions. >>> dfit.plot() ``` -------------------------------- ### Generate random data for distribution fitting Source: https://erdogant.github.io/distfit/pages/html/_sources/Examples.rst.txt Initializes sample data using numpy for testing distribution fitting. ```python from distfit import distfit import numpy as np # Example data X = np.random.normal(10, 3, 2000) y = [3,4,5,6,10,11,12,18,20] ``` -------------------------------- ### Create conda environment Source: https://erdogant.github.io/distfit/pages/html/Installation.html Sets up an isolated Python 3.6 environment for distfit. ```bash conda create -n env_distfit python=3.6 conda activate env_distfit ``` -------------------------------- ### Perform bootstrapping validation with distfit Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Initializes the distfit model with bootstrapping enabled, fits data, and visualizes the stability of the distributions. ```python # Import library from distfit import distfit # Initialize with 100 permutations dfit = distfit(n_boots=100) # Generate data from PDF # X = np.random.exponential(0.5, 10000) # X = np.random.uniform(0, 1000, 10000) X = np.random.normal(163, 10, 10000) # Fit model results = dfit.fit_transform(X) # Results are stored in summary dfit.summary[['name', 'score', 'bootstrap_score', 'bootstrap_pass']] # Create summary plot dfit.plot_summary() ``` -------------------------------- ### Fit Discrete Model Source: https://erdogant.github.io/distfit/pages/html/Discrete.html Initialize distfit with the discrete method and fit the binomial distribution to the generated data. ```python # Import distfit from distfit import distfit # Initialize for discrete distribution fitting dfit = distfit(method='discrete') # Run distfit to and determine whether we can find the parameters from the data. dfit.fit_transform(X) # Get the model and best fitted parameters. print(dfit.model) # Best fitted n=8 and p=0.4999 which is great because the input was n=8 and p=0.5 dfit.model['n'] dfit.model['p'] ``` -------------------------------- ### Generate sample data Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Creates a small dataset from a normal distribution for testing purposes. ```python # Generate data X = np.random.normal(0, 2, 100) ``` -------------------------------- ### Configure Parallel Computing in Distfit Source: https://erdogant.github.io/distfit/pages/html/_sources/Examples.rst.txt Initializes the distfit model with parallel processing enabled for bootstrapping. Ensure n_jobs is set to allocate cores effectively for performance gains. ```python start_time = time.time() # Initialization dfit = distfit(distr='popular', n_boots=50, n_jobs=8, verbose='info') # Fit dfit.fit_transform(X) # Compute time elapsed_time = time.time() - start_time print(elapsed_time) ``` -------------------------------- ### Fit distributions to empirical data Source: https://erdogant.github.io/distfit/pages/html/_sources/Examples.rst.txt Initializes the distfit class and performs a fit on the provided data to find the best theoretical distribution based on RSS. ```python # From the distfit library import the class distfit from distfit import distfit # Initialize dfit = distfit(todf=True) # Search for best theoretical fit on your empirical data results = dfit.fit_transform(X) # [distfit] >fit.. # [distfit] >transform.. # [distfit] >[norm ] [0.00 sec] [RSS: 0.0036058] [loc=10.035 scale=2.947] # [distfit] >[expon ] [0.00 sec] [RSS: 0.1821936] [loc=-0.496 scale=10.531] # [distfit] >[pareto ] [0.12 sec] [RSS: 0.1821326] [loc=-699709.530 scale=699709.035] # [distfit] >[dweibull ] [0.02 sec] [RSS: 0.0059431] [loc=10.001 scale=2.541] # [distfit] >[t ] [0.09 sec] [RSS: 0.0036059] [loc=10.035 scale=2.947] # [distfit] >[genextreme] [0.27 sec] [RSS: 0.7053157] [loc=17.658 scale=2.731] # [distfit] >[gamma ] [0.07 sec] [RSS: 0.0036036] [loc=-326.130 scale=0.026] # [distfit] >[lognorm ] [0.15 sec] [RSS: 0.0036144] [loc=-187.018 scale=197.039] # [distfit] >[beta ] [0.05 sec] [RSS: 0.0036176] [loc=-16.974 scale=51.538] # [distfit] >[uniform ] [0.00 sec] [RSS: 0.1162497] [loc=-0.496 scale=19.280] # [distfit] >[loggamma ] [0.07 sec] [RSS: 0.0036382] [loc=-493.477 scale=77.133] # [distfit] >Compute confidence interval [parametric] ``` -------------------------------- ### distfit Initialization Source: https://erdogant.github.io/distfit/pages/html/_sources/Parametric.rst.txt The distfit class is initialized with various parameters to configure the distribution fitting process. ```APIDOC ## distfit Initialization ### Description Initialize the distfit model with specific parameters to control the fitting behavior, significance testing, and distribution selection. ### Parameters - **method** (str) - Optional - Specify the method type: 'parametric', 'empirical'. Default: 'parametric'. - **alpha** (float) - Optional - Significance alpha. Default: 0.05. - **multtest** (str) - Optional - Multiple test correction method. Options include: 'None', 'bonferroni', 'sidak', 'holm-sidak', 'holm', 'simes-hochberg', 'hommel', 'fdr_bh', 'fdr_by', 'fdr_tsbh', 'fdr_tsbky'. Default: 'fdr_bh'. - **bins** (int) - Optional - To determine the empirical histogram. Default: 50. - **bound** (str) - Optional - Directionality to test for significance. Options: 'both', 'up', 'high', 'right', 'down', 'low', 'left'. Default: 'both'. - **distr** (str) - Optional - The set of distributions to test. Options: 'popular', 'full', or specific distributions like 't', 'norm'. Default: 'popular'. - **n_perm** (int) - Optional - Number of permutations to model null-distribution in case of method is 'empirical'. Default: 10000. ### Example ```python # Initialize model and select popular distributions dfit = distfit(alpha=0.01) ``` ``` -------------------------------- ### Import distfit class Source: https://erdogant.github.io/distfit/pages/html/_sources/Examples.rst.txt Basic import statement for the distfit library. ```python # From the distfit library import the class distfit from distfit import distfit ``` -------------------------------- ### Compare smoothing parameters Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Demonstrates the effect of the smooth parameter on model fitting by comparing a default fit with a heavily smoothed fit. ```python # Fit model without smoothing model = distfit() model.fit_transform(X) model.plot() # Fit model with heavy smoothing model = distfit(smooth=10) model.fit_transform(X) model.plot() ``` -------------------------------- ### Fit Discrete Model Source: https://erdogant.github.io/distfit/pages/html/_sources/Discrete.rst.txt Initializes the distfit object with the discrete method and fits the model to the input data X to retrieve parameters. ```python # Import distfit from distfit import distfit # Initialize for discrete distribution fitting dfit = distfit(method='discrete') # Run distfit to and determine whether we can find the parameters from the data. dfit.fit_transform(X) # [distfit] >fit.. # [distfit] >transform.. # [distfit] >Fit using binomial distribution.. # [distfit] >[binomial] [SSE: 7.79] [n: 8] [p: 0.499959] [chi^2: 1.11] # [distfit] >Compute confidence interval [discrete] # Get the model and best fitted parameters. print(dfit.model) # {'model': , # 'params': (8, 0.4999585504197037), # 'name': 'binom', # 'SSE': 7.786589839641551, # 'chi2r': 1.1123699770916502, # 'n': 8, # 'p': 0.4999585504197037, # 'CII_min_alpha': 2.0, # 'CII_max_alpha': 6.0} # Best fitted n=8 and p=0.4999 which is great because the input was n=8 and p=0.5 dfit.model['n'] dfit.model['p'] ``` -------------------------------- ### Generate QQ Plots Source: https://erdogant.github.io/distfit/pages/html/Plots.html Creates Quantile-Quantile plots to assess distribution fit. ```python # Plot seperately fig, ax = dfit.qqplot(X) fig, ax = dfit.qqplot(X, n_top=11) ``` -------------------------------- ### Perform Multivariate Fitting and Analysis Source: https://erdogant.github.io/distfit/pages/html/_sources/multivariate.rst.txt Complete workflow for loading data, fitting the model, evaluating joint density, generating synthetic samples, and detecting outliers. ```python from distfit import distfit # Initialize with multivariate mode dfit = distfit(multivariate=True) # Load example data X = dfit.import_example(data='multi_normal') # X = dfit.import_example(data='multi_t') # Fit model dfit.fit_transform(X) # Access estimated correlation matrix (Gaussian copula) print(dfit.model.corr) # Evaluate joint density results = dfit.evaluate_pdf(X) print(results['score']) print(results['copula_density']) # Generate synthetic samples Xnew = dfit.generate(n=10) # Detect multivariate outliers bool_outliers = dfit.predict_outliers(X) ``` -------------------------------- ### Initialize and fit normal distribution Source: https://erdogant.github.io/distfit/pages/html/_sources/Univariate.rst.txt Generate random normal data and fit a specific normal distribution model to estimate parameters. ```python from distfit import distfit import numpy as np # Initialize model and specify distribution to be normal X = np.random.normal(0, 2, 5000) ``` ```python dfit = distfit(distr='norm') dfit.fit_transform(X) print(dfit.model) # {'name': 'norm', # 'score': 0.0008652960233748114, # 'loc': -0.03151369033439687, # 'scale': 2.0045971659158206, # 'arg': (), # 'params': (-0.03151369033439687, 2.0045971659158206), # 'model': , # 'bootstrap_score': 0, # 'bootstrap_pass': None, # 'color': '#e41a1c', # 'CII_min_alpha': -3.3287826092676776, # 'CII_max_alpha': 3.265755228598883} ``` -------------------------------- ### qqplot(X, *args, **kwargs) Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Generates a QQ-plot to compare the sample distribution against a theoretical distribution. ```APIDOC ## qqplot(X, *args, **kwargs) ### Description Plots QQ-plot results to visualize how well the data fits the distribution. ### Parameters - **X** (array-like) - Required - The Null distribution or background data. - **line** (str) - Optional - Reference line type ('45', 's', 'r', 'q', 'None'). - **n_top** (int) - Optional - Number of top results to show. - **title** (str) - Optional - Plot title. - **fontsize** (int) - Optional - Font size for axis and ticks. - **figsize** (tuple) - Optional - Figure dimensions. ``` -------------------------------- ### Print model summary Source: https://erdogant.github.io/distfit/pages/html/Parametric.html Display the summary of fits across various distributions. ```python print(dfit.summary) # name RSS ... scale arg # 0 gamma 0.00185211 ... 0.0370159 (3004.147964288284,) # 1 t 0.00186936 ... 2.02883 (2517332.591227023,) # 2 norm 0.00186945 ... 2.02882 () # 3 beta 0.00186949 ... 37.7852 (39.068072383763294, 46.06165256503778) # 4 lognorm 0.00197359 ... 57.4149 (0.03537982752374607,) # 5 genextreme 0.00297519 ... 2.0106 (0.2437702978900108,) # 6 dweibull 0.00695379 ... 1.73297 (1.2545534252305621,) # 7 uniform 0.241881 ... 14.1011 () # 8 expon 0.353202 ... 6.99491 () # 9 pareto 0.634924 ... 1.42095 (0.5384782616155881,) ``` -------------------------------- ### Initialize distfit with multiple testing correction Source: https://erdogant.github.io/distfit/pages/html/Parametric.html Configure the distfit object with a specific multiple testing correction method and alpha level. ```python # Initialize dfit = distfit(multtest='holm', alpha=0.01) ``` -------------------------------- ### Initialize distfit model Source: https://erdogant.github.io/distfit/pages/html/Parametric.html Initialize the distfit object with custom parameters such as alpha. ```python # Initialize model and select popular distributions dfit = distfit(alpha=0.01) ``` -------------------------------- ### Fit distribution and predict Source: https://erdogant.github.io/distfit/pages/html/Examples.html Initializes the distfit class, fits the model to data, and predicts probabilities for new points. ```python # From the distfit library import the class distfit from distfit import distfit # Initialize dfit = distfit(todf=True) # Search for best theoretical fit on your empirical data dfit.fit_transform(X) # Make prediction on new datapoints based on the fit results = dfit.predict(y) ``` -------------------------------- ### Fit specific or multiple distributions Source: https://erdogant.github.io/distfit/pages/html/_sources/Examples.rst.txt Demonstrates how to restrict the fitting process to a single distribution or a specific list of distributions. ```python # Create random data X = np.random.normal(10, 3, 2000) y = [3,4,5,6,10,11,12,18,20] # Initialize dfit = distfit(distr='norm') # Fit on data results = dfit.fit_transform(X) # [distfit] >fit.. # [distfit] >transform.. # [distfit] >[norm] [RSS: 0.0151267] [loc=0.103 scale=2.028] dfit.plot() ``` ```python # Create random data X = np.random.normal(10, 3, 2000) y = [3,4,5,6,10,11,12,18,20] # Initialize dfit = distfit(distr=['norm', 't', 'uniform']) # Fit on data results = dfit.fit_transform(X) # [distfit] >fit.. # [distfit] >transform.. # [distfit] >[norm ] [0.00 sec] [RSS: 0.0012337] [loc=0.005 scale=1.982] # [distfit] >[t ] [0.12 sec] [RSS: 0.0012336] [loc=0.005 scale=1.982] # [distfit] >[uniform] [0.00 sec] [RSS: 0.2505846] [loc=-6.583 scale=15.076] # [distfit] >Compute confidence interval [parametric] dfit.plot() ``` -------------------------------- ### Generate Random Discrete Data Source: https://erdogant.github.io/distfit/pages/html/Discrete.html Create a sample dataset of 10,000 integers following a binomial distribution. ```python # Generate random numbers from scipy.stats import binom # Set parameters for the test-case n = 8 p = 0.5 # Generate 10000 samples of the distribution of (n, p) X = binom(n, p).rvs(10000) print(X) ``` -------------------------------- ### distfit.distfit Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Initializes the distfit object for probability density fitting. Users can configure the method (parametric, non-parametric), distribution selection, and scoring statistics. ```APIDOC ## class distfit.distfit(method='parametric', distr='popular', stats='RSS', bins='auto', bound='both', alpha=0.05, n_boots=None, smooth=None, n_perm=10000, todf=False, weighted=True, f=1.5, mhist='numpy', cmap='Set1', random_state=None, multtest=None, multivariate=False, n_jobs=1, verbose='info') ### Description Initializes the main distfit class to perform probability density fitting on univariate random variables. ### Parameters - **method** (str) - Optional - Fitting method: 'parametric', 'non-parametric', or 'discrete'. - **distr** (str) - Optional - Distribution selection strategy. - **stats** (str) - Optional - Scoring statistic for goodness-of-fit (e.g., 'RSS', 'Wasserstein', 'KS'). - **alpha** (float) - Optional - Significance level for tests. - **n_boots** (int) - Optional - Number of bootstraps for validation. ``` -------------------------------- ### Load a distfit model Source: https://erdogant.github.io/distfit/pages/html/Save%20and%20Load.html Restores a previously saved model from a pickle file. ```python # Initialize dfit = distfit() # Load model dfit.load('my_first_model.pkl') ``` -------------------------------- ### distfit.load Source: https://erdogant.github.io/distfit/pages/html/_sources/Functions.rst.txt Loads a previously saved distfit model from a file. ```APIDOC ## distfit.load ### Description Loads a previously saved distfit model from a file. ### Method Method call on distfit instance. ``` -------------------------------- ### Visualize Fitted Distributions Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Shows how to visualize fitted distributions using the lineplot method, including optional prediction plotting. ```python >>> from distfit import distfit >>> import numpy as np >>> >>> # Create dataset >>> X = np.random.normal(0, 2, 1000) >>> >>> # Initialize >>> dfit = distfit() >>> >>> # Fit >>> dfit.fit_transform(X) >>> >>> # Make line plot >>> dfit.lineplot(X) >>> >>> # Make line plot >>> dfit.predict([0, 1, 2, 3, 4, 5]) >>> dfit.lineplot(X) ``` -------------------------------- ### Unit test session output Source: https://erdogant.github.io/distfit/pages/html/Coding%20quality.html Shows the console output from running pytest on the distfit library. ```text ============================ test session starts ============================ platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: d:\stack\PYTHON\REPOSITORIES\distfit collected 1 item tests\test_distfit.py . [100%] ============================ warnings summary =============================== tests/test_distfit.py::test_distfit tests/test_distfit.py::test_distfit tests/test_distfit.py::test_distfit tests/test_distfit.py::test_distfit tests/test_distfit.py::test_distfit tests/test_distfit.py::test_distfit ====================== 1 passed, 8 warnings in 15.59s ======================= ``` -------------------------------- ### Initialize Normal Distribution Data Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Generate random samples from a normal distribution to serve as input for the model. ```python from distfit import distfit import numpy as np # Initialize model and specify distribution to be normal X = np.random.normal(0, 2, 5000) ``` -------------------------------- ### load Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Loads a previously saved model from a file. ```APIDOC ## load(filepath) ### Description Loads a learned model from a pickle file. ### Parameters - **filepath** (str) - Required - Path to the stored pickle file. - **verbose** (int) - Optional - Verbosity level. ``` -------------------------------- ### save Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Saves the learned model to a pickle file. ```APIDOC ## save(filepath, overwrite=True, allow_external=False) ### Description Persists the learned model state into a pickle file for later use. ### Parameters - **filepath** (str) - Required - Pathname to store the pickle file. - **overwrite** (bool) - Optional - Whether to overwrite existing files. Default is False. - **allow_external** (bool) - Optional - Allow saving outside safe directories. Default is False. - **verbose** (int) - Optional - Verbosity level. Default is 3. ### Response - **Returns** (object) - The saved model object. ``` -------------------------------- ### Visualize distribution fit results Source: https://erdogant.github.io/distfit/pages/html/_sources/Examples.rst.txt Generates plots to visualize the fitted distribution and the summary of RSS values. ```python # Plot dfit.plot() ``` ```python # Make plot dfit.plot_summary() ``` -------------------------------- ### distfit.bootstrap Source: https://erdogant.github.io/distfit/pages/html/_sources/Functions.rst.txt Performs bootstrapping to estimate the uncertainty of the distribution parameters. ```APIDOC ## distfit.bootstrap ### Description Performs bootstrapping to estimate the uncertainty of the distribution parameters. ### Method Method call on distfit instance. ``` -------------------------------- ### Analyze integer fitting performance with varying smoothing windows Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Iterates through different sample sizes and smoothing windows to evaluate the RSS score of a uniform distribution fit on integer data. ```python import matplotlib.pyplot as plt from tqdm import tqdm import pandas as pd import numpy as np from distfit import distfit # Sample sizes samples = np.arange(250, 20000, 250) # Smooting windows smooth_window=[None, 2, 4, 6, 8, 10] # Figure plt.figure(figsize=(15,10)) # Iterate over smooting window for smooth in tqdm(smooth_window): # Fit only for the uniform distribution dfit = distfit(distr='uniform', smooth=smooth, stats='RSS', verbose=0) # Estimate paramters for the number of samples out = [] # Iterate over sample sizes for s in samples: X = np.random.randint(0, 100, s) dfit.fit_transform(X) out.append([dfit.model['score'], dfit.model['name'], s]) df = pd.DataFrame(out, columns=[dfit.stats,'name','samples']) ax=df[dfit.stats].plot(grid=True, label='smooth: '+str(smooth) + ' - RSS: ' + str(df[dfit.stats].mean())) ax.set_xlabel('Nr.Samples') ax.set_ylabel('RSS') ax.set_xticks(np.arange(0,len(samples))) ax.set_xticklabels(samples.astype(str)) ax.set_ylim([0, 0.001]) ax.legend() ``` -------------------------------- ### Evaluate Multiple PDFs Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Fit multiple distributions to the data and display the summary of goodness of fit scores. ```python dfit = distfit() dfit.fit_transform(X) print(dfit.summary) # distr score ... scale arg # 0 norm 0.00215419 ... 2.021 () # 1 t 0.00215429 ... 2.02105 (2734197.302263666,) # 2 gamma 0.00216592 ... 0.00599666 (113584.76147029496,) # 3 beta 0.00220002 ... 39.4803 (46.39522231565038, 47.98055489856441) # 4 lognorm 0.00226011 ... 139.173 (0.014515926633415211,) # 5 genextreme 0.00370334 ... 2.01326 (0.2516817342848604,) # 6 dweibull 0.00617939 ... 1.732 (1.2681369071313497,) # 7 uniform 0.244839 ... 14.3579 () # 8 pareto 0.358765 ... 2.40844e+08 (31772216.567824945,) # 9 expon 0.360553 ... 7.51848 () ``` -------------------------------- ### density(X, bins='auto', mhist='numpy') Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Compute density based on input data and number of bins. ```APIDOC ## density(X, bins='auto', mhist='numpy') ### Description Compute density based on input data and number of bins. ### Parameters - **X** (array-like) - Required - Set of values belonging to the data - **bins** (int, default: 'auto') - Optional - Bin size to determine the empirical histogram ('auto' or specific integer) - **mhist** (str, default: 'numpy') - Optional - The density extraction method ('numpy' or 'seaborn') ### Returns - **binedges** (array-like) - Array with the bin edges. - **histvals** (array-like) - Array with the histogram density values. ``` -------------------------------- ### Fit data to the model Source: https://erdogant.github.io/distfit/pages/html/Parametric.html Use fit_transform to process input data X and populate the model with output variables. ```python # Initialize model and select popular distributions dfit.fit_transform(X) ``` -------------------------------- ### distfit.save Source: https://erdogant.github.io/distfit/pages/html/_sources/Functions.rst.txt Saves the current distfit model state to a file. ```APIDOC ## distfit.save ### Description Saves the current distfit model state to a file. ### Method Method call on distfit instance. ``` -------------------------------- ### distfit binomial fitting Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Parameters and return values for the binomial distribution fitting function. ```APIDOC ## distfit binomial fitting ### Description Fits a binomial distribution to the provided histogram data. ### Parameters - **hist** (array-like) - Required - Histogram as int array with counts, array index as bin. - **weighted** (Bool) - Optional (default: True) - If True, uses weighted fitting for best fit; if False, uses minimum residual sum of squares (RSS). - **f** (float) - Optional (default: 1.5) - Search range factor for n (n0/f to n0*f). ### Returns #### model (dict) - **distrObject** - Fitted binomial model. - **name** (String) - Name of the fitted distribution. - **RSS** (float) - Best RSS score. - **n** (int) - Binomial n value. - **p** (float) - Binomial p value. - **chi2r** (float) - Reduced chi-squared value. #### figdata (dict) - **sses** (array-like) - Computed RSS scores. - **Xdata** (array-like) - Input data. - **hist** (array-like) - Fitted histogram. - **Ydata** (array-like) - Probability mass function. - **nvals** (array-like) - Evaluated n values. ``` -------------------------------- ### Save a distfit model Source: https://erdogant.github.io/distfit/pages/html/Save%20and%20Load.html Persists a fitted model to a pickle file. ```python from distfit import distfit import numpy as np # Example data X = np.random.normal(0, 2, 5000) y = [-8,-6,0,1,2,3,4,5,6] dfit = distfit() dfit.fit_transform(X) dfit.predict(y) # Save model dfit.save('my_first_model.pkl') ``` -------------------------------- ### Plot PDF Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Visualize the fitted probability density function. ```python dfit.plot(chart='pdf') ``` -------------------------------- ### distfit.plot(chart='pdf', ...) Source: https://erdogant.github.io/distfit/pages/html/distfit.distfit.html Visualizes the fitted distribution. This method supports both univariate and multivariate cases and returns the matplotlib figure and axes objects. ```APIDOC ## distfit.plot ### Description Creates visualizations of the fitted distribution based on the specified chart type (PDF or CDF). It allows for detailed customization of plot properties including colors, line styles, and axis labels. ### Parameters - **chart** (str) - Optional - Type of chart to display: 'pdf' or 'cdf'. Default: 'pdf'. - **n_top** (int) - Optional - Number of top distributions to show in plots. Default: 1. - **title** (str) - Optional - Plot title. - **emp_properties** (dict) - Optional - Properties for empirical data visualization. - **pdf_properties** (dict) - Optional - Properties for theoretical distribution curves. - **bar_properties** (dict) - Optional - Properties for histogram bars. - **cii_properties** (dict) - Optional - Properties for confidence interval indicators. - **fontsize** (int) - Optional - Font size for axis labels and tick marks. Default: 16. - **xlabel** (str) - Optional - X-axis label. Default: 'Density'. - **ylabel** (str) - Optional - Y-axis label. Default: 'Frequency'. - **figsize** (tuple) - Optional - Figure dimensions (width, height). Default: (20, 15). - **xlim** (tuple) - Optional - X-axis limits (min, max). - **ylim** (tuple) - Optional - Y-axis limits (min, max). - **fig** (matplotlib.figure.Figure) - Optional - Matplotlib figure object. - **ax** (matplotlib.axes.Axes) - Optional - Matplotlib axes object. - **grid** (bool) - Optional - Whether to display grid lines. Default: True. - **cmap** (str) - Optional - Colormap for multiple CDF plots. - **verbose** (str/int) - Optional - Verbosity level for logging messages. ### Returns - **fig** (matplotlib.figure.Figure) - The matplotlib figure object. - **ax** (matplotlib.axes.Axes) - The matplotlib axes object. ``` -------------------------------- ### Apply Smoothing to Histogram Fitting Source: https://erdogant.github.io/distfit/pages/html/_sources/Univariate.rst.txt Demonstrates the effect of the smooth parameter on model fitting when dealing with low sample sizes. ```python # Generate data X = np.random.normal(0, 2, 100) ``` ```python # Fit model without smoothing model = distfit() model.fit_transform(X) model.plot() # Fit model with heavy smoothing model = distfit(smooth=10) model.fit_transform(X) model.plot() ``` -------------------------------- ### Estimate parameters across varying sample sizes Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Iteratively fits a normal distribution to datasets of increasing size to observe parameter convergence. ```python # Initialize without verbose. dfit = distfit(verbose=0) # Create random data with varying number of samples samples = np.arange(250, 10000, 250) # Initialize model distr='norm' # Estimate parameters for the number of samples for s in samples: print(s) X = np.random.normal(0, 2, s) dfit.fit_transform(X) out.append([dfit.model['loc'], dfit.model['scale'], dfit.model['name'], s]) ``` -------------------------------- ### Access Evaluation Results Source: https://erdogant.github.io/distfit/pages/html/_sources/multivariate.rst.txt Retrieve copula density and score metrics from the evaluation results. ```python results = dfit.evaluate_pdf(X) # Output results['copula_density'] results['score'] ``` -------------------------------- ### Plot Summary Source: https://erdogant.github.io/distfit/pages/html/Univariate.html Visualize the summary of the fitted distributions. ```python dfit.plot_summary() ```