Skip to content

Privacy Attacks

API Description
Attack Abstract base class for privacy attacks.
LocationAttack Assess risk from a set of visited locations.
LocationSequenceAttack Assess risk from a chronologically ordered sequence of locations.
LocationTimeAttack Assess risk from locations observed at a selected time precision.
UniqueLocationAttack Assess risk from the set of unique locations visited by each user.
LocationFrequencyAttack Assess risk from locations and visit frequencies.
LocationProbabilityAttack Assess risk from locations and visit probabilities.
LocationProportionAttack Assess risk from locations and relative visit-frequency proportions.
HomeWorkAttack Assess risk from each user's two most frequent locations.

fastmob.privacy.Attack

Bases: ABC

Abstract base class for privacy risk attacks.

Implements the background-knowledge attack framework from [TIST2018] [MOB2018]. For each target user, all combinations of knowledge_length observations are generated as background-knowledge instances. Each instance is matched against every candidate trajectory; the re-identification probability of an instance is 1 / number_of_matching_candidates. The reported risk is the maximum probability across all instances for that user.

Concrete subclasses must override :meth:_match_counts.

Parameters:

Name Type Description Default
knowledge_length int

Number of observations known by the attacker for each target user. Values greater than a user's trajectory length are capped to that user's available rows.

required

Attributes:

Name Type Description
knowledge_length int

Number of trajectory observations known by the attacker.

Raises:

Type Description
ValueError

If knowledge_length is less than 1.

References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

knowledge_length property writable

Number of trajectory observations known by the attacker.


fastmob.privacy.LocationAttack

Bases: Attack

Location Attack: assess re-identification risk from visited locations.

The attacker knows the coordinates of up to knowledge_length location observations for a target user [TIST2018] [MOB2018]. Matching is multiset-based: the instance locations must appear in the candidate trajectory with at least the same frequency, regardless of visit order or timestamp.

Parameters:

Name Type Description Default
knowledge_length int

Number of location observations known by the attacker.

required

Attributes:

Name Type Description
knowledge_length int

Number of trajectory observations known by the attacker.

Examples:

>>> import pandas as pd
>>> from fastmob.privacy.attacks import LocationAttack
>>> traj = pd.DataFrame(
...     {
...         "uid": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6],
...         "lat": [43.843, 43.544, 43.708, 43.779, 43.843, 43.708, 43.843, 43.544,
...                 43.544, 43.708, 43.843, 43.779, 43.708, 43.544, 43.779, 43.708,
...                 43.779, 43.843, 43.843, 43.544],
...         "lng": [10.508, 10.326, 10.404, 11.246, 10.508, 10.404, 10.508, 10.326,
...                 10.326, 10.404, 10.508, 11.246, 10.404, 10.326, 11.246, 10.404,
...                 11.246, 10.508, 10.508, 10.326],
...         "datetime": pd.to_datetime([
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-03 10:34", "2011-02-04 10:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-04 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-05 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34",
...         ]),
...     }
... )
>>> at = LocationAttack(knowledge_length=2)
>>> print(at.assess_risk(traj).to_string(index=False))
 uid      risk
   1  0.333333
   2  1.000000
   3  0.333333
   4  0.333333
   5  0.333333
   6  0.250000
References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

assess_risk(traj, targets=None, force_instances=False, show_progress=False, *, presorted=False)

Assess privacy risk for each user in the trajectory.

Parameters:

Name Type Description Default
traj DataFrame - like

Trajectory dataframe; any Narwhals-compatible eager backend (pandas, polars, …). Must have uid, lat, lng, and datetime columns (auto-detected by name).

required
targets DataFrame-like or list of int

Subset of user IDs to assess. When None (default), risk is computed for every user in traj.

None
force_instances bool

When True, return one row per background-knowledge instance element with its re-identification probability instead of the per-user maximum. Default: False.

False
show_progress bool

Accepted for API compatibility with skmob; has no effect in fastmob. Default: False.

False

Returns:

Type Description
DataFrame or DataFrame

When force_instances=False: one row per user with columns ["uid", "risk"]. When force_instances=True: one row per instance element with columns ["lat", "lng", "datetime", "uid", "instance", "instance_elem", "prob"]. The returned backend matches the input backend.


