Jacobian#
Functions
|
Convenience function to compute the Jacobian matrix from an AdaptiveEnv object. |
|
Calculate the second derivative of the log-influence function. |
|
Compute diagonal Jacobian matrix elements using JIT-optimized vectorized operations. |
|
Compute the full Jacobian matrix for multi-agent adaptive dynamics using optimized vectorized operations. |
|
Compute off-diagonal Jacobian matrix elements using JIT-optimized vectorized operations. |
|
Compute the shift matrix Jacobian using JIT-optimized vectorized operations. |
|
Compute second-order shift matrix Jacobian using JIT-optimized vectorized operations. |
|
Compute mixed partial derivative shift matrix Jacobian using JIT-optimized vectorized operations. |
- InflGame.adaptive.jacobian.compute_jacobian_optimized(adaptive_env, position, infl_fshift=False, device='cpu')#
Convenience function to compute the Jacobian matrix from an AdaptiveEnv object.
This high-level wrapper extracts all necessary data from an adaptive environment instance and computes the Jacobian matrix. It handles device placement and automatically computes required intermediate matrices (influence, probability, derivatives).
Usage Example:
from InflGame.adaptive.grad_func_env import AdaptiveEnv from InflGame.adaptive.jacobian import compute_jacobian_optimized # Create and set up environment env = AdaptiveEnv(...) env.gradient_ascent() # Compute Jacobian at final positions jacobian = compute_jacobian_optimized(env, env.agents_pos) # Check stability eigenvalues = torch.linalg.eigvals(jacobian) is_stable = torch.all(eigenvalues.real < 0)
- Parameters:
adaptive_env (AdaptiveEnv) – The adaptive environment containing agent configuration and dynamics.
position (torch.Tensor) – Agent positions at which to evaluate the Jacobian.
infl_fshift (bool) – Whether to include influence function shift corrections.
device (str) – Device for tensor computations (‘cpu’ or ‘cuda’).
- Returns:
Jacobian matrix of shape (N, N) on the specified device.
- Return type:
torch.Tensor
Note
This function automatically computes influence_matrix, prob_matrix, and d_lnf_matrix from the environment. For repeated Jacobian evaluations at the same position, consider caching these intermediate matrices.
- InflGame.adaptive.jacobian.dd_lnf_matrix(agent_id, parameter_instance, infl_type, x=None)#
Calculate the second derivative of the log-influence function.
Computes \(\frac{\partial^2}{\partial x_i^2} \ln(f_i(x_i, b_k))\) where \(f_i\) is the influence kernel for agent \(i\). This second derivative is needed for computing diagonal elements of the Jacobian matrix.
Supported Influence Types:
‘gaussian’: Returns \(-1/\sigma_i^2\) where \(\sigma_i\) is the reach parameter
‘beta’: Uses polygamma function to compute beta distribution second derivatives
- Parameters:
agent_id (int) – Index of the agent (0 to N-1).
parameter_instance (Union[List[float], np.ndarray, torch.Tensor]) – Parameters for the influence function (e.g., reach/sigma values).
infl_type (str) – Type of influence kernel (‘gaussian’, ‘beta’, etc.).
x (Union[float, torch.Tensor], optional) – Agent positions (required for beta kernel, optional otherwise).
- Returns:
Second derivative value as a scalar tensor.
- Return type:
torch.Tensor
- Raises:
ValueError – If influence type is not recognized.
Note
For beta kernels, the computation uses scipy’s polygamma function which requires scalar inputs, so tensor values are converted to float.
- InflGame.adaptive.jacobian.jacobian_diag(resource_distribution, infl_fshift, dd_i, di, pi, shift_i=0, shift_ii=0)#
Compute diagonal Jacobian matrix elements using JIT-optimized vectorized operations.
Calculates \(J_{ii}\), representing the second derivative of agent \(i\)’s utility with respect to its own position. This determines local stability and convergence rate for the agent.
- Parameters:
resource_distribution (Union[List[float], np.ndarray, torch.Tensor]) – Resource values at each bin point.
infl_fshift (bool) – Whether to include influence function shift corrections.
dd_i (Union[float, torch.Tensor]) – Second derivative of log-influence \(\partial^2 \ln(f_i)/\partial x_i^2\) for agent i.
di (torch.Tensor) – First derivative of log-influence \(\partial \ln(f_i)/\partial x_i\) for agent i.
pi (torch.Tensor) – Probability distribution for agent i across bins.
shift_i (Union[float, torch.Tensor]) – Shift function correction for agent i (used if infl_fshift=True).
shift_ii (Union[float, torch.Tensor]) – Second-order shift function correction for agent i (used if infl_fshift=True).
- Returns:
Scalar Jacobian element \(J_{ii}\).
- Return type:
torch.Tensor
Note
All tensor inputs are automatically converted to float32 for consistency.
- InflGame.adaptive.jacobian.jacobian_matrix(num_agents, parameters, agents_pos, bin_points, resource_distribution, infl_type, infl_fshift, Q, infl_matrix, prob_matrix, d_lnf_matrix, x=None)#
Compute the full Jacobian matrix for multi-agent adaptive dynamics using optimized vectorized operations.
The Jacobian matrix \(J\) is an \(N \times N\) matrix where element \(J_{ij}\) represents the partial derivative of agent \(i\)’s utility gradient with respect to agent \(j\)’s position.
Mathematical Formulation:
For agent \(i\) with utility:
\[u_i(x) = \sum_{k=1}^{K} G_{i,k}(x, b_k) B(b_k)\]The Jacobian elements are:
\[J_{ij} = \frac{\partial^2 u_i}{\partial x_i \partial x_j}\]Stability Analysis:
The eigenvalues of \(J\) determine equilibrium stability: - All eigenvalues with negative real parts → stable equilibrium - Any eigenvalue with positive real part → unstable equilibrium - Zero real parts → marginal stability (further analysis needed)
- Parameters:
num_agents (int) – Number of agents in the system.
parameters (Union[List[float], np.ndarray, torch.Tensor]) – Influence function parameters (e.g., reach/sigma values for each agent).
agents_pos (Union[List[float], np.ndarray, torch.Tensor]) – Current positions of all agents.
bin_points (Union[List[float], np.ndarray, torch.Tensor]) – Discretized domain points for resource distribution.
resource_distribution (Union[List[float], np.ndarray, torch.Tensor]) – Resource values at each bin point.
infl_type (str) – Type of influence kernel (‘gaussian’, ‘beta’, ‘multi_gaussian’, etc.).
infl_fshift (bool) – Whether to include influence function shift corrections.
Q (float) – Scaling factor for shift functions (relevant if infl_fshift=True).
infl_matrix (torch.Tensor) – Pre-computed influence matrix of shape (N, K).
prob_matrix (torch.Tensor) – Pre-computed probability matrix of shape (N, K).
d_lnf_matrix (torch.Tensor) – Pre-computed first derivatives of log-influence, shape (N, K).
x (Union[float, torch.Tensor], optional) – Agent positions (used for beta kernel second derivatives, optional).
- Returns:
Jacobian matrix of shape (N, N).
- Return type:
torch.Tensor
Note
This is the primary function for Jacobian computation. It pre-computes shift matrices and second derivatives before assembling the full Jacobian using vectorized operations.
See also
compute_jacobian_optimized- Convenience wrapper for use with AdaptiveEnv objects.
- InflGame.adaptive.jacobian.jacobian_off_diag(resource_distribution, infl_fshift, di, pi, dj, pj, shift_i=0, shift_j=0, shift_ij=0)#
Compute off-diagonal Jacobian matrix elements using JIT-optimized vectorized operations.
Calculates \(J_{ij}\) for :math:`i
- eq j`, representing the cross-derivative of
agent \(i\)’s utility with respect to agent \(j\)’s position. This captures how agents strategically respond to each other’s positions.
- param resource_distribution:
Resource values at each bin point.
- type resource_distribution:
Union[List[float], np.ndarray, torch.Tensor]
- param infl_fshift:
Whether to include influence function shift corrections.
- type infl_fshift:
bool
- param di:
First derivative of log-influence \(\partial \ln(f_i)/\partial x_i\) for agent i.
- type di:
torch.Tensor
- param pi:
Probability distribution for agent i across bins.
- type pi:
torch.Tensor
- param dj:
First derivative of log-influence \(\partial \ln(f_j)/\partial x_j\) for agent j.
- type dj:
torch.Tensor
- param pj:
Probability distribution for agent j across bins.
- type pj:
torch.Tensor
- param shift_i:
Shift function correction for agent i (used if infl_fshift=True).
- type shift_i:
Union[float, torch.Tensor]
- param shift_j:
Shift function correction for agent j (used if infl_fshift=True).
- type shift_j:
Union[float, torch.Tensor]
- param shift_ij:
Mixed shift function correction for agents i and j (used if infl_fshift=True).
- type shift_ij:
Union[float, torch.Tensor]
- return:
Scalar Jacobian element \(J_{ij}\).
- rtype:
torch.Tensor
Note
All tensor inputs are automatically converted to float32 for consistency.
- InflGame.adaptive.jacobian.shift_matrix_jacobian(num_agents, agents_pos, bin_points, Q, infl_matrix)#
Compute the shift matrix Jacobian using JIT-optimized vectorized operations.
This function calculates the first-order partial derivatives of the shift function \(S_i(x_i, b_k)\) with respect to agent positions. The shift function modifies the influence kernel to account for boundary effects or strategic considerations.
The shift Jacobian is used in computing off-diagonal elements of the full Jacobian matrix.
- Parameters:
num_agents (int) – Number of agents in the system.
agents_pos (Union[List[float], np.ndarray, torch.Tensor]) – Positions of the agents (can be list, numpy array, or tensor).
bin_points (Union[List[float], np.ndarray, torch.Tensor]) – Discretized domain points (can be list, numpy array, or tensor).
Q (float) – Scaling factor for the shift function magnitude.
infl_matrix (torch.Tensor) – Pre-computed influence matrix of shape (N, K).
- Returns:
Shift matrix Jacobian of shape (N, K) where N is number of agents and K is number of bins.
- Return type:
torch.Tensor
Note
Input types are automatically converted to torch.float32 tensors for consistency. This function uses JIT compilation for optimal performance.
- InflGame.adaptive.jacobian.shift_matrix_jacobian_ii(num_agents, agents_pos, bin_points, Q, infl_matrix)#
Compute second-order shift matrix Jacobian using JIT-optimized vectorized operations.
This function calculates the second-order partial derivatives \(\frac{\partial^2 S_i}{\partial x_i^2}\) of the shift function with respect to an agent’s own position. These derivatives contribute to the diagonal elements of the full Jacobian matrix.
- Parameters:
num_agents (int) – Number of agents in the system.
agents_pos (Union[List[float], np.ndarray, torch.Tensor]) – Positions of the agents (can be list, numpy array, or tensor).
bin_points (Union[List[float], np.ndarray, torch.Tensor]) – Discretized domain points (can be list, numpy array, or tensor).
Q (float) – Scaling factor for the shift function magnitude.
infl_matrix (torch.Tensor) – Pre-computed influence matrix of shape (N, K).
- Returns:
Second-order shift matrix Jacobian of shape (N, K).
- Return type:
torch.Tensor
Note
Input types are automatically converted to torch.float32 tensors for consistency.
- InflGame.adaptive.jacobian.shift_matrix_jacobian_ij(num_agents, agents_pos, bin_points, Q, infl_matrix)#
Compute mixed partial derivative shift matrix Jacobian using JIT-optimized vectorized operations.
This function calculates the mixed second-order partial derivatives \(\frac{\partial^2 S_i}{\partial x_i \partial x_j}\) of the shift function with respect to positions of two different agents. These derivatives contribute to the off-diagonal elements of the full Jacobian matrix.
- Parameters:
num_agents (int) – Number of agents in the system.
agents_pos (Union[List[float], np.ndarray, torch.Tensor]) – Positions of the agents (can be list, numpy array, or tensor).
bin_points (Union[List[float], np.ndarray, torch.Tensor]) – Discretized domain points (can be list, numpy array, or tensor).
Q (float) – Scaling factor for the shift function magnitude.
infl_matrix (torch.Tensor) – Pre-computed influence matrix of shape (N, K).
- Returns:
Mixed partial derivative shift matrix Jacobian of shape (N, K).
- Return type:
torch.Tensor
Note
Input types are automatically converted to torch.float32 tensors for consistency.