My RL Work — Homework and Mini Projects · Fall 2024

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.

Homework highlights

  1. Sep 3, 2024 — Assignment 1: Where RL applies. Three applied problems: Jelly Bean World, conversational AI (intent detection), and reward models for language models — RLHF and RLAIF. Plus the gambler's ruin problem with a biased coin, with states, actions, rewards and transitions written out.
  2. Sep 10, 2024 — Assignment 2: Discounted dynamic programming. The discounted value function and the optimal value function, with the discount factor γ — earlier rewards matter more than later ones, like interest rates and inflation.
  3. Sep 17, 2024 — Assignment 3: Bellman optimality. Restating the optimality conditions for the state-action value function (the Q-function), building the policy from Q by argmax, and proving that randomized policies never beat the optimal deterministic one.
  4. Sep 2024 — Assignment 4: Markov chains. A 10×10 random transition probability matrix drawn from the uniform distribution, with rows summing to one — the Dirichlet distribution helps here.
  5. Sep 2024 — Assignment 5: Reservoir management. Water levels 0 to 100, outflows 0 to 7, reward −(s−50)² − |a−3|, γ = 0.99. Value iteration for the optimal policy, checked numerically, and the Neumann series representation of the value function.
  6. Oct 8, 2024 — Assignment 6: Linear algebra for RL. Solving a 3×3 system by matrix inverse, and an over-determined system by least squares, (AᵀA)⁻¹Aᵀb. Plus a value-function-approximation notebook.
  7. Oct 16, 2024 — Assignment 7: MDP formulation. Managing a portfolio of stocks and bonds — continuous state and action spaces, the Markov property (the current state fully characterizes the process), and Bellman optimality restated for Q.
  8. Oct 29, 2024 — Assignment 8: Fitted value iteration and LSTD. The reservoir problem again, now with a continuous state space — approximating the value function with fitted value iteration, trying at least two choices of features.
  9. Nov 6, 2024 — Assignment 9: LSTD on-policy vs off-policy. A frozen-lake-style grid world with a hand-worked Q-table, showing that LSTD may not converge off-policy while it does on-policy.
  10. Nov 12, 2024 — Assignment 10: Temporal difference and approximation. Deriving the stochastic gradient descent update for tabular features — the TD update, step by step.
  11. Nov 26, 2024 — Assignment 11: Q-learning, Double Q-learning, policy gradient, actor-critic. All four on the mini-project election data — optimizing a policy for the election problem, and asking whether Double Q-learning gives different results from Q-learning.
  12. Dec 9, 2024 — Assignment 12: Gradient descent. First-order optimality — E[y] minimizes E[(y−x)²] — and the gradient descent and stochastic gradient descent updates for computing it.

Mini Project: the Election Campaign

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.

The problem

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.

What the data showed

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.

Intuition, from my Colab notebooks

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.

Beta regression — an idea I wanted to explore

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.

What I tried, and what happened

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.

The notebooks

Main Project: the Principle of Optimality in Lean4

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.

Homework files

Assignment 1 (PDF) Assignment 2 (PDF) Assignment 3 (PDF) Assignment 4 — Markov chains (notebook) Assignment 5 (PDF) Assignment 5 — reservoir (notebook) Assignment 6 (PDF) Assignment 6 — value function approximation (notebook) Assignment 7 (PDF) Assignment 8 (PDF) Assignment 8 — FVI and LSTD (notebook) Assignment 9 (PDF) Assignment 9 — LSTD on vs off policy (notebook) Assignment 10 (PDF) Assignment 11 (PDF) Assignment 11 — Q-learning and policy gradient (notebook) Assignment 12 (PDF)

Resources

cs-851a-rl — homework, mini project and main project repo lean-theorem-proving — the Lean repo My Lean Work — the Lean page My UNH folder in research-experience The mathematics of voting and elections : a hands-on approach — Hodge, Jonathan K., 1980-, 2005 (Internet Archive) — my background reading on the mathematics of voting systems