Skip to content

Commit

Permalink
Fix a bug in reports functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dcajasn committed Oct 9, 2024
1 parent 465d3e4 commit 3ce1427
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 113 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ If you use Riskfolio-Lib for published work, please use the following BibTeX ent
```
@misc{riskfolio,
author = {Dany Cajas},
title = {Riskfolio-Lib (6.3.0)},
title = {Riskfolio-Lib (6.3.1)},
year = {2024},
url = {https://github.com/dcajasn/Riskfolio-Lib},
}
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
copyright = '2020-2024, Dany Cajas'
author = 'Dany Cajas'

__version__ = "6.3.0"
__version__ = "6.3.1"

# The short X.Y version
version = '.'.join(__version__.split('.')[:2])
Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ If you use Riskfolio-Lib for published work, please use the following BibTeX ent

@misc{riskfolio,
author = {Dany Cajas},
title = {Riskfolio-Lib (6.3.0)},
title = {Riskfolio-Lib (6.3.1)},
year = {2024},
url = {https://github.com/dcajasn/Riskfolio-Lib},
}
Expand Down

Large diffs are not rendered by default.

Binary file modified examples/report.xlsx
Binary file not shown.
54 changes: 43 additions & 11 deletions riskfolio/src/PlotFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import matplotlib.lines as mlines
import matplotlib.ticker as mticker
from matplotlib import cm, colors
from matplotlib.gridspec import GridSpec
from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec
import scipy.stats as st
import scipy.cluster.hierarchy as hr
from scipy.spatial.distance import squareform
Expand Down Expand Up @@ -452,7 +452,12 @@ def plot_frontier(
if isinstance(ax, plt.Axes):
ax.axis("off")
fig = ax.get_figure()
gs = GridSpec(nrows=1, ncols=2, figure=fig, width_ratios=width_ratios)
if hasattr(ax, 'get_subplotspec'):
subplot_spec = ax.get_subplotspec()
gs0 = subplot_spec.get_gridspec()
gs = GridSpecFromSubplotSpec(nrows=1, ncols=2, width_ratios=width_ratios, subplot_spec=gs0[0])
else:
gs = GridSpec(nrows=1, ncols=2, figure=fig, width_ratios=width_ratios)
axes = []
axes.append(fig.add_subplot(gs[0]))
axes.append(fig.add_subplot(gs[1]))
Expand Down Expand Up @@ -2274,7 +2279,12 @@ def plot_drawdown(
if isinstance(ax, plt.Axes):
ax.axis("off")
fig = ax.get_figure()
gs = GridSpec(nrows=2, ncols=1, figure=fig, height_ratios=height_ratios)
if hasattr(ax, 'get_subplotspec'):
subplot_spec = ax.get_subplotspec()
gs0 = subplot_spec.get_gridspec()
gs = GridSpecFromSubplotSpec(nrows=2, ncols=1, height_ratios=height_ratios, subplot_spec=gs0[0])
else:
gs = GridSpec(nrows=2, ncols=1, figure=fig, height_ratios=height_ratios)
axes = []
axes.append(fig.add_subplot(gs[0]))
axes.append(fig.add_subplot(gs[1]))
Expand Down Expand Up @@ -2838,13 +2848,22 @@ def plot_clusters(
fig = ax.get_figure()
if dendrogram == True:
if isinstance(ax, plt.Axes):
gs = GridSpec(
nrows=2,
ncols=3,
figure=fig,
height_ratios=height_ratios_1,
width_ratios=width_ratios_1,
)
if hasattr(ax, 'get_subplotspec'):
subplot_spec = ax.get_subplotspec()
gs0 = subplot_spec.get_gridspec()
gs = GridSpecFromSubplotSpec(nrows=2,
ncols=3,
height_ratios=height_ratios_1,
width_ratios=width_ratios_1,
subplot_spec=gs0[0]
)
else:
gs = GridSpec(nrows=2,
ncols=3,
figure=fig,
height_ratios=height_ratios_1,
width_ratios=width_ratios_1,
)
axes = []
for i in range(2):
for j in range(3):
Expand All @@ -2853,7 +2872,20 @@ def plot_clusters(
raise TypeError("ax must be a matplotlib axes object.")
else:
if isinstance(ax, plt.Axes):
gs = GridSpec(nrows=1, ncols=2, figure=fig, width_ratios=width_ratios_2)
if hasattr(ax, 'get_subplotspec'):
subplot_spec = ax.get_subplotspec()
gs0 = subplot_spec.get_gridspec()
gs = GridSpecFromSubplotSpec(nrows=1,
ncols=2,
width_ratios=width_ratios_2,
subplot_spec=gs0[0]
)
else:
gs = GridSpec(nrows=1,
ncols=2,
figure=fig,
width_ratios=width_ratios_2,
)
axes = []
for i in range(1):
for j in range(2):
Expand Down
71 changes: 43 additions & 28 deletions riskfolio/src/Reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from xlsxwriter.utility import xl_range_abs, xl_range, xl_rowcol_to_cell, xl_col_to_name
import datetime
import riskfolio.src.PlotFunctions as plf
Expand All @@ -21,7 +22,7 @@
]


__LICENSE__ = """Copyright (c) 2020-2023, Dany Cajas
__LICENSE__ = """Copyright (c) 2020-2024, Dany Cajas
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -194,13 +195,26 @@ def jupyter_report(

cov = returns.cov()

fig, ax = plt.subplots(
nrows=6,
figsize=(width, height * 6),
gridspec_kw={"height_ratios": [2, 1, 1.5, 1, 1, 1]},
)
fig, ax = plt.subplots(figsize=(width, height * 6))
ax.axis('off')

gs0 = GridSpec(5, 1, figure=fig, height_ratios=[2, 1.5, 1, 2, 1])
gs00 = gs0[0].subgridspec(1, 1)
ax0 = fig.add_subplot(gs00[0, 0])

gs01 = gs0[1].subgridspec(1, 1)
ax1 = fig.add_subplot(gs01[0, 0])

gs02 = gs0[2].subgridspec(1, 1)
ax2 = fig.add_subplot(gs02[0, 0])

ax[0] = plf.plot_table(
gs03 = gs0[3].subgridspec(1, 1)
ax3 = fig.add_subplot(gs03[0, 0])

gs04 = gs0[4].subgridspec(1, 1)
ax4 = fig.add_subplot(gs04[0, 0])

ax0 = plf.plot_table(
returns,
w,
MAR=rf,
Expand All @@ -211,19 +225,34 @@ def jupyter_report(
t_factor=t_factor,
ini_days=ini_days,
days_per_year=days_per_year,
ax=ax[0],
ax=ax0,
)

ax[2] = plf.plot_pie(
ax1 = plf.plot_pie(
w=w,
title="Portfolio Composition",
others=others,
nrow=nrow,
cmap=cmap,
ax=ax[2],
ax=ax1,
)

ax2 = plf.plot_hist(
returns=returns,
w=w,
alpha=alpha,
a_sim=a_sim,
kappa=kappa,
solver=solver,
bins=bins,
ax=ax2,
)

ax3 = plf.plot_drawdown(
returns=returns, w=w, alpha=alpha, kappa=kappa, solver=solver, ax=ax3
)

ax[3] = plf.plot_risk_con(
ax4 = plf.plot_risk_con(
w=w,
cov=cov,
returns=returns,
Expand All @@ -240,22 +269,7 @@ def jupyter_report(
erc_line=erc_line,
color=color,
erc_linecolor=erc_linecolor,
ax=ax[3],
)

ax[4] = plf.plot_hist(
returns=returns,
w=w,
alpha=alpha,
a_sim=a_sim,
kappa=kappa,
solver=solver,
bins=bins,
ax=ax[4],
)

ax[[1, 5]] = plf.plot_drawdown(
returns=returns, w=w, alpha=alpha, kappa=kappa, solver=solver, ax=ax[[1, 5]]
ax=ax4,
)

year = str(datetime.datetime.now().year)
Expand All @@ -264,7 +278,7 @@ def jupyter_report(
subtitle = "Copyright (c) 2020-" + year + ", Dany Cajas. All rights reserved."

fig.suptitle(title, fontsize="xx-large", y=1.011, fontweight="bold")
ax[0].set_title(subtitle, fontsize="large", ha="center", pad=10)
ax0.set_title(subtitle, fontsize="large", ha="center", pad=10)

return ax

Expand Down Expand Up @@ -349,6 +363,7 @@ def excel_report(
n2 = returns.shape[0]

portfolios = w.columns.tolist()
returns.index = returns.index.tz_localize(None)
dates = returns.index.tolist()
year = str(datetime.datetime.now().year)
days = (returns.index[-1] - returns.index[0]).days + ini_days
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

MAJOR = 6
MINOR = 3
MICRO = 0
MICRO = 1
VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)

def write_version_py(filename='riskfolio/version.py'):
Expand Down

0 comments on commit 3ce1427

Please sign in to comment.