LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-14-2009, 06:00 AM   #1
pcock
LQ Newbie
 
Registered: Sep 2004
Location: Sydney, Australia
Distribution: Archlinux
Posts: 16

Rep: Reputation: 0
Question [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

Last edited by pcock; 06-15-2009 at 08:49 PM. Reason: added [solved]
 
Old 06-14-2009, 08:42 AM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,165

Rep: Reputation: 506Reputation: 506Reputation: 506Reputation: 506Reputation: 506Reputation: 506
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 View Post
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 View Post
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
 
Old 06-14-2009, 04:02 PM   #3
pcock
LQ Newbie
 
Registered: Sep 2004
Location: Sydney, Australia
Distribution: Archlinux
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlinkels View Post
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>
 
Old 06-14-2009, 09:19 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,165

Rep: Reputation: 506Reputation: 506Reputation: 506Reputation: 506Reputation: 506Reputation: 506
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
 
Old 06-15-2009, 08:51 PM   #5
pcock
LQ Newbie
 
Registered: Sep 2004
Location: Sydney, Australia
Distribution: Archlinux
Posts: 16

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
array, average, moving, php


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP OOP moving arrays around functions? eco Programming 3 01-19-2009 08:07 AM
moving apache, php and mysql and webmin abc27 Linux - Server 1 05-04-2008 01:14 PM
question before moving from windows2003 to Linux+apache+php+iodbc+FreeTDS sugar2 Linux - Newbie 6 06-30-2006 12:00 PM
Programming C++ moving average overule Programming 3 08-09-2005 10:46 AM
Apache Qs: Moving Document Root and PHP with Apache2 larsenmtl Linux - Networking 3 09-29-2004 08:31 AM


All times are GMT -5. The time now is 03:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration