# Path: diffprivlib/models/standard_scaler.py
# def _incremental_mean_and_var(X, epsilon, bounds, last_mean, last_variance, last_sample_count):
#     # Initialising new accountant, as budget is tracked in main class. Subject to review in line with GH issue #21
#     temp_acc = BudgetAccountant()
# 
#     # old = stats until now
#     # new = the current increment
#     # updated = the aggregated stats
#     last_sum = last_mean * last_sample_count
# 
#     new_mean = nanmean(X, epsilon=epsilon, axis=0, bounds=bounds, accountant=temp_acc)
#     new_sample_count = np.sum(~np.isnan(X), axis=0)
#     new_sum = new_mean * new_sample_count
#     updated_sample_count = last_sample_count + new_sample_count
# 
#     updated_mean = (last_sum + new_sum) / updated_sample_count
# 
#     if last_variance is None:
#         updated_variance = None
#     else:
#         new_unnormalized_variance = nanvar(X, epsilon=epsilon, axis=0, bounds=bounds,
#                                            accountant=temp_acc) * new_sample_count
#         last_unnormalized_variance = last_variance * last_sample_count
# 
#         with np.errstate(divide='ignore', invalid='ignore'):
#             last_over_new_count = last_sample_count / new_sample_count
#             updated_unnormalized_variance = (
#                 last_unnormalized_variance + new_unnormalized_variance +
#                 last_over_new_count / updated_sample_count *
#                 (last_sum / last_over_new_count - new_sum) ** 2)
# 
#         zeros = last_sample_count == 0
#         updated_unnormalized_variance[zeros] = new_unnormalized_variance[zeros]
#         updated_variance = updated_unnormalized_variance / updated_sample_count
# 
#     return updated_mean, updated_variance, updated_sample_count
# 
# Path: diffprivlib/utils.py
# class PrivacyLeakWarning(RuntimeWarning):
#     """Custom warning to capture privacy leaks resulting from incorrect parameter setting.
# 
#     For example, this warning may occur when the user:
# 
#         - fails to specify the bounds or range of data to a model where required (e.g., `bounds=None` to
#           :class:`.GaussianNB`).
#         - inputs data to a model that falls outside the bounds or range originally specified.
# 
#     """


from unittest.case import TestCase
from sklearn.utils.extmath import _incremental_mean_and_var as sk_incremental_mean_and_var
from diffprivlib.models.standard_scaler import _incremental_mean_and_var
from diffprivlib.utils import PrivacyLeakWarning
import numpy as np

        self.assertTrue(np.allclose(dp_mean, sk_mean))
        self.assertTrue(np.allclose(dp_var, sk_var))
        self.assertTrue((dp_count == sk_count).all())

    def test_increment_inf_epsilon(self):
        X = np.ones((5, 1))
        dp_mean, dp_var, dp_count = _incremental_mean_and_var(X, epsilon=float("inf"), bounds=(0, 1), last_mean=0.,
                                                              last_variance=None, last_sample_count=5)
        self.assertAlmostEqual(dp_mean, 0.5, places=5)
        self.assertEqual(dp_count, 10)

    def test_duplicate_dataset(self):
        X = np.random.rand(10, 5)
        mean1, var1, count1 = _incremental_mean_and_var(X, epsilon=float("inf"), bounds=(0, 1), last_mean=0.,
                                                        last_variance=0., last_sample_count=0)

        mean2, var2, count2 = _incremental_mean_and_var(X, epsilon=float("inf"), bounds=(0, 1), last_mean=mean1,
                                                        last_variance=var1, last_sample_count=count1)

        self.assertTrue(np.allclose(mean1, mean2))
        self.assertTrue(np.allclose(var1, var2))
        self.assertTrue(np.all(count1 == 10), "Counts should be 10, got %s" % count1)
        self.assertTrue(np.all(count2 == 20), "Counts should be 20, got %s" % count2)

    def test_different_results(self):
        X = np.random.rand(10, 5)
        mean1, var1, count1 = _incremental_mean_and_var(X, epsilon=1, bounds=(0, 1), last_mean=0., last_variance=0.,
                                                        last_sample_count=0)


