def sindexf(decomp, h): ''' Projects the seasonal component of a seasonal decomposition of a time series forward by h time steps into the future. Args: decomp: an R seasonal decomposition from stl or decompose, or a Pandas data frame containing a seasonal decomposition. h: a forecast horizon Returns: an object that maps to am R time series containing the seasonal component of decomp, projected naively forward h steps. ''' if validate.is_Pandas_decomposition(decomp): freq = len(decomp.index.levels[1]) seasonal = list(decomp.seasonal[-freq:]) it = itertools.cycle(seasonal) data = [] for k in range(h): data.append(it.next()) data = [0] + data start = decomp.seasonal.last_valid_index() series = converters.sequence_as_series(data, start=start, freq=freq) return series[1:] elif validate.is_R_decomposition(decomp): return fc.sindexf(decomp, h) else: raise ValueError('seasadj requires a seasonal decomposition as input')
def seasadj(decomp): ''' Return a seasonally adjusted version of the origin time series that was seasonally decomposed to get decomp. Args: decomp: an R seasonal decomposition from stl or decompose, or a Pandas data frame containing a seasonal decomposition. Returns: an object that maps an R time series of the seasonally adjusted values of the series that decomp was formed from ''' if validate.is_Pandas_decomposition(decomp): if decomp.trend.isnull().any(): # classical seasonal_mean = decomp.seasonal.mean(axis=0) if numpy.allclose(seasonal_mean, 1.0, atol=1e-5): # multiplicative return decomp.data / decomp.seasonal else: # additive return decomp.data - decomp.seasonal else: # stl decomposition return decomp.data - decomp.seasonal elif validate.is_R_decomposition(decomp): return fc.seasadj(decomp) else: raise ValueError('seasadj requires a seasonal decomposition as input')
def to_decomp(dc): ''' Accepts either an R decomposition or a Pandas Data Frame containing a decomposition. In either case, it returns a Pandas Data Frame. Args: dc: an R decomposition or a Pandas Data Frame containing a decomposition Returns: the decomposition in dc, as a Pandas Data Frame ''' if validate.is_Pandas_decomposition(dc): return dc elif validate.is_R_decomposition(dc): return decomposition(dc) else: raise TypeError('Must provide a Pandas-type or R-type decomposition')