Ejemplo n.º 1
0
    def generate_yields(self, var_data, adjusted=False):
        """
        Returns DataFrame of yields given var_data.

        Assumes that var_data begins with last lag elemnt of VAR

        Parameters
        ----------
        var_data : DataFrame
            data for var model
        adjusted : boolean
            Indicates whether data for VAR has already been transformed into
            VAR(1)
        """
        assert len(var_data) == len(var_data.dropna()), \
            "Null values in var_data"

        neqs = self.model.neqs
        k_ar = self.model.k_ar
        mats = self.model.mats
        # generate affine prediction coefficients
        a_rsk, b_rsk = self.model.gen_pred_coef(lam_0=self.lam_0,
                                                lam_1=self.lam_1,
                                                delta_0=self.delta_0,
                                                delta_1=self.delta_1,
                                                mu=self.mu,
                                                phi=self.phi,
                                                sigma=self.sigma,
                                                dtype=self.dtype)
        if not adjusted:
            var_data = transform_var1(var_data, k_ar)
        var_data_vert = var_data[var_data.columns[:-neqs]]
        yields = pa.DataFrame(index=var_data_vert.index)
        for mat in mats:
            yields[str(mat) + '_pred'] = a_rsk[mat - 1] + \
                    np.dot(b_rsk[mat - 1], var_data_vert.values.T)
        return yields
Ejemplo n.º 2
0
    def generate_yields(self, var_data, adjusted=False):
        """
        Returns DataFrame of yields given var_data.

        Assumes that var_data begins with last lag elemnt of VAR

        Parameters
        ----------
        var_data : DataFrame
            data for var model
        adjusted : boolean
            Indicates whether data for VAR has already been transformed into
            VAR(1)
        """
        assert len(var_data) == len(var_data.dropna()), \
            "Null values in var_data"

        neqs = self.model.neqs
        k_ar = self.model.k_ar
        mats = self.model.mats
        # generate affine prediction coefficients
        a_rsk, b_rsk = self.model.gen_pred_coef(lam_0=self.lam_0,
                                                lam_1=self.lam_1,
                                                delta_0=self.delta_0,
                                                delta_1=self.delta_1,
                                                mu=self.mu, phi=self.phi,
                                                sigma=self.sigma,
                                                dtype=self.dtype)
        if not adjusted:
            var_data = transform_var1(var_data, k_ar)
        var_data_vert = var_data[var_data.columns[:-neqs]]
        yields = pa.DataFrame(index=var_data_vert.index)
        for mat in mats:
            yields[str(mat) + '_pred'] = a_rsk[mat - 1] + \
                    np.dot(b_rsk[mat - 1], var_data_vert.values.T)
        return yields
