# Nonlinear Rnn Linear Equivalence

> Linear equivalence of nonlinear recurrent neural networks using two-site cavity method. Shows covariance matrix of large nonlinear RNNs takes same form as linear networks with mean-field order parameters. Activation: nonlinear RNN, linear equivalence, cavity method, mean-field analysis, covariance matrix.

- Skill: `hiyenwong/nonlinear-rnn-linear-equivalence` (Agent Skill)
- Install (CLI): `npx get-skill add hiyenwong/nonlinear-rnn-linear-equivalence`
- Raw SKILL.md: https://api.skillmd.com/api/skills/hiyenwong/nonlinear-rnn-linear-equivalence/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: hiyenwong (https://skillmd.com/u/hiyenwong)
- Updated: 2026-11-22
- Page: https://skillmd.com/skills/hiyenwong/nonlinear-rnn-linear-equivalence

---


# 非线性循环神经网络的线性等价理论

> 使用双位点腔方法证明：大N极限下，具有随机耦合的非线性RNN的协方差矩阵与相同耦合的线性网络形式相同，由平均场序参数决定有效传递函数和噪声谱。

## Metadata
- **Source**: arXiv:2604.23489
- **Authors**: David G. Clark
- **Published**: 2026-04-26
- **Category**: Disordered Systems and Neural Networks (cond-mat.dis-nn), Neurons and Cognition (q-bio.NC)

## Core Methodology

### 核心问题

**背景**: 大N非线性RNN生成高维、可能混沌的活动，其集体结构编码在N×N协方差矩阵中。

**先前局限**: 分析限于低维汇总统计，而非特定耦合实现的高维协方差矩阵。

**新结果**: 大N极限下，典型淬火实现的协方差矩阵与相同耦合的线性网络形式相同。

### 线性等价假设

**假设陈述**:
> 非线性RNN的协方差矩阵 Σ 满足：
> Σ = Σ_linear(g_eff, D_eff)
> 其中 g_eff 为有效传递函数，D_eff 为有效噪声谱

### 双位点腔方法

#### 两种互补推导

**推导1: 线性-非线性分解**

将每个单元的活动分解为：
```
x_i = x_i^linear + δx_i^nonlinear
```

- **线性分量**: x_i^linear — 线性网络响应
- **非线性残差**: δx_i^nonlinear — 非线性引入的偏差

**关键结果**: 不同位点间残差的交叉协方差被强烈抑制

```
Cov(δx_i^nonlinear, δx_j^nonlinear) ≈ 0  (i ≠ j)
```

→ 残差在有效线性网络中充当独立噪声

**推导2: 自洽矩阵方程**

为协方差矩阵写出自洽方程：

```
Σ = A Σ A^T + D
```

其中 A 为有效耦合矩阵，D 为噪声协方差。

**高斯闭合问题**:
- 朴素高斯闭合给出**错误方程**
- 腔方法分离高斯和非高斯贡献
- 两者以相同阶数进入，产生正确方程

### 平均场序参数

#### 有效传递函数

```
g_eff(μ, σ²) = ⟨φ'(h)⟩_h
```
其中 h ~ N(μ, σ²)，φ 为非线性激活函数

#### 有效噪声谱

```
D_eff = ⟨(φ(h) - g_eff·h)²⟩_h + D_input
```

## Implementation Guide

### 协方差矩阵计算

