Class: BTAP::LinearRegression::LinearModel
- Inherits:
-
Object
- Object
- BTAP::LinearRegression::LinearModel
- Defined in:
- lib/openstudio-standards/btap/linear_regression.rb
Instance Method Summary collapse
-
#initialize(xs, ys) ⇒ LinearModel
constructor
A new instance of LinearModel.
- #mean(values) ⇒ Object
- #slope ⇒ Object
- #y_intercept ⇒ Object
Constructor Details
#initialize(xs, ys) ⇒ LinearModel
Returns a new instance of LinearModel.
96 97 98 99 100 101 |
# File 'lib/openstudio-standards/btap/linear_regression.rb', line 96 def initialize(xs, ys) @xs, @ys = xs, ys if @xs.length != @ys.length raise "Unbalanced data. xs need to be same length as ys" end end |
Instance Method Details
#mean(values) ⇒ Object
122 123 124 125 |
# File 'lib/openstudio-standards/btap/linear_regression.rb', line 122 def mean(values) total = values.reduce(0) { |sum, x| x + sum } return Float(total) / Float(values.length) end |
#slope ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/openstudio-standards/btap/linear_regression.rb', line 107 def slope x_mean = mean(@xs) y_mean = mean(@ys) numerator = (0...@xs.length).reduce(0) do |sum, i| sum + ((@xs[i] - x_mean) * (@ys[i] - y_mean)) end denominator = @xs.reduce(0) do |sum, x| sum + ((x - x_mean) ** 2) end return (numerator / denominator) end |
#y_intercept ⇒ Object
103 104 105 |
# File 'lib/openstudio-standards/btap/linear_regression.rb', line 103 def y_intercept return mean(@ys) - (slope * mean(@xs)) end |