-------------------------------1147108394
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit
Dear Ara,
well thank you very much again.
Actually, I am trying to find parameter values for statistical distributions
from data, i.e.,
you have, say, a normal distribution, and some measured data that are
distributed
according to it, but you don't know the mean and variance parameters.
Then, one can solve some equations called maximum likelihood equations for
that.
I was writing a Ruby script to do that. I wanted it to be able to find
parameters for
several well-known distributions. One of these is a lesser well-known one,
the
Cauchy distribution, which is sometimes used to model random events where
extremely high values have a higher probability than if the data were
distributed
according to the normal distribution. Some people claim that, e.g., natural
disasters
generate insurance claims that are distributed according to it or that the
changes
in stock market values are distributed like that ( when a crash occurs, and
your money is lost, you know the famous (normal distribution) random-walk
pricing models are wrong).
This distribution has two parameters and I took a set of combined likelihood
equations out of a book, which are solved numerically.
Now, thanks to you, I can solve them. Below is the code.
Best regards,
Axel
--------------------------------------------
require "gsl"
include Math
include MultiMin
class Hash
def ml_fit
fitted_params
# fit distribution parameters from data using maximum likelihood or method
# of moments
# self is a hash with keys 'distribution', and values any of the following:
# 'normal','bernoulli','binomial','uniform','gamma','exponential','cauchy'
# 'data' - an Array with the data measured.
n¥í¥¡¥¡¥ó¥ç
¥ç¡¬¥½¥£¥¡¥¡¥¥alse
raise 'No distribution specified'
else
if self['distribution'].downcase normal' or
self['distribution'].downcase gaussian'
mu_val 0
self['data'].each{|x| mu_val _val+x}
mu_val _val/self['data'].length
fitted_params.store('mu_fitted',mu_val)
sigma_val 0
self['data'].each{|x| sigma_val gma_val+(x-mu_val)*(x-mu_val)}
sigma_val gma_val/self['data'].length
fitted_params.store('sigma_square_fitted',sigma_val)
end
if self['distribution'].downcase bernoulli'
# p(x) x*(1-p)^(1-x), if x \in \{0,1\}, p(x) otherwise
p_fitted 0
self['data'].each{|x| p_fitted fitted+x}
p_fitted fitted/self['data'].length
fitted_params.store('p_fitted',p_fitted)
end
if self['distribution'].downcase binomial'
# needs parameter n !
# p(x)
hoose{n,x} p^x (1-p)^(n-x), if x \in \{0,..,n\}
if self.has_key?('params')alse
raise('parameter n missing')
end
p_fitted 0
self['data'].each{|x| p_fitted fitted+x}
p_fitted fitted/self['data'].length/self['params']['n']
fitted_params.store('p_fitted',p_fitted)
end
if self['distribution'].downcase poisson'
# p(x)rac{\lambada^x}{x!} exp(-\lambda), if x \in \{0,1,..\}
lambda_fitted 0
self['data'].each{|x| lambda_fitted
mbda_fitted+x}
p_fitted fitted/self['data'].length
fitted_params.store('lambda_fitted',lambda_fitted)
end
if self['distribution'].downcase uniform'
# p(x)(b-a), if x \in \[a,b\]
a_fitted¥í¥¡¥¡¥ó¥ç¥ç¡¬
¡¬¥í¥¡¥¡¥ó¥ç¥ç¡¬
¡¬¥ç¥£¥¡¡¬¥¡¥ã¡¬¥¥
¡¬¥ç¥£¥¡¡¬¥¡¥ã¡¬¥¥
¥í¥¡¥¡¥ó¥ç ¥¡
¡× ¡¬¥ï¥ã¥ï¥£¥¥ ¥ï ¡¼¥µ
¡× ¡¬¥ï¥ã¥ï¥£¥¥rac{\lambda^{\alpha}}{\Gamma_{\alpha}}
x^{\alpha-1}exp(-\lambda*x), if x >;
# use method of moments to estimate params
m_1_fitted 0
self['data'].each{|x| m_1_fitted 1_fitted+x}
m_1_fitted 1_fitted/self['data'].length
m_2_fitted 0
self['data'].each{|x| m_2_fitted 2_fitted+x*x}
m_2_fitted 1_fitted/self['data'].length
alpha_fitted _2_fitted*m_2_fitted)/(m_2_fitted-m_1_fitted*m_1_fitted)
lambda_fitted 1_fitted/(m_2_fitted-m_1_fitted*m_1_fitted)
fitted_params.store('alpha_fitted',alpha_fitted)
fitted_params.store('lambda_fitted',lambda_fitted)
end
if self['distribution'].downcase exponential'
# p(x) ambda*exp(-\lambda*x)
lambda_fitted 0
self['data'].each{|x| lambda_fitted
mbda_fitted+x}
lambda_fitted
mbda_fitted/self['data'].length
if lambda_fitted
raise('error fitting exponential distribution parameter: lambda is infinite')
else
lambda_fittedlambda_fitted
fitted_params.store('lambda_fitted',lambda_fitted)
end
end
if self['distribution'].downcase cauchy'
# p(x,\alpha,\beta)rac{\beta}{\pi*(\beta^2+(x-\alpha)^2))
# likelihood equations for parameters are
#
# \frac{\partial L}{\partial \alpha} <