Exemplo n.º 1
0
    def forecast_all_signals_hierarchical(self, project_data, last_date, H):
        import pyaf.HierarchicalForecastEngine as hautof

        lEngine = hautof.cHierarchicalForecastEngine()
        lEngine.mOptions.mHierarchicalCombinationMethod = "TD"
        # lEngine
        lEngine.mOptions.mAddPredictionIntervals = False
        # lEngine.mOptions.mParallelMode = False
        lEngine.mOptions.set_active_transformations(
            ['None', 'Difference', 'Anscombe'])
        lEngine.mOptions.mMaxAROrder = 16

        lHierarchy = self.define_hierarchy(project_data)

        # lEngine
        df1 = self.mHierarchyVisits.fillna(0.0)
        lEngine.train(df1, 'Date', None, 1, lHierarchy, None)
        lEngine.getModelInfo()
        # lEngine.standrdPlots()

        df_forecast = lEngine.forecast(iInputDS=df1, iHorizon=H)
        logger.info(str(df_forecast.columns))
        df_forecast.to_csv("hierarchical_td_Forecast.csv")
        dates = df_forecast['Date'].tail(H).values
        forecasts = {}
        for col in project_data.mVisitsDF.columns:
            if (col != 'Date'):
                logger.info("FORECAST_SIGNAL " +
                            str([self.__class__.__name__, col]))
                predictions = None
                if (col in self.mHierarchyVisits.columns):
                    predictions = df_forecast[
                        str(col) + '_AHP_TD_Forecast'].tail(H).values
                else:
                    nb_other = self.nb_discarded[self.mHierarchyClass[col]]
                    predictions = df_forecast[str(self.mOtherArticleId) +
                                              '_AHP_TD_Forecast'] / nb_other
                    predictions = predictions.tail(H).values

                # logger.info(dates)
                # logger.info(predictions)
                fcst_dict = {}
                for i in range(H):
                    ts = pd.to_datetime(str(dates[i]))
                    date_str = ts.strftime('%Y-%m-%d')
                    fcst_dict[date_str] = int(predictions[i])
                logger.info("SIGNAL_FORECAST " + str([col, fcst_dict]))
                forecasts[col] = fcst_dict

        return forecasts
def train_and_force_fail(b, error_message):
    try:
        df = b.mPastData

        lEngine = hautof.cHierarchicalForecastEngine()
        lEngine.mOptions.mHierarchicalCombinationMethod = "TD"
        lEngine.mOptions.set_active_autoregressions([])
        lEngine

        H = b.mHorizon

        lEngine.train(df, b.mTimeVar, b.mSignalVar, H, b.mHierarchy, None)
        raise Exception("NOT_OK")
    except Exception as e:
        # should fail
        print(str(e))
        assert (str(e) == error_message)
        if (str(e) == "NOT_OK"):
            raise
        pass
Exemplo n.º 3
0
    'Data':
    pd.DataFrame({
        'area': areas,
        'tract': tracts,
        'district': districts,
        'austin': area_list
    }),
    'Type':
    'Hierarchical'
}

# Need to complete the time series...
dat = dat.set_index('time').groupby(['area']).resample('15T').mean().fillna(0)
dat.reset_index(inplace=True)

lEngine = hautof.cHierarchicalForecastEngine()

lSignalHierarchy = lEngine.plot_Hierarchy(dat, "time", "n", 1, lHierarchy,
                                          None)
# lSignalHierarchy.mStructure

# drop final day for predictions
train_df = dat[dat['time'] < pd.to_datetime('2019-01-30')].copy().pivot(
    index='time', columns='area', values='n').reset_index()

lSignalHierarchy = lEngine.train(train_df, 'time', 'n', 1, lHierarchy, None)

# And now results time
lInfo = lEngine.to_json()
perfs = []
for model in sorted(lInfo['Models'].keys()):