fastmob.privacy.LocationSequenceAttack

Bases: Attack

Location Sequence Attack: assess risk from an ordered location sequence.

The attacker knows the coordinates of up to knowledge_length locations and their relative temporal order [TIST2018] [MOB2018]. Matching requires the known locations to appear as an ordered subsequence of the candidate trajectory — timestamps are not used, only the visit order.

Parameters:

Name Type Description Default
knowledge_length int

Number of ordered location observations known by the attacker.

required

Attributes:

Name Type Description
knowledge_length int

Number of trajectory observations known by the attacker.

Examples:

>>> import pandas as pd
>>> from fastmob.privacy.attacks import LocationSequenceAttack
>>> traj = pd.DataFrame(
...     {
...         "uid": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6],
...         "lat": [43.843, 43.544, 43.708, 43.779, 43.843, 43.708, 43.843, 43.544,
...                 43.544, 43.708, 43.843, 43.779, 43.708, 43.544, 43.779, 43.708,
...                 43.779, 43.843, 43.843, 43.544],
...         "lng": [10.508, 10.326, 10.404, 11.246, 10.508, 10.404, 10.508, 10.326,
...                 10.326, 10.404, 10.508, 11.246, 10.404, 10.326, 11.246, 10.404,
...                 11.246, 10.508, 10.508, 10.326],
...         "datetime": pd.to_datetime([
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-03 10:34", "2011-02-04 10:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-04 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-05 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34",
...         ]),
...     }
... )
>>> at = LocationSequenceAttack(knowledge_length=2)
>>> print(at.assess_risk(traj).to_string(index=False))
 uid      risk
   1  0.500000
   2  1.000000
   3  1.000000
   4  0.500000
   5  1.000000
   6  0.333333
References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

assess_risk(traj, targets=None, force_instances=False, show_progress=False, *, presorted=False)

Assess privacy risk for each user in the trajectory.

Parameters:

Name Type Description Default
traj DataFrame - like

Trajectory dataframe; any Narwhals-compatible eager backend (pandas, polars, …). Must have uid, lat, lng, and datetime columns (auto-detected by name).

required
targets DataFrame-like or list of int

Subset of user IDs to assess. When None (default), risk is computed for every user in traj.

None
force_instances bool

When True, return one row per background-knowledge instance element with its re-identification probability instead of the per-user maximum. Default: False.

False
show_progress bool

Accepted for API compatibility with skmob; has no effect in fastmob. Default: False.

False

Returns:

Type Description
DataFrame or DataFrame

When force_instances=False: one row per user with columns ["uid", "risk"]. When force_instances=True: one row per instance element with columns ["lat", "lng", "datetime", "uid", "instance", "instance_elem", "prob"]. The returned backend matches the input backend.


fastmob.privacy.LocationTimeAttack

Bases: LocationAttack

Location Time Attack: assess risk from locations and timestamps.

The attacker knows the coordinates and timestamps (truncated to time_precision) of up to knowledge_length observations [TIST2018] [MOB2018]. Matching requires latitude, longitude, and the truncated datetime to coincide, regardless of visit order.

Parameters:

Name Type Description Default
knowledge_length int

Number of location/time observations known by the attacker.

required
time_precision str

Datetime component to use when comparing timestamps. One of "Year", "Month", "Day", "Hour", "Minute", "Second". Default: "Hour".

'Hour'

Attributes:

Name Type Description
knowledge_length int

Number of trajectory observations known by the attacker.

time_precision str

Datetime precision used to match known observations.

Examples:

