Variance Calculator Function - why mean of SMA-1 is taken?

Course Name: Trading using Options Sentiment Indicators, Section No: 2, Unit No: 9, Unit type: WaterMarkVideo

def variance_calculator(series,series_average,win_len):
sma = win_len
temp = series.subtract(series_average) # a-b
temp2 = temp.apply(lambda x: x**2) #(a-b)^2
temp3 = temp2.rolling(sma-1).mean() # summation[(a-b)^2 ]/ (sma -1)
sigma = temp3.apply(lambda x : math.sqrt(x)) #sigma is the standard deviation
return sigma =========================================================== I want to intention behind using temp2.rolling(sma-1).mean() as this is not equivalent of what is in the comment against this line of code # summation[(a-b)^2 ]/ (sma -1). 'temp2.rolling(sma-1).mean() ’ will not calculate sample Standard deviation and is not equivalent of summation[(a-b)^2 ]/ (sma -1), if intention was to calculate sample standard deviation - this can be be done - sigma = series.rolling(win_len).std(ddof=1) ? Can you please advise , if I am thinking along on correct lines ?

or keeping with same code structure we can probably do following -

# Compute rolling sum over full window (sma)
temp3 = temp2.rolling(win_len).sum()

# Apply proper Bessel’s correction: divide by (win_len-1)
temp3 = temp3 / (win_len - 1)

# Take the square root to get standard deviation
sigma = temp3.apply(lambda x: math.sqrt(x))

Hi Rohan,

Thanks for pointing this out. You may have a valid point, and we are currently evaluating it.
If you have any other concerns, feel free to reach out us via the community.

Best,
Ajay

Thanks so much for your response. Please update me once you guys have reviewed. Thanks.

Hi Rohan,

Thank you for your input.
The intention is to calculate the sample standard deviation, which can be achieved through the following methods:

  • temp3 = temp2.rolling(sma).mean() / (sma - 1) [implemented].
  • sigma = series.rolling(win_len).std(ddof=1)

I’ve updated the content accordingly.
If you have any further concerns, feel free to reach out via the community.

Regards,
Ajay

Thanks Ajay for udpate.