@backtrader
Sorry for the confusion. My understanding is incorrect, thank you for the help!
nonamestreet
@nonamestreet
0
Reputation
2
Posts
51
Profile views
0
Followers
0
Following
Posts made by nonamestreet

RE: Standard deviation calculation is wrong?

Standard deviation calculation is wrong?
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)