>>> import pandas as pd
>>> from fastmob.privacy.attacks import LocationTimeAttack
>>> traj = pd.DataFrame(
...     {
...         "uid": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6],
...         "lat": [43.843, 43.544, 43.708, 43.779, 43.843, 43.708, 43.843, 43.544,
...                 43.544, 43.708, 43.843, 43.779, 43.708, 43.544, 43.779, 43.708,
...                 43.779, 43.843, 43.843, 43.544],
...         "lng": [10.508, 10.326, 10.404, 11.246, 10.508, 10.404, 10.508, 10.326,
...                 10.326, 10.404, 10.508, 11.246, 10.404, 10.326, 11.246, 10.404,
...                 11.246, 10.508, 10.508, 10.326],
...         "datetime": pd.to_datetime([
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-03 10:34", "2011-02-04 10:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-04 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-05 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34",
...         ]),
...     }
... )
>>> at = LocationTimeAttack(knowledge_length=2, time_precision="Hour")
>>> print(at.assess_risk(traj).to_string(index=False))
 uid  risk
   1   1.0
   2   1.0
   3   1.0
   4   1.0
   5   1.0
   6   0.5
>>> at.time_precision = "Month"
>>> print(at.assess_risk(traj).to_string(index=False))
 uid      risk
   1  0.333333
   2  1.000000
   3  0.333333
   4  0.333333
   5  0.333333
   6  0.250000
References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

time_precision property writable

Datetime precision used to match known observations.

assess_risk(traj, targets=None, force_instances=False, show_progress=False, *, presorted=False)

Assess privacy risk for each user in the trajectory.

Parameters:

Name Type Description Default
traj DataFrame - like

Trajectory dataframe; any Narwhals-compatible eager backend (pandas, polars, …). Must have uid, lat, lng, and datetime columns (auto-detected by name).

required
targets DataFrame-like or list of int

Subset of user IDs to assess. When None (default), risk is computed for every user in traj.

None
force_instances bool

When True, return one row per background-knowledge instance element with its re-identification probability instead of the per-user maximum. Default: False.

False
show_progress bool

Accepted for API compatibility with skmob; has no effect in fastmob. Default: False.

False

Returns:

Type Description
DataFrame or DataFrame

When force_instances=False: one row per user with columns ["uid", "risk"]. When force_instances=True: one row per instance element with columns ["lat", "lng", "datetime", "uid", "instance", "instance_elem", "prob"]. The returned backend matches the input backend.


fastmob.privacy.UniqueLocationAttack

Bases: Attack

Unique Location Attack: assess risk from the set of distinct locations.

The attacker knows up to knowledge_length distinct (lat, lng) locations visited by a target user [TIST2018] [MOB2018]. Matching is set-based: an instance matches a candidate if every instance location appears in the candidate's frequency vector at least once. Visit counts, order, and timestamps are not used.

Parameters:

Name Type Description Default
knowledge_length int

Number of distinct locations known by the attacker.

required

Attributes:

Name Type Description
knowledge_length int

Number of trajectory observations known by the attacker.

Examples:

>>> import pandas as pd
>>> from fastmob.privacy.attacks import UniqueLocationAttack
>>> traj = pd.DataFrame(
...     {
...         "uid": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6],
...         "lat": [43.843, 43.544, 43.708, 43.779, 43.843, 43.708, 43.843, 43.544,
...                 43.544, 43.708, 43.843, 43.779, 43.708, 43.544, 43.779, 43.708,
...                 43.779, 43.843, 43.843, 43.544],
...         "lng": [10.508, 10.326, 10.404, 11.246, 10.508, 10.404, 10.508, 10.326,
...                 10.326, 10.404, 10.508, 11.246, 10.404, 10.326, 11.246, 10.404,
...                 11.246, 10.508, 10.508, 10.326],
...         "datetime": pd.to_datetime([
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-03 10:34", "2011-02-04 10:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-04 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-05 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34",
...         ]),
...     }
... )
>>> at = UniqueLocationAttack(knowledge_length=2)
>>> print(at.assess_risk(traj).to_string(index=False))
 uid      risk
   1  0.333333
   2  0.250000
   3  0.333333
   4  0.333333
   5  0.333333
   6  0.250000
References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

assess_risk(traj, targets=None, force_instances=False, show_progress=False, *, presorted=False)

Assess privacy risk for each user in the trajectory.

Parameters:

Name Type Description Default
traj DataFrame - like

Trajectory dataframe; any Narwhals-compatible eager backend (pandas, polars, …). Must have uid, lat, and lng columns (auto-detected by name).

required
targets DataFrame-like or list of int

