<jupyter_start><jupyter_text>One working engine would be to work with 'fred' and download 'DJIA' (Dow Jones Industrial Average). This is the longest time series available.Others that should work are:* AAPL google* SP500 fred* NASDAQCOM fred* (see more at https://fred.stlouisfed.org/categories/32255)<jupyter_code>df = pddr.DataReader('DJIA', data_source='fred', start=datetime(year=1990, month=1, day=1))
df.head()
df.tail()
f, ax = subplots(figsize=(16, 4))
df.plot(y='DJIA', ax=ax)
df['Return'] = (df['DJIA'] - df.shift(1)['DJIA']) / df.shift(1)['DJIA']
df.head()
f, ax = subplots(figsize=(16, 4))
df.plot(y='Return', ax=ax)
df['LogReturn'] = np.log(1 + df['Return'])
f, ax = subplots(figsize=(16, 4))
df.plot(y='LogReturn', ax=ax)
df.head()<jupyter_output><empty_output><jupyter_text>Financial time series: stylized facts the seemingly random variations of asset prices do share some quite non-trivial statistical properties. Such properties, common across a wide range of instruments, markets and time periods are called _stylized empirical facts._  1. Heavy tails the (unconditional) distribution of returns seems to display a power-law or Pareto-like tail, with a tail index which is finite, higher than two and less than five for most data sets studied. In particular this excludes stable laws with infinite variance and the normal distribution.<jupyter_code>### plot histogram of log-returns
df.LogReturn.hist(bins=100)
### plot normal distribution PDF with parameters estimated from log returns
### plot Student's t (nu=3) with parameters estimated from log returns<jupyter_output><empty_output><jupyter_text>2. Aggregational Gaussianity as one increases the time scale $Δt$ over which returns are calculated, their distribution looks more and more like a normal distribution. In particular, the shape of the distribution is not the same at different time scales.<jupyter_code>### plot histogram of WEEKLY, MONTHLY and YEARLY log-returns
### plot normal distribution PDF with parameters estimated from log returns
### plot Student's t (nu=3, 4, 5) with parameters estimated from log returns<jupyter_output><empty_output><jupyter_text>3. Absence of autocorrelation (linear) autocorrelations of asset returns are often insignificant, except for very small intraday time scales.<jupyter_code>### use statsmodels.graphics.tsaplots.plot_acf to visualise the autocorrelation in log-returns
### use statsmodels.tsa.stattools.q_stat to compute Ljung-Box test
### do the original log-return series exhibit autocorrelations?<jupyter_output><empty_output><jupyter_text>4. Autocorrelation of absolute returns It is a stylized fact that the autocorrelation function (ACF) of absolute log-returns decays as a power-law. However, we will see below that this widespread belief is probably not justified.<jupyter_code>### use statsmodels.graphics.tsaplots.plot_acf to visualise the autocorrelation in ABSOLUTE log-returns
### use statsmodels.tsa.stattools.q_stat to compute Ljung-Box test
### do the ABSOLUTE log-return series exhibit autocorrelations?
### plot log(autocorrelation coefficient) vs. log(lag) and approximate it using a linear fit
### what is the slope of the fit? write down the approximation to the dependency<jupyter_output><empty_output><jupyter_text>It is often concluded from fits like this that the autocorrelation function of volatility decays roughly as $t^{-\alpha}$ with $\alpha \approx 0.3$.  5. Autocorrelation function of volatility is nonlinear Decay of autocorrelations in volatility over time:$$\rho(\Delta) \sim \exp\Big\{-\frac 1 2 \nu^2 \Delta^{2H}\Big\}$$<jupyter_code>### plot fitted dependency over log(lag_k)/log(rho_k) plot
### plot nonlinear dependency with H=0.2, A=0.3<jupyter_output><empty_output><jupyter_text>6. Autocorrelation of squared log-returns. Decay of the volatility ACF Absolute daily log-returns $|r_t|$ are (very) noisy proxies for daily volatilities $\sigma_t$ and squared daily log-returns $r_t^2$ are (very) noisy proxies for daily variances $\sigma^2_t$.  7. Conditional heavy tails even after correcting returns for volatility clustering (e.g. via GARCH-type models), the residual time series still exhibit heavy tails. However, the tails are less heavy than in the unconditional distribution of returns.<jupyter_code>### fit a GARCH(p, q) model to the log-return time series (use arch_model package)
### compute standardized residuals from the model<jupyter_output><empty_output><jupyter_text>8. Gain/loss asymmetry one observes large drawdowns in stock prices and stock index values but not equally large upward movements.<jupyter_code>### pick a certain investment level sigma (say, +2%)
### for that level, compute the estimate of the distribution of first passage times and plot it
### do the same for the negative log-return level (say, -2%)
### compare the plotted dependencies<jupyter_output><empty_output><jupyter_text>9. Volume/volatility correlation trading volume is positively correlated with market volatility.<jupyter_code>### compute pairs of traded volumes and volatility
### estimate correlation (regress linearly one upon the other)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09246642008954643, 'fraction_numerical': 0.010901304263188631, 'mean_word_length': 6.0480109739369, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '586630', 'license': 'MIT', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1423, 'n_words': 594, 'path': '2018-spring/hometasks/homework-4-bonus-seminar.ipynb', 'repo_name': 'kstepanmpmg/ssp-course-fintech', 'size': '5137'}