Module: Malge::LeastSquare
- Defined in:
- lib/malge/leastsquare.rb
Defined Under Namespace
Classes: UnableCalculationError
Class Method Summary collapse
-
.a_exp_bx(data_pairs) ⇒ Object
Return fitted values of [a, b] in a e^bx from 2 point data_pairs; [[x0, y0], [x1, y1]] Argument ‘data_pairs’ should have two items.
-
.least_square_1st_degree(data_pairs) ⇒ Object
Return values of [a_0, a_1] in y = a_0 x^0 + a_1 x^1.
-
.least_square_a_exp_bx(data_pairs) ⇒ Object
Return fitted values of [a, b] in a e^bx from more than two data points.
- .least_square_proportional(data_pairs) ⇒ Object
-
.variance_1st_degree(data_pairs) ⇒ Object
Return variance when fitted to y = a_0 x^0 + a_1 x^1.
Class Method Details
.a_exp_bx(data_pairs) ⇒ Object
Return fitted values of [a, b] in a e^bx from 2 point data_pairs;
- [x0, y0], [x1, y1]
-
Argument ‘data_pairs’ should have two items. Raise if including the same data.
74 75 76 77 78 79 80 81 82 |
# File 'lib/malge/leastsquare.rb', line 74 def self.a_exp_bx(data_pairs) raise UnableCalculationError if data_pairs[0][1] == data_pairs[1][1] raise UnableCalculationError if data_pairs[0][0] == data_pairs[1][0] matrix = data_pairs.map { |pair| [1.0, pair[0]] } values = data_pairs.map { |pair| Math::log pair[1] } coefficients = Malge::SimultaneousEquations.cramer( matrix, values ) coefficients[0] = Math::exp coefficients[0] coefficients end |
.least_square_1st_degree(data_pairs) ⇒ Object
Return values of [a_0, a_1] in y = a_0 x^0 + a_1 x^1. Argument ‘data_pairs’ should be Array of Array’s. For example,
[
[ 1.0, 2.0],
[ 2.0, 4.0],
[ 3.0, 6.0],
]
Note that not [a, b] in y = ax + b.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/malge/leastsquare.rb', line 19 def self.least_square_1st_degree(data_pairs) a = 0.0 #x^2 #b = 0.0 #y^2 c = 0.0 #x^1 d = 0.0 #x*y e = 0.0 #y^1 n = data_pairs.size data_pairs.each do |pairs| x = pairs[0].to_f y = pairs[1].to_f a += x**2 #b += y**2 c += x d += x*y e += y end a_1 = (n*d - c*e) / (n*a - c**2) a_0 = (a*e - c*d) / (n*a - c**2) [a_0, a_1] end |
.least_square_a_exp_bx(data_pairs) ⇒ Object
Return fitted values of [a, b] in a e^bx from more than two data points. Argument ‘data_pairs’ should be Array of Array’s. For example,
[
[ 1.0, 2.0],
[ 2.0, 4.0],
[ 3.0, 6.0],
]
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/malge/leastsquare.rb', line 91 def self.least_square_a_exp_bx(data_pairs) data_pairs.each do |pair| raise UnableCalculationError if pair[1] == 0.0 end data_pairs = data_pairs.map do |pair| [pair[0], Math::log(pair[1])] end coefficients = self.least_square_1st_degree(data_pairs) [Math::exp(coefficients[0]), coefficients[1]] end |
.least_square_proportional(data_pairs) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/malge/leastsquare.rb', line 40 def self.least_square_proportional(data_pairs) a = 0.0 #x^2 d = 0.0 #x*y n = data_pairs.size data_pairs.each do |pairs| x = pairs[0].to_f y = pairs[1].to_f a += x**2 d += x*y end [d/a] end |
.variance_1st_degree(data_pairs) ⇒ Object
Return variance when fitted to y = a_0 x^0 + a_1 x^1. Argument ‘data_pairs’ should be Array of Array’s. For example,
[
[ 1.0, 2.0],
[ 2.0, 4.0],
[ 3.0, 6.0],
]
60 61 62 63 64 65 66 67 68 |
# File 'lib/malge/leastsquare.rb', line 60 def self.variance_1st_degree(data_pairs) coefficients = self.least_square_1st_degree(data_pairs) data_pairs.inject(0.0) do |sum, pair| x = pair[0] y = pair[1] f_x = coefficients[0] + coefficients[1] * x sum += (y - f_x)**2 end end |