Question

I am trying to graph the delivery distribution of a known quantity in a decreasing fashion over X number of days. I need help with a formula which I can implement in php.

The constants would be 10,000 units, and a start value of 300% of an averaged distribution (sorry for the poor terms).

For example:

10,000 units will be distributed over 10 days. The first day would had have a delivery of 300% higher than a regular average, so 3,000. The number will fall over the next 9 days until all have been delivered.

I can fudge the above example via excel by figuring out a coefficient to multiple each delivery by to reduce it (this example is .71).

I will be implementing this in php. The days can range from anywhere from as small as 3 days to 365 days.

So ideally the solution would allow me to do something along the following:

$units = array();
$startValue = (10000 / $daysToDeliver) * 3;
for ($x= 0, $x < $daysToDeliver, $x++) {
   //add the next deliver quantity onto the array
   $units[] = awesomefunction($lastDeliverAmount, $daysLeftToDeliver);  // guessing here
}

I know I'm over simplifying the inputs on the function, just trying to give a rough idea.

Thank you for your time and consideration!

Was it helpful?

Solution

As Manny Ramirez mentions, this is like an exponential decay function; except it's not continuous, but discrete. You basically want to produce:

seq(3a/n*(1-r)^(i-1)) for i from 1 to n

where in your case

a = 10000
n = 10
r = necessary value such that sum(seq(...)) == a == 10000

The challenge is finding r. I used a = ∑3a/n*(1-r)^(i-1) for i from 1 to n. Doing some algebraic manipulation, I simplified this as far as:

3(1-r)^n+r*n-3=0

But, honestly, without a computer algebra system, there's no easy way to solve that. Frankly, as much as I like PHP, it sucks at this kind of thing. There may be a module in Python to do this for you. That said...

Once you've used one of these methods (or another) to find r, then you need to build the array:

function getUnits ($total, $days, $rate) {
  $start = $total * 3 / $days;
  $units = array();
  for ($i = 1; $i <= $days; $i++) {
    $units[] = $start * pow((1 - $rate), $i - 1);
  }
  return $units;
}

//try with the numbers we have here
$units = getUnits(10000, 10, 0.290271556501);
print_r($units);
echo array_sum($units);

This gives

>> Array ( [0] => 3000 [1] => 2129.185330497 [2] => 1511.1433905345 [3] => 1072.5014464679 [4] => 761.18478225207 [5] => 540.23449072289 [6] => 383.41978422523 [7] => 272.1239266649 [8] => 193.13409091071 [9] => 137.07275772865 )
>> 10000.000000004

OTHER TIPS

You are dealing with a growth and decay problem. The decay is given by this formula: y=a(1-r)^x. a is the initial amount, y is the amount shipped each day, and x is the number of days, which in your case is 10-1, since you are forcing the 1st day amount to be a/x*3. Then it is a simple matter of calculating the rate r and constructing a simple loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top