# Path: diffprivlib/models/standard_scaler.py
# def _incremental_mean_and_var(X, epsilon, bounds, last_mean, last_variance, last_sample_count):
#     # Initialising new accountant, as budget is tracked in main class. Subject to review in line with GH issue #21
#     temp_acc = BudgetAccountant()
# 
#     # old = stats until now
#     # new = the current increment
#     # updated = the aggregated stats
#     last_sum = last_mean * last_sample_count
# 
#     new_mean = nanmean(X, epsilon=epsilon, axis=0, bounds=bounds, accountant=temp_acc)
#     new_sample_count = np.sum(~np.isnan(X), axis=0)
#     new_sum = new_mean * new_sample_count
#     updated_sample_count = last_sample_count + new_sample_count
# 
#     updated_mean = (last_sum + new_sum) / updated_sample_count
# 
#     if last_variance is None:
#         updated_variance = None
#     else:
#         new_unnormalized_variance = nanvar(X, epsilon=epsilon, axis=0, bounds=bounds,
#                                            accountant=temp_acc) * new_sample_count
#         last_unnormalized_variance = last_variance * last_sample_count
# 
#         with np.errstate(divide='ignore', invalid='ignore'):
#             last_over_new_count = last_sample_count / new_sample_count
#             updated_unnormalized_variance = (
#                 last_unnormalized_variance + new_unnormalized_variance +
#                 last_over_new_count / updated_sample_count *
#                 (last_sum / last_over_new_count - new_sum) ** 2)
# 
#         zeros = last_sample_count == 0
#         updated_unnormalized_variance[zeros] = new_unnormalized_variance[zeros]
#         updated_variance = updated_unnormalized_variance / updated_sample_count
# 
#     return updated_mean, updated_variance, updated_sample_count
# 
# Path: diffprivlib/utils.py
# class PrivacyLeakWarning(RuntimeWarning):
#     """Custom warning to capture privacy leaks resulting from incorrect parameter setting.
# 
#     For example, this warning may occur when the user:
# 
#         - fails to specify the bounds or range of data to a model where required (e.g., `bounds=None` to
#           :class:`.GaussianNB`).
#         - inputs data to a model that falls outside the bounds or range originally specified.
# 
#     """


# Path: tests/models/test_incremental_mean_and_var.py
from unittest.case import TestCase
from sklearn.utils.extmath import _incremental_mean_and_var as sk_incremental_mean_and_var
from diffprivlib.models.standard_scaler import _incremental_mean_and_var
from diffprivlib.utils import PrivacyLeakWarning
import numpy as np


        self.assertTrue(np.allclose(dp_mean, sk_mean))
        self.assertTrue(np.allclose(dp_var, sk_var))
        self.assertTrue((dp_count == sk_count).all())

    def test_increment_inf_epsilon(self):
        X = np.ones((5, 1))
        dp_mean, dp_var, dp_count = _incremental_mean_and_var(X, epsilon=float("inf"), bounds=(0, 1), last_mean=0.,
                                                              last_variance=None, last_sample_count=5)
        self.assertAlmostEqual(dp_mean, 0.5, places=5)
        self.assertEqual(dp_count, 10)

    def test_duplicate_dataset(self):
        X = np.random.rand(10, 5)
        mean1, var1, count1 = _incremental_mean_and_var(X, epsilon=float("inf"), bounds=(0, 1), last_mean=0.,
                                                        last_variance=0., last_sample_count=0)

        mean2, var2, count2 = _incremental_mean_and_var(X, epsilon=float("inf"), bounds=(0, 1), last_mean=mean1,
                                                        last_variance=var1, last_sample_count=count1)

        self.assertTrue(np.allclose(mean1, mean2))
        self.assertTrue(np.allclose(var1, var2))
        self.assertTrue(np.all(count1 == 10), "Counts should be 10, got %s" % count1)
        self.assertTrue(np.all(count2 == 20), "Counts should be 20, got %s" % count2)

    def test_different_results(self):
        X = np.random.rand(10, 5)
        mean1, var1, count1 = _incremental_mean_and_var(X, epsilon=1, bounds=(0, 1), last_mean=0., last_variance=0.,
                                                        last_sample_count=0)


        mean2, var2, count2 = _incremental_mean_and_var(X, epsilon=1, bounds=(0, 1), last_mean=0, last_variance=0,