Coverage for cvx/simulator/utils/metric.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2025-01-10 14:11 +0000

1import numpy as np 

2import pandas as pd 

3 

4 

5def _periods(ts): 

6 """ 

7 compute the number of periods in a time series 

8 """ 

9 series = pd.Series(data=ts.index) 

10 return 365 * 24 * 60 * 60 / (series.diff().dropna().mean().total_seconds()) 

11 

12 

13def sharpe(ts, n=None): 

14 """ 

15 compute the sharpe ratio of a time series 

16 """ 

17 std = ts.std() 

18 if std > 0: 

19 n = n or _periods(ts) 

20 return (ts.mean() / std) * np.sqrt(n) 

21 else: 

22 return np.inf