riskcal.accountants

CTDAccountant

Opacus-compatible Connect-the-Dots accountant for DP-SGD with risk measurement functionality.

class riskcal.accountants.CTDAccountant[source]

Bases: object

Opacus-compatible Connect the Dots accountant.

This accountant tracks privacy loss for DP-SGD using Google’s Connect the Dots (CTD) method via the dp_accounting library. It maintains a history of (noise_multiplier, sample_rate, num_steps) tuples and composes them to compute overall privacy loss.

Example

>>> acct = CTDAccountant()
>>> for _ in range(1000):
...     acct.step(noise_multiplier=1.0, sample_rate=0.01)
>>> epsilon = acct.get_epsilon(delta=1e-5)
>>> print(f"Privacy: ({epsilon:.2f}, 1e-5)-DP")
__init__()[source]
step(*, noise_multiplier, sample_rate)[source]

Record a single DP-SGD step.

Parameters:
  • noise_multiplier – Noise scale for this step.

  • sample_rate – Poisson sampling probability for this step.

get_pld(grid_step=0.0001, use_connect_dots=True)[source]

Get the composed privacy loss distribution.

Parameters:
  • grid_step – Discretization interval for PLD computation.

  • use_connect_dots – Whether to use Connect the Dots composition.

Returns:

Composed PrivacyLossDistribution from dp_accounting.

get_epsilon(*, delta, **kwargs)[source]

Get epsilon for given delta.

Parameters:
  • delta – Target delta value.

  • **kwargs – Additional arguments passed to get_pld (e.g., grid_step).

Returns:

Epsilon value for (epsilon, delta)-DP.

get_beta(*, alpha, **kwargs)[source]

Get FNR (beta) for given FPR (alpha).

Parameters:
  • alpha – False positive rate.

  • **kwargs – Additional arguments passed to get_pld (e.g., grid_step).

Returns:

False negative rate corresponding to alpha.

get_advantage(**kwargs)[source]

Get attack advantage.

Parameters:

**kwargs – Additional arguments passed to get_pld (e.g., grid_step).

Returns:

Maximum attack advantage.

__len__()[source]

Return total number of steps recorded.

classmethod mechanism()[source]

Return mechanism name for state dict compatibility.

get_optimizer_hook_fn(sample_rate)[source]

Returns a callback function which can be attached to DPOptimizer.

Parameters:

sample_rate (float) – Expected sampling rate used for accounting.

Returns:

Hook function for DPOptimizer.

state_dict(destination=None)[source]

Returns a dictionary containing the state of the accountant.

Parameters:

destination – A mappable object to populate the current state_dict into. If this arg is None, an OrderedDict is created and populated. Default: None.

Returns:

State dictionary.

load_state_dict(state_dict)[source]

Validates the supplied state_dict and populates the current Privacy Accountant’s state dict.

Parameters:

state_dict – State dict to load.

Raises:

ValueError – If supplied state_dict is invalid and cannot be loaded.