code: def downside_deviation(rets, mar=0, expanding=0, full=0, ann=0):
      
      below = rets[rets < mar]
      if expanding:
          n = pd.expanding_count(rets)[below.index] if full else pd.expanding_count(below)
          dd = np.sqrt(((below - mar) ** 2).cumsum() / n)
          if ann:
              dd *= np.sqrt(periods_in_year(rets))
          return dd.reindex(rets.index).ffill()
      else:
          n = rets.count() if full else below.count()
          dd = np.sqrt(((below - mar) ** 2).sum() / n)
          if ann:
              dd *= np.sqrt(periods_in_year(rets))
          return dd
docs: Compute the downside deviation for the specifed return series
      :param rets: periodic return series
      :param mar: minimum acceptable rate of return (MAR)
      :param full: If True, use the lenght of full series. If False, use only values below MAR
      :param expanding:
      :param ann: True if result should be annualized