Metrics
Metrics are components that track and store a given quantity over the duration of the simulation. All metrics in this module are based on the Measurement
abstract class, which inherits from the BaseObservable
abstract class (refer to the Observer design pattern for more details).
This module comes with a number of pre-loaded metrics, as well as a simple programmer interface to develop new metrics.
Interaction Measurement
- class metrics.measurement.InteractionMeasurement(name='interaction_histogram', verbose=False)[source]
Keeps track of the interactions between users and items.
Specifically, at each timestep, it stores a histogram of length \(|I|\), where element \(i\) is the number of interactions received by item \(i\).
- Parameters
verbose (bool, default False) – If
True
, enables verbose mode. Disabled by default.
- Inherited by Measurement
- Type
- name
Name of the measurement component.
- Type
str, default
"interaction_histogram"
- measure(recommender)[source]
Measures and stores a histogram of the number of interactions per item at the given timestep.
- Parameters
recommender (
BaseRecommender
) – Model that inherits fromBaseRecommender
.
Interaction Spread
- class metrics.measurement.InteractionSpread(verbose=False)[source]
Measures the diversity of the interactions between users and items.
Specifically, at each timestep, it measures whether interactions are spread among many items or only a few items.
This class inherits from
InteractionMeasurement
.- Parameters
verbose (bool, default False) – If
True
, enables verbose mode. Disabled by default.
- Inherited by InteractionMeasurement
- name
Name of the measurement component.
- Type
str, default
"interaction_spread"
- measure(recommender)[source]
Measures the diversity of user interactions – that is, whether interactions are spread among many items or only a few items.
- Parameters
recommender (
BaseRecommender
) – Model that inherits fromBaseRecommender
.
RecSimilarity
- class metrics.measurement.RecSimilarity(pairs, name='rec_similarity', verbose=False)[source]
Keeps track of the average Jaccard similarity between items seen by pairs of users at each timestep. The pairs of users must be passed in by the user.
- Parameters
pairs (iterable of tuples) – Contains tuples representing each pair of users. Each user should be represented as an index into the user profiles matrix.
verbose (bool, default False) – If
True
, enables verbose mode. Disabled by default.
- Inherited by Measurement
- Type
- name
Name of the measurement component.
- Type
str, default
"rec_similarity"
- measure(recommender)[source]
Measures the average Jaccard index of items shown to pairs of users in the system. Intuitively, a higher average Jaccard index corresponds to increasing “homogenization” in that the recommender system is starting to treat each user the same way (i.e., show them the same items).
- Parameters
recommender (
BaseRecommender
) – Model that inherits fromBaseRecommender
.
InteractionSimilarity
- class metrics.measurement.InteractionSimilarity(pairs, name='interaction_similarity', verbose=False, diagnostics=False, **kwargs)[source]
Keeps track of the average Jaccard similarity between interactions with items between pairs of users at each timestep. The pairs of users must be passed in by the user.
- Parameters
pairs (iterable of tuples) – Contains tuples representing each pair of users. Each user should be represented as an index into the user profiles matrix.
verbose (bool, default False) – If
True
, enables verbose mode. Disabled by default.
- Inherited by Measurement
- Type
- name
Name of the measurement component.
- Type
str, default
"interaction_similarity"
- measure(recommender)[source]
Measures the average Jaccard index of items that pairs of users have interacted with in the system. Intuitively, a higher average Jaccard index corresponds to increasing “homogenization” in that user behavior is becoming more and more similar (i.e., users have all interacted with the same items).
- Parameters
recommender (
BaseRecommender
) – Model that inherits fromBaseRecommender
.
MSE Measurement
- class metrics.measurement.MSEMeasurement(verbose=False, diagnostics=False, **kwargs)[source]
Measures the mean squared error (MSE) between real and predicted user scores.
It can be used to evaluate how accurate the model predictions are.
This class inherits from
Measurement
.- Parameters
verbose (bool, default False) – If
True
, enables verbose mode. Disabled by default.
- Inherited by Measurement
- Type
- measure(recommender)[source]
Measures and records the mean squared error between the user preferences predicted by the system and the users’ actual preferences.
- Parameters
recommender (
BaseRecommender
) – Model that inherits fromBaseRecommender
.
Diffusion Tree
- class metrics.measurement.DiffusionTreeMeasurement(verbose=False)[source]
Class that implements an information diffusion tree. The current implementation assumes that agents using this class (i.e., a model) implement an
infection_state
matrix that denotes the initial state of information.In this implementation, the nodes represent users and are labeled with the user indices. A branch between nodes u and v indicates that user u passed information onto user v – that is, u “infected” v.
Trees are implemented using the Networkx library. Please refer to Networkx’s documentation for more details.
- Parameters
infection_state (
InfectionState
) – The initial “infection state” of all usersverbose (bool, default False) – If
True
, enables verbose mode. Disabled by default.
- Inherited by Measurement
- Type
- name
Name of the metric that is recorded at each time step. Note that, in this case, the metric stored in
measurement_history
is actually the number of infected users. The diffusion tree itself is kept in thediffusion_tree
data structure.- Type
str, default
"num_infected"
- diffusion_tree
Diffusion tree.
- Type
networkx.Graph
- _old_infection_state
Infection state at the previous timestep.
- Type
array_like
- measure(recommender)[source]
Updates tree with new infections and stores information about new infections. In
measurement_history
, it stores the total number of infected users in the system – that is, the number of nodes in the tree.- Parameters
recommender (
BaseRecommender
) – Model that inherits fromBaseRecommender
.
Average Feature Score Range
- class metrics.measurement.AverageFeatureScoreRange(name='afsr', verbose=False)[source]
Measures the average range (across users) of item attributes for items users were recommended at a time step.
This metric is based on the item diversity measure used in :
Willemsen, M. C., Graus, M. P., & Knijnenburg, B. P. (2016). Understanding the role of latent feature diversification on choice difficulty and satisfaction. User Modeling and User-Adapted Interaction, 26(4), 347-389.
This class inherits from
Measurement
.- Parameters
verbose (bool, default False) – If
True
, enables verbose mode. Disabled by default.
- Inherited by Measurement
- Type
- name
Name of the measurement component.
- Type
str, default
"afsr"
- measure(recommender)[source]
Measures the average range (across users) of item attributes for items users were recommended at a time step. Used as a measure of within list recommendation diversity
This metric is based on the item diversity measure used in : Willemsen, M. C., Graus, M. P., & Knijnenburg, B. P. (2016). Understanding the role of latent feature diversification on choice difficulty and satisfaction. User Modeling and User-Adapted Interaction, 26(4), 347-389.
- Parameters
recommender (
BaseRecommender
) – Model that inherits fromBaseRecommender
.
Measurement: base class
- class metrics.measurement.Measurement(name, verbose=False)[source]
Abstract observable class to store measurements.
- Parameters
verbose (bool, default False) – If
True
, enables verbose mode. Disabled by default.
- get_measurement()[source]
Returns measurements. See
get_observable()
for more details.- Returns
Measurements
- Return type
- get_timesteps()[source]
Returns the number of measurements stored (which is equivalent to the number of timesteps that the system has been measuring).
- Returns
Length of
measurement_history
- Return type
- abstract measure(recommender)[source]
Function that should calculate some outcome of interest of the system at the current timestep