Beispiel #1
0
    def _run_holts_trend_corrected_exponential_smoothing(
            self, individual: tuple):
        f = Forecast(self.__orders, self.__average_order)
        holts_trend_corrected_smoothing = []
        demand = [{
            't': index,
            'demand': order
        } for index, order in enumerate(self.__orders, 1)]
        stats = LinearRegression(demand)
        log_stats = stats.least_squared_error(slice_end=6)

        for sm_lvl in individual:
            p = [
                i for i in f.holts_trend_corrected_exponential_smoothing(
                    alpha=sm_lvl[0],
                    gamma=sm_lvl[1],
                    intercept=log_stats.get('intercept'),
                    slope=log_stats.get('slope'))
            ]
            # print(p)
            holts_trend_corrected_smoothing.append(p)

        appraised_individual = {}
        for smoothing_level in individual:
            sum_squared_error = f.sum_squared_errors_indi_htces(
                squared_error=holts_trend_corrected_smoothing,
                alpha=smoothing_level[0],
                gamma=smoothing_level[1])
            standard_error = f.standard_error(sum_squared_error,
                                              len(self.__orders),
                                              smoothing_level)
            appraised_individual.update({smoothing_level: standard_error})
        # print('The standard error as a trait has been calculated {}'.format(appraised_individual))
        return appraised_individual
    def test_holts_trend_corrected_exponential_smoothing_len(self):
        total_orders = 0

        for order in self.__orders_ex[:12]:
            total_orders += order

        avg_orders = total_orders / 12
        f = Forecast(self.__orders_ex)
        p = [i for i in f.holts_trend_corrected_exponential_smoothing(0.5, 0.5, 155.88, 0.8369)]
        self.assertEqual(len(p), 36)
Beispiel #3
0
    def test_holts_trend_corrected_exponential_smoothing_len(self):
        total_orders = 0

        for order in self.__orders_ex[:12]:
            total_orders += order

        avg_orders = total_orders / 12
        f = Forecast(self.__orders_ex)
        p = [i for i in f.holts_trend_corrected_exponential_smoothing(0.5, 0.5, 155.88, 0.8369)]
        self.assertEqual(len(p), 36)
    def test_holts_trend_corrected_exponential_smoothing_forecast(self):
        total_orders = 0

        for order in self.__orders_ex[:12]:
            total_orders += order

        f = Forecast(self.__orders_ex)
        p = [i for i in f.holts_trend_corrected_exponential_smoothing(0.5, 0.5, 155.88, 0.8369)]
        holts_forecast = f.holts_trend_corrected_forecast(forecast=p, forecast_length=4)
        self.assertEqual(round(holts_forecast[0]), 281)
        self.assertEqual(round(holts_forecast[1]), 307)
        self.assertEqual(round(holts_forecast[2]), 334)
        self.assertEqual(round(holts_forecast[3]), 361)
Beispiel #5
0
    def test_holts_trend_corrected_exponential_smoothing_forecast(self):
        total_orders = 0

        for order in self.__orders_ex[:12]:
            total_orders += order

        f = Forecast(self.__orders_ex)
        p = [i for i in f.holts_trend_corrected_exponential_smoothing(0.5, 0.5, 155.88, 0.8369)]
        holts_forecast = f.holts_trend_corrected_forecast(forecast=p, forecast_length=4)
        self.assertEqual(round(holts_forecast[0]), 281)
        self.assertEqual(round(holts_forecast[1]), 307)
        self.assertEqual(round(holts_forecast[2]), 334)
        self.assertEqual(round(holts_forecast[3]), 361)
