|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Statistics::Distribution::Bernoulli do |
| 4 | + describe '.density_function' do |
| 5 | + it 'is not defined when the outcome is different from zero or one' do |
| 6 | + expect(described_class.density_function(rand(2..10), rand)).to be_nil |
| 7 | + expect(described_class.density_function(rand(-5..-1), rand)).to be_nil |
| 8 | + end |
| 9 | + |
| 10 | + it 'returns the expected value when the outcome is zero' do |
| 11 | + p = rand |
| 12 | + expect(described_class.density_function(0, p)).to eq (1.0 - p) |
| 13 | + end |
| 14 | + |
| 15 | + it 'returns the expected value when the outcome is one' do |
| 16 | + p = rand |
| 17 | + expect(described_class.density_function(1, p)).to eq p |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + describe '.cumulative_function' do |
| 22 | + it 'is not defined when the outcome is different from zero or one' do |
| 23 | + expect(described_class.cumulative_function(rand(2..10), rand)).to be_nil |
| 24 | + expect(described_class.density_function(rand(-5..-1), rand)).to be_nil |
| 25 | + end |
| 26 | + |
| 27 | + it 'returns the expected value when the outcome is zero' do |
| 28 | + p = rand |
| 29 | + expect(described_class.cumulative_function(0, p)).to eq (1.0 - p) |
| 30 | + end |
| 31 | + |
| 32 | + it 'returns the expected value when the outcome is one' do |
| 33 | + expect(described_class.cumulative_function(1, rand)).to eq 1.0 |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe '.variance' do |
| 38 | + it 'returns the expected value for the bernoulli distribution' do |
| 39 | + p = rand |
| 40 | + |
| 41 | + expect(described_class.variance(p)).to eq p * (1.0 - p) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + describe '.skewness' do |
| 46 | + it 'returns the expected value for the bernoulli distribution' do |
| 47 | + p = rand |
| 48 | + expected_value = (1.0 - 2.0*p).to_f / Math.sqrt(p * (1.0 - p)) |
| 49 | + |
| 50 | + expect(described_class.skewness(p)).to eq expected_value |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + describe '.kurtosis' do |
| 55 | + it 'returns the expected value for the bernoulli distribution' do |
| 56 | + p = rand |
| 57 | + expected_value = (6.0 * (p ** 2) - (6 * p) + 1) / (p * (1.0 - p)) |
| 58 | + |
| 59 | + expect(described_class.kurtosis(p)).to eq expected_value |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments