File: mixtureDistribution.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (311 lines) | stat: -rw-r--r-- 10,161 bytes parent folder | download | duplicates (5)
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
#include "mixtureDistribution.h"
#include "generalGammaDistributionLaguerre.h"
#include "talRandom.h"
#include "someUtil.h"
#include "errorMsg.h"

#include <cmath>


mixtureDistribution::mixtureDistribution(const vector<generalGammaDistribution*>& components, const Vdouble& componentsProb, quadratureType gammaType)
{
	if (components.size() < 1)
		errorMsg::reportError("the number of Gamma components must be positive");
	
	_components.clear();
	for (int i = 0; i < components.size(); ++i)
	{
		generalGammaDistribution* comp = static_cast<generalGammaDistribution*>(components[i]->clone());
		_components.push_back(comp);
	}

	_globalRate = 1.0;
	setComponentsProb(componentsProb);
}


//init the mixture with componentsNum components - the alpha, beta, and probability for each component is assigned "randomly" 
mixtureDistribution::mixtureDistribution(int componentsNum, int categoriesNumInComponent, quadratureType gammaType/*=LAGUERRE*/, MDOUBLE maxAlpha/*=5.0*/, MDOUBLE maxBeta/*=5.0*/)
{
	if (componentsNum < 1)
		errorMsg::reportError("the number of Gamma components must be positive");

	_components.clear();
	Vdouble componentsProb(componentsNum, 0);
	for (int i = 0; i < componentsNum; ++i)
	{
		MDOUBLE alpha = talRandom::giveRandomNumberBetweenZeroAndEntry(maxAlpha);
		MDOUBLE beta = talRandom::giveRandomNumberBetweenZeroAndEntry(maxBeta);
		componentsProb[i] = talRandom::giveRandomNumberBetweenZeroAndEntry(1.0);
		generalGammaDistribution* pComp; 
		switch (gammaType)
		{
		case LAGUERRE:
			pComp = new generalGammaDistributionLaguerre(alpha, beta, categoriesNumInComponent);
			break;
		case QUANTILE:
			pComp = new generalGammaDistribution(alpha, beta, categoriesNumInComponent);
			break;
		default:
			errorMsg::reportError("unknown quadrature type in mixtureDistribution");
		}
		_components.push_back(pComp);
	}

	scaleVec(componentsProb, 1.0/componentsNum);
	setComponentsProb(componentsProb);
	_globalRate = 1.0;
}
//init the mixture with componentsNum components - the alpha, beta, and probability for each component is assigned with given values
mixtureDistribution::mixtureDistribution(int componentsNum, int categoriesNumInComponent,Vdouble AlphaInit ,Vdouble BetaInit, Vdouble componentProbInit ,quadratureType gammaType/*=LAGUERRE*/, MDOUBLE maxAlpha/*=5.0*/, MDOUBLE maxBeta/*=5.0*/)
{
	if (componentsNum < 1)
		errorMsg::reportError("the number of Gamma components must be positive");

	_components.clear();
	Vdouble componentsProb(componentsNum, 0);
	for (int i = 0; i < componentsNum; ++i)
	{
		MDOUBLE alpha = AlphaInit[i];
		MDOUBLE beta = BetaInit[i];
		componentsProb[i] = componentProbInit[i];
		generalGammaDistribution* pComp; 
		switch (gammaType)
		{
		case LAGUERRE:
			pComp = new generalGammaDistributionLaguerre(alpha, beta, categoriesNumInComponent);
			break;
		case QUANTILE:
			pComp = new generalGammaDistribution(alpha, beta, categoriesNumInComponent);
			break;
		default:
			errorMsg::reportError("unknown quadrature type in mixtureDistribution");
		}
		_components.push_back(pComp);
	}

	scaleVec(componentsProb, 1.0/componentsNum);
	setComponentsProb(componentsProb);
	_globalRate = 1.0;
}

