title: Monte Carlo Simulation in Ruby
body: <p>Hey I have a problem with my simulation.</p>
  
  <p>I am a Ruby-Starter and don't know what's wrong in my code. That is only the part with the simulation: </p>
  
  <pre><code>def mean
   mean = self.reduce(:+)/self.length.to_f
   return mean
  end
  
  def randn
    begin 
    rg1 = (rand*2)-1 
    rg2 = (rand*2)-1 
    q = rg1**2 + rg2**2
    end while (q == 0 || q &gt; 1)
    p = Math.sqrt((-2*Math.log(q))/q)
  
    rn1 = rg1 * p
    rn2 = rg2 * p
    return rn1, rn2
   end
  
  monte_carlo = 10
  ren1_sim = Array.new
  ren2_sim = Array.new
  
  monte_carlo.times {
   (1..20).each{ |i|
   (1..250).each { |j|
    r = randn() 
      ren1= * Math.exp(mu_ren1 + sigma_ren1 * r[0]) 
      # ren1 is an array with prices, mu_ren1 and sigma_ren1 are individual values
  
      ren2= * Math.exp(mu_ren2 + chol_21 * r[0] + chol_22 * r[1])
      # chol_21 and chol_22 are also individual values
  
       ren1_sim.push(ren1)
       ren2_sim.push(ren2)
     } 
    }
   }
   puts ren1_sim.mean
   puts ren2_sim.mean
  </code></pre>
  
  <p>I don't get an error without the last two puts, but when I want to calculate the average of the arrays ren1_sim and rent_sim I get the error:</p>
  
  <pre><code> undefined method 'mean' for #&lt;Array:0x2acf318&gt; (NoMethodError)
  </code></pre>
  
  <p>Do you know how to fix that?</p>