Beispiel #1
0
    def get_current_value(self):
        expected_model_counts = self._spectrum_plugin.get_model()

        loglike, bkg_model = poisson_observed_gaussian_background(self._spectrum_plugin.current_observed_counts,
                                                                  self._spectrum_plugin.current_background_counts,
                                                                  self._spectrum_plugin.current_background_count_errors,
                                                                  expected_model_counts)

        return np.sum(loglike), bkg_model
Beispiel #2
0
    def get_current_value(self):
        expected_model_counts = self._spectrum_plugin.get_model()

        loglike, bkg_model = poisson_observed_gaussian_background(self._spectrum_plugin.current_observed_counts,
                                                                  self._spectrum_plugin.current_background_counts,
                                                                  self._spectrum_plugin.current_background_count_errors,
                                                                  expected_model_counts)

        return np.sum(loglike), bkg_model
Beispiel #3
0
    def get_current_value(self, precalc_fluxes: Optional[np.array]=None):
        expected_model_counts = self._spectrum_plugin.get_model(precalc_fluxes=precalc_fluxes)

        loglike, bkg_model = poisson_observed_gaussian_background(
            self._spectrum_plugin.current_observed_counts,
            self._spectrum_plugin.current_background_counts,
            self._spectrum_plugin.current_background_count_errors,
            expected_model_counts,
        )

        return nb_sum(loglike), bkg_model
Beispiel #4
0
    def get_log_like(self):

        model_counts = self._get_model_counts()

        if self._background.is_poisson:

            loglike, bkg_model = poisson_observed_poisson_background(
                self._current_observed_counts, self._current_background_counts,
                self._scale, model_counts)

        else:

            loglike, bkg_model = poisson_observed_gaussian_background(
                self._current_observed_counts, self._current_background_counts,
                self._current_background_count_errors, model_counts)

        return np.sum(loglike)
Beispiel #5
0
    def get_simulated_dataset(self, new_name=None, **kwargs):
        """
        Returns another Binned instance where data have been obtained by randomizing the current expectation from the
        model, as well as from the background (depending on the respective noise models)

        :return: an BinnedSpectrum or child instance
        """

        assert self._likelihood_model is not None, "You need to set up a model before randomizing"

        # Keep track of how many syntethic datasets we have generated

        self._n_synthetic_datasets += 1

        # Generate a name for the new dataset if needed
        if new_name is None:
            new_name = "%s_sim_%i" % (self.name, self._n_synthetic_datasets)

        # Generate randomized data depending on the different noise models

        # We remove the mask temporarily because we need the various elements for all channels. We will restore it
        # at the end

        # Get the source model for all channels (that's why we don't use the .folded_model property)

        # We remove the mask temporarily because we need the various elements for all channels. We will restore it
        # at the end

        original_rebinner = self._rebinner

        with self._without_rebinner():

            # Get the source model for all channels (that's why we don't use the .folded_model property)

            source_model_counts = self._get_model_counts()

            if self._background.is_poisson:
                _, background_model_counts = poisson_observed_poisson_background(
                    self._current_observed_counts,
                    self._current_background_counts, self._scale,
                    source_model_counts)
                randomized_background_counts = np.random.poisson(
                    background_model_counts)

                background_count_errors = None
            else:

                _, background_model_counts = poisson_observed_gaussian_background(
                    self._current_observed_counts,
                    self._current_background_counts,
                    self._current_background_count_errors, source_model_counts)

                randomized_background_counts = np.zeros_like(
                    background_model_counts)

                idx = (self._background_count_errors > 0)

                randomized_background_counts[idx] = np.random.normal(
                    loc=background_model_counts[idx],
                    scale=self._background_count_errors[idx])

                # Issue a warning if the generated background is less than zero, and fix it by placing it at zero

                idx = (randomized_background_counts < 0)  # type: np.ndarray

                negative_background_n = np.sum(idx)

                if negative_background_n > 0:
                    # custom_warnings.warn("Generated background has negative counts "
                    #                      "in %i channels. Fixing them to zero" % (negative_background_n))

                    randomized_background_counts[idx] = 0

                background_count_errors = self._background_count_errors

            # Now randomize the expectations

            # Randomize expectations for the source

            randomized_source_counts = np.random.poisson(
                source_model_counts + background_model_counts)

            #

            new_observation = self._observation.clone(
                new_counts=randomized_source_counts)

            new_background = self._background.clone(
                new_counts=randomized_background_counts,
                new_count_errors=background_count_errors)

            new_plugin = PolarLike(
                name=new_name,
                observation=new_observation,
                background=new_background,
                response=self._response,
                verbose=False,
            )

            # Apply the same selections as the current data set
            if original_rebinner is not None:

                # Apply rebinning, which also applies the mask
                new_plugin._apply_rebinner(original_rebinner)

            return new_plugin