Exemple #1
0
# ## [Step 8](https://www.arpm.co/lab/redirect.php?permalink=s_bl_equilibrium_ret-implementation-step08): Compute Black-Litterman sure posterior parameters

mu_r_sure_bl = mu_r_equil + sig2_hat_r @ v.T @ \
             np.linalg.solve(v @ sig2_hat_r @ v.T, i - v @ mu_r_equil)
sig2_r_sure_bl = (1 + 1 / tau) * sig2_hat_r - (1 / tau) * sig2_hat_r @ v.T\
               @ np.linalg.solve(v @ sig2_hat_r @ v.T, v @ sig2_hat_r)

# ## [Step 9](https://www.arpm.co/lab/redirect.php?permalink=s_bl_equilibrium_ret-implementation-step09): Compare posterior parameters from point views

# +
k_ = len(v)  # view variables dimension
v_point = v
z_point = i

mu_r_point, sig2_r_point = min_rel_entropy_normal(mu_r_equil, sig2_hat_r,
                                                  v_point, z_point, v_point,
                                                  np.zeros((k_)))
# -

# ## [Step 10](https://www.arpm.co/lab/redirect.php?permalink=s_bl_equilibrium_ret-implementation-step10): Compute posterior parameters from distributional views (Minimum Relative Entropy)

# +
v_mre = v
v_sig_mre = np.eye(n_)
imre = i
sig2_i_mumre = sig2_hat_r

mu_r_mre, sig2_r_mre = min_rel_entropy_normal(mu_r_equil, sig2_hat_r, v_mre,
                                              imre, v_sig_mre, sig2_i_mumre)
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # s_min_rel_ent_point_view [<img src="https://www.arpm.co/lab/icons/icon_permalink.png" width=30 height=30 style="display: inline;">](https://www.arpm.co/lab/redirect.php?code=s_min_rel_ent_point_view&codeLang=Python)
# For details, see [here](https://www.arpm.co/lab/redirect.php?permalink=ExViewTheoryPoint).

# +
import numpy as np

from arpym.views import min_rel_entropy_normal
# -

# ## [Input parameters](https://www.arpm.co/lab/redirect.php?permalink=s_min_rel_ent_point_view-parameters)

mu_base = np.array([0.26, 0.29, 0.33])  # base expectation
sig2_base = np.array([[0.18, 0.11, 0.13],
                     [0.11, 0.23, 0.16],
                     [0.13, 0.16, 0.23]])  # base covariance
v = np.array([[1, -1, 0], [0, 1, -1]])  # view matrix
z_view = np.array([1.02, -0.5])  # point view

# ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_min_rel_ent_point_view-implementation-step01): Compute point view updated parameters

# +
k_, n_ = v.shape  # market and view dimension

mu_upd, sig2_upd = min_rel_entropy_normal(mu_base, sig2_base, v, z_view, v,
                                          np.zeros((k_)))
# +
import numpy as np

from arpym.views import min_rel_entropy_normal
# -

# ## [Input parameters](https://www.arpm.co/lab/redirect.php?permalink=s_min_rel_ent_distr_view-parameters)

# +
mu_x_base = np.array([0.26, 0.29, 0.33])  # base case expectation
sig2_x_base = np.array([[0.18, 0.11, 0.13], [0.11, 0.23, 0.16],
                        [0.13, 0.16, 0.23]])  # base case covariance

v = np.array([[1, -1, 0], [0, 1, -1]])  # view matrix

mu_z_view = np.array([1.02, -0.50])  # expectation of the view variables

sig2_z_view = np.array([[0.35, -0.40],
                        [-0.40, 0.21]])  # covariance of the view variables
# -

# ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_min_rel_ent_distr_view-implementation-step01): Compute base parameters of the view variables

mu_z_base = v @ mu_x_base
sig2_z_base = v @ sig2_x_base @ v.T

# ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_min_rel_ent_distr_view-implementation-step02): Compute distributional view updated parameters

mu_x_upd, sig2_x_upd = min_rel_entropy_normal(mu_x_base, sig2_x_base, v,
                                              mu_z_view, v, sig2_z_view)
# +


def eff_rank(s2):
    lam2_n, _ = np.linalg.eig(s2)
    w_n = lam2_n / np.sum(lam2_n)
    return np.exp(-w_n @ np.log(w_n))


eff_rank_v_mu = eff_rank(cov_2_corr(v_mu @ sig2_x_base @ v_mu.T)[0])
eff_rank_v_sig = eff_rank(cov_2_corr(v_sig @ sig2_x_base @ v_sig.T)[0])
# -

# ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_min_rel_ent_partial_view-implementation-step02): Compute updated parameters

mu_x_upd, sig2_x_upd = min_rel_entropy_normal(mu_x_base, sig2_x_base, v_mu,
                                              mu_view, v_sig, sig2_view)

# ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_min_rel_ent_partial_view-implementation-step03): Compute projectors

# +
k_ = len(mu_view)  # view variables dimension
n_ = len(mu_x_base)  # market dimension

v_mu_inv = sig2_x_base @ v_mu.T @ np.linalg.solve(v_mu @ sig2_x_base @ v_mu.T,
                                                  np.identity(k_))
v_sig_inv = sig2_x_base @ v_sig.T @\
    (np.linalg.solve(v_sig @ sig2_x_base @ v_sig.T, np.identity(k_)))
p_mu = np.eye(n_) - v_mu_inv @ v_mu
p_mu_c = v_mu_inv @ v_mu
p_sig = np.eye(n_) - v_sig_inv @ v_sig
p_sig_c = v_sig_inv @ v_sig