Browse Source

Initialize the repository

- add normale distribution and binomial distribution
- add specs, litte documentation

imported from RubyProba, RubyNormale, and RubyBinomial (all created by myself)
pull/2/head
Arthur Poulet 5 years ago
commit
0dd803f737
No known key found for this signature in database GPG Key ID: 7C42395DCA99B923
14 changed files with 254 additions and 0 deletions
  1. +10
    -0
      .gitignore
  2. +1
    -0
      .travis.yml
  3. +21
    -0
      LICENSE
  4. +45
    -0
      README.md
  5. +7
    -0
      shard.yml
  6. +6
    -0
      spec/CrystalProba_spec.cr
  7. +9
    -0
      spec/binomial_distribution.cr
  8. +29
    -0
      spec/normale_distribution.cr
  9. +2
    -0
      spec/spec_helper.cr
  10. +5
    -0
      src/CrystalProba.cr
  11. +59
    -0
      src/CrystalProba/binomial_distribution.cr
  12. +17
    -0
      src/CrystalProba/normale_distribution.cr
  13. +40
    -0
      src/CrystalProba/normale_distribution/persistant.cr
  14. +3
    -0
      src/CrystalProba/version.cr

+ 10
- 0
.gitignore View File

@ -0,0 +1,10 @@
/doc/
/libs/
/.crystal/
/.shards/
# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock

+ 1
- 0
.travis.yml View File

@ -0,0 +1 @@
language: crystal

+ 21
- 0
LICENSE View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 Arthur Poulet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

+ 45
- 0
README.md View File

@ -0,0 +1,45 @@
# CrystalProba
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
CrystalProba:
github: pouleta/CrystalProba
```
## Usage
```crystal
require "CrystalProba"
```
You should read the specs to understand how it works.
```crystal
Math.binomial_distribution(tries: 1, probability: 0.4, success: 1) # => 0.4
NormaleDistribution::between standard_deviation: 15, esperance: 100, a: 85, b: 115 # => 0.6826894921370859
NormaleDistribution::less_than standard_deviation: 15, esperance: 100, a: 85 # => 0.15865525393145707
NormaleDistribution::greater_than standard_deviation: 15, esperance: 100, a: 115 # => 0.15865525393145707
```
## Development
TODO: Write development instructions here
## Contributing
1. Fork it ( https://github.com/pouleta/CrystalProba/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request
## Contributors
- [pouleta](https://github.com/pouleta) Arthur Poulet - creator, maintainer

+ 7
- 0
shard.yml View File

@ -0,0 +1,7 @@
name: CrystalProba
version: 0.1.0
authors:
- Arthur Poulet <arthur.poulet@cryptolab.net>
license: MIT

+ 6
- 0
spec/CrystalProba_spec.cr View File

@ -0,0 +1,6 @@
require "./spec_helper"
require "./normale_distribution"
require "./binomial_distribution"
describe CrystalProba do
end

+ 9
- 0
spec/binomial_distribution.cr View File

@ -0,0 +1,9 @@
describe BinomialDistribution do
it "simple instances" do
0.0.step(by: 0.1, limit: 1.0) do |proba|
Math.binomial_distribution(tries: 1, probability: proba, success: 1).should eq(proba)
end
end
end

+ 29
- 0
spec/normale_distribution.cr View File

@ -0,0 +1,29 @@
describe NormaleDistribution do
it "simple instances" do
NormaleDistribution::Persistant.new
NormaleDistribution::Persistant.new(standard_deviation: 1).standard_deviation.should eq(1)
NormaleDistribution::Persistant.new(esperance: 1).esperance.should eq(1)
end
it "QI" do
rule = NormaleDistribution::Persistant.new standard_deviation: 15, esperance: 100
rule.between(85, 115).round(2).should eq(0.68)
end
it "centroid" do
[0.1, 1, 2, 4.1324].each do |space|
[0, 1, -1, 12, 41, 0.2, 0.233].each do |center|
rule = NormaleDistribution::Persistant.new standard_deviation: space, esperance: center
[0.2, 0.4, 0.45, 0.55, 0.94, 1.1].each do |diff|
(-rule.between(diff, center)).should eq(rule.between(center, diff))
end
end
end
end
it "must fail" do
expect_raises { NormaleDistribution::Persistant.new standard_deviation: -1 }
expect_raises { NormaleDistribution::Persistant.new standard_deviation: 0 }
end
end

+ 2
- 0
spec/spec_helper.cr View File

@ -0,0 +1,2 @@
require "spec"
require "../src/CrystalProba"

+ 5
- 0
src/CrystalProba.cr View File

@ -0,0 +1,5 @@
require "./CrystalProba/*"
module CrystalProba
# TODO Put your code here
end

+ 59
- 0
src/CrystalProba/binomial_distribution.cr View File

@ -0,0 +1,59 @@
module Math
class DomainError < Exception; end
class SuperiorityError < Exception; end
alias NumericValue = Float32 | Float64 | Int32 | Int64
def self.factorial(n : Int32 | Int64)
raise Math::DomainError.new "The argument must be a natural (out of domain -- factorial)" if n < 0
return 1 if n == 0
return (1..n).to_a.reduce{|a, b| a * b} #(:*)
end
def self.coef_binomial(n : Int32 | Int64, k : Int32 | Int64)
return 0 if n < 0 || k < 0 || n < k
return factorial(n) / factorial(k) * factorial(n - k)
end
# @note if no named parameters are used, then it will try to use the unamed parameters (tries, success, probability)
def self.binomial_distribution(tries : Int32 | Int64, success : Int32 | Int64, probability : NumericValue)
BinomialDistribution.new(n: tries, p: probability).distribute(success)
end
end
class BinomialDistribution
alias NumericValue = Float32 | Float64 | Int32 | Int64
@n : Int32 | Int64
@p : Float64
# @param n [Fixnum] number of tries
# @param p [Float] probability of success
# @note if no probability is defined, the default value will be 0.5
def initialize(@n, p : NumericValue = 0.5)
@p = p.to_f64
raise Math::DomainError.new "The argument `p` `#{@p}` is not in greater or equal to 0" if @p < 0.0
raise Math::DomainError.new "The argument `p` `#{@p}` is not in lesser or equal to 1" if @p > 1.0
raise Math::DomainError.new "The argument `n` `#{@n}` is not in greater or equal to 0" if @n < 0.0
end
def to_s
"#<#{self.class} @n=#@n, @p=#@p>"
end
# @param k [Fixnum] number of test successful.
# @return [Float] probability
def distribute(k : Enumerable)
k.map{|p| calc(p) }.inject(&:+)
end
# @param k [Enumerable] list of number of test successful.
# @return [Float] probability
def distribute(k : Int32 | Int64)
raise Math::SuperiorityError.new "the number of success must be lesser or equal to the number of tries (#{@n})" if k > @n
Math.coef_binomial(@n, k) * @p**k * (1 - @p) ** (@n - k)
end
end

