<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="fr" style="position: static; "><head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Examples with matrix class</title> </head> <body> This line is simple HTML you should be able to read this, no matter if you have PHP or not on your server.<br><br>
<?php include_once("/Users/pabsta/Desktop/matrix/matrix.php"); ?> <ol> <li>The simplest example is to solve a system of linear equations : <br> <table> <tr><td>x_1</td><td>+</td><td>3 x_2</td><td>+</td><td>5 x_3</td><td>=</td><td>1</td></tr> <tr><td>3x_1</td><td>+</td><td>5 x_2</td><td>+</td><td>11 x_3</td><td>=</td><td>2</td></tr> <tr><td>5x_1</td><td>+</td><td>11 x_2</td><td>+</td><td>9 x_3</td><td>=</td><td>3</td></tr> </table><br> Which translates into the following matricial system : <br> <table> <tr><td> <table> <tr align="center"><td>1</td><td>3</td><td>5</td><td></tr> <tr align="center"><td>3</td><td>5</td><td>11</td><td></tr> <tr align="center"><td>5</td><td>11</td><td>9</td><td></tr> </table> </td> <td> <table><tr><td>x_1</td></tr><tr><td>x_2</td></tr><tr><td>x_3</td></tr></table> </td> <td>=</td> <td><table><tr><td>1</td></tr><tr><td>2</td></tr><tr><td>3</td></tr></table></td> </tr> </table> Or : <table> <tr><td>A</td><td>x</td><td>=</td><td>b</td></tr> </table><br> The solution is obviously to inverse the A matrix : <table> <tr><td>x</td><td>=</td><td>A^(-1)b</td></tr> </table><br> We can do this using this class :<br> <code> $A = new matrix(array(array(1,3,5), array(3,5,11), array(5,11,9)));<br> $b = new matrix(array(array(1), array(2), array(3)));<br> $A_inv = $A->inv();<br> $sol = $A_inv->times($b);<br> $sol->print_matrix();<br> </code><br> Which yields the following solution : <?php $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(); ?><br> <li>We can also perform a regression : <br> Let X be the following matrix of data : <br> <table> <tr align="center"><td>1</td><td>0.21</td><td>10</td></tr> <tr align="center"><td>1</td><td>0.35</td><td>11</td></tr> <tr align="center"><td>1</td><td>0.45</td><td>12</td></tr> <tr align="center"><td>1</td><td>0.52</td><td>13</td></tr> <tr align="center"><td>1</td><td>0.63</td><td>14</td></tr> <tr align="center"><td>1</td><td>0.79</td><td>15</td></tr> </table><br> (Note that the matrix X is almost colinear)<br> <table> <tr align="center"><td>103.20</td></tr> <tr align="center"><td>113.30</td></tr> <tr align="center"><td>123.40</td></tr> <tr align="center"><td>133.50</td></tr> <tr align="center"><td>143.60</td></tr> <tr align="center"><td>153.70</td></tr> </table><br> 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 : <br> <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));<br> $X = new matrix($arr);<br> $arr = array(array(103.20), array(113.30), array(123.40), array(133.50), array(143.60), array(153.70));<br> $y = new matrix($arr);<br> $X_p = $X->prime();<br> $XX = $X_p->times($X);<br> $XX_inv = $XX->inv();<br> $X_py = $X_p->times($y);<br> $b = $XX_inv->times($X_py); <br> $b->print_matrix();<br> </code> <?php $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(); ?><br> The predicted values of y and e, the variance of the error terms and the covariance matrix can then be found with : <br> <code> $y_hat = $X->times($b);<br> $e_hat = $y->minus($y_hat);<br> $e_hat_p = $e_hat->prime();<br> $sigma2 = $e_hat_p->times($e_hat);<br> $sigma2 = $sigma2->s_times(1/($X->get_num_rows() - $X->get_num_columns()));<br> $cov = $XX_inv->s_times($sigma2->get_value(1,1));<br> $cov->print_matrix();<br> </code><br> <?php $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(); ?><br> </li> <li>We can also perform some fixed point algorithms. Let X be the following Markov transition matrix :<br> <table> <tr align="center"><td>0.6</td><td>0.4</td><td>0.0</td></tr> <tr align="center"><td>0.2</td><td>0.1</td><td>0.7</td></tr> <tr align="center"><td>0.1</td><td>0.3</td><td>0.6</td></tr> </table><br> One can then find the limiting probability distribution by iterating until convergence : <br> <code> $arr = array(array(0.6, 0.4, 0), array(0.2, 0.1, 0.7), array(0.1, 0.3, 0.6));<br> $X = new matrix($arr);<br> $power = $X;<br> $tolerance = 0.000001;<br> $check = 10;<br> $iter = 0;<br><br> while($check > $tolerance)<br> {<br> $last_max = $max;<br> $power = $power->times($X);<br><br> $max = $power->mat_max();<br> $max = $max->prime();<br> $max = $max->mat_max();<br> $max = $max->get_value(1,1);<br> $check = abs($max - $last_max);<br><br> $iter++;<br><br> }<br> $power->print_matrix(); echo "Number of iterations : $iter";<br> </code><br><br> <?php $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<br><br>"; ?> </li> </ol>
</body></html>
|