Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[081fbe]: src / modules / bugs / samplers / ConjugateFactory.cc Maximize Restore History

Download this file

ConjugateFactory.cc    156 lines (143 with data), 3.4 kB

  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
#include <config.h>
#include "ConjugateFactory.h"
#include "ConjugateNormal.h"
#include "ConjugateGamma.h"
#include "ConjugateBeta.h"
#include "ConjugateDirichlet.h"
#include "ConjugateMNormal.h"
#include "ConjugateWishart.h"
#include "ConjugateSampler.h"
//#include "Censored.h"
//#include "TruncatedGamma.h"
#include "ShiftedCount.h"
#include "ShiftedMultinomial.h"
#include <graph/StochasticNode.h>
#include <distribution/Distribution.h>
#include <sampler/GraphView.h>
#include <module/ModuleError.h>
#include <string>
using std::string;
namespace bugs {
bool ConjugateFactory::canSample(StochasticNode * snode,
Graph const &graph) const
{
/*
if (Censored::canSample(snode, graph))
return true;
*/
bool ans = false;
switch(getDist(snode)) {
case NORM:
ans = ConjugateNormal::canSample(snode, graph);
break;
case GAMMA: case CHISQ:
ans = ConjugateGamma::canSample(snode, graph);
break;
case EXP:
ans = ConjugateGamma::canSample(snode, graph) ||
ConjugateNormal::canSample(snode, graph);
break;
case BETA:
ans = ConjugateBeta::canSample(snode, graph);
break;
case DIRCH:
ans = ConjugateDirichlet::canSample(snode, graph);
break;
case MNORM:
ans = ConjugateMNormal::canSample(snode, graph);
break;
case WISH:
ans = ConjugateWishart::canSample(snode, graph);
break;
case UNIF:
/*
ans = TruncatedGamma::canSample(snode, graph) ||
ConjugateBeta::canSample(snode, graph);
*/
ans = ConjugateBeta::canSample(snode, graph);
break;
case POIS: case BIN: case NEGBIN:
ans = ShiftedCount::canSample(snode, graph);
break;
case MULTI:
ans = ShiftedMultinomial::canSample(snode, graph);
break;
default:
break;
}
return ans;
}
Sampler *ConjugateFactory::makeSampler(StochasticNode *snode,
Graph const &graph) const
{
GraphView *gv = new GraphView(snode, graph);
ConjugateMethod* method = 0;
/*
if (Censored::canSample(snode, graph)) {
method = new Censored(gv);
}
else {
*/
switch (getDist(snode)) {
case NORM:
method = new ConjugateNormal(gv);
break;
case GAMMA: case CHISQ:
method = new ConjugateGamma(gv);
break;
case EXP:
if (ConjugateGamma::canSample(snode, graph)) {
method = new ConjugateGamma(gv);
}
else if (ConjugateNormal::canSample(snode, graph)) {
method = new ConjugateNormal(gv);
}
else {
throwLogicError("Cannot find conjugate sampler for exponential");
}
break;
case BETA:
method = new ConjugateBeta(gv);
break;
case DIRCH:
method = new ConjugateDirichlet(gv);
break;
case MNORM:
method = new ConjugateMNormal(gv);
break;
case WISH:
method = new ConjugateWishart(gv);
break;
case UNIF:
/*
if (TruncatedGamma::canSample(snode, graph)) {
method = new TruncatedGamma(gv);
}
else
*/
if (ConjugateBeta::canSample(snode, graph)) {
method = new ConjugateBeta(gv);
}
else {
throwLogicError("Cannot find conjugate sampler for uniform");
}
break;
case POIS:
method = new ShiftedCount(gv);
break;
case MULTI:
method = new ShiftedMultinomial(gv);
break;
default:
throwLogicError("Unable to create conjugate sampler");
}
/*
}
*/
return new ConjugateSampler(gv, method);
}
string ConjugateFactory::name() const
{
return "bugs::Conjugate";
}
}