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]
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
Table
so you can write"iQ"->(q=q+1)
where of courseq=Q
is set before the table starts. But for more complicated recurrence you'll probably have to run the recurrence table firstq=RecurrenceTable[...]
then only create the dataset the same way you did but"iQ"->q[[i]]
. $\endgroup$RecurrenceTable
can be easily translated toTable
. 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 yourdat
command to build the dataset. $\endgroup$