예제 #1
0
    def var(self, column: str, privacy_budget: PrivacyBudget) -> float:
        """Compute the variance.

        :param column: Name of the selected column
        :param privacy_budget: Privacy budget to be used

        :return: Private variance of the selected column
        """
        assert column in self._columns, f'Column `{column}`is not exists.'
        assert column in self._data_domains
        domain = self._data_domains[column]
        assert isinstance(domain, RealDataDomain)

        var = np.var(self._dataframe[column])
        sensitivity = domain.length()**2/len(self._dataframe[column])  # (H-L)^2/N
        noisy_var = laplace_mechanism(var, sensitivity, privacy_budget)

        self.privacy_budget_tracker.update_privacy_loss(privacy_budget)

        return noisy_var
예제 #2
0
    def median(self, column: str, privacy_budget: PrivacyBudget) -> float:
        """Compute the median.

        :param column: Name of the selected column
        :param privacy_budget: Privacy budget to be used

        :return: Private median of the selected column
        """
        assert column in self._columns, f'Column `{column}`is not exists.'
        assert column in self._data_domains
        domain = self._data_domains[column]
        assert isinstance(domain, RealDataDomain)

        median = np.median(self._dataframe[column])
        sensitivity = domain.length()/2
        noisy_median = laplace_mechanism(median, sensitivity, privacy_budget)

        self.privacy_budget_tracker.update_privacy_loss(privacy_budget)

        return noisy_median
예제 #3
0
    def std(self, column: str, privacy_budget: PrivacyBudget) -> float:
        """Compute the standard deviation.

        :param column: Name of the selected column
        :param privacy_budget: Privacy budget to be used

        :return: Private standard deviation of the selected column
        """
        assert column in self._columns, f'Column `{column}`is not exists.'
        assert column in self._data_domains
        domain = self._data_domains[column]
        assert isinstance(domain, RealDataDomain)

        std = np.std(self._dataframe[column])
        sensitivity = domain.length()/np.sqrt(len(self._dataframe[column]))
        noisy_std = laplace_mechanism(std, sensitivity, privacy_budget)

        self.privacy_budget_tracker.update_privacy_loss(privacy_budget)

        return noisy_std