LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php moving average (https://www.linuxquestions.org/questions/programming-9/php-moving-average-732829/)

pcock 06-14-2009 06:00 AM

[SOLVED] php moving average
 
Hello,

Suppose I have the following array to generate a table. How can I perform calculation to get the moving average of last 10 collections on the 11th row (not the 10th)? I managed to pull the index value for each array but have no clue how to go further. Also, any suggestions to improve the code is most appreciated.


PHP Code:

<?php

$amounts 
= array(
       
102030405060708090,
       
1002003004005006007008009001000,
       
10002000300040005000600070008000900010000);

echo 
'<table border="1"><thead><tr>
  <th>day</th>
  <th>amount</th>
  <th>Moving Average</th><tr></thead>'
;

foreach (
$amounts as $day => $amount) {
  echo 
"<tr>";
  echo 
"<td>".$day "</td><td> " .$amount"</td><td>&nbsp;</td>";
  echo 
"</tr>";
}
  echo 
"</table>";

?>

Thanks

jlinkels 06-14-2009 08:42 AM

This can't be difficult, not even in PHP, but you should clarify your question. Now it is fully incomprehensible.

Quote:

Originally Posted by pcock (Post 3573354)
moving average of last 10 collections on the 11th row (not the 10th)?

Which 10 collections? Do you mean "a collection of 10 values in this array"?

Quote:

Originally Posted by pcock (Post 3573354)
pull the index value for each array but have no clue how to go further. Also, any suggestions to improve the code is most appreciated.

Each array? I see only one array.

Please make clear what your intention is, give values and examples of what you want to calculate. When that is fully clear, translating it into PHP is a breeze.

jlinkels

pcock 06-14-2009 04:02 PM

Quote:

Originally Posted by jlinkels (Post 3573443)
Each array? I see only one array.
jlinkels

Sorry, I meant values in array.

Here's a sample table of what i'm expecting to achieve.
Code:

<table border="1">
<thead><tr><th>day</th><th>amount</th><th>Moving Average</th></tr></thead>
<tr><td>0</td><td>10</td><td></td></tr>
<tr><td>1</td><td>20</td><td></td></tr>
<tr><td>2</td><td>30</td><td></td></tr>
<tr><td>3</td><td>40</td><td></td></tr>
<tr><td>4</td><td>50</td><td></td></tr>
<tr><td>5</td><td>60</td><td></td></tr>
<tr><td>6</td><td>70</td><td></td></tr>
<tr><td>7</td><td>80</td><td></td></tr>
<tr><td>8</td><td>90</td><td></td></tr>
<tr><td>9</td><td>100</td><td></td></tr>
<tr><td>10</td><td>200</td><td>55</td></tr>
<tr><td>11</td><td>300</td><td>74</td></tr>
<tr><td>12</td><td>400</td><td>102</td></tr>
<tr><td>13</td><td>500</td><td>139</td></tr>
<tr><td>14</td><td>600</td><td>185</td></tr>
<tr><td>15</td><td>700</td><td>240</td></tr>
<tr><td>16</td><td>800</td><td>304</td></tr>
<tr><td>17</td><td>900</td><td>377</td></tr>
<tr><td>18</td><td>1000</td><td>459</td></tr>
<tr><td>19</td><td>1000</td><td>550</td></tr>
<tr><td>20</td><td>2000</td><td>640</td></tr>
<tr><td>21</td><td>3000</td><td>829</td></tr>
<tr><td>22</td><td>4000</td><td>1090</td></tr>
<tr><td>23</td><td>5000</td><td>1450</td></tr>
<tr><td>24</td><td>6000</td><td>1900</td></tr>
<tr><td>25</td><td>7000</td><td>2440</td></tr>
<tr><td>26</td><td>8000</td><td>3070</td></tr>
<tr><td>27</td><td>90000</td><td>3790</td></tr>
<tr><td>28</td><td>10000</td><td>4600</td></tr>
</table>


jlinkels 06-14-2009 09:19 PM

This should come close. Bear in mind that with a moving average you'll always having difficulties at the start and end of the data range as a moving average assumes a continuous data stream.

PHP Code:

?
$amounts = array(
  
102030405060708090,
  
1002003004005006007008009001000,
  
10002000300040005000600070008000900010000);
$AVG_WINDOW=10;

/*
* We crate a new array with the same indices as $amounts,
* but then for the average. This could be done in the same
* array as well by making it two-dimensional.
* Build the first sum:
*/
$sum 0;
for (
$i 0$i $AVG_WINDOW$i++){
  
$sum $sum $amounts[$i];
}

$last_i count ($amounts);
for (
$i $AVG_WINDOW$i $last_i$i++){
  
$averages[$i]=$sum / ($AVG_WINDOW);
  
$sum $sum $amounts[$i-$AVG_WINDOW] + $amounts[$i];
}

/*
* Table header etc here....
*/
for ($i=0$i $last_i$i++){
  echo 
"<tr><td>";
  echo 
$i;
  echo 
"</td>";
  echo 
"<td>";
  echo 
$amounts[$i];
  echo 
"</td>";
  echo 
"<td>";
  if (isset(
$averages[$i])){ 
    echo 
$averages[$i];
  }
  echo 
"</td></tr>\n";
}

/*
Table closing tag, other stuff here...
*/

?> 

jlinkels

pcock 06-15-2009 08:51 PM

Thanks jlinkels :)
Now I'll have to try and figure out how to use this method to generate the moving average for a postgresql query.


All times are GMT -5. The time now is 06:51 PM.