-
Notifications
You must be signed in to change notification settings - Fork 212
/
eg.py
37 lines (28 loc) · 901 Bytes
/
eg.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
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from .. import tools
from ..algo import Algo
class EG(Algo):
"""Exponentiated Gradient (EG) algorithm by Helmbold et al.
Reference:
Helmbold, David P., et al.
"On‐Line Portfolio Selection Using Multiplicative Updates."
Mathematical Finance 8.4 (1998): 325-347.
"""
def __init__(self, eta=0.05):
"""
:params eta: Learning rate. Controls volatility of weights.
"""
super().__init__()
self.eta = eta
def init_weights(self, columns):
m = len(columns)
return np.ones(m) / m
def step(self, x, last_b, history):
b = last_b * np.exp(self.eta * x / sum(x * last_b))
return b / sum(b)
if __name__ == "__main__":
data = tools.dataset("nyse_o")
tools.quickrun(EG(eta=0.5), data)