Undirected Graphs

class netin.UnDiGraph(n: int, k: int, f_m: float, seed: object | None = None)

Undirected graph base model.

Parameters

n: int

number of nodes (minimum=2)

k: int

minimum degree of nodes (minimum=1)

f_m: float

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

seed: object

seed for random number generator

Notes

The initialization is an undirected graph with n nodes and no edges. Then, everytime a node is selected as source, it gets connected to k target nodes. Target nodes are selected depending on the chosen mechanism of edge formation:

References

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

[Karimi2018] (1,2,3,4,5)
  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.

[HolmeKim2002] (1,2,3,4)
  1. Holme and B. J. Kim “Growing scale-free networks with tunable clustering” Phys. Rev. E 2002.

calculate_degree_powerlaw_exponents() tuple[float, float]

Returns the powerlaw exponents for the majority and minority class.

Returns

pl_Mfloat

Powerlaw exponent for the majority class

pl_m: float

Powerlaw exponent for the minority class

fit_degree_powerlaw() tuple[Fit, Fit]

Returns the powerlaw fit of the degree distribution to a powerlaw for the majority and minority class.

Returns

fit_Mpowerlaw.Fit

Powerlaw fit for the majority class

fit_m: powerlaw.Fit

Powerlaw fit for the minority class

generate()

An undirected graph of n nodes is grown by attaching new nodes each with k edges. Each edge is either drawn by preferential attachment, homophily, and/or triadic closure.

For triadic closure, a candidate is chosen uniformly at random from all triad-closing edges (of the new node). Otherwise, or if there are no triads to close, edges are connected via preferential attachment and/or homophily.

Homophily varies ranges from 0 (heterophilic) to 1 (homophilic), where 0.5 is neutral. Similarly, triadic closure varies from 0 (no triadic closure) to 1 (full triadic closure).

  • PA: An undirected graph with h_mm = h_MM in [0.5, None] and tc = 0 is a BA preferential attachment model.

  • PAH: An undirected graph with h_mm not in [0.5, None] and h_MM not in [0.5, None] and tc = 0 is a PA model with homophily.

  • PATC: An undirected graph with h_mm = h_MM in [0.5, None] and tc > 0 is a PA model with triadic closure.

  • PATCH: An undirected graph with h_mm not in [0.5, None] and h_MM not in [0.5, None] and tc > 0 is a PA model with homophily and triadic closure.

get_expected_number_of_edges() int

Computes and returns the expected number of edges based on minimum degree k and number of nodes n

Returns

int

Expected number of edges

get_metadata_as_dict() dict

Returns metadata for a undirected.

get_target(source: int, available_nodes: list[int]) int

Picks a random target node based on preferential attachment.

Parameters

source: int

Newly added node

available_nodes: List[int]

Potential (available) target nodes to connect to

Returns

int

Target node that an edge should be added to

info_computed()

Shows the computed properties of the graph.

info_params()

Shows the parameters of the model.

makecopy()

Makes a copy of the current object.

validate_parameters()

Validates the parameters of the undirected.

class netin.PA(n: int, k: int, f_m: float, seed: object | None = None)

Creates a new PA instance. An undirected graph with preferential attachment.

Parameters

n: int

number of nodes (minimum=2)

k: int

minimum degree of nodes (minimum=1)

f_m: float

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

seed: object

seed for random number generator

Notes

The initialization is an undirected graph with n nodes and no edges. Then, everytime a node is selected as source, it gets connected to k target nodes. Target nodes are selected via preferential attachment (in-degree), see [BarabasiAlbert1999].

static fit(g, n=None, k=None, seed=None)

It fits the PA model to the given graph.

Parameters

g: netin.UnDiGraph

graph to fit the model to

n: int

number of nodes to override (e.g., to generate a smaller network)

k: int

minimum node degree to override (e.g., to generate a denser network k>1)

seed: object

seed for random number generator

Returns

netin.PA

fitted model

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

Returns the probabilities of the target nodes to be selected given a source node. This probability is proportional to the degree of the target 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

available_nodes: set

set of target nodes (ids)

makecopy()

Makes a copy of the current object.

class netin.PATC(n: int, k: int, f_m: float, tc: float, seed: object | None = None)

