Multiple regression: Difference between revisions
m (→{{header|Ruby}}: use correct output function) |
(Mark this task as being too sparsely implemented) |
||
Line 1: | Line 1: | ||
{{task|Matrices}} |
{{task|Matrices}}[[Category:Less Than 5 Examples]] |
||
Given a set of data vectors in the following format: |
Given a set of data vectors in the following format: |
||
Revision as of 08:53, 9 July 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Given a set of data vectors in the following format:
Compute the vector using ordinary least squares regression using the following equation:
You can assume y is given to you as an array, and x is given to you as a two-dimensional array.
Note: This is more general than Polynomial Fitting, which only deals with 2 datasets and only deals with polynomial equations. Ordinary least squares can deal with an arbitrary number of datasets (limited by the processing power of the machine) and can have more advanced equations such as:
Ruby
Using the standard library Matrix class:
<lang ruby>require 'matrix'
def regression_coefficients y, x
y = Matrix.column_vector y.map { |i| i.to_f } x = Matrix.columns x.map { |xi| xi.map { |i| i.to_f }}
(x.t * x).inverse * x.t * y
end</lang>
Testing: <lang ruby>puts regression_coefficients([1, 2, 3, 4, 5], [ [2, 1, 3, 4, 5] ])</lang> Output:
Matrix[[0.981818181818182]]