New Zipf random variable
authorFrancesco Malandrino <francesco.malandrino@gmail.com>
Tue, 23 Jun 2009 22:42:09 -0700
changeset 4579 c86681050541
parent 4578 88434ff8f0a5
child 4580 8092e3e83487
New Zipf random variable
src/core/random-variable.cc
src/core/random-variable.h
--- a/src/core/random-variable.cc	Tue Jun 23 22:12:35 2009 -0700
+++ b/src/core/random-variable.cc	Tue Jun 23 22:42:09 2009 -0700
@@ -1152,7 +1152,7 @@
 
 RandomVariableBase* LogNormalVariableImpl::Copy () const
 {
-  return new LogNormalVariableImpl (m_mu, m_sigma);
+  return new LogNormalVariableImpl (*this);
 }
 
 LogNormalVariableImpl::LogNormalVariableImpl (double mu, double sigma)
@@ -1515,6 +1515,87 @@
   : RandomVariable (TriangularVariableImpl (s,l,mean))
 {}
 
+//-----------------------------------------------------------------------------
+//-----------------------------------------------------------------------------
+// ZipfVariableImpl
+class ZipfVariableImpl : public RandomVariableBase { 
+public:
+  /**
+   * \param n the number of possible items
+   * \param alpha the alpha parameter
+   */
+  ZipfVariableImpl (long n, double alpha);
+
+  /**
+   * \A zipf variable with N=1 and alpha=0
+   */
+  ZipfVariableImpl ();
+
+  /**
+   * \return A random value from this distribution
+   */
+  virtual double GetValue ();
+  virtual RandomVariableBase* Copy(void) const;
+
+private:
+  long m_n;
+  double m_alpha;
+  double m_c; //the normalization constant
+};
+
+
+RandomVariableBase* ZipfVariableImpl::Copy () const
+{
+  return new ZipfVariableImpl (m_n, m_alpha);
+}
+
+ZipfVariableImpl::ZipfVariableImpl ()
+    :m_n(1), m_alpha(0), m_c(1)
+{
+}
+
+
+ZipfVariableImpl::ZipfVariableImpl (long n, double alpha)
+    :m_n(n), m_alpha(alpha), m_c(0)
+{
+  //calculate the normalization constant c
+  for(int i=1;i<=n;i++)
+    {
+      m_c+=(1.0/pow((double)i,alpha));
+    }
+  m_c=1.0/m_c;
+}
+
+double
+ZipfVariableImpl::GetValue ()
+{
+  if(!m_generator)
+    {
+      m_generator = new RngStream();
+    }
+
+  double u = m_generator->RandU01();
+  double sum_prob=0,zipf_value=0;
+  for(int i=1;i<=m_n;i++)
+    {
+      sum_prob+=m_c/pow((double)i,m_alpha);
+      if(sum_prob>u)
+        {
+          zipf_value=i;
+          break;
+        }
+    }
+  return zipf_value;
+}
+
+ZipfVariable::ZipfVariable ()
+  : RandomVariable (ZipfVariableImpl ())
+{}
+
+ZipfVariable::ZipfVariable (long n, double alpha)
+  : RandomVariable (ZipfVariableImpl (n, alpha))
+{}
+
 
 std::ostream &operator << (std::ostream &os, const RandomVariable &var)
 {
--- a/src/core/random-variable.h	Tue Jun 23 22:12:35 2009 -0700
+++ b/src/core/random-variable.h	Tue Jun 23 22:42:09 2009 -0700
@@ -695,6 +695,25 @@
 };
 
 /**
+ * \brief Zipf Distributed random var (between 1 and n included)
+ * \ingroup randomvariable
+ *
+ */
+class ZipfVariable : public RandomVariable 
+{
+public:
+  /**
+   * \param n the number of possible items
+   * \param alpha the alpha parameter
+   */
+  ZipfVariable (long n, double alpha);
+  /**
+   * A zipf variable with N=1 and alpha=0
+   */
+  ZipfVariable ();
+};
+
+/**
  * \brief Triangularly Distributed random var
  * \ingroup randomvariable
  *