-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCDist.h
373 lines (327 loc) · 9.18 KB
/
CDist.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* This file contains distributions which can be used as priors over parameters.
16/11/2005 Fixed a bug associated with loading more than one prior.
21/10/2005 Further minor updates to header.
20/10/2005 File updated to include William V. Baxter's corrections which allow the file to be compiled under MSVC. Most corrections involve removing redundant const values. */
#ifndef CDIST_H
#define CDIST_H
#include <cmath>
#include <vector>
#include "CMatrix.h"
#include "CNdlInterfaces.h"
#include "CTransform.h"
#include "ndlutil.h"
const string DISTVERSION="0.1";
// Base distribution class.
class CDist : public CMatInterface, public CStreamInterface, public CTransformable {
public:
CDist(){}
virtual ~CDist(){}
unsigned int getNumParams() const
{
return nParams;
}
void setNumParams(unsigned int num)
{
nParams = num;
}
virtual double getParam(unsigned int paramNo) const=0;
virtual void setParam(double val, unsigned int paramNo)=0;
virtual void getGradParams(CMatrix& g) const
{
// This is a dummy function
cerr << "getGradParams should not be used in CDist" << endl;
exit(1);
}
string getBaseType() const
{
return "dist";
}
#ifdef _NDLMATLAB
// returns an mxArray of the dist for use with matlab.
virtual mxArray* toMxArray() const;
virtual void fromMxArray(const mxArray* matlabArray);
// Adds parameters to the mxArray.
virtual void addParamToMxArray(mxArray* matlabArray) const;
// Gets the parameters from the mxArray.
virtual void extractParamFromMxArray(const mxArray* matlabArray);
#endif /* _NDLMATLAB*/
bool equals(const CDist& dist, double tol=ndlutil::MATCHTOL) const;
virtual void writeParamsToStream(ostream& out) const;
virtual void readParamsFromStream(istream& in);
//CDist(CDist& dist);
// get the gradient with respect to an input.
virtual double getGradInput(double x) const=0;
// get the gradient with respect to a matrix of inputs
virtual void getGradInputs(CMatrix& g, const CMatrix& x)
{
DIMENSIONMATCH(g.getRows()==x.getRows());
DIMENSIONMATCH(g.getCols()==x.getCols());
for(unsigned int i=0; i<g.getRows(); i++)
for(unsigned int j=0; j<g.getCols(); j++)
g.setVal(getGradInput(x.getVal(i, j)), i, j);
}
virtual void setInitParam()=0;
// Get log probability at a particualar value
virtual double logProb(double val) const=0;
virtual double logProb(const CMatrix& x) const
{
double ll = 0.0;
for(unsigned int i=0; i<x.getRows(); i++)
for(unsigned int j=0; j<x.getCols(); j++)
ll+=logProb(x.getVal(i, j));
return ll;
}
void setParamName(const string name, unsigned int index)
{
BOUNDCHECK(index<nParams);
if(paramNames.size() == index)
paramNames.push_back(name);
else
{
if(paramNames.size()<index)
paramNames.resize(index+1, "no name");
paramNames[index] = name;
}
}
virtual string getParamName(unsigned int index) const
{
BOUNDCHECK(index<paramNames.size());
return paramNames[index];
}
void setType(string name)
{
type = name;
}
string getType() const
{
return type;
}
void setName(string name)
{
distName = name;
}
string getName() const
{
return distName;
}
private:
void _init();
unsigned int nParams;
string type;
string distName;
vector<string> paramNames;
};
// Read and write dists to streams
void writeDistToStream(const CDist& dist, ostream& out);
CDist* readDistFromStream(istream& in);
// The Gaussian distribution.
class CGaussianDist : public CDist {
public:
CGaussianDist();
CGaussianDist(const CGaussianDist&);
~CGaussianDist();
CGaussianDist* clone() const
{
return new CGaussianDist(*this);
}
double getParam(unsigned int paramNo) const;
void setParam(double val, unsigned int paramNo);
double getGradInput(double x) const;
void setInitParam();
double logProb(double val) const;
private:
void _init();
double precision;
};
// The gamma distribution
class CGammaDist : public CDist {
public:
CGammaDist();
CGammaDist(const CGammaDist&);
~CGammaDist();
CGammaDist* clone() const
{
return new CGammaDist(*this);
}
double getParam(unsigned int paramNo) const;
void setParam(double val, unsigned int paramNo);
double getGradInput(double x) const;
void setInitParam();
double logProb(double val) const;
private:
void _init();
double a;
double b;
};
// A class which stores distributions in a container for priors over parameters.
// An unusual prior used by Wang in the GPDM thesis.
class CWangDist : public CDist {
public:
CWangDist();
CWangDist(const CWangDist&);
~CWangDist();
CWangDist* clone() const
{
return new CWangDist(*this);
}
double getParam(unsigned int paramNo) const;
void setParam(double val, unsigned int paramNo);
double getGradInput(double x) const;
void setInitParam();
double logProb(double val) const;
private:
void _init();
double M;
};
// A class which stores distributions in a container for priors over parameters.
class CParamPriors : CMatInterface {
public:
#ifdef _NDLMATLAB
mxArray* toMxArray() const;
void fromMxArray(const mxArray* distArray);
#endif
void addDist(CDist* dist, unsigned int index)
{
distIndex.push_back(index);
dists.push_back(dist);
}
void clearDists()
{
distIndex.clear();
dists.clear();
}
inline string getDistType(unsigned int ind) const
{
BOUNDCHECK(ind<getNumDists());
return dists[ind]->getType();
}
inline unsigned int getDistIndex(unsigned int ind) const
{
BOUNDCHECK(ind<getNumDists());
return distIndex[ind];
}
inline unsigned int getNumDists() const
{
return dists.size();
}
vector<CDist*> dists;
vector<int> distIndex;
};
// A virtual base class which makes its descendents regluarisable.
class CRegularisable {
public:
virtual ~CRegularisable() {}
// these are the pure virtual functions.
virtual unsigned int getNumParams() const=0;
virtual double getParam(unsigned int paramNo) const=0;
virtual void setParam(double val, unsigned int paramNo)=0;
virtual void getGradParams(CMatrix& g) const=0;
// these are default implementations.
virtual void getParams(CMatrix& params) const
{
DIMENSIONMATCH(params.getRows()==1);
DIMENSIONMATCH(params.getCols()==getNumParams());
for(unsigned int i=0; i<params.getCols(); i++)
params.setVal(getParam(i), i);
}
virtual void setParams(const CMatrix& params)
{
DIMENSIONMATCH(params.getRows()==1);
DIMENSIONMATCH(params.getCols()==getNumParams());
for(unsigned int i=0; i<params.getCols(); i++)
setParam(params.getVal(i), i);
}
virtual void addPriorGrad(CMatrix& g) const
{
DIMENSIONMATCH(g.getRows()==1);
DIMENSIONMATCH(g.getCols()==getNumParams());
double param=0.0;
for(unsigned int i=0; i<distArray.distIndex.size(); i++)
{
param=getParam(distArray.distIndex[i]);
g.addVal(getPriorGradInput(param, i),
distArray.distIndex[i]);
}
}
virtual void writePriorsToStream(ostream& out) const
{
for(unsigned int i=0; i<distArray.distIndex.size(); i++)
{
out << "priorIndex=" << distArray.distIndex[i] << endl;
writeDistToStream(*distArray.dists[i], out);
}
}
virtual void readPriorsFromStream(istream& in, unsigned int numPriors)
{
string line;
vector<string> tokens;
for(unsigned int i=0; i<numPriors; i++)
{
CDist* prior;
getline(in, line);
ndlstrutil::tokenise(tokens, line, "=");
if(tokens.size()>2 || tokens[0]!="priorIndex")
throw ndlexceptions::StreamFormatError("priorIndex");
prior = readDistFromStream(in);
addPrior(prior, atol(tokens[1].c_str()));
tokens.clear();
}
}
virtual double priorLogProb() const
{
double L = 0.0;
double param=0.0;
for(unsigned int i=0; i<distArray.distIndex.size(); i++)
{
param = getParam(distArray.distIndex[i]);
L+=distArray.dists[i]->logProb(param);
}
return L;
}
// These are non-modifiable methods.
inline unsigned int getNumPriors() const
{
return distArray.getNumDists();
}
inline CDist* getPrior(unsigned int ind) const
{
BOUNDCHECK(ind<getNumPriors());
return distArray.dists[ind];
}
inline string getPriorType(unsigned int ind) const
{
return distArray.getDistType(ind);
}
inline unsigned int getPriorIndex(unsigned int ind) const
{
return distArray.getDistIndex(ind);
}
inline double getPriorGradInput(double val, unsigned int ind) const
{
return distArray.dists[ind]->getGradInput(val);
}
void addPrior(CDist* dist, unsigned int index)
{
BOUNDCHECK(index<getNumParams());
distArray.distIndex.push_back(index);
distArray.dists.push_back(dist);
}
void clearPriors()
{
distArray.distIndex.clear();
distArray.dists.clear();
}
#ifdef _NDLMATLAB
mxArray* distsToMxArray() const
{
return distArray.toMxArray();
}
void distsFromMxArray(const mxArray* matlabArray)
{
distArray.fromMxArray(matlabArray);
}
#endif
private:
CParamPriors distArray;
};
#endif