Standard deviation calculation is wrong?
nonamestreet last edited by
Thank you for providing this great framework! I have used it for one year now.
I think the standard deviation code(bt.indicators.StandardDeviation) is incorrect.
For example, we give the function an input array and a mean array:
Case A: [1,2,3] with mean [3,2,1]
Case B: [3,2,1] with mean [3,2,1].
Both case A and B would have the same result, but clearly case B should be zero. I cannot create issues on github so I have to put it here. I guess it might has huge impact on people's backtests.
class StandardDeviation(Indicator): ''' Calculates the standard deviation of the passed data for a given period Note: - If 2 datas are provided as parameters, the 2nd is considered to be the mean of the first - ``safepow`` (default: False) If this parameter is True, the standard deviation will be calculated as pow(abs(meansq - sqmean), 0.5) to safe guard for possible negative results of ``meansq - sqmean`` caused by the floating point representation. Formula: - meansquared = SimpleMovingAverage(pow(data, 2), period) - squaredmean = pow(SimpleMovingAverage(data, period), 2) - stddev = pow(meansquared - squaredmean, 0.5) # square root See: - http://en.wikipedia.org/wiki/Standard_deviation ''' alias = ('StdDev',) lines = ('stddev',) params = (('period', 20), ('movav', MovAv.Simple), ('safepow', True),) def __init__(self): if len(self.datas) > 1: mean = self.data1 else: mean = self.p.movav(self.data, period=self.p.period) meansq = self.p.movav(pow(self.data, 2), period=self.p.period) sqmean = pow(mean, 2) if self.p.safepow: self.lines.stddev = pow(abs(meansq - sqmean), 0.5) else: self.lines.stddev = pow(meansq - sqmean, 0.5)
I cannot create issues on github
Issue on GitHub were disabled exactly because of reports like this one.
Understanding that your intentions are well meant, do you have something to sustain your claims?
Sorry but when I read that you "give the function" ... makes me wonder to which "function" are you actually referring? The backtrader code shown defines no function.
nonamestreet last edited by nonamestreet
Sorry for the confusion. My understanding is incorrect, thank you for the help!