如何在 Atoti 中动态过滤向量?

How can I filter vectors dynamically in Atoti?

我正在尝试实施滚动 window 风险价值聚合,想知道在 Atoti 中是否可行:我的主要问题是我不知道如何按索引过滤每个矢量"observation windows".

在文档中我找到了如何基于静态索引创建子向量,这里是:https://docs.atoti.io/0.3.1/tutorial/07-arrays.html#Sub-arrays - 但我需要根据我正在查看的 "observation window" 更改索引。

我的输入数据如下所示,其中每个向量包含 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 windows,例如:

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 Vector" 是我努力传递索引的地方:

# 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")

您可以使用 parameter hierarchy 创建带索引的日期层次结构,然后在数组上使用该索引获取大小为 250 的 PNL 的子数组。

# Convert the date range to list
dates = list(historical_dates)
# Create the date hierarchy and an index measure
cube.create_parameter_hierarchy("Date", dates, index_measure="Date Index")

cube.query(m["Date Index"], levels=lvl["Date"])

# Take subarray using this index measure
m["VaR Vector"] = m["Vectors.SUM"][m["Date Index"]:m["Date Index"]+250]
# take the 95 percentile of the subarray
m["95 percentile"] = tt.array.percentile(m["VaR Vector"], 0.95, mode="simple")

cube.query( m["VaR Vector"], m["95 percentile"], levels=lvl["Date"])

cube.visualize("percentile per date")

免责声明:我是 atoti 团队的开发人员。