PHP algorithm for multivariate linear regression simulation curve based on PHP

Source: Internet
Author: User
This article mainly introduces the multi-linear regression simulation curve algorithm based on PHP implementation, and analyzes the principle of the multivariate linear regression simulation curve algorithm and the relevant PHP implementation skills with the specific instance form, and the friends who need can refer to the following

In this paper, we describe a multi-linear regression simulation curve algorithm based on PHP implementation. Share to everyone for your reference, as follows:

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, ten], [One, ten,, +]]; arr_y = [5,] ; The last we asked for was a number Group, including from 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] = $dat        a[$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% = = = 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;}  /* For the 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;}  /* For 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 equation */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 to find the method of the B-parameter array, pass 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.

PS: We recommend two relevant simulation curve tools for your reference:

On-line polynomial curve and curve function fitting tool:
Http://tools.jb51.net/jisuanqi/create_fun

Online plotting of polynomial/function curve graphic tools:
Http://tools.jb51.net/jisuanqi/fun_draw

Articles you may be interested in:

PHP bidirectional list definition and usage example PHP tips

PHP uses a foreach Magic Transform Array (example) for PHP instances

The solution of error in accessing array elements based on PHP double quotes PHP Tips

Related Article

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.