我正在尝试实现滚动窗口风险值聚合,并想知道在 Atoti 中是否可能:我的主要问题是我不知道如何按索引为每个“观察窗口”过滤向量。
- 我的数据是 10 年的历史模拟 PL
- 我想计算每个观察窗口的百分位数,每个观察是 250 个连续的历史天数。
在文档中,我找到了如何基于静态索引创建子向量,这里:https ://docs.atoti.io/0.3.1/tutorial/07-arrays.html#Sub-arrays - 但我需要索引根据我正在查看的“观察窗口”进行更改。
我的输入数据如下所示,其中每个向量包含 2500 个值,我们需要计算重叠子向量的百分位数,每个子向量具有 250 个连续值:
Book Vectors
A [877.30;137.33;-1406.62;-156.48;-915.56;1702.2...
B [2182.98;394.09;-845.23;-422.25;-2262.86;-2010...
C [9.94;972.31;1266.79;178.33;-102.00;508.13;-23...
我希望能够为每个观察窗口显示 VaR,例如:
WindowIndex VaR
0 -98.8
1 -1000.9
2 -500.88
... ...
2250 -088.7
或更好:
WindowStartDate VaR
2011-05-17 -98.8
2011-05-18 -1000.9
2011-05-19 -500.88
... ...
2019-12-31 -088.7
此代码重现了用例 - “VaR 向量”是我努力传递索引的地方:
# sample data
import pandas as pd
import random
history_size = 2500 # 10 years of data
var_window_length = 250
df =pd.DataFrame(data = {
'Book': ['A', 'B', 'C'],
'Vectors': [[';'.join(["{0:.2f}".format(random.gauss(0,1000)) for x in range(history_size)])] for y in range(3)]
})
# atoti part
import atoti as tt
session = tt.create_session()
store = session.read_pandas(
df, keys=["Book"], store_name="Store With Arrays", array_sep=";"
)
cube = session.create_cube(store, "Cube")
lvl = cube.levels
m = cube.measures
# historical dates:
historical_dates = pd.bdate_range(periods = history_size - var_window_length + 1, end = pd.Timestamp('2019-12-31'), freq='B')
historical_dates
# This measure aggreates vectors across positions:
cube.query(m["Vectors.SUM"])
# This measure will display vectors for a given window - but how can I pass the right indexes for each observation window?
m["VaR Vector"] = m["Vectors.SUM"][???]
# This measure will compute VaR from each subvector:
m["95 percentile"] = tt.array.percentile(m["VaR Vector"], 0.05, "simple")