Ejemplo n.º 3
0
    def __init__(self, yc_data, var_data, mats, lam_0_e, lam_1_e, delta_0_e,
                 delta_1_e, mu_e, phi_e, sigma_e, latent=0, k_ar=None,
                 neqs=None, no_err=None, adjusted=False, use_C_extension=True):
        """
        Attempts to instantiate an affine model object
        yc_data : DataFrame
            yield curve data
        var_data : DataFrame
            data for var model (pass None if no observed factors)
        mats : list of int
            Maturities in periods of yields included in yc_data
        latent: int
            Number of latent variables to estimate
        no_err : list of ints
            list of the column indexes of yields to be measured without
            error
            ex: [0, 3, 4]
            (1st, 4th, and 5th columns in yc_data to be estimated
            without error)
        adjusted : boolean
            Indicates whether data for VAR has already been transformed into
            VAR(1)
        use_C_extension : boolean
            Indicates whether to use C extension
        k_ar : int
            number of lags for VAR system
            Only respected when adjusted=False and var_data not None
        neqs : int
            Number of observed factors included
            If no observed factors, then set to 0
            Only respected when adjusted=True

        For all estimate parameter arrays:
            elements marked with 'E' or 'e' are estimated
            n = number of variables in fully-specified VAR(1) at t

        lam_0_e : Numpy masked array, n x 1
            constant vector of risk pricing equation
        lam_1_e : Numpy masked array, n x n
            parameter array of risk pricing equation
        delta_0_e : Numpy masked array, 1 x 1
            constant in short-rate equation
        delta_1_e : Numpy masked array, n x 1
            parameter vector in short-rate equation
        mu_e : Numpy masked array, n x 1
            constant vector for VAR process
        phi_e : Numpy masked array, n x n
            parameter array for VAR process
        sigma_e : Numpy masked array, n x n
            covariance array for VAR process
        """
        self.yc_data = yc_data
        self.var_data = var_data
        self.yc_names = yc_data.columns
        self.num_yields = len(yc_data.columns)
        self.yobs = len(yc_data)

        # ensure that var_data is either None or a pandas DataFrame
        self.obs_factors = type(var_data) == pa.core.frame.DataFrame

        #NOTE: Not sure if this should be kept
        # if not self.obs_factors:
        #     warnings.warn("k_ar and neqs ignored when obs_factors not used")

        if k_ar:
            self.k_ar = k_ar
        else:
            self.k_ar = 0

        if neqs:
            self.neqs = neqs
        else:
            self.neqs = 0

        # Number of latent factors
        self.latent = latent

        # Arrays for setting up affine system
        self.lam_0_e = lam_0_e
        self.lam_1_e = lam_1_e
        self.delta_0_e = delta_0_e
        self.delta_1_e = delta_1_e

        self.mu_e = mu_e
        self.phi_e = phi_e
        self.sigma_e = sigma_e

        # generates mats: list of mats in yield curve data
        self.mats = mats
        self.max_mat = max(mats)
        self.mats_ix = [mat - 1 for mat in mats]

        self.no_err = no_err
        if no_err:
            # parameters for identification of yields measured without error
            self.err = list(set(range(len(mats))).difference(no_err))
            self.no_err_mat, self.err_mat = self._gen_mat_list()
            # gen position list for processing list input to solver
            self.noerr_cols, self.err_cols = self._gen_col_names()

        # DOC: Whether to use Cython extension
        self.use_C_extension = use_C_extension
        if avail_fast_gen_pred and use_C_extension:
            self.fast_gen_pred = True
        else:
            self.fast_gen_pred = False

        self.adjusted = adjusted
        if self.obs_factors:
            if adjusted:
                assert len(yc_data.dropna(axis=0)) == \
                       len(var_data.dropna(axis=0)), \
                    "Number of non-null values unequal in VAR and yield curve data"
                var_data_vert = self.var_data_vert = var_data[ \
                                                     var_data.columns[:-neqs]]
                var_data_vertm1 = self.var_data_vertm1 = var_data[ \
                                                         var_data.columns[neqs:]]

            else:
                assert len(yc_data.dropna(axis=0)) == len(var_data.dropna(axis=0)) \
                                                        - k_ar, \
                    "Number of non-null values unequal in VAR and yield curve data"

                x_t_na = transform_var1(var_data, k_ar)

                var_data_vert = self.var_data_vert = x_t_na[x_t_na.columns[:-neqs]]
                var_data_vertm1 = self.var_data_vertm1 = \
                    x_t_na[x_t_na.columns[neqs:]]

            self.var_data_vertc = self.var_data_vert.copy()
            self.var_data_vert_T = np.array(self.var_data_vert.T,
                                            dtype=np.complex_)
            self.var_data_vertc.insert(0, "constant",
                                       np.ones((len(var_data_vert), 1)))
        else:
            self.var_data_vert = None
            self.var_data_vertc = None
            self.var_data_vert_T = None

        self.periods = len(self.yc_data)
        self.guess_length = self._gen_guess_length()
        assert self.guess_length > 0, "guess_length must be at least 1"

        # DOC: Shape checks
        self._size_checks()
        # DOC: Type checks
        self._type_checks()

        #super(Affine, self).__init__(var_data_vert)
        self._init_extend()
