Formalizing Behavioral Activation in Lean
As the mathematical capabilities of AI systems speed up, especially with the recent proof of the Riemann hypothesis, I keep coming back to the idea of AIs being able to make significant progress in scientific research when formalization is available, because it’s more quickly verifiable. In a field like psychology, formalizing latent variables, conceptualizations, relationships, and theories seems like it could help the field advance much more quickly.
Being a clinical psychology student, I haven't had much exposure to computational psychology or related domains. I’m aware of the CogLean project, for example, and some other examples of behavioral activation, panic disorder, or cognitive avoidance mechanisms being put into code and simulated. As AI models are getting much better at coding, I thought it would be interesting to see what I can accomplish with an AI model directly in Lean.
The start of this project, I would say, was a failure. As I worked with Codex, I didn't realize it was mostly coding in Python and writing statements in Lean that weren't actually proving theorems. That was still useful later when comparing predictive studies, but it was much more helpful when I had Google Gemini's Neon take a look at the work and point out what was happening. Then we went into Lean for real. I had it walk me through what it actually means to write a theory as a theorem in Lean, and how that’s different from just running a simulation in Python.
Something I learned about Lean, compared to simulating patients or groups in regular code or even machine learning, is that Lean checks a mathematical argument that can hold for every person or state that meets the model’s assumptions. Those are universal theorems in Lean 4. While it isn't super common in psychology, I thought it would be interesting to take a clinical conceptualization and formalize it that way. Behavioral activation seemed like a good place to start, especially from a purely behavioral point of view, because there are fewer internal or cognitive elements involved. It is a little more straightforward, and there is a clear downward spiral we could model and see if we could recreate.
Building the Model and Testing Edge Cases
I ended up with 14 theorems total (GitHub). A couple of them were added after looking at the studies and seeing what showed up, including one I had thought about adding earlier and definitely added once the data backed it up, which is that lifted mood from a pleasant activity can carry over into the activity that follows.
First we have activity, how much activity someone’s engaging in. There’s valence, how they’re rating their feeling. Then there’s energy and anhedonia. We ended up adding strain for modeling stress, and demand, which is more like unresolved problems. That’s important when we start talking about avoidance. Then there’s how hard it is to start an activity. With repetition it gets easier over time. And there’s the avoidance tendency. As you withdraw more, it gets more comfortable, and you keep doing that.
The first theorem doesn't really relate specifically to behavioral activation. It is more about making sure the code is working right, confirming that variables like activity, anhedonia, strain, demand, and avoidance tendency stay within reasonable limits as they interact. That also let valence and energy work without arbitrary limits to how high or low they go.
Something interesting that came out of this process for me, was realizing from a coding perspective that high-stress times or low-mood spirals have to sit around some kind of personal homeostasis. So one of the central theorems is that each person has their own set point for things like mood, energy, activity, and life demands, and when we looked at real datasets later we used each person's past data to estimate it. Of course people have different baselines for energy and mood, so building the model around that set point felt right.
What happens if someone’s just grumpy, kind of a shut-in and not doing very much? Will the model push them into a spiral, or can they just stay there? First they have their own homeostasis, their own baseline. Then there’s the amount and quality of activity. If their usual activity is giving them at least their usual amount of reinforcement and there’s no disruption, they should be able to stick around there.
The statement of Theorem 2 in Lean
theorem baseline_stays_fixed (p : Person) (hp : ValidPerson p) (q : ActivityQuality)
(hUsualReinf : p.usualReinforcement ≤ totalReinforcement p (baseline p) q)
(steps : Nat) :
run p q (baseline p) steps = baseline p
First we can look at p : Person. We’re talking about any modeled person who meets the assumptions in ValidPerson p. The less-than-or-equal sign says they’re getting at least their usual reinforcement. And steps : Nat can be zero, one, or any whole number after that. The last line says that after that many steps, the state equals what they started in.
The part I had to work through was how Lean checks that argument. At zero steps they’re still at baseline. Then suppose they’re at baseline after some number of steps. With the same activity quality and reinforcement, one more step leaves them right there. That’s the induction proof. It covers any number of steps. So what happens to that grumpy person in the model? They just hang out there.
I also wanted someone to actually spiral. The first thing I wanted in there was that regular pleasant activity, like a routine, helps maintain mood. When something disrupts that and there’s less activity, there are fewer chances for pleasure or mastery. If anhedonia sets in, the activities they still do can feel less pleasant. And pulling back can bring some short-term relief. Doing less kind of makes it easier to do less.
Building the model alongside Neon was neat because you can run things in Lean, see whether everything resolves the way it should, and ask "what if" questions to look for loopholes. Maybe for some people this isn't revolutionary, and it is kind of a toy model, something to play with and test different scenarios. Beyond the grumpy shut-in, we also checked whether we could model burnout, where someone who is overactive builds up enough strain from high demands or overwork to eventually burn out and drop into a downward mood spiral afterward, and how rewarding or meaningful work gives a bit more resilience at high energy before the crash hits.
Checking the Model Against Real Data
After working through our own example scenarios, we turned to some open datasets. First, does the real-life observational data match up with what our code would predict? And then after that, how well does the model actually predict?
It was neat to see real-world evidence line up with a lot of the theorems. For personal homeostasis and sitting outside the extremes, we looked at the Kuczynski study of 417 community adults and found a good amount of people sitting in a middle gray zone, not severely depressed and not in the super happy zone, just staying there over time. In the Van Roekel study of 68 young adults with persistent anhedonia, the vast majority sat in that same gray zone, reporting some anhedonia even without near-zero activity or social contact. Our model could account for people sitting steadily in that middle zone without forcing them into a spiral.
For pleasant activity helping mood and energy, we saw support in the Kossakowski dataset, where just pure activity didn't show a relationship to next-prompt energy, but pairing activity with whether it was enjoyable did line up with the model. We saw the same thing in the Campbell dataset, where the enjoyment of activity predicted a higher mood and lower stress, even in the more depressed part of the sample.
The final dataset we looked at, from Fisher and colleagues, was significant because it was actually a clinical sample of 40 patients diagnosed with depression, generalized anxiety disorder, or both, tracking mood, energy, anhedonia, and behavioral avoidance four times a day. In our observational checks at four-hour intervals and the next day, participants reported right in line with how the BA model was built, with avoidance leading toward worse mood and higher anhedonia, and low mood, low energy, anhedonia, and prior avoidance all feeding back in to predict more avoidance later.
Testing Prediction Across Timescales
Okay, so it has the right direction of things, but how close is it to the actual? And how tough really is it to beat linear regression? Neon helped me translate the Lean rules into Python. Then we could compare predictions for people held out of training and see how far off we were. We also tried repeating the latest rating, taking a recent average, and taking a person’s average over all their earlier ratings.
Surprisingly, that’s kind of hard to beat. People tend to do more of the same thing. And with smaller samples and self-report, there’s only so much information available from the few variables measured. Even small machine learning models, random forests and other statistical models have little to work with.
In the Kuczynski study of 426 community adults, predicting one to eight weeks ahead, our model did better than repeating the latest rating and mostly edged out a recent average, though linear regression beat our Lean model. There is a pretty straightforward explanation for that, since it was passive observational data from a non-clinical sample where most people were hovering around their usual mean, and linear regression is very good at tracking stable mean reversion.
In the Van Roekel study of 68 young adults with persistent anhedonia across one to eight weeks, our model tied linear regression on both physical activity (0.633 vs. 0.637 mean absolute error) and social activity (0.730 vs. 0.732), while the regression model pulled ahead on symptoms like low mood and anhedonia, especially in weeks four through eight. That study included an advice intervention, and we hadn't wired advice or therapy inputs into the Lean model to account for that shift, which would be possible to add, we just didn't do it here.
In the Campbell study of 254 adults, we had much closer behavioral activation variables like enjoyment, meaning, mastery, and effort, predicting two to three hours ahead instead of weeks. When both models received the enjoyment, meaning, mastery, and effort reported at the target check-in, our BA model had narrowly lower mean absolute error than linear regression on both mood (0.513 vs. 0.514) and stress (0.856 vs. 0.861), though both 95% confidence intervals crossed zero, so it is essentially a tie. In the past-only next check-in comparison without those target-moment qualities, linear regression had slightly lower error on both mood (0.551 vs. 0.552) and stress (0.889 vs. 0.895). It was interesting seeing the same model work reasonably well at both an hours horizon in Campbell and a multi-week horizon in the other two studies.
Then we also ran that same prediction benchmark on the Fisher clinical sample of 40 patients at both four hours ahead and the next day. Here our Lean model clearly beat repeating the latest rating and personal past averages on all four outcomes at both timescales, beat a recent average on mood, and tied linear regression across the board while actually edging it slightly on overall average error at both four hours (1.523 vs. 1.529, with slightly lower raw error on mood, energy, and avoidance) and the next day (1.159 vs. 1.162), though the confidence intervals still crossed zero. For the only clinical sample, it is a welcome addition to the evidence for this small project, and seeing how well it handles the kind of data it is meant to describe seems like a real strength for it.
When we pooled normalized errors across the earlier non-clinical and anhedonia studies, the results looked about like you would expect, with linear regression in first place, our Lean model right behind in second, and the simple averages behind that, before essentially matching linear regression on the Fisher clinical sample. I don't want to take that for granted, because my earlier attempt that relied mostly on Python instead of real theorems in Lean did noticeably worse and was a much more mixed bag. Putting together a mathematically checked model that also holds up as decent forecasting felt much more grounded.

