<commit_before># -*- coding: utf-8 -*-
"""
Created on 2017-5-5

@author: cheng.li
"""

import unittest
import numpy as np
from alphamind.portfolio.linearbuilder import linear_build


class TestLinearBuild(unittest.TestCase):

    def setUp(self):
        self.er = np.random.randn(3000)
        self.risk_exp = np.random.randn(3000, 30)
        self.bm = np.random.randint(100, size=3000).astype(float)

    def test_linear_build(self):
        bm = self.bm / self.bm.sum()
        eplson = 1e-6

        status, value, w = linear_build(self.er, 0., 0.01, self.risk_exp, bm)
        self.assertEqual(status, 'optimal')
        self.assertAlmostEqual(np.sum(w), 1.)
        self.assertTrue(np.all(w <= 0.01 + eplson))
        self.assertTrue(np.all(w >= -eplson))

        calc_risk = (w - bm) @self. risk_exp
        expected_risk = np.zeros(self.risk_exp.shape[1])
        np.testing.assert_array_almost_equal(calc_risk, expected_risk)

    def test_linear_build_with_inequality_constraints(self):
        bm = self.bm / self.bm.sum()
        eplson = 1e-6

        status, value, w = linear_build(self.er,
                                        0.,
                                        0.01,
                                        self.risk_exp,
                                        bm,
                                        risk_target=(-1e-2, 1e-2))
        self.assertEqual(status, 'optimal')
        self.assertAlmostEqual(np.sum(w), 1.)
        self.assertTrue(np.all(w <= 0.01 + eplson))
        self.assertTrue(np.all(w >= -eplson))

        calc_risk = (w - bm) @ self.risk_exp / np.abs(bm @ self.risk_exp)
        self.assertTrue(np.all(np.abs(calc_risk) <= 1e-2))


if __name__ == '__main__':
    unittest.main()<commit_msg>fixed unused variable issue<commit_after># -*- coding: utf-8 -*-
"""
Created on 2017-5-5

@author: cheng.li
"""

import unittest
import numpy as np
from alphamind.portfolio.linearbuilder import linear_build


class TestLinearBuild(unittest.TestCase):

    def setUp(self):
        self.er = np.random.randn(3000)
        self.risk_exp = np.random.randn(3000, 30)
        self.bm = np.random.randint(100, size=3000).astype(float)

    def test_linear_build(self):
        bm = self.bm / self.bm.sum()
        eplson = 1e-6

        status, _, w = linear_build(self.er, 0., 0.01, self.risk_exp, bm)
        self.assertEqual(status, 'optimal')
        self.assertAlmostEqual(np.sum(w), 1.)
        self.assertTrue(np.all(w <= 0.01 + eplson))
        self.assertTrue(np.all(w >= -eplson))

        calc_risk = (w - bm) @self. risk_exp
        expected_risk = np.zeros(self.risk_exp.shape[1])
        np.testing.assert_array_almost_equal(calc_risk, expected_risk)

    def test_linear_build_with_inequality_constraints(self):
        bm = self.bm / self.bm.sum()
        eplson = 1e-6

        status, _, w = linear_build(self.er,
                                        0.,
                                        0.01,
                                        self.risk_exp,
                                        bm,
                                        risk_target=(-1e-2, 1e-2))
        self.assertEqual(status, 'optimal')
        self.assertAlmostEqual(np.sum(w), 1.)
        self.assertTrue(np.all(w <= 0.01 + eplson))
        self.assertTrue(np.all(w >= -eplson))

        calc_risk = (w - bm) @ self.risk_exp / np.abs(bm @ self.risk_exp)
        self.assertTrue(np.all(np.abs(calc_risk) <= 1e-2))


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.1320754716981132, 'fraction_numerical': 0.029159519725557463, 'mean_word_length': 1.8354943273905997, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3621914', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1139, 'n_words': 202}