1 /*****************************************************************************
2  *                                                                           *
3  *          UNURAN -- Universal Non-Uniform Random number generator          *
4  *                                                                           *
5  *****************************************************************************
6  *                                                                           *
7  *   FILE: distr_struct.h                                                    *
8  *                                                                           *
9  *   PURPOSE:                                                                *
10  *         declares structures for distributions                             *
11  *                                                                           *
12  *   USAGE:                                                                  *
13  *         only included in unur_struct.h                                    *
14  *                                                                           *
15  *****************************************************************************
16  *                                                                           *
17  *   Copyright (c) 2000-2006 Wolfgang Hoermann and Josef Leydold             *
18  *   Department of Statistics and Mathematics, WU Wien, Austria              *
19  *                                                                           *
20  *   This program is free software; you can redistribute it and/or modify    *
21  *   it under the terms of the GNU General Public License as published by    *
22  *   the Free Software Foundation; either version 2 of the License, or       *
23  *   (at your option) any later version.                                     *
24  *                                                                           *
25  *   This program is distributed in the hope that it will be useful,         *
26  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
27  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
28  *   GNU General Public License for more details.                            *
29  *                                                                           *
30  *   You should have received a copy of the GNU General Public License       *
31  *   along with this program; if not, write to the                           *
32  *   Free Software Foundation, Inc.,                                         *
33  *   59 Temple Place, Suite 330, Boston, MA 02111-1307, USA                  *
34  *                                                                           *
35  *****************************************************************************/
36 
37 /*---------------------------------------------------------------------------*/
38 /* define object for univariate continuous distribution                      */
39 
40 struct unur_distr_cont {
41 
42   UNUR_FUNCT_CONT *pdf;         /* pointer to PDF                            */
43   UNUR_FUNCT_CONT *dpdf;        /* pointer to derivative of PDF              */
44   UNUR_FUNCT_CONT *cdf;         /* pointer to CDF                            */
45   UNUR_FUNCT_CONT *invcdf;      /* pointer to inverse of CDF                 */
46   UNUR_FUNCT_CONT *logpdf;      /* pointer to logPDF                         */
47   UNUR_FUNCT_CONT *dlogpdf;     /* pointer to derivative of logPDF           */
48   UNUR_FUNCT_CONT *logcdf;      /* pointer to logCDF                         */
49   UNUR_FUNCT_CONT *hr;          /* pointer to hazard rate                    */
50 
51   double norm_constant;         /* (log of) normalization constant for PDF   */
52 
53   double params[UNUR_DISTR_MAXPARAMS];  /* parameters of the PDF             */
54   int    n_params;              /* number of parameters of the PDF           */
55 
56   double *param_vecs[UNUR_DISTR_MAXPARAMS];  /* parameter vectors            */
57   int    n_param_vec[UNUR_DISTR_MAXPARAMS]; /* lengths of the parameter vecs */
58 
59   double mode;                  /* location of mode                          */
60   double center;                /* location of center                        */
61   double area;                  /* area below PDF                            */
62   double domain[2];             /* boundary of domain                        */
63   double trunc[2];              /* boundary of truncated domain              */
64 
65   struct ftreenode *pdftree;    /* pointer to function tree for PDF          */
66   struct ftreenode *dpdftree;   /* pointer to function tree for dPDF         */
67   struct ftreenode *logpdftree; /* pointer to function tree for logPDF       */
68   struct ftreenode *dlogpdftree;/* pointer to function tree for dlogPDF      */
69   struct ftreenode *cdftree;    /* pointer to function tree for CDF          */
70   struct ftreenode *logcdftree; /* pointer to function tree for logCDF       */
71   struct ftreenode *hrtree;     /* pointer to function tree for hazard rate  */
72 
73   int (*set_params)(struct unur_distr *distr, const double *params, int n_params );
74                                 /* function for setting parameters and domain*/
75   int (*upd_mode)(struct unur_distr *distr);
76                                 /* function for computing mode               */
77   int (*upd_area)(struct unur_distr *distr);
78                                 /* function for computing area               */
79 
80   int  (*init)(struct unur_par *par,struct unur_gen *gen);
81                                 /* pointer to special init routine           */
82 };
83 
84 /*---------------------------------------------------------------------------*/
85 /* define object for multivariate continuous distribution                    */
86 
87 struct unur_distr_cvec {
88 
89   UNUR_FUNCT_CVEC *pdf;         /* pointer to PDF                            */
90   UNUR_VFUNCT_CVEC *dpdf;       /* pointer to gradient of PDF                */
91   UNUR_FUNCTD_CVEC *pdpdf;      /* pointer to partial derivative of PDF      */
92   UNUR_FUNCT_CVEC *logpdf;      /* pointer to logPDF                         */
93   UNUR_VFUNCT_CVEC *dlogpdf;    /* pointer to gradient of logPDF             */
94   UNUR_FUNCTD_CVEC *pdlogpdf;   /* pointer to partial derivative of logPDF   */
95 
96   double *mean;                 /* mean vector of distribution               */
97   double *covar;                /* covariance matrix of distribution         */
98 
99   double *cholesky;             /* cholesky factor of covariance matrix      */
100   double *covar_inv;            /* inverse of covariance matrix              */
101 
102   double *rankcorr;             /* rank correlation matrix                   */
103   double *rk_cholesky;          /* cholesky factor of rank corr. matrix      */
104 
105   struct unur_distr **marginals; /* array of pointers to marginal distributions */
106 
107   double params[UNUR_DISTR_MAXPARAMS];  /* parameters of the PDF             */
108   int    n_params;              /* number of parameters of the PDF           */
109 
110   double *param_vecs[UNUR_DISTR_MAXPARAMS];  /* parameter vectors            */
111   int    n_param_vec[UNUR_DISTR_MAXPARAMS]; /* lengths of the parameter vecs */
112 
113   double norm_constant;         /* (log of) normalization constant for PDF   */
114 
115   double *mode;                 /* location of mode                          */
116   double *center;               /* location of center                        */
117   double volume;                /* volume below PDF                          */
118   double *domainrect;           /* (rectangular) domain of domain:
119 				   the lower left and the upper right vertices
120 				   are stored componentwise, i.e.
121 				   l[0], r[0], l[1], r[1], l[2], r[2], ...   */
122 
123 #ifdef USE_DEPRECATED_CODE
124   struct unur_distr **stdmarginals; /* array of pointers to standardized marginal distributions */
125 #endif
126 
127   int (*upd_mode)(struct unur_distr *distr);
128                                 /* function for computing mode               */
129   int (*upd_volume)(struct unur_distr *distr);
130                                 /* function for computing volume             */
131 
132   int (*init)(struct unur_gen *gen);
133                                 /* pointer to special init routine           */
134 };
135 
136 /*---------------------------------------------------------------------------*/
137 /* define object for univariate discrete distribution                        */
138 
139 struct unur_distr_discr {
140   /* (finite) probability vector */
141   double *pv;                   /* pointer to probability vector             */
142   int     n_pv;                 /* length of probability vector              */
143 
144   /* probability mass function */
145   UNUR_FUNCT_DISCR  *pmf;       /* pointer to probability mass function      */
146   UNUR_FUNCT_DISCR  *cdf;       /* pointer to CDF                            */
147   UNUR_IFUNCT_DISCR *invcdf;    /* pointer to inverse of CDF                 */
148 
149   double params[UNUR_DISTR_MAXPARAMS];  /* parameters of the PMF             */
150   int    n_params;              /* number of parameters of the PMF           */
151 
152   double norm_constant;         /* (log of) normalization constant for PMF   */
153 
154   int    mode;                  /* location of mode                          */
155   double sum;                   /* sum over PMF                              */
156 
157   int (*set_params)(struct unur_distr *distr, const double *params, int n_params );
158                                 /* function for setting parameters and domain*/
159   int (*upd_mode)(struct unur_distr *distr);
160                                 /* function for computing mode               */
161   int (*upd_sum)(struct unur_distr *distr);
162                                 /* function for computing sum                */
163 
164   /* other parameters */
165   int domain[2];                /* boundary of domain                        */
166   int trunc[2];                 /* boundary of truncated domain              */
167   /** trunc[] not supported yet **/
168 
169   struct ftreenode *pmftree;    /* pointer to function tree for PMF          */
170   struct ftreenode *cdftree;    /* pointer to function tree for CDF          */
171 
172   int  (*init)(struct unur_par *par,struct unur_gen *gen);
173                                 /* pointer to special init routine           */
174 };
175 
176 /*---------------------------------------------------------------------------*/
177 /* define object for empirical univariate constinuous distribution           */
178 /* (given by empirical sample)                                               */
179 
180 struct unur_distr_cemp {
181   /* raw data */
182   int     n_sample;             /* length of sample probability vector       */
183   double *sample;               /* pointer to sample                         */
184   /* histogram */
185   int     n_hist;               /* number of bins in histogram               */
186   double *hist_prob;            /* probabilities for bins                    */
187   double  hmin, hmax;           /* lower and upper bound for histograms with
188 				   bins of equal length                      */
189   double *hist_bins;            /* boundary between bins (of different length) */
190 };
191 
192 /*---------------------------------------------------------------------------*/
193 /* define object for empirical mulitvariate constinuous distribution         */
194 /* (given by empirical sample)                                               */
195 
196 struct unur_distr_cvemp {
197   double *sample;              /* pointer to sample                          */
198   int    n_sample;             /* length of sample probability vector        */
199 };
200 
201 /*---------------------------------------------------------------------------*/
202 /* define object for matrix distribution                                     */
203 
204 struct unur_distr_matr {
205 
206   int n_rows;                   /* number of rows                            */
207   int n_cols;                   /* number of columns                         */
208 
209   int  (*init)(struct unur_par *par,struct unur_gen *gen);
210                                 /* pointer to special init routine           */
211 };
212 
213 /*---------------------------------------------------------------------------*/
214 /* define distribution object                                                */
215 
216 struct unur_distr {
217   union {
218     struct unur_distr_cont  cont;   /* univariate continuous distribution    */
219     struct unur_distr_matr  matr;   /* matrix distribution                   */
220     struct unur_distr_cvec  cvec;   /* multivariate continuous distribution  */
221     struct unur_distr_discr discr;  /* univariate discrete distribution      */
222     struct unur_distr_cemp  cemp;   /* empirical univ. cont. distr. (sample) */
223     struct unur_distr_cvemp cvemp;  /* empir. multiv. cont. distr. (sample)  */
224   } data;                           /* data for distribution                 */
225 
226   unsigned type;                    /* type of distribution                  */
227   unsigned id;                      /* identifier for distribution           */
228   const char *name;                 /* name of distribution                  */
229   char *name_str;                   /* string for storing user name of distr */
230   int dim;                          /* number of components of random vector */
231 
232   unsigned set;                     /* indicate changed parameters           */
233 
234   const void *extobj;               /* pointer to an object for additional data */
235 
236   struct unur_distr *base;          /* pointer to distribution object for
237 				       derived distribution
238 				       (e.g. order statistics)               */
239 
240   void (*destroy)(struct unur_distr *distr); /* pointer to destructor        */
241   struct unur_distr* (*clone)(const struct unur_distr *distr ); /* clone     */
242 
243 #ifdef UNUR_COOKIES
244   unsigned cookie;                  /* magic cookie                          */
245 #endif
246 };
247 
248 /*---------------------------------------------------------------------------*/
249