Weighted Hegselmann-Krause

The Weighted Hegselmann-Krause was introduced by Milli et al. in 2021 [1].

This model is a variation of the well-known Hegselmann-Krause (HK). 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. Moreover, to account for the heterogeneity of interaction frequency among agent pairs, WHK leverages edge weights, thus capturing the effect of different social bonds’ strength/trust as it happens in reality. To such extent, each edge \((i,j) \in E\), carries a value \(w_{i,j}\in [0,1]\). The update rule then becomes:

\[\begin{split}x_i(t+1)= \left\{ \begin{array}{ll} x_i(t) + \frac{\sum_{j \in \Gamma_{\epsilon}} x_j(t)w_{ij}}{\#\Gamma_{\epsilon}} (1-x_i(t)) \quad \quad \text{\quad if x_i(t) \geq 0}\\ x_i(t) + \frac{\sum_{j \in \Gamma_{\epsilon}} x_j(t)w_{ij}}{\#\Gamma_{\epsilon}} (1+x_i(t)) \quad \text{if x_i(t) < 0 } \end{array} \right.\end{split}\]

The idea behind the WHK formulation is that the opinion of agent \(i\) at time \(t+1\), will be given by the combined effect of his previous belief and the average opinion weighed by its, selected, \(\epsilon\)-neighbor, where \(w_{i,j}\) accounts for \(i\)’s perceived influence/trust of \(j\).


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
perc_stubborness Model float in [0, 1] 0 False Percentage of stubborn agent
similarity Model int in {0, 1} 0 False The method use the feature of the nodes ot not
option_for_stubbornness Model int in {-1,0, 1} 0 False Define distribution of stubborns
weight Edge float in [0, 1] 0.1 False Edge weight
stubborn Node int in {0, 1} 0 False The agent is stubborn or not
vector Node Vector of float in [0, 1] [] False Vector represents the character of the node


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


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

Model Parameters to be specified via ModelConfig :param epsilon: bounded confidence threshold from the HK model (float in [0,1]) :param perc_stubborness: Percentage of stubborn agent (float in [0,1], default 0) :param option_for_stubbornness: Define distribution of stubborns (in {-1,0,1}, default 0) :param similarity: the method uses the similarity or not ( in {0,1}, default 0) :param weight: the weight of edges (float in [0,1]) :param stubborn: The agent is stubborn or not ( in {0,1}, default 0) :param vector: represents the character of the node (list in [0,1], default [])


Model Constructor :param graph: A networkx graph object

WHKModel.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)
WHKModel.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 WHK model simulation on a random graph: we an epsilon value of 0.32 and a weight equal 0.2 to all the edges.

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.WHKModel(g)

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

# Setting the edge parameters
weight = 0.2
if isinstance(g, nx.Graph):
    edges = g.edges
    edges = [(g.vs[e.tuple[0]]['name'], g.vs[e.tuple[1]]['name']) for e in g.es]

for e in edges:
    config.add_edge_configuration("weight", e, weight)


# Simulation execution
iterations = model.iteration_bunch(20)
  1. Milli and G. Rossetti. “Opinion Dynamic Modeling of News Perception”.