# Hegselmann-Krause¶

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.

## Statuses¶

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

## Parameters¶

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

## Methods¶

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

### Configure¶

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])

HKModel.__init__(graph)

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.

HKModel.reset(self)

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

### Describe¶

HKModel.get_info(self)

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

Returns: a dictionary containing for each parameter class the values specified during model configuration
HKModel.get_status_map(self)

Specify the statuses allowed by the model and their numeric code

Returns: a dictionary (status->code)

### Execute Simulation¶

HKModel.iteration(self)

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

Parameters: 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}

## Example¶

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()