Example #1
0
    def savefig(self):
        returns = self.port.returns

        model = 'Classic'

        # 포트폴리오 수익률과 비중 저장
        with open(
                'returns-{}_{}.pkl'.format(self.risk_profile,
                                           self.current_date), 'wb') as f:
            pl.dump(returns, f)

        with open('w-{}_{}.pkl'.format(self.risk_profile, self.current_date),
                  'wb') as f:
            pl.dump(self.w, f)

        # Efficient fronter 저장
        points = 50  # Number of points of the frontier
        frontier = self.port.efficient_frontier(model=model,
                                                rm=self.rm,
                                                points=points,
                                                rf=self.rf,
                                                hist=True)

        label = 'Max-return Portfolio' if self.risk_profile == 4 else 'Min-risk Portfolio'
        mu = self.port.mu  # Expected returns
        cov = self.port.cov  # Covariance matrix

        fig1, ax1 = plt.subplots()

        ax1 = plf.plot_frontier(w_frontier=frontier,
                                mu=mu,
                                cov=cov,
                                returns=returns,
                                rm=self.rm,
                                rf=self.rf,
                                alpha=0.05,
                                cmap='viridis',
                                w=self.w,
                                label=label,
                                marker='*',
                                s=16,
                                c='r',
                                height=6,
                                width=10,
                                ax=ax1)

        fig1.savefig('ef-{}_{}.png'.format(self.risk_profile,
                                           self.current_date))

        fig2, ax2 = plt.subplots(nrows=1, ncols=1)
        # Plotting efficient frontier composition
        ax2 = plf.plot_frontier_area(w_frontier=frontier,
                                     cmap="tab20",
                                     height=6,
                                     width=10,
                                     ax=None)
        fig2.savefig('ef_area-{}_{}.png'.format(self.risk_profile,
                                                self.current_date))
                                   hist=hist)
print(frontier.T.head())

# Plotting the efficient frontier
label = 'Max Risk Adjusted Return Portfolio'  # Title of point
mu = port.mu  # Expected returns
cov = port.cov  # Covariance matrix
returns = port.returns  # Returns of the assets
ax = plf.plot_frontier(w_frontier=frontier,
                       mu=mu,
                       cov=cov,
                       returns=returns,
                       rm=rm,
                       rf=rf,
                       alpha=0.01,
                       cmap='viridis',
                       w=w,
                       label=label,
                       marker='*',
                       s=16,
                       c='r',
                       height=6,
                       width=10,
                       ax=None)

# Plotting efficient frontier composition
ax = plf.plot_frontier_area(w_frontier=frontier,
                            cmap="tab20",
                            height=6,
                            width=10,
                            ax=None)
rm = 'CVaR'  # Risk measure
Example #3
0
#%%

label = "Max Risk Adjusted Return Portfolio"
mu = port.mu
cov = port.cov
returns = port.returns

ax = plf.plot_frontier(
    w_frontier=w_5,
    mu=mu,
    cov=cov,
    returns=returns,
    rm=rm,
    rf=0,
    alpha=0.01,
    cmap="viridis",
    w=w2_1,
    label="Portfolio",
    marker="*",
    s=16,
    c="r",
    height=6,
    width=10,
    ax=None,
)

ax = plf.plot_pie(w=w2_1,
                  title="Portafolio",
                  height=6,
                  width=10,
                  cmap="tab20",
                  ax=None)