-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRcauchy.c
190 lines (152 loc) · 4.88 KB
/
LRcauchy.c
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
/*!
\file LRcauchy.c
\brief The Cauchy (or Lorentz) distribution centered on m and width given by 2s
The pseudo-random numbers are distributed from a Cauchy or Lorentz
distribution. It's a pathological distribution where the mean and variance
are undefined, but the distribution has a clear peak and width (given by m
and 2s respectively).
\manonly
PDF(z) = 1/[s*pi * {1 + (z-m)^2 / s)}]
CDF(z) = 1/pi*arctan((x-m)/s) + 1/2
\endmanonly
\f{eqnarray*}{
\mbox{PDF}(x)
&= \frac{1}{ \pi s \left[1 + \left(\frac{x-m}{s} \right)^2 \right]} \\
&= \frac{1}{ \pi s }\left[\frac{s^2}{(x-m)^2 + s^2}\right] \\
\mbox{CDF}(x)
&= \frac{1}{\pi}\arctan \left(\frac{x-m}{s} \right) + \frac{ 1}{ 2}
\f}
The default is m = 0, s = 1; which is called the <b>standard Cauchy
distribution</b>.
\image html CauchyLorentzDistribution.png
\image latex CauchyLorentzDistribution.eps "Cauchy/Lorentz"
The Cauchy-Lorentz distributed random variates can be generated in a couple of
ways. The first and most direct way comes using the inverse of the
\f$ \mbox{CDF}(x) \f$ function, which is simple enough to have an exact
analytic form. However, the computational complexity may cause it to be
slower than the second method.
The second method
uses the polar method, the Marsagalia method, and acceptance/rejection for
generating a Gaussian/Normal variate - the ratio of the two independent
variates gives the Cauchy-Lorentz distribution. However, it relies on
acceptance/rejection to generate random variates confined to the unit circle
centered on the origin with radius 1.
This gives an acceptance ratio to be \f$\frac{\pi}{4} \approx 78.5\% \f$, but
the only arithmetic operations, however, are couple of multiplications and a
single division. This simplicity may be sufficient to overcome the
time to generate rejected samples and the overall method may be
competative in comparison to the first method.
Default values: peak m = 0, half width s = 1
*/
/*
* Copyright 2019 R.K. Owen, Ph.D.
* License see lgpl.md (Gnu Lesser General Public License)
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <math.h>
#include "libran.h"
/* double */
/*!
@brief LRd_cauchy_RAN(LR_obj *o) - double random Cauchy/Lorentz distribution
using the inversion method on the CDF(x).
Default values: peak m = 0, half width s = 1
@param o LR_obj object
@return double
*/
double LRd_cauchy_RAN(LR_obj *o) {
return o->m.d + o->s.d*tan(M_PI * o->ud(o));
}
/*!
@brief LRd_cauchymar_RAN(LR_obj *o) - double random Cauchy/Lorentz distribution
using the polar method and the Marsagalia method and acceptance/rejection for generating a
Gaussian/Normal variate.
Default values: peak m = 0, half width s = 1
@param o LR_obj object
@return double
*/
double LRd_cauchymar_RAN(LR_obj *o) {
double zero = 0.0, one = 1.0, two = 2.0;
double s, z1, z2;
do {
z1 = two*o->ud(o) - one;
z2 = two*o->ud(o) - one;
s = z1*z1 + z2*z2;
} while (s > one || z2 == zero);
return o->m.d + o->s.d*z1/z2;
}
/*!
@brief LRd_cauchy_PDF(LR_obj *o, double x) - double Cauchy/Lorentz probablity distribution function
@param o LR_obj object
@param x value
@return double PDF at x
*/
double LRd_cauchy_PDF(LR_obj *o, double x) {
x -= o->m.d;
return o->s.d * M_1_PI / (x*x + o->s.d * o->s.d);
}
/*!
@brief LRd_cauchy_CDF(LR_obj *o, double x) - double Cauchy/Lorentz cumulative distribution function
@param o LR_obj object
@param x value
@return double CDF at x
*/
double LRd_cauchy_CDF(LR_obj *o, double x) {
double half = 0.5;
x -= o->m.d;
return half + M_1_PI * atan(x/o->s.d);
}
/* float */
/*!
@brief LRf_cauchy_RAN(LR_obj *o) - float random Cauchy/Lorentz distribution
using the inversion method.
Default values: peak m = 0, half width s = 1
@param o LR_obj object
@return float
*/
float LRf_cauchy_RAN(LR_obj *o) {
return o->m.f + o->s.f*tanf(M_PI * o->uf(o));
}
/*!
@brief LRf_cauchymar_RAN(LR_obj *o) - float random Cauchy/Lorentz distribution
using the polar method and the Marsagalia method and acceptance/rejection for generating a
Gaussian/Normal variate.
Default values: peak m = 0, half width s = 1
@param o LR_obj object
@return float
*/
float LRf_cauchymar_RAN(LR_obj *o) {
float zero = 0.0, one = 1.0, two = 2.0;
float s, z1, z2;
do {
z1 = two*o->uf(o) - one;
z2 = two*o->uf(o) - one;
s = z1*z1 + z2*z2;
} while (s > one || z2 == zero);
return o->m.f + o->s.f*z1/z2;
}
/*!
@brief LRf_cauchy_PDF(LR_obj *o, float x) - float Cauchy/Lorentz probablity distribution function
@param o LR_obj object
@param x value
@return float PDF at x
*/
float LRf_cauchy_PDF(LR_obj *o, float x) {
x -= o->m.f;
return o->s.f * M_1_PI / (x*x + o->s.f * o->s.f);
}
/*!
@brief LRf_cauchy_CDF(LR_obj *o, float x) - float Cauchy/Lorentz cumulative distribution function
@param o LR_obj object
@param x value
@return float CDF at x
*/
float LRf_cauchy_CDF(LR_obj *o, float x) {
float half = 0.5;
x -= o->m.f;
return half + M_1_PI * atanf(x/o->s.f);
}
#ifdef __cplusplus
}
#endif