コード例 #1
0
    def __init__(self,
                 y,
                 x,
                 window_type='rolling',
                 window=10,
                 intercept=True,
                 nw_lags=None,
                 nw_lags_beta=None,
                 entity_effects=False,
                 time_effects=False,
                 x_effects=None,
                 cluster=None,
                 dropped_dummies={},
                 verbose=False):
        self._window_type = common._get_window_type(window_type)
        self._window = window

        FamaMacBeth.__init__(self,
                             y=y,
                             x=x,
                             intercept=intercept,
                             nw_lags=nw_lags,
                             nw_lags_beta=nw_lags_beta,
                             entity_effects=entity_effects,
                             time_effects=time_effects,
                             x_effects=x_effects,
                             cluster=cluster,
                             dropped_dummies=dropped_dummies,
                             verbose=verbose)

        self._index = self._ols_result._index
        self._T = len(self._index)
コード例 #2
0
    def _set_window(self, window_type, window, min_periods):
        self._window_type = scom._get_window_type(window_type)

        if self._is_rolling:
            assert (window is not None)
            if min_periods is None:
                min_periods = window
        else:
            window = len(self._x)
            if min_periods is None:
                min_periods = 1

        self._window = int(window)
        self._min_periods = min_periods
コード例 #3
0
ファイル: ols.py プロジェクト: 123jefferson/MiniBloq-Sparki
    def _set_window(self, window_type, window, min_periods):
        self._window_type = scom._get_window_type(window_type)

        if self._is_rolling:
            assert(window is not None)
            if min_periods is None:
                min_periods = window
        else:
            window = len(self._x)
            if min_periods is None:
                min_periods = 1

        self._window = int(window)
        self._min_periods = min_periods
コード例 #4
0
ファイル: ols.py プロジェクト: zfs002/pandas
    def _set_window(self, window_type, window, min_periods):
        self._window_type = scom._get_window_type(window_type)

        if self._is_rolling:
            if window is None:
                raise AssertionError("Must specify window.")
            if min_periods is None:
                min_periods = window
        else:
            window = len(self._x)
            if min_periods is None:
                min_periods = 1

        self._window = int(window)
        self._min_periods = min_periods
コード例 #5
0
ファイル: ols.py プロジェクト: Black-Milk/pandas
    def _set_window(self, window_type, window, min_periods):
        self._window_type = scom._get_window_type(window_type)

        if self._is_rolling:
            if window is None:
                raise AssertionError("Must specify window.")
            if min_periods is None:
                min_periods = window
        else:
            window = len(self._x)
            if min_periods is None:
                min_periods = 1

        self._window = int(window)
        self._min_periods = min_periods
コード例 #6
0
ファイル: fama_macbeth.py プロジェクト: Libardo1/pandas
    def __init__(self, y, x, window_type='rolling', window=10,
                 intercept=True, nw_lags=None, nw_lags_beta=None,
                 entity_effects=False, time_effects=False, x_effects=None,
                 cluster=None, dropped_dummies={}, verbose=False):
        self._window_type = common._get_window_type(window_type)
        self._window = window

        FamaMacBeth.__init__(
            self, y=y, x=x, intercept=intercept,
            nw_lags=nw_lags, nw_lags_beta=nw_lags_beta,
            entity_effects=entity_effects, time_effects=time_effects,
            x_effects=x_effects, cluster=cluster,
            dropped_dummies=dropped_dummies, verbose=verbose)

        self._index = self._ols_result._index
        self._T = len(self._index)
コード例 #7
0
ファイル: ols.py プロジェクト: willgrass/pandas
    def _set_window(self, window_type, window, min_periods):
        self._window_type = common._get_window_type(window_type)

        if self._is_rolling:
            if window is None:
                raise Exception('Must pass window when doing rolling '
                                'regression')

            if min_periods is None:
                min_periods = window
        else:
            window = len(self._x)
            if min_periods is None:
                min_periods = 1

        self._window = int(window)
        self._min_periods = min_periods
コード例 #8
0
ファイル: ols.py プロジェクト: timClicks/pandas
    def _set_window(self, window_type, window, min_periods):
        self._window_type = common._get_window_type(window_type)

        if self._is_rolling:
            if window is None:
                raise Exception('Must pass window when doing rolling '
                                'regression')

            if min_periods is None:
                min_periods = window
        else:
            window = len(self._x)
            if min_periods is None:
                min_periods = 1

        self._window = int(window)
        self._min_periods = min_periods
コード例 #9
0
ファイル: interface.py プロジェクト: benracine/pandas
def ols(**kwargs):
    """Returns the appropriate OLS object depending on whether you need
    simple or panel OLS, and a full-sample or rolling/expanding OLS.

    Will be a normal linear regression or a (pooled) panel regression depending
    on the type of the inputs:

    y : Series, x : DataFrame -> OLS
    y : Series, x : dict of DataFrame -> OLS
    y : DataFrame, x : DataFrame -> PanelOLS
    y : DataFrame, x : dict of DataFrame/Panel/LongPanel -> PanelOLS
    y : Series with MultiIndex, x : Panel/LongPanel -> PanelOLS

    Parameters
    ----------
    y: Series or DataFrame
        See above for types
    x: Series, DataFrame, dict of Series, dict of DataFrame, Panel, or
        LongPanel
    weights : Series or ndarray
        The weights are presumed to be (proportional to) the inverse of the
        variance of the observations.  That is, if the variables are to be
        transformed by 1/sqrt(W) you must supply weights = 1/W
    intercept: bool
        True if you want an intercept.  Defaults to True.
    nw_lags: None or int
        Number of Newey-West lags.  Defaults to None.
    nw_overlap: bool
        Whether there are overlaps in the NW lags.  Defaults to False.
    window_type: {'full sample', 'rolling', 'expanding'}
        'full sample' by default
    window: int
        size of window (for rolling/expanding OLS). If window passed and no
        explicit window_type, 'rolling" will be used as the window_type

    Panel OLS options:
        pool: bool
            Whether to run pooled panel regression.  Defaults to true.
        entity_effects: bool
            Whether to account for entity fixed effects.  Defaults to false.
        time_effects: bool
            Whether to account for time fixed effects.  Defaults to false.
        x_effects: list
            List of x's to account for fixed effects.  Defaults to none.
        dropped_dummies: dict
            Key is the name of the variable for the fixed effect.
            Value is the value of that variable for which we drop the dummy.

            For entity fixed effects, key equals 'entity'.

            By default, the first dummy is dropped if no dummy is specified.
        cluster: {'time', 'entity'}
            cluster variances

    Examples
    --------
    # Run simple OLS.
    result = ols(y=y, x=x)

    # Run rolling simple OLS with window of size 10.
    result = ols(y=y, x=x, window_type='rolling', window=10)
    print result.beta

    result = ols(y=y, x=x, nw_lags=1)

    # Set up LHS and RHS for data across all items
    y = A
    x = {'B' : B, 'C' : C}

    # Run panel OLS.
    result = ols(y=y, x=x)

    # Run expanding panel OLS with window 10 and entity clustering.
    result = ols(y=y, x=x, cluster='entity', window_type='expanding', window=10)

    Returns
    -------
    The appropriate OLS object, which allows you to obtain betas and various
    statistics, such as std err, t-stat, etc.
    """
    pool = kwargs.get('pool')
    if 'pool' in kwargs:
        del kwargs['pool']

    window_type = kwargs.get('window_type')
    window = kwargs.get('window')

    if window_type is None:
        if window is None:
            window_type = common.FULL_SAMPLE
        else:
            window_type = common.ROLLING
    else:
        window_type = common._get_window_type(window_type)

    if window_type != common.FULL_SAMPLE:
        kwargs['window_type'] = common._get_window_type_name(window_type)

    y = kwargs.get('y')
    x = kwargs.get('x')

    panel = False
    if isinstance(y, DataFrame) or (isinstance(y, Series) and
                                    isinstance(y.index, MultiIndex)):
        panel = True
    if isinstance(x, (Panel, LongPanel)):
        panel = True

    if window_type == common.FULL_SAMPLE:
        for rolling_field in ('window_type', 'window', 'min_periods'):
            if rolling_field in kwargs:
                del kwargs[rolling_field]

        if panel:
            if pool == False:
                klass = NonPooledPanelOLS
            else:
                klass = PanelOLS
        else:
            klass = OLS
    else:
        if panel:
            if pool == False:
                klass = NonPooledPanelOLS
            else:
                klass = MovingPanelOLS
        else:
            klass = MovingOLS

    return klass(**kwargs)