```python
import numpy as np
from scipy.integrate import quad

def linear_equivalence_approximation(N, J, phi, phi_prime, noise_var):
    """
    非线性RNN的线性等价近似
    
    参数:
        N: 网络大小
        J: N×N耦合矩阵 (随机)
        phi: 非线性激活函数
        phi_prime: 激活函数导数
        noise_var: 输入噪声方差
    
    返回:
        Sigma: 预测的协方差矩阵
        g_eff: 有效传递函数
        D_eff: 有效噪声
    """
    # 1. 计算平均场参数
    # 假设稳态高斯分布 N(μ, σ²)
    mu, sigma2 = solve_mean_field_stationary(N, J, phi)
    
    # 2. 计算有效传递函数
    # g_eff = <phi'(h)>，h ~ N(mu, sigma2)
    def integrand_g(h):
        return phi_prime(h) * np.exp(-(h-mu)**2 / (2*sigma2)) / np.sqrt(2*np.pi*sigma2)
    
    g_eff, _ = quad(integrand_g, -np.inf, np.inf)
    
    # 3. 计算有效噪声
    def integrand_D(h):
        return (phi(h) - g_eff*h)**2 * np.exp(-(h-mu)**2 / (2*sigma2)) / np.sqrt(2*np.pi*sigma2)
    
    D_ba, _ = quad(integrand_D, -mu/np.sqrt(sigma2), np.inf)
    D_eff = D_ba + noise_var
    
    # 4. 构建有效线性网络
    # 注意: 需要自洽求解
    A_eff = g_eff * J  # 有效耦合矩阵
    
    # 5. 求解Lyapunov方程: Σ = A Σ A^T + D·I
    # 对于对角D，使用直接方法
    Sigma = solve_discrete_lyapunov(A_eff, D_eff * np.eye(N))
    
    return Sigma, g_eff, D_eff


def solve_mean_field_stationary(N, J, phi, tol=1e-6, max_iter=1000):
    """
    求解平均场稳态方程
    
    自洽方程:
    μ = (1/N) Σ_i <φ(h_i)>
    σ² = (1/N) Σ_i <φ(h_i)²> - μ²
    h_i ~ N(μ, σ²)  (在腔近似下)
    """
    # 耦合强度
    g = np.std(J) * np.sqrt(N)
    
    # 初始化
    mu, sigma2 = 0.0, 1.0
    
    for _ in range(max_iter):
        # 计算新矩
        def integrand_mu(h):
            return phi(h) * np.exp(-(h-mu)**2 / (2*sigma2))
        
        def integrand_second(h):
            return phi(h)**2 * np.exp(-(h-mu)**2 / (2*sigma2))
        
        norm = 1 / np.sqrt(2*np.pi*sigma2)
        
        new_mu = g * norm * quad(integrand_mu, -10, 10)[0]
        second_moment = g**2 * norm * quad(integrand_second, -10, 10)[0]
        new_sigma2 = second_moment - new_mu**2
        
        # 检查收敛
        if abs(new_mu - mu) < tol and abs(new_sigma2 - sigma2) < tol:
            break
            
        mu, sigma2 = new_mu, new_sigma2
    
    return mu, sigma2


def solve_discrete_lyapunov(A, Q):
    """
    求解离散Lyapunov方程: X = A X A^T + Q
    
    使用直接方法或迭代方法
    """
    from scipy.linalg import solve_discrete_are
    
    # 对于稳定性，求解稳态
    # X = solve_discrete_are(A.T, np.zeros_like(A), Q, np.eye(A.shape[0]))
    # 注意: 这实际上是Riccati方程，对于线性Lyapunov需要不同方法
    
    # 使用向量化方法
    n = A.shape[0]
    I = np.eye(n)
    
    # 使用Kronecker积: (I - A⊗A) vec(X) = vec(Q)
    LHS = np.kron(I, I) - np.kron(A, A)
    RHS = Q.flatten()
    
    X_vec = np.linalg.solve(LHS, RHS)
    X = X_vec.reshape(n, n)
    
    return X
```

### 腔方法验证

```python
def cavity_method_verification(N, J, phi, phi_prime, num_samples=100):
    """
    使用数值模拟验证腔方法预测
    """
    # 1. 理论预测
    Sigma_theory, g_eff, D_eff = linear_equivalence_approximation(
        N, J, phi, phi_prime, noise_var=0.1
    )
    
    # 2. 数值模拟
    T = 10000  # 时间步
    x = np.random.randn(N) * 0.1  # 初始化
    
    # 模拟动力学
    trajectory = []
    for t in range(T):
        h = J @ x  # 总输入
        x = phi(h) + np.random.randn(N) * np.sqrt(0.1)  # 更新 + 噪声
        trajectory.append(x.copy())
    
    trajectory = np.array(trajectory[1000:])  # 去除瞬态
    
    # 3. 计算经验协方差
    Sigma_empirical = np.cov(trajectory.T)
    
    # 4. 比较
    correlation = np.corrcoef(
        Sigma_theory.flatten(),
        Sigma_empirical.flatten()
    )[0, 1]
    
    print(f"理论-经验相关性: {correlation:.4f}")
    print(f"平均场参数: g_eff={g_eff:.3f}, D_eff={D_eff:.3f}")
    
    return {
        'theory': Sigma_theory,
        'empirical': Sigma_empirical,
        'correlation': correlation
    }


# 测试不同网络大小
def scaling_analysis():
    """
    验证大N极限下的收敛
    """
    N_values = [100, 500, 1000, 5000]
    correlations = []
    
    for N in N_values:
        # 随机耦合矩阵 (高斯)
        J = np.random.randn(N, N) / np.sqrt(N)
        
        # tanh激活
        phi = np.tanh
        phi_prime = lambda x: 1 - np.tanh(x)**2
        
        result = cavity_method_verification(N, J, phi, phi_prime)
        correlations.append(result['correlation'])
    
    # 随N增大，相关性应趋近1
    print("网络大小 vs 理论-经验相关性:")
    for N, corr in zip(N_values, correlations):
        print(f"  N={N:5d}: {corr:.4f}")
```

## Applications

- **大尺度脑动力学**: 预测全脑协方差结构
- **循环神经网络理论**: 理解RNN的表示能力
- **生态系统稳定性**: 物种丰度波动分析
- **神经网络均值场理论**: 连接微观与宏观动力学

## Pitfalls

- **有限尺寸效应**: N不够大时近似精度下降
- **混沌区域**: 高度混沌时腔方法可能失效
- **非高斯性**: 极端非线性下高斯近似可能不足
- **数值稳定性**: 大N矩阵运算需要 careful 实现

## Related Skills

- neural-dynamics-decision-making
- neural-population-dynamics
- neural-code-dynamics-analysis
- neural-dynamics-universal-translator

