-
Notifications
You must be signed in to change notification settings - Fork 20
/
UPPAllocationProposal.h
239 lines (178 loc) · 7.23 KB
/
UPPAllocationProposal.h
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
236
237
238
#ifndef UPPAllocationProposal_H
#define UPPAllocationProposal_H
#include <set>
#include <string>
#include "Proposal.h"
#include "StochasticNode.h"
namespace RevBayesCore {
/**
* The allocation proposal for the UPP.
*
* Will Freyman 6/30/17
*
*/
template <class mixtureType>
class UPPAllocationProposal : public Proposal {
public:
UPPAllocationProposal( StochasticNode< RbVector<mixtureType> >* n ); //!< constructor
// Basic utility functions
void cleanProposal(void); //!< Clean up proposal
UPPAllocationProposal* clone(void) const; //!< Clone object
double doProposal(void); //!< Perform proposal
const std::string& getProposalName(void) const; //!< Get the name of the proposal for summary printing
double getProposalTuningParameter(void) const;
void prepareProposal(void); //!< Prepare the proposal
void printParameterSummary(std::ostream &o, bool name_only) const; //!< Print the parameter summary
void setProposalTuningParameter(double tp);
void tune(double r); //!< Tune the proposal to achieve a better acceptance/rejection ratio
void undoProposal(void); //!< Reject the proposal
protected:
void swapNodeInternal(DagNode *oldN, DagNode *newN); //!< Swap the DAG nodes on which the Proposal is working on
private:
// parameters
StochasticNode< RbVector<mixtureType> >* variable; //!< The variable the Proposal is working on
size_t old_partition;
std::vector<int> old_value_assignments;
};
}
#include "RandomNumberFactory.h"
#include "RandomNumberGenerator.h"
#include "RbConstants.h"
#include "RbException.h"
#include "ReversibleJumpMixtureConstantDistribution.h"
#include "TypedDagNode.h"
#include "UniformPartitioningDistribution.h"
#include <cmath>
#include <iostream>
/**
* Constructor
*
* Here we simply allocate and initialize the Proposal object.
*/
template <class mixtureType>
RevBayesCore::UPPAllocationProposal<mixtureType>::UPPAllocationProposal( StochasticNode< RbVector<mixtureType> >* n ) : Proposal(),
variable( n ),
old_partition( 0 )
{
// tell the base class to add the node
addNode( variable );
}
/**
* The cleanProposal function may be called to clean up memory allocations after AbstractMove
* decides whether to accept, reject, etc. the proposed value.
*
*/
template <class mixtureType>
void RevBayesCore::UPPAllocationProposal<mixtureType>::cleanProposal( void )
{
}
/**
* The clone function is a convenience function to create proper copies of inherited objected.
* E.g. a.clone() will create a clone of the correct type even if 'a' is of derived type 'b'.
*
* \return A new copy of the proposal.
*/
template <class mixtureType>
RevBayesCore::UPPAllocationProposal<mixtureType>* RevBayesCore::UPPAllocationProposal<mixtureType>::clone( void ) const
{
return new UPPAllocationProposal<mixtureType>( *this );
}
/**
* Get Proposals' name of object
*
* \return The Proposals' name.
*/
template <class mixtureType>
const std::string& RevBayesCore::UPPAllocationProposal<mixtureType>::getProposalName( void ) const
{
static std::string name = "UPP-Allocation";
return name;
}
template <class mixtureType>
double RevBayesCore::UPPAllocationProposal<mixtureType>::getProposalTuningParameter( void ) const
{
// this proposal has no tuning parameter
return RbConstants::Double::nan;
}
/**
* Perform the proposal.
*
* The reversible jump proposal switches the current "dimension".
*
* \return The hastings ratio.
*/
template <class mixtureType>
double RevBayesCore::UPPAllocationProposal<mixtureType>::doProposal( void )
{
UniformPartitioningDistribution<mixtureType>& dist = static_cast<UniformPartitioningDistribution<mixtureType> &>( variable->getDistribution() );
// get the current index
old_partition = dist.getCurrentIndex();
// get the value assignments
old_value_assignments = dist.getValueAssignments();
// draw a new random partition
dist.redrawValue();
return 0.0;
}
/**
*
*/
template <class mixtureType>
void RevBayesCore::UPPAllocationProposal<mixtureType>::prepareProposal( void )
{
}
/**
* Print the summary of the Proposal.
*
* The summary just contains the current value of the tuning parameter.
* It is printed to the stream that it passed in.
*
* \param[in] o The stream to which we print the summary.
*/
template <class mixtureType>
void RevBayesCore::UPPAllocationProposal<mixtureType>::printParameterSummary(std::ostream &o, bool name_only) const
{
// nothing to print
}
/**
* Reject the Proposal.
*
* Since the Proposal stores the previous value and it is the only place
* where complex undo operations are known/implement, we need to revert
* the value of the variable/DAG-node to its original value.
*/
template <class mixtureType>
void RevBayesCore::UPPAllocationProposal<mixtureType>::undoProposal( void )
{
UniformPartitioningDistribution<mixtureType>& dist = static_cast<UniformPartitioningDistribution<mixtureType> &>( variable->getDistribution() );
dist.setCurrentIndex( (int)old_partition );
dist.setValueAssignments( old_value_assignments );
}
/**
* Swap the current variable for a new one.
*
* \param[in] oldN The old variable that needs to be replaced.
* \param[in] newN The new RevVariable.
*/
template <class mixtureType>
void RevBayesCore::UPPAllocationProposal<mixtureType>::swapNodeInternal(DagNode *oldN, DagNode *newN)
{
variable = static_cast<StochasticNode< RbVector<mixtureType> >* >(newN) ;
}
template <class mixtureType>
void RevBayesCore::UPPAllocationProposal<mixtureType>::setProposalTuningParameter(double tp)
{
// this proposal has no tuning parameter: nothing to do
}
/**
* Tune the Proposal to accept the desired acceptance ratio.
*
* The acceptance ratio for this Proposal should be around 0.44.
* If it is too large, then we increase the proposal size,
* and if it is too small, then we decrease the proposal size.
*/
template <class mixtureType>
void RevBayesCore::UPPAllocationProposal<mixtureType>::tune( double rate )
{
// nothing to do here.
}
#endif