2019-01-04 16:29:57 +01:00
|
|
|
|
// Copyright 2007, Ondra Kamenik
|
|
|
|
|
|
|
|
|
|
// Conjugate family for normal distribution
|
|
|
|
|
|
|
|
|
|
/* The main purpose here is to implement a class representing conjugate
|
2019-06-11 16:45:09 +02:00
|
|
|
|
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 y₁…yₙ, then the posterior distribution has the same
|
|
|
|
|
form with the following parameters:
|
|
|
|
|
|
|
|
|
|
κ₀ n
|
|
|
|
|
μₙ = ──── μ₀ + ──── ȳ
|
|
|
|
|
κ₀+n κ₀+n
|
|
|
|
|
|
|
|
|
|
κₙ = κ₀ + n
|
|
|
|
|
|
|
|
|
|
νₙ = ν₀ + n
|
|
|
|
|
|
|
|
|
|
κ₀·n
|
|
|
|
|
Λₙ = Λ₀ + S + ──── (ȳ − μ₀)(ȳ − μ₀)ᵀ
|
|
|
|
|
κ₀+n
|
|
|
|
|
|
2019-01-04 16:29:57 +01:00
|
|
|
|
where
|
2019-06-11 16:45:09 +02:00
|
|
|
|
|
|
|
|
|
1 ₙ
|
|
|
|
|
ȳ = ─ ∑ yᵢ
|
|
|
|
|
n ⁱ⁼¹
|
|
|
|
|
|
|
|
|
|
ₙ
|
|
|
|
|
S = ∑ (yᵢ − ȳ)(yᵢ − ȳ)ᵀ
|
|
|
|
|
ⁱ⁼¹
|
|
|
|
|
*/
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
|
|
#ifndef NORMAL_CONJUGATE_H
|
|
|
|
|
#define NORMAL_CONJUGATE_H
|
|
|
|
|
|
2019-01-08 16:09:25 +01:00
|
|
|
|
#include "twod_matrix.hh"
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
2019-06-11 16:45:09 +02:00
|
|
|
|
/* The class is described by the four parameters: μ, κ, ν and Λ. */
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
|
|
|
|
class NormalConj
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
Vector mu;
|
|
|
|
|
int kappa;
|
|
|
|
|
int nu;
|
|
|
|
|
TwoDMatrix lambda;
|
|
|
|
|
public:
|
|
|
|
|
/* We provide the following constructors: The first constructs diffuse
|
2019-06-11 16:45:09 +02:00
|
|
|
|
(Jeffrey’s) 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. */
|
2019-01-04 16:29:57 +01:00
|
|
|
|
NormalConj(int d);
|
|
|
|
|
NormalConj(const ConstTwoDMatrix &ydata);
|
2019-03-07 18:17:43 +01:00
|
|
|
|
NormalConj(const NormalConj &) = default;
|
|
|
|
|
NormalConj(NormalConj &&) = default;
|
2019-01-04 16:29:57 +01:00
|
|
|
|
|
2019-03-07 18:17:43 +01:00
|
|
|
|
virtual ~NormalConj() = default;
|
2019-01-04 16:29:57 +01:00
|
|
|
|
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
|