File: finv.tex

package info (click to toggle)
testu01 1.2.3%2Bds1-3
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 17,748 kB
  • sloc: ansic: 52,357; makefile: 248; sh: 53
file content (323 lines) | stat: -rw-r--r-- 9,142 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
\defmodule {finv}

Here one finds procedures to compute or approximate the 
inverse of certain distribution functions.  
Each procedure computes $F^{-1}(u) = \inf\{x\in\mathbb{R} : F(x)\ge u\}$,
where $0\le u\le 1$ and $F$
is the distribution function of a specific type of random variable.
These procedures can be used, among other things, to generate
the corresponding random variables by inversion, by passing a
$U(0,1)$ random variate as the value of $u$.

Several distributions are only implemented in standardized form here,
i.e., with the location parameter set to 0 and the scale parameter
set to 1.  To obtain the inverse for the distribution shifted 
by $x_0$ and rescaled by a factor $c$, it suffices to multiply the
returned value by $c$ and add $x_0$.

\bigskip\hrule\medskip
\code\hide
/* finv.h for ANSI C */

#ifndef FINV_H
#define FINV_H
\endhide
#include <testu01/gdef.h>     /* From the library mylib */
#include <testu01/fmass.h>
#include <testu01/fdist.h>
#include <testu01/wdist.h>
\endcode

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\guisec{Continuous distributions}

\code

double finv_Expon (double u);
\endcode
  \tab
  Returns the inverse of the standard exponential distribution,
$$
     F^{-1}(u) = -\ln (1-u), \qquad  0 \le u \le 1.
$$
 \endtab
\code

double finv_Weibull (double alpha, double u);
\endcode
  \tab
  Returns the inverse of the standard Weibull distribution,
$$
     F^{-1}(u) = \left(-\ln (1-u) \right)^{1/\alpha}, \qquad  0 \le u \le 1.
$$
  Restriction: $\alpha > 0$.
 \endtab
\code


double finv_ExtremeValue (double u);
\endcode
  \tab
  Returns the inverse of the standard extreme value distribution,
$$
     F^{-1}(u) = -\ln (-\ln (u)), \qquad  0 \le u \le 1.
$$
 \endtab
\code

double finv_Logistic (double u);
\endcode
  \tab
  Returns the inverse of the standard logistic distribution,
$$
     F^{-1}(u) = \ln  \left(\frac{u}{1-u}\right), \qquad  0 \le u \le 1.
$$
 \endtab
\code

double finv_Pareto (double c, double u);
\endcode
  \tab
  Returns the inverse of the standard Pareto distribution,
$$
     F^{-1}(u) = \left(\frac 1 {1 - u}\right)^{1/c}, 
          \qquad  0 \le u \le 1.
$$
  Restriction: $c > 0$.
 \endtab
\code


double finv_Normal1 (double u);
\endcode
  \tab  
  Returns an approximation of $\Phi^{-1}(u)$, 
  where $\Phi$ is the standard normal distribution function,
  with mean 0 and variance 1. 
  Uses rational Chebyshev approximations giving at least 15 decimal 
  digits of precision over most of the range \cite{tBLA76a}. 
  Far in the lower tail ($u < 10 ^{-122}$), the precision decreases slowly 
  until for $u < 10 ^{-308}$, the function gives only 11 decimal 
  digits of precision.
 \endtab
\code


double finv_Normal2 (double u);
\endcode
  \tab  
  Returns an approximation of $\Phi^{-1}(u)$, 
  where $\Phi$ is the standard normal distribution function, with mean 0 and
  variance 1. Uses Marsaglia's et al \cite{rMAR94b} method
  with tables lookup. The method works provided that
  the processor respects the IEEE-754 floating-point standard. 
  Returns 6 decimal digits of precision. This function is twice as fast
  as {\tt finv\_Normal1}.
 \endtab
\hide\code


double finv_Normal3 (double u);
\endcode
  \tab  
  Returns an approximation of $\Phi^{-1}(u)$, 
  where $\Phi$ is the standard normal distribution function,
  with mean 0 and variance 1. 
  Uses a rational approximation giving at least 7 decimal digits of
  precision when  $10^{-20} < u < 1 - 10^{-20}$
  (see \cite{sBRA87a,tKEN80a}). This method is slow.
 \endtab
\endhide\code


double finv_LogNormal (double mu, double sigma, double u);
\endcode
  \tab
  Returns the inverse of the lognormal distribution,
$$
     F^{-1}(u) = e^{\mu + \sigma\Phi^{-1} (u)}, 
          \qquad  0 \le u \le 1.
$$
  Restriction: $\sigma > 0$.
 \endtab
\code


double finv_JohnsonSB (double alpha, double beta, double a, double b,
                       double u);
\endcode
  \tab
  Returns the inverse of the Johnson JSB distribution,
$$
     F^{-1}(u) = \frac {a + b\,v}{1 + v},
$$
 where
$$ 
     v = \exp \left({\frac{\Phi^{-1}(u) - \alpha}{\beta}}\right), 
              \qquad  0 \le u \le 1.
$$
 and $\Phi^{-1}$ is the inverse of the standard normal distribution.
 Restrictions: $\beta>0$ and $a < x < b$.
 \endtab
\code


double finv_JohnsonSU (double alpha, double beta, double u);
\endcode
  \tab
  Returns the inverse of the Johnson JSU distribution,
