-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRnexp.c
160 lines (128 loc) · 3.24 KB
/
LRnexp.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
/*!
\file LRnexp.c
\brief The negative-exponential distribution has mean \e m and deviation \e m.
The pseudo-random numbers are distributed from a negative exponential
distribution. It's only defined on interval \f$ x \ge 0 \f$ and zero otherwise.
This distribution typically represents the time between events of a
Poisson point process, \e i.e. a process in which events occur continuously
and independently at a constant average rate.
\manonly
PDF(x) = 1/m * exp(-x/m)
CDF(x) = 1 - exp(-x/m)
\endmanonly
\f{eqnarray*}{
\mbox{PDF}(x) &=
\left\{ \begin{array}{ll}
0, & x < 0 \\
\frac{1}{m} e^{-\frac{x}{m}}, & 0 \le x .
\end{array} \right.
\\
\\
\mbox{CDF}(x) &=
\left\{ \begin{array}{ll}
0, & x < 0 \\
1 - e^{-\frac{x}{m}}, & 0 \le x .
\end{array} \right.
\f}
The default is \f$ m = 1 \f$ and \em s will be set to \f$ 1/m \f$
for calculation efficiency.
Do not set \e s when declaring this distribution.
*/
/*
* 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_nexp_RAN(LR_obj *o) - double random negative exponential
distribution using the inversion method.
Default values: peak m = 1.
@param o LR_obj object
@return double
*/
double LRd_nexp_RAN(LR_obj *o) {
double u, zero = 0.0, one = 1.0;
do {
u = o->ud(o);
} while (u == zero);
return - o->m.d*log(u);
}
/*!
@brief LRd_nexp_PDF(LR_obj *o, double x) - double negative exponential
probablity distribution function
@param o LR_obj object
@param x value
@return double PDF at x
*/
double LRd_nexp_PDF(LR_obj *o, double x) {
double zero = 0.0, one = 1.0;
if (isnan(o->s.d))
o->s.d = one/o->m.d;
if (x < zero) return zero;
return o->s.d * exp(- o->s.d * x);
}
/*!
@brief LRd_nexp_CDF(LR_obj *o, double x) - double negative exponential
cumulative distribution function
@param o LR_obj object
@param x value
@return double CDF at x
*/
double LRd_nexp_CDF(LR_obj *o, double x) {
double zero = 0.0, one = 1.0;
if (isnan(o->s.d))
o->s.d = one/o->m.d;
if (x < zero) return zero;
return one - exp(- o->s.d * x);
}
/* float */
/*!
@brief LRf_nexp_RAN(LR_obj *o) - float random negative exponential
distribution using the inversion method.
Default values: peak m = 1.
@param o LR_obj object
@return float
*/
float LRf_nexp_RAN(LR_obj *o) {
float u, zero = 0.0, one = 1.0;
do {
u = o->uf(o);
} while (u == zero);
return - o->m.f*log(u);
}
/*!
@brief LRf_nexp_PDF(LR_obj *o, float x) - float negative exponential
probablity distribution function
@param o LR_obj object
@param x value
@return float PDF at x
*/
float LRf_nexp_PDF(LR_obj *o, float x) {
float zero = 0.0, one = 1.0;
if (isnan(o->s.f))
o->s.f = one/o->m.f;
if (x < zero) return zero;
return o->s.f * expf(- o->s.f * x);
}
/*!
@brief LRf_nexp_CDF(LR_obj *o, float x) - float negative exponential
cumulative distribution function
@param o LR_obj object
@param x value
@return float CDF at x
*/
float LRf_nexp_CDF(LR_obj *o, float x) {
float zero = 0.0, one = 1.0;
if (isnan(o->s.f))
o->s.f = one/o->m.f;
if (x < zero) return zero;
return one - expf(- o->s.f * x);
}
#ifdef __cplusplus
}
#endif