[英]MySQL cumulative product group by
我一直在使用 WRDS/CRSP 数据集(由 UPenn 维护的用于学术研究的股票价格数据库)。 我一直在用 Python 下载数据并将其插入到我的本地 MySQL 数据库中。
数据如下所示,主键为 (quote_date, security_id):
quote_date security_id tr accum_index
10-Jan-86 10002 null 1000
13-Jan-86 10002 -0.026595745 973.4042548
14-Jan-86 10002 0.005464481 978.7234036
15-Jan-86 10002 -0.016304348 962.7659569
16-Jan-86 10002 0 962.7659569
17-Jan-86 10002 0 962.7659569
20-Jan-86 10002 0 962.7659569
21-Jan-86 10002 0.005524862 968.0851061
22-Jan-86 10002 -0.005494506 962.765957
23-Jan-86 10002 0 962.765957
24-Jan-86 10002 -0.005524862 957.4468078
27-Jan-86 10002 0.005555556 962.7659569
28-Jan-86 10002 0 962.7659569
29-Jan-86 10002 0 962.7659569
30-Jan-86 10002 0 962.7659569
31-Jan-86 10002 0.027624309 989.3617013
3-Feb-86 10002 0.016129032 1005.319148
4-Feb-86 10002 0.042328041 1047.872338
5-Feb-86 10002 0.04568528 1095.744679
我需要计算 accum_index 列,它基本上是股票总回报的指数,计算如下:
accum_index_t = accum_index_{t-1} * (1 + tr_t)
该表有 80m 行。 我已经写了一些代码来迭代每个 security_id 并计算累积乘积,如下所示:
select @sid := min(security_id)
from stock_prices;
create temporary table prices (
quote_date datetime,
security_id int,
tr double null,
accum_index double null,
PRIMARY KEY (quote_date, security_id)
);
while @sid is not null
do
select 'security_id', @sid;
select @accum := null;
insert into prices
select quote_date, security_id, tr, accum_index
from stock_prices
where security_id = @sid
order by quote_date asc;
update prices
set accum_index = (@accum := ifnull(@accum * (1 + tr), 1000.0));
update stock_prices p use index(PRIMARY), prices a use index(PRIMARY)
set p.accum_index = a.accum_index
where p.security_id = a.security_id
and p.quote_date = a.quote_date;
select @sid := min(security_id)
from stock_prices
where security_id > @sid;
delete from prices;
end while;
drop table prices;
但这太慢了,我的笔记本电脑上每个安全性大约需要一分钟,计算这个系列需要数年时间。 有没有办法将其矢量化?
干杯,史蒂夫
如果您使用的是 MySQL 8,则可以使用窗口函数来创建累积乘积。 不幸的是,在我知道的任何 SQL 数据库中都没有PROD()
聚合/窗口函数,但您可以使用EXP(SUM(LOG(factor)))
模拟它:
SELECT
quote_date,
security_id,
tr,
1000 * (EXP(SUM(LOG(1 + COALESCE(tr, 1)))
OVER (PARTITION BY security_id ORDER BY quote_date)) - 1)
AS accum_index
FROM stock_prices
如果您使用的是 MySQL 5,则可以模拟此函数将 current 与最后一个 tr 逐行相乘。 之后我们取最后一行的累计值。
tr 是百分比值,现在? 所以让我们为每个 tr 加 1。
第一个存储的值将是中性 1。
试试这个:
SET @variation = 1;
SET @row_number = 0;
SELECT accumulateTr
FROM
(SELECT
@row_number := (@row_number + 1) AS rowNumber,
@variation := (1 + variation) * @variation AS accumulateTr
FROM
prices) accumulatedTrs
ORDER BY rowNumber DESC
LIMIT 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.