Pandas 高效的 VWAP 计算 [英] Pandas Efficient VWAP Calculation

查看:26
本文介绍了Pandas 高效的 VWAP 计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,用它我可以通过三行 Pandas 代码计算出成交量加权平均价格.

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 的 cumsum 的时间复杂度为 N (?).所以我想知道,不是运行两个单独的 cumsum,我们可以一次计算两者 - 沿着 this.很高兴接受这个答案 - 而不是工作代码.

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 行 Pandas、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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