+ 17
- 0
src/CrystalProba/normale_distribution.cr View File

@ -0,0 +1,17 @@
require "./normale_distribution/persistant"
module NormaleDistribution
def self.between(standard_deviation : Float, esperance : Float, a : Float, b : Float)
NormaleDistribution::Persistant.new(standard_deviation: standard_deviation, esperance: esperance).between a, b
end
def self.greater_than(standard_deviation : Float, esperance : Float, a : Float)
NormaleDistribution::Persistant.new(standard_deviation: standard_deviation, esperance: esperance).greater_than a
end
def self.less_than(standard_deviation : Float, esperance : Float, a : Float, b : Float)
NormaleDistribution::Persistant.new(standard_deviation: standard_deviation, esperance: esperance).less_than a
end
end

+ 40
- 0
src/CrystalProba/normale_distribution/persistant.cr View File

@ -0,0 +1,40 @@
module NormaleDistribution
class Persistant
getter standard_deviation, esperance
setter standard_deviation, esperance
alias NumericValue = Float32 | Float64 | Int32 | Int64
@standard_deviation : Float64
@esperance : Float64
def initialize(standard_deviation : NumericValue = 1.0, esperance : NumericValue = 0.0)
@standard_deviation = standard_deviation.to_f64
@esperance = esperance.to_f64
raise ArgumentError.new "standard_deviation must be > 0" unless @standard_deviation > 0.0
end
def greater_than(a : NumericValue)
1.0 - repartition(a)
end
def less_than(a : NumericValue)
repartition a
end
def between(a : NumericValue, b : NumericValue)
repartition(b) - repartition(a)
end
private def density(t : NumericValue)
#1.0 / (standard_deviation * (2 * Math::PI) ** 0.5) * Math::exp((t - esperance) / (2 * standard_deviation)))
end
private def repartition(t : NumericValue)
erf = (t - esperance) / (standard_deviation * 2.0**0.5)
0.5 * (1.0 + Math.erf(erf))
end
end
end

+ 3
- 0
src/CrystalProba/version.cr View File

@ -0,0 +1,3 @@
module CrystalProba
VERSION = "0.1.0"
end

Loading…
Cancel
Save