- scipy.stats.rv_continuous.fit#
- method-of-moments 0.1.0
- Навигация
- Ссылки проекта
- Статистика
- Метаданные
- Сопровождающие
- Классификаторы
- Описание проекта
- method_of_moments
- Prerequisites
- Installing method_of_moments
- Using method_of_moments
- Contributing to method_of_moments
- Contributors
- Contact
- License
- Method of Moments with Python
- Generalized Method of Moments gmm ¶
- Module Reference¶
scipy.stats.rv_continuous.fit#
Return estimates of shape (if applicable), location, and scale parameters from data. The default estimation method is Maximum Likelihood Estimation (MLE), but Method of Moments (MM) is also available.
Starting estimates for the fit are given by input arguments; for any arguments not provided with starting estimates, self._fitstart(data) is called to generate such.
One can hold some parameters fixed to specific values by passing in keyword arguments f0 , f1 , …, fn (for shape parameters) and floc and fscale (for location and scale parameters, respectively).
Parameters : data array_like or CensoredData instance
Data to use in estimating the distribution parameters.
arg1, arg2, arg3,… floats, optional
Starting value(s) for any shape-characterizing arguments (those not provided will be determined by a call to _fitstart(data) ). No default value.
- loc: initial guess of the distribution’s location parameter.
- scale: initial guess of the distribution’s scale parameter.
Special keyword arguments are recognized as holding certain parameters fixed:
- f0…fn : hold respective shape parameters fixed. Alternatively, shape parameters to fix can be specified by name. For example, if self.shapes == «a, b» , fa and fix_a are equivalent to f0 , and fb and fix_b are equivalent to f1 .
- floc : hold location parameter fixed to specified value.
- fscale : hold scale parameter fixed to specified value.
- optimizer : The optimizer to use. The optimizer must take func and starting position as the first two arguments, plus args (for extra arguments to pass to the function to be optimized) and disp=0 to suppress output as keyword arguments.
- method : The method to use. The default is “MLE” (Maximum Likelihood Estimate); “MM” (Method of Moments) is also available.
Estimates for any shape parameters (if applicable), followed by those for location and scale. For most random variables, shape statistics will be returned, but there are exceptions (e.g. norm ).
Raises : TypeError, ValueError
If fitting fails or the fit produced would be invalid
With method=»MLE» (default), the fit is computed by minimizing the negative log-likelihood function. A large, finite penalty (rather than infinite negative log-likelihood) is applied for observations beyond the support of the distribution.
With method=»MM» , the fit is computed by minimizing the L2 norm of the relative errors between the first k raw (about zero) data moments and the corresponding distribution moments, where k is the number of non-fixed parameters. More precisely, the objective function is:
(((data_moments - dist_moments) / np.maximum(np.abs(data_moments), 1e-8))**2).sum()
where the constant 1e-8 avoids division by zero in case of vanishing data moments. Typically, this error norm can be reduced to zero. Note that the standard method of moments can produce parameters for which some data are outside the support of the fitted distribution; this implementation does nothing to prevent this.
For either method, the returned answer is not guaranteed to be globally optimal; it may only be locally optimal, or the optimization may fail altogether. If the data contain any of np.nan , np.inf , or -np.inf , the fit method will raise a RuntimeError .
Generate some data to fit: draw random variates from the beta distribution
>>> from scipy.stats import beta >>> a, b = 1., 2. >>> x = beta.rvs(a, b, size=1000)
Now we can fit all four parameters ( a , b , loc and scale ):
>>> a1, b1, loc1, scale1 = beta.fit(x)
We can also use some prior knowledge about the dataset: let’s keep loc and scale fixed:
>>> a1, b1, loc1, scale1 = beta.fit(x, floc=0, fscale=1) >>> loc1, scale1 (0, 1)
We can also keep shape parameters fixed by using f -keywords. To keep the zero-th shape parameter a equal 1, use f0=1 or, equivalently, fa=1 :
>>> a1, b1, loc1, scale1 = beta.fit(x, fa=1, floc=0, fscale=1) >>> a1 1
Not all distributions return estimates for the shape parameters. norm for example just returns estimates for location and scale:
>>> from scipy.stats import norm >>> x = norm.rvs(a, b, size=1000, random_state=123) >>> loc1, scale1 = norm.fit(x) >>> loc1, scale1 (0.92087172783841631, 2.0015750750324668)
method-of-moments 0.1.0
The package that allows you to work with probability distributions with a specified mean values and variances.
Навигация
Ссылки проекта
Статистика
Метаданные
Лицензия: MIT License
Требует: Python >=3.6
Сопровождающие
Классификаторы
Описание проекта
method_of_moments
method_of_moments is a package that allows you to work with probability distributions with a specified mean values and variances.
Prerequisites
Before you begin, ensure you have installed the latest version of Python.
Installing method_of_moments
To install method_of_moments , follow these steps:
pip3 install method-of-moments
pip install method-of-moments
Using method_of_moments
There are will be examples of how to use method_of_moments .
Contributing to method_of_moments
To contribute to method_of_moments , follow these steps:
- Fork this repository.
- Create a branch: git checkout -b .
- Make your changes and commit them: git commit -m »
- Push to the original branch: git push origin /
- Create the pull request.
Alternatively see the GitHub documentation on creating a pull request.
Contributors
Contact
If you want to contact me you can reach me at albertfarhutdinov@gmail.com .
License
This project uses the following license: MIT License.
Method of Moments with Python
Imagine that we have a population with a specific distribution. As we already know distribution density function have the number of parameters. There is a large number of distribution functions and each of them has a different number of parameters, however most of the time one or two. For example, normal distribution has two parameters — μ and σ, which is mean and variance, however, Poisson distribution has only one parameter — λ, which is the rate. As always in statistics task, we have data sample from this population and need to make some estimation. Since we have data sample we can calculate sample moments(numerical characteristics of statistical distribution). The most used moments are first — expected value and second — variance. Also sometimes can be used third and fourth central moments. They are — skewness and kurtosis.
The method of moments solves such task: calculate the parameters of the population distribution function having a distribution function and a sample data. Let’s take the distribution from one of the previous articles, calculate parameters and compare an actual distribution with one calculated with the method of moments.
As you can see from the example we obtain result close to actual distribution with a small sample. In this example, we calculate only two moments since the population has a normal distribution, which has two parameters.
The generic approach for calculating parameters of population distribution function with k parameters by using the method of moments:
- find k ssample moments.
- calculate parameters of population distribution function by solving equations by using previously calculated moments.
Generalized Method of Moments gmm ¶
statsmodels.gmm contains model classes and functions that are based on estimation with Generalized Method of Moments. Currently the general non-linear case is implemented. An example class for the standard linear instrumental variable model is included. This has been introduced as a test case, it works correctly but it does not take the linear structure into account. For the linear case we intend to introduce a specific implementation which will be faster and numerically more accurate.
Currently, GMM takes arbitrary non-linear moment conditions and calculates the estimates either for a given weighting matrix or iteratively by alternating between estimating the optimal weighting matrix and estimating the parameters. Implementing models with different moment conditions is done by subclassing GMM. In the minimal implementation only the moment conditions, momcond have to be defined.
Module Reference¶
GMM (endog, exog, instrument[, k_moms, . ])
Class for estimation by Generalized Method of Moments
just a storage class right now
Instrumental variables estimation using Two-Stage Least-Squares (2SLS)
IVGMM (endog, exog, instrument[, k_moms, . ])
Basic class for instrumental variables estimation using GMM
Results class for for an OLS model.
class for linear instrumental variables models estimated with GMM
Class for non-linear instrumental variables estimation using GMM