Skip to content

brycewang-stanford/Auto-Empirical-Research-Skills / skills/11-James-Traina-compound-science/skills/structural-modeling

structural-modeling

Verified

This skill covers structural econometric models. Use when the user is building, estimating, or debugging structural models — including BLP demand estimation, dynamic discrete choice, auction models, or any workflow involving moment conditions, nested fixed-point algorithms, or MPEC formulations. Triggers on "structural model", "moment conditions", "NFXP", "MPEC", "BLP", "random coefficients", "dynamic discrete choice", "CCP", "Rust model", "auction estimation", "GMM objective", "inner loop", "contraction mapping", or convergence/starting value problems in optimization-based estimation.

FileKindSize
SKILL.mdskill md10.4 KB
references/diagnostics-and-se.mdmarkdown4.9 KB
references/estimation-methods.mdmarkdown12.5 KB
references/jax-guide.mdmarkdown2.4 KB

references/jax-guide.md

View on GitHub
# JAX for Structural Estimation

JAX provides automatic differentiation and JIT compilation — valuable for structural models where analytic gradients are tedious.

```python
import jax
import jax.numpy as jnp
from jax import grad, jit

@jit
def bellman_operator(EV, theta, beta, trans_mat):
    """JAX-compatible Bellman operator with autodiff support."""
    RC, theta1 = theta[0], theta[1]
    n_states = EV.shape[0]

    flow_maintain = -theta1 * jnp.arange(n_states, dtype=float)
    cv_maintain = flow_maintain + beta * trans_mat @ EV
    cv_replace = -RC + beta * trans_mat[0, :] @ EV

    # logsumexp for numerical stability
    EV_new = jnp.logaddexp(cv_maintain, cv_replace)
    return EV_new

# Automatic gradient of the objective w.r.t. parameters
# — no hand-derived gradients needed
grad_objective = jit(grad(nfxp_objective_jax, argnums=0))
```

**When to use JAX:**
- Models with many parameters (gradient computation is expensive)
- Need second derivatives (Hessian) for standard errors or Newton steps
- Inner loop can be expressed as a differentiable fixed-point iteration
- Want GPU acceleration for large state spaces

**When NOT to use JAX:**
- Simple models where scipy.optimize works fine
- Models with non-differentiable components (discrete jumps, if-else logic)
- PyBLP already handles the specific model class

## vmap for Simulated Moments

JAX's `vmap` vectorizes computation over simulation draws — useful for MSM and simulated MLE:

```python
from jax import vmap

# Simulate moments for each draw in parallel (no Python loop)
simulate_one = lambda draw: compute_moments(theta, draw)
all_moments = vmap(simulate_one)(simulation_draws)  # (R, n_moments)
simulated_moments = all_moments.mean(axis=0)
```

## Fixed-Point Iteration with JAX

For differentiating through contraction mappings (e.g., BLP inversion, Bellman iteration):

```python
from jax.lax import while_loop

def contraction_jax(ev0, theta, beta, trans_mat, tol=1e-12):
    """Differentiable fixed-point via lax.while_loop."""
    def cond_fn(state):
        ev, ev_prev, _ = state
        return jnp.max(jnp.abs(ev - ev_prev)) > tol

    def body_fn(state):
        ev, _, i = state
        ev_new = bellman_operator(ev, theta, beta, trans_mat)
        return ev_new, ev, i + 1

    ev_init = jnp.zeros(trans_mat.shape[0])
    ev_final, _, n_iter = while_loop(cond_fn, body_fn, (ev_init, ev_init + 1.0, 0))
    return ev_final
```
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexUnverifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall