def _estimate_confidence_intervals_with_bootstrap()

in dowhy/causal_estimator.py [0:0]


    def _estimate_confidence_intervals_with_bootstrap(self, estimate_value,
                                                      confidence_level=None,
                                                      num_simulations=None, sample_size_fraction=None):
        '''
            Method to compute confidence interval using bootstrapped sampling.

            :param estimate_value: obtained estimate's value
            :param confidence_level: The level for which to compute CI (e.g., 95% confidence level translates to confidence_level=0.95)
            :param num_simulations: The number of simulations to be performed to get the bootstrap confidence intervals.
            :param sample_size_fraction: The fraction of the dataset to be resampled.
            :returns: confidence interval at the specified level.

            For more details on bootstrap or resampling statistics, refer to the following links:
            https://ocw.mit.edu/courses/mathematics/18-05-introduction-to-probability-and-statistics-spring-2014/readings/MIT18_05S14_Reading24.pdf
            https://projecteuclid.org/download/pdf_1/euclid.ss/1032280214
        '''
        # Using class default parameters if not specified
        if num_simulations is None:
            num_simulations = self.num_simulations
        if sample_size_fraction is None:
            sample_size_fraction = self.sample_size_fraction

        # Checking if bootstrap_estimates are already computed
        if self._bootstrap_estimates is None:
            self._bootstrap_estimates = self._generate_bootstrap_estimates(
                num_simulations, sample_size_fraction)
        elif CausalEstimator.is_bootstrap_parameter_changed(self._bootstrap_estimates.params, locals()):
            # Checked if any parameter is changed from the previous std error estimate
            self._bootstrap_estimates = self._generate_bootstrap_estimates(
                num_simulations, sample_size_fraction)
        # Now use the data obtained from the simulations to get the value of the confidence estimates
        bootstrap_estimates = self._bootstrap_estimates.estimates
        # Get the variations of each bootstrap estimate and sort
        bootstrap_variations = [bootstrap_estimate - estimate_value for bootstrap_estimate in bootstrap_estimates]
        sorted_bootstrap_variations = np.sort(bootstrap_variations)

        # Now we take the (1- p)th and the (p)th variations, where p is the chosen confidence level
        upper_bound_index = int((1 - confidence_level) * len(sorted_bootstrap_variations))
        lower_bound_index = int(confidence_level * len(sorted_bootstrap_variations))

        # Get the lower and upper bounds by subtracting the variations from the estimate
        lower_bound = estimate_value - sorted_bootstrap_variations[lower_bound_index]
        upper_bound = estimate_value - sorted_bootstrap_variations[upper_bound_index]
        return lower_bound, upper_bound