def test_percent_build_with_masks(self): for n_portfolio in self.n_portfolios: x = np.random.randn(self.n_samples, n_portfolio) choices = np.random.choice(self.n_samples, self.n_mask, replace=False) masks = np.full(self.n_samples, True, dtype=bool) masks[choices] = False calc_weights = percent_build(x, self.p_included, masks=masks) expected_weights = np.zeros((len(x), n_portfolio)) filtered_index = np.arange(len(x))[masks] filtered_x = x[masks] big_boolen = np.full(x.shape, False, dtype=bool) n_included = int(self.p_included * len(x)) chosen = (-filtered_x).argsort(axis=0).argsort(axis=0) < n_included big_boolen[filtered_index] = chosen for j in range(x.shape[1]): expected_weights[big_boolen[:, j], j] = 1. np.testing.assert_array_almost_equal(calc_weights, expected_weights)
def benchmark_build_percent_with_group(n_samples: int, n_loops: int, p_included: float, n_groups: int) -> None: print("-" * 60) print("Starting portfolio construction by percent with group-by values benchmarking") print("Parameters(n_samples: {0}, p_included: {1}, n_loops: {2}, n_groups: {3})".format(n_samples, p_included, n_loops, n_groups)) n_portfolio = 10 x = np.random.randn(n_samples, n_portfolio) groups = np.random.randint(n_groups, size=n_samples) start = dt.datetime.now() for _ in range(n_loops): calc_weights = percent_build(x, p_included, groups=groups) impl_model_time = dt.datetime.now() - start print('{0:20s}: {1}'.format('Implemented model', impl_model_time)) start = dt.datetime.now() for _ in range(n_loops): grouped_ordering = pd.DataFrame(-x).groupby(groups).rank() grouped_count = pd.DataFrame(-x).groupby(groups).transform(lambda x: x.count()) exp_weights = np.zeros((len(x), n_portfolio)) n_included = (grouped_count * p_included).astype(int) masks = (grouped_ordering <= n_included).values for j in range(n_portfolio): exp_weights[masks[:, j], j] = 1. benchmark_model_time = dt.datetime.now() - start np.testing.assert_array_almost_equal(calc_weights, exp_weights) print('{0:20s}: {1}'.format('Benchmark model', benchmark_model_time))
def benchmark_build_percent(n_samples: int, n_loops: int, p_included: float) -> None: print("-" * 60) print("Starting portfolio construction by percent benchmarking") print("Parameters(n_samples: {0}, p_included: {1}, n_loops: {2})".format(n_samples, p_included, n_loops)) n_portfolio = 10 x = np.random.randn(n_samples, n_portfolio) start = dt.datetime.now() for _ in range(n_loops): calc_weights = percent_build(x, p_included) impl_model_time = dt.datetime.now() - start print('{0:20s}: {1}'.format('Implemented model', impl_model_time)) start = dt.datetime.now() for _ in range(n_loops): exp_weights = np.zeros((len(x), n_portfolio)) n_incuded = int(p_included * len(x)) choosed_index = (-x).argsort(axis=0).argsort(axis=0) < n_incuded for j in range(n_portfolio): exp_weights[choosed_index[:, j], j] = 1. benchmark_model_time = dt.datetime.now() - start np.testing.assert_array_almost_equal(calc_weights, exp_weights) print('{0:20s}: {1}'.format('Benchmark model', benchmark_model_time))
def test_percent_build(self): n_include = int(self.n_samples * self.p_included) for n_portfolio in self.n_portfolios: x = np.random.randn(self.n_samples, n_portfolio) calc_weights = percent_build(x, self.p_included) expected_weights = np.zeros((len(x), n_portfolio)) masks = (-x).argsort(axis=0).argsort(axis=0) < n_include for j in range(x.shape[1]): expected_weights[masks[:, j], j] = 1. np.testing.assert_array_almost_equal(calc_weights, expected_weights)
def test_percent_build_with_group(self): for n_portfolio in self.n_portfolios: x = np.random.randn(self.n_samples, n_portfolio) groups = np.random.randint(self.n_groups, size=self.n_samples) calc_weights = percent_build(x, self.p_included, groups) grouped_ordering = pd.DataFrame(-x).groupby(groups).rank() grouped_count = pd.DataFrame(-x).groupby(groups).transform(lambda x: x.count()) expected_weights = np.zeros((len(x), n_portfolio)) n_include = (grouped_count * self.p_included).astype(int) masks = (grouped_ordering <= n_include).values for j in range(x.shape[1]): expected_weights[masks[:, j], j] = 1. np.testing.assert_array_almost_equal(calc_weights, expected_weights)
def build_portfolio(er: np.ndarray, builder: Optional[str] = 'long_short', **kwargs) -> np.ndarray: builder = builder.lower() if builder == 'ls' or builder == 'long_short': return long_short_build(er, **kwargs).flatten() elif builder == 'rank': return rank_build(er, **kwargs).flatten() elif builder == 'percent': return percent_build(er, **kwargs).flatten() elif builder == 'linear_prog' or builder == 'linear': status, _, weight = linear_build(er, **kwargs) if status != 'optimal': raise ValueError( 'linear programming optimizer in status: {0}'.format(status)) else: return weight