'Tests for `Factor.linear_regression` when passed another 2D factor
instead of a Slice.'

@parameter_space(regression_length=[2, 3, 4])
def test_factor_regression_method_two_factors(self, regression_length):

    assets = self.assets
    dates = self.dates
    start_date = self.pipeline_start_date
    end_date = self.pipeline_end_date
    start_date_index = self.start_date_index
    end_date_index = self.end_date_index
    num_days = self.num_days
    run_pipeline = self.run_pipeline
    outputs = ['beta', 'alpha', 'r_value', 'p_value', 'stderr']
    returns_masked_1 = Returns(window_length=5, inputs=[self.col], mask=AssetID().eq(1))
    returns_masked_2 = Returns(window_length=5, inputs=[self.col], mask=AssetID().eq(2))
    with self.assertRaises(IncompatibleTerms):
        returns_masked_1.linear_regression(target=returns_masked_2, regression_length=regression_length)
    returns_5 = Returns(window_length=5, inputs=[self.col])
    returns_10 = Returns(window_length=10, inputs=[self.col])
    regression_factor = returns_5.linear_regression(target=returns_10, regression_length=regression_length)
    columns = {output: getattr(regression_factor, output) for output in outputs}
    pipeline = Pipeline(columns=columns)
    results = run_pipeline(pipeline, start_date, end_date)
    output_results = {}
    expected_output_results = {}
    for output in outputs:
        output_results[output] = results[output].unstack()
        expected_output_results[output] = full_like(output_results[output], nan)
    columns = {'returns_5': returns_5, 'returns_10': returns_10}
    results = run_pipeline(Pipeline(columns=columns), dates[(start_date_index - (regression_length - 1))], dates[end_date_index])
    returns_5_results = results['returns_5'].unstack()
    returns_10_results = results['returns_10'].unstack()
    for day in range(num_days):
        todays_returns_5 = returns_5_results.iloc[day:(day + regression_length)]
        todays_returns_10 = returns_10_results.iloc[day:(day + regression_length)]
        for (asset, asset_returns_5) in todays_returns_5.iteritems():
            asset_column = (int(asset) - 1)
            asset_returns_10 = todays_returns_10[asset]
            expected_regression_results = linregress(y=asset_returns_5, x=asset_returns_10)
            for (i, output) in enumerate(outputs):
                expected_output_results[output][(day, asset_column)] = expected_regression_results[i]
    for output in outputs:
        output_result = output_results[output]
        expected_output_result = DataFrame(expected_output_results[output], index=dates[start_date_index:(end_date_index + 1)], columns=assets)
        assert_frame_equal(output_result, expected_output_result)
