Multiple regression: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
m (Moved link to text)
Line 6: Line 6:
<math>x_i = \{ x_{i1}, x_{i2}, ..., x_{in} \}, i \in 1..k\,</math>
<math>x_i = \{ x_{i1}, x_{i2}, ..., x_{in} \}, i \in 1..k\,</math>


Compute the vector <math>\beta = \{ \beta_1, \beta_2, ..., \beta_n \}</math> using ordinary least squares regression using the following equation:
Compute the vector <math>\beta = \{ \beta_1, \beta_2, ..., \beta_n \}</math> using
[[wp:Ordinary least squares|ordinary least squares]] regression using the following equation:


<math>y_i = \beta \cdot x_i\,</math>
<math>y_i = \beta \cdot x_i\,</math>


You can assume <i>y</i> is given to you as an array, and <i>x</i> is given to you as a two-dimensional array.
You can assume <i>y</i> is given to you as an array, and <i>x</i> is given to you as a two-dimensional array.

Wikipedia: http://en.wikipedia.org/wiki/Ordinary_least_squares


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:
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:

Revision as of 18:10, 30 June 2009

Task
Multiple regression
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> regression_coefficients([1, 2, 3, 4, 5], [ [2, 1, 3, 4, 5] ]) # => Matrix0.981818181818182 </lang>