Directed Graphs

class netin.DiGraph(n: int, f_m: float, d: float, plo_M: float, plo_m: float, seed: object | None = None)

Directed graph base model.

Parameters

n: int

number of nodes (minimum=2)

d: float

edge density (minimum=0, maximum=1)

f_m: float

fraction of minorities (minimum=1/n, maximum=(n-1)/n)

plo_M: float

activity (out-degree power law exponent) majority group (minimum=1)

plo_m: float

activity (out-degree power law exponent) minority group (minimum=1)

seed: object

seed for random number generator

Notes

The initialization is a directed graph with n nodes and no edges. Source nodes are selected based on their activity given by plo_M (if majority) or plo_m (if minority). Target nodes are selected depending on the chosen mechanism of edge formation.

  • DPAH: preferential attachment (in-degree) and homophily (h**), see netin.DPAH

  • DPA: preferential attachment (in-degree), see netin.DPA

  • DH: homophily (h**), see netin.DH

References

[Espin-Noboa2022] (1,2,3)
  1. Espín-Noboa, C. Wagner, M. Strohmaier, & F. Karimi “Inequality and inequity in network-based ranking and recommendation algorithms” Scientific reports 12(1), 1-14, 2022.

[Karimi2018]
  1. Karimi, M. Génois, C. Wagner, P. Singer, & M. Strohmaier, M “Homophily influences ranking of minorities in social networks”, Scientific reports 8(1), 11077, 2018.

[BarabasiAlbert1999]
    1. Barabasi and R. Albert “Emergence of scaling in random networks”, Science 286, pp 509-512, 1999.

calculate_in_degree_powerlaw_exponents() tuple[float, float]

Returns the power law exponents for the in-degree distribution of the majority and minority class.

Returns

Tuple[float, float]

power law exponents for the in-degree distribution of the majority and minority class

calculate_out_degree_powerlaw_exponents() tuple[float, float]

Returns the power law exponents for the out-degree distribution of the majority and minority class.

Returns

Tuple[float, float]

power law exponents for the out-degree distribution of the majority and minority class

generate()

A directed graph of n nodes is grown by attaching new nodes. Source nodes are selected randomly with replacement based on their activity. Each target node drawn based on the chosen mechanism of edge formation.

  • DPA: A graph with h_mm = h_MM in [0.5, None] is a directed BA preferential attachment model, see netin.DPA.

  • DH: A graph with h_mm not in [0.5, None] and h_MM not in [0.5, None] is a directed Erdos-Renyi with homophily, see netin.DPH.

  • DPAH: A graph with h_mm not in [0.5, None] and h_MM not in [0.5, None] is a DPA model with homophily, see netin.DPAH.

get_activity_distribution() array

Returns the activity distribution of all the nodes in the graph.

Returns

np.array

activity distribution of all the nodes in the graph

get_expected_density() float

Returns the expected edge density (d, the input parameter).

Returns

float

expected edge density

get_expected_number_of_edges() int

Returns the expected number of edges based on number of nodes and edge density.

Returns

int

expected number of edges

get_expected_powerlaw_out_degree_majority() float

Returns the expected power law exponent for the out-degree distribution of the majority class (plo_M, the input parameter).

Returns

float

expected power law exponent for the out-degree distribution of the majority class

get_expected_powerlaw_out_degree_minority()

Returns the expected power law exponent for the out-degree distribution of the minority class (plo_m, the input parameter).

Returns

float

expected power law exponent for the out-degree distribution of the minority class

get_metadata_as_dict() dict

Returns metadata for a directed.

get_sources() array

Returns a random sample with replacement of nodes to be used as source nodes. The sample has the length of the expected number of edges, and the probability of each node to be selected is based on its activity.

Returns

np.array

array of source nodes

get_target(source: int, edge_list: dict, **kwargs) None | int

Returns a target node for a given source node.

Parameters

source: int

source node

edge_list: dict

dictionary of edges

kwargs: dict

additional parameters

Returns

Union[None, int]

target node

Notes

The target node must have out_degree > 0 (the older the node in the network, the more likely to get more links)

info_computed()

Shows the computer properties of the graph.

info_params()

Shows the (input) parameters of the graph.

init_activity()

Initializes the level of activity for each node based on the power law exponents (input param).

init_edges()

Initializes the expected number of edges based on the number of nodes and density of the graph (input param). It also initializes the in- and out-degrees of the nodes.

initialize(class_attribute: str = 'm', class_values: list | None = None, class_labels: list | None = None)

Initializes the model.

Parameters

class_attribute: str

name of the attribute that represents the class

class_values: list

values of the class attribute

class_labels: list

labels of the class attribute mapping the class_values.

makecopy()

Makes a copy of the current object.

validate_parameters()

Validates the parameters of the directed.

class netin.DH(n: int, d: float, f_m: float, plo_M: float, plo_m: float, h_MM: float, h_mm: float, seed: object | None = None)

Creates a new DH instance. A directed graph with homophily.

Parameters

n: int

number of nodes (minimum=2)

