Class: SimpleLinearRegression
- Inherits:
-
Object
- Object
- SimpleLinearRegression
- Defined in:
- lib/openstudio-standards/btap/costing/btap_costing.rb
Instance Method Summary collapse
- #initialize(xs, ys) ⇒ SimpleLinearRegression constructor
- #mean(values) ⇒ Object
- #slope ⇒ Object
- #y_intercept ⇒ Object
Constructor Details
#initialize(xs, ys) ⇒ SimpleLinearRegression
7 8 9 10 11 12 |
# File 'lib/openstudio-standards/btap/costing/btap_costing.rb', line 7 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
33 34 35 36 |
# File 'lib/openstudio-standards/btap/costing/btap_costing.rb', line 33 def mean(values) total = values.reduce(0) { |sum, x| x + sum } return Float(total) / Float(values.length) end |
#slope ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/openstudio-standards/btap/costing/btap_costing.rb', line 18 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
14 15 16 |
# File 'lib/openstudio-standards/btap/costing/btap_costing.rb', line 14 def y_intercept return mean(@ys) - (slope * mean(@xs)) end |