# How to fit probability density function from sample moments?

If I have calculated the sample mean, variance, skew and kurtosis of a set of data, how would I go about fitting a probability distribution to match these moments (i.e. choosing a probability distribution and optimizing its parameters to fit the sample moments). Are there any packages in R/MATLAB/etc. that are capable of this?

For context, I believe I can calculate these moments for a portfolio's return distribution, but I actually need a whole probability density function for the portfolio's returns in order to perform additional analysis.

-

## 5 Answers

I asked an almost identical question on the Cross Validated site here. I think my accepted answer, given by Whuber, might be what you are looking for.

-

You should be able to do this with the `fitdistr` function in the MASS package. You will certainly be able to hold the mean and variance constant, but I'm less sure about skewness and kurtosis (they would need to be arguments to the density function).

The actuar package may also be useful, as it contains additional density functions.

-

If you have a formula giving you the moments as a function of the parameters of the distribution, you can use `gmm`, in the `gmm` package: there is a detailed example for the Gaussian distribution in the documentation.

(Of course, in this case, you are only solving a system of equations, and could probably do it by hand: the generalized method of moments (GMM) is typically used when there are more moments than parameters.)

-

Often, we are interested to check if our data is close to normal, then you can use the Jarque-Bera test, where skewness and kurthosis are directly deployed. Look up Matlab implementation.

If you are not constrained to use the moments, you can calculate histogram and use Komogorov-Smirnov test, which can test similarity to any distribution, not necessarily normal. Look up Matlab implementation.

-

It may never have been done, but it seems to me that the Cornish-Fisher expansion for moments up to 4 could be used to trace a density function from a tabulation of the percentiles of the normal distribution, for instance:

$$w_{\alpha}\cong z_{\alpha}+\frac{1}{6}\left(z_{\alpha}^{2}-1\right)AS+\frac{1}{24}\left(z_{\alpha}^{3}-3z_{\alpha}\right)EKUR-\frac{1}{36}\left(2z_{\alpha}^{3}-5z_{\alpha}\right)AS^{2}$$ where $w_{\alpha}$ is the revised percentile, $z_{\alpha}$ is the percentile for the standard normal distribution, AS is the skewness coefficient and EKUR is the excess kurtosis coefficient (from a kurtosis coefficient of 3 for a normal distribution).

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 2, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9190283417701721, "perplexity_flag": "head"}