Subset of user IDs to assess. When None (default), risk is computed for every user in traj.

None
force_instances bool

When True, return one row per background-knowledge instance element with its re-identification probability instead of the per-user maximum. Default: False.

False
show_progress bool

Accepted for API compatibility with skmob; has no effect in fastmob. Default: False.

False

Returns:

Type Description
DataFrame or DataFrame

When force_instances=False: one row per user with columns ["uid", "risk"]. When force_instances=True: one row per instance element with columns ["lat", "lng", "datetime", "uid", "instance", "instance_elem", "prob"]. The returned backend matches the input backend.


fastmob.privacy.LocationFrequencyAttack

Bases: Attack

Location Frequency Attack: assess risk from locations and visit counts.

The attacker knows up to knowledge_length distinct locations and the number of times each was visited [TIST2018] [MOB2018]. Matching compares the known frequency against the candidate's frequency vector within a relative tolerance: a frequency \(f_k\) matches \(f_c\) when

\[ f_c(1 - \text{tolerance}) \leq f_k \leq f_c(1 + \text{tolerance}) \]

Parameters:

Name Type Description Default
knowledge_length int

Number of location observations known by the attacker.

required
tolerance float

Relative tolerance used when comparing known frequencies to candidate frequencies. Must be in [0.0, 1.0]. Default: 0.0 (exact match).

0.0

Attributes:

Name Type Description
knowledge_length int

Number of trajectory observations known by the attacker.

tolerance float

Relative tolerance for frequency matching.

Raises:

Type Description
ValueError

If knowledge_length is less than 1 or tolerance is outside [0.0, 1.0].

Examples:

>>> import pandas as pd
>>> from fastmob.privacy.attacks import LocationFrequencyAttack
>>> traj = pd.DataFrame(
...     {
...         "uid": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6],
...         "lat": [43.843, 43.544, 43.708, 43.779, 43.843, 43.708, 43.843, 43.544,
...                 43.544, 43.708, 43.843, 43.779, 43.708, 43.544, 43.779, 43.708,
...                 43.779, 43.843, 43.843, 43.544],
...         "lng": [10.508, 10.326, 10.404, 11.246, 10.508, 10.404, 10.508, 10.326,
...                 10.326, 10.404, 10.508, 11.246, 10.404, 10.326, 11.246, 10.404,
...                 11.246, 10.508, 10.508, 10.326],
...         "datetime": pd.to_datetime([
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-03 10:34", "2011-02-04 10:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-04 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-05 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34",
...         ]),
...     }
... )
>>> at = LocationFrequencyAttack(knowledge_length=2)
>>> print(at.assess_risk(traj).to_string(index=False))
 uid      risk
   1  0.333333
   2  1.000000
   3  0.333333
   4  0.333333
   5  0.333333
   6  0.333333
References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

tolerance property writable

Relative tolerance used when comparing known values.

assess_risk(traj, targets=None, force_instances=False, show_progress=False, *, presorted=False)

Assess privacy risk for each user in the trajectory.

Parameters:

Name Type Description Default
traj DataFrame - like

Trajectory dataframe; any Narwhals-compatible eager backend (pandas, polars, …). Must have uid, lat, and lng columns (auto-detected by name).

required
targets DataFrame-like or list of int

Subset of user IDs to assess. When None (default), risk is computed for every user in traj.

None
force_instances bool

When True, return one row per background-knowledge instance element with its re-identification probability instead of the per-user maximum. Default: False.

False
show_progress bool

Accepted for API compatibility with skmob; has no effect in fastmob. Default: False.

False

Returns:

Type Description
DataFrame or DataFrame

When force_instances=False: one row per user with columns ["uid", "risk"]. When force_instances=True: one row per instance element with columns ["lat", "lng", "datetime", "uid", "instance", "instance_elem", "prob"]. The returned backend matches the input backend.


fastmob.privacy.LocationProbabilityAttack

Bases: LocationFrequencyAttack

Location Probability Attack: assess risk from locations and visit probabilities.

