Module: BTAP::LinearRegression

Defined in:
lib/openstudio-standards/btap/linear_regression.rb

Defined Under Namespace

Classes: LinearModel

Class Method Summary collapse

Class Method Details

.extrapolation_boundaries_exceeded? ⇒ TrueClass|FalseClass

Read and reset the error to not carry over from previous calls in the case of using this module across different facilities.

Returns:



8
9
10
11
12
# File 'lib/openstudio-standards/btap/linear_regression.rb', line 8

def self.extrapolation_boundaries_exceeded?
  value = @extrapolation_boundaries_exceeded
  @extrapolation_boundaries_exceeded = false
  return value
end

.fmt(message) ⇒ String

Parameters:

Returns:



16
17
18
# File 'lib/openstudio-standards/btap/linear_regression.rb', line 16

def self.fmt(message)
  return "#{[BTAP::LinearRegression]} #{message}"
end

.interpolate(x_y_array:, x2:, extrapolation_range: 0.02) ⇒ [Float, String]

Interpolate a 2D array for the dependent variable x2 and get the y-intercept. Sets the extrapolation class variable if extrapolation boundaries were exceeded.

Parameters:

  • x_y_array (Array[Array]) —

    2D array of floats.

  • x2 (Float) —

    Dependent variable to be interpolated.

  • extrapolation_range (Float) (defaults to: 0.02)

Returns:

  • ([Float, String]) —

    The interpolated y-intercept and the warning message, "OK" if none.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/openstudio-standards/btap/linear_regression.rb', line 29

def self.interpolate(x_y_array:, x2:, extrapolation_range: 0.02)
  notes = "OK"
  array = x_y_array.uniq.sort { |a, b| a[0] <=> b[0] }
  extrapolation_percent = extrapolation_range * 100

  if array.empty? 
    return 0.0, fmt("Empty array given for interpolation, returning zero.")
  elsif array.size == 1
    return array.first[1].to_f, notes
  end

  x_lower_bound = (1.0 - extrapolation_range) * array[0][0]
  x_upper_bound = (1.0 + extrapolation_range) * array[-1][0]
  y_lower_bound = (1.0 - extrapolation_range) * array[0][1]
  y_upper_bound = (1.0 + extrapolation_range) * array[-1][1]

  if x2 < x_lower_bound
    return y_lower_bound, fmt("Warning: Dependent variable #{x2} precedes the lower bound " \
                        "(#{x_lower_bound}) for the #{extrapolation_percent}% range. Returning the lower " \
                        "bound.")
  elsif x2 > x_upper_bound
    @extrapolation_boundaries_exceeded = true
    return y_upper_bound, fmt("Warning: Dependent variable #{x2} exceeds the upper bound " \
                        "(#{x_upper_bound}) for the #{extrapolation_percent}% range. Returning the upper " \
                        "bound.")
  elsif x2 < array.first[0].to_f

    # Extrapolate down using first and second cost value to this
    # out-of-range input.
    x_array      = [array[0][0].to_f, array[1][0].to_f]
    y_array      = [array[0][1].to_f, array[1][1].to_f]
    linear_model = LinearModel.new(x_array, y_array)
    y2           = linear_model.y_intercept + linear_model.slope * x2
    return y2, notes
  elsif x2 > array.last[0].to_f

    # Extrapolate up using second to last and last cost value to this
    # out-of-range input.
    x_array      = [array[-2][0].to_f, array[-1][0].to_f]
    y_array      = [array[-2][1].to_f, array[-1][1].to_f]
    linear_model = LinearModel.new(x_array, y_array)
    y2           = linear_model.y_intercept + linear_model.slope * x2
    return y2, notes
  else
    array.each_index do |counter|

      # skip last value.
      next if array[counter] == array.last

      x0 = array[counter][0]
      y0 = array[counter][1]
      x1 = array[counter + 1][0]
      y1 = array[counter + 1][1]

      # Skip if x2 is not between x0 and x1.
      next if x2 < x0 || x2 > x1

      y2 = y0 # Just in-case x0, x1 and x2 are identical.
      if (x1 - x0) > 0.0
        y2 = y0.to_f + ((y1 - y0).to_f * (x2 - x0).to_f / (x1 - x0).to_f)
      end
      return y2, notes
    end
  end
end