pandas 高效VWAP计算 [英] Pandas Efficient VWAP Calculation
问题描述
我有下面的代码,通过它我可以通过三行熊猫代码来计算交易量加权平均价格.
I have the below code, using which I can calculate the volume-weighted average price by three lines of Pandas code.
import numpy as np
import pandas as pd
from pandas.io.data import DataReader
import datetime as dt
df = DataReader(['AAPL'], 'yahoo', dt.datetime(2013, 12, 30), dt.datetime(2014, 12, 30))
df['Cum_Vol'] = df['Volume'].cumsum()
df['Cum_Vol_Price'] = (df['Volume'] * (df['High'] + df['Low'] + df['Close'] ) /3).cumsum()
df['VWAP'] = df['Cum_Vol_Price'] / df['Cum_Vol']
我正在尝试找到一种无需使用 cumsum()
作为练习的方式对此进行编码的方法.我正在尝试找到一种解决方案,它可以一次性给出 VWAP
列.我已经尝试过使用 .apply()
在下面的行.逻辑在那里,但是问题是我无法在第n行中存储值以便在第(n + 1)行中使用.您如何在 pandas
中解决此问题-仅使用外部连音符或字典临时存储累积值?
I am trying to find a way to code this without using cumsum()
as an exercise. I am trying to find a solution which gives the VWAP
column in one pass. I have tried the below line, using .apply()
. The logic is there, but the issue is I am not able to store values in row n in order to use in row (n+1). How do you approach this in pandas
- just use an external tuplet or dictionary for temporary storage of cumulative values?
df['Cum_Vol']= np.nan
df['Cum_Vol_Price'] = np.nan
# calculate running cumulatives by apply - assume df row index is 0 to N
df['Cum_Vol'] = df.apply(lambda x: df.iloc[x.name-1]['Cum_Vol'] + x['Volume'] if int(x.name)>0 else x['Volume'], axis=1)
是否存在针对上述问题的一站式解决方案?
Is there a one-pass solution to the above problem?
我的主要动机是了解幕后发生的事情.因此,这主要是出于锻炼而非任何正当理由.我相信大小为N的序列上的每个累加都具有时间复杂度N(?).因此,我想知道,我们是否可以一次计算两次而不是运行两个单独的累加数-沿此的行进行计算一个>.很高兴接受这个答案-而不是有效的代码.
My main motivation is to understand what is happening under the hood. So, it is mainly for exercise than any valid reason. I believe each cumsum on a Series of size N has time complexity N (?). So I was wondering, instead of running two separate cumsum's, can we calculate both in one pass - along the lines of this. Very happy to accept an answer to this - rather than working code.
推荐答案
单行通行与单行通行在语义上有所不同.这样做有何不同:您可以用1行熊猫,1行numpy或多行numba来做到这一点.
Getting into one pass vs one line starts to get a little semantical. How about this for a distinction: you can do it with 1 line of pandas, 1 line of numpy, or several lines of numba.
from numba import jit
df=pd.DataFrame( np.random.randn(10000,3), columns=['v','h','l'] )
df['vwap_pandas'] = (df.v*(df.h+df.l)/2).cumsum() / df.v.cumsum()
@jit
def vwap():
tmp1 = np.zeros_like(v)
tmp2 = np.zeros_like(v)
for i in range(0,len(v)):
tmp1[i] = tmp1[i-1] + v[i] * ( h[i] + l[i] ) / 2.
tmp2[i] = tmp2[i-1] + v[i]
return tmp1 / tmp2
v = df.v.values
h = df.h.values
l = df.l.values
df['vwap_numpy'] = np.cumsum(v*(h+l)/2) / np.cumsum(v)
df['vwap_numba'] = vwap()
时间:
%timeit (df.v*(df.h+df.l)/2).cumsum() / df.v.cumsum() # pandas
1000 loops, best of 3: 829 µs per loop
%timeit np.cumsum(v*(h+l)/2) / np.cumsum(v) # numpy
10000 loops, best of 3: 165 µs per loop
%timeit vwap() # numba
10000 loops, best of 3: 87.4 µs per loop
这篇关于 pandas 高效VWAP计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!