You can use np.lib.stride_tricks.as_strided
and np.std
with ddof=1
:
>>> np.std(
np.lib.stride_tricks.as_strided(
PC_list,
shape=(PC_list.shape[0] - number + 1, number),
strides=PC_list.strides*2
),
axis=-1,
ddof=1
)
array([25.35313557, 11.6209317 , 16.32415133, 15.46019574, 15.29513506,
14.02947067, 14.68620846, 17.04664993, 16.38348865, 12.9925946 ,
9.58525968, 5.32623099, 10.61466493, 23.71209646, 27.85489139,
23.31091745, 14.78211757, 12.11214834, 17.90301391, 15.42895731,
11.7602241 , 9.27171536, 12.57714149, 17.25865608, 15.2717403 ,
9.02825105])
Otherwise you can move use pandas.Series.rolling.std
, pandas.Series.dropna
then pandas.Series.to_numpy
:
>>> pd.Series(PC_list).rolling(number).std().dropna().to_numpy()
array([25.35313557, 11.6209317 , 16.32415133, 15.46019574, 15.29513506,
14.02947067, 14.68620846, 17.04664993, 16.38348865, 12.9925946 ,
9.58525968, 5.32623099, 10.61466493, 23.71209646, 27.85489139,
23.31091745, 14.78211757, 12.11214834, 17.90301391, 15.42895731,
11.7602241 , 9.27171536, 12.57714149, 17.25865608, 15.2717403 ,
9.02825105])
EXPLANATION:
np.lib.stride_tricks.as_strided
is used to reshape the array in a special way, that resembles rolling:
>>> np.lib.stride_tricks.as_strided(
PC_list,
shape=(PC_list.shape[0] - number + 1, number),
strides=PC_list.strides*2
)
array([[457.334015, 424.440002, 394.79599 , 408.903992, 398.821014], #index: 0,1,2,3,4
[424.440002, 394.79599 , 408.903992, 398.821014, 402.152008], #index: 1,2,3,4,5
[394.79599 , 408.903992, 398.821014, 402.152008, 435.790985], #index: 2,3,4,5,6
[408.903992, 398.821014, 402.152008, 435.790985, 423.204987], # ... and so on
[398.821014, 402.152008, 435.790985, 423.204987, 411.574005],
[402.152008, 435.790985, 423.204987, 411.574005, 404.424988],
[435.790985, 423.204987, 411.574005, 404.424988, 399.519989],
[423.204987, 411.574005, 404.424988, 399.519989, 377.181 ],
[411.574005, 404.424988, 399.519989, 377.181 , 375.46701 ],
[404.424988, 399.519989, 377.181 , 375.46701 , 386.944 ],
[399.519989, 377.181 , 375.46701 , 386.944 , 383.61499 ],
[377.181 , 375.46701 , 386.944 , 383.61499 , 375.071991],
[375.46701 , 386.944 , 383.61499 , 375.071991, 359.511993],
[386.944 , 383.61499 , 375.071991, 359.511993, 328.865997],
[383.61499 , 375.071991, 359.511993, 328.865997, 320.51001 ],
[375.071991, 359.511993, 328.865997, 320.51001 , 330.07901 ],
[359.511993, 328.865997, 320.51001 , 330.07901 , 336.187012],
[328.865997, 320.51001 , 330.07901 , 336.187012, 352.940002],
[320.51001 , 330.07901 , 336.187012, 352.940002, 365.026001],
[330.07901 , 336.187012, 352.940002, 365.026001, 361.562012],
[336.187012, 352.940002, 365.026001, 361.562012, 362.299011],
[352.940002, 365.026001, 361.562012, 362.299011, 378.549011],
[365.026001, 361.562012, 362.299011, 378.549011, 390.414001],
[361.562012, 362.299011, 378.549011, 390.414001, 400.869995],
[362.299011, 378.549011, 390.414001, 400.869995, 394.77301 ],
[378.549011, 390.414001, 400.869995, 394.77301 , 382.556 ]])
Now if we take the std
of the above array across the last axis, to obtain the rolling std
. By default numpy
uses ddof=0
, i.e. Delta Degrees of Freedom = 0, which means for number
amount of samples, the divisor will be equal to number - 0
. Now as you want number - 1
, you need ddof=1
.