Skip to content

Code to generate maximum-entropy weighted networks with power-law degree and strength distributions.

License

Notifications You must be signed in to change notification settings

ivanvoitalov/whscm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Weighted Hypersoft Configuration Model (WHSCM)

Summary

This repository contains the code to generate networks from the Weighted Hypersoft Configuration Model (WHSCM) with the prescribed power-law degree distribution and tunable super-linear scaling between nodes' strengths and degrees. If you use this code in your research, please kindly cite this repo and/or the original paper.

The directory Paper contains some materials used in the paper, and is not necessary for running the code. This directory contains a Mathematica script used to visualize some functions considered in the paper, and the MLE-inferred Lagrange multipliers $x$, $y$ for synthetic degree/strength sequences. Each degree/strength sequence is saved to the k_s_sequence.dat file, and the Lagrange multipliers corresponding to each degree/strength sequence element are saved to the coordinates.dat file. Each line in the k_s_sequence.dat file has two entries $k_i,s_i$ corresponding to the sampled degree and strength of node $i$. Each line in the coordinates.dat file has the corresponding Lagrange multipliers $x_i,y_i$. The Lagrange multipliers are transformed so that they are supported on the unit interval. To obtain the $\nu$, $\mu$ Lagrange multipliers as in the paper, one has to transform $x$, $y$ as $\nu = \log{(1/x - 1)}$ and $\mu = \log{(1/y)}$.

In case you notice any bugs or have difficulties with running the code, please feel free to contact the authors via GitHub or email.

Dependencies

The WHSCM generator presented here is a combination of Python scripts and C++ code with the OpenMP support for multi-thread generation of links. To use the code, you need standard libraries shipped with Python and:

  • g++ compiler with the C++11 and OpenMP support
  • NumPy
  • SciPy
  • mpmath

Installation

No installation is required. You may directly launch the Python scripts from the Python2/Python3 directories corresponding to the version of Python installed on your computer.

C++ code compilation. The C++ code is supposed to compile itself when the script is first launched. The source C++ code may be found under the src directory. The compiled binary file will be placed in the bin directory which is created when the generator is launched for the first time. In case you need to implement any changes to the C++ source code, you may simply remove the bin directory, and the code will be recompiled the next time it is launched.

For Windows users. We tested the code on Windows 10 using the g++ compiler shipped with Cygwin. Python and the necessary packages may be installed using Cygwin and pip installers. Alternatively, if Python and the necessary libraries are already installed on your system, you may provide Cygwin with a path to them using the .bash_profile configuration file.

Simple Usage Example

Suppose you are given a real weighted network where you observe that network degrees and strengths are distributed as power-laws. Suppose the size of the network is $n = 10^4$, the power-law degree distribution has the exponent $\gamma = 2.5$ and the average degree is $\bar{k} = 10$. Additionally, as observed in many real weighted networks, suppose you observe that nodes' strength scale super-linearly with nodes' degrees, $\bar{s}(k) \sim k^{\eta}$, where $\eta = 1.5$ is the exponent describing this scaling. Moreover, you observe the "baseline" of the strength-degree scaling curve $\bar{s}(k)$ such that the expected strength as a function of degree rescaled by the degree approaches to some given constant $\sigma_0 = 0.1$ such that $\bar{s}(k) / k^{\eta} \rightarrow \sigma_0$.

Now you would like to generate the null model weighted networks that resemble these basic structural properties and are maximally random. To do this, you need to generate graphs from the WHSCM ensemble, and this can be done using the following command:

python Python2/generate_weighted_network.py -n 10000 -g 2.5 -e 1.5 -o output_edgelist.net -k 10.0 --sigma0 0.1

This will create a weighted edge list "output_edgelist.net" in the root directory of the repository. Within this file, each line denotes a connected pair of nodes and their link weight in the following format:

node_i node_j w_ij

Additional Notes

If you would like to also save the latent parameters ("coordinates") $\lambda, \mu$ for each node that are used to generate this WHSCM network instance, you may provide an output path for the coordinates file using the --params_output flag. The terminal command would then look as follows:

python Python2/generate_weighted_network.py -n 10000 -g 2.5 -e 1.5 -o output_edgelist.net -k 10.0 --sigma0 0.1 --params_output output_coordinates.dat

The coordinates file has the following format output format per line:

node_label lambda mu

Note that the current code package should generate networks with the power-law degree and strength distributions for a limited range of input parameters due to approximations used, as explained in the paper. We observed that it behaves well for the mild values of strength-degree non-linearity $\eta \leq 2$ and $\gamma > 2$.

Additional Options

The code supports some additional options that may help users while running the code. The description of all available flags may be accessed by running the following command:

python Python2/generate_weighted_network.py --help

Here we briefly describe these flags and their functionality.

-n. This parameter sets the number of nodes in the WHSCM network.

-g. This parameter sets the power-law exponent of the target degree distribution.

-e. This parameter sets the target exponent of strength-degree super-linear scaling. If it is set to 1, the special case generator equivalent to the unweighted HSCM is launched.

-k. This parameter sets the target average degree of a network.

--sigma0. This parameter sets the "baseline" of the strength-degree scaling.

-o. This parameter sets the output path of the resulting weighted edge list.

--params_output. This parameter sets the output path of the file containing the latent variables $\lambda, \mu$ for each node.

--n_threads. This flag allows to run the network generator in parallel using OpenMP. By default, it is set to 1, however, for faster generation of weighted links, it is advised to set it to the number of available threads.

-R, -a. These flags should be used simultaneously instead of -k and --sigma0 flags. If the parameters $R, a$ of the WHSCM (see the paper for reference) are known either from a custom solver or from previous runs of the generator, there is no need to solve for them again given the same input parameters, so you may use them directly instead of specifying the $\bar{k}, \sigma_0$ parameters.

--solver. This parameter may take the values of 0 and 1. If it is set to 0, then the approximate solver is used to find the $R, a$ parameters of the WHSCM given the $\bar{k}, \sigma_0$ parameters. If it is set to 1, then an exact solver is launched on top of the results of the approximate solver to find a more precise estimate of $R, a$. The approximate solver is significantly faster that the exact one, and finds relatively accurate estimates of the $R, a$ parameters.

-s. This parameter sets the random seed. Note that it should not exceed the maximum value of the C++ uint32 type minus the number of parallel threads, as each parallel thread is seeded with its unique random number generator. Moreover, because of that, to get the same results for a given seed, make sure you use the same number of parallel threads.

-v. This parameter equals either 0 or 1. If it is set to 0, the verbosity of the code is set to minimum. If it is set to 1, the verbosity of the code is set to maximum.

--max_repetitions. This parameter defines the maximum number of restarts for the approximate solver. If the approximate solver fails to find a good solution for the $R, a$ parameters of the WHSCM and reaches this number, the solver stops.

About

Code to generate maximum-entropy weighted networks with power-law degree and strength distributions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published