# How to sum over periods till Period t?

I am trying to set up a constraint that does the following. I a binary variable $$p_{it}$$ that indicates whether a student $$i$$ passed the test in $$t$$. 1 if yes and 0 if no. Now I want to set up a constraint that fills another variable, so to speak. $$n_{it}$$ should indicate how many tests a student $$i$$ has passed at time $$t$$. If for example $$p_{i2}=1$$, $$p_{i3}=1$$ and $$p_{i4}=1$$ then $$n_{i4}=3$$ shall be valid. Thereby $$p_{it}$$ exists only from $$t=2$$. My suggestion is: $$n_{i t}=\sum_{j=t}^{t} p_{i (j)} \quad \forall i \in I, t \in \{2,\ldots,T\}$$

Is that correct?

That is almost correct, but what you proposed is a one-term sum. The summation index $$j$$ should instead start at your first period: $$n_{it}=\sum_{j=2}^t p_{ij}$$

Alternative shorthand: $$n_{it}=\sum_{j\le t} p_{ij}$$

Also, $$p_{i(j)}$$ looks like $$i$$ is a function of $$j$$, so $$p_{ij}$$ is better.

• Thanks. And this would then apply to $\forall i\in I, t\in \{2,\ldots,T\}$ right? Commented Jun 6, 2023 at 13:47
• Yes, that’s right. Commented Jun 6, 2023 at 13:52
• @RobPratt Your second version looks fine, but why does the summation index of the first version start at 2 rather than 1?
– prubin
Commented Jun 6, 2023 at 15:50
• @prubin Apparently $2$ is the first period, the smallest element of $T$. Commented Jun 6, 2023 at 16:17
• @prubin Yes, $2 \le j \le t$, but I was going for even shorter shorthand. :) Commented Jun 6, 2023 at 17:48