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

(-)a/src/core/random-variable.cc (+79 lines)
 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
    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
{
(-)a/src/core/random-variable.h (+19 lines)
 Lines 594-599    Link Here 
594
};
594
};
595
595
596
/**
596
/**
597
 * \brief Zipf Distributed random var (between 1 and n included)
598
 * \ingroup randomvariable
599
 *
600
 */
601
class ZipfVariable : public RandomVariable 
602
{
603
public:
604
  /**
605
   * \param n the number of possible items
606
   * \param alpha the alpha parameter
607
   */
608
  ZipfVariable (long n, double alpha);
609
  /**
610
   * A zipf variable with N=1 and alpha=0
611
   */
612
  ZipfVariable ();
613
};
614
615
/**
597
 * \brief Triangularly Distributed random var
616
 * \brief Triangularly Distributed random var
598
 * \ingroup randomvariable
617
 * \ingroup randomvariable
599
 * 
618
 * 

Return to bug 578