Implementing multivariate linear regression simulation curve with PHP

Source: Internet
Author: User

Multivariate linear regression model: y = b1x1 + b2x2 + b3x3 + ... +bnxn;

We are based on a set of data: similar to arr_x = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]; Arr_y = [5, 10, 15]; The last we asked for was an array that contained the B1 to Bn;

Methods: Using least squares method

Formula: We only use the first half of the formula, that is, the matrix to calculate

The x in the formula is arr_x, a two-dimensional array we can think of as a matrix, the y in the formula is arr_y, and it is considered a matrix (5, 10, 15), but it should be written vertically.

Then we can find that the matrix is multiplied, transpose, and inverse by the formula, so the following code gives:

Public Function Get_complement ($data, $i, $j)
{
/* x and Y are the number of rows and columns of the matrix data */
$x = count ($data);
$y = count ($data [0]);
/* data2 for the remaining matrix */
$data 2 = [];
for ($k = 0; $k < $x-1; $k + +) {
if ($k < $i) {
for ($kk = 0; $kk < $y-1; $kk + +) {
if ($kk < $j) {
$data 2[$k] [$KK] = $data [$k] [$KK];
} else {
$data 2[$k] [$KK] = $data [$k] [$kk + 1];
}
}
} else {
for ($kk = 0; $kk < $y 1; $kk + +) {
if ($kk < $j) {
$data 2[$k] [$KK] = $data [$k + 1][$kk ];
} else {
$data 2[$k] [$KK] = $data [$k + 1][$kk + 1];
}
}
}
}
return $data 2;
}

/* Calculate Matrix determinant */
Public Function Cal_det ($data)
{
$ans = 0;
if (count ($data [0]) = = = 2) {
$ans = $data [0][0] * $data [1][1]-$data [0][1] * $data [1][0];
} else {
for ($i = 0; $i < count ($data [0]); $i + +) {
$data _temp = $this->get_complement ($data, 0, $i);
if ($i% 2 = = = 0) {
$ans = $ans + $data [0][$i] * ($this->cal_det ($data _temp));
} else {
$ans = $ans-$data [0][$i] * ($this->cal_det ($data _temp));
}
}
}
return $ans;
}

/* The adjoint matrix of the computed matrix */
Public Function Ajoint ($data)
{
$m = count ($data);
$n = count ($data [0]);
$data 2 = [];
for ($i = 0; $i < $m; $i + +) {
for ($j = 0; $j < $n; $j + +) {
if (($i + $j)% 2 = = = 0) {
$data 2[$i] [$j] = $this->cal_det ($this->get_complement ($data, $i, $j));
} else {
$data 2[$i] [$j] =-$this->cal_det ($this->get_complement ($data, $i, $j));
}
}
}
return $this->trans ($data 2);
}

/* Transpose Matrix */
Public function trans ($data)
{
$i = count ($data);
$j = count ($data [0]);
$data 2 = [];
for ($k 2 = 0; $k 2 < $j; $k 2 +) {
for ($k 1 = 0; $k 1 < $i; $k 1 +) {
$data 2[$k 2][$k 1] = $data [$k 1][$k 2];
}
}
/* Transpose The matrix to get the adjoint matrix */
return $data 2;
}

/* Inverse of the matrix, the input parameter is the original matrix */
Public Function Inv ($data)
{
$m = count ($data);
$n = count ($data [0]);
$data 2 = [];
$det _val = $this->cal_det ($data);
$data 2 = $this->ajoint ($data);
for ($i = 0; $i < $m; $i + +) {
for ($j = 0; $j < $n; $j + +) {
$data 2[$i] [$j] = $data 2[$i] [$j]/$det _val;
}
}
return $data 2;
}

/* To find the product of two matrices */
Public Function GetProduct ($data 1, $data 2)
{
/* $data 1 is the left multiplicative matrix */
$m 1 = count ($data 1);
$n 1 = count ($data 1[0]);
$m 2 = count ($data 2);
$n 2 = count ($data 2[0]);
$data _new = [];
if ($n 1!== $m 2) {
return false;
} else {
for ($i = 0; $i <= $m 1-1; $i + +) {
for ($k = 0; $k <= $n 2-1; $k + +) {
$data _new[$i] [$k] = 0;
for ($j = 0; $j <= $n 1-1; $j + +) {
$data _new[$i] [$k] + = $data 1[$i] [$j] * $data 2[$j] [$k];
}
}
}
}
return $data _new;
}

/* Multivariate linear equations */
Public Function Getparams ($arr _x, $arr _y)
{
$final = [];
$arr _x_t = $this->trans ($arr _x);
$result = $this->getproduct ($this->getproduct ($this->inv ($this->getproduct ($arr _x_t, $arr _x)), $arr _x_ T), $arr _y);
foreach ($result as $key = = $val) {
foreach ($val as $_k = $_v) {
$final [] = $_v;
}
}
return $final;
}

The last Getparams () method is the last method of finding a B-parameter array, passing in a two-dimensional array arr_x, and a one-dimensional array arr_y.

This is typically used for big data analysis to simulate and predict the following developments and trends based on big data.

Implementing multivariate linear regression simulation curve with PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.