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

@author: cheng.li
"""

import unittest
import numpy as np
import pandas as pd
from alphamind.portfolio.rankbuilder import rank_build


class TestRankBuild(unittest.TestCase):

    def test_rank_build(self):

        n_samples = 3000
        n_included = 300

        x = np.random.randn(n_samples, 2)

        calc_weights = rank_build(x, n_included)

        expected_weights = np.zeros((len(x), 2))
        masks = (-x).argsort(axis=0).argsort(axis=0) < n_included

        for j in range(x.shape[1]):
            expected_weights[masks[:, j], j] = 1. / n_included

        np.testing.assert_array_almost_equal(calc_weights, expected_weights)

    def test_rank_build_with_group(self):

        n_samples = 3000
        n_include = 10
        n_groups = 30

        x = np.random.randn(n_samples, 2)
        groups = np.random.randint(n_groups, size=n_samples)

        calc_weights = rank_build(x, n_include, groups)

        grouped_ordering = pd.DataFrame(-x).groupby(groups).rank()
        expected_weights = np.zeros((len(x), 2))
        masks = (grouped_ordering <= n_include).values
        choosed = masks.sum(axis=0)
        for j in range(x.shape[1]):
            expected_weights[masks[:, j], j] = 1. / choosed[j]

        np.testing.assert_array_almost_equal(calc_weights, expected_weights)


if __name__ == '__main__':
    unittest.main()<commit_msg>update rank build tests<commit_after># -*- coding: utf-8 -*-
"""
Created on 2017-4-27

@author: cheng.li
"""

import unittest
import numpy as np
import pandas as pd
from alphamind.portfolio.rankbuilder import rank_build


class TestRankBuild(unittest.TestCase):

    def test_rank_build(self):

        n_samples = 3000
        n_included = 300

        n_portfolios = range(10)

        for n_portfolio in n_portfolios:
            x = np.random.randn(n_samples, n_portfolio)

            calc_weights = rank_build(x, n_included)

            expected_weights = np.zeros((len(x), n_portfolio))
            masks = (-x).argsort(axis=0).argsort(axis=0) < n_included

            for j in range(x.shape[1]):
                expected_weights[masks[:, j], j] = 1. / n_included

            np.testing.assert_array_almost_equal(calc_weights, expected_weights)

    def test_rank_build_with_group(self):

        n_samples = 3000
        n_include = 10
        n_groups = 30

        n_portfolios = range(10)

        for n_portfolio in n_portfolios:

            x = np.random.randn(n_samples, n_portfolio)
            groups = np.random.randint(n_groups, size=n_samples)

            calc_weights = rank_build(x, n_include, groups)

            grouped_ordering = pd.DataFrame(-x).groupby(groups).rank()
            expected_weights = np.zeros((len(x), n_portfolio))
            masks = (grouped_ordering <= n_include).values
            choosed = masks.sum(axis=0)
            for j in range(x.shape[1]):
                expected_weights[masks[:, j], j] = 1. / choosed[j]

            np.testing.assert_array_almost_equal(calc_weights, expected_weights)


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.11313518696069032, 'fraction_numerical': 0.021732182806008308, 'mean_word_length': 2.5853379152348226, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4497697', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1013, 'n_words': 206}