コード例 #10
0
def ols(**kwargs):
    """Returns the appropriate OLS object depending on whether you need
    simple or panel OLS, and a full-sample or rolling/expanding OLS.

    Will be a normal linear regression or a (pooled) panel regression depending
    on the type of the inputs:

    y : Series, x : DataFrame -> OLS
    y : Series, x : dict of DataFrame -> OLS
    y : DataFrame, x : DataFrame -> PanelOLS
    y : DataFrame, x : dict of DataFrame/Panel -> PanelOLS
    y : Series with MultiIndex, x : Panel/DataFrame + MultiIndex -> PanelOLS

    Parameters
    ----------
    y: Series or DataFrame
        See above for types
    x: Series, DataFrame, dict of Series, dict of DataFrame, Panel
    weights : Series or ndarray
        The weights are presumed to be (proportional to) the inverse of the
        variance of the observations.  That is, if the variables are to be
        transformed by 1/sqrt(W) you must supply weights = 1/W
    intercept: bool
        True if you want an intercept.  Defaults to True.
    nw_lags: None or int
        Number of Newey-West lags.  Defaults to None.
    nw_overlap: bool
        Whether there are overlaps in the NW lags.  Defaults to False.
    window_type: {'full sample', 'rolling', 'expanding'}
        'full sample' by default
    window: int
        size of window (for rolling/expanding OLS). If window passed and no
        explicit window_type, 'rolling" will be used as the window_type

    Panel OLS options:
        pool: bool
            Whether to run pooled panel regression.  Defaults to true.
        entity_effects: bool
            Whether to account for entity fixed effects.  Defaults to false.
        time_effects: bool
            Whether to account for time fixed effects.  Defaults to false.
        x_effects: list
            List of x's to account for fixed effects.  Defaults to none.
        dropped_dummies: dict
            Key is the name of the variable for the fixed effect.
            Value is the value of that variable for which we drop the dummy.

            For entity fixed effects, key equals 'entity'.

            By default, the first dummy is dropped if no dummy is specified.
        cluster: {'time', 'entity'}
            cluster variances

    Examples
    --------
    # Run simple OLS.
    result = ols(y=y, x=x)

    # Run rolling simple OLS with window of size 10.
    result = ols(y=y, x=x, window_type='rolling', window=10)
    print result.beta

    result = ols(y=y, x=x, nw_lags=1)

    # Set up LHS and RHS for data across all items
    y = A
    x = {'B' : B, 'C' : C}

    # Run panel OLS.
    result = ols(y=y, x=x)

    # Run expanding panel OLS with window 10 and entity clustering.
    result = ols(y=y, x=x, cluster='entity', window_type='expanding', window=10)

    Returns
    -------
    The appropriate OLS object, which allows you to obtain betas and various
    statistics, such as std err, t-stat, etc.
    """
    pool = kwargs.get('pool')
    if 'pool' in kwargs:
        del kwargs['pool']

    window_type = kwargs.get('window_type')
    window = kwargs.get('window')

    if window_type is None:
        if window is None:
            window_type = 'full_sample'
        else:
            window_type = 'rolling'
    else:
        window_type = common._get_window_type(window_type)

    if window_type != 'full_sample':
        kwargs['window_type'] = common._get_window_type(window_type)

    y = kwargs.get('y')
    x = kwargs.get('x')

    panel = False
    if isinstance(y, DataFrame) or (isinstance(y, Series)
                                    and isinstance(y.index, MultiIndex)):
        panel = True
    if isinstance(x, Panel):
        panel = True

    if window_type == 'full_sample':
        for rolling_field in ('window_type', 'window', 'min_periods'):
            if rolling_field in kwargs:
                del kwargs[rolling_field]

        if panel:
            if pool == False:
                klass = NonPooledPanelOLS
            else:
                klass = PanelOLS
        else:
            klass = OLS
    else:
        if panel:
            if pool == False:
                klass = NonPooledPanelOLS
            else:
                klass = MovingPanelOLS
        else:
            klass = MovingOLS

    return klass(**kwargs)