$$
     F^{-1}(u) = \frac {v - 1/v}{2}
$$
  where
$$
      v = \exp \left({\frac{\Phi^{-1}(u) - \alpha}{\beta}}\right), 
          \qquad  0 \le u \le 1.
$$
and $\Phi^{-1}$ is the inverse of the standard normal distribution.
Restriction: $\beta>0$.
 \endtab
\code


double finv_ChiSquare1 (long k, double u);
\endcode
  \tab
  Returns a quick and dirty approximation of $F^{-1}(u)$, 
  where $F$ is the chi-square distribution with $k$ degrees of freedom.
  Uses the approximation given in  Figure L.24 of \cite{sBRA87a}.
 \endtab
\code


double finv_ChiSquare2 (long k, double u);
\endcode
  \tab
  Returns an approximation of $F^{-1}(u)$, where $F$ is the 
  chi-square distribution with $k$ degrees of freedom.
  Uses the approximation given in \cite{tBES75a}
  and in Figure L.23 of \cite{sBRA87a}. This function is up to
  20 times slower than {\tt finv\_ChiSquare1}.
 \endtab
\code


double finv_Student (long n, double u);
\endcode
  \tab
  Returns an approximation of $F^{-1}(u)$, where $F$ is the 
  {\em Student-$t$\/} distribution function with $n$ degrees of freedom.
  Uses an approximation giving at least 
  5 decimal digits of precision when $n \ge 8$ or $n\le 2$, and
  3 decimal digits of precision when $3\le n \le 7$
  (see \cite{tHIL70a} and Figure L.28 of \cite{sBRA87a}).
  \endtab
\code


double finv_BetaSymmetric (double p, double u);
\endcode
  \tab
  Returns a special approximation of $F^{-1}(u)$, where $F(x)$ is the 
  symmetric {\it beta} distribution with shape parameter $p = q$
  as defined in (\ref{eq:Fbeta}). Uses four different hypergeometric series 
  (for the four cases $x$ close to 0 and $p \le 1$,
     $x$ close to 0 and $p > 1$,  $x$ close to 1/2 and $p \le 1$,
  and  $x$ close to 1/2 and $p > 1$)
  to compute the distribution $u = F(x)$,
  which are then solved by Newton's method for the solution of equations.
  For $p > 100000$, uses a normal approximation given in \cite{tPEI68a}.
  Restrictions: $p > 0$ and $0 \le u \le 1$.
 \endtab
\code


double finv_GenericC (wdist_CFUNC F, double par[], double u, int d,
                      int detail);
\endcode
  \tab
   Uses binary search to find the inverse of a generic continuous
   distribution function $F$, evaluated at $u$.
   The parameters of $F$ (if any) are passed in the array {\tt par}.
   The returned value has $d$ decimal digits of precision.
   If {\tt detail > 0}, the procedure will print detailed information
   about the inversion process.
   Restrictions: $0 \le u \le 1$ and $d>0$.
  \endtab

\newpage
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\guisec{Discrete distributions}
\hide
\code

long finv_GenericD1 (fmass_INFO W, double u);
\endcode
  \tab
This function is not finished. Must check that it works for all cases
and also eliminate some ugliness.
   Uses binary search to find the inverse of a generic discrete
   distribution function  evaluated at $u$, and whose values have been
   precomputed and are kept inside the structure $W$.
   The procedure  returns an approximation  of $F^{-1}(u)$. \\
   Restriction: $0 \le u \le 1$.
  \endtab
\code

#if 0
long finv_GenericD2 (wdist_DFUNC F, fmass_INFO W, double u);
#endif
\endcode
  \tab
   Uses binary search to find the inverse of a generic discrete
   distribution function $F$, evaluated at $u$.
   The parameters of $F$ (if any) are passed in the structure {\tt W}.
   The procedure assumes that the variable {\tt F}
   contains the distribution function $F$ and returns an approximation 
   of $F^{-1}(u)$. \\
  \hpierre{Je ne vois pas trop \`a quoi inverser $\bar F$ peut bien servir.
    En tous cas, il faut faire bien attention comment on d\'efinit
    l'inverse dans ce cas. }
  \hrichard {J'ai enlev\'e le parametre comp}
   Restriction: $0 \le u \le 1$.
  \endtab
\endhide
\code


long finv_Geometric (double p, double u);
\endcode
  \tab Returns the inverse of the geometric distribution,
$$
   F^{-1}(u) = \left\lfloor \frac{\ln (1 - u)}{\ln (1 - p)}\right\rfloor,
               \qquad  0 \le u \le 1.
$$
  Restriction: $0 \le p \le 1$.
 \endtab
\code

\hide
#if 0
long finv_Poisson2 (fmass_INFO W, double u);
\endcode
 \tab  Returns the inverse of the Poisson distribution function,
  using the structure {\tt W}, which must have been created previously
  by calling {\tt fmass\_CreatePoisson} with the desired $\lambda$.
  Uses binary search.
 \endtab
\code


long finv_Binomial2 (fmass_INFO W, double u);
\endcode
 \tab  Returns the inverse of the binomial distribution function,
  from the structure {\tt W}, which must have been created previously
  by calling {\tt fmass\_CreateBinomial} with the desired $n$ and $p$.
  Uses binary search.
 \endtab
\code

#endif
#endif
\endhide
\endcode