Parameter Estimations of Hyperbolic and Normal InverseGaussian Distributions
Mounir Aout
Abstract: Generalized Hyperbolic distributions, introduced by Barndorff-Nielsen
in 1977, have become quite popular in various areas of theoretical and applied
statistics. These distributions possess a number of attractive properties and they
allow representation of the skewness and their tails tend to be heavier than those
of the normal. In this paper we develop a numerical algorithm for estimating
the parameters of both Hyperbolic (HYP) and Normal Inverse Gaussian (NIG)
distributions via Maximum Likelihood. This task relies on numerical methods
for solving systems of nonlinear equations. We give two C programs, based on
GSL library available under GPL license, for the estimation procedure.
Key Words: Hyperbolic distributions, Normal Inverse Gaussian distributions,
maximum likelihood, C programs.
1 Introduction
It is well known that the returns of most financial assets have semi-heavy tails, i.e.
the actual kurtosis is higher than the kurtosis of the normal distribution: Man-
delbrot (1963). He strongly rejected normality as a distributional model for asset
returns and conjectured that financial returns are more appropriately described
by stable Paretian distributions. These investigations were carried further by
Fama (1965). The major attack in the 1970s and 1980s centered around the claim
that while the empirical evidence does not support the normal distribution, it is
also not consistent with the stable Paretian distribution: Officer (1972), Akgiray
and Booth (1988) and Akgiray and Lamoureux (1989). Related ideas for testing
for stability are in Csorgo (1986) and references therein, and these tests were
recently simulated by Koutrouvelis and Meintanis (1999). Partly in response to
these empirical inconsistencies, various alternatives to the stable Paretian distri-
bution were proposed in the literature. Among the many models, let us mention
in particular the student distribution: Blatteberg and Gonedes (1974) and fi-
nite discrete mixtures of normals Kon (1984), Tucker (1992). Nevertheless, it is
1
the class of Genralized Hyperbolic (GH) distributions which will turn to be an
excellent candidate and which will provide a more realistic model. These dis-
tributions was introduced by Barndorff-Nielsen (1977) and at first applied them
to model grain size distributions of wind blown sands. Since its development,
GH distributions were used in physics, biology and agronomy, but Eberlein and
Keller (1995) were the first to apply these distributions to finance. An impor-
tant aspect is, that GH distributions embrace many special cases, respectively
limiting distributions, of hyperbolic, normal inverse Gaussian (NIG), Student-t,
variance-gamma and normal distributions. All of them have been used to model
financial returns and for market risk measurement.
In early 90s Blæsild and Sørensen (1992) developed a computer program called
Hyp which was used to estimate the parameters of Hyperbolic subclass distribu-
tions up to three dimensions. Prause (1999) develops a program to estimate the
GH distribution parameters, but the structure of these programs are not freely
available. In this paper, we will be interested by the hyperbolic and normal in-
verse Gaussian distributions as a special case of GH distributions. We give two C
programs, based on GSL library available under GPL license, for the estimation
procedure.
The paper is organized as follows: In section 2 we present GH distributions and
its subclass HYP and NIG distributions. Section 3 deals with the maximum like-
lihood estimation and multidimensional root-finding. This section is concluded
by giving two C programs called hyp.c and nig.c and some estimation results.
2 Generalized Hyperbolic Distributions
We start with an exposition of the univariate generalized hyperbolic distributions
and the subclasses which are relevant for applications.
2
2.1 Definition and Parameterizations
Definition: The one-dimensional generalized hyperbolic (GH) distribution is
defined by the following Lebesgue density
gh(x; λ, µ, α, β, δ) =(α2 − β2)λ/2
√2παλ−1/2δλKλ(δ
√α2 − β2)
(δ2 + (x− µ)2)(λ−12)/2
× Kλ−1/2(α√
δ2 + (x− µ)2) exp β(x− µ)
The parameters µ ∈ R and δ > 0 describe the location and the scale, whereas
|β| < α describes the skewness and α > 0 determines the shape. λ ∈ R charac-
terizes certain subclasses and considerably influences the size of mass contained
in the tails.
Kλ here denotes the modified Bessel function of the third kind with index λ and
x ∈ R.
Kλ(x) = 1/2∫ ∞
0yλ−1 exp(−x
2(y +
1y))dy, x > 0
The Bessel functions are discussed in (Abramowitz, 1968). Here we recall some
properties that will be used later:
• Kλ is symmetric with respect to λ, i.e. Kλ(x) = K−λ(x).
• For λ = ±12 , we get K± 1
2(x) =
√π2 x
−12 e−x.
• K′λ(x) = −λ
xKλ(x)−Kλ−1(x)
• Rλ(x) = Kλ+1(x)Kλ(x)
• (log Kλ(x))′= λ
x −Rλ(x)
Definition 1
For λ = 1 we obtain hyperbolic distribution (HYP) with density
hyp(x, α, β, δ, µ) =
√α2 − β2
2δαK1(δ√
α2 − β2)e−α
√δ2+(x−µ)2+β(x−µ)
3
For λ = −12 we obtain normal inverse gaussian distribution (NIG) with density
nig(x, α, β, δ, µ) =αδ
π
K1
(αδ
√1 + (x−µ
δ )2)
√δ2 + (x− µ)2
eδ√
α2−β2+β(x−µ)
Definition 2
Let β = ζτδ and α = ζ
√(1+τ2)
δ , we get the following parameterizations
hyp(x, τ , ζ, δ, µ) =1
2δ√
1 + τ2K1(ζ)e−ζ
�√1+τ2
q1+(x−µ
δ)2−τ x−µ
δ
�
nig(x, τ , ζ, δ, µ) =ζ√
1 + τ2
πeζ+ ζτ
δ(x−µ)
K1
(ζ√
1 + τ2√
1 + (x−µδ )2
)
√δ2 + (x− µ)2
3 Maximum-Likelihood Estimation
In this section we begin with a description of the ML estimation algorithm: We
assume the independence of the observations xi, i = 1, ..., n and maximize the
log-likelihood function
L =n∑
1
log(hyp(xi, τ , ζ, δ, µ))
and
L =n∑
1
log(nig(xi, τ , ζ, δ, µ))
To get the estimates of the parameters we take the partial derivatives with respect
to the corresponding parameters (τ , ζ, δ, µ) and we set the resulting expression
equal to zero.
3.1 Normal Inverse Gaussian Distributions
In this case, we get
4
dL
dτ=
2nτ
1 + τ2− τζ√
1 + τ2
n∑
i=1
√1 + (
xi − µ
δ)2R1(ai) +
ζ
δ
n∑
i=1
(xi − µ)
dL
dζ= n(1 +
2ζ)−
√1 + τ2
n∑
i=1
√1 + (
xi − µ
δ)2R1(ai) +
τ
δ
n∑
i=1
(xi − µ)
dL
dδ=−n
δ+
ζ√
1 + τ2
δ3
n∑
i=1
(xi − µ)2√1 + (xi−µ
δ )2R1(ai)− τζ
δ2
n∑
i=1
(xi − µ)
dL
dµ=−nζτ
δ+
ζ√
1 + τ2
δ2
n∑
i=1
xi − µ√1 + (xi−µ
δ )2R1(ai)
where ai = ζ√
1 + τ2√
1 + (xi−µδ )2
3.2 The Hyperbolic Distributions
The same computations lead to
dL
dτ=
ζ
δ
(n∑
i=1
(xi − µ)− τ√1 + τ2
n∑
i=1
√δ2 + (xi − µ)2 − nτδ
ζ(1 + τ2)
)
dL
dζ=
1δ
(−nδ(
1ζ−R1(ζ))−
√1 + τ2
n∑
i=1
√δ2 + (xi − µ)2 + τ
n∑
i=1
(xi − µ)
)
dL
dδ=
ζ
δ2
−nδ
ζ+
√1 + τ2
n∑
i=1
(xi − µ)2√δ2 + (xi − µ)2
− τ
n∑
i=1
(xi − µ)
dL
dµ=
ζ
τ
√
1 + τ2
n∑
i=1
xi − µ√δ2 + (xi − µ)2
− nτ
5
3.3 Multidimensional Root-Finding
In this section, we want to solve the nonlinear systems described above for both
the NIG and HYP distributions. We use the functions for multidimensional
root-finding in the GSL library http://www.gnu.org/software/gsl/. The library
provides low level components for a variety of iterative solvers and convergence
tests, and all algorithms proceed from an initial guess using a variant of the
Newton iteration. There are three main phases of the iteration:
• initialize solver state , s, for algorithm T
• update s using the iteration T
• test s for convergence , and repeat iteration if necessary.
Note also that the algorithms provided by the library give us the choice to use
or not the derivatives to evaluate the Jacobian matrix.
For the starting values of NIG distributions, we use the followings expressions
for the moments
• E(X) = µ + δτ
• V (X) = δ2(1+τ2)ζ
• S(X) = 3 τ√ζ√
1+τ2
• K(X) = 3ζ
(1 + 4 τ2
1+τ2
)
Solving the above equations leads to explicit expressions of (τ , ζ, δ, µ) which can
be used as starting values. The nig.c program is dealing with this case. For the
HYP distributions, we use the starting values as in HyperbolicDist R-packages
(http://cran.r-project.org). In this case, the user has the possibility to choose an
integer value for the number of breaks in the histogram (cf hyp.c).
3.4 Examples
We used data from several stock markets as basis for an empirical evaluation
of HYP and NIG distributions. The data consists of daily closing prices. The
6
historical time period goes from January, 1995, when data is available, to Dece-
meber,2004. We first transform the data to get the log-return and then fitted
the HYP and NIG distributions to the data sets. The estimates of parameters
are shown in Tables 1 and 2.
Sample τ ζ δ µ
CAC40 -0.04878 0.92283 0.00820 0.00142
DOWJONES -0.03475 0.69422 0.00490 0.00101
KFX -0.04311 0.80537 0.00564 0.00120
IBEX -0.07480 0.72291 0.00654 0.00210
FTSE -0.03622 0.01588 0.00013 0.00038
TASE -0.01511 0.87944 0.00829 0.00090
NIKEII -0.00588 1.44524 0.01220 -0.00006
NASDAQ -0.10282 0.24027 0.00298 0.00310
Table 1: Estimated HYP Parameters
Sample τ ζ δ µ
cac40 -0.06923 1.16076 0.01565 0.00136
DOWJONES -0.05529 1.01682 0.01119 0.00103
KFX -0.07168 1.08389 0.01168 0.00127
IBEX -0.09904 1.05993 0.01472 0.00188
FTSE -0.43608 0.50465 0.00854 0.00218
TASE -0.03158 1.17035 0.01653 0.00106
NIKEII -0.00755 1.74813 0.01969 -0.00007
NASQ -0.16433 0.68569 0.01512 0.00281
Table 2: Estimated NIG Parameters
Acknowledgment. A part of this work was carried out while I was working at
the IBL CNRS-UMR8090. The author thanks C. Wachter for helpful discussions.
I would also like to thank the referee for his helpful comments and suggestions.
7
References
Abramowitz, M. and Stegun, I. A. (1968). Handbook of Mathematical Func-
tions. Dover Publ., New York.
Akgiray, A. and Booth,G.G. (1988). The stable-law model of stock returns.
Journal of Business and Economic Statistics, 6, 5157.
Akgiray, V. and Lamoureux, C. (1989). Estimation of stable-law parameters: A
comparative study. Journal of Business and Economic Statistics, 7, 8593.
Barndorff-Nielsen, O. E. (1977). Exponentially decreasing distributions for the
logarithm of particle size. Proceedings of the Royal Society London A,
353, 401-419.
Blæsild, P. and Sørensen, M. (1992). Hyp a computer program for analyzing
data by means of the hyperbolic distribution. Department of Theoretical
Statistics, Aarhus University Research Report, 248.
Blattberg, R. and Gonedes, N. (1974). A comparison of the stable and Student
distributions as statistical models for stock prices. Journal of Business,
47,244-280.
Csorgo, S. (1986). Testing for stability. In: Colloquia Mathematica Societatis
Janos Bolyai, 45. Goodness of Fit (P. Revesz, K. Sarkadi and P.K. Sen,
eds.), 101-132. North-Holland, Amsterdam.
Eberlein, E. and Keller, U. (1995). Hyperbolic distributions in finance. Bernoulli,
1, 281-299.
Fama, E. (1965). The behaviour of stock market prices. Journal of business,
38, 34-105.
Kon, S.J. (1984). Models of stock returns - a comparison. Journal of Finance,
39, 147-165.
Koutrouvelis, I.A. and Meintanis, S.G. (1999). Testing for stability based on
the empirical characteristic function with applications to financial data.
Journal of Statistical Computing and Simulation, 64, 275-300.
8
Mandelbrot, B. (1963). The variation of certain speculative prices. Journal of
Business, 36, 394-419.
Officer, R. (1972). The distribution of stock returns. Journal of the American
Statistical Association, 67, 807-812.
Prause, K. (1999). The generalized hyperbolic model: Estimation, financial
derivatives, and risk measures. University of Freiburg, Doctoral Thesis.
R Development Core Team (2005). R: A language and environment for statisti-
cal computing. R Foundation for Statistical Computing, Vienna, Austria.
ISBN 3-900051-07-0, URL http://www.R-project.org
Scott, D. (2003). HyperbolicDist: The hyperbolic distribution. R package
version 0.0-1.
Tucker, A. (1992). A reexamination of finite- and infinite-variance distribu-
tions as models of daily stock returns. Journal of Business and Economic
Statistics, 10, 73-81.
http://www.gnu.org/software/gsl/
9
Appendix
To compile and execute the programs we use
under Linux
gcc program.c -lgsl -lgslcblas -Wall -lm
a.out data N
Under Windows
gcc -Wall -I PATH :\gsl\include -c nig.c
gcc -L PATH :\gsl\lib -static nig.o -lgsl -lgslcblas -lm
a.exe data N
The last parameter N is optional, and it is used only in hyp.c to permit the user
to choose an adequate integer to estimate initial parameter values.
/∗ NIG distribution Program nig.cAuthor: Mounir AoutUnder GNU General Public License, version 2∗/#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl math.h>
#include <gsl/gsl vector.h>
#include <gsl/gsl multiroots.h>
#include <gsl/gsl statistics.h>
#include <gsl/gsl sf bessel.h>
#include <gsl/gsl histogram.h>
#include <gsl/gsl fit.h>
#include <gsl/gsl sort.h>
#include <gsl/gsl sort vector.h>
#include <gsl/gsl permutation.h>
gsl vector ∗ d;
10
gsl vector ∗vinit;int N=0;struct rparams{
double a;double b;
};
int print state (gsl multiroot fsolver ∗ s){int i;printf(”\n”);
for (i=0;i<4;i++){printf (”iter = %7u x = % .5f” ” f(x) = % .7e\n”,i,
gsl vector get (s−>x, i), gsl vector get (s−>f, i));}
return 1;}// Reading Datavoid lecture (char ∗ filein){FILE ∗ f1;{float jj;f1 = fopen (filein, ”r”);
if ( f1 == NULL){
printf (”No datafile %s!!\n”,filein);exit(1);
}while((fscanf(f1,”%f[ \n]”,&jj))>0){
N++;}
fclose (f1);d = gsl vector alloc(N);f1 = fopen (filein, ”r”);
11
gsl vector fscanf(f1, d);fclose (f1);}
// Find initial parametersdouble moyenne = gsl stats mean(d−>data,1,N);double vvv = gsl stats variance(d−>data,1,N);double skewness = gsl stats skew(d−>data,1,N);double kurtosis = gsl stats kurtosis(d−>data,1,N);double ss = skewness/3;double kkk = kurtosis/3;double gg = kkk − 5∗gsl pow 2(ss);double hyppi= ss/sqrt(gg);double hypzeta = 1/(gg+gsl pow 2(ss));double hypdelta= sqrt(gg∗vvv)/(gg + gsl pow 2(ss));double hypmu = moyenne − sqrt(vvv)∗ss/(gg+gsl pow 2(ss));vinit= gsl vector alloc(4);gsl vector set(vinit,0,hyppi);gsl vector set(vinit,1,hypzeta);gsl vector set(vinit,2,hypdelta);gsl vector set(vinit,3,hypmu);
}// Writing Functionsint mafonction f (const gsl vector ∗ x,void ∗params, gsl vector ∗ f){
int i;const double pi = gsl vector get(x, 0);const double zeta = gsl vector get(x,1);const double delta = gsl vector get(x,2);const double mu = gsl vector get(x, 3);double yy1=0;double yy2=0;double yy3=0;double yy4=0;double di;
for (i=0;i<N;i++){
di=gsl vector get(d,i);
12
yy1 +=sqrt(1+ gsl pow 2((di−mu)/delta))∗(gsl sf bessel Knu (2, zeta∗sqrt(1+gsl pow 2(pi))∗sqrt(1+ gsl pow 2((di−mu)/delta))))/(gsl sf bessel Knu(1,zeta∗sqrt(1+gsl pow 2(pi))∗sqrt(1+ gsl pow 2((di−mu)/delta))));
yy2+=gsl pow 2(di−mu)/sqrt(1+ gsl pow 2((di−mu)/delta))∗(gsl sf bessel Knu (2, zeta∗sqrt(1+gsl pow 2(pi))∗sqrt(1+ gsl pow 2((di−mu)/delta))))/(gsl sf bessel Knu(1,zeta∗sqrt(1+gsl pow 2(pi))∗sqrt(1+ gsl pow 2((di−mu)/delta))));
yy3+=(di−mu)/sqrt(1+ gsl pow 2((di−mu)/delta))∗(gsl sf bessel Knu (2, zeta∗sqrt(1+gsl pow 2(pi))∗sqrt(1+ gsl pow 2((di−mu)/delta))))/(gsl sf bessel Knu(1,zeta∗sqrt(1+gsl pow 2(pi))∗sqrt(1+ gsl pow 2((di−mu)/delta))));
yy4 += (di−mu);}
const double y1 = N∗(2∗pi/sqrt(1+gsl pow 2(pi))) − (pi∗zeta/sqrt(1+gsl pow 2(pi)))∗yy1 +(zeta/delta)∗yy4;
const double y2 = N∗(1+2/zeta) − sqrt(1+gsl pow 2(pi))∗yy1 +(pi/delta)∗yy4;const double y3 = (−N/delta)+(zeta∗ sqrt(1+ gsl pow 2(pi))/gsl pow 3(delta))∗yy2 −
(pi∗zeta/gsl pow 2(delta))∗yy4;const double y4 = (−N∗zeta∗pi/delta)+ (zeta∗sqrt(1+ gsl pow 2(pi))/gsl pow 2(delta))∗yy3;
gsl vector set(f,0,y1);gsl vector set(f,1,y2);gsl vector set(f,2,y3);gsl vector set(f,3,y4);return GSL SUCCESS;
}int main (int argc, char ∗∗ argv){
const gsl multiroot fsolver type ∗T;gsl multiroot fsolver ∗s;int status=−2;size t iter = 0;const size t n = 4;struct rparams p = {1.0, 10.0};
13
char ∗filein=argv[1];while (status != GSL CONTINUE);
{iter=0;lecture(filein);gsl multiroot function f = {&mafonction f,n,&p};double x init[4]={gsl vector get(vinit,0),gsl vector get(vinit,1),gsl vector get(vinit,2),
gsl vector get(vinit,3)};
gsl vector ∗x = gsl vector alloc (n);gsl vector set (x, 0, x init[0]);gsl vector set (x, 1, x init[1]);gsl vector set (x, 2, x init[2]);gsl vector set (x, 3, x init[3]);T = gsl multiroot fsolver dnewton;s = gsl multiroot fsolver alloc (T, 4);gsl multiroot fsolver set (s, &f, x);
do{
iter++;status = gsl multiroot fsolver iterate (s);print state(s);
if (status) /∗ check if solver is stuck ∗/break;
status = gsl multiroot test residual (s−>f, 1e−10);}
while (status == GSL CONTINUE && iter <5000);gsl vector free (x);printf (”status = %s\n”,gsl strerror (status));
}printf (”status = %s\n”,gsl strerror (status));gsl multiroot fsolver free (s);return 0;
}
14
/∗ Hyperbolic distribution Program hyp.cAuthor: Mounir AoutUnder GNU General Public License, version 2∗/#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl math.h>
#include <gsl/gsl vector.h>
#include <gsl/gsl multiroots.h>
#include <gsl/gsl statistics.h>
#include <gsl/gsl sf bessel.h>
#include <gsl/gsl histogram.h>
#include <gsl/gsl fit.h>
#include <gsl/gsl sort.h>
#include <gsl/gsl sort vector.h>
#include <gsl/gsl permutation.h>
gsl vector ∗ d;gsl vector ∗vinit;int N=0;struct rparams{
double a;double b;
};
int print state (gsl multiroot fsolver ∗ s){
int i;printf(”\n”);
for (i=0;i<4;i++){printf (”iter = %5u x = % .5f” ” f(x) = % .5e\n”,i,
gsl vector get (s−>x, i), gsl vector get (s−>f, i));}
return 1;}
15
// Reading Datavoid lecture (char ∗ filein,int Nn){
FILE ∗ f1;gsl vector ∗ rg1;gsl vector ∗ rg2;gsl vector ∗ binn;gsl vector ∗ midpoints;gsl vector ∗ midpoints1;gsl vector ∗ midpoints2;gsl vector ∗ dens;gsl vector ∗ dens1;gsl vector ∗ dens2;gsl vector ∗ logdens1;gsl vector ∗ logdens2;gsl permutation ∗p;gsl vector ∗rangeuser;double c0, c1, cov00, cov01, cov11, sumsq;double cc0, cc1, ccov00, ccov01, ccov11, csumsq;
{float jj;f1 = fopen (filein, ”r”);
if ( f1==NULL ){
printf (”No datafile !!\n”);exit(1);
}while((fscanf(f1,”%f[ \n]”,&jj))>0){
N++;}
fclose (f1);d = gsl vector alloc(N);f1 = fopen (filein, ”r”);gsl vector fscanf(f1, d);fclose (f1);}
16
// Finding initial parametersgsl histogram ∗ h = gsl histogram alloc (Nn);int i ;double a ,b,x ;a=gsl vector min(d);b=gsl vector max(d);rangeuser = gsl vector alloc(Nn+1);
for (i=0;i<(Nn+1);i++){
gsl vector set(rangeuser,i,a+i∗(b−a)/Nn);}
gsl histogram set ranges (h, rangeuser−>data, Nn+1);for (i=0;i<N;i++){
x=gsl vector get(d,i);gsl histogram increment (h, x);
}rg1=gsl vector alloc(Nn+1);rg2=gsl vector alloc(Nn+1);binn=gsl vector alloc(Nn);midpoints = gsl vector alloc(Nn);dens = gsl vector alloc(Nn);p = gsl permutation alloc(Nn);
for (i=0;i<(Nn+1);i++){
gsl vector set(rg1,i,h−>range[i]);gsl vector set(rg2,i,h−>range[i+1]);
}for (i=0;i<Nn;i++){
gsl vector set(binn,i,h−>bin[i]);gsl vector set(midpoints,i, (gsl vector get(rg1,i)+gsl vector get(rg2,i))/2);gsl vector set(dens, i, gsl vector get(binn,i)∗Nn/(N∗(b−a)));
}gsl sort vector index(p,dens);int maxindex=gsl permutation get(p,Nn−1);int nbzero=0;
for (i=0;i<=maxindex;i++)
17
{if (gsl vector get(dens, i)==0)
{nbzero++;
}}
gsl vector fprintf(stdout,dens,”%f”);dens1 = gsl vector alloc(maxindex+1−nbzero);midpoints1 = gsl vector alloc(maxindex+1−nbzero);logdens1 = gsl vector alloc(maxindex+1−nbzero);nbzero=0;
for (i=maxindex;i<Nn;i++){if (gsl vector get(dens, i)==0)
{nbzero++;
}}
midpoints2 = gsl vector alloc(Nn−maxindex−nbzero);logdens2 = gsl vector alloc(Nn−maxindex−nbzero);dens2 = gsl vector alloc(Nn−maxindex−nbzero);int j =0;
for (i=0,j=0;i<=maxindex;i++){if (gsl vector get(dens, i)!=0)
{gsl vector set(dens1,j,gsl vector get(dens, i));gsl vector set(logdens1,j,log(gsl vector get(dens, i)));gsl vector set(midpoints1,j,gsl vector get(midpoints, i));j++;
}}
int N1=j;for (i=maxindex,j=0;i<Nn;i++){if (gsl vector get(dens, i)!=0)
{
18
gsl vector set(dens2,j,gsl vector get(dens, i));gsl vector set(logdens2,j,log(gsl vector get(dens, i)));gsl vector set(midpoints2,j,gsl vector get(midpoints, i));j++;
}}
int N2=j;gsl fit linear (midpoints1−>data, 1,logdens1−>data, 1, N1,&c0, &c1, &cov00,
&cov01, &cov11,&sumsq);gsl fit linear (midpoints2−>data, 1,logdens2−>data, 1, N2,&cc0, &cc1, &ccov00,
&ccov01, &ccov11,&csumsq);printf (”\n %g %g\n”, c0,c1);printf (”\n %g %g \n”, cc0,cc1);double hypphi = c1;double hypgamma= −cc1;double hypmu =(−(c0−cc0)/(c1− cc1));double intersectionvalue =(c0+ hypmu∗c1);double logmodaldens = log(gsl vector max(dens));double hypzeta = intersectionvalue − logmodaldens;
if(hypzeta<=0){
hypzeta = 0.1; // This set arbitrarily}
double hypdelta = hypzeta/sqrt(hypphi∗hypgamma);double hyppi = (hypphi−hypgamma)/(2∗sqrt(hypphi∗hypgamma));vinit= gsl vector alloc(4);gsl vector set(vinit,0,hyppi);gsl vector set(vinit,1,hypzeta);gsl vector set(vinit,2,hypdelta);gsl vector set(vinit,3,hypmu);printf (”\n %g %g %g %g\n”, hyppi,hypzeta, hypdelta, hypmu);gsl histogram free (h);}// Writing Functionsint mafonction f (const gsl vector ∗ x,void ∗params, gsl vector ∗ f){int i;
19
const double pi = gsl vector get(x, 0);const double zeta = gsl vector get(x, 1);const double delta = gsl vector get(x, 2);const double mu = gsl vector get(x, 3);double Klambda1,Klambda2;double Rlambda1;Klambda2 = gsl sf bessel Knu (2, zeta);Klambda1 = gsl sf bessel Knu(1,zeta);Rlambda1 = Klambda2/ Klambda1;double yy1=0;double yy11=0;double yy3=0;double yy4=0;double di;
for (i=0;i<N;i++){
di=gsl vector get(d,i);yy1 += (di−mu);yy11+= sqrt(gsl pow 2(delta)+ gsl pow 2(di−mu));yy3+=gsl pow 2(di−mu)/sqrt(gsl pow 2(delta)+ gsl pow 2(di−mu));yy4 +=(di−mu)/sqrt(gsl pow 2(delta)+ gsl pow 2(di−mu));
}const double y1 = yy1−(pi/sqrt(1+ gsl pow 2(pi)))∗yy11−
(N∗pi∗delta/(zeta∗(1+ gsl pow 2(pi))));const double y2 =−N∗delta∗(1/zeta − Rlambda1)−
sqrt(1+ gsl pow 2(pi))∗yy11 + pi∗yy1;const double y3 = (−N∗delta/zeta)+ sqrt(1+ gsl pow 2(pi))∗yy3 − pi∗yy1;const double y4 = sqrt(1+ gsl pow 2(pi))∗yy4 − N∗pi;gsl vector set(f,0,y1);gsl vector set(f,1,y2);gsl vector set(f,2,y3);gsl vector set(f,3,y4);return GSL SUCCESS;}
int main (int argc, char ∗∗ argv){const gsl multiroot fsolver type ∗T;
20
gsl multiroot fsolver ∗s;int status=−2;size t iter = 0;const size t n = 4;struct rparams p = {1.0, 10.0};char ∗filein=argv[1];int Nntab[7];int nnt=6;Nntab[1]=10;Nntab[2]=100;Nntab[3]=1000;Nntab[4]=50;Nntab[5]=500;Nntab[6]=10000;
if (argc==3){
Nntab[0]=atoi(argv[2]);}
else{
Nntab[0]=1000;}
nnt=0;while (status != GSL CONTINUE && nnt <6);{
iter=0;int Nn=Nntab[nnt];lecture(filein,Nn);gsl multiroot function f = {&mafonction f,n,&p};double x init[4]={gsl vector get(vinit,0),gsl vector get(vinit,1),
gsl vector get(vinit,2),gsl vector get(vinit,3)};gsl vector ∗x = gsl vector alloc (n);gsl vector set (x, 0, x init[0]);gsl vector set (x, 1, x init[1]);gsl vector set (x, 2, x init[2]);gsl vector set (x, 3, x init[3]);T = gsl multiroot fsolver dnewton;s = gsl multiroot fsolver alloc (T, 4);
21
gsl multiroot fsolver set (s, &f, x);do{
iter++;status = gsl multiroot fsolver iterate (s);print state(s);
if (status) /∗ check if solver is stuck ∗/break;
status = gsl multiroot test residual (s−>f, 1e−7);}
while (status == GSL CONTINUE && iter <5000);gsl vector free (x);printf (”status %i= %s\n”, Nntab[nnt],gsl strerror (status));nnt++;
}printf (”status %i= %s\n”, Nntab[nnt−1],gsl strerror (status));gsl multiroot fsolver free (s);return 0;
}
Mounir Aout
Department of Statistics and Data Processing
IUT de Caen (Lisieux)
11 Bd Jules Ferry
14100 Lisieux France
E-mail: [email protected]
22