Exemple #1
0
    def __init__(self, losses, size, reps=1000, block_size=None, method='R',
                 bootstrap='stationary'):
        super(MCS, self).__init__()
        self.losses = ensure2d(losses, 'losses')
        self._losses_arr = np.asarray(self.losses)
        if self._losses_arr.shape[1] < 2:
            raise ValueError('losses must have at least two columns')
        self.size = size
        self.reps = reps
        if block_size is None:
            self.block_size = int(np.sqrt(losses.shape[0]))
        else:
            self.block_size = block_size

        self.t, self.k = losses.shape
        self.method = method
        # Bootstrap indices since the same bootstrap should be used in the
        # repeated steps
        indices = np.arange(self.t)
        bootstrap = bootstrap.lower().replace(' ', '_')
        if bootstrap in ('stationary', 'sb'):
            bootstrap = StationaryBootstrap(self.block_size, indices)
        elif bootstrap in ('circular', 'cbb'):
            bootstrap = CircularBlockBootstrap(self.block_size, indices)
        elif bootstrap in ('moving_block', 'mbb'):
            bootstrap = MovingBlockBootstrap(self.block_size, indices)
        else:
            raise ValueError('Unknown bootstrap:' + bootstrap)
        self.bootstrap = bootstrap
        self._bootsrap_indices = []  # For testing
        self._model = 'MCS'
        self._info = OrderedDict([('size', '{0:0.2f}'.format(self.size)),
                                  ('bootstrap', str(bootstrap)),
                                  ('ID', hex(id(self)))])
        self._results_computed = False
Exemple #2
0
 def __init__(
     self,
     benchmark: ArrayLike,
     models: ArrayLike,
     block_size: Optional[int] = None,
     reps: int = 1000,
     bootstrap: str = "stationary",
     studentize: bool = True,
     nested: bool = False,
 ) -> None:
     super().__init__()
     self.benchmark: np.ndarray[Any, np.dtype[np.float64]] = ensure2d(
         benchmark, "benchmark")
     self.models: np.ndarray[Any, np.dtype[np.float64]] = ensure2d(
         models, "models")
     self.reps: int = reps
     if block_size is None:
         self.block_size = int(np.sqrt(benchmark.shape[0]))
     else:
         self.block_size = block_size
     self.studentize: bool = studentize
     self.nested: bool = nested
     self._loss_diff = np.asarray(self.benchmark) - np.asarray(self.models)
     self._loss_diff_var = np.empty(0)
     self.t: int = self._loss_diff.shape[0]
     self.k: int = self._loss_diff.shape[1]
     bootstrap = bootstrap.lower().replace(" ", "_")
     if bootstrap in ("circular", "cbb"):
         bootstrap_inst = CircularBlockBootstrap(self.block_size,
                                                 self._loss_diff)
     elif bootstrap in ("stationary", "sb"):
         bootstrap_inst = StationaryBootstrap(self.block_size,
                                              self._loss_diff)
     elif bootstrap in ("moving_block", "mbb"):
         bootstrap_inst = MovingBlockBootstrap(self.block_size,
                                               self._loss_diff)
     else:
         raise ValueError("Unknown bootstrap:" + bootstrap)
     self.bootstrap: CircularBlockBootstrap = bootstrap_inst
     self._pvalues: Dict[str, float] = {}
     self._simulated_vals: Optional[NDArray] = None
     self._selector = np.ones(self.k, dtype=np.bool_)
     self._model = "SPA"
     if self.studentize:
         method = "bootstrap" if self.nested else "asymptotic"
     else:
         method = "none"
     self._info = dict([
         ("studentization", method),
         ("bootstrap", str(self.bootstrap)),
         ("ID", hex(id(self))),
     ])
Exemple #3
0
    def __init__(
        self,
        losses: ArrayLike,
        size: float,
        reps: int = 1000,
        block_size: Optional[int] = None,
        method: Literal["R", "max"] = "R",
        bootstrap: Literal[
            "stationary", "sb", "circular", "cbb", "moving block", "mbb"
        ] = "stationary",
    ) -> None:
        super().__init__()
        self.losses: np.ndarray[Any, np.dtype[np.float64]] = ensure2d(losses, "losses")
        self._losses_arr = np.asarray(self.losses)
        if self._losses_arr.shape[1] < 2:
            raise ValueError("losses must have at least two columns")
        self.size: float = size
        self.reps: int = reps
        if block_size is None:
            self.block_size = int(np.sqrt(losses.shape[0]))
        else:
            self.block_size = block_size

        self.t: int = losses.shape[0]
        self.k: int = losses.shape[1]
        self.method: Literal["R", "max"] = method
        # Bootstrap indices since the same bootstrap should be used in the
        # repeated steps
        indices = np.arange(self.t)
        bootstrap_meth = bootstrap.lower().replace(" ", "_")
        if bootstrap_meth in ("circular", "cbb"):
            bootstrap_inst = CircularBlockBootstrap(self.block_size, indices)
        elif bootstrap_meth in ("stationary", "sb"):
            bootstrap_inst = StationaryBootstrap(self.block_size, indices)
        elif bootstrap_meth in ("moving_block", "mbb"):
            bootstrap_inst = MovingBlockBootstrap(self.block_size, indices)
        else:
            raise ValueError(f"Unknown bootstrap: {bootstrap_meth}")
        self.bootstrap: CircularBlockBootstrap = bootstrap_inst
        self._bootstrap_indices: List[NDArray] = []  # For testing
        self._model = "MCS"
        self._info = dict(
            [
                ("size", "{0:0.2f}".format(self.size)),
                ("bootstrap", str(bootstrap_inst)),
                ("ID", hex(id(self))),
            ]
        )
        self._results_computed = False
 def __init__(self,
              benchmark,
              models,
              block_size=None,
              reps=1000,
              bootstrap='stationary',
              studentize=True,
              nested=False):
     super(SPA, self).__init__()
     self.benchmark = ensure2d(benchmark, 'benchmark')
     self.models = ensure2d(models, 'models')
     self.reps = reps
     if block_size is None:
         self.block_size = int(np.sqrt(benchmark.shape[0]))
     else:
         self.block_size = block_size
     self.studentize = studentize
     self.nested = nested
     self._loss_diff = np.asarray(self.benchmark) - np.asarray(self.models)
     self._loss_diff_var = None
     self.t, self.k = self._loss_diff.shape
     bootstrap = bootstrap.lower().replace(' ', '_')
     if bootstrap in ('stationary', 'sb'):
         bootstrap = StationaryBootstrap(self.block_size, self._loss_diff)
     elif bootstrap in ('circular', 'cbb'):
         bootstrap = CircularBlockBootstrap(self.block_size,
                                            self._loss_diff)
     elif bootstrap in ('moving_block', 'mbb'):
         bootstrap = MovingBlockBootstrap(self.block_size, self._loss_diff)
     else:
         raise ValueError('Unknown bootstrap:' + bootstrap)
     self.bootstrap = bootstrap
     self._pvalues = None
     self._simulated_vals = None
     self._selector = np.ones(self.k, dtype=np.bool)
     self._model = 'SPA'
     if self.studentize:
         method = 'bootstrap' if self.nested else 'asymptotic'
     else:
         method = 'none'
     self._info = OrderedDict([('studentization', method),
                               ('bootstrap', str(self.bootstrap)),
                               ('ID', hex(id(self)))])