Browse Source

add root mean square

pull/2/head
Arthur POULET 4 years ago
parent
commit
6f63b43fff
No known key found for this signature in database GPG Key ID: FC154EA63FF5D8BC
  1. 9
      README.md
  2. 5
      spec/math/mean.cr
  3. 6
      src/lib/math/mean.cr

9
README.md

@ -61,16 +61,15 @@ Math.factorial(4) # => 24
```crystal
90.radian # => (Math::PI / 2)
(Math::PI / 4) # => 45.0
```
### Series & Statistics
```crystal
[1.0, 2.0, 3.0].mean # => 2.0
[1.0, 2.0, 3.0].variance.round(4) # => 0.6667
[1.0, 2.0, 3.0].standard_deviation.round(4) # => 0.8165
[1, 2, 3].mean # => 2.0
[1, 2, 3].variance.round(4) # => 0.6667
[1, 2, 3].standard_deviation.round(4) # => 0.8165
[1, 2, 3].root_mean_square.round(2) # => 2.16
```
## Development

5
spec/math/mean.cr

@ -10,4 +10,9 @@ describe Math::Mean do
[1, 2, 3].mean.should eq 2.0
[1, 2, -3].mean.should eq 0.0
end
it "test quadratic mean" do
[1, 2, 3, 2].root_mean_square.round(4).should eq(2.1213)
[1, 2, 1, 5, 10, 9, 1, -13, 2].root_mean_square.round(4).should eq(6.549)
end
end

6
src/lib/math/mean.cr

@ -1,7 +1,13 @@
module Math::Mean
# Standard arithmetic mean
# TODO: Handle big Float/Int
def mean : Float64
return 0.0_f64 if empty?
sum.to_f64 / size.to_f64
end
# The square root of mean square
def root_mean_square : Float64
Math.sqrt map { |e| e ** 2 }.mean
end
end

Loading…
Cancel
Save