-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetropolis_hasting_sampler.hpp
More file actions
235 lines (216 loc) · 8.7 KB
/
Copy pathmetropolis_hasting_sampler.hpp
File metadata and controls
235 lines (216 loc) · 8.7 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* 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.
*
*/
#pragma once
#include <geode/stochastic/common.hpp>
#include <geode/stochastic/sampling/mcmc/models/gibbs_energy.hpp>
#include <geode/stochastic/sampling/mcmc/proposal/proposal_kernel.hpp>
namespace geode
{
enum struct MHDecision
{
Accepted,
Rejected,
Undecided
};
template < typename ObjectType >
struct StepResult
{
MHDecision decision{ MHDecision::Undecided };
MoveType move_type{ MoveType::Invalid };
double log_accept{ -std::numeric_limits< double >::infinity() };
double delta_log_energy{ 0.0 };
};
template < typename ObjectType >
class MetropolisHastings
{
public:
MetropolisHastings(
const EnergyTermCollection< ObjectType >& energy_term_collection,
std::unique_ptr< ProposalKernel< ObjectType > > proposal_kernel )
: gibbs_energy_{ energy_term_collection },
proposal_kernel_( std::move( proposal_kernel ) )
{
OPENGEODE_ASSERT(
proposal_kernel_ != nullptr, "[MH] null proposal kernel" );
}
StepResult< ObjectType > step(
ObjectSets< ObjectType >& state, RandomEngine& engine ) const
{
Proposal< ObjectType > proposal =
proposal_kernel_->propose( state, engine );
const auto& move_type = proposal.proposed_move.type;
if( move_type == MoveType::Birth )
{
return birth_step( proposal, state, engine );
}
if( move_type == MoveType::Death )
{
return death_step( proposal, state, engine );
}
if( move_type == MoveType::Change )
{
return change_step( proposal, state, engine );
}
return StepResult< ObjectType >{};
}
void walk( ObjectSets< ObjectType >& state,
RandomEngine& engine,
index_t nb_steps ) const
{
for( const auto count : geode::Range{ nb_steps } )
{
geode_unused( count );
step( state, engine );
}
}
ObjectSets< ObjectType > walk_copy( ObjectSets< ObjectType > initial,
RandomEngine& engine,
index_t nb_steps ) const
{
walk( initial, engine, nb_steps );
return initial;
}
double beta() const
{
return beta_;
}
void set_beta( double b )
{
OPENGEODE_EXCEPTION( b >= 0.0, "[MH] beta must be >= 0" );
if( b == 0 )
{
geode::Logger::info(
"[Metropolis Hastings] - beta == 0 all move will be "
"accepted - Uniform sampling." );
}
if( b < 1 )
{
geode::Logger::info(
"[Metropolis Hastings] - beta < 1 moves that increase "
"energy are "
"more likely to be accepted - Hot system introduce "
"randomness for exploration." );
}
if( b == 1 )
{
geode::Logger::info( "[Metropolis Hastings] - beta == 1 "
"default choice no temperature "
"- only consider energy." );
}
if( b > 1 )
{
geode::Logger::info(
"[Metropolis Hastings] - beta > 1 moves that increase "
"energy are "
"less likely to be accepted - Cold system to ensure "
"convergence but may find local minimum randomness." );
}
beta_ = b;
}
static double acceptance_prob_helper( double log_accept )
{
if( std::isnan( log_accept ) )
return 0.0;
if( log_accept >= 0.0 )
return 1.0;
// prevent exponential overflow
constexpr double LOG_MIN = -745.0;
if( log_accept < LOG_MIN )
return 0.0;
return std::exp( log_accept );
}
private:
const double compute_log_accept( const double deltaU,
const ProposalProbabilities& proposal_probas ) const
{
return -beta_ * deltaU + proposal_probas.transition_probability();
}
template < typename ApplyMove >
StepResult< ObjectType > accept_or_reject(
Proposal< ObjectType >& proposal,
ObjectSets< ObjectType >& state,
RandomEngine& engine,
const double delta_log_energy,
ApplyMove&& apply_move ) const
{
const auto& proposed_move = proposal.proposed_move;
StepResult< ObjectType > step_result;
step_result.move_type = proposed_move.type;
step_result.delta_log_energy = delta_log_energy;
step_result.log_accept = compute_log_accept(
delta_log_energy, proposed_move.proposal_probabilities );
double log_u = engine.sample_log();
step_result.decision = ( log_u < step_result.log_accept )
? MHDecision::Accepted
: MHDecision::Rejected;
if( step_result.decision == MHDecision::Accepted )
apply_move( state, proposal );
return step_result;
}
StepResult< ObjectType > birth_step( Proposal< ObjectType >& proposal,
ObjectSets< ObjectType >& state,
RandomEngine& engine ) const
{
const auto new_object = proposal.new_object();
const auto delta_log_energy =
gibbs_energy_.delta_log_add( state, new_object );
return accept_or_reject( proposal, state, engine, delta_log_energy,
[]( auto& state, auto& proposal ) {
state.add_free_object(
std::move( proposal.proposed_move.new_object.value() ),
proposal.set_id );
} );
};
StepResult< ObjectType > death_step( Proposal< ObjectType >& proposal,
ObjectSets< ObjectType >& state,
RandomEngine& engine ) const
{
const auto old_object_id = proposal.old_object_id();
const auto delta_log_energy =
gibbs_energy_.delta_log_remove( state, old_object_id );
return accept_or_reject( proposal, state, engine, delta_log_energy,
[]( auto& state, auto& proposal ) {
state.remove_free_object( proposal.old_object_id() );
} );
};
StepResult< ObjectType > change_step( Proposal< ObjectType >& proposal,
ObjectSets< ObjectType >& state,
RandomEngine& engine ) const
{
const auto new_object = proposal.new_object();
const auto old_object_id = proposal.old_object_id();
const auto delta_log_energy = gibbs_energy_.delta_log_change(
state, old_object_id, new_object );
return accept_or_reject( proposal, state, engine, delta_log_energy,
[]( auto& state, auto& proposal ) {
state.update_free_object( proposal.old_object_id(),
std::move(
proposal.proposed_move.new_object.value() ) );
} );
};
private:
GibbsEnergy< ObjectType > gibbs_energy_;
std::unique_ptr< ProposalKernel< ObjectType > > proposal_kernel_;
double beta_{ 1.0 };
};
} // namespace geode