[ff1121]: src / include / graph / StochasticNode.h  Maximize  Restore  History

Download this file

201 lines (188 with data), 7.4 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
#ifndef STOCHASTIC_NODE_H_
#define STOCHASTIC_NODE_H_
#include <graph/Node.h>
#include <distribution/Distribution.h>
namespace jags {
class RNG;
/**
* @short Node defined by the BUGS-language operator ~
*
* Stochastic nodes represent the random variables that are the
* fundamental building blocks of a Bayesian hierarchical model. In
* the BUGS language, they are defined on the left hand side of a
* stochastic relation. For example, the relation
*
* <pre>y ~ dnorm(mu, tau) T(L, U)</pre>
*
* defines y to be a normally distributed random variable with parameters
* mu, tau, L, and U (mean, precision, lower bound, upper bound). The
* last two parameters, defined by the T(,) construct, are optional. If
* they are supplied, then the distribution of the node is truncated
* to lie in the range (L, U). Not all distributions can be truncated.
* Distributions that are only bounded on one side may be specified by
* T(L,) or T(,U).
*
* JAGS allows you to define stochastic nodes that are, in fact, not
* random at all, but are deterministic functions of their parameters.
* A common example is the dinterval distribution
*
* <pre>group[i] ~ dinterval(true[i], cutpoints[1:N])</pre>
*
* where the value of group[i] is determined by where the value of
* true[i] falls in the vector of supplied cutpoints. In this case,
* the stochastic node leads a double life. If it is observed, then it
* is considered a random variable, and generates a likelihood for its
* stochastic parents. If it is unobserved then it is treated as a
* deterministic function of its parents, just as if it were a
* LogicalNode.
*
* @see Distribution
*/
class StochasticNode : public Node {
Distribution const * const _dist;
Node const * const _lower;
Node const * const _upper;
const bool _observed;
const bool _discrete;
virtual void sp(double *lower, double *upper, unsigned int length,
unsigned int chain) const = 0;
protected:
std::vector<std::vector<double const*> > _parameters;
public:
/**
* Constructs a new StochasticNode
*
* @param dim Dimensions of the node
* @param dist Pointer to the distribution
* @param parameters Vector of parameters
* @param lower Pointer to node defining the lower bound. A NULL
* pointer denotes no lower bound.
* @param upper Pointer to node defining the lower bound. A NULL
* pointer denotes no upper bound.
* @param data Optional pointer to an array of data values. If
* this is supplied then each chain is given the same value and
* the node is considered observed.
* @param length Optional length of the array containing the data
* values.
*/
StochasticNode(std::vector<unsigned int> const &dim,
Distribution const *dist,
std::vector<Node const *> const &parameters,
Node const *lower, Node const *upper,
double const *data=0, unsigned int length=0);
~StochasticNode();
/**
* Returns a pointer to the Distribution.
*/
Distribution const *distribution() const;
/**
* Returns the log of the prior density of the StochasticNode
* given the current parameter values.
*
* @param chain Number of chain (starting from zero) for which
* to evaluate log density.
*
* @param type Indicates whether the full probability density
* function is required (PDF_FULL) or whether partial calculations
* are permitted (PDF_PRIOR, PDF_LIKELIHOOD). See PDFType for
* details.
*/
virtual double logDensity(unsigned int chain, PDFType type) const = 0;
/**
* Draws a random sample from the prior distribution of the node
* given the current values of it's parents, and sets the Node
* to that value.
*
* @param rng Random Number Generator object
* @param chain Index umber of chain to modify
*/
virtual void randomSample(RNG *rng, unsigned int chain) = 0;
/**
* Draws a truncated random sample from the prior distribution of
* the node. The lower and upper parameters are pointers to arrays
* that are assumed to be of the correct size, or NULL pointers if
* there is no bound
*
* @param lower Optional lower bound
* @param upper Optional upper bound
*/
virtual void truncatedSample(RNG *rng, unsigned int chain,
double const *lower=0,
double const *upper=0) = 0;
/**
* A deterministic sample for a stochastic node sets it to a
* "typical" value of the prior distribution, given the current
* values of its parents. The exact behaviour depends on the
* Distribution used to define the StochasticNode, but it will
* usually be the prior mean, median, or mode.
*/
virtual void deterministicSample(unsigned int chain) = 0;
/**
* Stochastic nodes always represent random variables in the model.
*/
bool isRandomVariable() const;
/**
* Writes the lower and upper limits of the support of a given
* stochastic node to the supplied arrays. If the node has upper and
* lower bounds then their values are taken into account in the
* calculation.
*
* @param lower pointer to start of an array that will hold the lower
* limit of the support
*
* @param lower pointer to start of an array that will hold the upper
* limit of the support
*
* @param length size of the lower and upper arrays.
*
* @param chain Index number of chain to query
*/
void support(double *lower, double *upper, unsigned int length,
unsigned int chain) const;
double const *lowerLimit(unsigned int chain) const;
double const *upperLimit(unsigned int chain) const;
std::string deparse(std::vector<std::string> const &parameters) const;
bool isDiscreteValued() const;
/**
* A stochastic node is fixed if a non-NULL data argument was supplied
* to the constructor.
*/
bool isFixed() const;
/**
* A stochastic node is always a random variable, and is observed
* if it was constructed with a non-NULL data argument.
*/
RVStatus randomVariableStatus() const;
Node const *lowerBound() const;
Node const *upperBound() const;
/**
* Creates a copy of the stochastic node. Supplying the parents
* of this node as the argument creates an identical copy.
*
* @param parents Parents of the cloned node.
*/
StochasticNode * clone(std::vector<Node const *> const &parents) const;
virtual StochasticNode *
clone(std::vector<Node const *> const &parameters,
Node const *lower, Node const *upper) const = 0;
virtual unsigned int df() const = 0;
//Required for KL in dic
std::vector<double const*> const &parameters(unsigned int chain) const;
};
/**
* Returns true if the upper and lower limits of the support of
* the stochastic node are fixed. Upper and lower bounds are taken
* into account.
*/
bool isSupportFixed(StochasticNode const *snode);
/**
* Indicates whether the distribution of the node is bounded
* either above or below.
*/
bool isBounded(StochasticNode const *node);
/**
* For stochastic nodes, this is a synonym of isFixed
*/
inline bool isObserved(StochasticNode const *s) { return s->isFixed(); }
} /* namespace jags */
#endif /* STOCHASTIC_NODE_H_ */

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks