API Reference
This page documents the main public APIs of networkfm. Throughout, inputs are NumPy arrays (unless stated otherwise).
Data generation for examples and testing
networkfm.database.helpman09
- networkfm.database.helpman09()
Load the pre-processed Helpman–Melitz–Rubinstein trade network dataset shipped with
networkfm.- Returns:
A
pandas.DataFramecontaining the pre-processed dataset read from the package’sdatabase/helpman09.csv.- Return type:
pandas.DataFrame
Notes
Users typically reshape columns from the returned DataFrame into:
G: adjacency matrix, shape(N, N)Xand/orZ: covariate arrays, shape(N, N, K)
networkfm.demo.GenData
- networkfm.demo.GenData(N, directed=True, mutual=True, specification='A1', seed=111)
Generate one synthetic dyadic network dataset from the package’s DGP.
- Parameters:
N (int) – Number of agents (nodes).
directed (bool) – Whether to generate a directed utility component.
mutual (bool) – Whether to generate a mutual/reciprocity component.
specification (str) – DGP setting identifier (e.g.,
"A1","A2","A3","B1","B2","B3"), controlling fixed-effect heterogeneity and other DGP constants.seed (int) – Random seed for reproducibility.
- Returns:
A tuple
(G, X, Z, density, degree, transitivity, separation, trueParameter)whereGis a NumPy adjacency matrix of shape(N, N)Xis a NumPy array of shape(N, N, 1)(directed covariate in the current DGP)Zis a NumPy array of shape(N, N, 1)(mutual covariate in the current DGP)density,degree,transitivityare network summary statisticsseparationis an indicator for separation in the generated sampletrueParameterstores DGP parameters (used in truth/APE simulation workflows)
- Return type:
networkfm.demo.NetworkGenDataMC
- networkfm.demo.NetworkGenDataMC(N, directed=True, mutual=True, specification='A1', mc_sweep=1000)
Generate a Monte Carlo bank of synthetic datasets and pre-allocated result containers.
- Parameters:
- Returns:
A long tuple of NumPy arrays used for Monte Carlo experiments. Key elements include:
G_bank: shape(N, N, mc_sweep), adjacency matrices across replicationsX_bank: shape(N, N, mc_sweep), directed covariate bank (single covariate)Z_bank: shape(N, N, mc_sweep), mutual covariate bank (single covariate)density_mc,degree_mc,transitivity_mc,separation_mc: shape(mc_sweep,), network statistics across replicationsthetaEst_mc,thetaSE_mcand related arrays: pre-allocated containers for estimates, standard errors, and coverage rates (parameters and APEs)
- Return type:
Notes
This function is designed for simulation pipelines: it returns both generated datasets and empty arrays for storing Monte Carlo outputs.
Tetrad/quadruple indices
networkfm.netrics.generate_tetrad_indices
- networkfm.netrics.generate_tetrad_indices(N, full_set=True)
Precompute index mappings required by tetrad logit (conditional likelihood for undirected networks).
- Parameters:
- Returns:
A list
[tetrad_to_dyads_indices, dyad_to_tetrads_dict]wheretetrad_to_dyads_indicesis a NumPy array of shape(n_tetrads, 6), giving for each tetrad the six dyad indices in a vectorized(N, N)layout;dyad_to_tetrads_dictis a dictionary mapping each dyad to a set/list of tetrads containing it (orNoneiffull_set=False).
- Return type:
Usage
Precompute indices once and pass them via
indices=...innetworkfm.fit(..., bc_method="conditional")to avoid repeated combinatorial work.
networkfm.quadlogit.generate_quad_indices
- networkfm.quadlogit.generate_quad_indices(N)
Load and adapt precomputed indices for quadruple logit (conditional likelihood for directed networks without mutual utility).
- Parameters:
N (int) – Number of agents. Index files are shipped for
N=100. ForN < 100, this function filters theN=100indices down to the subset relevant for the requestedN.- Returns:
A tuple
(rearragement_index, permutations)of NumPy arrays (dtypeintp), both 0-based.- Return type:
Notes
This helper is intended for repeated estimation on the same
N(Monte Carlo, robustness checks). The returned indices can be passed via theindicesargument ofnetworkfm.fit(..., bc_method="conditional").
Estimation interface
networkfm.fit
- networkfm.fit(G, X=None, Z=None, directed=True, mutual=True, bc_method='likelihood', algorithm='JML', drop_separation=False, ape=True, X_names=None, Z_names=None, silent=False, indices=None, sv=None)
Fit dyadic network formation models with degree heterogeneity, with optional bias correction and (when available) average partial effects (APEs).
- Parameters:
G (numpy.ndarray) – Network adjacency matrix (dependent variable), shape
(N, N). Typically binary (0/1). The diagonal is ignored (and is commonly set to 0).X (numpy.ndarray) – Covariates entering the directed utility component, shape
(N, N, Kx). Required whendirected=True. The directed covariates may be asymmetric in(i, j)(i.e.,X[i, j, :]need not equalX[j, i, :]). UseX=Nonewhendirected=False.Z (numpy.ndarray) – Covariates entering the mutual/reciprocity component, shape
(N, N, Kz). Required whenmutual=True. Mutual covariates must be symmetric in(i, j)(i.e.,Z[i, j, :] == Z[j, i, :]). UseZ=Nonewhenmutual=False.directed (bool) – Whether to include a directed (sender→receiver) utility component.
mutual (bool) – Whether to include a mutual/reciprocity component. The undirected-network case is obtained by setting
directed=Falseandmutual=True.bc_method (str) –
Bias-correction / estimation method. One of:
"nocorr": uncorrected fixed-effects MLE (no bias correction). (In some code examples you may see"nocorr"used as a synonym.)"likelihood": likelihood correction (main method of the paper); supports APE debiasing whenape=True."estimator": estimator-based correction (for comparison)."conditional": conditional-likelihood methods (tetrad/quadruple logit); fixed effects and APE outputs are not available under this option.
algorithm (str) –
Numerical algorithm for the likelihood-based estimators.
"JML": joint maximum likelihood via a Newton-type optimizer."FP": fixed-point iteration (numerical alternative).
drop_separation (bool) –
Whether to iteratively drop separated nodes in sparse networks. When enabled, the routine removes nodes with no links (or no in-/out-links in directed settings), recomputes degrees, and repeats until no separated nodes remain.
Important: For
bc_method="nocorr"andbc_method="estimator", separation dropping may be enforced internally to ensure estimability, regardless of the user setting. The likelihood correction (and conditional likelihood) is designed to remain operational in sparse settings and may allow keeping all nodes.ape (bool) – If
True, compute APEs when supported. APE outputs are not available underbc_method="conditional".X_names (list[str]) – Optional names for the directed covariates, used in printed summaries.
Z_names (list[str]) – Optional names for the mutual covariates, used in printed summaries.
silent (bool) – If
True, suppress printing the summary table. The fitted result object is still returned.indices (object) – Optional precomputed index objects used by conditional likelihood (
bc_method="conditional"). Seenetworkfm.netrics.generate_tetrad_indices()andnetworkfm.quadlogit.generate_quad_indices().sv (dict) – Optional starting values for optimization. If omitted, the routine initializes all parameters at zero.
- Returns:
A fitted result object storing parameter estimates, standard errors, and (when applicable) APEs.
- Return type: