Jump to content

calculation returning wrong value on last array iteration


xenomorf

Recommended Posts

hi all,

i am currently doing some statistic analysis using PHP. Data are retrieved from database. I have a bit of problem with my function

 

 public function upperControlLimit(array $data , $target, $sd, $positiveShiftToDetect = .5)
  {
$k = $positiveShiftToDetect * $sd;

$n = count($data);

for($i=0; $i<$n; $i++)
{
  if(!empty($data[$i-1]))
  {
    $upper[] = max(0,max(0, $data[$i-1] - $target - $k) + $data[$i] - $target - $k);
  }
  else $upper[] = max(0, $data[$i] - $target - $k);
}

return $upper;
}
below are the data i am using to check the function

 

 

Array
(
    [0] => 0.175
    [1] => 0.152
    [2] => 0.15
    [3] => 0.207
    [4] => 0.136
    [5] => 0.212
    [6] => 0.166
    [7] => 0.141
    [8] => 0.157
    [9] => 0.197
    [10] => 0.172
    [11] => 0.183
    [12] => 0.166
    [13] => 0.164
    [14] => 0.141
    [15] => 0.186
    [16] => 0.127
    [17] => 0.149
    [18] => 0.155
    [19] => 0.21
    [20] => 0.197
    [21] => 0.191
    [22] => 0.211
    [23] => 0.158
    [24] => 0.201
)

 

 

other specific data

$target = 0.16
$sd = 0.027925531914894
my function return this value

 

 

Array
(
[0] => 0.0010372340425532
[1] => 0
[2] => 0
[3] => 0.033037234042553
[4] => 0
[5] => 0.038037234042553
[6] => 0.030074468085106
[7] => 0
[8] => 0
[9] => 0.023037234042553
[10] => 0.021074468085106
[11] => 0.0090372340425532
[12] => 0.0010744680851064
[13] => 0
[14] => 0
[15] => 0.012037234042553
[16] => 0
[17] => 0
[18] => 0
[19] => 0.036037234042553
[20] => 0.059074468085106
[21] => 0.040074468085106
[22] => 0.054074468085106
[23] => 0.021074468085106
[24] => 0.027037234042553
)

 

 

the data

 

 

+--------+-------+-------+--------+
| Sample |   X   |  SH   |   SL   |
+--------+-------+-------+--------+
|      1 | 0.175 | 0.001 |      0 |
|      2 | 0.152 |     0 |      0 |
|      3 |  0.15 |     0 |      0 |
|      4 | 0.207 | 0.033 |      0 |
|      5 | 0.136 |     0 |  -0.01 |
|      6 | 0.212 | 0.038 |      0 |
|      7 | 0.166 |  0.03 |      0 |
|      8 | 0.141 |     0 | -0.005 |
|      9 | 0.157 |     0 |      0 |
|     10 | 0.197 | 0.023 |      0 |
|     11 | 0.172 | 0.021 |      0 |
|     12 | 0.183 |  0.03 |      0 |
|     13 | 0.166 | 0.022 |      0 |
|     14 | 0.164 | 0.012 |      0 |
|     15 | 0.141 |     0 | -0.005 |
|     16 | 0.186 | 0.012 |      0 |
|     17 | 0.127 |     0 | -0.019 |
|     18 | 0.149 |     0 | -0.016 |
|     19 | 0.155 |     0 | -0.007 |
|     20 |  0.21 | 0.036 |      0 |
|     21 | 0.197 | 0.059 |      0 |
|     22 | 0.191 | 0.076 |      0 |
|     23 | 0.211 | 0.113 |      0 |
|     24 | 0.158 | 0.097 |      0 |
|     25 | 0.201 | 0.124 |      0 |
+--------+-------+-------+--------+

 

 

i am using the SH column to check my calculation.

 

my problem is, my function returning a wrong calculation starting from array[21] to [24] from my function.

 

the formula and the data are taken from here: https://www.spcforexcel.com/knowledge/variable-control-charts/keeping-process-target-cusum-charts

Link to comment
Share on other sites

seems like i myself made the function much more complicated than it should be

 

i've updated my function

  public function upperControlLimit(array $data , $target, $sd, $positiveShiftToDetect = .5)
  {
    $k = $positiveShiftToDetect * $sd;

    $n = count($data);

    //for SH(1), SH(i-1) is zero (0)
    $upper[] = max(0,$data[0]-$target-$k);

    for($i=1; $i<$n; $i++)
    {
      $upper[] = max(0,$upper[$i-1]+$data[$i]-$target-$k);
    }

    return $upper;
  }
now all calculation are correct!!
Link to comment
Share on other sites

Don't use a for() loop for an array with an arbitrary value to indicate the index - use a foreach() loop.

 

making some other changes this should be even simpler

 

  public function upperControlLimit(array $dataAry, $target, $sd, $positiveShiftToDetect = .5)
  {
    $k = $positiveShiftToDetect * $sd;
 
    $upperAry = array();
    $upperVal = 0;
 
    foreach($dataAry as $data)
    {
      $upperVal = max(0, $upperVal+$data-$target-$k);
      $upperAry[] = $upperVal;
    }
 
    return $upperAry;
  }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.