Class: RubyLinearRegression

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRubyLinearRegression

Returns a new instance of RubyLinearRegression.



7
8
9
10
# File 'lib/ruby_linear_regression.rb', line 7

def initialize
  @mu = 0
  @sigma = 1
end

Instance Attribute Details

#muObject (readonly)

Returns the value of attribute mu.



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

def mu
  @mu
end

#sigmaObject (readonly)

Returns the value of attribute sigma.



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

def sigma
  @sigma
end

#thetaObject (readonly)

Returns the value of attribute theta.



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

def theta
  @theta
end

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#compute_costObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_linear_regression.rb', line 27

def compute_cost
  # Compute the mean squared cost / error function

  # First use matrix multiplication and vector subtracton to find errors

  errors = (@x * @theta) - @y

  # Then square all errors

  errors = errors.map { |e| e * e  }

  # Find the mean of the square errors

  mean_square_error = 0.5 * (errors.inject{ |sum, e| sum + e }.to_f / errors.row_size)

  return mean_square_error
end

#load_training_data(x_data, y_data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_linear_regression.rb', line 12

def load_training_data x_data, y_data

      # normalize the x_data

      x_data = normalize_data( x_data )

      # add 1 column to our data

      x_data = x_data.map { |r| [1].concat(r) }

      # build our x Matrix & y Vector

      @x = Matrix.rows( x_data )
      @y = Matrix.rows( y_data.collect { |e| [e] } )

      @theta = Matrix[[0],[0]]
end

#predict(data) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby_linear_regression.rb', line 49

def predict data

  # normalize

  data.each_index do |i|
    data[i] = (data[i] - @mu[i]) / @sigma[i].to_f
  end

  # add 1 column to prediction data

  data = [1].concat( data )

  # perform prediction

  prediction = (Matrix[data] * @theta)[0,0].to_f

  return prediction

end

#train_normal_equationObject



41
42
43
44
45
46
47
# File 'lib/ruby_linear_regression.rb', line 41

def train_normal_equation
  # Calculate the optimal theta using the normal equation

  # theta = ( X' * X )^1 * X' * y

  @theta = (@x.transpose * @x).inverse * @x.transpose * @y

  return @theta
end