-
Notifications
You must be signed in to change notification settings - Fork 1
/
CollapsedNIWModel.java
159 lines (141 loc) · 4.48 KB
/
CollapsedNIWModel.java
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
package polya.parametric.normal;
import java.util.Random;
import org.apache.commons.lang3.tuple.Pair;
import org.ejml.simple.SimpleMatrix;
import polya.parametric.CollapsedConjugateModel;
import polya.parametric.HyperParameter;
import polya.parametric.Parameter;
import polya.parametric.SufficientStatistic;
import polya.parametric.TestedModel;
import tutorialj.Tutorial;
import bayonet.distributions.Normal;
import bayonet.math.EJMLUtils;
import bayonet.math.SpecialFunctions;
/**
*
* @author Alexandre Bouchard (alexandre.bouchard@gmail.com)
*
*/
public class CollapsedNIWModel implements CollapsedConjugateModel, TestedModel
{
public static CollapsedNIWModel instance = new CollapsedNIWModel();
private CollapsedNIWModel() {}
/**
* ### CollapsedNIWModel: Implementation of a NIW model
*
* Make sure you check this source carefully: p.46 of
*
* http://cs.brown.edu/~sudderth/papers/sudderthPhD.pdf
*
* Also, for matrix computation you will be using EJML:
*
* https://code.google.com/p/efficient-java-matrix-library/wiki/SimpleMatrix
*
* I suggest to start with ``SimpleMatrix`` operations (but see optional
* questions for suggested optional improvements in this
* area).
*
* #### Method to implement: logPriorDensityAtThetaStar
*
* See
* ``CollapsedConjugateModel``, ``NIWHyperParameter``, as well
* as ``bayonet.SpecialFunctions.multivariateLogGamma()``.
*
*/
@Tutorial(showSource = false, showLink = true)
@Override
public double logPriorDensityAtThetaStar(HyperParameter _hp)
{
NIWHyperParameter hp = (NIWHyperParameter) _hp;
throw new RuntimeException();
}
/**
* #### Method to implement: logLikelihoodGivenThetaStar
*
* See
* ``CollapsedConjugateModel``, ``SufficientStatistic``, as well as
* ``bayonet.distributions.Normal``.
*
* Hint: pick theta* to have mean zero and identity covariance.
*
*/
@Tutorial(showSource = false, showLink = true)
@Override
public double logLikelihoodGivenThetaStar(SufficientStatistic _data)
{
TwoMomentsSufficientStatistics data = (TwoMomentsSufficientStatistics) _data;
throw new RuntimeException();
}
/**
* #### Method to implement: update
*
* This is the last one
* before the end of the parametric part of this exercise!
*
* See
* ``CollapsedConjugateModel``, ``NIWHyperParameter``, ``SufficientStatistic``.
*/
@Tutorial(showSource = false, showLink = true)
@Override
public HyperParameter update(HyperParameter _before, SufficientStatistic _data)
{
NIWHyperParameter before = (NIWHyperParameter) _before;
TwoMomentsSufficientStatistics data = (TwoMomentsSufficientStatistics) _data;
checkCompatible(before, data);
throw new RuntimeException();
}
/**
* Performs a simple sanity check on dimensionality.
* @param before
* @param data
*/
private void checkCompatible(NIWHyperParameter before,
TwoMomentsSufficientStatistics data)
{
if (before.dim() != data.dim())
throw new RuntimeException();
}
/**
* Used for testing purpose. See ParametricsTutorial and TestedModel.
*/
@SuppressWarnings({"unchecked","rawtypes"})
@Override
public Pair<Parameter, SufficientStatistic> generateData(
Random rand,
HyperParameter _hp,
int nDataPoints)
{
NIWHyperParameter hp = (NIWHyperParameter) _hp;
// generate prior
MVNParameter parameter = NIWs.nextNIW(rand, hp);
// generate data
TwoMomentsSufficientStatistics stats = TwoMomentsSufficientStatistics.fromEmpty(hp.dim());
for (int i = 0; i < nDataPoints; i++)
{
SimpleMatrix sample = NIWs.nextMVN(rand, parameter.getMeanParameter(), parameter.getCovarianceParameter());
stats.plusEqual(TwoMomentsSufficientStatistics.fromOnePoint(sample.getMatrix().getData()));
}
Pair result = Pair.of(parameter, stats);
return result;
}
/**
* Used for testing purpose. See ParametricsTutorial and TestedModel
*/
@Override
public Parameter maximumAPosteriori(HyperParameter _hp)
{
NIWHyperParameter hp = (NIWHyperParameter) _hp;
return NIWs.maximumAPosteriori(hp);
}
/**
* Used for testing purpose. See ParametricsTutorial and TestedModel
*/
@Override
public double distance(Parameter _truth, Parameter _reconstructed)
{
MVNParameter truth = (MVNParameter) _truth;
MVNParameter reconstructed = (MVNParameter) _reconstructed;
double norm = NIWs.lInfinityDistance(truth, reconstructed);
return norm;
}
}