コード例 #11
0
ファイル: interface.py プロジェクト: timClicks/pandas
def ols(**kwargs):
    """Returns the appropriate OLS object depending on whether you need
    simple or panel OLS, and a full-sample or rolling/expanding OLS.

    Parameters
    ----------
    y: Series for simple OLS.  DataFrame for panel OLS.
    x: Series, DataFrame, or dict of Series for simple OLS.
       Dict of DataFrame for panel OLS.
    intercept: bool
        True if you want an intercept.  Defaults to True.
    nw_lags: None or int
        Number of Newey-West lags.  Defaults to None.
    nw_overlap: bool
        Whether there are overlaps in the NW lags.  Defaults to False.
    window_type: {'full sample', 'rolling', 'expanding'}
        'full sample' by default
    window: int
        size of window (for rolling/expanding OLS). If window passed and no
        explicit window_type, 'rolling" will be used as the window_type

    Panel OLS options:
        pool: bool
            Whether to run pooled panel regression.  Defaults to true.
        weights: DataFrame
            Weight for each observation.  The weights are not normalized;
            they're multiplied directly by each observation.
        entity_effects: bool
            Whether to account for entity fixed effects.  Defaults to false.
        time_effects: bool
            Whether to account for time fixed effects.  Defaults to false.
        x_effects: list
            List of x's to account for fixed effects.  Defaults to none.
        dropped_dummies: dict
            Key is the name of the variable for the fixed effect.
            Value is the value of that variable for which we drop the dummy.

            For entity fixed effects, key equals 'entity'.

            By default, the first dummy is dropped if no dummy is specified.
        cluster: {'time', 'entity'}
            cluster variances

    Returns
    -------
    The appropriate OLS object, which allows you to obtain betas and various
    statistics, such as std err, t-stat, etc.

    Examples
    --------
    # Run simple OLS.
    result = ols(y=y, x=x)

    # Run rolling simple OLS with window of size 10.
    result = ols(y=y, x=x, window_type='rolling', window=10)
    print result.beta

    result = ols(y=y, x=x, nw_lags=1)

    # Set up LHS and RHS for data across all items
    y = A
    x = {'B' : B, 'C' : C}

    # Run panel OLS.
    result = ols(y=y, x=x)

    # Run expanding panel OLS with window 10 and entity clustering.
    result = ols(y=y, x=x, cluster='entity', window_type='expanding', window=10)
    """
    pool = kwargs.get('pool')
    if 'pool' in kwargs:
        del kwargs['pool']

    window_type = kwargs.get('window_type')
    window = kwargs.get('window')

    if window_type is None:
        if window is None:
            window_type = common.FULL_SAMPLE
        else:
            window_type = common.ROLLING
    else:
        window_type = common._get_window_type(window_type)

    if window_type != common.FULL_SAMPLE:
        kwargs['window_type'] = common._get_window_type_name(window_type)

    y = kwargs.get('y')
    if window_type == common.FULL_SAMPLE:
        # HACK (!)
        for rolling_field in ('window_type', 'window', 'min_periods'):
            if rolling_field in kwargs:
                del kwargs[rolling_field]

        if isinstance(y, Series):
            klass = OLS
        else:
            if pool == False:
                klass = NonPooledPanelOLS
            else:
                klass = PanelOLS
    else:
        if isinstance(y, Series):
            klass = MovingOLS
        else:
            if pool == False:
                klass = NonPooledPanelOLS
            else:
                klass = MovingPanelOLS

    return klass(**kwargs)
