Class: Callidus::LinearRegression

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of LinearRegression.



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

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

    @trained = false
end

Instance Attribute Details

#correlationObject (readonly)

Returns the value of attribute correlation.



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

def correlation
  @correlation
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

#predicted_outputObject

Returns the value of attribute predicted_output.



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

def predicted_output
  @predicted_output
end

#slopeObject (readonly)

Returns the value of attribute slope.



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

def slope
  @slope
end

#standard_errorObject (readonly)

Returns the value of attribute standard_error.



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

def standard_error
  @standard_error
end

#y_interceptObject (readonly)

Returns the value of attribute y_intercept.



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

def y_intercept
  @y_intercept
end

Instance Method Details

#find_correlationObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/src/LinearRegression.rb', line 64

def find_correlation
    assert_trained()

    mean = @output.sum/@output.size

    diffYM = @output.map { |y| (y - mean) ** 2 };
    diffPYM = @predicted_output.map { |y| (y - mean) ** 2 };

    @correlation = (diffPYM.sum/diffYM.sum).round(3)

    self
end

#find_standard_errorObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/src/LinearRegression.rb', line 77

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/LinearRegression.rb', line 28

def formatted
    assert_trained()

    "f(x) = #{@slope}x + #{@y_intercept}"
end

#predict(x) ⇒ Object



93
94
95
96
97
# File 'lib/src/LinearRegression.rb', line 93

def predict(x)
    assert_trained()
    
    x * @slope + @y_intercept
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
# File 'lib/src/LinearRegression.rb', line 34

def train
    sum_x = 0
    sum_y = 0
    sum_xy = 0
    sum_xx = 0

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

    @predicted_output = []

    n.times do |i|
        x = @input[i]
        y = @output[i]

        sum_x += x
        sum_y += y
        sum_xx += (x * x)
        sum_xy += (x * y)
    end

    @slope = ((n * sum_xy - sum_x * sum_y).to_f / (n * sum_xx - sum_x * sum_x).to_f).round(3)
    @y_intercept = ((sum_y / n).to_f - (@slope * sum_x).to_f / n.to_f).round(3)

    @predicted_output = @input.map { |x| (x * @slope + @y_intercept).round(3) }

    @trained = true

    self.find_correlation.find_standard_error
end