View | Details | Raw Unified | Return to bug 578
Collapse All | Expand All

(-)a/src/core/random-variable.cc (-1 / +82 lines)
 Lines 1152-1158    Link Here 
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)
 Lines 1291-1296    Link Here 
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
  {
1340
    m_c+=(1.0/pow((double)i,alpha));
1341
  }
1342
  m_c=1.0/m_c;
1343
}
1344
1345
double
1346
ZipfVariableImpl::GetValue ()
1347
{
1348
  if(!m_generator)
1349
  {
1350
    m_generator = new RngStream();
1351
  }
1352
1353
  double u = m_generator->RandU01();
1354
  double sum_prob=0,zipf_value=0;
1355
  for(int i=1;i<=m_n;i++)
1356
  {
1357
    sum_prob+=m_c/pow((double)i,m_alpha);
1358
    if(sum_prob>u)
1359
    {
1360
      zipf_value=i;
1361
      break;
1362
    }
1363
  }
1364
  return zipf_value;
1365
}
1366
1367
ZipfVariable::ZipfVariable ()
1368
  : RandomVariable (ZipfVariableImpl ())
1369
{}
1370
1371
ZipfVariable::ZipfVariable (long n, double alpha)
1372
  : RandomVariable (ZipfVariableImpl (n, alpha))
1373
{}
1374
1294
1375
1295
std::ostream &operator << (std::ostream &os, const RandomVariable &var)
1376
std::ostream &operator << (std::ostream &os, const RandomVariable &var)
1296
{
1377
{

Return to bug 578