Creates a new PATC instance. An undirected graph with preferential attachment and triadic closure.

Parameters

n: int

number of nodes (minimum=2)

k: int

minimum degree of nodes (minimum=1)

f_m: float

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

tc: float

probability of a new edge to close a triad (minimum=0, maximum=1.)

seed: object

seed for random number generator

Notes

The initialization is an undirected graph with n nodes and no edges. Then, everytime a node is selected as source, it gets connected to k target nodes. Target nodes are selected via preferential attachment in-degree [BarabasiAlbert1999], or triadic closure tc [HolmeKim2002].

static fit(g, n=None, k=None, seed=None)

It fits the PATC model to the given graph.

Parameters

g: netin.UnDiGraph

graph to fit the model to

n: int

number of nodes to override (e.g., to generate a smaller network)

k: int

minimum node degree to override (e.g., to generate a denser network k>1)

seed: object

seed for random number generator

Returns

netin.PATC

fitted model

get_metadata_as_dict() Dict[str, Any]

Returns the metadata information (input parameters of the model) of the graph as a dictionary.

Returns

dict

Dictionary with the graph’s metadata

get_target_probabilities_regular(source: int, target_list: List[int]) Tuple[array, List[int]]

Returns the probabilities of selecting a target node from a set of nodes based on the preferential attachment.

Parameters

source: int

source node

target_list: set[int]

set of target nodes

special_targets: object

special available_nodes

Returns

Tuple[np.array, set[int]]

probabilities of selecting a target node from a set of nodes, and the set of target nodes

See Also

get_target_probabilities() in netin.PA.

info_params()

Shows the parameters of the model.

makecopy()

Makes a copy of the current object.

validate_parameters()

Validates the parameters of the undirected.

class netin.PAH(n: int, k: int, f_m: float, h_MM: float, h_mm: float, seed: object | None = None)

Creates a new PAH instance. An undirected graph with preferential attachment and homophily.

Parameters

n: int

number of nodes (minimum=2)

k: int

minimum degree of nodes (minimum=1)

f_m: float

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

h_MM: float

homophily (similarity) between majority nodes (minimum=0, maximum=1.)

h_mm: float

homophily (similarity) between minority nodes (minimum=0, maximum=1.)

seed: object

seed for random number generator

Notes

The initialization is an undirected graph with n nodes, where f_m are the minority. Then, everytime a node is selected as source, it gets connected to k target nodes. Target nodes are selected via preferential attachment (in-degree) and homophily (h_**). This model is based on [Karimi2018] known as the “Barabasi model with homophily” or “BA Homophily”.

static fit(g, n=None, k=None, seed=None)

It fits the PAH model to the given graph.

Parameters

g: netin.UnDiGraph

graph to fit the model to

n: int

number of nodes to override (e.g., to generate a smaller network)

k: int

minimum node degree to override (e.g., to generate a denser network k>1)

seed: object

seed for random number generator

Returns

netin.PAH

fitted model

get_metadata_as_dict() dict

Returns the metadata (parameters) of the model as a dictionary.

Returns

dict

metadata of the model

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

Returns the probabilities of selecting a target node from a set of nodes based on the preferential attachment and homophily.

Parameters

source: int

source node

available_nodes: set[int]

set of target nodes

special_targets: object

special available_nodes

Returns

tuple[np.array, set[int]]

probabilities of selecting a target node from a set of nodes, and the set of target nodes

infer_homophily_values() tuple[float, float]

Infers the level of homophily using the analytical solution of the model.

Returns

tuple[float, float]

homophily between majority nodes, and homophily between minority nodes

Notes

See derivations in [Karimi2018].

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.

validate_parameters()

Validates the parameters of the undirected.

class netin.PATCH(n: int, k: int, f_m: float, h_mm: float, h_MM: float, tc: float, tc_uniform: bool = True, seed: object | None = None)

Creates a new PATCH instance. An undirected graph with preferential attachment, homophily, and triadic closure.

Parameters

n: int

number of nodes (minimum=2)

k: int

minimum degree of nodes (minimum=1)

f_m: float

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

h_MM: float

homophily (similarity) between majority nodes (minimum=0, maximum=1.)

h_mm: float

homophily (similarity) between minority nodes (minimum=0, maximum=1.)

tc: float

probability of a new edge to close a triad (minimum=0, maximum=1.)

tc_uniform: bool

specifies whether the triadic closure target is chosen uniform at random or if it follows the regular link formation mechanisms (e.g., homophily) (default=True)

Notes

The initialization is an undirected graph with n nodes and no edges. Then, everytime a node is selected as source, it gets connected to k target nodes. Target nodes are selected via preferential attachment (in-degree) [BarabasiAlbert1999] and homophily (h_**; see netin.Homophily) [Karimi2018] with probability 1-p_{TC}, and with probability p_{TC} via triadic closure (see netin.GraphTC) [HolmeKim2002].

Note that this model is still work in progress and not fully implemented yet.

get_metadata_as_dict() Dict[str, Any]

Returns a dictionary with the metadata of the PATCH graph.

Returns

dict

the graph metadata as a dictionary

get_target_probabilities(source: int, available_nodes: List[int]) Tuple[Any, List[int]]

Returns the probabilities of nodes to be selected as target nodes.

Parameters

source: int

source node id

available_nodes: List[int]

list of available nodes to connect to

Returns

Tuple[np.ndarray, List[int]]

probabilities of nodes to be selected as target nodes, and list of target of nodes

get_target_probabilities_regular(source: int, target_list: List[int]) Tuple[ndarray, List[int]]

Returns the probability of nodes to be selected as target nodes using the preferential attachment with homophily mechanism.

Parameters

source: int

source node id

available_nodes: set

set of target node ids

special_targets: dict

dictionary of special target node ids to be considered

Returns

tuple

probabilities of nodes to be selected as target nodes, and set of target of nodes

infer_homophily_values() tuple[float, float]

Infers analytically the homophily values of the graph.

Returns

tuple

homophily values of the graph (majority, minority)

infer_triadic_closure() float

Infers analytically the triadic closure value of the graph.

Returns

float

triadic closure probability of the graph

info_computed()

Shows the (computed) properties of the graph.

info_params()

Shows the (input) parameters of the graph.

makecopy()

Makes a copy of the current object.

validate_parameters()

Validates the parameters of the undirected.

class netin.TCH(n: int, k: int, f_m: float, h_mm: float, h_MM: float, tc: float, tc_uniform: bool = True, seed: object | None = None)

Creates a new TCH graph. An undirected graph with homophily and triadic closure as link formation mechanisms.

Parameters

n: int

number of nodes (minimum=2)

k: int

minimum degree of nodes (minimum=1)

f_m: float

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

h_MM: float

homophily (similarity) between majority nodes (minimum=0, maximum=1.)

h_mm: float

homophily (similarity) between minority nodes (minimum=0, maximum=1.)

tc: float

probability of a new edge to close a triad (minimum=0, maximum=1.)

tc_uniform: bool

specifies whether the triadic closure target is chosen uniform at random or if it follows the regular link formation mechanisms (e.g., homophily) (default=True)

Notes

The initialization is an undirected graph with n nodes and no edges. Then, everytime a node is selected as source, it gets connected to k target nodes. Target nodes are selected via homophily (h_**; see netin.Homophily) [Karimi2018] with probability 1-p_{TC}, and with probability p_{TC} via triadic closure (see netin.TriadicClosure) [HolmeKim2002].

Note that this model is still work in progress and not fully implemented yet.

get_target_probabilities_regular(source: int, target_list: list[int]) tuple[ndarray, list[int]]

Returns the probability of nodes to be selected as target nodes using the homophily mechanism.

Parameters

source: int

source node id

target_list: set

set of target node ids

special_targets: dict

dictionary of special target node ids to be considered

Returns

tuple

probabilities of nodes to be selected as target nodes, and set of target of nodes

infer_triadic_closure() float

Infers analytically the triadic closure value of the graph. @TODO: This still needs to be implemented. Returns ——- float

triadic closure probability of the graph

info_computed()

Shows the (computed) properties of the graph.

info_params()

Shows the (input) parameters of the graph.

makecopy()

Makes a copy of the current object.