File: examples-2.php

 Classes of Pier-André Bouchard St-Amant > Matrix new > examples-2.php > Download
File: examples-2.php Example script text/plain More advanced examples. Matrix new Perform operations to manipulate matrices By Pier-André Bouchard St-Amant 5 years ago 6,151 bytes

Contents

 ``` Examples with matrix class This line is simple HTML you should be able to read this, no matter if you have PHP or not on your server.

1. The simplest example is to solve a system of linear equations :
x_1+3 x_2+5 x_3=1
3x_1+5 x_2+11 x_3=2
5x_1+11 x_2+9 x_3=3

Which translates into the following matricial system :
135
3511
5119
x_1
x_2
x_3
=
1
2
3
Or :
Ax=b

The solution is obviously to inverse the A matrix :
x=A^(-1)b

We can do this using this class :
\$A = new matrix(array(array(1,3,5), array(3,5,11), array(5,11,9)));
\$b = new matrix(array(array(1), array(2), array(3)));
\$A_inv = \$A->inv();
\$sol = \$A_inv->times(\$b);
\$sol->print_matrix();

Which yields the following solution : inv(); \$sol = \$A_inv->times(\$b); \$sol->print_matrix(); ?>
• We can also perform a regression :
Let X be the following matrix of data :
• 10.2110
10.3511
10.4512
10.5213
10.6314
10.7915

(Note that the matrix X is almost colinear)
103.20
113.30
123.40
133.50
143.60
153.70

The model is then y = b_1 * 1 + b_2 * x_2 + b_3*x_3 + e, or y = Xb + e and the minimised norm of e is given by : b = (X*X)^(-1)*X'y. This can be done with the following code :
\$arr = array(array(1,0.21, 10), array(1,0.35, 11), array(1,0.45, 12), array(1,0.52, 13), array(1,0.63, 14), array(1,0.79, 15));
\$X = new matrix(\$arr);
\$arr = array(array(103.20), array(113.30), array(123.40), array(133.50), array(143.60), array(153.70));
\$y = new matrix(\$arr);
\$X_p = \$X->prime();
\$XX = \$X_p->times(\$X);
\$XX_inv = \$XX->inv();
\$X_py = \$X_p->times(\$y);
\$b = \$XX_inv->times(\$X_py);
\$b->print_matrix();
prime(); \$XX = \$X_p->times(\$X); \$XX_inv = \$XX->inv(); \$X_py = \$X_p->times(\$y); \$b = \$XX_inv->times(\$X_py); \$b->print_matrix(); ?>
The predicted values of y and e, the variance of the error terms and the covariance matrix can then be found with :
\$y_hat = \$X->times(\$b);
\$e_hat = \$y->minus(\$y_hat);
\$e_hat_p = \$e_hat->prime();
\$sigma2 = \$e_hat_p->times(\$e_hat);
\$sigma2 = \$sigma2->s_times(1/(\$X->get_num_rows() - \$X->get_num_columns()));
\$cov = \$XX_inv->s_times(\$sigma2->get_value(1,1));
\$cov->print_matrix();

times(\$b); \$e_hat = \$y->minus(\$y_hat); \$e_hat_p = \$e_hat->prime(); \$sigma2 = \$e_hat_p->times(\$e_hat); \$sigma2 = \$sigma2->s_times(1/(\$X->get_num_rows() - \$X->get_num_columns())); \$cov = \$XX_inv->s_times(\$sigma2->get_value(1,1)); \$cov->print_matrix(); ?>
• We can also perform some fixed point algorithms. Let X be the following Markov transition matrix :
• 0.60.40.0
0.20.10.7
0.10.30.6

One can then find the limiting probability distribution by iterating until convergence :
\$arr = array(array(0.6, 0.4, 0), array(0.2, 0.1, 0.7), array(0.1, 0.3, 0.6));
\$X = new matrix(\$arr);
\$power = \$X;
\$tolerance = 0.000001;
\$check = 10;
\$iter = 0;

while(\$check > \$tolerance)
{
\$last_max = \$max;
\$power = \$power->times(\$X);

\$max = \$power->mat_max();
\$max = \$max->prime();
\$max = \$max->mat_max();
\$max = \$max->get_value(1,1);
\$check = abs(\$max - \$last_max);

\$iter++;

}
\$power->print_matrix(); echo "Number of iterations : \$iter";

\$tolerance) {     \$last_max = \$max;     \$power = \$power->times(\$X);         \$max = \$power->mat_max();     \$max = \$max->prime();     \$max = \$max->mat_max();     \$max = \$max->get_value(1,1);     \$check = abs(\$max - \$last_max);     \$iter++;   } \$power->print_matrix(); echo "Number of iterations : \$iter

"; ?> ```