def holts_trend_corrected_exponential_smoothing_forecast(demand: list, alpha: float, gamma: float,
                                                         forecast_length: int = 4, initial_period: int = 6, **kwargs):
    if len(kwargs) != 0:
        if kwargs['optimise']:

            total_orders = 0

            for order in demand[:initial_period]:
                total_orders += order

            avg_orders = total_orders / initial_period
            forecast_demand = Forecast(demand)

            processed_demand = [{'t': index, 'demand': order} for index, order in enumerate(demand, 1)]
            stats = LinearRegression(processed_demand)

            log_stats = stats.least_squared_error(slice_end=6)

            htces_forecast = [i for i in
                              forecast_demand.holts_trend_corrected_exponential_smoothing(alpha=alpha, gamma=gamma,
                                                                                          intercept=log_stats.get(
                                                                                              'intercept'),
                                                                                          slope=log_stats.get(
                                                                                              'slope'))]

            sum_squared_error = forecast_demand.sum_squared_errors_indi_htces(squared_error=[htces_forecast],
                                                                              alpha=alpha, gamma=gamma)

            standard_error = forecast_demand.standard_error(sum_squared_error, len(demand), (alpha, gamma), 2)

            evo_mod = OptimiseSmoothingLevelGeneticAlgorithm(orders=demand,
                                                             average_order=avg_orders,
                                                             population_size=10,
                                                             standard_error=standard_error,
                                                             recombination_type='single_point')

            optimal_alpha = evo_mod.initial_population(individual_type='htces')

            log.log(logging.WARNING,
                    'An optimal alpha {} and optimal gamma {} have been found.'.format(optimal_alpha[1][0],
                                                                                       optimal_alpha[1][1]))

            htces_forecast = [i for i in
                              forecast_demand.holts_trend_corrected_exponential_smoothing(alpha=optimal_alpha[1][0],
                                                                                          gamma=optimal_alpha[1][1],
                                                                                          intercept=log_stats.get(
                                                                                              'intercept'),
                                                                                          slope=log_stats.get('slope'))]

            holts_forecast = forecast_demand.holts_trend_corrected_forecast(forecast=htces_forecast,
                                                                            forecast_length=forecast_length)
            log.log(logging.INFO, 'An OPTIMAL Holts trend exponential smoothing forecast has been generated.')
            sum_squared_error_opt = forecast_demand.sum_squared_errors_indi_htces(squared_error=[htces_forecast],
                                                                                  alpha=optimal_alpha[1][0],
                                                                                  gamma=optimal_alpha[1][1])

            standard_error_opt = forecast_demand.standard_error(sum_squared_error_opt, len(demand),
                                                                (optimal_alpha[1][0], optimal_alpha[1][1]), 2)

            ape = LinearRegression(htces_forecast)
            mape = forecast_demand.mean_aboslute_percentage_error_opt(htces_forecast)
            stats = ape.least_squared_error()
            regression_line = deepcopy(regr_ln(stats=stats))
            return {'forecast_breakdown': htces_forecast, 'forecast': holts_forecast, 'mape': mape, 'statistics': stats,
                    'optimal_alpha': optimal_alpha[1][0],
                    'optimal_gamma': optimal_alpha[1][1],
                    'SSE': sum_squared_error_opt,
                    'standard_error': standard_error_opt,
                    'original_standard_error': standard_error,
                    'regression': [i for i in regression_line.get('regression')]}

    else:

        forecast_demand = Forecast(demand)
        processed_demand = [{'t': index, 'demand': order} for index, order in enumerate(demand, 1)]
        stats = LinearRegression(processed_demand)
        log_stats = stats.least_squared_error(slice_end=6)
        htces_forecast = [i for i in
                          forecast_demand.holts_trend_corrected_exponential_smoothing(alpha=alpha, gamma=gamma,
                                                                                      intercept=log_stats.get(
                                                                                          'intercept'),
                                                                                      slope=log_stats.get(
                                                                                          'slope'))]

        holts_forecast = forecast_demand.holts_trend_corrected_forecast(forecast=htces_forecast,
                                                                        forecast_length=forecast_length)

        log.log(logging.INFO, 'A STANDARD Holts trend exponential smoothing forecast has been generated.')

        sum_squared_error = forecast_demand.sum_squared_errors_indi_htces(squared_error=[htces_forecast],
                                                                          alpha=alpha, gamma=gamma)

        ape = LinearRegression(htces_forecast)
        mape = forecast_demand.mean_aboslute_percentage_error_opt(htces_forecast)
        stats = ape.least_squared_error()
        regression_line = regr_ln(stats=stats)
        log.log(logging.WARNING, "A STANDARD Holts trend exponential smoothing forecast has been completed.")
        return {'forecast_breakdown': htces_forecast,
                'forecast': holts_forecast,
                'mape': mape,
                'statistics': stats,
                'sum_squared_errors': sum_squared_error,
                'regression': [i for i in regression_line.get('regression')]}
