Exemplo n.º 1
0
    def _run_with_data(self, data, pre_period, post_period, model_args, alpha,
                       estimation):
        # Zoom in on data in modeling range
        if data.shape[1] == 1:  # no exogenous values provided
            raise ValueError("data contains no exogenous variables")
        data_modeling = data.copy()

        df_pre = data_modeling.loc[pre_period[0]:pre_period[1], :]
        df_post = data_modeling.loc[post_period[0]:post_period[1], :]

        # Standardize all variables
        orig_std_params = (0, 1)
        if model_args["standardize_data"]:
            sd_results = standardize_all_variables(data_modeling, pre_period,
                                                   post_period)
            df_pre = sd_results["data_pre"]
            df_post = sd_results["data_post"]
            orig_std_params = sd_results["orig_std_params"]

        # Construct model and perform inference
        model = construct_model(df_pre, model_args)
        self.model = model

        trained_model = model_fit(model, estimation, model_args["niter"])
        self.model = trained_model

        inferences = compile_posterior_inferences(trained_model, data, df_pre,
                                                  df_post, None, alpha,
                                                  orig_std_params, estimation)

        # "append" to 'CausalImpact' object
        self.inferences = inferences["series"]
Exemplo n.º 2
0
    def _run_with_data(self, data, pre_period, post_period, model_args, alpha,
                       estimation):
        # Zoom in on data in modeling range
        if data.shape[1] == 1:  # no exogenous values provided
            raise ValueError("data contains no exogenous variables")
        non_null = pd.isnull(data.iloc[:, 1]).to_numpy().nonzero()
        first_non_null = non_null[0]
        if first_non_null.size > 0:
            pre_period[0] = max(pre_period[0], data.index[first_non_null[0]])
        data_modeling = data.copy()
        df_pre = data_modeling.loc[pre_period[0]:pre_period[1], :]
        df_post = data_modeling.loc[post_period[0]:post_period[1], :]

        # Standardize all variables
        orig_std_params = (0, 1)
        if model_args["standardize_data"]:
            sd_results = standardize_all_variables(data_modeling, pre_period,
                                                   post_period)
            df_pre = sd_results["data_pre"]
            df_post = sd_results["data_post"]
            orig_std_params = sd_results["orig_std_params"]

        # Construct model and perform inference
        ucm_model = construct_model(self, df_pre, model_args)
        res = model_fit(self, ucm_model, estimation, model_args["niter"])

        inferences = compile_posterior_inferences(res, data, df_pre, df_post, None,
                                                  alpha, orig_std_params,
                                                  estimation)

        # "append" to 'CausalImpact' object
        self.inferences = inferences["series"]
        self.model = ucm_model
Exemplo n.º 3
0
    def _run_with_data(self, data, pre_period, post_period, model_args, alpha):
        # Zoom in on data in modeling range

        first_non_null = pd.isnull(data.iloc[:, 1]).nonzero()[0]
        if len(first_non_null) > 0:
            pre_period[0] = max(pre_period[0], data.index[first_non_null[0]])
        data_modeling = data.iloc[pre_period[0]:post_period[1], :]

        # Standardize all variables?
        orig_std_params = np.identity
        if model_args["standardize_data"]:
            sd_results = standardize_all_variables(data_modeling)
            data_modeling = sd_results["data"]
            orig_std_params = sd_results["orig_std_params"]

        # Set observed response in post-period to NA
        data_modeling.iloc[post_period[0]:, 1] = np.nan

        # Construct model and perform inference
        ucm_model = construct_model(data_modeling, model_args)

        # Compile posterior inferences
        if ucm_model is not None:
            data_post = data.iloc[post_period[0]:post_period[1], :]
            inferences = compile_posterior_inferences(ucm_model, data_post,
                                                      alpha, orig_std_params)
        else:
            inferences = compile_na_inferences(data.iloc[:, 1])

        # Extend <series> to cover original range
        # (padding with NA as necessary)
        empty = pd.DataFrame(index=data.index)
        inferences["series"] = pd.merge(inferences["series"], empty,
                                        left_index=True, right_index=True,
                                        how="outer")
        if len(inferences["series"]) != len(data):
            raise ValueError("""inferences['series'] must have the same number
                             of rows as 'data'""")

        # Replace <y.model> by full original response
        inferences["series"].iloc[:, 0] = data[:, 0]

        # Assign response-variable names
        inferences["series"].iloc[:, 0].name = "response"
        inferences["series"].iloc[:, 1].name = "cum.response"

        # Return 'CausalImpact' object
        params = {"pre_period": pre_period, "post_period": post_period,
                 "model_args": model_args, "alpha": alpha}

        self.inferences = inferences["series"]
        self.summary = inferences["summary"]
        self.report = inferences["report"]
        self.model = model
        self.params = params