Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Standard deviation calculation is wrong?

    General Code/Help
    2
    3
    420
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nonamestreet
      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)
      
      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @nonamestreet last edited by

        @nonamestreet said in Standard deviation calculation is wrong?:

        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.

        1 Reply Last reply Reply Quote 0
        • nonamestreet
          nonamestreet last edited by nonamestreet

          @backtrader
          Sorry for the confusion. My understanding is incorrect, thank you for the help!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }