The Hegselmann-Krause model was introduced in 2002 by Hegselmann, Krause et al [1].

During each interaction a random agenti is selected and the set \(\Gamma_{\epsilon}\) of its neighbors whose opinions differ at most \(\epsilon\) (\(d_{i,j}=|x_i(t)-x_j(t)|\leq \epsilon\)) is identified. The selected agent i changes its opinion based on the following update rule:

\[x_i(t+1)= \frac{\sum_{j \in \Gamma_{\epsilon}} x_j(t)}{\#\Gamma_{\epsilon}}\]

The idea behind the WHK formulation is that the opinion of agent \(i\) at time \(t+1\), will be given by the average opinion by its, selected, \(\epsilon\)-neighbor.


Node statuses are continuous values in [-1,1].


Name Type Value Type Default Mandatory Description
epsilon Model float in [0, 1] True Bounded confidence threshold


The following class methods are made available to configure, describe and execute the simulation:


class ndlib.models.opinions.HKModel.HKModel(graph)

Model Parameters to be specified via ModelConfig :param epsilon: bounded confidence threshold from the HK model (float in [0,1])


Model Constructor :param graph: A networkx graph object

HKModel.set_initial_status(self, configuration)

Override behaviour of methods in class DiffusionModel. Overwrites initial status using random real values.


Reset the simulation setting the actual status to the initial configuration.



Describes the current model parameters (nodes, edges, status)

Returns:a dictionary containing for each parameter class the values specified during model configuration

Specify the statuses allowed by the model and their numeric code

Returns:a dictionary (status->code)

Execute Simulation


Execute a single model iteration

Returns:Iteration_id, Incremental node status (dictionary code -> status)
HKModel.iteration_bunch(self, bunch_size)

Execute a bunch of model iterations

  • bunch_size – the number of iterations to execute
  • node_status – if the incremental node status has to be returned.
  • progress_bar – whether to display a progress bar, default False

a list containing for each iteration a dictionary {“iteration”: iteration_id, “status”: dictionary_node_to_status}


In the code below is shown an example of instantiation and execution of an HK model simulation on a random graph: we an epsilon value of 0.32 .

import networkx as nx
import ndlib.models.ModelConfig as mc
import ndlib.models.opinions as opn

# Network topology
g = nx.erdos_renyi_graph(1000, 0.1)

# Model selection
model = opn.HKModel(g)

# Model Configuration
config = mc.Configuration()
config.add_model_parameter("epsilon", 0.32)


# Simulation execution
iterations = model.iteration_bunch(20)
  1. Hegselmann, U. Krause, et al.: “Opinion dynamics and bounded confidence models, analysis, and simulation.” in Journal of artificial societies and social simulation, 2002