Non-linear rate functions for ContinuousObservable #2854
Replies: 2 comments
-
|
This is awesome @EwoutH! Although more cumbersome, I like option 3. Critically, to me this is phase transition of complex systems is a key hard problem. By allowing option 3, we can crowd source the widest net of measures that find indicators for phase transitions. Whether this be specific measures or trained models. (I am a particular fan of Martin Scheffer's work in this area.) To me the goal is find universal signals of phase transition of complex systems, and Mesa is serves as a knowledge bank of different measurements, so having options to increase awareness and an extensible format to let users add new ones to me is the goal. Per your questions for discussion, I would add the base architecture of option 3, as experimental to tweak to the optimal format of the architecture, but then I would argue to let this component of Mesa be at "edge of chaos", by having a low threshold for what measures get in. My thoughts, and excited for the discussion. |
Beta Was this translation helpful? Give feedback.
-
|
Some quick responses from my side to the questions
This directly interacts with how time is represented. In a classic ABM with fixed time steps, it's highly unlikely that the crossing of a treshold coincides exactly with a timestep. Most often, a threshold crossing will occur between two timesteps.
I would favor flexibility.
I would favor "bring your own" because of the wide variety of use cases. I doubt it is possible to cover them all. The main thing is to ensure that we have an easy to use and highly flexible structure for the signaling of threshold crossings.
Given that I favor bring your own, this largely becomes a non-issue for MESA.
Most modelers will have a basic understanding of maths and should be familiar with the distinction so I would not worry about this too much.
Experimental, clearly. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Building on Discussion #2529 and PR #2851 which adds
ContinuousObservableto mesa_signals, we need to address how to handle non-linear rate functions with threshold detection.The Problem
The current implementation assumes linear rates, which allows exact threshold crossing calculation: if
value_new = value_old + rate * elapsed_time, we can solve algebraically for whenvalue_old + rate * t = thresholdto gett = (threshold - value_old) / rate.However, many real-world phenomena follow non-linear dynamics. Energy doesn't always deplete at a constant rate - it might decay exponentially (proportional to current energy level), or follow more complex patterns like logistic growth for populations or oscillatory patterns for circadian rhythms. With arbitrary rate functions
rate_func(value, elapsed, agent), there's generally no closed-form solution for threshold crossings.This creates a fundamental tension: we want to support realistic non-linear dynamics while maintaining the efficiency and precision of lazy evaluation with exact threshold detection.
Common Non-linear Patterns
After linear rates, the most common patterns in agent-based modeling are:
Exponential decay/growth appears in radioactive decay, population growth, chemical reactions, drug metabolism, and temperature equilibration. The differential equation
dv/dt = k*vhas the solutionv(t) = v₀ * e^(k*t), and threshold crossings can be solved exactly:This pattern is probably the single most important non-linear case to support.
Logistic growth models populations with carrying capacity, disease spread, market saturation, and learning curves. Given
dv/dt = r*v*(1 - v/K), the solution isv(t) = K / (1 + A*e^(-r*t))whereA = (K - v₀)/v₀. Threshold crossings require solving:Still analytical, but more complex than exponential.
Power laws appear in allometric scaling, diffusion, and network growth. For
dv/dt = k*v^n, the solution depends on the exponentn. Most values have analytical solutions, though they require careful case handling:Harmonic/oscillatory patterns like
dv/dt = A*sin(ωt + φ) + offsetmodel circadian rhythms, seasonal effects, and economic cycles. These generally require numerical root-finding to locate threshold crossings.Together, linear, exponential, and logistic patterns likely cover around 90% of realistic ABM use cases where exact threshold detection matters.
Possible Approaches
1. Hand-coded Analytical Solutions
We could implement analytical solutions for the most common patterns as distinct classes. A
RateFunctionbase class would define methods for both calculating rates and finding threshold crossings:Users would then write:
Advantages: No additional dependencies, exact threshold detection for common cases, clear semantics, ~200-300 lines of documented mathematical code.
Disadvantages: Only covers predefined patterns, users need to learn the rate function classes, doesn't handle truly arbitrary functions.
2. Numerical Integration with Adaptive Sampling
When checking for threshold crossings, simulate the continuous evolution by sampling at intervals:
Advantages: Works with any rate function, configurable precision, can use more sophisticated integration methods (RK4, adaptive step size).
Disadvantages: Approximation error (might miss rapid crossings between samples), performance overhead with many agents, complexity in choosing sampling resolution, not truly "exact" timing.
3. Hybrid Approach with Tiered Complexity
Combine analytical and numerical methods based on what the user provides:
The implementation would auto-detect which tier to use:
Advantages: Best of both worlds, optimizes common cases, provides escape hatch for complex scenarios, clear performance/accuracy trade-offs.
Disadvantages: More complex implementation, two code paths to maintain, users need to understand when to use which tier.
4. SciPy Integration for Numerical Cases
For arbitrary rate functions, leverage SciPy's excellent ODE solvers:
Advantages: SciPy is already a transitive dependency for some Mesa features, excellent numerical solvers, adaptive step size, handles stiff equations.
Disadvantages: Heavier dependency if made required, still numerical approximation, need to solve forward in time then search for crossing, overhead for simple cases.
5. Lazy with Safety Warnings
Accept the limitations of lazy evaluation but add safeguards:
Advantages: Simple API, automatically adapts to usage patterns, warns about potential issues.
Disadvantages: Still not perfect, warnings might be annoying, feels like a patch rather than a solution.
Existing Libraries
I surveyed existing Python libraries to see if any could help:
SymPy can solve ODEs symbolically and find roots, but it's a heavy dependency (~100MB installed) with significant performance overhead. The symbolic manipulation is powerful but overkill for Mesa's runtime needs. It might be useful as an optional advanced feature, but not as a required dependency.
SciPy provides excellent numerical ODE solvers (
solve_ivp) and root finding (brentq), but no symbolic solutions. Mesa already has SciPy as a transitive dependency for some features. This would be useful for the numerical fallback case but doesn't help with analytical solutions.NumPy has polynomial root finding (
np.roots) which could help for polynomial rate functions, but doesn't solve the general ODE problem. Since Mesa already depends on NumPy, this could be useful for specific cases.There doesn't appear to be a perfect existing library that provides lightweight analytical solutions for common ODE patterns. Most libraries are either too heavy (SymPy) or purely numerical (SciPy).
Questions for Discussion
Beta Was this translation helpful? Give feedback.
All reactions