-
Notifications
You must be signed in to change notification settings - Fork 15
Helper functions
This is a collection of useful functions used through all the code to make possible the calculation of different distribution functions and significance tests. The functions are defined under the Math module as a monkey-patch strategy.
It uses the Math.gamma
function to improve accuracy and velocity. It's worth to mention that Gamma(x) == (x - 1)!
for integer values.
This method is used only for positive integer values. If you need to calculate the factorial of a rational number, use Math.gamma
instead.
[103] pry(main)> Math.factorial(5)
=> 120.0
[104] pry(main)> Math.factorial(4)
=> 24.0
[105] pry(main)> Math.factorial(-1)
=> nil
[106] pry(main)> Math.factorial(5.3)
=> 120.0
It calculates the combination of a number N over R repetitions: nCr
.
[114] pry(main)> Math.combination(4, 2)
=> 6.0
[115] pry(main)> Math.combination(4, 4)
=> 1.0
It calculates the permutation of a number N, R times: nPr
.
[118] pry(main)> Math.permutation(4, 2)
=> 12.0
[119] pry(main)> Math.permutation(4, 4)
=> 24.0
It calculates the defined integral over a range [a, b], split-up in N-intervals. The N-interval must be even.
[120] pry(main)> Math.simpson_rule(1, 10, 10_000) do |x|
[120] pry(main)* x**2
[120] pry(main)* end
=> 333.0302975699995
It calculates the lower incomplete gamma function y(s,z) using the simpson's rule.
122] pry(main)> Math.lower_incomplete_gamma_function(3, 4)
=> 1.5238031574778705
This implementation is an adaptation of the incomplete beta function made in C by Lewis Van Winkle, which released the code under the zlib license. The whole math behind this code is described in the following post: https://codeplea.com/incomplete-beta-function-c
[126] pry(main)> Math.incomplete_beta_function(0.3, 2, 3)
=> 0.34829999999999967