# Generating numbers that should add up to a fixed value while they follow a known distribution

Suppose a perishable item that is associated with a shelf life $$m\in \mathcal{M} = \{1,\dots,M\}$$. We have a periodic review system with stock level $$S$$, i.e., based on the inventory level of the item, denoted by $$\nu_{t}^{m}$$, we decide over the size of the new order. In other words, at each review period $$t$$, we order $$o_{t} = S - \sum_{m\in \mathcal{M}}\nu_{t-1}^{m}$$ items (stock level minus the remaining inventory of the previous period, that can be of any shelf life), so that the inventory level reaches $$S$$.

The incoming order is not necessarily fresh, and we don't decide over the shelf life when ordering. What we know is that the shelf life of the incoming order follows a known distribution.

My question is: how can I update the inventory level of the items with respect to their shelf life? If $$\epsilon_{t}^{m}$$ is the amount of items in the shipment that are of age $$m$$, we should have $$\sum_{m \in \mathcal{M}} \epsilon_{t}^{m} = o_{t}$$, and the values of $$\epsilon_{t}^{m}$$ should follow the known distribution.

$$$$\nu_{t}^{m} = \nu_{t-1}^{m-1} + \epsilon_{t}^{m}, \quad \forall m \in \mathcal{M}\setminus\{M\}, \forall t \in T\setminus\{1\}.$$$$

So, the question is how to generate the $$\epsilon_{t}^{m}$$ values so that they add up to a known value $$o_{t}$$ (that changes in different time periods) where they are the realizations of a known distribution (I prefer doing it in python-Gurobi)?

AN IDEA:

I have come up with an idea: suppose $$o_{t}$$ can be between 0 to 100. Before starting to solve the model, for each value $$i$$ between 0 to 100, I will generate $$i$$ random numbers based on the known distribution (e.g., a uniform distribution between 20 and 30), and then I count the number of individual values and store them. Then, I will use those counts for the relative $$o_{t}$$.

As an example, suppose $$i=6$$, I will generate a random number based on the distribution for six times. It will give six numbers (shelf lifes), say, 28, 22, 24, 22, 27, 24. Then, I will count those realizations: count(22)=2, count(24)=2, count(27)=1, count(28)=1, and count(20) = count(21) = count(23) = count(25) = count(26) = count(29) = count(30) = 0. Then, if I get $$o_t=6$$, I will refer to these counts and set: $$\epsilon^{22}_t=2$$, $$\epsilon^{24}_t=2$$, and so on (note that I do the process of generating numbers and counting the values for any $$i\in\{0,\dots,100\}$$).

However, the issue with this approach is that for the same value of o in different periods, the individuals are always the same, though it was supposed to be stochastic values.

• Which distribution is the known distribution? If the sum is constrained, the individual $\epsilon_t^m$ values won't have the distribution you specified. So something has to give. One thing you could do, which you will have to judge whether it makes sense, is to generate all of the $\epsilon_t^m$ values per the stated distribution (are they supposed to be mutually independent?), then multiply the individual values by $\frac{\Sigma \epsilon_t^m}{o_{t}}$. However, the individual values will not have the stated distribution. Mar 20 at 23:01
• Thanks @MarkL.Stone. The known distribution is the distribution of the shelf life. For example, I know that the shelf life of the incoming supply follows a uniform distribution between 20 and 30. Then, if I receive an order of size 1, it can be of any age between 20 to 30, but if the size is 5, we may have 2 with age 25, and three with ages 21, 27 and 28 each. And I think the restricting issue for using your suggestion for generating the individual values is that the ages should always be integers. Mar 20 at 23:22
• If everything is required to be integer, then something more might have to give. You could use ,y suggestion, except round values more or less to integers within the bounds, with additional adjustment as needed to get sum exactly met. The bottom line is that you need to decided what the multivariate distribution needs to be, As of now, your specifications are mutually inconsistent, i.e., they can't all hold simultaneously. Mar 20 at 23:59
• @MarkL.Stone I have an idea now: suppose $o_{t}$ can be between 0 to 100. Before starting to solve the model, for each value $i$ between 0 to 100, I will generate $i$ random numbers based on the distribution, and then I count the number of individual values and store them. Then, I will use those counts for the relative $o_{t}$. The issue with this approach is that for the same value of $o$ in different periods, the individuals are always the same, though it was supposed to be stochastic values. What do you reckon Mark? Mar 21 at 0:24
• I don't really understand your proposal. Nevertheless, As I wrote before, you have inconsistent requirements.It is your problem, so you have to decide who to resolve the inconsistencies. Mar 21 at 1:51

I'm still not sure I understand the question, but I'll suggest an answer to what I think is being asked. I'm going to assume that an a priori upper bound $$O$$ exists for $$o_t$$. To keep what follows somewhat compact, I'm going to assume that $$\mathcal{M} = \lbrace 20, 21, 22, 23\rbrace$$ and $$O=6$$.

To start, for each $$t\in T$$ generate a random sample of $$O$$ item ages, arranged as a vector $$v^t$$. Using my example with $$O=6$$, you might get $$v^t=(22, 23, 23, 20, 22, 23)$$ for some particular $$t$$.

Next, compute for each $$n \in \lbrace 1, \dots, O\rbrace$$ a vector $$a_{t,n} = (a_{t,n}^{m_1},\dots,a_{t,n}^{m_N})$$, where $$\mathcal{M}=\lbrace m_1,\dots,m_N\rbrace$$ and $$a_{t,n}^m$$ is the number of items among the first $$n$$ entries of $$v^t$$ having age $$m$$. Continuing the example, we would have $$a_{t,1} = (0, 0, 1, 0)$$ (one item of age 22), $$a_{t,2} = (0, 0, 1, 1)$$ (one item each ages 22 and 23), ..., $$a_{t,6} = (1, 0, 2, 3)$$.

Now in the MIP model we add binary variables $$z_{t,n}$$ for $$t\in T$$ and $$n\in \lbrace 1,\dots,O\rbrace$$, where $$z_{t,n}=1$$ if and only if $$o_t = n$$. We add the following constraints: $$\sum_{n=1}^O z_{t,n} \le 1 \quad \forall t\in T\quad (1)$$ $$o_t = \sum_{n=1}^O n z_{t,n} \quad \forall t\in T \quad (2)$$ $$\epsilon_t^m = \sum_{n=1}^O a_{t,n}^m z_{t,n} \quad \forall m\in \mathcal{M},\ \forall t\in T. \quad (3)$$

Note that (1) allows all the $$z_{t,n}$$ to be 0 at a particular time $$t$$, in which case $$o_t = 0$$ in (2). (1) ensures that at each time $$t$$ a single order size is selected, (2) makes $$o_t$$ the selected size and (3) ensures that the random age counts for that period and order size are used.

• Thanks professor @prubin. I just realized that it's exactly what I want, since you have generated the values $v^t$ for each particular $t$ separately. It's awesome. Mar 26 at 8:50
• Glad to hear it works for you. Mar 27 at 13:44