Where This Could Go
I had the thought that this would have been something cool to do on a project in a class in grad school. Pick a mechanism from some part of a conceptualization and see how it plays out. I thought it would be neat to plug in different numbers of pleasant activities and see how many it might take for a change to occur for a given person. I don’t know if this model is specific enough to do that yet. But you can test new predictions and see what comes out of the model. Does mood improve and carry over into a different activity afterward? How does that show up in the data?
So while this was a basic project, it showed that with a little bit of effort I could take a core clinical conceptualization, do internal validation through Lean proofs and edge-case checks, and then do external validation by checking patterns in observational data and testing predictive power.
Stepping back a bit, as AI models become better and better at coding, maybe superhuman in some sense over the next year, there could be an opportunity for AI’s ability in coding to impact other domains beyond just software engineering. Formalizing ideas in Lean is basically coding, and moving seamlessly between Python and Lean is already a strength of AI. Hearing about Claude writing a multi-million-line Lean proof of the Riemann hypothesis and then bringing it down by over 90 percent to tens of thousands of lines of Lean makes me wonder what happens when that kind of rigor reaches a field like psychology. I don't know whether individual prediction will mostly stay with classic machine learning models trained on better real-world data, while Lean gets used more to accurately describe psychological concepts and prove out how mechanisms line up with reality, or if we could eventually build Lean-like proofs that individuals fit into directly, or even software that turns tokens into Lean or another code interface. I’m just imagining what might be possible.
I don’t think reckless hill climbing on every problem is desirable. I do think that given safe, aligned AI models, scientific advancement and an increase in the pace of that advancement are welcome, and should be as human-centered as possible. What it means to be involved in that type of research will change over time and will require adaptation and flexibility. And considering what new fields and opportunities might grow is exciting.
The full Lean code, plain-English guide, and Python validation scripts are up on GitHub under an MIT license if you want to check them out.
Process note: I talked through the project in a series of spoken riffs. Google Gemini’s Neon and Codex helped turn those transcripts into this post. Neon helped correct my explanation of the proof and write up the numerical results. For this version, Codex went back to the riffs to restore more of my wording. Roughly speaking, the prose is about 80% my own spoken wording and 20% AI editing; the ideas, clinical conceptualization, and design direction are about 75% mine and 25% AI suggestions; and the technical execution (Lean proof code, Python scripts, and the figure) is about 10% my direction and review and 90% AI implementation. I directed the clinical conceptualization and reviewed the technical work. AI implemented most of the Lean and Python code and helped with the forecasting, figure, and fact-checking.
The views expressed here are my own and do not represent those of any past, current, or future employer or institution with which I am affiliated.
Subscribe for future posts
If you want new writing at the intersection of AI and psychology, ethics, and implementation of AI in clinical practice, subscribe on Substack.