Completed
Pull Request — master (#858)
by Eddie
02:39
created

zipline.finance.risk.information_ratio()   B

Complexity

Conditions 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 25
rs 8.8571
1
#
2
# Copyright 2014 Quantopian, Inc.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import functools
17
import logbook
18
import math
19
import numpy as np
20
21
import zipline.utils.math_utils as zp_math
22
23
import pandas as pd
24
from pandas.tseries.tools import normalize_date
25
26
from six import iteritems
27
28
from . risk import (
29
    alpha,
30
    check_entry,
31
    choose_treasury,
32
    downside_risk,
33
    sharpe_ratio,
34
    sortino_ratio,
35
)
36
37
from zipline.utils.serialization_utils import (
38
    VERSION_LABEL
39
)
40
41
log = logbook.Logger('Risk Cumulative')
42
43
44
choose_treasury = functools.partial(choose_treasury, lambda *args: '10year',
45
                                    compound=False)
46
47
48
def information_ratio(algo_volatility, algorithm_return, benchmark_return):
49
    """
50
    http://en.wikipedia.org/wiki/Information_ratio
51
52
    Args:
53
        algorithm_returns (np.array-like):
54
            All returns during algorithm lifetime.
55
        benchmark_returns (np.array-like):
56
            All benchmark returns during algo lifetime.
57
58
    Returns:
59
        float. Information ratio.
60
    """
61
    if zp_math.tolerant_equals(algo_volatility, 0):
62
        return np.nan
63
64
    # The square of the annualization factor is in the volatility,
65
    # because the volatility is also annualized,
66
    # i.e. the sqrt(annual factor) is in the volatility's numerator.
67
    # So to have the the correct annualization factor for the
68
    # Sharpe value's numerator, which should be the sqrt(annual factor).
69
    # The square of the sqrt of the annual factor, i.e. the annual factor
70
    # itself, is needed in the numerator to factor out the division by
71
    # its square root.
72
    return (algorithm_return - benchmark_return) / algo_volatility
73
74
75
class RiskMetricsCumulative(object):
76
    """
77
    :Usage:
78
        Instantiate RiskMetricsCumulative once.
79
        Call update() method on each dt to update the metrics.
80
    """
81
82
    METRIC_NAMES = (
83
        'alpha',
84
        'beta',
85
        'sharpe',
86
        'algorithm_volatility',
87
        'benchmark_volatility',
88
        'downside_risk',
89
        'sortino',
90
        'information',
91
    )
92
93
    def __init__(self, sim_params, env, create_first_day_stats=False):
94
        self.treasury_curves = env.treasury_curves
95
        self.start_date = sim_params.period_start.replace(
96
            hour=0, minute=0, second=0, microsecond=0
97
        )
98
        self.end_date = sim_params.period_end.replace(
99
            hour=0, minute=0, second=0, microsecond=0
100
        )
101
102
        self.trading_days = env.days_in_range(self.start_date, self.end_date)
103
104
        # Hold on to the trading day before the start,
105
        # used for index of the zero return value when forcing returns
106
        # on the first day.
107
        self.day_before_start = self.start_date - env.trading_days.freq
108
109
        last_day = normalize_date(sim_params.period_end)
110
        if last_day not in self.trading_days:
111
            last_day = pd.tseries.index.DatetimeIndex(
112
                [last_day]
113
            )
114
            self.trading_days = self.trading_days.append(last_day)
115
116
        self.sim_params = sim_params
117
        self.env = env
118
119
        self.create_first_day_stats = create_first_day_stats
120
121
        cont_index = self.trading_days
122
123
        self.cont_index = cont_index
124
        self.cont_len = len(self.cont_index)
125
126
        empty_cont = np.full(self.cont_len, np.nan)
127
128
        self.algorithm_returns_cont = empty_cont.copy()
129
        self.benchmark_returns_cont = empty_cont.copy()
130
        self.algorithm_cumulative_leverages_cont = empty_cont.copy()
131
        self.mean_returns_cont = empty_cont.copy()
132
        self.annualized_mean_returns_cont = empty_cont.copy()
133
        self.mean_benchmark_returns_cont = empty_cont.copy()
134
        self.annualized_mean_benchmark_returns_cont = empty_cont.copy()
135
136
        # The returns at a given time are read and reset from the respective
137
        # returns container.
138
        self.algorithm_returns = None
139
        self.benchmark_returns = None
140
        self.mean_returns = None
141
        self.annualized_mean_returns = None
142
        self.mean_benchmark_returns = None
143
        self.annualized_mean_benchmark_returns = None
144
145
        self.algorithm_cumulative_returns = empty_cont.copy()
146
        self.benchmark_cumulative_returns = empty_cont.copy()
147
        self.algorithm_cumulative_leverages = empty_cont.copy()
148
        self.excess_returns = empty_cont.copy()
149
150
        self.latest_dt_loc = 0
151
        self.latest_dt = cont_index[0]
152
153
        self.benchmark_volatility = empty_cont.copy()
154
        self.algorithm_volatility = empty_cont.copy()
155
        self.beta = empty_cont.copy()
156
        self.alpha = empty_cont.copy()
157
        self.sharpe = empty_cont.copy()
158
        self.downside_risk = empty_cont.copy()
159
        self.sortino = empty_cont.copy()
160
        self.information = empty_cont.copy()
161
162
        self.drawdowns = empty_cont.copy()
163
        self.max_drawdowns = empty_cont.copy()
164
        self.max_drawdown = 0
165
        self.max_leverages = empty_cont.copy()
166
        self.max_leverage = 0
167
        self.current_max = -np.inf
168
        self.daily_treasury = pd.Series(index=self.trading_days)
169
        self.treasury_period_return = np.nan
170
171
        self.num_trading_days = 0
172
173
    def update(self, dt, algorithm_returns, benchmark_returns, leverage):
174
        # Keep track of latest dt for use in to_dict and other methods
175
        # that report current state.
176
        self.latest_dt = dt
177
        dt_loc = self.cont_index.get_loc(dt)
178
        self.latest_dt_loc = dt_loc
179
180
        self.algorithm_returns_cont[dt_loc] = algorithm_returns
181
        self.algorithm_returns = self.algorithm_returns_cont[:dt_loc + 1]
182
183
        self.num_trading_days = len(self.algorithm_returns)
184
185
        if self.create_first_day_stats:
186
            if len(self.algorithm_returns) == 1:
187
                self.algorithm_returns = np.append(0.0, self.algorithm_returns)
188
189
        self.algorithm_cumulative_returns[dt_loc] = \
190
            self.calculate_cumulative_returns(self.algorithm_returns)
191
192
        algo_cumulative_returns_to_date = \
193
            self.algorithm_cumulative_returns[:dt_loc + 1]
194
195
        self.mean_returns_cont[dt_loc] = \
196
            algo_cumulative_returns_to_date[dt_loc] / self.num_trading_days
197
198
        self.mean_returns = self.mean_returns_cont[:dt_loc + 1]
199
200
        self.annualized_mean_returns_cont[dt_loc] = \
201
            self.mean_returns_cont[dt_loc] * 252
202
203
        self.annualized_mean_returns = \
204
            self.annualized_mean_returns_cont[:dt_loc + 1]
205
206
        if self.create_first_day_stats:
207
            if len(self.mean_returns) == 1:
208
                self.mean_returns = np.append(0.0, self.mean_returns)
209
                self.annualized_mean_returns = np.append(
210
                    0.0, self.annualized_mean_returns)
211
212
        self.benchmark_returns_cont[dt_loc] = benchmark_returns
213
        self.benchmark_returns = self.benchmark_returns_cont[:dt_loc + 1]
214
215
        if self.create_first_day_stats:
216
            if len(self.benchmark_returns) == 1:
217
                self.benchmark_returns = np.append(0.0, self.benchmark_returns)
218
219
        self.benchmark_cumulative_returns[dt_loc] = \
220
            self.calculate_cumulative_returns(self.benchmark_returns)
221
222
        benchmark_cumulative_returns_to_date = \
223
            self.benchmark_cumulative_returns[:dt_loc + 1]
224
225
        self.mean_benchmark_returns_cont[dt_loc] = \
226
            benchmark_cumulative_returns_to_date[dt_loc] / \
227
            self.num_trading_days
228
229
        self.mean_benchmark_returns = self.mean_benchmark_returns_cont[:dt_loc]
230
231
        self.annualized_mean_benchmark_returns_cont[dt_loc] = \
232
            self.mean_benchmark_returns_cont[dt_loc] * 252
233
234
        self.annualized_mean_benchmark_returns = \
235
            self.annualized_mean_benchmark_returns_cont[:dt_loc + 1]
236
237
        self.algorithm_cumulative_leverages_cont[dt_loc] = leverage
238
        self.algorithm_cumulative_leverages = \
239
            self.algorithm_cumulative_leverages_cont[:dt_loc + 1]
240
241
        if self.create_first_day_stats:
242
            if len(self.algorithm_cumulative_leverages) == 1:
243
                self.algorithm_cumulative_leverages = np.append(
244
                    0.0,
245
                    self.algorithm_cumulative_leverages)
246
247
        if not len(self.algorithm_returns) and len(self.benchmark_returns):
248
            message = "Mismatch between benchmark_returns ({bm_count}) and \
249
algorithm_returns ({algo_count}) in range {start} : {end} on {dt}"
250
            message = message.format(
251
                bm_count=len(self.benchmark_returns),
252
                algo_count=len(self.algorithm_returns),
253
                start=self.start_date,
254
                end=self.end_date,
255
                dt=dt
256
            )
257
            raise Exception(message)
258
259
        self.update_current_max()
260
        self.benchmark_volatility[dt_loc] = \
261
            self.calculate_volatility(self.benchmark_returns)
262
        self.algorithm_volatility[dt_loc] = \
263
            self.calculate_volatility(self.algorithm_returns)
264
265
        # caching the treasury rates for the minutely case is a
266
        # big speedup, because it avoids searching the treasury
267
        # curves on every minute.
268
        # In both minutely and daily, the daily curve is always used.
269
        treasury_end = dt.replace(hour=0, minute=0)
270
        if np.isnan(self.daily_treasury[treasury_end]):
271
            treasury_period_return = choose_treasury(
272
                self.treasury_curves,
273
                self.start_date,
274
                treasury_end,
275
                self.env,
276
            )
277
            self.daily_treasury[treasury_end] = treasury_period_return
278
        self.treasury_period_return = self.daily_treasury[treasury_end]
279
        self.excess_returns[dt_loc] = (
280
            self.algorithm_cumulative_returns[dt_loc] -
281
            self.treasury_period_return)
282
        self.beta[dt_loc] = self.calculate_beta()
283
        self.alpha[dt_loc] = self.calculate_alpha()
284
        self.sharpe[dt_loc] = self.calculate_sharpe()
285
        self.downside_risk[dt_loc] = \
286
            self.calculate_downside_risk()
287
        self.sortino[dt_loc] = self.calculate_sortino()
288
        self.information[dt_loc] = self.calculate_information()
289
        self.max_drawdown = self.calculate_max_drawdown()
290
        self.max_drawdowns[dt_loc] = self.max_drawdown
291
        self.max_leverage = self.calculate_max_leverage()
292
        self.max_leverages[dt_loc] = self.max_leverage
293
294
    def to_dict(self):
295
        """
296
        Creates a dictionary representing the state of the risk report.
297
        Returns a dict object of the form:
298
        """
299
        dt = self.latest_dt
300
        dt_loc = self.latest_dt_loc
301
        period_label = dt.strftime("%Y-%m")
302
        rval = {
303
            'trading_days': self.num_trading_days,
304
            'benchmark_volatility':
305
            self.benchmark_volatility[dt_loc],
306
            'algo_volatility':
307
            self.algorithm_volatility[dt_loc],
308
            'treasury_period_return': self.treasury_period_return,
309
            # Though the two following keys say period return,
310
            # they would be more accurately called the cumulative return.
311
            # However, the keys need to stay the same, for now, for backwards
312
            # compatibility with existing consumers.
313
            'algorithm_period_return':
314
            self.algorithm_cumulative_returns[dt_loc],
315
            'benchmark_period_return':
316
            self.benchmark_cumulative_returns[dt_loc],
317
            'beta': self.beta[dt_loc],
318
            'alpha': self.alpha[dt_loc],
319
            'sharpe': self.sharpe[dt_loc],
320
            'sortino': self.sortino[dt_loc],
321
            'information': self.information[dt_loc],
322
            'excess_return': self.excess_returns[dt_loc],
323
            'max_drawdown': self.max_drawdown,
324
            'max_leverage': self.max_leverage,
325
            'period_label': period_label
326
        }
327
328
        return {k: (None if check_entry(k, v) else v)
329
                for k, v in iteritems(rval)}
330
331
    def __repr__(self):
332
        statements = []
333
        for metric in self.METRIC_NAMES:
334
            value = getattr(self, metric)[-1]
335
            if isinstance(value, list):
336
                if len(value) == 0:
337
                    value = np.nan
338
                else:
339
                    value = value[-1]
340
            statements.append("{m}:{v}".format(m=metric, v=value))
341
342
        return '\n'.join(statements)
343
344
    def calculate_cumulative_returns(self, returns):
345
        return (1. + returns).prod() - 1
346
347
    def update_current_max(self):
348
        if len(self.algorithm_cumulative_returns) == 0:
349
            return
350
        current_cumulative_return = \
351
            self.algorithm_cumulative_returns[self.latest_dt_loc]
352
        if self.current_max < current_cumulative_return:
353
            self.current_max = current_cumulative_return
354
355
    def calculate_max_drawdown(self):
356
        if len(self.algorithm_cumulative_returns) == 0:
357
            return self.max_drawdown
358
359
        # The drawdown is defined as: (high - low) / high
360
        # The above factors out to: 1.0 - (low / high)
361
        #
362
        # Instead of explicitly always using the low, use the current total
363
        # return value, and test that against the max drawdown, which will
364
        # exceed the previous max_drawdown iff the current return is lower than
365
        # the previous low in the current drawdown window.
366
        cur_drawdown = 1.0 - (
367
            (1.0 + self.algorithm_cumulative_returns[self.latest_dt_loc])
368
            /
369
            (1.0 + self.current_max))
370
371
        self.drawdowns[self.latest_dt_loc] = cur_drawdown
372
373
        if self.max_drawdown < cur_drawdown:
374
            return cur_drawdown
375
        else:
376
            return self.max_drawdown
377
378
    def calculate_max_leverage(self):
379
        # The leverage is defined as: the gross_exposure/net_liquidation
380
        # gross_exposure = long_exposure + abs(short_exposure)
381
        # net_liquidation = ending_cash + long_exposure + short_exposure
382
        cur_leverage = self.algorithm_cumulative_leverages_cont[
383
            self.latest_dt_loc]
384
385
        return max(cur_leverage, self.max_leverage)
386
387
    def calculate_sharpe(self):
388
        """
389
        http://en.wikipedia.org/wiki/Sharpe_ratio
390
        """
391
        return sharpe_ratio(
392
            self.algorithm_volatility[self.latest_dt_loc],
393
            self.annualized_mean_returns_cont[self.latest_dt_loc],
394
            self.daily_treasury[self.latest_dt.date()])
395
396
    def calculate_sortino(self):
397
        """
398
        http://en.wikipedia.org/wiki/Sortino_ratio
399
        """
400
        return sortino_ratio(
401
            self.annualized_mean_returns_cont[self.latest_dt_loc],
402
            self.daily_treasury[self.latest_dt.date()],
403
            self.downside_risk[self.latest_dt_loc])
404
405
    def calculate_information(self):
406
        """
407
        http://en.wikipedia.org/wiki/Information_ratio
408
        """
409
        return information_ratio(
410
            self.algorithm_volatility[self.latest_dt_loc],
411
            self.annualized_mean_returns_cont[self.latest_dt_loc],
412
            self.annualized_mean_benchmark_returns_cont[self.latest_dt_loc])
413
414
    def calculate_alpha(self):
415
        """
416
        http://en.wikipedia.org/wiki/Alpha_(investment)
417
        """
418
        return alpha(
419
            self.annualized_mean_returns_cont[self.latest_dt_loc],
420
            self.treasury_period_return,
421
            self.annualized_mean_benchmark_returns_cont[self.latest_dt_loc],
422
            self.beta[self.latest_dt_loc])
423
424
    def calculate_volatility(self, daily_returns):
425
        if len(daily_returns) <= 1:
426
            return 0.0
427
        return np.std(daily_returns, ddof=1) * math.sqrt(252)
428
429
    def calculate_downside_risk(self):
430
        return downside_risk(self.algorithm_returns,
431
                             self.mean_returns,
432
                             252)
433
434
    def calculate_beta(self):
435
        """
436
437
        .. math::
438
439
            \\beta_a = \\frac{\mathrm{Cov}(r_a,r_p)}{\mathrm{Var}(r_p)}
440
441
        http://en.wikipedia.org/wiki/Beta_(finance)
442
        """
443
        # it doesn't make much sense to calculate beta for less than two
444
        # values, so return none.
445
        if len(self.algorithm_returns) < 2:
446
            return 0.0
447
448
        returns_matrix = np.vstack([self.algorithm_returns,
449
                                    self.benchmark_returns])
450
        C = np.cov(returns_matrix, ddof=1)
451
        algorithm_covariance = C[0][1]
452
        benchmark_variance = C[1][1]
453
        beta = algorithm_covariance / benchmark_variance
454
455
        return beta
456
457
    def __getstate__(self):
458
        state_dict = {k: v for k, v in iteritems(self.__dict__)
459
                      if not k.startswith('_')}
460
461
        STATE_VERSION = 3
462
        state_dict[VERSION_LABEL] = STATE_VERSION
463
464
        return state_dict
465
466
    def __setstate__(self, state):
467
468
        OLDEST_SUPPORTED_STATE = 3
469
        version = state.pop(VERSION_LABEL)
470
471
        if version < OLDEST_SUPPORTED_STATE:
472
            raise BaseException("RiskMetricsCumulative \
473
                    saved state is too old.")
474
475
        self.__dict__.update(state)
476