title: Wrong distribution from inverse(CDF) transform sampling
body: <p>I am trying to simulate a geometric distribution, using the Inverse CDF method, however I am getting slightly wrong results and I am not sure why.</p>
  
  <p>To be more specific, a geometric distribution with a shape factor p = 0.8, should have the following characteristics:</p>
  
  <pre><code>mean: 1.25 
  variance: 0.31
  </code></pre>
  
  <p>However, running the code below, I am getting:</p>
  
  <pre><code>mean: 0.6224363901913519
  var: 0.391813011265263
  [Finished in 0.3s]
  </code></pre>
  
  <p>As you can see, I am getting a wildly different mean value compared to the expected one.</p>
  
  <blockquote>
    <p>np.log(uniform[i])/np.log(1-p) is the result of solving the equation:
    F(X) = R for X in terms of R, F(X) = CDF of geometric distribution = 1 - (1 - p)^k.</p>
  </blockquote>
  
  <p>R is a uniform distribution over the interval (0,1).</p>
  
  <p>So solving it results in the following:</p>
  
  <blockquote>
    <p>X = ln(1-R)/ln(1-p)</p>
  </blockquote>
  
  <p>However, since both 1-R and R are uniformly distributed on (0,1), we can do the following simplification:</p>
  
  <blockquote>
    <p>X = ln(R)/ln(1-p)</p>
  </blockquote>
  
  <p>The above equation is correct and should result in a geometric distribution sample.</p>
  
  <pre><code>import numpy as np
  
  n = 10000
  p = 0.8
  geo_dist = np.zeros(n,dtype = np.float64)
  uniform = np.random.uniform(0, 1, n)
  for i in range(n):
      geo_dist[i] = np.log(uniform[i])/np.log(1-p)
  print("mean: " +str(geo_dist.mean()))
  print("var: " +str(geo_dist.var())) 
  </code></pre>
  
  <p>I have tried increasing the calculation precision by using np.float64 in a desperate attempt to fix what should be a trivial script, to no avail.</p>
  
  <p>I have also tried generating the uniform distribution using scipy uniform.rvs() instead of np.uniform and the problem persists.</p>
  
  <p>If p = 0.5:</p>
  
  <pre><code>expected mean: 2
  expected variance : 2
  </code></pre>
  
  <p>However, the code I have written has the following result:</p>
  
  <pre><code>mean: 1.4440009653569306
  var: 2.0421079966161093
  [Finished in 0.3s]
  </code></pre>
  
  <p>Anybody have any idea why this is not working?
  Thank you.</p>