2

So I'm trying to figure out a good way of vectorizing a calculation and I'm a bit stuck.

| A | B (Calculation)      | B (Value) |
|---|----------------------|-----------|
| 1 |                      |           |
| 2 |                      |           |
| 3 |                      |           |
| 4 | =SUM(A1:A4)/4        | 2.5       |
| 5 | =(1/4)*A5 + (3/4)*B4 | 3.125     |
| 6 | =(1/4)*A6 + (3/4)*B5 | 3.84375   |
| 7 | =(1/4)*A7 + (3/4)*B6 | 4.6328125 |

I'm basically trying to replicate Wilder's Average True Range (without using TA-Lib). In the case of my simplified example, column A is the precomputed True Range.

Average True Range Equation

Any ideas of how to do this without looping? Breaking down the equation it's effectively a weighted cumulative sum... but it's definitely not something that the existing pandas cumsum allows out of the box.

DustinTheDev
  • 173
  • 1
  • 6
  • 1
    Is `n` a constant? I see a `t` and an `n`; the `t` appears to be an index, but I don't see where `n` is coming from. – user2357112 Mar 23 '17 at 17:52
  • 1
    If `n` is a constant, this appears to be a fairly standard exponential moving average, and Googling `numpy exponential moving average` immediately turns up ways to do it. – user2357112 Mar 23 '17 at 17:57
  • Here's a [Q&A](http://stackoverflow.com/questions/42869495/numpy-version-of-exponential-weighted-moving-average-equivalent-to-pandas-ewm) on efficient implementation on EWM. – Divakar Mar 23 '17 at 20:07

1 Answers1

5

This is indeed an ewm problem. The issue is that the first 4 rows are crammed together into a single row... then ewm takes over

a = df.A.values
d1 = pd.DataFrame(dict(A=np.append(a[:4].mean(), a[4:])), df.index[3:])
d1.ewm(adjust=False, alpha=.25).mean()

          A
3  2.500000
4  3.125000
5  3.843750
6  4.632812
piRSquared
  • 285,575
  • 57
  • 475
  • 624