Overlay Portfolio and Benchmark Returns Distributions (#2233)

* overlay portfolio benchmark kdeplot

* add summary stats

* remove stats from plot

Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
This commit is contained in:
montezdesousa
2022-08-29 09:05:53 +01:00
committed by GitHub
parent 065cd78814
commit 5e54660d7b

View File

@@ -730,25 +730,34 @@ def display_distribution_returns(
else:
if external_axes is None:
_, ax = plt.subplots(
1,
2,
figsize=plot_autoscale(),
dpi=PLOT_DPI,
)
else:
ax = external_axes
ax[0].set_title("Portfolio distribution")
sns.kdeplot(portfolio_returns.values, ax=ax[0])
ax[0].set_ylabel("Density")
ax[0].set_xlabel("Daily return [%]")
theme.style_primary_axis(ax[0])
ax.set_title("Returns distribution")
ax.set_ylabel("Density")
ax.set_xlabel("Daily return [%]")
ax[1].set_title("Benchmark distribution")
sns.kdeplot(benchmark_returns.values, ax=ax[1])
ax[1].set_ylabel("Density")
ax[1].set_xlabel("Daily return [%]")
theme.style_primary_axis(ax[1])
ax = sns.kdeplot(portfolio_returns.values, label="portfolio")
kdeline = ax.lines[0]
mean = portfolio_returns.values.mean()
xs = kdeline.get_xdata()
ys = kdeline.get_ydata()
height = np.interp(mean, xs, ys)
ax.vlines(mean, 0, height, color="yellow", ls=":")
ax = sns.kdeplot(benchmark_returns.values, label="benchmark")
kdeline = ax.lines[1]
mean = benchmark_returns.values.mean()
xs = kdeline.get_xdata()
ys = kdeline.get_ydata()
height = np.interp(mean, xs, ys)
ax.vlines(mean, 0, height, color="orange", ls=":")
theme.style_primary_axis(ax)
ax.legend()
if not external_axes:
theme.visualize_output()