def test_ignore_objective(self): # test for https://github.com/matthewgilbert/erc/issues/1 cov = np.array( [[1.943718516077124966e-02, -2.439418300721416140e-03, 4.698396459820822213e-02], # NOQA [-2.439418300721416140e-03, 2.421052572519334818e-03, -6.379692612692000608e-03], # NOQA [4.698396459820822213e-02, -6.379692612692000608e-03, 1.757242094792315912e-01]] # NOQA ) # do not ignore the objective self.assertRaises( RuntimeError, erc.calc_weights, cov, options={'ftol': 1e-20, 'maxiter': 100}, scale_factor=1000, pcr_tolerance=0.001, ignore_objective=False ) # ignore object, tolerance not met self.assertRaises( RuntimeError, erc.calc_weights, cov, options={'ftol': 1e-20, 'maxiter': 100}, scale_factor=1000, pcr_tolerance=1e-12, ignore_objective=True ) # ignore object, tolerance met weights = erc.calc_weights( cov, options={'ftol': 1e-20, 'maxiter': 100}, scale_factor=1000, pcr_tolerance=0.001, ignore_objective=True ) pcr = weights.dot(cov) * weights / (weights.dot(cov).dot(weights)) pcr_exp = np.array([0.333, 0.333, 0.333]) assert_almost_equal(pcr, pcr_exp, decimal=3)
def test_cov_100(self): np.random.seed(42) # this will be PSD in general, for this seed PD was checked manually A = np.random.randn(100, 100) cov = A.dot(A.transpose()) res = erc.calc_weights(cov) pcr = res.dot(cov) * res / (res.dot(cov).dot(res)) pcr_exp = np.ones(100) / 100 assert_almost_equal(pcr, pcr_exp, decimal=6)
def test_callback_kwarg(self): """ Test for https://github.com/matthewgilbert/erc/issues/2 """ # Define a callback that immediately raises some special exception class SpecialException(Exception): pass def special_callback(*args): raise SpecialException cov = np.array([[2, 0], [0, 1]]) self.assertRaises(SpecialException, erc.calc_weights(cov, callback=special_callback))
def test_empirical_cov(self): cov = [[0.2009260436486249, 0.12707930451448998, -0.03069428002429466, -0.012189826518922382, 0.0019550406194923146, 0.1079776463792598, 0.07222202909410275, 0.0015933066081580752, 0.035776811740586924, 0.011257648065556939, 0.12498609259380353, 0.1133968452505988, 0.09432703378407521], # NOQA [0.12707930451448998, 0.14399932234276958, -0.033005462696590965, -0.013248779686775066, 0.011734167413457848, 0.116925500894747, 0.08560344469314012, 0.0035111122690138943, 0.038040451991215654, 0.013384152818939636, 0.1054178149693092, 0.11682775419715774, 0.08743423986163756], # NOQA [-0.03069428002429466, -0.033005462696590965, 0.0320498964544349, 0.018653824434561317, 0.0032689838036211983, -0.03130875403872759, -0.024673866343981683, 0.006452064039219779, -0.011215421112378787, -0.002670652714457522, -0.029560575694946092, -0.03221376568807623, -0.023822789110986512], # NOQA [-0.012189826518922382, -0.013248779686775066, 0.018653824434561317, 0.016556954296296132, 0.005964650800070247, -0.014033253346029553, -0.009836235339080417, 0.004801082202228667, -0.004982919661605619, 9.693343216181816e-05, -0.014961298918431695, -0.016999498829136282, -0.012047278172090711], # NOQA [0.0019550406194923146, 0.011734167413457848, 0.0032689838036211983, 0.005964650800070247, 0.04608779060756199, 0.014326023025826647, 0.0247718938210618, 0.0009476582667332079, 0.0014362153036267663, 0.003550650975489107, 0.0018336230941195717, 0.009179932223558684, 0.0022857090263957843], # NOQA [0.1079776463792598, 0.116925500894747, -0.03130875403872759, -0.014033253346029553, 0.014326023025826647, 0.1266993516568441, 0.08128639652621818, 0.002381830118329238, 0.03414730525470721, 0.011563617088933106, 0.09584989025844964, 0.10452916458435678, 0.079856640071604], # NOQA [0.07222202909410275, 0.08560344469314012, -0.024673866343981683, -0.009836235339080417, 0.0247718938210618, 0.08128639652621818, 0.12994848068717174, 0.0013662059790803623, 0.028211397949515167, 0.008794117786048519, 0.062467848936725544, 0.07627050652929292, 0.05161306881807267], # NOQA [0.0015933066081580752, 0.0035111122690138943, 0.006452064039219779, 0.004801082202228667, 0.0009476582667332079, 0.002381830118329238, 0.0013662059790803623, 0.008634392784989753, 0.003930197262121238, 0.002828477463827138, 0.001184986407957077, 0.0027338162940507005, 0.0013219770261998242], # NOQA [0.035776811740586924, 0.038040451991215654, -0.011215421112378787, -0.004982919661605619, 0.0014362153036267663, 0.03414730525470721, 0.028211397949515167, 0.003930197262121238, 0.030892646274119077, 0.008304416255595477, 0.03080767889567698, 0.03665248143348694, 0.026481978278886542], # NOQA [0.011257648065556939, 0.013384152818939636, -0.002670652714457522, 9.693343216181816e-05, 0.003550650975489107, 0.011563617088933106, 0.008794117786048519, 0.002828477463827138, 0.008304416255595477, 0.01764487384110859, 0.010835968094312372, 0.012322133150778545, 0.008918140414461703], # NOQA [0.12498609259380353, 0.1054178149693092, -0.029560575694946092, -0.014961298918431695, 0.0018336230941195717, 0.09584989025844964, 0.062467848936725544, 0.001184986407957077, 0.03080767889567698, 0.010835968094312372, 0.10751610449599577, 0.09668495392638546, 0.07803114829087793], # NOQA [0.1133968452505988, 0.11682775419715774, -0.03221376568807623, -0.016999498829136282, 0.009179932223558684, 0.10452916458435678, 0.07627050652929292, 0.0027338162940507005, 0.03665248143348694, 0.012322133150778545, 0.09668495392638546, 0.11969041342062207, 0.08027084555866724], # NOQA [0.09432703378407521, 0.08743423986163756, -0.023822789110986512, -0.012047278172090711, 0.0022857090263957843, 0.079856640071604, 0.05161306881807267, 0.0013219770261998242, 0.026481978278886542, 0.008918140414461703, 0.07803114829087793, 0.08027084555866724, 0.06393608726201636]] # NOQA cov = np.array(cov) res = erc.calc_weights(cov) pcr = res.dot(cov) * res / (res.dot(cov).dot(res)) pcr_exp = np.ones(len(pcr)) / len(pcr) assert_almost_equal(pcr, pcr_exp, decimal=6)
def test_cov_2(self): cov = np.array([[2, 0], [0, 1]]) res = erc.calc_weights(cov) pcr = res.dot(cov) * res / (res.dot(cov).dot(res)) pcr_exp = np.ones(2) / 2 assert_almost_equal(pcr, pcr_exp)
def solve_erc(): erc.calc_weights(cov)