mixtureDistribution::mixtureDistribution(const mixtureDistribution& other)
:	_componentsWeight(other._componentsWeight),
	_globalRate(other._globalRate),
	_totalWeight(other._totalWeight)
{
	_components.clear();
	for (int i = 0; i < other.getComponentsNum(); ++i)
	{
		generalGammaDistribution* comp = static_cast<generalGammaDistribution*>(other._components[i]->clone());
		_components.push_back(comp);
	}
}


mixtureDistribution& mixtureDistribution::operator=(const mixtureDistribution &otherDist) 
{
	_globalRate = otherDist._globalRate;
	_componentsWeight = otherDist._componentsWeight;
	_totalWeight = otherDist._totalWeight;
	if (this != &otherDist) // Check for self-assignment
	{
		for (int i = 0; i < getComponentsNum(); ++i)
		{
			if (_components[i] != NULL)
			{
				generalGammaDistribution* pComp = static_cast<generalGammaDistribution*>(otherDist.getComponent(i)->clone());
				delete _components[i];
				_components[i] = pComp;;
			}
		}
	}
	return *this;
}

  
void mixtureDistribution::clear()
{
	for (int i = 0; i < getComponentsNum(); ++i)
	{
		if (_components[i] != NULL)
		{
			delete _components[i];
			_components[i] = NULL;
		}
	}
	_components.clear();
}


mixtureDistribution::~mixtureDistribution()
{
	clear();
}

const int mixtureDistribution::categories() const
{
	int res = 0;
	for (int i = 0; i < getComponentsNum(); ++i)
	{
		res += _components[i]->categories();
	}
	return res;
}

void mixtureDistribution::setComponentsProb(const Vdouble& componentsProb)
{
	if (getComponentsNum() != componentsProb.size())
		errorMsg::reportError("the number of Gamma components is not the same as the number of probabilities");
	_totalWeight = 0.0;
	for (int i = 0; i < componentsProb.size(); ++i)
		_totalWeight += componentsProb[i];
	if (!DEQUAL(_totalWeight, 1.0))
		errorMsg::reportError("the sum of components probabilities must sum to 1.0");
	_componentsWeight = componentsProb;
}


void mixtureDistribution::change_number_of_categoriesPerComp(int in_number_of_categories)
{
	for (int i = 0; i <getComponentsNum(); ++i)
		_components[i]->change_number_of_categories(in_number_of_categories);
}

//change_number_of_components: if the newCompNum is getComponentsNum()-1
//then duplicate one of the components and adjust the probabilities
void mixtureDistribution::change_number_of_components(const int in_number_of_components)
{
	if (getComponentsNum() == in_number_of_components)
		return;
	else if (getComponentsNum() == in_number_of_components - 1)
	{
		//duplicate the first component
		normalizeProbabilities();
		generalGammaDistribution* comp = static_cast<generalGammaDistribution*>(_components[0]->clone());
		_components.push_back(comp);
		//adjust the components probabilities so that the probs of the 
		//two identical components (i.e., 0 and the new Comp) are equal
		_componentsWeight[0] /= 2; 
		_componentsWeight.push_back(_componentsWeight[0]);
		normalizeProbabilities();
	}
	else
		errorMsg::reportError("cannot change the number of components in mixtureDistribution::change_number_of_components()");
}


const MDOUBLE mixtureDistribution::getCumulativeProb(const MDOUBLE x) const
{
	MDOUBLE res = 0.0;
	for (int i = 0; i < getComponentsNum(); ++i)
		res += _components[i]->getCumulativeProb(x) * getComponentProb(i);
	return res;
}

const MDOUBLE mixtureDistribution::rates(const int category) const
{
	if (category > categories() - 1)
		errorMsg::reportError("the required category does not exist!");
	int componentNum, categoryInComponent, totalCat = 0;
	for (int i = 0; i < getComponentsNum(); ++i)
	{
		if (category < _components[i]->categories() + totalCat)
		{
			componentNum = i;
			categoryInComponent = category - totalCat;
			break;
		}
		totalCat += _components[i]->categories();
	}
	return _components[componentNum]->rates(categoryInComponent) * _globalRate;
}

