Jekyll2021-11-16T19:22:58+00:00http://micahcarroll.github.io/feed.xmlMicah CarrollMachine Learning and Convex Optimization Class Notes2018-07-18T02:10:31+00:002018-07-18T02:10:31+00:00http://micahcarroll.github.io/learning/2018/07/18/class-notes<p>Some review notes that I found useful in making connections between ideas in a couple of college classes:</p>
<ul>
<li><a href="/assets/CS189FinalMap.pdf">CS 189</a> (Spring 2018, Prof. Sahai)</li>
<li><a href="/assets/EE127FinalMap.pdf">EE 127</a> (Spring 2018, Prof. El Ghaoui)</li>
</ul>Some review notes that I found useful in making connections between ideas in a couple of college classes:Summer 2018 - CS 188 Discussion Section2018-07-12T02:42:31+00:002018-07-12T02:42:31+00:00http://micahcarroll.github.io/teaching/2018/07/12/CS188<p>Please fill out this <a href="https://goo.gl/Apzio2">anonymous feedback form</a>, anytime during the semester (even multiple times).</p>
<h2 id="section-notes">Section Notes</h2>
<p>If you find any typos or have additional questions, don’t hesitate to reach out at mdc@berkeley.edu.</p>
<table>
<thead>
<tr>
<th>Date</th>
<th>Topic</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>July 2</td>
<td>Policy Iteration + Reinforcement Learning</td>
<td><a href="https://drive.google.com/file/d/1S49omI0l8I9gYl2RVCkNLKiwUxKqMo2t/view">link</a> (unpolished)</td>
</tr>
<tr>
<td>July 9</td>
<td>Approximate Q Learning & Probability</td>
<td><a href="https://drive.google.com/file/d/16HSY-TBoKTsYb0FzOh2NzY9S_X8PYAOf/view">link</a></td>
</tr>
<tr>
<td>July 11</td>
<td>Conditional Independence and D-separation</td>
<td><a href="https://drive.google.com/open?id=1klQyNd-KkbmS2-1-TvohPBs-8_w2DZLm">link</a></td>
</tr>
<tr>
<td>July 18</td>
<td>Variable Elimination and Sampling</td>
<td><a href="/assets/188Notes/Discussion7Final.pdf">link</a></td>
</tr>
<tr>
<td>July 23</td>
<td>Decision Networks, VPI, and HHM</td>
<td><a href="/assets/188Notes/Discussion8Final.pdf">link</a></td>
</tr>
<tr>
<td>July 25</td>
<td>Particle Filtering and Naive Bayes</td>
<td><a href="/assets/188Notes/Discussion9Final.pdf">link</a></td>
</tr>
<tr>
<td>July 30</td>
<td>Perceptrons, MLE, and Neural Nets</td>
<td><a href="/assets/188Notes/Discussion10Final.pdf">link</a></td>
</tr>
</tbody>
</table>Please fill out this anonymous feedback form, anytime during the semester (even multiple times).Sutton & Barto - Reinforcement Learning: Some Notes and Exercises2018-05-17T02:42:31+00:002018-05-17T02:42:31+00:00http://micahcarroll.github.io/learning/2018/05/17/sutton-and-barto-rl<!-- [TOC] -->
<h1 id="a-note-about-these-notes">A note about these notes</h1>
<p><em>I made these notes a while ago, never completed them, and never double checked for correctness after becoming more comfortable with the content, so proceed at your own risk. I am only leaving them online as some people seemed to have found them useful in the past.</em></p>
<p>These notes and exercises are based off of the 15th of May 2018 draft of <em>Reinforcement Learning – An Introduction</em> by Sutton & Barto (the newest version is available <a href="http://incompleteideas.net/book/the-book-2nd.html">here</a>). I tend to summarize the main concepts from the chapters I go through and attempt the exercises. Feel free to let me know if you spot any mistakes.</p>
<p>My notes will not match up with the book as I have skipped things that I was familiar with and delved into more detail in concepts that I found confusing. As background before reading this book, I took <a href="https://edge.edx.org/courses/course-v1:BerkeleyX+CS188+2018_SP/info">CS188</a> in Spring 2017. However, since then I had definitely become pretty rusty on many of the concepts.</p>
<h2 id="1-introduction">1. Introduction</h2>
<h3 id="what-is-reinforcement-learning">What is reinforcement learning?</h3>
<p>It’s picking actions in an environment with the objective of maximizing a reward signal.</p>
<p>Important aspects of RL are:</p>
<ul>
<li><strong>Trial-and-error search</strong>: try things and fail until things work out</li>
<li><strong>Delayed reward</strong>: being able to take actions not only to maximize immediate reward but also more long-term rewards</li>
<li><strong>exploration vs exploitation</strong>: this is a very RL characteristic tradeoff, arising from the fact that in order to find the best way to maximize reward, one could naively just adopt the best strategy seen so far (<em>exploitation</em>) or look for new strategies (<em>exploration</em>). However, an agent can be truly successful only if he combines both of these modes during his learning.</li>
<li><strong>There is a clear goal</strong> that the agent is trying to achieve</li>
<li><strong>Can sense environment and affect it with their actions</strong>: Pacman can see the food and eat the food pellets (changing the state of the environment)</li>
</ul>
<p>Reinforcement learning is commonly considered as a different field from supervised and unsupervised learning, although they share some interesting characteristics.</p>
<p>An attempt to provide a brief description of a RL problem could be:</p>
<p>“we have an <em>interaction</em> between an active decision-making agent and its environment, within which the agent tries to achieve a <em>goal</em> despite <em>uncertainty</em> about its environment.”</p>
<h3 id="rl-vs-supervised-learning">RL vs Supervised Learning</h3>
<p>I feel like in a way reinforcement learning and supervised learning are pretty similar. The book disagrees with this, but I can see how you could consider supervised learning as a form of reinforcement learning, where each item that is labeled correctly gives a positive reward, and viceversa.</p>
<p>However, <em>there is one important difference</em>: reinforcement learning agents take actions sequentially and interact with the environment. In this sense I think that it makes more sense to say that reinforcement learning is a very different beast compared to supervised learning.</p>
<h3 id="rl-and-the-curse-of-dimensionality">RL and the curse of dimensionality</h3>
<p>One of RL’s strong suits is that it partially addresses the curse of dimensionality characteristic of control theory and operations research.</p>
<p><em>How?</em> Mainly (I believe) with temporal difference approaches, in which the value function is defined more simply: we don’t learn a value for each state independently, but learn values based on certain features of states.</p>
<p>Backgammon has about $10^{20}$ states, so we can’t define a policy explicitly for each state. What we do instead is come up with approximations of the value of each state, based on <em>features</em> of the state, that can be handcrafted, or can be also learned with supervised learning methods (for example neural networks).</p>
<h3 id="elements-of-rl">Elements of RL</h3>
<ul>
<li><strong>Policy</strong>: defines the <em>agent’s behaviour</em> at a given time</li>
<li><strong>Reward signal</strong>: the immediate feedback the environment gives at every choice of action of the agent</li>
<li><strong>Value function</strong>: a function that returns for each state $s$ the <em>estimated</em> expected <em>total</em> reward that the agent could gain starting from the state $s$</li>
<li><strong>Model</strong> (optional): Enables the agent to estimate how the environment will react to its actions, enabling better planning</li>
</ul>
<p>One characteristic element of RL compared to other “learning algorithms” is the dependence on <em>value functions</em>. Other solution methods such as <strong>genetic algorithms, genetic programming, and simulated annealing</strong> apply multiple static policies and estimate their effectiveness statistically. We call these <em>evolutionary methods</em>.</p>
<blockquote>
<p>What is the clear distinction between evolutionary methods and RL? Are evolutionary methods reinforcement learning methods or are they separate things?</p>
</blockquote>
<p>RL is more effective as it manages to use the information of each run, while genetic algorithms just evaluate policies in their entirety and throw away all information within each individual run.</p>
<p>Prior information can be incorporated algorithmically in order to make the policy search more efficient.</p>
<p>Some reinforcement learning methods don’t need any kind of environment model at all. These systems are called <strong>model-free</strong>. Their advantage is that they are a lot less computationally intensive, and can give better results if coming up with an accurate model is hard for the problem at hand. In general if <strong>model-based</strong> systems have bad environment approximations, it might be better to just go model-free.</p>
<hr />
<p><strong><em>Exercise 1.1: Self-Play</em></strong></p>
<blockquote>
<p>Suppose, instead of playing against a random opponent, the reinforcement learning algorithm described above played against itself, with both sides learning. What do you think would happen in this case? Would it learn a different policy for selecting moves?</p>
</blockquote>
<p>If both sides were learning against themselves, I believe that it would end up learning the optimal game policy. Interestingly, I think it’s not obvious what the <em>optimal game policy</em> is. I think it’s kind of like the prisoner’s dilemma. If faced with a sub-optimal static-policy adversary, then the policy it would learn would be guaranteed to converge to the optimal policy against that agent.
However, if the agent was trained against itself, it would eventually converge to the <em>policy that would in expectation perform the best against a policy optimized against it</em>. This is kind of a tongue twister.
In particular, I think this is similar to the prisoner’s dilemma as in, the game-theoretic optimal action is to actually take an action that is sub-optimal in an absolute sense.
I think this has a very strong relationship to minimax, even though it is a bit more complicated as the agent is playing against itself rather than against an <em>optimal</em> agent, making it a bit less trivial.</p>
<p>It would learn a different policy as the optimal actions would be different against some arbitrary opponent compared to training against itself. Also, something to consider is that both sides would constantly be changing their strategy in order to have a better chance against the other (basically itself). I don’t think it’s completely trivial to even state that convergence is guaranteed, as I can imagine a scenario in which the policy just bounces back and forth between a absolutely optimal and game-theoretic optimal stance.</p>
<hr />
<p><strong><em>Exercise 1.2: Symmetries</em></strong></p>
<blockquote>
<p>Many tic-tac-toe positions appear different but are really the same because of symmetries. How might we amend the learning process described above to take advantage of this? In what ways would this change improve the learning process? Now think again. Suppose the opponent did not take advantage of symmetries. In that case, should we? Is it true, then, that symmetrically equivalent positions should necessarily have the same value?</p>
</blockquote>
<p>If we have a tabular approach, where we store the expected value associated with each state, we could enforce some constraints of equality between all states that are identical up to symmetry. This would definitely make sense when playing against an optimal player, for which states identical up to symmetry should have the same value.
However, if the opponent has a skewed policy, in which he behaves differently given states identical up to symmetry, then we shouldn’t have these symmetry constraints enforced: in fact, not enforcing them might enable us to exploit the biases of the opponent. If the opponent behaves in a particularly sub-optimal way in a certain symmetric state, then the value we associate with that state should be higher than the other states that are identical up to symmetry.</p>
<p>If instead we are using a temporal difference approach, enforcing symmetry constraints could be a lot easier or harder. In general, naive approaches would already enforce symmetry constraints implicitly, as the most common features that come to mind are usually symmetrical. However, if we wanted to break this and enable non-symmetrical values, that would be harder and require a lot more thought.</p>
<hr />
<p><strong><em>Exercise 1.3: Greedy Play</em></strong></p>
<blockquote>
<p>Suppose the reinforcement learning player was greedy, that is, it always played the move that brought it to the position that it rated the best. Might it learn to play better, or worse, than a non-greedy player? What problems might occur?</p>
</blockquote>
<p>I believe that it would be guaranteed to play equally or worse compared to a normal RL agent, under reasonable assumptions. This is because RL agents will try to optimize not only immediate but also long-term rewards, while a greedy agent would only optimize for immediate ones. This could cause it to prioritize the wrong things and optimize getting small rewards in the short term missing out on bigger long term ones.</p>
<hr />
<p><strong><em>Exercise 1.4: Learning from Exploration</em></strong></p>
<blockquote>
<p>Suppose learning updates occurred after <strong>all</strong> moves, including exploratory moves. If the step-size parameter is appropriately reduced over time (but not the tendency to explore), then the state values would converge to a set of probabilities. What are the two sets of probabilities computed when we do, and when we do not, learn from exploratory moves? Assuming that we do continue to make exploratory moves, which set of probabilities might be better to learn? Which would result in more wins?</p>
</blockquote>
<p>If we perform learning updates even when performing explanatory moves, we will underestimate the actual value of the states we update after exploratory moves. This is because, unless the previous estimates were wrong, the action we pick is actually sub-optimal, and will lead to a worse state than the one we would have reached by exploiting our policy.</p>
<p>Therefore:</p>
<ul>
<li>When we perform the updates also for exploration, our probabilities (value function values) will be lower bounds on the optimal ones</li>
<li>Otherwise, we will have unbiased values</li>
</ul>
<p>Assuming that we continue to make explanatory moves, it’s better to learn the unbiased ones. If a action ($s \to a_{explore} \to s’$) that we choose during exploration is actually better than the greedy action, one we explore the state $s’$ and update it’s value, it could overtake the current best action $a_{exploit}$ for state $s$.</p>
<p>Given that the estimates for the values for the first case are biased, they would perform at least as bad as the unbiased estimates. I’m not totally sure if the bound would be strict or not.</p>
<h1 id="part-i-tabular-solution-methods">Part I: Tabular Solution Methods</h1>
<p>In this part we only deal with problems in which state and action spaces are sufficiently small that they can be stored in a table (or equivalently, an array). In this case it is often possible to find exact solutions, i.e. the <em>optimal policy</em> for our agent.</p>
<p>Problems with just one state are called <strong>bandit problems</strong>.</p>
<blockquote>
<p>return to this after having finished part one</p>
</blockquote>
<h2 id="2-multi-armed-bandits">2. Multi-armed Bandits</h2>
<ul>
<li><strong><em>evaluative</em> feedback</strong>: indicates how good an action taken was, but not whether it was the worst or best action possible</li>
<li><strong><em>instructive</em> feedback</strong>: indicates the correct (best) action, independently of the action actually taken</li>
</ul>
<p><em>Instructive</em> feedback is that the basis of supervised learning. <em>Evaluative</em> is instead at the basis of RL.</p>
<p>Another important distinction is that between <em>associative</em> and <em>non-associative</em> problems: non-associative problems are simpler, as one doesn’t have to care about the combination of action and state, but just about the action. This means that taking a certain action right now will not affect the possibilities of the agent in the future in any way.</p>
<h3 id="21-a-k-armed-bandit-problem">2.1 A <em>k</em>-armed Bandit Problem</h3>
<p>In a <em>k</em>-armed bandit problem there are k possible actions to choose from, and after you select an action you get a reward, according to a distribution corresponding to that action. The goal is to be able to identify which are the best actions as soon as possible and concentrate on them (or more likely, <em>the one</em> best/optimal action). The problem becomes more complicated if the reward distributions are non-stationary, as our learning algorithm must realize the change in optimality and change it’s policy.</p>
<p>In the <strong>stationary</strong> version, each of our actions has an <em>expected reward</em> given that that action is selected, called <strong><em>action value</em></strong>.</p>
\[q_\star(a) = \mathbb{E}[R_t \ \mid \ A_t = a]\]
<p>We want $Q_t(a)$, our approximation of the action value, to be close to $q_\star(a)$, the <strong>true value</strong>. $Q_t(a)$ is also commonly called the <strong>$Q$ Value</strong> for action $a$.</p>
<p>One of the main ideas in the <em>exploration vs exploitation</em> is that if we want $Q$ to be close to $q$, it isn’t sufficient to take greedy (exploitative) moves all the time. In fact, with exploring – <strong>reward is lower in the short run, during exploration, but higher in the long run because after you have discovered the better actions, you can exploit them <em>many times</em></strong>.</p>
<p>Whether to explore or exploit is a very hard question, and depends in a very complex way on the precise values of estimates, uncertainties, and number of remaining steps. There are some methods to determine what is the best tradeoff, <em>but they usually make very limiting assumptions</em>, such as stationarity and prior knowledge that can be impossible to verify or are just not true in most applications.</p>
<blockquote>
<p>Does the definition above for action value contrast with CS188? Isn’t value in CS188 defined as “expected rewards from <strong>all</strong> following actions”?</p>
</blockquote>
<p>I think the conflict is resolved by the fact that in this case we’re considering a stationary problem with infinite horizon and no discount, so it doesn’t make sense to define value as expected rewards up to infinity, as they would be infinite.</p>
<h3 id="22-action-value-methods">2.2 Action-value Methods</h3>
<p><strong>Action-value methods</strong> are all methods that estimate values of actions and that use these estimates to pick the agent’s next action. Remember that the <em>true</em> value of an action is it’s expected/mean reward.</p>
<p>One natural way to estimate the value of a state is just taking the <strong>average reward</strong> obtained every time we reach that state:</p>
\[Q_t(a) = \frac{\text{sum of rewards when action `a` taken before time t}}{\text{number of times action `a` was taken}}\]
<p>As the denominator goes to infinity, our estimate converges to the real value $q_\star(a)$. This is called the <strong>sample-average</strong> method.</p>
<p>The simples method for <em>selecting actions</em> is instead the <em>greedy</em> approach:</p>
\[A_t = argmax_a Q_t(a)\]
<p>A better, but still simple approach is the <em>$\epsilon$-greedy</em> approach, in which the greedy option is selected with probability $1-\epsilon$ and a random action (among the remaining) is selected with probability $\epsilon$.</p>
<hr />
<p><strong><em>Exercise 2.1</em></strong></p>
<blockquote>
<p>In $\epsilon$-greedy action selection, for the case of two actions and $ε = 0.5$, what is the probability that the greedy action is selected?</p>
</blockquote>
<p>It is $\epsilon = 0.5$ if we follow the convention that when choosing the random action (if we’re in the $\epsilon$ probability case) we exclude the greedy action.</p>
<p>Otherwise:
\(\begin{equation}
\begin{split}
P(\text{greedy}) & = P(\text{pick greedy} \mid \text{exploit}) P(\text{exploit}) + P(\text{pick greedy} \mid \text{exploration}) P(\text{exploration}) \\
& = 1 * (1 - \epsilon) + \frac{1}{\mid A\mid} \epsilon \\
& = 1 - (1 - \frac{1}{\mid A\mid})\epsilon \\
& = 1 - \frac{\mid A\mid - 1}{\mid A\mid} \epsilon
\end{split}
\end{equation}\)</p>
<!-- $$P(\text{greedy action is taken}) = P(\text{pick greedy} \mid \text{exploit}) P(\text{exploit}) + P(\text{pick greedy} \mid \text{exploration}) P(\text{exploration})$$$$ = 1 * (1 - \epsilon) + \frac{1}{\midA\mid} \epsilon = 1 - (1 - \frac{1}{\midA\mid})\epsilon =1 - \frac{\midA\mid - 1}{\midA\mid} \epsilon$$ -->
<p>I think that enabling to pick the greedy action when exploring should not be allowed as it just complicates what it means to be $\epsilon$-greedy. In fact, as seen above, this does not mean under this approach that we pick a non-greedy action $\epsilon$ proportion of the time.</p>
<h3 id="23-the-10-armed-testbed">2.3 The 10-armed Testbed</h3>
<p>10-armed testbed is a test set for the performance of these algorithms. It’s composed of $N=2000$ randomly generated <em>k</em>-bandit problems, with $k=10$. The <em>action values</em> $q_\star(a)$ were selected at random from a normal distribution with mean 0 and variance 1. The <em>reward distributions</em> for each action were instead still gaussian, and centered at $q_\star(a)$ with variance 1.</p>
<p>For any learning method, we can benchmark it by measuring performance and behaviour over 1000 timesteps when applied to one of the $N$ bandit problems. This makes up one <em>run</em>. Repeating this for $N$ independent <em>runs</em>, each with a different bandit problem, enables us to measure expected behaviour.</p>
<p>We see that low $\epsilon$ converges slower, but to higher values. See book for details/images. It’s all pretty straightforward.</p>
<p>Things get a bit more complicated if the distribution of the rewards is non-stationary, i.e. it changes over time. In this case we always want to be exploring, and probably with a rate that is tuned to do well with the rate of change of the reward functions.</p>
<blockquote>
<p>Would be interesting to look into this above point more.</p>
</blockquote>
<hr />
<p><strong><em>Exercise 2.2: Bandit example</em></strong></p>
<blockquote>
<p>Consider a k-armed bandit problem with $k = 4$ actions, denoted 1, 2, 3, and 4. Consider applying to this problem a bandit algorithm using $ε$-greedy action selection, sample-average action-value estimates, and initial estimates of $Q_1(a) = 0$, for all $a$. Suppose the initial sequence of actions and rewards is $A_1 = 1, R_1 = 1, A_2 = 2, R_2 = 1, A_3 = 2, R_3 = 2, A_4 = 2, R_4 = 2, A_5 = 3, R_5 = 0$. On some of these time steps the $ε$ case may have occurred, causing an action to be selected at random. On which time steps did this definitely occur? On which time steps could this possibly have occurred?</p>
</blockquote>
<p>At timestep $2$ this <em>definitely</em> occurred, as we know that the average reward associated with $A_1$ is $1$ and so $Q_2(a_1) > Q_2(a_2) = 0$. Therefore, choosing $A_2 = 2$ must have been the result of a exploration step. Similarly, this must have also occurred at timestep $5$.</p>
<p>It might have occurred at timestep $1$, depending on how the algorithm picks among actions with equal $Q$ values. The same is true for timestep $3$, at the beginning of which $a_2$ and $a_1$ have the same $Q$ value.</p>
<p>If when one is picking a random action, one chooses among <em>all</em> actions rather than just all the ones currently considered suboptimal, then it is possible that a random action was selected at any of the timesteps.</p>
<hr />
<p><strong><em>Exercise 2.3</em></strong></p>
<blockquote>
<p>In the comparison shown in Figure 2.2, which method will perform best in the long run in terms of cumulative reward and probability of selecting the best action? How much better will it be? Express your answer quantitatively.</p>
</blockquote>
<p>The $\epsilon = 0.01$ method will perform the best in the long run. This is because – assuming stationarity – it is guaranteed to find the optimal action and then exploit it. Once the algorithm has found the optimal action, it will exploit it 99% of the time, while the $\epsilon = 0.1$ will only exploit the optimal action 90% of the time.</p>
<p>Let’s say that the average $Q$ value of suboptimal values is $\bar{q}_s$. Then we know that:</p>
<ul>
<li>$\epsilon = 0.01$ will have an average reward of $0.01 * \bar{q}<em>s + 0.99 * q</em>{opt}$</li>
<li>$\epsilon = 0.1$ will have an average reward of $0.1 * \bar{q}<em>s + 0.9 * q</em>{opt}$</li>
</ul>
<p>Depending on how large the gap between $\bar{q}<em>s$ and $q</em>{opt}$ is, this could change the performance of the algorithm very much.</p>
<blockquote>
<p>From the figure mentioned in the exercise above: I wonder why it takes so long for the algorithm to figure it out – 1000 timesteps is a long time. It would be nice to see how the belief changes over time.</p>
</blockquote>
<p>Note that $\epsilon=0.01$ means that it only explores a new action on average 10 times within the 1000 timesteps. The chance that the optimal action is not explored at all is decently high, and the chance that even if explored at one timestep, it’s reward was not decisively better than all other actions, is even higher. Therefore it makes sense that it would require quite a long time to converge. The graph looks the way it does (with incremental improvement) not because that’s the way it would look for just one bandit game, but because it’s averaged out. It shows the incremental discovery of the optimal action across all the 2000 games.</p>
<h3 id="24-incremental-implementation">2.4 Incremental Implementation</h3>
<p>How can these averages be computed in a computationally efficient manner?</p>
<p>Basically just store the average and update it at each timestep. (see book for details)</p>
<p>Given $Q_n$ and the $n$th reward $R_n$ (note that the $n$th reward forms the $Q_{n+1}$ estimate as $Q_1$ is based off of a prior – usually just set to zero), we get:</p>
\[Q_{n+1} = \frac{1}{n} \sum_i^n R_i = \frac{1}{n} \Bigg(R_n + (n-1) \frac{1}{(n-1)}\sum_i^{n-1} R_i\Bigg) = \frac{1}{n} \Big(R_n + (n-1) Q_{n}\Big) = Q_n + \frac{1}{n} \Big[R_n - Q_{n}\Big]\]
<p>The update formula turns out to take the form:</p>
\[NewEstimate \leftarrow OldEstimate + StepSize [Target - OldEstimate]\]
<p>This is a form that comes up very often in RL. Note that $[Target - OldEstimate]$ can be considered a measure of <strong>error</strong> of the estimate.</p>
<p>Note that the step-size parameter changes from timestep to timestep, depending on the number of times that one has chosen that action. Usually <strong>step-size</strong> is denoted by $\alpha_n$.</p>
<h3 id="25-tracking-a-non-stationary-problem">2.5 Tracking a Non-stationary Problem</h3>
<p>If the reward probabilities change over time, we don’t want to give the same weight to all observations when calculating our estimates for the mean-rewards for each action. <strong>We want to give more weight to the most recent observations</strong>.</p>
<p>We can modify the formula derived in <a href="#2.4">2.4</a> to have a <strong>constant step size $\alpha$</strong>. Note that the step size found in 2.4 decreased with the number of observations ($\frac{1}{n}$), indicating that the weight given to observations decreases over time. However, we want the weight given to <em>new</em> observations to be the same, but that given to old observations to <em>decrease</em>.</p>
<p>By setting $\alpha$ as a constant, we effectively achieve this, as we get (see book for full derivation):</p>
\[Q_{n+1} = Q_n + \alpha [R_n - Q_n] = ... = (1-\alpha)^n Q_1 + \sum_{i=1}^n \alpha (1-\alpha)^{n-1} R_i\]
<p>This is a form of <em>weighted average</em> because the weights sum up to one: $(1-\alpha)^n + \sum_{i=1}^n \alpha (1-\alpha)^{n-1} = 1$</p>
<p>The above expression shows us that the weight given to each reward $R_i$ is exponentially decreasing as time progresses. This is also sometimes referred to as <em>exponential recently-weighted average</em>.</p>
<p>Instead of having a constant step size, it might sometimes be beneficial to have a <strong>variable step size $\alpha_n$</strong>. Such as step size should be chosen in a way that is which is mindful of the properties below.</p>
<p>To assure convergence with probability 1: \(\sum_{n=1}^\infty \alpha_n(a) = \infty \ \text{ and } \ \sum_{n=1}^\infty \alpha_n^2(a) < \infty\) Why is this? Intuitively, the first condition makes sure that the step size is large enough for the estimate to travel an infinite distance (hence overcoming an initial bias of arbitrary size). The second condition instead guarantees that the step size will eventually be small enough to guarantee convergence.</p>
<p>Note that for the <em>step-size $\frac{1}{n}$, both conditions are met</em>, but this is <strong>not the case for a constant step size</strong>. In fact, in the latter case the second condition is not met, i.e. the estimate never converges. This means that there will always be some bias in our estimate, but it also means that throughout time, our estimate will be able to “follow” our parameter through parameter space, without ever getting “tired” (converging).</p>
<p>Note that these last sentences also apply to the step size $\frac{1}{n}$, but as the step size would decrease over time, it’s convergence (though guaranteed in the limit) would become slower and slower as time progresses, making it less useful in real-world applications. In fact, if the true parameters move fast enough, an estimate updated with step size of $\frac{1}{n}$ would never be able to catch up, while for a constant step size that is not necessarily true.</p>
<p>The <strong>tradeoff</strong> here appears to be <strong>between speed of convergence and bias in the estimate</strong>.</p>
<hr />
<p><strong><em>Exercise 2.4</em></strong></p>
<blockquote>
<p>If the step-size parameters $\alpha_n$ are not constant, then the estimate $Q_n$ is a weighted average of previously received rewards with a weighting different from that given by $Q_{n+1} = Q_n + \alpha [R_n - Q_n]$. What is the weighting on each prior reward for the general case, analogous to $Q_{n+1} = Q_n + \alpha [R_n - Q_n]$, in terms of the sequence of step-size parameters?</p>
</blockquote>
\[\begin{equation}
\begin{split}
Q_{n + 1} & = Q_n + \alpha_n [R_n - Q_n] \\
& = (1 - \alpha_n)Q_n + \alpha_n R_n \\
& = (1 - \alpha_n)\Big((1 - \alpha_{n - 1})Q_{n-1} + \alpha_{n-1} R_{n-1}\Big) + \alpha_n R_n \\
& = (1 - \alpha_n)(1 - \alpha_{n - 1})Q_{n-1} + (1 - \alpha_n)\alpha_{n-1}R_{n-1} + \alpha_n R_n \\
& = Q_1 \prod_i^n(1-\alpha_i) + \sum_i^n \Bigg(\alpha_i R_i \prod_{j=i}^{n - 1} (1 - \alpha_j)\Bigg)
\end{split}
\end{equation}\]
<p>Therefore, we see that the expressions end up being pretty similar, where the one in section 2.5 is just a special case of this more general one.</p>
<hr />
<p><strong><em>Exercise 2.5 (programming)</em></strong></p>
<blockquote>
<p>Design and conduct an experiment to demonstrate the difficulties that sample-average methods have for non-stationary problems. Use a modified version of the 10-armed testbed in which all the $q_∗(a)$ start out equal and then take independent random walks (say by adding a normally distributed increment with mean zero and standard deviation $0.01$ to all the $q_∗(a)$ on each step). Prepare plots like Figure 2.2 for an action-value method using sample averages, incrementally computed, and another action-value method using a constant step-size parameter, $α = 0.1$. Use $ε = 0.1$ and longer runs, say of $10,000$ steps.</p>
</blockquote>
<p><strong><em>TODO</em></strong></p>
<h3 id="26-optimistic-initial-values">2.6 Optimistic Initial Values</h3>
<p>All the methods for estimating $Q(a)$ so far depend on $Q_1(a)$ in some amount. Note that $Q_1$ is the initial action-value estimate that we have before having observed any action whatsoever. Therefore, we can say that our estimators of $Q$ are <em>biased</em> because of this by their initial estimates.</p>
<p>For <strong>sample average methods, the bias disappears</strong> once all actions have been selected at least once. This is true as once we have taken one action we have $Q_2(a) = Q_1 + \frac{1}{n} [R_1 - Q_1] = Q_1 + [ R_1 - Q_1] = R_1 $, which is an unbiased estimate. From then onwards, the estimate will not depend on $Q_1$ anymore, and so the resulting estimator will be unbiased.</p>
<p>Instead, for methods with a <strong>constant $\alpha$, the bias is permanent</strong>, even though it decreases over time (this is true as we can see from section <a href="#2.5">2.5</a> that $Q_1$ does not cancel out in this case, even though it is given exponentially less weight in the final estimate as time progresses).</p>
<p>This kind of <strong>bias can actually turn out to be helpful</strong>. This is the main idea underlying the method of <strong><em>optimistic initial values</em></strong>: using the bias as a tool to encourage exploration and speed up convergence. Specifying the initial values $Q_1$ can be seen as introducing additional information (a prior) to the problem. In particular, consider setting all $Q_1$ values to $+5$ in our testbed case. This is very optimistic and the learner will definitely be “disappointed”. However, this as the effect of reducing the value of that action, making the agent more prone to try the other ones, still valued at $+5$. This way, we are encouraging a very active exploration at the start, without having to implement a $\epsilon$-greedy policy.</p>
<p>This <em>trick</em> is quite <strong>effective on stationary problems but not on non-stationary ones</strong>. This is because once the value estimates have started to converge to their true values, the agent will not explore anymore. The problem is that <strong>the drive for exploration is inherently temporary</strong>.</p>
<blockquote>
<p>“This criticism also applies to sample-average methods, in which the beginning of time is treated as a special event”? (unsure what point the book is trying to make here)</p>
</blockquote>
<hr />
<p><strong><em>Exercise 2.6: Mysterious Spikes</em></strong></p>
<blockquote>
<p>The results shown in Figure 2.3 should be quite reliable because they are averages over 2000 individual, randomly chosen 10-armed bandit tasks. Why, then, are there oscillations and spikes in the early part of the curve for the optimistic method? In other words, what might make this method perform particularly better or worse, on average, on particular early steps?</p>
</blockquote>
<p>At initial steps, most agents across all tests will do the same thing: they will go through all actions once and be “disappointed” in all the rewards they receive. Depending on the rewards they receive, they now have a pretty high chance (it seems plausible that it would be around 40%, as in the figure) of selecting the optimal action in their next move after trying out all actions once, as on average the optimal action should have yielded a higher reward.</p>
<p>The fact that a large proportion of agents selects the optimal action exactly at that timestep causes the spike in the graph. However, after having selected it, most agents will have been “disappointed” even more, and switch back to selecting other actions. At this point most agents are doing different things and there is not more large constructive interference as in this case at $t=10$.</p>
<blockquote>
<p>I’m not quite sure why optimistic takes so long to converge to the optimal action however – even though it takes less iterations than the realistic $\epsilon$-greedy approach. Might be something to look into more.</p>
</blockquote>
<p>Another thing to notice is that the graph reflects that the probability of choosing the optimal action prior to $t=10$ is around 10%, that is a random pick out of all actions with $Q_1 = 5$.</p>
<hr />
<p><strong><em>Exercise 2.7: Unbiased Constant-Step-Size Trick</em></strong></p>
<blockquote>
<p>In most of this chapter we have used sample averages to estimate action values because sample averages do not produce the initial bias that constant step sizes do. However, sample averages are not a completely satisfactory solution because they may perform poorly on nonstationary problems. Is it possible to avoid the bias of constant step sizes while retaining their advantages on nonstationary problems? One way is to use a step size of $\beta_t = \frac{\alpha}{\bar{o}_t}$, where $α > 0$ is a conventional constant step size, and $\bar{o}_t$ is a trace of one that starts at $0$:</p>
\[\bar{o}_{t+1} = \bar{o}_t + \alpha (1 - \bar{o}_t) \text{ for } t \geq 1 \text{ with } \bar{o}_1 = \alpha\]
<p>Carry out an analysis to show that $\beta_t$ is an exponential recency-weighted average without initial bias.</p>
</blockquote>
<p><strong><em>TODO</em></strong></p>
<h3 id="27-upper-confidence-bound-action-selection">2.7 Upper-Confidence-Bound Action Selection</h3>
<p>Something that can be improved with $\epsilon$-greedy methods, is that if the $\epsilon$ chance of exploring is selected, the action taken is <em>at random</em>. We might want to explore according to a prior belief on how much benefit we expect to gain: if we already are very sure that an action is suboptimal, it might not be worth exploring that anymore, and prefer other ones where the certainty is less. In other words, we might want to <strong>select among the non-greedy actions according for their potential for actually being optimal</strong> (taking into account both how close their estimates are to the optimal action value and their uncertainty).</p>
<p>One way to do this is to select <em>all</em> actions as such (we don’t have $\epsilon$-greedy actions anymore):</p>
\[A_t = arg\max\limits_a \Bigg[Q_t(a) + c \sqrt{\frac{\ln t}{N_t(a)}}\Bigg]\]
<p>Note: when $N_t(a)=0$, $a$ is considered to be the maximizing action.</p>
<p>Why this expression? We see that we are selecting according to a proxy value that is a sum of the actual belief of the $Q$ value and a rescaled (by $c$) proxy for the uncertainty of the estimate. In fact, we see that every time the action is explored, $N_t(a)$ will increase, decreasing this proxy for uncertainty. Similarly, at every timestep, $\ln t$ will increase (even though at a slower – logarithmic – rate): this serves the purpose to increase the “uncertainty” of actions even though they might not be selected. In this case it’s not really about uncertainty, but more about making sure that every action is selected every once in a while, even though we are pretty confident of its sub-optimality.</p>
<p>This approach is called <strong>upper confidence bound (UCB)</strong>. The <em>upper bound</em> aspect of it comes from the fact that the quantity argmaxed over can be considered <em>an approximate upper bound</em> for the actual value of each action $a$.</p>
<p>This all seems very reasonable and more theoretically grounded than most of the previous methods (even though the statistics is still very rough and <em>approximate</em>). However, unfortunately, even though UCB performs well, it is very hard to extend to general RL cases. In particular, it <strong>does not support nonstationary problems, or large state spaces</strong> (where the states cannot be stored in tabular form).</p>
<blockquote>
<p>Why can’t we apply the nonstationary update rules for $Q_t$ <a href="#2.5">2.5</a> to UCB and be able to track nonstationary problems effectively?</p>
</blockquote>
<p>The main reason behind this is that even though we would be updating the $Q$ values in the “right way” – capable of dealing with nonstationary problems – we would not be selecting the actions in a very legitimate way. This arises from the fact that <strong><em>our proxy for uncertainty assumes stationarity</em></strong>. In fact, there is no clear way to change the expression for action selection (presented in the equation above) in order to be good at dealing with nonstationary problems.</p>
<hr />
<p><strong><em>Exercise 2.8:</em></strong></p>
<blockquote>
<p>UCB Spikes In Figure 2.4 the UCB algorithm shows a distinct spike in performance on the 11th step. Why is this? Note that for your answer to be fully satisfactory it must explain both why the reward increases on the 11th step and why it decreases on the subsequent steps. Hint: if $c = 1$, then the spike is less prominent.</p>
</blockquote>
<p>This is related to Exercise 2.6. In this case this phenomenon arises from the way we define the argmax to behave when $N_t(a)=0$: as mentioned above, when $N_t(a)=0$, $a$ is considered to be the maximizing action.</p>
<p>This means that at each of the first 10 moves (as there are 10 actions in our testbed), we will pick at random among the actions that have not been chosen yet, as they will have $N_t(a)= 0$ and so will be considered “optimal”.</p>
<p>On the 11th step, all the agents will pick the action among the 10 that in this first round of attempts yielded the highest reward: for all actions, the value of $c \sqrt{\frac{\ln t}{N_t(a)}}$ will be the same, so the only discriminatory factor will be the value of $Q_t$. And we know that this only depends on the first round of exploration.</p>
<p>Therefore the 11th step will be an example of “constructive interference” (as in Exercise 2.6) where all agents across games are in sync with selecting a “pretty good” action (not necessarily optimal, but most likely not too bad). This is what causes the spike. After step 11, the agents across games will not be “in sync” anymore, and the average reward will go down, to then slowly increase as more certainty about the system is gathered.</p>
<p>The spike therefore is mainly due to this “sync” across different games, that is guaranteed by the structure of the UCB action selection.</p>
<h3 id="28-gradient-bandit-algorithms">2.8 Gradient Bandit Algorithms</h3>
<p>Up until now, we have concerned ourselves with estimating action values (<em>$Q$ values</em>) and using those estimates to select actions. This is not the only approach possible: in fact one can also try to learn <em>a numerical <strong>preference</strong> $H_t(a)$ for each action</em>. The value of this preference value <strong>is unrelated to reward</strong>: only the relative preference of one action over another is important, as <strong>the probability of selecting each action is determined by a performing <em>soft-max</em> on the preference values</strong>.</p>
\[P(A_t = a) = \frac{e^{H_t(a)}}{\sum_{b=1}^k e^{H_t(b)}} = \pi_t(a)\]
<p>where $\pi_t(a)$ denotes the <em>probability of taking action a at time t</em>. This notation is very common.</p>
<p>There is a natural learning algorithm in this setting based on the idea of stochastic gradient descent. This is based on updating the preference values as such, after taking action $A_t$ and obtaining reward $R_t$:</p>
\[H_{t+1} (A_t) = H_{t+1} (A_t) + \alpha (R_t - \bar{R}_t)(1 - \pi_t(A_t))\]
\[H_{t+1} (a) = H_t(a) - \alpha (R_t - \bar{R}_t) \pi_t(a) \ \text{ for all } \ a \neq A_t\]
<p>Note that $(R_t - \bar{R}_t)$ will be positive if the reward obtained at time $t$ is greater than the average reward obtained.</p>
<p>This means that after taking action $A_t$, we will update the preference $H_{t+1}$ for $A_t$:</p>
<ul>
<li>Increasing it if the reward obtained $R_t$ is greater than average</li>
<li>Decreasing it if the reward obtained $R_t$ is smaller than average</li>
</ul>
<p>The amount the preference for $A_t$ is updated depends on $\pi_t(A_t)$: in particular, if $A_t$ has a high chance of being selected (high $\pi_t(A_t)$), then we will have a small update, and viceversa.</p>
<p>The opposite happens for all the other actions $a \neq A_t$. Their preference is updated too, in the opposite direction as that for $A_t$, and the update size will be proportional to their probability of being selected (the higher the chance, the bigger the update).</p>
<p>Note that the amount of increase/decrease is proportional to the step-size $\alpha$ that basically measures how aggressively we update our preferences.</p>
<p>There are quite a lot of moving parts, but they can all be derived by trying to port the concept of <strong>stochastic gradient ascent</strong> to this context. In particular, it turns out the the above update scheme is equivalent to stochastic gradient ascent with batch size 1. (See book for details).</p>
<hr />
<p><strong><em>Exercise 2.9:</em></strong></p>
<blockquote>
<p>Show that in the case of two actions, the soft-max distribution is the same as that given by the logistic, or sigmoid, function often used in statistics and artificial neural networks.</p>
</blockquote>
<!-- In the case of just two actions, $a_1$ and $a_2$, we have:
$$P(A_t = a_1) = \frac{e^{H_t(a_1)}}{e^{H_t(a_1)} + e^{H_t(a_2)}} = \frac{1}{1 + e^{H_t(a_2)}} = \sigma\big(H_t(a_2)\big)$$
where $\sigma$ be the sigmoid function.
Similarly, we have that $P(A_t = a_2) = \sigma\big(H_t(a_1)\big)$.
Therefore, soft-max is the same as sigmoid when $k=2$, but with arguments switched: the soft-max probability that is assigned to $a_1$ is the same as the sigmoid of $a_2$ and viceversa.
> (?? pretty sure this is wrong) -->
<p><strong><em>TODO</em></strong></p>
<h3 id="29-associative-search-contextual-bandits">2.9 Associative Search (Contextual Bandits)</h3>
<p>For now we have only considered nonassociative tasks, that is, tasks in which there is no need to associate different actions with different situations. However, in the <strong>general RL task there is more than one situation, and the goal is to learn a policy, i.e. a <em>mapping</em> from states to optimal actions given the state</strong>.</p>
<p>Suppose that instead of having one bandit for the entire game, at each turn you were given a bandit selected among a pool of bandits, and also a clue as to which bandit it might be. In this case, the optimal action depends on the state (the bandit you are actually confronting). Now you can learn a policy that associates an optimal action with each state you could potentially be in.</p>
<p><strong>This is an example of <em>associative search</em> task, called as such as it involves both trial-and-error learning to <em>search</em> and <em>association</em> of these action with the situations in which they are best.</strong> This task is now called <em>contextual bandits</em> in the literature.</p>
<p>In this setting, we still don’t have actions affecting the <em>next situation</em> as well as the reward. This will add another layer of complexity and lead us to full reinforcement learning problems.</p>
<hr />
<p><strong><em>Exercise 2.10</em></strong></p>
<blockquote>
<p>Suppose you face a 2-armed bandit task whose true action values change randomly from time step to time step. Specifically, suppose that, for any time step, the true values of actions 1 and 2 are respectively 0.1 and 0.2 with probability 0.5 (case A), and 0.9 and 0.8 with probability 0.5 (case B). If you are not able to tell which case you face at any step, what is the best expectation of success you can achieve and how should you behave to achieve it? Now suppose that on each step you are told whether you are facing case A or case B (although you still don’t know the true action values). This is an associative search task. What is the best expectation of success you can achieve in this task, and how should you behave to achieve it?</p>
</blockquote>
<p>If we are not told which case we face at any step, the best one can do is the (weighted) average the Q values associated with each action across cases, and always pick the action that leads to the highest (weighted) average reward across all cases.</p>
<p>In this case we are given the weights (0.5 for case A and 0.5 for case B), to the weighted average just corresponds to the average. Given that the average reward for action 1 and action 2 are both $1/2$, in this case the choice of action doesn’t matter, as we expect – on average – to receive the same reward in the long run.</p>
<p>To be able to achieve this optimal behavior, we could just use one of the standard stationary approaches, such as UCB with incremental $Q$ value update. (I think?)</p>
<p>If instead we were told what case we were facing, then we would want to run two separate nonstationary updates: instead of keeping one set of $Q$ value estimates, we have one for case A and one for case B, and update them independently. If implemented correctly this would lead us to select action 2 in case A, and action 1 in case B.</p>
<h3 id="210-summary">2.10 Summary</h3>
<p>This chapter presented several ways to balance exploration and exploitation:</p>
<ul>
<li>$\epsilon$-greedy methods choose randomly among actions a small fraction of the time to encourage exploration</li>
<li>UCB methods choose <em>deterministically</em> but favouring actions with uncertain estimates</li>
<li>Gradient methods don’t estimate action values, but preferences, and choose actions probabilistically according to the preferences</li>
<li>Optimistic initialization sets optimistic initial $Q$ values in order to encourage a very active exploration in the initial phase, leading to a fast convergence with no bias</li>
</ul>
<p>There is no method among the above that <em>is best</em>. Common ways to assess performance of these kind of algorithms are through graphs:</p>
<ul>
<li><em>learning curve</em>: shows the performance of an algorithm vs iterations for a certain parameter setting</li>
<li><em>parameter studies</em>: summarize each algorithm by the average reward over the first 1000 steps, and show such a value <em>for various parameter settings and algorithms</em></li>
</ul>
<p>One should also consider the sensitivity to parameter settings, that is a indication of <strong>robustness</strong>.</p>
<p><strong>Despite their simplicity, these methods can be considered state of the art, as more sophisticated methods usually impose additional complexity and assumptions</strong>.</p>
<blockquote>
<p>Stuff to potentially explore more: <em>Gittins indices</em></p>
</blockquote>
<p>Something to keep in mind is that in a Bayesian setting it would be possible to even compute the <em>optimal</em> balance between exploration and exploitation. However, this is often computationally infeasible. See book for more details. The main idea is that one could imagine expanding all possible outcomes for the next $x$ actions, and computing which tradeoff would lead, in expectation, to the best outcome.</p>
<hr />
<p><strong><em>Exercise 2.11 (programming)</em></strong></p>
<blockquote>
<p>Make a figure analogous to Figure 2.6 for the nonstationary case outlined in Exercise 2.5. Include the constant-step-size $ε$-greedy algorithm with $α=0.1$. Use runs of 200,000 steps and, as a performance measure for each algorithm and parameter setting, use the average reward over the last 100,000 steps.</p>
</blockquote>
<p><strong><em>TODO</em></strong></p>
<h2 id="13-policy-gradient-methods">13. Policy Gradient Methods</h2>
<p>In opposition to <em>action-value methods</em>, that associate a <strong>value to each action given the state</strong>, and pick actions according to these values, there are also <em>policy-gradient methods</em>. These methods <strong>learn a <em>parametrized policy</em> that can select actions without consulting a value function</strong>. A <strong>value function</strong> might still be <strong>used to learn the policy parameter</strong>, but is not required for action selection.</p>
<blockquote>
<p>What does this mean? As far as I can tell, the major difference is that now the choice of action (the policy) is made <strong>distinct</strong> from the values of the state. An implicit assumption in action-value methods is that we always pick the actions that maximize our value, or at least we do so most of the time.</p>
</blockquote>
<p>In this context, we let $\theta \in \mathbb{R}^{d’}$ be the policy parameter vector: $\pi(a \mid s, \theta) = P(A_t = a \mid S_t = s, \theta_t = \theta)$. Our policy also depends on the value of $\theta$.</p>
<p>If the method uses a <em>learned value function as well, we can denote it as</em> $\hat{v}(s, w)$ where $w \in \mathbb{R}^d$ is the weight vector.</p>
<p>We can now consider methods for <strong>learning the policy parameter $\theta$ based on some performance measure $J(\theta)$</strong> by performing <strong>gradient ascent</strong> with respect of $J$ with respect to $\theta$:</p>
\[\theta_{t+1} = \theta_t + \alpha \widehat{\nabla J(\theta_t)}\]
<p>where $\widehat{\nabla J(\theta_t)}$ is a <strong><em>stochastic</em></strong> estimated gradient of the performance measure $J$ with respect to the parameter $\theta$.</p>
<p>All methods that follow this schema are called <strong>policy gradient methods</strong>. Note that in this case, by performing gradient ascent with respect to $\theta$, if our parametrized policy is sufficiently expressive, we are guaranteed (I believe) to converge to the <strong>optimal policy</strong>, given that our performance measure is correct and probably some other assumptions. In general an important takeaway is this: by picking a value for $\theta$ we are implicitly <strong>approximating the optimal policy function</strong>.</p>
<blockquote>
<p>Can’t action value methods be considered as a parametrized policy case also? Where the $\epsilon$ (for $\epsilon$-greedy methods) could be considered part of the $\theta$? What is the main difference? I think that the answer to this is yes – action-value methods are a subset of methods with a parametrized policy. However, the difference is that in that case $\theta$ (i.e. $\epsilon$) is not updated over time in order to optimize performance, so it is not to be considered a <em>gradient method</em>.</p>
</blockquote>
<p>Methods that <strong>learn approximations to both the policy and value functions</strong> are called <strong><em>actor-critic methods</em></strong>, where <em>actor</em> refers to the learned policy, and <em>critic</em> refers to the learned value function, usually a <em>state-value function</em>.</p>
<blockquote>
<p>What other function could we have that is not <em>state-value</em>? Probably skipped too much for now to have a good example.</p>
</blockquote>
<h3 id="131-policy-approximation-and-its-advantages">13.1 Policy Approximation and its Advantages</h3>
<p>In the setting of policy-based methods, the policy can be parametrized in <em>any way, as long as $\pi(a \mid s, \theta)$ is differentiable with respect to $\theta$</em>.</p>
<p>In practice, <strong>to ensure exploration, we require the policy to never become deterministic</strong> (i.e. $\pi(a \mid s, \theta) \in (0,1) \ \ \forall s, \theta$).</p>
<blockquote>
<p>I think that the concept of not exploring is related to that of local minima in optimization. If our policy is deterministic, we don’t explore, and so the agent has a hard time improving. However, in this setting it’s a bit more complicated: even if the policy was deterministic, the $\theta$ value would still be updated, and thus it’s policy would still change over time to maximize performance. However, given that this relies on the environment being stochastic, the whole matter becomes more complicated. In fact, if the environment is deterministic, then a deterministic policy would lead us to not be able to improve at all? Or would it? I’m kind of confused as to how one would go about actually calculating $J$ in a real setting, or what $J$ would be. (I’m imagining something like the long-run average reward gained by playing the game with policy $\pi$)</p>
</blockquote>
<p>Policy-based methods can also work well with continuous action spaces, as explored in 13.7, but here we only consider reasonably small discrete action spaces. In this case, the natural way to parametrize the policy function is with a <strong>vector of numerical preferences</strong> $h(s, a, \theta) \in \mathbb{R}$ for each state-action pair. Then one can recover the actual policy approximation with a <strong>soft-max</strong>, that converts these preferences to probabilities. This kind of parameterization is called <em>soft-max in action preferences</em>. The preferences could be parameterized arbitrarily, for example with a neural net or just with a linear combination of features.</p>
<p>One advantage of using the soft-max is that <strong>the approximate policy can approach a deterministic policy</strong>, while this is not possible with $\epsilon$-greedy approaches, that always maintain a certain amount of stochasticity. <em>One could select according to a soft-max distribution based on action values</em>, but this alone would <em>not allow the policy to approach a deterministic policy</em>.</p>
<blockquote>
<p>Let’s think about why for a minute. If we were to select actions according to soft-max action values, then we would always preserve stochasticity in our action choices. This is because eventually, if we are estimating our action-values correctly, they will converge to their true values, and so their soft-max version will inevitably preserve some non-negligible probability of selecting all of them. The main difference from using the preference values $h$ is that they could grow arbitrarily large or small such that the soft-max probabilities actually approach a deterministic policy (one action has probability arbitrarily close to one and the others arbitrarily close to zero). One could still include a temperature parameter for the soft-max, but then the question arises as to how to vary it over time.</p>
</blockquote>
<p><strong>Action preferences are different because they are driven by the policy gradient (that drives the updates of $w$) to produce the optimal stochastic policy</strong>. If the optimal stochastic policy is deterministic, then it will produce a policy that is arbitrarily close to that deterministic one.</p>
<p>Another advantage of <strong>parametrizing policies according to the soft-max in action preferences is that it enables to converge to policies with particular probabilities of selecting each action</strong>. There are situations in which, for a given state, there does not exist one optimal action, but the optimal thing to do is to return a random action with a certain distribution. This occurs for example in poker, with bluffs. One has to switch up one’s game and can’t play too predictively. There is no optimal deterministic strategy. This is different than with action-based methods, that in their vanilla version determine an “optimal action” and then return it $1-\epsilon$ proportion of the time. Even in the case in which there are multiple “optimal actions” (actions with the same action-value), one usually picks among them uniformly at random.</p>
<p>This is to say that <strong>action-value methods have no natural way of finding stochastic policies, whereas policy approximating methods can</strong>.</p>
<p>The simplest advantage that policy parameterization might have over action-value parameterizations is that the policy may be a simpler function to approximate. For some problems, the action-value function is simpler and thus is easier to approximate, and for others the policy function is.</p>
<p>The <strong>choice of policy parameterization</strong> can also be <strong>a good way of injecting prior knowledge into the problem</strong>. This is often <em>the most important reason</em> in using a policy-based learning method.</p>
<p><strong><em>Exercise 13.1</em></strong></p>
<blockquote>
<p>Use your knowledge of the gridworld and its dynamics to determine an exact symbolic expression for the optimal probability of selecting the right action in Example 13.1.</p>
</blockquote>
<p>Note that in this example, $J(\theta) = v_{\pi_\theta}(S)$. One can derive a closed form symbolic expression for $v_{\pi_\theta}(S)$ and then differentiate with respect to $\theta$ to find the stationary point for the gradient ascent. This would parametrize the optimal policy, so one can derive the optimal action probabilities from the soft-max optimal preference values (that in turn are determined by the optimal $\theta$). <strong><em>TODO</em></strong></p>
<p><em>Still in progress</em></p>