The attacker knows up to knowledge_length distinct locations and the probability (relative frequency) of visiting each one [TIST2018] [MOB2018]. Matching compares known probabilities against the candidate's probability vector within a relative tolerance. Inherits knowledge_length and tolerance from :class:LocationFrequencyAttack.

Examples:

>>> import pandas as pd
>>> from fastmob.privacy.attacks import LocationProbabilityAttack
>>> traj = pd.DataFrame(
...     {
...         "uid": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6],
...         "lat": [43.843, 43.544, 43.708, 43.779, 43.843, 43.708, 43.843, 43.544,
...                 43.544, 43.708, 43.843, 43.779, 43.708, 43.544, 43.779, 43.708,
...                 43.779, 43.843, 43.843, 43.544],
...         "lng": [10.508, 10.326, 10.404, 11.246, 10.508, 10.404, 10.508, 10.326,
...                 10.326, 10.404, 10.508, 11.246, 10.404, 10.326, 11.246, 10.404,
...                 11.246, 10.508, 10.508, 10.326],
...         "datetime": pd.to_datetime([
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-03 10:34", "2011-02-04 10:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-04 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-05 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34",
...         ]),
...     }
... )
>>> at = LocationProbabilityAttack(knowledge_length=2)
>>> print(at.assess_risk(traj).to_string(index=False))
 uid  risk
   1   0.5
   2   1.0
   3   0.5
   4   1.0
   5   1.0
   6   1.0
References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

assess_risk(traj, targets=None, force_instances=False, show_progress=False, *, presorted=False)

Assess privacy risk for each user in the trajectory.

Parameters:

Name Type Description Default
traj DataFrame - like

Trajectory dataframe; any Narwhals-compatible eager backend (pandas, polars, …). Must have uid, lat, and lng columns (auto-detected by name).

required
targets DataFrame-like or list of int

Subset of user IDs to assess. When None (default), risk is computed for every user in traj.

None
force_instances bool

When True, return one row per background-knowledge instance element with its re-identification probability instead of the per-user maximum. Default: False.

False
show_progress bool

Accepted for API compatibility with skmob; has no effect in fastmob. Default: False.

False

Returns:

Type Description
DataFrame or DataFrame

When force_instances=False: one row per user with columns ["uid", "risk"]. When force_instances=True: one row per instance element with columns ["lat", "lng", "datetime", "uid", "instance", "instance_elem", "prob"]. The returned backend matches the input backend.


fastmob.privacy.LocationProportionAttack

Bases: LocationFrequencyAttack

Location Proportion Attack: assess risk from locations and frequency proportions.

The attacker knows up to knowledge_length distinct locations and the relative proportions between their visit frequencies [TIST2018] [MOB2018]. Matching normalises both the instance and candidate frequencies by their respective maximums before comparing within tolerance. Inherits knowledge_length and tolerance from :class:LocationFrequencyAttack.

Examples:

>>> import pandas as pd
>>> from fastmob.privacy.attacks import LocationProportionAttack
>>> traj = pd.DataFrame(
...     {
...         "uid": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6],
...         "lat": [43.843, 43.544, 43.708, 43.779, 43.843, 43.708, 43.843, 43.544,
...                 43.544, 43.708, 43.843, 43.779, 43.708, 43.544, 43.779, 43.708,
...                 43.779, 43.843, 43.843, 43.544],
...         "lng": [10.508, 10.326, 10.404, 11.246, 10.508, 10.404, 10.508, 10.326,
...                 10.326, 10.404, 10.508, 11.246, 10.404, 10.326, 11.246, 10.404,
...                 11.246, 10.508, 10.508, 10.326],
...         "datetime": pd.to_datetime([
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-03 10:34", "2011-02-04 10:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-04 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-05 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34",
...         ]),
...     }
... )
>>> at = LocationProportionAttack(knowledge_length=2)
>>> print(at.assess_risk(traj).to_string(index=False))
 uid      risk
   1  0.333333
   2  1.000000
   3  0.333333
   4  0.333333
   5  0.333333
   6  0.333333
References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

assess_risk(traj, targets=None, force_instances=False, show_progress=False, *, presorted=False)

Assess privacy risk for each user in the trajectory.

Parameters:

Name Type Description Default
traj DataFrame - like

