A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
| import matplotlib.pyplot as plt | |
| from scipy import stats as scs | |
| import statsmodels.tsa.api as smt | |
| import statsmodels.api as sm | |
| def tsplot(y, lags=None, figsize=(10, 8), style='bmh'): | |
| if not isinstance(y, pd.Series): | |
| y = pd.Series(y) | |
| with plt.style.context(style): |
| import numpy as np | |
| import pandas as pd | |
| import xarray as xr | |
| def to_xarray(data: pd.DataFrame) -> xr.Dataset: | |
| r"""Quickly transforms a pandas DataFrame into a xarray Dataset. | |
| For large dataframes, much faster than the existing built-in | |
| "to_xarray" method. |
| add # Julia GARCH(1,1), t-GARCH(1,1) and APARCH(1,1) estimation/simulation package | |
| # Copyright Jon Danielsson and Jia Rong Fan, June 2018 | |
| # Based on Jon Danielsson's work in Financial Risk Forecasting (FRF) | |
| # Estimation/simulation of GARCH(1,1), t-GARCH(1,1) and APARCH(1,1) in Julia. | |
| module FRFGarch | |
| using Optim, Distributions | |
| export GARCHfit, GARCHsim |
| def ef(stocks, log_ret, m3, m4): | |
| np.random.seed(10) | |
| trials = 10000 | |
| all_weights = np.zeros((trials, len(stocks.columns))) | |
| ret_arr = np.zeros(trials) | |
| vol_arr = np.zeros(trials) | |
| coskew_arr = np.zeros(trials) | |
| cokurt_arr = np.zeros(trials) |
| def cokurt(df): | |
| # Number of stocks | |
| num = len(df.columns) | |
| #First Tensor Product Matrix | |
| mtx1 = np.zeros(shape = (len(df), num**2)) | |
| #Second Tensor Product Matrix | |
| mtx2 = np.zeros(shape = (len(df), num**3)) | |
| def coskew(df): | |
| # Number of stocks | |
| num = len(df.columns) | |
| # Two dimionsal matrix for tensor product | |
| mtx = np.zeros(shape = (len(df), num**2)) | |
| v = df.values | |
| means = v.mean(0,keepdims=True) |