d: float

edge density (minimum=0, maximum=1)

f_m: float

fraction of minorities (minimum=1/n, maximum=(n-1)/n)

plo_M: float

activity (out-degree power law exponent) majority group (minimum=1)

plo_m: float

activity (out-degree power law exponent) minority group (minimum=1)

h_MM: float

homophily within majority group (minimum=0, maximum=1)

h_mm: float

homophily within minority group (minimum=0, maximum=1)

seed: object

seed for random number generator

Notes

The initialization is a directed graph with n nodes and no edges. Source nodes are selected based on their activity given by plo_M (if majority) or plo_m (if minority). Target nodes are selected via homophily, see [Espin-Noboa2022].

get_target_probabilities(source: int, available_nodes: None | list[int] | array) array

Returns the probabilities of the target nodes to be selected given a source node.

Parameters

source: int

source node (id)

available_nodes: set

set of target nodes (ids)

special_targets: object

special available_nodes

Returns

probs: np.array

probabilities of the target nodes to be selected

infer_homophily_values() tuple[float, float]

Infers analytically the homophily values for the majority and minority classes.

Returns

h_MM: float

homophily within majority group

h_mm: float

homophily within minority group

info_computed()

Shows the computed properties of the graph.

info_params()

Shows the parameters of the model.

initialize(class_attribute: str = 'm', class_values: list | None = None, class_labels: list | None = None)

Initializes the model.

Parameters

class_attribute: str

name of the attribute that represents the class

class_values: list

values of the class attribute

class_labels: list

labels of the class attribute mapping the class_values.

makecopy()

Makes a copy of the current object.

class netin.DPA(n: int, d: float, f_m: float, plo_M: float, plo_m: float, seed: object | None = None)

Creates a new DPA instance. A directed graph with preferential attachment.

Parameters

n: int

number of nodes (minimum=2)

d: float

edge density (minimum=0, maximum=1)

f_m: float

fraction of minorities (minimum=1/n, maximum=(n-1)/n)

plo_M: float

activity (out-degree power law exponent) majority group (minimum=1)

plo_m: float

activity (out-degree power law exponent) minority group (minimum=1)

seed: object

seed for random number generator

Notes

The initialization is a directed graph with n nodes and no edges. Source nodes are selected based on their activity given by plo_M (if majority) or plo_m (if minority) [Espin-Noboa2022]. Target nodes are selected via preferential attachment [BarabasiAlbert1999].

get_in_degree(n: int) int

Returns the in-degree of node n. This in-degree is not calculated, it is taken from the object in_degrees that is populated while generating the graph.

Parameters

n: int

node id

Returns

int

in-degree of node n

get_target_probabilities(source: int, available_nodes: None | list[int] | array) array

Returns the probabilities for each target node in available_nodes to be selected as target node given source node source.

Parameters

source: int

source node id

available_nodes: Set[int]

set of target node ids

Returns

np.array

array of probabilities for each target node.

makecopy()

Makes a copy of the current object.

class netin.DPAH(n: int, d: float, f_m: float, plo_M: float, plo_m: float, h_MM: float, h_mm: float, seed: object | None = None)

Creates a new DPAH instance. A directed graph with preferential attachment and homophily.

Parameters

n: int

number of nodes (minimum=2)

d: float

edge density (minimum=0, maximum=1)

f_m: float

fraction of minorities (minimum=1/n, maximum=(n-1)/n)

plo_M: float

activity (out-degree power law exponent) majority group (minimum=1)

plo_m: float

activity (out-degree power law exponent) minority group (minimum=1)

h_MM: float

homophily within majority group (minimum=0, maximum=1)

h_mm: float

homophily within minority group (minimum=0, maximum=1)

seed: object

seed for random number generator

Notes

The initialization is a directed graph with n nodes where f_m are the minority. Source nodes are selected based on their activity given by plo_M (if majority) or plo_m (if minority). Target nodes are selected via preferential attachment (in-degree) an homophily (h**). This model is based on [Espin-Noboa2022] which is the directed version of the “BA Homophily” model [Karimi2018].

get_target_probabilities(source: int, available_nodes: None | list[int]) array

Returns the probabilities of selecting a target node from a set of nodes based on preferential attachment and homophily, i.e., in-degree or target and homophily between source and target.

Parameters

source: int

source node

available_nodes: Set[int]

set of target nodes

Returns

np.array

probabilities of selecting a target node from a set of nodes

infer_homophily_values() tuple[float, float]

Infers the level of homophily within the majority and minority groups analytically.

Returns

Tuple[float, float]

homophily within the majority and minority groups

info_computed()

Shows the (computed) properties of the graph.

info_params()

Shows the (input) parameters of the model.

initialize(class_attribute: str = 'm', class_values: list | None = None, class_labels: list | None = None)

Initializes the model.

Parameters

class_attribute: str

name of the attribute that represents the class

class_values: list

values of the class attribute

class_labels: list

labels of the class attribute mapping the class_values.

makecopy()

Makes a copy of the current object.