コード例 #12
0
def ols(**kwargs):
    """Returns the appropriate OLS object depending on whether you need
    simple or panel OLS, and a full-sample or rolling/expanding OLS.

    Parameters
    ----------
    y: Series for simple OLS.  DataFrame for panel OLS.
    x: Series, DataFrame, or dict of Series for simple OLS.
       Dict of DataFrame for panel OLS.
    intercept: bool
        True if you want an intercept.  Defaults to True.
    nw_lags: None or int
        Number of Newey-West lags.  Defaults to None.
    nw_overlap: bool
        Whether there are overlaps in the NW lags.  Defaults to False.
    window_type: int
        FULL_SAMPLE, ROLLING, EXPANDING.  FULL_SAMPLE by default.
    window: int
        size of window (for rolling/expanding OLS)

    Panel OLS options:
        pool: bool
            Whether to run pooled panel regression.  Defaults to true.
        weights: DataFrame
            Weight for each observation.  The weights are not normalized;
            they're multiplied directly by each observation.
        entity_effects: bool
            Whether to account for entity fixed effects.  Defaults to false.
        time_effects: bool
            Whether to account for time fixed effects.  Defaults to false.
        x_effects: list
            List of x's to account for fixed effects.  Defaults to none.
        dropped_dummies: dict
            Key is the name of the variable for the fixed effect.
            Value is the value of that variable for which we drop the dummy.

            For entity fixed effects, key equals 'entity'.

            By default, the first dummy is dropped if no dummy is specified.
        cluster: {'time', 'entity'}
            cluster variances

    Returns
    -------
    The appropriate OLS object, which allows you to obtain betas and various
    statistics, such as std err, t-stat, etc.

    Examples
    --------
    # Run simple OLS.
    result = ols(y=y, x=x)

    # Run rolling simple OLS with window of size 10.
    result = ols(y=y, x=x, window_type=ROLLING, window=10)
    print result.beta

    result = ols(y=y, x=x, nw_lags=1)

    # Set up LHS and RHS for data across all items
    y = A
    x = {'B' : B, 'C' : C}

    # Run panel OLS.
    result = ols(y=y, x=x)

    # Run expanding panel OLS with window 10 and entity clustering.
    result = ols(y=y, x=x, cluster=ENTITY, window_type=EXPANDING, window=10)
    """
    try:
        import scipy as _
    except ImportError:
        raise Exception('Must install SciPy to use OLS functionality')

    pool = kwargs.get('pool')
    if 'pool' in kwargs:
        del kwargs['pool']

    window_type = kwargs.get('window_type', common.FULL_SAMPLE)
    window_type = common._get_window_type(window_type)

    y = kwargs.get('y')
    if window_type == common.FULL_SAMPLE:
        for rolling_field in ('window_type', 'window', 'min_periods'):
            if rolling_field in kwargs:
                del kwargs[rolling_field]

        if isinstance(y, Series):
            klass = OLS
        else:
            if pool == False:
                klass = NonPooledPanelOLS
            else:
                klass = PanelOLS
    else:
        if isinstance(y, Series):
            klass = MovingOLS
        else:
            if pool == False:
                klass = NonPooledPanelOLS
            else:
                klass = MovingPanelOLS

    return klass(**kwargs)