Intuition The one core idea
REINFORCE tries out actions, watches what reward follows, and then adjusts the knobs of a decision-maker so that actions followed by lots of reward become more likely and actions followed by little reward become less likely. That is the whole story — everything below is just the vocabulary you need to say that story precisely and turn it into math.
This page assumes nothing . Before you touch the REINFORCE algorithm derivation on the parent note, you must be able to read every symbol in it on sight. We build them one at a time, each picture leaning on the last. If a term appears in the parent, it appears here first, defined and drawn.
Reinforcement learning has an agent living inside an environment . Look at the loop below.
s
A state s is a snapshot of the situation the agent is in right now — everything it needs to decide what to do next.
Picture: one particular square on a game board, or the current screen of a video game.
Why the topic needs it: the policy reads s to decide. No notion of "situation", no notion of "good decision in that situation".
a
An action a is one of the choices the agent can make from a state.
Picture: the arrow the agent picks — move Left, move Right, jump.
Why the topic needs it: REINFORCE's whole job is to change how likely each action is .
r (and its subscript r t )
A reward r is a single number the environment hands back after an action: bigger = better.
The subscript t in r t is a time-stamp — the reward received at step number t (steps counted 0 , 1 , 2 , … ).
Picture: a treat dropping out of a machine after you press a button.
Why the topic needs it: reward is the only teaching signal. There is no "correct answer" label — only "how much reward followed".
Before we can say "make this action more likely ", we need the word likely as a number.
A probability is a number between 0 and 1 measuring how sure we are something happens. 0 = never, 1 = always, 0.5 = fifty-fifty. All the probabilities of the possible outcomes must add to 1 (one of them must happen).
Picture: slicing a pie into wedges — each wedge is one action, its slice-size is its probability, the whole pie is 1 .
Intuition Why not just pick the single best action?
A firm "always do X" is a point ; a probability spread is a dial you can nudge smoothly . REINFORCE improves by nudging — so it needs the smooth, probabilistic version. That smoothness is exactly why the topic prefers a probability distribution over a hard choice.
Now we assemble state + action + probability into the central object.
π
A policy π is the agent's strategy: a rule that, given a state s , outputs a probability for each action.
The Greek letter is pi (the letter "p", for policy / probability ).
π ( a ∣ s )
Read the vertical bar ∣ as the word "given" . So π ( a ∣ s ) = "the probability of choosing action a , given that we are in state s ."
Picture: feed a state-photo into a box; out comes the pie-chart of action probabilities from §1.
θ , and π θ
θ (Greek "theta") is a whole bag of tunable numbers — the knobs inside the decision box (for a neural network, these are its weights). Writing π θ means "the policy whose behaviour is controlled by the setting θ ."
Picture: a mixing desk full of sliders. Slide them one way, the pie-chart tilts toward Left; slide them another, it tilts toward Right.
Why the topic needs it: REINFORCE never edits probabilities by hand — it edits θ , and the probabilities follow. "Learning" = "finding a good θ ."
Two standard ways to build π θ appear in the parent: the Softmax Policy (for a handful of discrete actions — turns raw scores into a pie-chart) and the Gaussian Policy (for continuous actions — outputs a bell-curve of where to aim). Both are just concrete recipes for the box.
τ
A trajectory τ (Greek "tau") is the entire sequence recorded during one episode: state, action, reward, next state, and so on until the end.
τ = ( s 0 , a 0 , r 0 , s 1 , a 1 , r 1 , … , s T )
Picture: the full replay of a single game, frame by frame.
T is the final time step — the length of the episode.
τ ∼ π θ
The wavy sign ∼ reads "is sampled from" / "is generated by". So τ ∼ π θ means: this play-through was produced by letting the policy π θ make the choices.
Why the topic needs it: the trajectory is random — same policy, different luck, different τ . That randomness is why expectations (§6) show up.
Rewards arrive over time. We need one number summarizing a whole play-through, and a way to say "sooner is worth a bit more than later."
Definition Discount factor
γ
γ (Greek "gamma") is a number in [ 0 , 1 ] that shrinks future rewards. A reward k steps in the future is multiplied by γ k .
Picture: a treat seen far down the hallway looks smaller. γ = 1 = "the future counts fully"; γ = 0.5 = "each step ahead, halve its worth."
R ( τ )
The return is the total discounted reward of a whole trajectory:
R ( τ ) = ∑ t = 0 T γ t r t
The tall ∑ (capital sigma) means "add up" : run t from 0 to T and sum every term γ t r t .
Picture: the height of the final stacked bar in the figure above.
G t
G t is the return counted only from step t onward :
G t = ∑ k = t T γ k − t r k
The sum now starts at k = t (not 0 ), and the exponent k − t re-zeroes the clock so step t itself is discounted by γ 0 = 1 .
Picture: stand at step t , look forward only , and total up what still lies ahead.
Why the topic needs it: an action can only affect the future . Rewards banked before it are noise for judging it. Using G t instead of R ( τ ) is the parent's "causality" trick that lowers variance.
R ( τ ) with G t
Why it feels right: both are "the reward of the episode."
Fix: R ( τ ) is fixed for the whole trajectory; G t depends on t — it shrinks as t grows because fewer future rewards remain. G 0 (with γ = 1 ) equals R ( τ ) ; later G t are the tail sums.
J ( θ )
J ( θ ) is the average return we'd get if we played many episodes with policy π θ . It is a single number measuring how good the current knob-setting is.
Picture: your long-run average score in a game. Turn the θ -sliders; watch J rise or fall.
Why the topic needs it: this is the mountain REINFORCE climbs. "Learning" = "make J ( θ ) as large as possible."
The return is random (different τ each time). To talk about "the typical return", we need the average operator.
E [ ⋅ ]
E (a fancy "E", for Expected value ) means the probability-weighted average of the thing inside the brackets: run it infinitely many times and take the mean.
The subscript names the source of randomness. E τ ∼ π θ [ R ( τ )] = "average of R ( τ ) over many trajectories drawn from π θ ."
Picture: roll a die a thousand times, average the faces — you land near 3.5 even though no single roll is 3.5 .
So the parent's objective reads, in plain words: "J ( θ ) is the average return you get when the policy π θ plays."
J ( θ ) = E τ ∼ π θ [ R ( τ ) ]
Intuition Why estimate the average by sampling?
We can never actually average over all trajectories — there are too many. Instead we play a handful of episodes and average those. This "average a few samples to guess the true mean" is exactly the Monte Carlo idea, and it is why REINFORCE is called a Monte Carlo policy-gradient method.
To increase J , we need the direction that increases it. That direction is the gradient.
∇ θ J
The upside-down triangle ∇ ("nabla") with subscript θ means: for each knob in θ , how much does J change if I nudge that knob up a hair? Bundle all those sensitivities into one arrow.
Picture: stand on a hillside (the hill = J as a function of θ ). The gradient is the arrow pointing straight uphill , steepest direction.
Definition Gradient ascent
The update θ ← θ + α ∇ θ J takes a small step in the uphill direction. α (Greek "alpha") is the learning rate — the step size.
Picture: repeatedly step uphill until you reach the top (max J ). Descent would step downhill ; that's why the parent flips a minus sign in the code — optimizers descend, so we hand them − J .
Definition Log and the log-derivative trick
log (natural logarithm) turns a product into a sum : log ( x ⋅ y ) = log x + log y . That single fact is why the parent takes the log of the trajectory probability — a long product over time steps becomes a friendly sum.
The Log-Derivative Trick then uses ∇ θ log p = p ∇ θ p to convert a gradient-of-an-average into an average-of-a-gradient, which we can sample. You will meet the full derivation on the parent; here just know log = "turn multiplication into addition."
Definition Value function
V ( s )
The Value Function V(s) V ^ ( s ) estimates the expected return starting from state s — "how good is being here, on average?"
Picture: a number written on each board square saying "typical score from here on."
b ( s ) and Advantage A t
A baseline b ( s ) is a reference level we subtract from G t to ask "was this better or worse than usual?" A natural choice is b ( s ) = V ^ ( s ) , giving the advantage A t = G t − V ^ ( s t ) .
Picture: grading on a curve — an action scoring 105 against an average of 100 is "+5 good"; against an average of 110 it's "−5 bad." Same raw score, opposite lesson.
Why the topic needs it: subtracting the baseline recentres the signal without changing the true direction — the parent's variance-reduction step. Feeding a learned baseline is the first step toward Actor-Critic Methods , and the pattern grows out of Policy Gradient Methods .
Policy pi_theta of a given s
Parameters theta the knobs
Trajectory tau one playthrough
Return and reward-to-go G_t
Objective J theta average return
Expectation E average over luck
Gradient nabla ascent uphill
Log turns product into sum
Cover the right side; can you answer before revealing?
What does π θ ( a ∣ s ) output? A probability for each action, given the state s ; controlled by the knob-bag θ .
What is the vertical bar ∣ read as? "given" — a conditional probability.
What is a trajectory τ ? The full recorded sequence s 0 , a 0 , r 0 , s 1 , … , s T of one episode.
What does γ do and what range is it in? Discounts future rewards by γ k ; it lives in [ 0 , 1 ] .
Difference between R ( τ ) and G t ? R ( τ ) is the whole-episode total; G t is the reward-to-go counted only from step t forward.
What is J ( θ ) in words? The average (expected) return when policy π θ plays.
What does E [ ⋅ ] mean and why is it here? Probability-weighted average; trajectories are random, so we average over them.
What does ∇ θ J point toward? The steepest-uphill direction in J as we vary θ .
Why gradient ascent not descent? We want to maximize J ; descent would minimize it (hence the minus sign in code).
What single algebra fact makes log useful here? log ( x y ) = log x + log y — turns the trajectory product into a sum.
What is the advantage A t ? G t − V ^ ( s t ) : how much better than the state's average return this outcome was.
Why subtract a baseline at all? To recentre the signal and cut variance, without biasing the gradient direction.