Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

modeling

modeling

Provide standalone modeling and inference functions for Adjacency matrices.

Each function takes an Adjacency instance as its first argument (adj).

Methods:

NameDescription
bootstrapBootstrap statistics using efficient online algorithms.
convert_bootstrap_results_to_adjacencyConvert bootstrap results dictionary to Adjacency format.
generate_permutationsGenerate permuted versions of an Adjacency instance lazily.
regressRun a regression on an adjacency instance.
social_relations_modelEstimate the social relations model from a matrix for a round-robin design.

Methods

bootstrap

bootstrap(adj, stat, *, n_samples = 5000, save_boots = False, percentiles = (2.5, 97.5), tail = 2, n_jobs = -1, random_state = None, progress_bar = False)

Bootstrap statistics using efficient online algorithms.

Uses memory-efficient bootstrap infrastructure with CPU parallelization. Supports simple aggregation statistics (mean, std, median, sum, min, max).

Parameters:

NameTypeDescriptionDefault
adj(Adjacency) Adjacency instance containing multiple matricesrequired
stat(str) Statistic to bootstrap. Options: - Simple stats: ‘mean’, ‘median’, ‘std’, ‘sum’, ‘min’, ‘max’required
n_samples(int) Number of bootstrap iterations. Default: 50005000
save_boots(bool) If True, store all bootstrap samples (memory intensive). Default: FalseFalse
percentiles(tuple) Percentiles for confidence intervals. Default: (2.5, 97.5)(2.5, 97.5)
tail2‘two’ (two-tailed, default) or 1
n_jobs(int) Number of CPU cores for parallelization. -1 means all CPUs.-1
random_state(int, optional) Random seed for reproducibilityNone
progress_bar(bool) If True, show a progress bar. Default False.False

Returns:

NameTypeDescription
dictDictionary with keys: ‘Z’, ‘p’, ‘mean’, ‘std’, ‘ci_lower’, ‘ci_upper’ (all Adjacency objects). If save_boots=True, also includes ‘samples’.

Examples:

>>> # Simple aggregation
>>> boot = bootstrap(adj, stat='mean', n_samples=1000)
>>> assert 'mean' in boot
>>> assert isinstance(boot['mean'], Adjacency)

convert_bootstrap_results_to_adjacency

convert_bootstrap_results_to_adjacency(adj, result, save_boots = False)

Convert bootstrap results dictionary to Adjacency format.

Helper function to convert numpy arrays from bootstrap functions into Adjacency objects.

Parameters:

NameTypeDescriptionDefault
adj(Adjacency) Adjacency instance (used for matrix_type metadata)required
result(dict) Result dictionary from bootstrap function with keys: ‘mean’, ‘std’, ‘Z’, ‘p’, ‘ci_lower’, ‘ci_upper’, and optionally ‘samples’required
save_boots(bool) If True, include ‘samples’ key in outputFalse

Returns:

NameTypeDescription
dictDictionary with Adjacency objects for each statistic

generate_permutations

generate_permutations(adj, n_permute, random_state = None)

Generate permuted versions of an Adjacency instance lazily.

This is useful for iterative comparisons.

Parameters:

NameTypeDescriptionDefault
adj(Adjacency) Adjacency instancerequired
n_permuteintnumber of permutationsrequired
random_stateint or RandomStaterandom seed for reproducibility. Defaults to None.None

Examples:

>>> for perm in generate_permutations(adj, 1000):
>>>     out = neural_distance_mat.similarity(perm)
>>>     ...

Yields:

NameTypeDescription
Adjacencypermuted version of adj

regress

regress(adj, X, method = 'ols', tail = 2)

Run a regression on an adjacency instance. You can decompose an adjacency instance with another adjacency instance. You can also decompose each pixel by passing a design_matrix instance.

Parameters:

NameTypeDescriptionDefault
adj(Adjacency) Adjacency instancerequired
XDesign matrix can be an Adjacency or DesignMatrix instancerequired
methodtype of regression (default: ols) - only ‘ols’ is currently supported‘ols’
tail2‘two’ (two-tailed, default) or 1

Returns:

NameTypeDescription
stats(dict) dictionary of stats outputs.

social_relations_model

social_relations_model(adj, summarize_results = True, nan_replace = True)

Estimate the social relations model from a matrix for a round-robin design.

Xij=m+αi+βj+gij+ϵijlX_{ij} = m + \alpha_i + \beta_j + g_{ij} + \epsilon_{ijl}

where XijX_{ij} is the score for person i rating person j, mm is the group mean, αi\alpha_i is person i’s actor effect, βj\beta_j is person j’s partner effect, gijg_{ij} is the relationship effect and ϵijl\epsilon_{ijl} is the error in measure l for actor i and partner j.

This model is primarily concerned with partioning the variance of the various effects.

Code is based on implementation presented in Chapter 8 of Kenny, Kashy, & Cook (2006). Tests replicate examples presented in the book. Note, that this method assumes that actor scores are rows (lower triangle), while partner scores are columnns (upper triangle). The minimal sample size to estimate these effects is 4.

Model Assumptions
  • Social interactions are exclusively dyadic

  • People are randomly sampled from population

  • No order effects

  • The effects combine additively and relationships are linear

In the future we might update the formulas and standard errors based on Bond and Lashley, 1996

Parameters:

NameTypeDescriptionDefault
adj(Adjacency) can be a single matrix or many matrices for each grouprequired
summarize_results(bool) will provide a formatted summary of model resultsTrue
nan_replace(bool) will replace nan values with row and column meansTrue

Returns:

TypeDescription
estimated effects: (pd.Series/pd.DataFrame) All of the effects estimated using SRM