1

Hey I have a problem with my simulation.

I am a Ruby-Starter and don't know what's wrong in my code. That is only the part with the simulation:

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

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:

 undefined method 'mean' for #<Array:0x2acf318> (NoMethodError)

Do you know how to fix that?

jeffrey
  • 2,026
  • 5
  • 28
  • 42

3 Answers3

3

You're trying to invoke mean on an Array, which is not a method of Array. Perhaps you meant to use Statsample::Vector, which is Statsample's extension of Array, and does have mean?

ren1_sim = Statsample::Vector.new
ren2_sim = Statsample::Vector.new

You can also call to_vector on an Array instance to get a Statsample::Vector.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • ok, but when I use the method mean (s.o.) to calculate the average of array. but I still get the same error.there must be an other mistake in the code? – jeffrey Apr 15 '12 at 10:10
2

You've defined a mean method at the top of your file, but that just creates a method on the top level object, and you're trying to call it on an individual array. You could either change that code to

def mean(array)
  array.reduce(:+)/array.length.to_f
end

and then change your usage of it later on to mean(ren1_sim)

or change your code so that you are adding the method to array, i.e.

class Array
  def mean
    self.reduce(:+)/self.length.to_f
  end
end
Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • I still get the same error "undefined method ´/' for #Array:0x3462738". there must be something wrong in the simulation. when I print out the values in the array ren_sim1, I see that each iteration produces the same values. but why? the random number in r[0] and r[1] should change for each iteration...I am looking for two arrays (ren_sim1 and ten_sim2), containing the values from the calculation in sim1 and sim2 for each iteration of the monte carlo simulation.... – jeffrey Apr 15 '12 at 10:39
  • @ FrederickCheung Ok, I think I have found the problem. The array ren1_sim contains an array for each value of the simulation. so I have problems to use it for other calculations. do you know how to write the values in a "normal array"? – jeffrey Apr 15 '12 at 11:07
  • 1
    ren1 will indeed look like `[[1], [2], ...]` because you are doing `ren1 = *Math.exp(...)` which means `ren1` will be an array with 1 value – Frederick Cheung Apr 16 '12 at 04:40
0

have a look at this post to calculate the average of a array

How do I create an average from a Ruby array?

Community
  • 1
  • 1
Baptiste Pernet
  • 3,318
  • 22
  • 47
  • 1
    Please do not simply post a link to another question, if you think another question wholly answers this one, you should vote to close as duplicate. – Andrew Marshall Apr 15 '12 at 00:16
  • I've just tried it. Mean is a method from the statsample gem for the average...are you sure that the rest of the code is without an error? – jeffrey Apr 15 '12 at 00:16