def test_allocate_mcos(): """ Test the estimates of the optimal allocation using the Monte Carlo optimization selection """ nco = NCO() # Covariance matrix, mean vector and other variables for the method np.random.seed(0) mu_vec = np.array([0, 0.1, 0.2, 0.3]) cov_mat = np.array([[1, 0.1, 0.2, 0.3], [0.1, 1, 0.1, 0.2], [0.2, 0.1, 1, 0.1], [0.3, 0.2, 0.1, 1]]) num_obs = 100 num_sims = 2 kde_bwidth = 0.25 min_var_portf = True lw_shrinkage = False # Alternative set of values min_var_portf_alt = False kde_bwidth_alt = 0 # Expected weights for minimum variance allocation w_cvo_expected = pd.DataFrame( [[0.249287, 0.256002, 0.242593, 0.252118], [0.257547, 0.265450, 0.242453, 0.234551]]) w_nco_expected = pd.DataFrame( [[0.248396, 0.243172, 0.250751, 0.257680], [0.257547, 0.265450, 0.242453, 0.234551]]) # Expected weights for maximum Sharpe ratio allocation w_cvo_sr_expected = pd.DataFrame( [[-1.081719, 1.810936, 1.218067, 3.978880], [-2.431651, 0.594868, -0.210175, 5.117628]]) w_nco_sr_expected = pd.DataFrame( [[-1.060835, 1.910503, 1.315026, 3.908128], [-0.937168, 1.886158, -0.389275, 4.884809]]) # Finding the optimal weights for minimum variance w_cvo, w_nco = nco.allocate_mcos(mu_vec, cov_mat, num_obs, num_sims, kde_bwidth, min_var_portf, lw_shrinkage) # Finding the optimal weights for maximum Sharpe ratio w_cvo_sr, w_nco_sr = nco.allocate_mcos(mu_vec, cov_mat, num_obs, num_sims, kde_bwidth_alt, min_var_portf_alt, lw_shrinkage) # Testing if the optimal allocation simulations are right np.testing.assert_almost_equal(np.array(w_cvo), np.array(w_cvo_expected), decimal=4) np.testing.assert_almost_equal(np.array(w_nco), np.array(w_nco_expected), decimal=4) np.testing.assert_almost_equal(np.array(w_cvo_sr), np.array(w_cvo_sr_expected), decimal=4) np.testing.assert_almost_equal(np.array(w_nco_sr), np.array(w_nco_sr_expected), decimal=4)
def test_allocate_mcos(): """ Test the estimates of the optimal allocation using the Monte Carlo optimization selection """ nco = NCO() # Covariance matrix, mean vector and other variables for the method np.random.seed(0) mu_vec = np.array([0, 0.1, 0.2, 0.3]) cov_mat = np.array([[1, 0.1, 0.2, 0.3], [0.1, 1, 0.1, 0.2], [0.2, 0.1, 1, 0.1], [0.3, 0.2, 0.1, 1]]) num_obs = 100 num_sims = 2 kde_bwidth = 0.25 min_var_portf = True lw_shrinkage = False # Alternative set of values min_var_portf_alt = False kde_bwidth_alt = 0 # Expected weights for minimum variance allocation # Second line in the below test was changed after v.0.11.3 from [0.257547, 0.265450, 0.242453, 0.234551] # to [0.303161, 0.256327, 0.177548, 0.262963] # due to scikit-learn changing np.random outputs w_cvo_expected = pd.DataFrame([[0.249287, 0.256002, 0.242593, 0.252118], [0.303161, 0.256327, 0.177548, 0.262963]]) # Second line in the below test was changed after v.0.11.3 from [0.257547, 0.265450, 0.242453, 0.234551] # to [0.301132, 0.247042, 0.198597, 0.25323] # due to scikit-learn changing np.random outputs w_nco_expected = pd.DataFrame([[0.248396, 0.243172, 0.250751, 0.257680], [0.301132, 0.247042, 0.198597, 0.25323]]) # Expected weights for maximum Sharpe ratio allocation # Values in the below test was changed after v.0.11.3 from [[-1.081719, 1.810936, 1.218067, 3.978880] # [-2.431651, 0.594868, -0.210175, 5.117628]] # to [[-0.128849, -0.326671, 0.870183, 2.020053] # [-2.095454, -0.7403, 2.390951, 1.793756]] # due to scikit-learn changing np.random outputs w_cvo_sr_expected = pd.DataFrame([[-0.128849, -0.326671, 0.870183, 2.020053], [-2.095454, -0.7403, 2.390951, 1.793756]]) # Values in the below test was changed after v.0.11.3 from [[-1.060835, 1.910503, 1.315026, 3.908128] # [-0.937168, 1.886158, -0.389275, 4.884809]] # to [[-0.204089, -0.050088, 0.912494, 1.983382] # [-2.036145, -0.769203, 2.677561, 1.347365]] # due to scikit-learn changing np.random outputs w_nco_sr_expected = pd.DataFrame([[-0.204089, -0.050088, 0.912494, 1.983382], [-2.036145, -0.769203, 2.677561, 1.347365]]) # Finding the optimal weights for minimum variance w_cvo, w_nco = nco.allocate_mcos(mu_vec, cov_mat, num_obs, num_sims, kde_bwidth, min_var_portf, lw_shrinkage) # Finding the optimal weights for maximum Sharpe ratio w_cvo_sr, w_nco_sr = nco.allocate_mcos(mu_vec, cov_mat, num_obs, num_sims, kde_bwidth_alt, min_var_portf_alt, lw_shrinkage) # Testing if the optimal allocation simulations are right np.testing.assert_almost_equal(np.array(w_cvo), np.array(w_cvo_expected), decimal=6) np.testing.assert_almost_equal(np.array(w_nco), np.array(w_nco_expected), decimal=6) np.testing.assert_almost_equal(np.array(w_cvo_sr), np.array(w_cvo_sr_expected), decimal=6) np.testing.assert_almost_equal(np.array(w_nco_sr), np.array(w_nco_sr_expected), decimal=6)