-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_sampler.cpp
More file actions
217 lines (200 loc) · 8.36 KB
/
Copy pathdouble_sampler.cpp
File metadata and controls
217 lines (200 loc) · 8.36 KB
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
/*
* Copyright (c) 2019 - 2025 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <geode/stochastic/sampling/direct/double_sampler.hpp>
#include <geode/geometry/angle.hpp>
#include <geode/stochastic/common.hpp>
#include <geode/stochastic/sampling/random_engine.hpp>
namespace
{
struct DistributionParams
{
double min;
double max;
double mean;
double std;
};
DistributionParams complete_distribution_params(
const geode::DoubleSampler::DistributionDescription& d )
{
DistributionParams p{};
if( d.min_value && d.max_value )
{
p.min = *d.min_value;
p.max = *d.max_value;
p.mean = ( p.min + p.max ) / 2.0;
p.std = ( p.max - p.min ) / std::sqrt( 12.0 );
return p;
}
if( d.mean && d.standard_deviation )
{
p.mean = *d.mean;
p.std = *d.standard_deviation;
p.min = p.mean - std::sqrt( 3.0 ) * p.std;
p.max = p.mean + std::sqrt( 3.0 ) * p.std;
return p;
}
throw geode::OpenGeodeException(
"[DistributionDescripption] Incomplete distribution description: "
"need at least (min,max) or (mean,std)." );
return p;
}
double compute_kappa_von_mises( double standard_deviation_rad )
{
geode::Logger::info(
"[VonMises] approximate concentation (kappa) from with "
"1/(std*std)." );
OPENGEODE_EXCEPTION( standard_deviation_rad > geode::GLOBAL_EPSILON,
"Cannot evaluate the VonMises concentration since standard "
"deviation is equal to ",
standard_deviation_rad, " (in rad)." );
return 1.0 / ( standard_deviation_rad * standard_deviation_rad );
}
} // namespace
namespace geode
{
struct DistributionTypeHasher
{
std::size_t operator()( const DistributionType& d ) const noexcept
{
// Use the underlying string from NamedType
return absl::Hash< std::string >{}( d.get() );
}
};
using DistributionFactory = std::function< DoubleSampler::Distribution(
const DoubleSampler::DistributionDescription& ) >;
static absl::flat_hash_map< DistributionType, // key type
DistributionFactory, // value type
DistributionTypeHasher // custom hasher
>
distribution_registry = {
{ UniformClosed< double >::distribution_type_static(),
[]( const DoubleSampler::DistributionDescription& d ) {
auto p = complete_distribution_params( d );
UniformClosed< double > dist;
dist.min_value = p.min;
dist.max_value = p.max;
return dist;
} },
{ UniformClosedOpen< double >::distribution_type_static(),
[]( const DoubleSampler::DistributionDescription& d ) {
auto p = complete_distribution_params( d );
UniformClosedOpen< double > dist;
dist.min_value = p.min;
dist.max_value = p.max;
return dist;
} },
{ Gaussian::distribution_type_static(),
[]( const DoubleSampler::DistributionDescription& d ) {
auto p = complete_distribution_params( d );
Gaussian dist;
dist.mean = p.mean;
dist.standard_deviation = p.std;
return dist;
} },
{ TruncatedGaussian::distribution_type_static(),
[]( const DoubleSampler::DistributionDescription& d ) {
auto p = complete_distribution_params( d );
TruncatedGaussian dist;
dist.mean = p.mean;
dist.standard_deviation = p.std;
dist.min_value = p.min;
dist.max_value = p.max;
return dist;
} },
{ VonMises::distribution_type_static(),
[]( const DoubleSampler::DistributionDescription& d ) {
OPENGEODE_EXCEPTION( d.mean && d.standard_deviation,
"[DoubleSampler] - Provide mean and Standard deviation "
"to set up a VonMises Distribution." );
VonMises dist;
dist.mean = *d.mean;
dist.concentration =
compute_kappa_von_mises( *d.standard_deviation );
return dist;
} },
};
DoubleSampler::Distribution DoubleSampler::create_distribution(
const DistributionDescription& desc )
{
auto it = distribution_registry.find( desc.distribution_type );
if( it == distribution_registry.end() )
throw geode::OpenGeodeException( absl::StrCat(
"Unknown distribution type: ", desc.distribution_type.get() ) );
return it->second( desc );
}
DoubleSampler::Distribution
DoubleSampler::create_rad_angle_distribution_from_degree(
const DistributionDescription& desc_deg )
{
DistributionDescription desc_rad = desc_deg;
if( desc_rad.mean )
{
auto mean_angle = Angle::create_from_degrees( *( desc_rad.mean ) );
desc_rad.mean = mean_angle.normalized_between_0_and_2pi().radians();
}
if( desc_rad.standard_deviation )
{
auto std_angle =
Angle::create_from_degrees( *( desc_rad.standard_deviation ) );
desc_rad.standard_deviation =
std_angle.normalized_between_0_and_2pi().radians();
}
if( desc_rad.min_value )
{
auto min_angle =
Angle::create_from_degrees( *( desc_rad.min_value ) );
desc_rad.min_value =
min_angle.normalized_between_0_and_2pi().radians();
}
if( desc_rad.max_value )
{
auto max_angle =
Angle::create_from_degrees( *( desc_rad.max_value ) );
desc_rad.max_value =
max_angle.normalized_between_0_and_2pi().radians();
}
// Call the general create_distribution
return create_distribution( desc_rad );
}
double DoubleSampler::sample(
RandomEngine& engine, const Distribution& dist )
{
return std::visit(
[&engine]( auto&& d ) {
using D = std::decay_t< decltype( d ) >;
if constexpr( std::is_same_v< D, UniformClosed< double > > )
return engine.sample_uniform< double >( d );
if constexpr( std::is_same_v< D, UniformClosedOpen< double > > )
return engine.sample_uniform< double >( d );
if constexpr( std::is_same_v< D, Gaussian > )
return engine.sample_gaussian( d );
if constexpr( std::is_same_v< D, TruncatedGaussian > )
return engine.sample_truncated_gaussian( d );
if constexpr( std::is_same_v< D, VonMises > )
return engine.sample_von_mises( d );
throw OpenGeodeException( "DoubleSampler - Unsupported "
"distribution for double" );
},
dist );
}
} // namespace geode