-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcauchy_dist.hpp
232 lines (210 loc) · 8.15 KB
/
cauchy_dist.hpp
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
// Copyright (c) 2000-2024, Heiko Bauke
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
#if !(defined TRNG_CAUCHY_DIST_HPP)
#define TRNG_CAUCHY_DIST_HPP
#include <trng/cuda.hpp>
#include <trng/constants.hpp>
#include <trng/limits.hpp>
#include <trng/utility.hpp>
#include <trng/math.hpp>
#include <ostream>
#include <istream>
#include <iomanip>
#include <cerrno>
#include <ciso646>
namespace trng {
// uniform random number generator class
template<typename float_t = double>
class cauchy_dist {
public:
using result_type = float_t;
class param_type {
private:
result_type theta_{1}, eta_{0};
public:
TRNG_CUDA_ENABLE
result_type theta() const { return theta_; }
TRNG_CUDA_ENABLE
void theta(result_type theta_new) { theta_ = theta_new; }
TRNG_CUDA_ENABLE
result_type eta() const { return eta_; }
TRNG_CUDA_ENABLE
void eta(result_type eta_new) { eta_ = eta_new; }
TRNG_CUDA_ENABLE
param_type() = default;
TRNG_CUDA_ENABLE
explicit param_type(result_type theta, result_type eta) : theta_(theta), eta_(eta) {}
friend class cauchy_dist;
// EqualityComparable concept
friend TRNG_CUDA_ENABLE inline bool operator==(const param_type &P1,
const param_type &P2) {
return P1.theta_ == P2.theta_ and P1.eta_ == P2.eta_;
}
friend TRNG_CUDA_ENABLE inline bool operator!=(const param_type &P1,
const param_type &P2) {
return not(P1 == P2);
}
// Streamable concept
template<typename char_t, typename traits_t>
friend std::basic_ostream<char_t, traits_t> &operator<<(
std::basic_ostream<char_t, traits_t> &out, const param_type &P) {
std::ios_base::fmtflags flags(out.flags());
out.flags(std::ios_base::dec | std::ios_base::fixed | std::ios_base::left);
out << '(' << std::setprecision(math::numeric_limits<float_t>::digits10 + 1)
<< P.theta() << ' ' << P.eta() << ')';
out.flags(flags);
return out;
}
template<typename char_t, typename traits_t>
friend std::basic_istream<char_t, traits_t> &operator>>(
std::basic_istream<char_t, traits_t> &in, param_type &P) {
float_t theta, eta;
std::ios_base::fmtflags flags(in.flags());
in.flags(std::ios_base::dec | std::ios_base::fixed | std::ios_base::left);
in >> utility::delim('(') >> theta >> utility::delim(' ') >> eta >> utility::delim(')');
if (in)
P = param_type(theta, eta);
in.flags(flags);
return in;
}
};
private:
param_type P;
// inverse cumulative density function
TRNG_CUDA_ENABLE
result_type icdf_(result_type x) const {
const result_type t{x * math::constants<result_type>::pi};
return P.eta() - P.theta() * math::cos(t) / math::sin(t);
}
public:
// constructor
TRNG_CUDA_ENABLE
explicit cauchy_dist(result_type theta, result_type eta) : P{theta, eta} {}
TRNG_CUDA_ENABLE
explicit cauchy_dist(const param_type &P) : P{P} {}
// reset internal state
TRNG_CUDA_ENABLE
void reset() {}
// random numbers
template<typename R>
TRNG_CUDA_ENABLE result_type operator()(R &r) {
return icdf_(utility::uniformoo<result_type>(r));
}
template<typename R>
TRNG_CUDA_ENABLE result_type operator()(R &r, const param_type &P) {
cauchy_dist g(P);
return g(r);
}
// property methods
TRNG_CUDA_ENABLE
result_type min() const { return -math::numeric_limits<result_type>::infinity(); }
TRNG_CUDA_ENABLE
result_type max() const { return math::numeric_limits<result_type>::infinity(); }
TRNG_CUDA_ENABLE
const param_type ¶m() const { return P; }
TRNG_CUDA_ENABLE
void param(const param_type &p_new) { P = p_new; }
TRNG_CUDA_ENABLE
result_type theta() const { return P.theta(); }
TRNG_CUDA_ENABLE
void theta(result_type theta_new) { P.theta(theta_new); }
TRNG_CUDA_ENABLE
result_type eta() const { return P.eta(); }
TRNG_CUDA_ENABLE
void eta(result_type eta_new) { P.eta(eta_new); }
// probability density function
TRNG_CUDA_ENABLE
result_type pdf(result_type x) const {
x -= P.eta();
x /= P.theta();
return math::constants<result_type>::one_over_pi / (1 + x * x) / P.theta();
}
// cumulative density function
TRNG_CUDA_ENABLE
result_type cdf(result_type x) const {
x -= P.eta();
x /= P.theta();
return math::constants<result_type>::one_over_pi * math::atan(x) +
result_type(1) / result_type(2);
}
// inverse cumulative density function
TRNG_CUDA_ENABLE
result_type icdf(result_type x) const {
if (x <= 0 or x >= 1) {
#if !(defined TRNG_CUDA)
errno = EDOM;
#endif
return math::numeric_limits<result_type>::quiet_NaN();
}
if (x == 0)
return -math::numeric_limits<result_type>::infinity();
if (x == 1)
return math::numeric_limits<result_type>::infinity();
return icdf_(x);
}
};
// -------------------------------------------------------------------
// EqualityComparable concept
template<typename float_t>
TRNG_CUDA_ENABLE inline bool operator==(const cauchy_dist<float_t> &g1,
const cauchy_dist<float_t> &g2) {
return g1.param() == g2.param();
}
template<typename float_t>
TRNG_CUDA_ENABLE inline bool operator!=(const cauchy_dist<float_t> &g1,
const cauchy_dist<float_t> &g2) {
return g1.param() != g2.param();
}
// Streamable concept
template<typename char_t, typename traits_t, typename float_t>
std::basic_ostream<char_t, traits_t> &operator<<(std::basic_ostream<char_t, traits_t> &out,
const cauchy_dist<float_t> &g) {
std::ios_base::fmtflags flags(out.flags());
out.flags(std::ios_base::dec | std::ios_base::fixed | std::ios_base::left);
out << "[cauchy " << g.param() << ']';
out.flags(flags);
return out;
}
template<typename char_t, typename traits_t, typename float_t>
std::basic_istream<char_t, traits_t> &operator>>(std::basic_istream<char_t, traits_t> &in,
cauchy_dist<float_t> &g) {
typename cauchy_dist<float_t>::param_type P;
std::ios_base::fmtflags flags(in.flags());
in.flags(std::ios_base::dec | std::ios_base::fixed | std::ios_base::left);
in >> utility::ignore_spaces() >> utility::delim("[cauchy ") >> P >> utility::delim(']');
if (in)
g.param(P);
in.flags(flags);
return in;
}
} // namespace trng
#endif