Skip to main content
Back

Berkeley Lab's MLNNs Bake Financial Rules Directly Into Neural Networks

A neurosymbolic framework wires modal logic into neural networks, letting trading agents, portfolio optimizers, and contract classifiers reason about rules, worlds, and risk.

Pro

  • New MLNN paper makes modal logic operators differentiable inside neural networks for finance.
  • Necessity and possibility neurons reason across future timesteps, stress worlds, and agent viewpoints.
  • RL trading agent learned wash-sale-compliant strategy with zero violations versus baseline's one.
  • Inductive mode recovered planted collusion cartel with 0.9997 trust weight on the true edge.
  • On CUAD contracts, MLNN hit 100 percent trap detection versus 96.6 percent baseline, with interpretable Belief-Knowledge decomposition.
  • Code on GitHub, installable via pip install torchmodal.

Deep learning models can spot statistical patterns in markets that no human trader would catch, but they cannot natively represent a sentence like if you sell at a loss, you may not repurchase within 30 days. A new paper from Lawrence Berkeley National Lab argues that Modal Logical Neural Networks (MLNNs) can close that gap by treating logical constraints as differentiable layers inside the network itself.

The work integrates Kripke semantics into neural architectures to enable differentiable reasoning about necessity, possibility, time, and knowledge, and demonstrates the idea across four financial scenarios: compliance for trading bots, collusion detection, crash-proof portfolio optimization, and interpretable contract review.

Why symbolic guardrails keep breaking

Regulated finance sits on a fault line. Neuro-symbolic architectures like Logic Tensor Networks and Logical Neural Networks successfully integrate deep learning with first-order rules, but they treat facts as static. An RL trading agent that discovers wash trading is profitable will happily exploit it, because the regulation forbidding it lives outside the model. Traditional symbolic systems can encode the rule but tend to snap under noisy, high-dimensional market data.

Modal logic, borrowed from philosophy, extends classical logic with two operators: the necessity box, which asserts something is true in every accessible world, and the possibility diamond, which asserts it holds in at least one. A Kripke model is just a set of possible worlds plus an accessibility relation saying which worlds you can jump to from where. That structure is a natural fit for finance, where you constantly reason across future timesteps, stress scenarios, or other traders' viewpoints.

Inside the differentiable logic layer

An MLNN turns that Kripke tuple into trainable components. MLNNs integrate Kripke semantics directly into the neural architecture, with a few key pieces:

  • Possible worlds W stand in for future timesteps, stress scenarios, or agent perspectives.
  • Necessity neurons act as a differentiable min-pooling operator across worlds, so a claim only holds if it holds everywhere.
  • Learnable accessibility A is a parameterized matrix saying which worlds influence which, discovered from data in inductive mode or fixed in deductive mode.
  • Contradiction loss penalizes axiom violations during training rather than post-hoc verification.

The framework separates Belief, the network's statistical confidence, from Knowledge, defined as truth verified across all accessible worlds. That distinction is what the paper leans on to attack hallucinations in robo-advisors.

Four scenarios, four uses of the box operator

Wash sale guardrail. The authors wrap an RL trading agent in a temporal MLNN whose worlds are the next 30 timesteps and enforce the axiom that a sell-at-loss forbids a buy in any accessible future world. In simulation a standard RL agent exploited wash sales for profit 17.96 with one violation, while the MLNN agent found a compliant strategy at profit 12.86 with zero violations, showing the constraint reshapes the policy rather than merely blocking actions.

Collusion hunter. Here the accessibility relation is learned. Traders become nodes, and A represents latent trust. Given the axiom that a spoof by trader i implies profit for some trader j in an accessible world, the model recovered a planted cartel in a five-trader simulation, assigning a trust weight of 0.9997 to the true collusive edge and effectively zero elsewhere.

Crash-proof portfolios. Instead of maximizing expected return, the box operator demands solvency in every stress world. Given a bond at 2 percent versus a risky asset at 10 percent that loses half its value in a crash, a classical optimizer went 100 percent risky. The MLNN allocated 95 percent to bonds, keeping crash value at 0.99 rather than 0.50.

Safe Signer contract review. This is the only scenario with real data. Trained on the CUAD legal contract dataset, a dual-head architecture uses a Proposer head to compute Belief from contract titles and an Auditor head to compute accessibility to four risk worlds ranging from safe to severe. Knowledge is the softmin across those worlds, capped by Belief.

ModelF1Trap detection
Baseline0.88396.6%
Baseline + threshold0.87096.6%
MLNN0.883100%

On ambiguous cases the MLNN reached 62.1 percent accuracy versus 55.2 percent for the baseline while explaining uncertainty sources, sorting predictions into Verified Safe, Trap Detected, and Uncertain buckets. The learned temperature settled at 0.02, effectively enforcing strict modal logic where one toxic clause invalidates the whole contract.

What is actually new

The headline is finance, but the real contribution is a working recipe for making modal operators differentiable end to end. Prior connectionist modal logic work used one network per world with hand-specified accessibility. MLNNs incorporate a weighted, real-valued logic capable of modeling uncertainty and treat the accessibility relation as a learnable object, so the network can discover the structure of possible worlds from data.

Where it is useful, and where it is not

MLNNs shine when constraints are hard to encode as reward signals: temporal rules like holding periods, cross-world safety like solvency under stress, or epistemic distinctions like belief versus verified knowledge. They are a natural safety wrapper around RL policies and classifier heads in regulated pipelines.

The paper is candid about limits. Three of the four scenarios are proof-of-concept simulations, not backtests on real market data, and even the CUAD experiment matches the baseline on F1 rather than beating it, winning instead on trap detection and interpretability. The approach also assumes you can enumerate the relevant worlds, which is tractable for stress scenarios and short time horizons but harder for open-ended agent behavior.

Trying it yourself

Code is public. The torchmodal library ships on PyPI, and the accompanying MLNN_public repo contains the experiments from the original modal logical neural networks paper. A minimal example looks like this:

import torch
from torchmodal import nn, KripkeModel
model = KripkeModel(
    num_worlds=3,
    accessibility=nn.LearnableAccessibility(3, init_bias=-2.0),
    tau=0.1,
)
model.add_proposition("safe", learnable=True)
A = model.get_accessibility()
box_safe = model.necessity("safe", A)  # necessarily safe
loss = model.contradiction_loss()

For teams already shipping RL agents or LLM-based advisors into regulated workflows, MLNNs are worth a look as a way to make constraints part of the gradient rather than part of the incident report.