ahead.MLARCH

1from .mlarch import MLARCH 
2
3__all__ = ["MLARCH"]
class MLARCH(ahead.Base.Base.Base):
 10class MLARCH(Base):
 11    """Conformalized Forecasting using Machine Learning (and statistical) models with ARCH effects
 12
 13    mean_model: `forecast::auto.arima` (main series)
 14    model_residuals: `forecast::thetaf` (residuals)
 15    fit_func: `ahead::ridge` (volatility)
 16
 17    
 18
 19    Parameters:
 20
 21        h: an integer;
 22            forecasting horizon
 23
 24        level: an integer;
 25            Confidence level for prediction intervals
 26
 27        B: an integer;
 28            Number of bootstrap replications for `type_pi == bootstrap`, "blockbootstrap",
 29            "movingblockbootstrap", or "rvinecopula"
 30
 31        type_pi: a string;
 32            Type of conformal prediction interval ("surrogate", "bootstrap", "kde") for volatility modeling
 33
 34        type_sim_conformalize: a string;
 35            Type of simulation for conformalization of standardized residuals ("block-bootstrap", "surrogate", "kde", "bootstrap", or "fitdistr")
 36
 37        date_formatting: a string;
 38            Currently:
 39            - "original": yyyy-mm-dd
 40            - "ms": milliseconds
 41
 42    Attributes:
 43
 44        fcast_: an object;
 45            raw result from fitting R's `ahead::MLARCHf` through `rpy2`
 46
 47        averages_: a list;
 48            mean forecast in a list
 49
 50        ranges_: a list;
 51            lower and upper prediction intervals in a list
 52
 53        output_dates_: a list;
 54            a list of output dates (associated to forecast)
 55
 56        mean_: a numpy array
 57            contains series mean forecast as a numpy array
 58
 59        lower_: a numpy array
 60            contains series lower bound forecast as a numpy array
 61
 62        upper_: a numpy array
 63            contains series upper bound forecast as a numpy array
 64
 65        result_df_: a data frame;
 66            contains 3 columns, mean forecast, lower + upper
 67            prediction intervals, and a date index
 68
 69    Examples:
 70
 71    ```python
 72    import pandas as pd
 73    from ahead import MLARCH
 74
 75    # Data frame containing the time series
 76    dataset = {
 77    'date' : ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01'],
 78    'value' : [34, 30, 35.6, 33.3, 38.1]}
 79
 80    df = pd.DataFrame(dataset).set_index('date')
 81    print(df)
 82
 83    # univariate time series forecasting
 84    e1 = MLARCH(h = 5) 
 85    e1.forecast(df)
 86    print(e1.result_df_)
 87    ```
 88
 89    """
 90
 91    def __init__(
 92        self,
 93        h=5,
 94        level=95,
 95        B=100,
 96        type_pi="surrogate",
 97        type_sim_conformalize="block-bootstrap",
 98        seed=123,
 99        date_formatting="original",
100    ):
101
102        super().__init__(h=h, level=level)
103        
104        self.h = h
105        self.level = level
106        self.B = B
107        self.type_pi = type_pi
108        self.type_sim_conformalize = type_sim_conformalize  
109        self.seed = seed
110        self.date_formatting = date_formatting
111        self.input_df = None
112        self.type_input = "univariate"
113
114        self.fcast_ = None
115        self.averages_ = None
116        self.ranges_ = None
117        self.output_dates_ = []
118        self.mean_ = []
119        self.lower_ = []
120        self.upper_ = []
121        self.result_df_ = None
122
123    def forecast(self, df):
124        """Forecasting method from `MLARCH` class
125
126        Parameters:
127
128            df: a data frame;
129                a data frame containing the input time series (see example)
130
131        """
132
133        # get input dates, output dates, number of series, series names, etc.
134        self.init_forecasting_params(df)
135
136        # obtain time series object -----
137        self.format_input()
138
139        self.get_forecast("MLARCH")
140
141        print(f"MLARCH: {self.fcast_}")
142
143        # result -----
144        (
145            self.averages_,
146            self.ranges_,
147            _,
148        ) = uv.format_univariate_forecast(
149            date_formatting=self.date_formatting,
150            output_dates=self.output_dates_,
151            horizon=self.h,
152            fcast=self.fcast_,
153        )
154
155        self.mean_ = np.asarray(self.fcast_.rx2["mean"])
156        self.lower_ = np.asarray(self.fcast_.rx2["lower"])
157        self.upper_ = np.asarray(self.fcast_.rx2["upper"])
158
159        self.result_df_ = umv.compute_result_df(self.averages_, self.ranges_)
160
161        return self

Conformalized Forecasting using Machine Learning (and statistical) models with ARCH effects

mean_model: forecast::auto.arima (main series) model_residuals: forecast::thetaf (residuals) fit_func: ahead::ridge (volatility)

Parameters:

h: an integer;
    forecasting horizon

level: an integer;
    Confidence level for prediction intervals

B: an integer;
    Number of bootstrap replications for `type_pi == bootstrap`, "blockbootstrap",
    "movingblockbootstrap", or "rvinecopula"

type_pi: a string;
    Type of conformal prediction interval ("surrogate", "bootstrap", "kde") for volatility modeling

type_sim_conformalize: a string;
    Type of simulation for conformalization of standardized residuals ("block-bootstrap", "surrogate", "kde", "bootstrap", or "fitdistr")

date_formatting: a string;
    Currently:
    - "original": yyyy-mm-dd
    - "ms": milliseconds

Attributes:

fcast_: an object;
    raw result from fitting R's `ahead::MLARCHf` through `rpy2`

averages_: a list;
    mean forecast in a list

ranges_: a list;
    lower and upper prediction intervals in a list

output_dates_: a list;
    a list of output dates (associated to forecast)

mean_: a numpy array
    contains series mean forecast as a numpy array

lower_: a numpy array
    contains series lower bound forecast as a numpy array

upper_: a numpy array
    contains series upper bound forecast as a numpy array

result_df_: a data frame;
    contains 3 columns, mean forecast, lower + upper
    prediction intervals, and a date index

Examples:

import pandas as pd
from ahead import MLARCH

# Data frame containing the time series
dataset = {
'date' : ['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01'],
'value' : [34, 30, 35.6, 33.3, 38.1]}

df = pd.DataFrame(dataset).set_index('date')
print(df)

# univariate time series forecasting
e1 = MLARCH(h = 5) 
e1.forecast(df)
print(e1.result_df_)