forked from joanne-affolter/Financial-big-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library_stylized_facts.py
65 lines (56 loc) · 1.85 KB
/
library_stylized_facts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import pandas as pd
import dask
def logreturns(df, col):
"""Generate logreturns for a given column.
:param df: Dataframe with the historical data.
:type df: DataFrame
:param col: Column name.
:type col: String
:return: Dataframe with the logreturns.
:rtype: DataFrame
"""
df['Logrets'] = np.log(df[col]).diff()
df.dropna(subset="Logrets", inplace=True)
return df
@dask.delayed
def autocorrelation(df, col_name, lag) :
"""Compute autocorrelation for a given lag.
:param df: Dataframe with the historical data.
:type df: DataFrame
:param col_name: Column name.
:type col_name: String
:param lag: Lag.
:type lag: Int
:return: Autocorrelation.
:rtype: List of floats
"""
autocorr = df[col_name].autocorr(lag=lag)
return autocorr
def autocorrelation_dask(df, col_name, lags) :
"""Compute autocorrelation for a list of lags.
:param df: Dataframe with the historical data.
:type df: DataFrame
:param col_name: Column name.
:type col_name: String
:param lags: List of lags.
:type lags: List of ints
:return: Autocorrelation.
:rtype: List of floats
"""
allpromises=[autocorrelation(df, col_name, lag) for lag in lags]
alldata=dask.compute(allpromises)[0]
return alldata
def volatility(data, rolling_window_size):
"""Compute volatility for a given rolling window size.
:param data: Dataframe with the log-returns data.
:type data: DataFrame
:param rolling_window_size: Rolling window size.
:type rolling_window_size: Int
:return: Volatility.
:rtype: DataFrame
"""
df = data.copy()
df['Volatility'] = df["Logrets"].rolling(rolling_window_size).std()
df.dropna(subset="Volatility", inplace=True)
return df[["Volatility"]]