Trajectory dataframe; any Narwhals-compatible eager backend (pandas, polars, …). Must have uid, lat, and lng columns (auto-detected by name).

required
targets DataFrame-like or list of int

Subset of user IDs to assess. When None (default), risk is computed for every user in traj.

None
force_instances bool

When True, return one row per background-knowledge instance element with its re-identification probability instead of the per-user maximum. Default: False.

False
show_progress bool

Accepted for API compatibility with skmob; has no effect in fastmob. Default: False.

False

Returns:

Type Description
DataFrame or DataFrame

When force_instances=False: one row per user with columns ["uid", "risk"]. When force_instances=True: one row per instance element with columns ["lat", "lng", "datetime", "uid", "instance", "instance_elem", "prob"]. The returned backend matches the input backend.


fastmob.privacy.HomeWorkAttack

Bases: UniqueLocationAttack

Home-Work Attack: assess risk from the two most-visited locations.

A special case of :class:UniqueLocationAttack where the attacker always uses the two most-visited locations as background knowledge, regardless of knowledge_length. This models the scenario where an attacker knows a user's home and workplace [TIST2018] [MOB2018].

Parameters:

Name Type Description Default
knowledge_length int

Kept for API compatibility; always uses exactly two locations. Default: 1.

1

Attributes:

Name Type Description
knowledge_length int

Number of trajectory observations known by the attacker.

Examples:

>>> import pandas as pd
>>> from fastmob.privacy.attacks import HomeWorkAttack
>>> traj = pd.DataFrame(
...     {
...         "uid": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6],
...         "lat": [43.843, 43.544, 43.708, 43.779, 43.843, 43.708, 43.843, 43.544,
...                 43.544, 43.708, 43.843, 43.779, 43.708, 43.544, 43.779, 43.708,
...                 43.779, 43.843, 43.843, 43.544],
...         "lng": [10.508, 10.326, 10.404, 11.246, 10.508, 10.404, 10.508, 10.326,
...                 10.326, 10.404, 10.508, 11.246, 10.404, 10.326, 11.246, 10.404,
...                 11.246, 10.508, 10.508, 10.326],
...         "datetime": pd.to_datetime([
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-03 10:34", "2011-02-04 10:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-03 08:34", "2011-02-03 09:34", "2011-02-04 10:34", "2011-02-04 11:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-04 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34", "2011-02-05 12:34",
...             "2011-02-04 10:34", "2011-02-04 11:34",
...         ]),
...     }
... )
>>> at = HomeWorkAttack()
>>> print(at.assess_risk(traj).to_string(index=False))
 uid  risk
   1  0.25
   2  0.25
   3  0.25
   4  0.25
   5  1.00
   6  1.00
References
  • [TIST2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2017) A Data Mining Approach to Assess Privacy Risk in Human Mobility Data. ACM Trans. Intell. Syst. Technol. 9(3), Article 31. https://doi.org/10.1145/3106774
  • [MOB2018] Pellungrini, R., Pappalardo, L., Pratesi, F. & Monreale, A. (2018) Analyzing Privacy Risk in Human Mobility Data. STAF Workshops 2018: 114-129.

assess_risk(traj, targets=None, force_instances=False, show_progress=False, *, presorted=False)

Assess privacy risk for each user in the trajectory.

Parameters:

Name Type Description Default
traj DataFrame - like

Trajectory dataframe; any Narwhals-compatible eager backend (pandas, polars, …). Must have uid, lat, and lng columns (auto-detected by name).

required
targets DataFrame-like or list of int

Subset of user IDs to assess. When None (default), risk is computed for every user in traj.

None
force_instances bool

When True, return one row per background-knowledge instance element with its re-identification probability instead of the per-user maximum. Default: False.

False
show_progress bool

Accepted for API compatibility with skmob; has no effect in fastmob. Default: False.

False

Returns:

Type Description
DataFrame or DataFrame

When force_instances=False: one row per user with columns ["uid", "risk"]. When force_instances=True: one row per instance element with columns ["lat", "lng", "datetime", "uid", "instance", "instance_elem", "prob"]. The returned backend matches the input backend.