我的问题是,我无法从csv文件中读取双打,但只能读取整数。因此当我尝试使用增长数组进行计算时会出现错误“-infinity”和“NaN”:
require 'csv'
filename = 'file.csv'
csv_data = CSV.read(filename, :col_sep => ";")
csv_data.shift
price1, price2 = csv_data.transpose.map { |a| a.map(&:to_i)}
#calulate yields
yields1 = Array.new
yields2 = Array.new
price1.each_cons(2) { |c|
yields1.push(Math.log(c[1]/c[0]))
}
price2.each_cons(2) { |c|
yields2.push(Math.log(c[1]/c[0]))
}
# Statistics
module Enumerable
def mean
mean = self.reduce(:+)/self.length.to_f
return mean
end
end
puts "mean yields1 = " + yields1.mean.to_s
我的两个问题:
答案 0 :(得分:1)
更改
price1, price2 = csv_data.transpose.map { |a| a.map(&:to_i)}
到
price1, price2 = csv_data.transpose.map { |a| a.map(&:to_f)}