Class: Callidus::ExponentialRegression

Inherits:
Object
  • Object
show all
Defined in:
lib/src/ExponentialRegression.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip = [], op = []) ⇒ ExponentialRegression

Returns a new instance of ExponentialRegression.



15
16
17
18
19
20
# File 'lib/src/ExponentialRegression.rb', line 15

def initialize(ip = [], op = [])
    @input = ip
    @output = op

    @trained = false
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



9
10
11
# File 'lib/src/ExponentialRegression.rb', line 9

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



10
11
12
# File 'lib/src/ExponentialRegression.rb', line 10

def b
  @b
end

#correlationObject (readonly)

Returns the value of attribute correlation.



12
13
14
# File 'lib/src/ExponentialRegression.rb', line 12

def correlation
  @correlation
end

#inputObject

Returns the value of attribute input.



5
6
7
# File 'lib/src/ExponentialRegression.rb', line 5

def input
  @input
end

#outputObject

Returns the value of attribute output.



6
7
8
# File 'lib/src/ExponentialRegression.rb', line 6

def output
  @output
end

#predicted_outputObject

Returns the value of attribute predicted_output.



7
8
9
# File 'lib/src/ExponentialRegression.rb', line 7

def predicted_output
  @predicted_output
end

#standard_errorObject (readonly)

Returns the value of attribute standard_error.



13
14
15
# File 'lib/src/ExponentialRegression.rb', line 13

def standard_error
  @standard_error
end

Instance Method Details

#find_standard_errorObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/src/ExponentialRegression.rb', line 67

def find_standard_error
    assert_trained()

    n = @input.size > @output.size ? @output.size : @input.size

    diffs = []

    n.times do |i|
        diffs << (@predicted_output[i] - @output[i]) ** 2
    end

    @standard_error = (Math.sqrt(diffs.sum/(n - 2.0))).round(3)

    self
end

#formattedObject



28
29
30
31
32
# File 'lib/src/ExponentialRegression.rb', line 28

def formatted
    assert_trained()

    "f(x) = #{@a}e^#{@b}x"
end

#predict(x) ⇒ Object



83
84
85
86
87
# File 'lib/src/ExponentialRegression.rb', line 83

def predict(x)
    assert_trained()

    @a * (2.7182818284590452353602 ** (@b * x))
end

#trainObject



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
# File 'lib/src/ExponentialRegression.rb', line 34

def train
    mean_x = @input.mean
    mean_lny = @output.inject(0.0) { |s, n|  s + Math.log(n) } / @output.size

    sum_xx = 0.0
    sum_xy = 0.0
    sum_yy = 0.0

    @input.each_index do |i|
        sum_xx += @input[i] * @input[i]
        sum_xy += @input[i] * Math.log(@output[i])
        sum_yy += Math.log(@output[i]) * Math.log(@output[i])
    end

    sum_xx = (sum_xx / @input.size) - (mean_x * mean_x)
    sum_xy = (sum_xy / @input.size) - (mean_x * mean_lny)
    sum_yy = (sum_yy / @output.size) - (mean_lny * mean_lny)

    @b = sum_xy / sum_xx
    @a = 2.7182818284590452353602 ** (mean_lny - b * mean_x)

    @predicted_output = @input.map { |x| (@a * (2.7182818284590452353602 ** (@b * x))).round(3) }

    @correlation = (sum_xy / (Math.sqrt(sum_xx) * Math.sqrt(sum_yy))).round(3)

    @trained = true

    @a = @a.round(3)
    @b = @b.round(3)

    self.find_standard_error
end