-
Notifications
You must be signed in to change notification settings - Fork 13
/
evaluation.py
28 lines (23 loc) · 1.16 KB
/
evaluation.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
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score
def position_returns(yport_pred, hedgeRatio,df,df_opens, start,end):
df = df.iloc[start:end]
df_opens = df_opens.iloc[start:end]
hedgeRatio = hedgeRatio[start:end]
print(df.shape,hedgeRatio.shape)
mean = np.mean(yport_pred)
std = np.std(yport_pred)
numUnits = (yport_pred - mean)/std
print(numUnits.shape)
positions= pd.DataFrame(np.expand_dims(numUnits, axis=1)*hedgeRatio)*df.values
# results.evec(:, 0)' can be viewed as the capital allocation, while positions is the dollar capital in each ETF.
pnl=np.sum((positions.shift().values)*(df_opens.pct_change().values), axis=1)# daily P&L of the strategy, entering at the open
ret=pnl/np.sum(np.abs(positions.shift()), axis=1)
ret.fillna(value=0, inplace=True)
returns = np.cumprod(1+ret)-1
print('APR=%f Sharpe=%f' % (np.prod(1+ret)**(252/len(ret))-1, np.sqrt(252)*np.mean(ret)/np.std(ret)))
return positions,pnl, returns
def eval_metrics(actual, predicted):
rmspe = np.sqrt(np.mean(np.square(((actual - predicted) / actual))))*100
return rmspe