def holts_trend_corrected_exponential_smoothing_forecast(
        demand: list,
        alpha: float,
        gamma: float,
        forecast_length: int = 4,
        initial_period: int = 6,
        optimise: bool = True) -> dict:
    """ Performs a holt's trend corrected exponential smoothing forecast on known demand

    Args:
        demand (list):  Original historical demand.
        alpha (float): smoothing constant
        gamma (float):
        forecast_length (int):
        initial_period (int):
        optimise (bool):    Flag for using solver. Default is set to True. 

    Returns:
        dict: Simple exponential forecast.

    """
    if optimise:
        total_orders = 0
        for order in demand[:initial_period]:
            total_orders += order
        avg_orders = total_orders // initial_period
        forecast_demand = Forecast(demand)
        processed_demand = [{
            't': index,
            'demand': order
        } for index, order in enumerate(demand, 1)]
        stats = LinearRegression(processed_demand)
        log_stats = stats.least_squared_error(slice_end=6)
        htces_forecast = [
            i for i in
            forecast_demand.holts_trend_corrected_exponential_smoothing(
                alpha=alpha,
                gamma=gamma,
                intercept=log_stats.get('intercept'),
                slope=log_stats.get('slope'))
        ]
        sum_squared_error = forecast_demand.sum_squared_errors_indi_htces(
            squared_error=[htces_forecast], alpha=alpha, gamma=gamma)
        standard_error = forecast_demand.standard_error(
            sum_squared_error, len(demand), (alpha, gamma), 2)
        evo_mod = OptimiseSmoothingLevelGeneticAlgorithm(
            orders=demand,
            average_order=avg_orders,
            population_size=10,
            standard_error=standard_error,
            recombination_type='single_point')
        optimal_alpha = evo_mod.initial_population(individual_type='htces')
        log.log(
            logging.WARNING,
            'An optimal alpha {} and optimal gamma {} have been found.'.format(
                optimal_alpha[1][0], optimal_alpha[1][1]))
        htces_forecast = [
            i for i in
            forecast_demand.holts_trend_corrected_exponential_smoothing(
                alpha=optimal_alpha[1][0],
                gamma=optimal_alpha[1][1],
                intercept=log_stats.get('intercept'),
                slope=log_stats.get('slope'))
        ]
        holts_forecast = forecast_demand.holts_trend_corrected_forecast(
            forecast=htces_forecast, forecast_length=forecast_length)
        log.log(
            logging.INFO,
            'An OPTIMAL Holts trend exponential smoothing forecast has been generated.'
        )
        sum_squared_error_opt = forecast_demand.sum_squared_errors_indi_htces(
            squared_error=[htces_forecast],
            alpha=optimal_alpha[1][0],
            gamma=optimal_alpha[1][1])
        standard_error_opt = forecast_demand.standard_error(
            sum_squared_error_opt, len(demand),
            (optimal_alpha[1][0], optimal_alpha[1][1]), 2)

        ape = LinearRegression(htces_forecast)
        mape = forecast_demand.mean_aboslute_percentage_error_opt(
            htces_forecast)
        stats = ape.least_squared_error()
        regression_line = deepcopy(_regr_ln(stats=stats))
        return {
            'forecast_breakdown': htces_forecast,
            'forecast': holts_forecast,
            'mape': mape,
            'statistics': stats,
            'optimal_alpha': optimal_alpha[1][0],
            'optimal_gamma': optimal_alpha[1][1],
            'SSE': sum_squared_error_opt,
            'standard_error': standard_error_opt,
            'original_standard_error': standard_error,
            'regression': [i for i in regression_line.get('regression')]
        }

    else:

        forecast_demand = Forecast(demand)
        processed_demand = [{
            't': index,
            'demand': order
        } for index, order in enumerate(demand, 1)]
        stats = LinearRegression(processed_demand)
        log_stats = stats.least_squared_error(slice_end=6)
        htces_forecast = [
            i for i in
            forecast_demand.holts_trend_corrected_exponential_smoothing(
                alpha=alpha,
                gamma=gamma,
                intercept=log_stats.get('intercept'),
                slope=log_stats.get('slope'))
        ]

        holts_forecast = forecast_demand.holts_trend_corrected_forecast(
            forecast=htces_forecast, forecast_length=forecast_length)
        log.log(
            logging.INFO,
            'A STANDARD Holts trend exponential smoothing forecast has been generated.'
        )
        sum_squared_error = forecast_demand.sum_squared_errors_indi_htces(
            squared_error=[htces_forecast], alpha=alpha, gamma=gamma)
        ape = LinearRegression(htces_forecast)
        mape = forecast_demand.mean_aboslute_percentage_error_opt(
            htces_forecast)
        stats = ape.least_squared_error()
        regression_line = _regr_ln(stats=stats)
        log.log(
            logging.WARNING,
            "A STANDARD Holts trend exponential smoothing forecast has been completed."
        )
        return {
            'forecast_breakdown': htces_forecast,
            'forecast': holts_forecast,
            'mape': mape,
            'statistics': stats,
            'sum_squared_errors': sum_squared_error,
            'regression': [i for i in regression_line.get('regression')]
        }