Thank you for offering a reproducible
reprex,
I appreciate that.
problem definition
calculate the quarterly average ... in a rolling fashion.
Months in the Gregorian calendar do not, alas, all have 30 days.
But we won't soon be going back to the five intercalary days of the
Revolutionary
calendar.
It isn't clear from the review context if there is some regulatory
requirement you are trying to conform to.
You may find it more convenient to rephrase "quarterly average"
as "90-day average".
That would let you use the numpy concept of a
sliding window,
or the excellent support for rolling windows offered by
pandas
or polars.
caching
Computing (df.index.quarter == q)
hundreds and hundreds of times
is just silly, given that the current t
is in
the same old q
> 98% of the time.
Numpy must examine more than 1400 rows each time.
Better to cache the result, and only recompute when t
advances
into a new quarter.
But that brings us to the sticking point of the final quarter.
If there's just a handful of days, do we really want to
compute a "smoothed" mean over just a tiny sample?
stability
The OP code proposes μ = 550 for the random variable Volume
,
which is a stationary process.
But imagine that the Generating Process was four light trading days
centered on 450, followed by four heavy trading days with mean 650,
and then the cycle repeats.
In the opening days of a new quarter, do we really want to
assert the long-term mean has moved below 500, or above 600?
We average across multiple days in order to reduce the variance --
σ for a 3-day sample will be greater than σ for a 90-day sample.
The OP code essentially offers large σ in the opening days of the current
quarter, with σ dwindling as we move further into the quarter.
specification
If there is a written Requirements document, perhaps part of
GAAP rules,
then mention that in a """docstring""" or comment.
Absent such constraints, I advise you to define
a "90-day window" requirement, and implement it
with support from the libraries I mentioned above.
df.index < df.index[t]
is valid in context; I would expect<=
. \$\endgroup\$<
is correct for the problem at hand. I am not sure about the "initial condition". If it refers to200
, it was just for sake of testing at some point, apologies. It should work also before that point. \$\endgroup\$