I just learned that I can create a Dataset by combining Dataset and Table functions. For example:

d = RandomVariate[DemandProcess, nperiods];
dat = Table[<|
  "d" -> d[[i]],
  "underQ" -> Boole[d[[i]] > Q] (d[[i]] - Q),
  "overQ" -> Boole[d[[i]] <= Q] (-d[[i]] + Q),
  "under$" -> Boole[d[[i]] > Q] (d[[i]] - Q) cu,
  "over$" -> Boole[d[[i]] <= Q] (-d[[i]] + Q) co,
  "iQ" -> If[i == 1, Q, 0]
  |>, {i, 1, nperiods}];
dat = Dataset[dat]

result of dataset on table

For anyone interested in the background of this problem, this is a newsvendor model where Q is quantity of inventory to order in every period, d is a list of demand quantities per period, underQ is shortage of inventory, under\$ is cost of the shortage, cu is the cost of unit shortage; overQ, over\$, co have similar meanings; rows are periods and inventory doesn't carry over.

Now, where I'm struggling is the iQ column: I want its consecutive values to depend on preceding values. I could achieve that, for example, by doing this:

RecurrenceTable[{iQ[t + 1] == 
  iQ[t] + Boole[iQ[t] < d[[t]]] cu - Boole[iQ[t] > d[[t]]] co, 
  iQ[1] == iQstart}, iQ, {t, 1, nperiods}]

I don't think that this is achievable by using Table function and I don't know how to combine Dataset function with the above RecurrenceTable code.

Please help and advise on how to keep the solution compact and neat. Thank you

  • $\begingroup$ For such a simple recurrence you gave you can directly do the same with Table so you can write "iQ"->(q=q+1) where of course q=Q is set before the table starts. But for more complicated recurrence you'll probably have to run the recurrence table first q=RecurrenceTable[...] then only create the dataset the same way you did but "iQ"->q[[i]]. $\endgroup$
    – SquareOne
    Sep 30, 2017 at 9:12
  • $\begingroup$ Thanks @SquareOne but my RecurrenceTable code shows a code that I made up to save me some typing, not the actual code. I should have realized that. I'll edit my question with the actual code. $\endgroup$ Sep 30, 2017 at 12:28
  • $\begingroup$ Your actual code for the RecurrenceTable can be easily translated to Table. Run this for example:q=iQstart ; Table[q=q+Boole[q<d[[t]]] cu - Boole[q>d[[t]]] co, {t,1,nperiods}]. Then, you can directly use it inside your dat command to build the dataset. $\endgroup$
    – SquareOne
    Sep 30, 2017 at 20:47

1 Answer 1


Just happened across this. If I understand the situation, you're looking for an accumulation of iQ (over time) as you proceed down the dataset. I do loads of this type of thing with time series. I've simplified from your example, hope this helps:

  "d" -> Query[All, "d"],
  "d(rand)" -> Query[All, "d(rand)"], 
  "d(accum)" -> Query[Accumulate, "d(rand)"]
  • $\begingroup$ Please show that this addresses the question by including the result of your code. $\endgroup$
    – bbgodfrey
    Feb 8, 2018 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.