Spaces:
Runtime error
Runtime error
File size: 1,037 Bytes
a4539de 8e3072c a4539de 8e3072c 2863f52 a4539de 8e3072c a4539de 8e3072c a4539de 8e3072c a4539de |
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 |
import numpy as np
import torch
class myOptimizer():
def __init__(self, lr, mu, mu_square, adaptation_rate, transaction_cost):
self.lr = lr
self.mu = mu
self.mu_square = mu_square
self.adaptation_rate = adaptation_rate
self.transaction_cost = transaction_cost
def step(self, grad_n, reward, last_observation, last_gradient):
numerator = self.mu_square - (self.mu * reward)
denominator = np.sqrt((self.mu_square - (self.mu ** 2)) ** 3)
gradient = numerator / denominator
current_grad = (-1.0 * self.transaction_cost * grad_n)
previous_grad = (last_observation + self.transaction_cost) * last_gradient
gradient = torch.as_tensor(gradient) * (current_grad + previous_grad)
return torch.as_tensor(self.lr * gradient)
def after_step(self, reward):
self.mu = self.mu + self.adaptation_rate * (reward - self.mu)
self.mu_square = self.mu_square + self.adaptation_rate * ((reward ** 2) - self.mu_square)
|