19
19
20
20
import matplotlib .pyplot as plt
21
21
from pyfolio .pos import get_percent_alloc
22
- from pyfolio .utils import print_table , set_legend_location , COLORS
22
+ from pyfolio .utils import print_table , set_legend_location
23
23
24
24
25
25
def perf_attrib (returns , positions , factor_returns , factor_loadings ,
@@ -151,35 +151,38 @@ def create_perf_attrib_stats(perf_attrib):
151
151
152
152
153
153
def show_perf_attrib_stats (returns , positions , factor_returns ,
154
- factor_loadings ):
154
+ factor_loadings , pos_in_dollars = True ):
155
155
"""
156
156
Calls `perf_attrib` using inputs, and displays outputs using
157
157
`utils.print_table`.
158
158
"""
159
- risk_exposures , perf_attrib_data = perf_attrib (returns ,
160
- positions ,
161
- factor_returns ,
162
- factor_loadings )
159
+ risk_exposures , perf_attrib_data = perf_attrib (
160
+ returns ,
161
+ positions ,
162
+ factor_returns ,
163
+ factor_loadings ,
164
+ pos_in_dollars = pos_in_dollars ,
165
+ )
163
166
164
167
perf_attrib_stats = create_perf_attrib_stats (perf_attrib_data )
165
168
print_table (perf_attrib_stats )
166
169
print_table (risk_exposures )
167
170
168
171
169
- def plot_returns (returns , specific_returns , common_returns , ax = None ):
172
+ def plot_returns (perf_attrib_data , ax = None ):
170
173
"""
171
174
Plot total, specific, and common returns.
172
175
173
176
Parameters
174
177
----------
175
- returns : pd.Series
176
- total returns, indexed by datetime
177
-
178
- specific_returns : pd.Series
179
- specific returns, indexed by datetime
180
-
181
- commons_returns : pd.Series
182
- common returns, indexed by datetime
178
+ perf_attrib_data : pd.DataFrame
179
+ df with factors, common returns, and specific returns as columns,
180
+ and datetimes as index
181
+ - Example:
182
+ momentum reversal common_returns specific_returns
183
+ dt
184
+ 2017-01-01 0.249087 0.935925 1.185012 1.185012
185
+ 2017-01-02 -0.003194 -0.400786 -0.403980 -0.403980
183
186
184
187
ax : matplotlib.axes.Axes
185
188
axes on which plots are made. if None, current axes will be used
@@ -191,13 +194,17 @@ def plot_returns(returns, specific_returns, common_returns, ax=None):
191
194
if ax is None :
192
195
ax = plt .gca ()
193
196
197
+ returns = perf_attrib_data ['total_returns' ]
198
+ specific_returns = perf_attrib_data ['specific_returns' ]
199
+ common_returns = perf_attrib_data ['common_returns' ]
200
+
194
201
ax .plot (ep .cum_returns (returns ), color = 'g' , label = 'Total returns' )
195
202
ax .plot (ep .cum_returns (specific_returns ), color = 'b' ,
196
203
label = 'Cumulative specific returns' )
197
204
ax .plot (ep .cum_returns (common_returns ), color = 'r' ,
198
205
label = 'Cumulative common returns' )
199
206
200
- ax .set_title ('Time Series of cumulative returns' )
207
+ ax .set_title ('Time series of cumulative returns' )
201
208
ax .set_ylabel ('Returns' )
202
209
203
210
set_legend_location (ax )
@@ -235,20 +242,12 @@ def plot_alpha_returns(alpha_returns, ax=None):
235
242
return ax
236
243
237
244
238
- def plot_factor_contribution_to_perf (exposures , perf_attrib_data , ax = None ):
245
+ def plot_factor_contribution_to_perf (perf_attrib_data , ax = None ):
239
246
"""
240
247
Plot each factor's contribution to performance.
241
248
242
249
Parameters
243
250
----------
244
- exposures : pd.DataFrame
245
- df indexed by datetime, with factors as columns
246
- - Example:
247
- momentum reversal
248
- dt
249
- 2017-01-01 -0.238655 0.077123
250
- 2017-01-02 0.821872 1.520515
251
-
252
251
perf_attrib_data : pd.DataFrame
253
252
df with factors, common returns, and specific returns as columns,
254
253
and datetimes as index
@@ -271,12 +270,8 @@ def plot_factor_contribution_to_perf(exposures, perf_attrib_data, ax=None):
271
270
factors_and_specific = perf_attrib_data .drop (
272
271
['total_returns' , 'common_returns' ], axis = 'columns' )
273
272
274
- ax .stackplot (
275
- factors_and_specific .index ,
276
- [factors_and_specific [s ] for s in factors_and_specific ],
277
- labels = factors_and_specific .columns ,
278
- colors = COLORS
279
- )
273
+ for col in factors_and_specific :
274
+ ax .plot (factors_and_specific [col ])
280
275
281
276
ax .axhline (0 , color = 'k' )
282
277
set_legend_location (ax )
@@ -309,10 +304,8 @@ def plot_risk_exposures(exposures, ax=None):
309
304
if ax is None :
310
305
ax = plt .gca ()
311
306
312
- ax .stackplot (exposures .index ,
313
- [exposures [s ] for s in exposures ],
314
- labels = exposures .columns ,
315
- colors = COLORS )
307
+ for col in exposures :
308
+ ax .plot (exposures [col ])
316
309
317
310
set_legend_location (ax )
318
311
ax .set_ylabel ('Factor exposures' )
0 commit comments