Advertisement
cdsatrian

Weigthed Moving Average (WMA)

Nov 2nd, 2024 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | Source Code | 0 0
  1. <?php
  2. $periode=isset($_GET['period'])?$_GET['period']:3;
  3. $data=array(
  4.   array('2201','1618'),
  5.   array('2202','1625'),
  6.   array('2203','1643'),
  7.   array('2204','1661'),
  8.   array('2205','1678'),
  9.   array('2206','1740'),
  10.   array('2207','1746'),
  11.   array('2208','1749'),
  12.   array('2209','1757'),
  13.   array('2210','1793'),
  14.   array('2211','1810'),
  15.   array('2212','1865'),
  16. );
  17. ?>
  18. <html>
  19. <head>
  20. <title>Weighted Metode Moving Average</title>
  21. <style>
  22. td,th {padding:5px;border: solid 1px #333;}
  23. </style>
  24. </head>
  25. <body>
  26. <table>
  27. <caption>Weighted Metode Moving Average (period= <select name='period' onChange='ubah(this.value);'>
  28. <?php
  29. for($i=1;$i<10;$i++){
  30. echo "<option value='{$i}'".($i==$periode?' selected':'').">{$i}</option>";
  31. }
  32. ?>
  33. </select>)</caption>
  34. <tr><th>bulan</th><th>penjualan(y)</th><th>prediksi(x)</th>
  35. <th>MAPE |(y-x)/y|</th>
  36. <th>MAD |(y-x)|</th>
  37. <th>MSE (y-x)<sup>2</th>
  38. </tr>
  39. <?php
  40. $prediksi=$mape=$mad=array();
  41. $panjang_data=count($data);
  42. for($i=$periode;$i<$panjang_data;$i++){
  43.   $pembagi=0;
  44.   $jml=0;
  45.   for($j=$i-$periode;$j<$i;$j++){
  46.     $jml+=$data[$j][1]*($j-($i-$periode)+1);
  47.     $pembagi+=($j-($i-$periode)+1);
  48.   }
  49.   $prediksi[$data[$i][0]]=$jml/$pembagi;
  50.   $mape[$data[$i][0]]=abs(($prediksi[$data[$i][0]]-$data[$i][1])/$prediksi[$data[$i][0]]);
  51.   $mad[$data[$i][0]]=abs($prediksi[$data[$i][0]]-$data[$i][1]);
  52.   $mse[$data[$i][0]]=pow($mad[$data[$i][0]],2);
  53. }
  54. foreach($data as $k=>$v){
  55.   echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td>"
  56.       ."<td>".($k<$periode?"-":round($prediksi[$v[0]],4))."</td>"
  57.       ."<td>".($k<$periode?"-":round($mape[$v[0]],4))."</td>"
  58.       ."<td>".($k<$periode?"-":round($mad[$v[0]],4))."</td>"
  59.       ."<td>".($k<$periode?"-":round($mse[$v[0]],4))."</td>"
  60.       ."</tr>";
  61. }
  62. $total_mape=round(array_sum($mape),4);
  63. $total_mad =round(array_sum($mad),4);
  64. $total_mse =round(array_sum($mse),4);
  65.  
  66. $jml_mape=count($mape);
  67. $jml_mad=count($mad);
  68. $jml_mse=count($mse);
  69.  
  70. $hasil_mape=round($total_mape/$jml_mape*100,4);
  71. $hasil_mad =round($total_mad/$jml_mad,4);
  72. $hasil_mse =round($total_mse/$jml_mse,4);
  73.  
  74. echo "<tr><th colspan='3'>Total</th><th>{$total_mape}</th><th>{$total_mad}</th><th>{$total_mse}</th></tr>";
  75. echo "<tr><th colspan='3'>Jumlah Data</th><th>{$jml_mape}</th><th>{$jml_mad}</th><th>{$jml_mse}</th></tr>";
  76. echo "<tr><th colspan='3'>Hasil</th><th>{$hasil_mape} %</th><th>{$hasil_mad}</th><th>{$hasil_mse}</th></tr>";
  77. ?>
  78. </table>
  79. <p>
  80. <b>MAPE</b>  = <i>Mean Absolute Percent Error</i><br />
  81. <b>MAD</b>  = <i>Mean Absolute Deviation</i><br />
  82. <b>MSE</b>  = <i>Mean Squared Error</i>
  83. </p>
  84. <script>
  85. function ubah(nilai){
  86.   location.href='?period='+nilai;
  87. }
  88. </script>
  89. </body>
  90. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement