Method: NMatrix#variance
- Defined in:
- lib/nmatrix/math.rb
#variance(dimen = 0) ⇒ Object
call-seq:
variance() -> NMatrix
variance(dimen) -> NMatrix
Calculates the sample variance along the specified dimension.
This will force integer types to float64 dtype.
567 568 569 570 571 572 573 574 575 576 |
# File 'lib/nmatrix/math.rb', line 567 def variance(dimen=0) reduce_dtype = nil if integer_dtype? then reduce_dtype = :float64 end m = mean(dimen) inject_rank(dimen, 0.0, reduce_dtype) do |var, sub_mat| var + (m - sub_mat)*(m - sub_mat)/(shape[dimen]-1) end end |