dynare/dynare++/kord/normal_conjugate.hh

88 lines
2.3 KiB
C++
Raw Normal View History

// Copyright 2007, Ondra Kamenik
// Conjugate family for normal distribution
/* The main purpose here is to implement a class representing conjugate
distributions for mean and variance of the normal distribution. The class
has two main methods: the first one is to update itself with respect to one
observation, the second one is to update itself with respect to anothe
object of the class. In the both methods, the previous state of the class
corresponds to the prior distribution, and the final state corresponds to
the posterior distribution.
The algebra can be found in Gelman, Carlin, Stern, Rubin (p.87). It goes as
follows. Prior conjugate distribution takes the following form:
Σ InvWishart_ν(Λ¹)
μ|Σ 𝒩(μ,Σ/κ)
If the observations are yy, then the posterior distribution has the same
form with the following parameters:
κ n
μ = μ + ȳ
κ+n κ+n
κ = κ + n
ν = ν + n
κ·n
Λ = Λ + S + (ȳ μ)(ȳ μ)
κ+n
where
1
ȳ = y
n ¹
S = (y ȳ)(y ȳ)
¹
*/
#ifndef NORMAL_CONJUGATE_H
#define NORMAL_CONJUGATE_H
#include "twod_matrix.hh"
/* The class is described by the four parameters: μ, κ, ν and Λ. */
class NormalConj
{
protected:
Vector mu;
int kappa;
int nu;
TwoDMatrix lambda;
public:
/* We provide the following constructors: The first constructs diffuse
(Jeffreys) prior. It sets κ and Λ to zeros, ν to 1 and also the mean μ
to zero (it should not be referenced). The second constructs the posterior
using the diffuse prior and the observed data (columnwise). The third is a
copy constructor. */
NormalConj(int d);
NormalConj(const ConstTwoDMatrix &ydata);
NormalConj(const NormalConj &) = default;
NormalConj(NormalConj &&) = default;
virtual ~NormalConj() = default;
void update(const ConstVector &y);
void update(const ConstTwoDMatrix &ydata);
void update(const NormalConj &nc);
int
getDim() const
{
return mu.length();
}
const Vector &
getMean() const
{
return mu;
}
void getVariance(TwoDMatrix &v) const;
};
#endif