|
|
| 1152 |
|
1152 |
|
| 1153 |
RandomVariableBase* LogNormalVariableImpl::Copy () const |
1153 |
RandomVariableBase* LogNormalVariableImpl::Copy () const |
| 1154 |
{ |
1154 |
{ |
| 1155 |
return new LogNormalVariableImpl (m_mu, m_sigma); |
1155 |
return new LogNormalVariableImpl (*this); |
| 1156 |
} |
1156 |
} |
| 1157 |
|
1157 |
|
| 1158 |
LogNormalVariableImpl::LogNormalVariableImpl (double mu, double sigma) |
1158 |
LogNormalVariableImpl::LogNormalVariableImpl (double mu, double sigma) |
|
|
| 1291 |
: RandomVariable (TriangularVariableImpl (s,l,mean)) |
1291 |
: RandomVariable (TriangularVariableImpl (s,l,mean)) |
| 1292 |
{} |
1292 |
{} |
| 1293 |
|
1293 |
|
|
|
1294 |
//----------------------------------------------------------------------------- |
| 1295 |
//----------------------------------------------------------------------------- |
| 1296 |
// ZipfVariableImpl |
| 1297 |
class ZipfVariableImpl : public RandomVariableBase { |
| 1298 |
public: |
| 1299 |
/** |
| 1300 |
* \param n the number of possible items |
| 1301 |
* \param alpha the alpha parameter |
| 1302 |
*/ |
| 1303 |
ZipfVariableImpl (long n, double alpha); |
| 1304 |
|
| 1305 |
/** |
| 1306 |
* \A zipf variable with N=1 and alpha=0 |
| 1307 |
*/ |
| 1308 |
ZipfVariableImpl (); |
| 1309 |
|
| 1310 |
/** |
| 1311 |
* \return A random value from this distribution |
| 1312 |
*/ |
| 1313 |
virtual double GetValue (); |
| 1314 |
virtual RandomVariableBase* Copy(void) const; |
| 1315 |
|
| 1316 |
private: |
| 1317 |
long m_n; |
| 1318 |
double m_alpha; |
| 1319 |
double m_c; //the normalization constant |
| 1320 |
}; |
| 1321 |
|
| 1322 |
|
| 1323 |
RandomVariableBase* ZipfVariableImpl::Copy () const |
| 1324 |
{ |
| 1325 |
return new ZipfVariableImpl (m_n, m_alpha); |
| 1326 |
} |
| 1327 |
|
| 1328 |
ZipfVariableImpl::ZipfVariableImpl () |
| 1329 |
:m_n(1), m_alpha(0), m_c(1) |
| 1330 |
{ |
| 1331 |
} |
| 1332 |
|
| 1333 |
|
| 1334 |
ZipfVariableImpl::ZipfVariableImpl (long n, double alpha) |
| 1335 |
:m_n(n), m_alpha(alpha), m_c(0) |
| 1336 |
{ |
| 1337 |
//calculate the normalization constant c |
| 1338 |
for(int i=1;i<=n;i++) |
| 1339 |
m_c+=(1.0/pow((double)i,alpha)); |
| 1340 |
m_c=1.0/m_c; |
| 1341 |
} |
| 1342 |
|
| 1343 |
double |
| 1344 |
ZipfVariableImpl::GetValue () |
| 1345 |
{ |
| 1346 |
if(!m_generator) |
| 1347 |
{ |
| 1348 |
m_generator = new RngStream(); |
| 1349 |
} |
| 1350 |
|
| 1351 |
double u = m_generator->RandU01(); |
| 1352 |
double sum_prob=0,zipf_value=0; |
| 1353 |
for(int i=1;i<=m_n;i++) |
| 1354 |
{ |
| 1355 |
sum_prob+=m_c/pow((double)i,m_alpha); |
| 1356 |
if(sum_prob>u) |
| 1357 |
{ |
| 1358 |
zipf_value=i; |
| 1359 |
break; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
return zipf_value; |
| 1363 |
} |
| 1364 |
|
| 1365 |
ZipfVariable::ZipfVariable () |
| 1366 |
: RandomVariable (ZipfVariableImpl ()) |
| 1367 |
{} |
| 1368 |
|
| 1369 |
ZipfVariable::ZipfVariable (long n, double alpha) |
| 1370 |
: RandomVariable (ZipfVariableImpl (n, alpha)) |
| 1371 |
{} |
| 1372 |
|
| 1294 |
|
1373 |
|
| 1295 |
std::ostream &operator << (std::ostream &os, const RandomVariable &var) |
1374 |
std::ostream &operator << (std::ostream &os, const RandomVariable &var) |
| 1296 |
{ |
1375 |
{ |