|
|
| 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) |
|
|
| 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 |
{ |