-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_loss_function.py
148 lines (111 loc) · 5.71 KB
/
get_loss_function.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import time
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import torch
import torch.optim as optim
import pandas as pd
import scipy
from G_learning_portfolio_opt import G_learning_portfolio_opt
def get_loss(trajs,
num_steps,
benchmark_portf,
gamma,
num_risky_assets,
riskfree_rate,
expected_risky_returns,
Sigma_r,
x_vals_init,
max_iter_RL,
reward_params,
beta,
num_trajs,
grad=False,
eps=1e-7):
error_tol= 1.e-12
max_iter_RL = 200
num_assets= num_risky_assets + 1
data_xvals = torch.zeros(num_trajs, num_steps, num_assets, dtype=torch.float64, requires_grad=False)
data_uvals = torch.zeros(num_trajs, num_steps, num_assets, dtype=torch.float64, requires_grad=False)
for n in range(num_trajs):
for t in range(num_steps):
data_xvals[n,t,:] = torch.tensor(trajs[n][t][0],dtype=torch.float64)
data_uvals[n,t,:] = torch.tensor(trajs[n][t][1],dtype=torch.float64)
# allocate memory for tensors that wil be used to compute the forward pass
realized_rewards = torch.zeros(num_trajs, num_steps, dtype=torch.float64, requires_grad=False)
realized_cum_rewards = torch.zeros(num_trajs, dtype=torch.float64, requires_grad=False)
realized_G_fun = torch.zeros(num_trajs, num_steps, dtype=torch.float64, requires_grad=False)
realized_F_fun = torch.zeros(num_trajs, num_steps, dtype=torch.float64, requires_grad=False)
realized_G_fun_cum = torch.zeros(num_trajs, dtype=torch.float64, requires_grad=False)
realized_F_fun_cum = torch.zeros(num_trajs, dtype=torch.float64, requires_grad=False)
reward_params_dict={}
loss_dict={}
loss_dict[-1]=np.array([0]*len(reward_params), dtype='float64') # perturb up
loss_dict[1]=np.array([0]*len(reward_params), dtype='float64') # perturb down
loss_grad = np.array([0]*len(reward_params), dtype='float64')
if grad: # compute gradient
for j in range(len(reward_params)):
for k in [-1,1]:
reward_params_dict[k]=reward_params
reward_params_dict[k][j]= reward_params_dict[k][j] + k*eps
# 1. create a G-learner
G_learner = G_learning_portfolio_opt(num_steps,
reward_params_dict[k],
beta,
benchmark_portf,
gamma,
num_risky_assets,
riskfree_rate,
expected_risky_returns,
Sigma_r,
x_vals_init,
use_for_WM=True)
G_learner.reset_prior_policy()
# run the G-learning recursion to get parameters of G- and F-functions
G_learner.G_learning(error_tol, max_iter_RL)
# compute the rewards and realized values of G- and F-functions from
# all trajectories
for n in range(num_trajs):
for t in range(num_steps):
realized_rewards[n,t] = G_learner.compute_reward_on_traj(t,
data_xvals[n,t,:], data_uvals[n,t,:])
realized_G_fun[n,t] = G_learner.compute_G_fun_on_traj(t,
data_xvals[n,t,:], data_uvals[n,t,:])
realized_F_fun[n,t] = G_learner.compute_F_fun_on_traj(t,
data_xvals[n,t,:])
realized_cum_rewards[n] = realized_rewards[n,:].sum()
realized_G_fun_cum[n] = realized_G_fun[n,:].sum()
realized_F_fun_cum[n] = realized_F_fun[n,:].sum()
loss_dict[k][j] = - beta *(realized_G_fun_cum.sum() - realized_F_fun_cum.sum())
loss_grad[j]=(loss_dict[1][j]-loss_dict[-1][j])/(2.0*eps)
G_learner = G_learning_portfolio_opt(num_steps,
reward_params,
beta,
benchmark_portf,
gamma,
num_risky_assets,
riskfree_rate,
expected_risky_returns,
Sigma_r,
x_vals_init,
use_for_WM=True)
G_learner.reset_prior_policy()
G_learner.G_learning(error_tol, max_iter_RL)
# compute the rewards and realized values of G- and F-functions from
# all trajectories
for n in range(num_trajs):
for t in range(num_steps):
realized_rewards[n,t] = G_learner.compute_reward_on_traj(t,
data_xvals[n,t,:], data_uvals[n,t,:])
realized_G_fun[n,t] = G_learner.compute_G_fun_on_traj(t,
data_xvals[n,t,:], data_uvals[n,t,:])
realized_F_fun[n,t] = G_learner.compute_F_fun_on_traj(t,
data_xvals[n,t,:])
realized_cum_rewards[n] = realized_rewards[n,:].sum()
realized_G_fun_cum[n] = realized_G_fun[n,:].sum()
realized_F_fun_cum[n] = realized_F_fun[n,:].sum()
loss = - beta *(realized_G_fun_cum.sum() - realized_F_fun_cum.sum())
if grad:
return loss, loss_grad
else:
return loss