Repository: quantopian/zipline
File: zipline/pipeline/factors/statistical.py (Python)

Code before:
            allowed_missing_count=allowed_missing_count,
        )

    def compute(self,
                today,
                assets,
                out,
                all_returns,
                target_returns,
                allowed_missing_count):
        vectorized_beta(
            dependents=all_returns,
            independent=target_returns,
            allowed_missing=allowed_missing_count,
            out=out,
        )

    def __repr__(self):
        return "{}(window_length={}, allowed_missing={})".format(
            type(self).__name__,
            self.window_length,
            self.params['allowed_missing'],
        )


def vectorized_beta(dependents, independent, allowed_missing, out=None):
    """
    Compute slopes of linear regressions between columns of ``dependents`` and
    ``independent``.

    Parameters
    ----------
    dependents : np.array[N, M]
        Array with columns of data to be regressed against ``independent``.
    independent : np.array[N, 1]
        Independent variable of the regression
    allowed_missing : int
        Number of allowed missing (NaN) observations per column. Columns with
        more than this many non-nan observations in both ``dependents`` and
        ``independents`` will output NaN as the regression coefficient.

    Returns
    -------
    slopes : np.array[M]
        Linear regression coefficients for each column of ``dependents``.
    """
    # Cache these as locals since we're going to call them multiple times.
    nan = np.nan
    isnan = np.isnan
    N, M = dependents.shape


Review comment (suggestion):
I couldn't find another place where `vectorized_beta` might be useful at the moment, however at a later date we might want to move it to `empyrical`.

Code after:
            inputs=[daily_returns, daily_returns[target]],
            window_length=regression_length,
            allowed_missing_count=allowed_missing_count,
        )

    def compute(self,
                today,
                assets,
                out,
                all_returns,
                target_returns,
                allowed_missing_count):
        vectorized_beta(
            dependents=all_returns,
            independent=target_returns,
            allowed_missing=allowed_missing_count,
            out=out,
        )

    def short_repr(self):
        return "{}({!r}, {}, {})".format(
            type(self).__name__,
            str(self.target.symbol),  # coerce from unicode to str in py2.
            self.window_length,
            self.params['allowed_missing_count'],
        )

    @property
    def target(self):
        """Get the target of the beta calculation.
        """
        return self.inputs[1].asset

    def __repr__(self):
        return "{}({}, length={}, allowed_missing={})".format(
            type(self).__name__,
            self.target,
            self.window_length,
            self.params['allowed_missing_count'],
        )


def vectorized_beta(dependents, independent, allowed_missing, out=None):
    """
    Compute slopes of linear regressions between columns of ``dependents`` and
    ``independent``.

    Parameters
    ----------
    dependents : np.array[N, M]
        Array with columns of data to be regressed against ``independent``.