Reinforcement Learning, CS 851A, Fall 2024, University of New Hampshire, with Prof. Marek Petrik — twelve homework assignments, a mini project, and a main project, in date order. This page summarizes my own works and learning, not a general stance on the subject.
The homework built up week by week — from dynamic programming and Bellman optimality, through value function approximation and temporal difference learning, to Q-learning and policy gradients. The mini project applied that to an election campaign, and the main project took the Principle of Optimality into Lean4.
Due December 2, 2024. I translated the professor's Julia code — election.jl, generatesamples.jl and simulate.jl — into Python. I did this mini project on election data at the same time as my main project — verifying the Principle of Optimality in Lean4 — and two additional courses, Introduction to Functional Analysis and Measure Theory, that I took to learn Lean4.
Opinion is positives and negatives, each Poisson: pos λ is the number of people voting “for” from the current state and action, neg λ the number not voting for. Fundraising is Beta(α, β) × 30, where α is positive and β is negative popularity. Balance is the previous balance, minus the action's cost, plus fundraising. The return is E[X], X ~ Beta(α, β), when the sum of rewards is above 0.
Example configuration — 6 campaign options, actions 0 to 5: pos λ = 0.4, 1.5, 1.5, 3.0, 5.0, 6.0; neg λ = 0.5, 1.5, 1.0, 2.0, 3.0, 4.0; costs = 5, 20, 28, 35, 50, 65. Initial state: step 1, balance 100, positive 1, negative 1.
State i influences state i+1: balance falls as steps go on, and later states have lower balances and larger negative differences. The positive-minus-negative difference and the balance move nearly inversely. When the difference was negative, the likelihood of being elected stayed below 50%; when positive, above 50%. The features depend on each other — the feature function has to be a function of all of them. The notebooks hold 3-D plots, pair plots, rolling means, density and kernel-density plots, and beta-regression fits.
The notebooks work through the project in order, and each one answers one question.
First, look at the data. The example data has 5,000 rows; the final data has 15,000 rows, with 6 columns — Action, Step, Balance, Positive, Negative, Reward — and 300 (action, step) pairs across 6 actions and 50 steps, with no missing trajectories. One row, for instance: action 2, step 2, balance 107.2, positive 4, negative 3, reward 0.42. The 3-D plots and the pair plot show the structure: balance falls as steps go on, the positive-minus-negative difference moves nearly inversely to balance, and as the likelihood of being elected rises, so does the balance.
Then, test one gradient step before training anything. In the part-1 notebook I built a small test world — 50 states, 7 actions, rewards a·s, a one-hot feature for each action and a softmax policy — and computed a single policy-gradient step from a uniform policy, where every action starts at probability 1/7. The action values run from 24.5 up to 173.1, and the gradient at the start is [−11.5, −7.5, −4.0, 0.5, 3.3, 9.7, 9.5]: it pushes probability toward the best actions (6 and 7) and away from the worst (1, 2, 3). The quiver plots show that direction for every pair of actions. That is the check that the gradient points the right way, before the full training.
Then, REINFORCE. The policy network takes (state, balance) and outputs action probabilities — Linear, ReLU, Linear, ReLU, Linear, softmax. Each episode's returns are normalized (the baseline), and the loss is −log probability × return, so actions that led to better-than-average returns become more likely. Train 12,000, test 3,000, discount γ = 0.99. I built it step by step with chain-of-thought prompts on Claude, checking each step against Algorithm 2.1 from lecture notes 17.
This was an idea I wanted to explore. It was not required for the class or the project, but I knew it would provide some insights. I had prior background in this: I studied the mathematics of voting systems — The mathematics of voting and elections : a hands-on approach, by Hodge, Jonathan K., 1980- (American Mathematical Society, 2005) — which has been one of the active research topics since 2020. So an election's proportions, and the likelihood of being elected, were familiar ground to me. The return is a proportion — α/(α+β), the likelihood of being elected — so the natural model for it is beta regression, not a linear model over counts. A beta-distributed response has mean μ and precision φ, with α = μφ and β = (1−μ)φ, and a link function g(μ) = Σ xiβi = η maps the mean from its bounded (0,1) space onto the features. The likelihood is L = Π f(yt; μt, φt), with E[y] = μ and Var(y) = μ(1−μ)/(1+φ).
Why it fits this problem, in the small details: the reward at each step is a draw from Beta(positive, negative) minus 0.5, fundraising is Beta(positive, negative) × 30, and the feature function is φ(s,a) ≈ f(balance, cost, Beta(positive, negative)×30). The pair plot had already shown the same shape — as the positive-minus-negative difference turned positive, the likelihood of being elected crossed 50%, and rewards and likelihood move together. The generalized linear models over counts and proportions did not work for this problem; Assignment 11 had already shown that. So beta regression was the better guess, and one I wanted to explore.
I fitted it in the data-comprehension notebook with bambi and arviz: posterior predictions against the observed values, the fitted regression, and the posterior predictive (figures 7–11 in the report). In the beta-regression notebook I also set up a policy whose parameters are the beta α and β for each state-action pair, learned by policy gradient with a logit link. I proposed beta regression as the feature function, but I did not have time to type the proof and implement it in the policy-gradient run — that part is unfinished.
How it was built. I translated the professor's Julia code to Python with Claude Haiku, as demonstrated in class on November 12, 2024 and following the course policy on AI tool use — and I was honest about the assistance in the notebook itself.
REINFORCE with a baseline, a 3-layer policy network (64 → 32 → 1) in PyTorch, ReLU and softmax. I ran for 1,000 epochs but ran out of GPU compute and lost the weights, so I reran 200 epochs: loss down 50%, but not a well-trained policy — sum of rewards −0.0784, action accuracy 0.1533, simulation returns around 0.0035–0.0042. Then a linear feature function with Monte Carlo estimation (lecture notes 17, algorithm 2.1), importance sampling and entropy regularization: F1 0.10679 deterministic, 0.10263 stochastic, simulation returns around 0.013. The model did not learn well — and I say so plainly. A beta-regression feature function looked like the better guess, but I did not have time to prove and implement it.
October 24 – December 1, 2024. The main project was Automated Theorem Proving using Lean4 Prover to verify a foundational theorem of Principle of Optimality in Reinforcement Learning — implementing deterministic dynamic programming theorems in Lean4, with the expectation from the start that the proof might not be completed by December 1, 2024. That work lives on my Lean page.