Ejemplo n.º 4
0
    def __init__(self,
                 yc_data,
                 var_data,
                 mats,
                 lam_0_e,
                 lam_1_e,
                 delta_0_e,
                 delta_1_e,
                 mu_e,
                 phi_e,
                 sigma_e,
                 latent=0,
                 k_ar=None,
                 neqs=None,
                 no_err=None,
                 adjusted=False,
                 use_C_extension=True):
        """
        Attempts to instantiate an affine model object
        yc_data : DataFrame
            yield curve data
        var_data : DataFrame
            data for var model (pass None if no observed factors)
        mats : list of int
            Maturities in periods of yields included in yc_data
        latent: int
            Number of latent variables to estimate
        no_err : list of ints
            list of the column indexes of yields to be measured without
            error
            ex: [0, 3, 4]
            (1st, 4th, and 5th columns in yc_data to be estimated
            without error)
        adjusted : boolean
            Indicates whether data for VAR has already been transformed into
            VAR(1)
        use_C_extension : boolean
            Indicates whether to use C extension
        k_ar : int
            number of lags for VAR system
            Only respected when adjusted=False and var_data not None
        neqs : int
            Number of observed factors included
            If no observed factors, then set to 0
            Only respected when adjusted=True

        For all estimate parameter arrays:
            elements marked with 'E' or 'e' are estimated
            n = number of variables in fully-specified VAR(1) at t

        lam_0_e : Numpy masked array, n x 1
            constant vector of risk pricing equation
        lam_1_e : Numpy masked array, n x n
            parameter array of risk pricing equation
        delta_0_e : Numpy masked array, 1 x 1
            constant in short-rate equation
        delta_1_e : Numpy masked array, n x 1
            parameter vector in short-rate equation
        mu_e : Numpy masked array, n x 1
            constant vector for VAR process
        phi_e : Numpy masked array, n x n
            parameter array for VAR process
        sigma_e : Numpy masked array, n x n
            covariance array for VAR process
        """
        self.yc_data = yc_data
        self.var_data = var_data
        self.yc_names = yc_data.columns
        self.num_yields = len(yc_data.columns)
        self.yobs = len(yc_data)

        # ensure that var_data is either None or a pandas DataFrame
        self.obs_factors = type(var_data) == pa.core.frame.DataFrame

        #NOTE: Not sure if this should be kept
        # if not self.obs_factors:
        #     warnings.warn("k_ar and neqs ignored when obs_factors not used")

        if k_ar:
            self.k_ar = k_ar
        else:
            self.k_ar = 0

        if neqs:
            self.neqs = neqs
        else:
            self.neqs = 0

        # Number of latent factors
        self.latent = latent

        # Arrays for setting up affine system
        self.lam_0_e = lam_0_e
        self.lam_1_e = lam_1_e
        self.delta_0_e = delta_0_e
        self.delta_1_e = delta_1_e

        self.mu_e = mu_e
        self.phi_e = phi_e
        self.sigma_e = sigma_e

        # generates mats: list of mats in yield curve data
        self.mats = mats
        self.max_mat = max(mats)
        self.mats_ix = [mat - 1 for mat in mats]

        self.no_err = no_err
        if no_err:
            # parameters for identification of yields measured without error
            self.err = list(set(range(len(mats))).difference(no_err))
            self.no_err_mat, self.err_mat = self._gen_mat_list()
            # gen position list for processing list input to solver
            self.noerr_cols, self.err_cols = self._gen_col_names()

        # DOC: Whether to use Cython extension
        self.use_C_extension = use_C_extension
        if avail_fast_gen_pred and use_C_extension:
            self.fast_gen_pred = True
        else:
            self.fast_gen_pred = False

        self.adjusted = adjusted
        if self.obs_factors:
            if adjusted:
                assert len(yc_data.dropna(axis=0)) == \
                       len(var_data.dropna(axis=0)), \
                    "Number of non-null values unequal in VAR and yield curve data"
                var_data_vert = self.var_data_vert = var_data[ \
                                                     var_data.columns[:-neqs]]
                var_data_vertm1 = self.var_data_vertm1 = var_data[ \
                                                         var_data.columns[neqs:]]

            else:
                assert len(yc_data.dropna(axis=0)) == len(var_data.dropna(axis=0)) \
                                                        - k_ar, \
                    "Number of non-null values unequal in VAR and yield curve data"

                x_t_na = transform_var1(var_data, k_ar)

                var_data_vert = self.var_data_vert = x_t_na[
                    x_t_na.columns[:-neqs]]
                var_data_vertm1 = self.var_data_vertm1 = \
                    x_t_na[x_t_na.columns[neqs:]]

            self.var_data_vertc = self.var_data_vert.copy()
            self.var_data_vert_T = np.array(self.var_data_vert.T,
                                            dtype=np.complex_)
            self.var_data_vertc.insert(0, "constant",
                                       np.ones((len(var_data_vert), 1)))
        else:
            self.var_data_vert = None
            self.var_data_vertc = None
            self.var_data_vert_T = None

        self.periods = len(self.yc_data)
        self.guess_length = self._gen_guess_length()
        assert self.guess_length > 0, "guess_length must be at least 1"

        # DOC: Shape checks
        self._size_checks()
        # DOC: Type checks
        self._type_checks()

        #super(Affine, self).__init__(var_data_vert)
        self._init_extend()