Home > Mobile >  Python: Calculate confidence interval on a rolling basis
Python: Calculate confidence interval on a rolling basis

Time:12-21

I have the following dataframe:

df = pd.DataFrame({
'a': [0.1, 0.5, 0.1, 0.3],
'b': [0.2, 0.4, 0.2, 0.2],
'c': [0.3, 0.2, 0.4, 0.1],
'd': [0.1, 0.1, 0.1, 0.7],
'e': [0.2, 0.1, 0.3, 0.4],
'f': [0.7, 0.1, 0.1, 0.1]}
)

I want to get the mean of each column on a rolling basis (let's say rolling(1).mean()) and then get the 95% confidence interval for each entry CI = x - z*s/sqrt(n), where x is the rolling average, z is confidence level value, s is the rolling standard deviation (let's say rolling(1).stdev), and n is the number of entries in the column.

Can this be done pythonically without loops?

Thank you.

CodePudding user response:

IIUC, try:

WINDOW = 2
lower_bound = df.rolling(WINDOW).mean() - 1.96*df.rolling(WINDOW).std()
upper_bound = df.rolling(WINDOW).mean()   1.96*df.rolling(WINDOW).std()
output = lower_bound.join(upper_bound, lsuffix="_lower", rsuffix="_upper").sort_index(axis=1)

>>> output
    a_lower   a_upper   b_lower  ...   e_upper   f_lower   f_upper
0       NaN       NaN       NaN  ...       NaN       NaN       NaN
1 -0.254372  0.854372  0.022814  ...  0.288593 -0.431558  1.231558
2 -0.254372  0.854372  0.022814  ...  0.477186  0.100000  0.100000
3 -0.077186  0.477186  0.200000  ...  0.488593  0.100000  0.100000

[4 rows x 12 columns]
  • Related