const MDOUBLE mixtureDistribution::ratesProb(const int category) const
{
	if (category > categories() - 1)
		errorMsg::reportError("there required category does not exist!");
	int componentNum, categoryInComponent, totalCat = 0;
	for (int i = 0; i < getComponentsNum(); ++i)
	{
		if (category < _components[i]->categories() + totalCat)
		{
			componentNum = i;
			categoryInComponent = category - totalCat;
			break;
		}
		totalCat += _components[i]->categories();
	}	
	return  getComponentProb(componentNum) * _components[componentNum]->ratesProb(categoryInComponent);
}


void mixtureDistribution::setMixtureParameters(const Vdouble& alphaVec, const Vdouble& betaVec, const Vdouble& componentsProb)
{
	if (alphaVec.size() != getComponentsNum())
		errorMsg::reportError("the size of the alphas vector is not identical to the number of components");
	if (betaVec.size() != getComponentsNum())
		errorMsg::reportError("the size of the batas vector is not identical to the number of components");
	if (componentsProb.size() != getComponentsNum())
		errorMsg::reportError("the size of the components probabilities vector is not identical to the number of components");

	setComponentsProb(componentsProb);
	int categoriesInComponent = _components[0]->categories(); 
	for (int i = 0; i < getComponentsNum(); ++i)
		_components[i]->setGammaParameters(categoriesInComponent, alphaVec[i], betaVec[i]);
}

//the following functions set the components probabilities.
//Note, that the new prob is not inWeight, but is scaled so that the total probabilities are 1.0
void mixtureDistribution::setComponentWeight(MDOUBLE inWeight, const int componentNum, const MDOUBLE minWeight/*=0.01*/)
{
	if((inWeight<0.0) || (inWeight>1.0)){
		errorMsg::reportError("the probability assignment is not [0,1]");
	}
	if (inWeight < minWeight)
		inWeight = minWeight;
	MDOUBLE otherProbs = 1-inWeight;
	Vdouble probs(getComponentsNum(), 0.0);
	MDOUBLE sumOther = 0.0;
	int i;
	for (i = 0; i < getComponentsNum(); ++i)
	{
		if (i != componentNum)
			sumOther += _componentsWeight[i];
	}
	MDOUBLE factor = otherProbs / sumOther;
	for (i = 0; i < getComponentsNum(); ++i)
	{
		probs[i] = _componentsWeight[i] * factor ;
	}
	probs[componentNum] = inWeight;
	setComponentsProb(probs);
 	
	//_totalWeight -= _componentsWeight[componentNum];
 //   _componentsWeight[componentNum] = inWeight;
	//_totalWeight += _componentsWeight[componentNum];
}

//scale the components weights so that they sum to 1.0.
void mixtureDistribution::normalizeProbabilities()
{
	if (_componentsWeight.size() != getComponentsNum())
		errorMsg::reportError("problem in mixtureDistribution::normalizeProbabilities()");
	int i;
	for(i = 0; i < getComponentsNum(); ++i)
	{
		_componentsWeight[i] /= _totalWeight;
	}
	_totalWeight = 1.0;
}

void mixtureDistribution::printParams(ostream& outF)
{		
	MDOUBLE avgRate = 0.0;
	for (int k = 0; k < getComponentsNum(); ++k)
	{
		outF << "comp="<<k<<" Alp/Beta= "<<getAlpha(k)/getBeta(k)<<" alpha= "<<getAlpha(k) << " beta= " <<getBeta(k)<<" Prob= "<<getComponentProb(k)<<endl;  
		avgRate +=  (getAlpha(k) / getBeta(k)) * getComponentProb(k);
	}
	outF<<"# The prior average rate is: " <<avgRate<<endl;
}