def setup(self, window_size=None, step=None): """ Returns the series of models (each model is placed under the date corresponding to the end of its time window). Parameters ---------- window_size: int, optional number of samples contained in each window. If it's not provided, than it will be calculated automatically step: int, optional number of samples by which window shall be moved each time it is moved. If the value isn't provided, than it will be calculated automatically """ if window_size is None: window_size = RollingWindowsEstimator.estimate_rolling_window_size( self.input_data.analysed_tms) if step is None: step = RollingWindowsEstimator.estimate_rolling_window_step( self.input_data.analysed_tms) assert step > 0 self.window_size_ = window_size self.step_ = step end_of_window_idx = len(self.input_data.analysed_tms) - 1 beginning_of_window_idx = end_of_window_idx - window_size ref_dates = [] models = [] while beginning_of_window_idx >= 0: data_model, ref_date = self._get_model_for_window( beginning_of_window_idx, end_of_window_idx) models.append(data_model) ref_dates.append(ref_date) end_of_window_idx -= step beginning_of_window_idx -= step models_series_ = QFSeries(data=models, index=ref_dates) self.coefficients_df = models_series_.apply( lambda model: model.coefficients) self.t_stats_df = models_series_.apply(lambda model: model.t_values) self.p_values_df = models_series_.apply(lambda model: model.p_values) self.r_squared_tms = models_series_.apply( lambda model: model.r_squared) self.risk_contribs_df = models_series_.apply( lambda model: model.risk_contribution) self.factors_performance_attributions_df = models_series_.apply( lambda model: model.factors_performance_attribution_ret) self.unexplained_performance_attributions_tms = models_series_.apply( lambda model: model.unexplained_performance_attribution_ret) # select correlations of different series with analysed timeseries self.correlations_df = models_series_.apply( lambda model: model.correlation_matrix.iloc[-1, :-1])
def _get_futures_chain_tickers(self): """ Function used to download the expiration dates of the futures contracts, in order to return afterwards current futures tickers. It uses the list of month codes of designated contracts and filter out these, that should not be considered by the future ticker. """ futures_chain_tickers_df = self._data_provider.get_futures_chain_tickers(self, ExpirationDateField.all_dates())[self] # Get the minimum date futures_chain_tickers = futures_chain_tickers_df.min(axis=1) futures_chain_tickers = QFSeries(data=futures_chain_tickers.index, index=futures_chain_tickers.values) futures_chain_tickers.index = pd.to_datetime(futures_chain_tickers.index) # Filter out the non-designated contracts seed = self.family_id.split("{}")[0] designated_contracts_seeds = tuple(seed + month_code for month_code in self.designated_contracts) futures_chain_tickers = futures_chain_tickers[futures_chain_tickers.apply( lambda t: t.ticker.startswith(designated_contracts_seeds) )] return futures_chain_tickers