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 1515-1520    Link Here 
1515
  : RandomVariable (TriangularVariableImpl (s,l,mean))
1515
  : RandomVariable (TriangularVariableImpl (s,l,mean))
1516
{}
1516
{}
1517
1517
1518
//-----------------------------------------------------------------------------
1519
//-----------------------------------------------------------------------------
1520
// ZipfVariableImpl
1521
class ZipfVariableImpl : public RandomVariableBase { 
1522
public:
1523
  /**
1524
   * \param n the number of possible items
1525
   * \param alpha the alpha parameter
1526
   */
1527
  ZipfVariableImpl (long n, double alpha);
1528
1529
  /**
1530
   * \A zipf variable with N=1 and alpha=0
1531
   */
1532
  ZipfVariableImpl ();
1533
1534
  /**
1535
   * \return A random value from this distribution
1536
   */
1537
  virtual double GetValue ();
1538
  virtual RandomVariableBase* Copy(void) const;
1539
1540
private:
1541
  long m_n;
1542
  double m_alpha;
1543
  double m_c; //the normalization constant
1544
};
1545
1546
1547
RandomVariableBase* ZipfVariableImpl::Copy () const
1548
{
1549
  return new ZipfVariableImpl (m_n, m_alpha);
1550
}
1551
1552
ZipfVariableImpl::ZipfVariableImpl ()
1553
    :m_n(1), m_alpha(0), m_c(1)
1554
{
1555
}
1556
1557
1558
ZipfVariableImpl::ZipfVariableImpl (long n, double alpha)
1559
    :m_n(n), m_alpha(alpha), m_c(0)
1560
{
1561
  //calculate the normalization constant c
1562
  for(int i=1;i<=n;i++)
1563
    {
1564
      m_c+=(1.0/pow((double)i,alpha));
1565
    }
1566
  m_c=1.0/m_c;
1567
}
1568
1569
double
1570
ZipfVariableImpl::GetValue ()
1571
{
1572
  if(!m_generator)
1573
    {
1574
      m_generator = new RngStream();
1575
    }
1576
1577
  double u = m_generator->RandU01();
1578
  double sum_prob=0,zipf_value=0;
1579
  for(int i=1;i<=m_n;i++)
1580
    {
1581
      sum_prob+=m_c/pow((double)i,m_alpha);
1582
      if(sum_prob>u)
1583
        {
1584
          zipf_value=i;
1585
          break;
1586
        }
1587
    }
1588
  return zipf_value;
1589
}
1590
1591
ZipfVariable::ZipfVariable ()
1592
  : RandomVariable (ZipfVariableImpl ())
1593
{}
1594
1595
ZipfVariable::ZipfVariable (long n, double alpha)
1596
  : RandomVariable (ZipfVariableImpl (n, alpha))
1597
{}
1598
1518
1599
1519
std::ostream &operator << (std::ostream &os, const RandomVariable &var)
1600
std::ostream &operator << (std::ostream &os, const RandomVariable &var)
1520
{
1601
{
(-)a/src/core/random-variable.h (+19 lines)
 Lines 695-700    Link Here 
695
};
695
};
696
696
697
/**
697
/**
698
 * \brief Zipf Distributed random var (between 1 and n included)
699
 * \ingroup randomvariable
700
 *
701
 */
702
class ZipfVariable : public RandomVariable 
703
{
704
public:
705
  /**
706
   * \param n the number of possible items
707
   * \param alpha the alpha parameter
708
   */
709
  ZipfVariable (long n, double alpha);
710
  /**
711
   * A zipf variable with N=1 and alpha=0
712
   */
713
  ZipfVariable ();
714
};
715
716
/**
698
 * \brief Triangularly Distributed random var
717
 * \brief Triangularly Distributed random var
699
 * \ingroup randomvariable
718
 * \ingroup randomvariable
700
 * 
719
 * 

Return to bug 578