- #1

member 428835

Hi PF!

I've posted a bunch lately and you have been SO helpful (seriously, thank you all). Can you corroborate if my python script below matches the math? I'd seriously appreciate it!

The math: $$\max_{x\in [0.7,1)}F(x) : F(x) := \sum_{t = 250}^{750} \log\left( P_{\nu=3} \left( \frac{y_t^2}{\sigma_t^2} \right) \right) : \sigma_t^2 = x\sigma_{t-1}^2 + (1-x)y_{t-1}^2,$$ $$ \sigma_{250}^2 = \sum_{t=1}^{250} y_t^2 / 250$$. Here ##P## is the student-t PDF with 3 degrees of freedom. The data ##y_t## are generated as

$$y_t = N(0,\hat\sigma_t) :\hat\sigma_t^2 = 0.95\hat\sigma_{t-1}^2 + (1-0.95)y_{t-1} $$

where ##y_0 = N(0,0.2)## where ##N(0,\hat\sigma)## is a normally random distributed variable with mean 0 and standard deviation ##\hat\sigma##. Do the maximizing values of this function correspond to the following python code (I minimize the negative of ##F##, which is the same as maximizing F)?

EDIT: question stem and python code accurate as of 8:18 PM EST (sorry, for some reason PF rarely shows me math mode and python mode until I've posted the problem, leading to tons of edits after. Maybe a Chrome issue, or ubuntu?

I've posted a bunch lately and you have been SO helpful (seriously, thank you all). Can you corroborate if my python script below matches the math? I'd seriously appreciate it!

The math: $$\max_{x\in [0.7,1)}F(x) : F(x) := \sum_{t = 250}^{750} \log\left( P_{\nu=3} \left( \frac{y_t^2}{\sigma_t^2} \right) \right) : \sigma_t^2 = x\sigma_{t-1}^2 + (1-x)y_{t-1}^2,$$ $$ \sigma_{250}^2 = \sum_{t=1}^{250} y_t^2 / 250$$. Here ##P## is the student-t PDF with 3 degrees of freedom. The data ##y_t## are generated as

$$y_t = N(0,\hat\sigma_t) :\hat\sigma_t^2 = 0.95\hat\sigma_{t-1}^2 + (1-0.95)y_{t-1} $$

where ##y_0 = N(0,0.2)## where ##N(0,\hat\sigma)## is a normally random distributed variable with mean 0 and standard deviation ##\hat\sigma##. Do the maximizing values of this function correspond to the following python code (I minimize the negative of ##F##, which is the same as maximizing F)?

Python:

```
import numpy as np
from scipy.stats import t
import scipy.optimize as optimize
import matplotlib.pyplot as plt
from scipy.stats import t
# STEP 1: BUILD DATA
def build_data(N=750, lmda=0.95, sig_0=0.2):
# PREALLOCATE AND INITIATE DATA
sig_sq = np.zeros(N)
sig_sq[0] = sig_0**2
y = np.zeros(N)
y[0] = np.random.normal(loc=0, scale=sig_0)
# CONSTRUCT SUCCESSIVE DATA
for i in range(1, N):
sig_sq[i] = lmda*sig_sq[i-1] + (1-lmda)*y[i-1]**2
y[i] = np.random.normal(loc=0, scale=np.sqrt(sig_sq[i]))
plt.plot(y)
plt.plot(sig_sq)
plt.show()
return y, sig_sq
# STEP 2: OPTIMIZE
def arg_max(M = 250):
# GET DATA
data = build_data(750, 0.95, 0.2)[0]
y = data[M:]
# OPTIMIZING FUNCTION
def F(X):
# BUILD pdf argument
sig_sq_pre = np.sum((data[0:M]) ** 2) / M
lmda_star = -np.log(t.pdf(y[0] ** 2 / sig_sq_pre, df=3))
for i in range(len(y)):
sig_sq = X*sig_sq_pre + (1-X)*(y[i-1])**2
sig_sq_pre = sig_sq
lmda_star += -np.log( t.pdf( y[i]**2/sig_sq, df=3 ))
print(X)
return lmda_star
bnds = [(0.7, 0.999)]
IC = np.array(0.75)
result = optimize.minimize(F, method='TNC', bounds=bnds, x0=IC, options={'maxiter': 200})
print(result)
return result.x
if __name__ == '__main__':
arg_max()
```

EDIT: question stem and python code accurate as of 8:18 PM EST (sorry, for some reason PF rarely shows me math mode and python mode until I've posted the problem, leading to tons of edits after. Maybe a Chrome issue, or ubuntu?

Last edited by a moderator: