-
Notifications
You must be signed in to change notification settings - Fork 28
/
CIvm.h
265 lines (218 loc) · 7.15 KB
/
CIvm.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
This file and CIvm.cpp contain classes for representing Gaussian processes using the IVM. The model relies on the CKern class and the CNoise class to define the kernel and the noise model.
01/03/2005 Gregg Keller had problems loading in a data set. It turned out it was when the last feature in an active point was 0 no end of line character was placed on the file. This is fixed as part of release 0.12.
23/10/2005 Modifications for compiling under Visual C++, by Neil.*/
#ifndef CIVM_H
#define CIVM_H
#include "CMltools.h"
using namespace std;
const double NULOW=1e-16;
const string IVMVERSION="0.2";
class CIvm : public CMapModel, public CProbabilisticOptimisable, public CStreamInterface, public CMatInterface
{
public:
CIvm();
// Constructor given a filename.
// CIvm(const string modelFileName, int verbos=2);
// Constructor given a kernel and a noise model.
CIvm(CMatrix* inData, CMatrix* targetData,
CKern* kernel, CNoise* noiseModel, int selectCrit,
unsigned int dVal, int verbos=2);
CIvm(CMatrix& actX, CMatrix& actY,
CMatrix& mmat, CMatrix& betamat,
vector<unsigned int> actSet, CKern* kernel,
CNoise* noiseModel, int selectCrit=ENTROPY,
int verbos=2);
#ifdef _NDLMATLAB
// Constructor using file containing ivmInfo.
CIvm(CMatrix* inData,
CMatrix* targetData,
CKern* kernel,
CNoise* noiseModel,
const string ivmInfoFile,
const string ivmInfoVariable,
int verbos=2);
#endif
void writeParamsToStream(ostream& os) const;
void readParamsFromStream(istream& is);
inline unsigned int getNumActive() const
{
return numActive;
}
void setNumActive(unsigned int val)
{
numActive = val;
}
// initialise the model.
void init();
// Initialise the storeage for the model.
void initStoreage();
// Set the initial values for the model.
void initVals();
void selectPoints(); // select active set points.
void addPoint(unsigned int index); // add a point to the model.
void updateSite(unsigned int index); // update the site parameters at index.
void updateM(unsigned int index); // update M at index.
unsigned int selectPointAdd(); // select a point to add to active set.
unsigned int entropyPointAdd(); // add a point selected by entropy change.
unsigned int randomPointAdd(); // add a point selected randomly.
double entropyChangeAdd(unsigned int) const; // entropy change associated with adding a point
unsigned int selectPointRemove(); // select a point to remove from the active set.
unsigned int entropyPointRemove(); // remove a point selected by entropy change.
unsigned int randomPointRemove(); // remove a point selected randomly.
double entropyChangeRemove(unsigned int) const; // entropy change associated with removing a point
void test(const CMatrix& ytest, const CMatrix& Xin) const;
void likelihoods(CMatrix& pout, CMatrix& yTest, const CMatrix& Xin) const;
double logLikelihood(const CMatrix& yTest, const CMatrix& Xin) const;
// For MapModel interface.
void out(CMatrix& yPred, const CMatrix& inData) const;
void out(CMatrix& yPred, CMatrix& probPred, const CMatrix& inData) const;
double outGradParams(CMatrix& g, const CMatrix& Xin, unsigned int pointNo, unsigned int outputNo) const;
double outGradX(CMatrix& g, const CMatrix& Xin, unsigned int pointNo, unsigned int outputNo) const;
void posteriorMeanVar(CMatrix& mu, CMatrix& varSigma, const CMatrix& X) const;
string getNoiseName() const
{
return pnoise->getName();
}
inline void changeEntropy(double val)
{
cumEntropy += val;
lastEntropyChange = val;
}
// Gradient routines
void updateCovGradient(unsigned int index) const;
inline void setTerminate(const bool val)
{
terminate = val;
}
inline void setEpUpdate(const bool val)
{
epUpdate = val;
}
inline bool isTerminate() const
{
return terminate;
}
inline bool isEpUpdate() const
{
return epUpdate;
}
void updateNuG();
// update K with the kernel computed from the active points.
void updateK() const;
// update invK with the inverse of the kernel plus beta terms computed from the active points.
void updateInvK(unsigned int index=0) const;
// compute the approximation to the log likelihood.
double logLikelihood() const;
// compute the gradients of the approximation wrt parameters.
double logLikelihoodGradient(CMatrix& g) const;
void optimise(unsigned int maxIters=15, unsigned int kernIters=100, unsigned int noiseIters=100);
bool equals(const CIvm& model, double tol=ndlutil::MATCHTOL) const;
void display(ostream& os) const;
inline unsigned int getOptNumParams() const
{
return pkern->getNumParams();
}
void getOptParams(CMatrix& param) const
{
pkern->getTransParams(param);
}
void setOptParams(const CMatrix& param)
{
pkern->setTransParams(param);
}
// Whether noise model on outputs is spherical.
bool isSpherical() const
{
return pnoise->isSpherical();
}
string getTypeSelection() const
{
switch(selectionCriterion)
{
case ENTROPY:
return "entropy";
case RENTROPY:
return "rentropy";
case RANDOM:
return "random";
default:
cerr << "Unrecognised selection criterion." << endl;
}
}
void setTypeSelection(const string val)
{
if(val=="entropy")
selectionCriterion=ENTROPY;
else if(val=="rentropy")
selectionCriterion=RENTROPY;
else if(val=="random")
selectionCriterion=RANDOM;
else
cerr << "Unrecognised selection criterion " << val << "." << endl;
}
void setTypeSelection(unsigned int val)
{
BOUNDCHECK(val>=ENTROPY && val<=RANDOM);
selectionCriterion=val;
}
#ifdef _NDLMATLAB
mxArray* toMxArray() const;
void fromMxArray(const mxArray* matlabArray);
#endif
double getActiveX(unsigned int i, unsigned int j) const
{
return activeX.getVal(i, j);
}
unsigned int getActivePoint(unsigned int i) const
{
return activeSet[i];
}
// arguably these are noise model associated.
CMatrix* pX;
CMatrix* py;
CMatrix nu;
CMatrix g;
CMatrix Kstore;
// these are IVM associated.
CMatrix m;
CMatrix beta;
// these really just provide local storage
mutable CMatrix covGrad;
mutable CMatrix invK;
mutable double logDetK;
mutable CMatrix K;
mutable CMatrix s;
mutable CMatrix a;
mutable CMatrix ainv;
CMatrix activeX;
CMatrix activeY;
CMatrix* M;
CMatrix* L;
CMatrix* Linv;
// COptions options;
vector<unsigned int> activeSet;
vector<unsigned int> inactiveSet;
CKern* pkern;
CNoise* pnoise;
enum{ENTROPY, RENTROPY, RANDOM};
private:
void _init();
bool terminate;
bool epUpdate;
bool loadedModel;
unsigned int numCovStruct;
unsigned int numActive;
unsigned int numTarget;
unsigned int numData;
double lastEntropyChange;
double cumEntropy;
int selectionCriterion;
string type;
};
// Functions which operate on the object
void writeIvmToStream(const CIvm& model, ostream& out);
void writeIvmToFile(const CIvm& model, const string modelFileName, const string comment="");
CIvm* readIvmFromStream(istream& in);
CIvm* readIvmFromFile(const string modelfileName, int verbosity=2);
#endif