Spatial econometric regression models for analyzing geographically-related data interactions.
Overall
score
87%
Core utilities for spatial operations, matrix computations, GMM optimization, and data generation for simulation studies.
def get_lags(w, x, w_lags):
"""
Generate spatial lags of variables.
Parameters:
- w: Spatial weights object
- x: Variable to lag
- w_lags: Number of lags to generate
Returns:
- array: Spatially lagged variables
"""
def get_spFilter(w, lamb, sf):
"""
Apply spatial filtering transformation: (I - λW)x.
Parameters:
- w: Spatial weights object
- lamb: Spatial parameter (scalar)
- sf: Variable to filter
Returns:
- array: Spatially filtered variable
"""
def get_lags_split(w, x, max_lags, split_at):
"""Generate spatial lags with split functionality."""def optim_moments(moments_in, vcX=np.array([0]), all_par=False, start=None, hard_bound=False):
"""
Optimize GMM moments estimation.
Parameters:
- moments_in: Moment conditions function
- vcX: Variance-covariance matrix
- all_par: Boolean, return all parameters if True
- start: Starting values for optimization
- hard_bound: Boolean, enforce parameter bounds
Returns:
- array: Optimized parameters
"""
def get_A1_het(S):
"""Build A1 matrix for heteroskedastic GMM estimation."""
def get_A1_hom(s, scalarKP=False):
"""Build A1 matrix for homoskedastic GMM estimation."""
def get_A2_hom(s):
"""Build A2 matrix for homoskedastic GMM estimation."""
def inverse_prod(w, data, scalar, post_multiply=False, inv_method='power_exp', threshold=0.99999999, max_iter=100):
"""Compute inverse matrix product for spatial filtering."""
def power_expansion(w, data, scalar, post_multiply=False, threshold=0.99999999, max_iter=100):
"""Power expansion approximation for matrix inverse."""class RegressionPropsY:
"""
Helper class providing regression properties for dependent variable.
Properties:
- mean_y: float, mean of dependent variable
- std_y: float, standard deviation of dependent variable
- sig2n: float, sigma-squared (n denominator)
- sig2n_k: float, sigma-squared (n-k denominator)
"""
class RegressionPropsVM:
"""
Helper class providing variance-covariance matrix properties.
Properties:
- vm: array, variance-covariance matrix
- std_err: array, standard errors
- z_stat: array, z-statistics
- p_values: array, p-values
"""import numpy as np
import spreg
from libpysal import weights
# Create spatial weights
n = 100
coords = np.random.randn(n, 2)
w = weights.KNN.from_array(coords, k=5)
# Variable to lag
x = np.random.randn(n, 1)
# Multiple lags (note: lag_spatial is from libpysal, not spreg)
from libpysal.weights.spatial_lag import lag_spatial
wx = lag_spatial(w, x)
print(f"Original variable shape: {x.shape}")
print(f"Spatial lag shape: {wx.shape}")
# Generate lags using spreg utilities
x_multi = np.random.randn(n, 3)
lags = spreg.get_lags(w, x_multi, w_lags=2)
print(f"Multiple spatial lags shape: {lags.shape}")import numpy as np
import spreg
from libpysal import weights
# Example: Set up endogenous variables for spatial model
n = 100
y = np.random.randn(n, 1)
x = np.random.randn(n, 2)
yend = np.random.randn(n, 1) # endogenous variable
q = np.random.randn(n, 1) # instrument
w = weights.lat2W(10, 10)
# Set up for spatial estimation
y_setup, x_setup, yend_setup, q_setup = spreg.set_endog(
y, x, w, yend, q, w_lags=1, lag_q=True
)
print("Data setup for spatial GMM estimation completed")
print(f"Processed shapes: y={y_setup.shape}, x={x_setup.shape}")Install with Tessl CLI
npx tessl i tessl/pypi-spregdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10