ML Module-5 Updated
ML Module-5 Updated
Module V
The input for cluster analysis is examples or samples. These are known as objects,
data points or data instances. All these terms are same and used interchangeably in this
chapter. All the samples or objects with no labels associated with them are called unlabelled.
The output is the set of clusters (or groups) of similar data if it exists in the input. For
example, the following Figure 13.1(a) shows data points or samples with two features shown
in different shaded samples and Figure 13.1(b) shows the manually drawn ellipse to indicate
the clusters formed.
Visual identification of clusters in this case is easy as the examples have only two features.
But, when examples have more features, say 100, then clustering cannot be done manually
and automatic clustering algorithms are required. Also, automating the clustering process is
desirable as these tasks are considered difficult by humans and almost impossible. All
clusters are represented by centroids.
Example: For example, if the input examples or data is (3, 3), (2, 6) and (7, 9), then the
centroid is given as.
The clusters should not overlap, and every cluster should represent only one class. Therefore,
clustering algorithms use trial and error method to form clusters that can be converted to
labels.
Applications of Clustering
Scalability Issue: Some algorithms perform well for small datasets but fail for large-scale
data.
Unit Inconsistency: Different measurement units (e.g., kg vs. pounds) can create problems.
Proximity Measure Design: Choosing an appropriate distance metric is crucial for accurate
clustering.
Questions:
1. What is clustering?
2. What are the applications of clustering?
3. List the advantages and disadvantages of clustering?
4. What are the challenges in clustering?
5. List the differences between clustering and classification.
The distance between two objects, say i and j , is denoted by the symbol Dij.
If all of these conditions are satisfied, then the distance measure is called a metric. Based on
the data types of attributes of an objects, the distance measures vary.
Proximity measures:
1. Euclidean Distance: It is one of the most important and common distance measures.
It is also called as L2 norm. It can be defined as the square root of the squared
differences between the coordinates of a pair of objects.
The Euclidean distance between objects x1 and x2 with k features is given as follows:
The advantage of Euclidean Distance is that distance does not change with the
addition of new objects. But the disadvantage is that if the units change, the resulting
Euclidean or squared Euclidean changes drastically. Another disadvantage is that as
the Euclidean Distance involves a square root and a square, the computational
complexity is high for implementing the distance for millions or billions of operations
involved.
2. City block Distance: City block distance is known as Manhattan distance. This is
also known as boxcar, absolute value distance , Taxicab or L1 norm. The formula for
finding the distance is as follows:
4. Minkowski Distance: In general, all the above distance measures can be generalized
as
Binary attributes: Binary attributes have only two values. Distance measures discussed
above cannot be applied to find the distance between objects that have binary attributes. For
finding the distance among objects with binary objects, the contingency table shown below
can be used.
Let x,y be the objects consisting of N-binary objects. Then, the contingency table can be
constructed by counting the number of matching transitions, 0-0, 0-1, 1-0 and 1-1. In other
ords, ‘a’ is t e n mber o attrib tes ere x attrib te is 0 and y attribute is 0. b is the
number of attributes where 𝑥 attribute is 0 and 𝑦 attribute is 1 and so on.
2. Jaccard Coefficient: Jaccard Coefficient is another useful measure for and is given
as follows:
3. Hamming Distance: It is another useful measure that can be used for knowing the
sequence of characters or binary values. It indicates the number of positions at which
characters or binary bits are different.
For example, the hamming distance between x=( 1 0 1) and y=(1 1 0) is 2 as x and y
differ in two positions.
Categorical variables: In many cases, categorical values are used. It is just a code or symbol
to represent the values. For example, for the attribute Gender, a code 1 can be given to female
and 0 can be given to male.
To calculate the distance between two objects represented by two variables, we need to find
only whether they are equal or not. This is given as,
Ordinal variables: Ordinal variables are categorical values but with an inherent order. For
example, designation is an ordinal variable. If job designation is 1 or 2 or 3, it means 1 is
higher than 2, code 2 is higher than 3. It is ranked as 1>2>3.
Let us assume the designations of office employees are clerk, supervisor, manager and
general manager. These can be designated as clerk=1, supervisor=2, manager=3 and general
manager=4. Then , the distance between employee X who is clerk and Y who is a manager is
obtained as,
Vector type Distance measures: For text classifications, vectors are normally used. Cosine
similarity is a metric used to measure how the similar documents are irrespective of their size.
Cosine similarity measures the cosine of the angle between the two vectors projected in a
multi-dimensional space. The similarity function for vector objects can be defined as:
Questions:
1. Which are the distance metrics that can be used for numerical features?
2. List the distance metric to be used for binary attributes?
3. How do you find the difference between two categorical variables?
4. How do you find the difference between two vector type features?
All the clusters that are produced by the hierarchical algorithms have equal diameters. The
main disadvantage of this approach is once the cluster if formed, it is an irreversible decision.
used for merging the clusters. This corresponds to finding of minimum spanning tree (MST)
of a graph.
The distance measures between individual samples or data points is already demonstrated in
the previous section.
Complete Linkage or MAX or Clique: In complete linkage algorithm, the distance (x,y ), (
where x is from one cluster and y is from another cluster), is the largest distance between all
possible pairs of the two groups or clusters ( or simply the largest distance of two points
where points are in different clusters) as given below. It is used for merging the clusters.
Average Linkage: In case of an average linkage algorithm, the average distance of all pairs
of points across the clusters is used to form clusters. The average value computed between
clusters ci and cj is given as follows:
The algorithm uses a window, which is basically a weighting function. Gaussian window is a
good example of a window. The radius of the kernel is called bandwidth. The entire window
is called a kernel. The window is based on the concept of kernel density function and its aim
is to find the underlying data distribution. The method of calculation is dependent on the
choice of windows. If a Gaussian window is chosen, then every point is assigned a weight
that decreases as the distance from the kernel center increases. The algorithm is given below.
Advantages:
No model assumptions
Suitable for all non-convex shapes
Only one parameter of the window, that is bandwidth is required.
Robust to noise
No issues of local minima or premature termination
Disadvantages:
Selecting the bandwidth is the challenging task. If it is larger, then many clusters are
missed. If it is small, then many points are missed, and convergence occurs is the
problem.
The number of clusters cannot be specified and user has no control over the
parameter.
K-means is the straight forward iterative partitional algorithm. Here k stands for the user
specified number of clusters as users are not aware of the number of clusters present in the
dataset. The k-means algorithm assumes that the clusters do not overlap. Therefore, a sample
of data point can belong to only one cluster in the end. Also this algorithm can detect clusters
of shapes like spherical or circular.
Initially, the algorithm needs to be initialized. The algorithm can select k data points
randomly or use the prior knowledge of the data. In most cases, in k-means algorithm setup,
prior knowledge is absent. The composition of the cluster is based on the initial condition;
therefore, initialization is an important task. The sample or data points need to be normalized
for better performance.
The core process of the k-mean algorithm is assigning a sample to a cluster, that is, assigning
each sample or data point to the k cluster centres based on its distance and the centroid of the
clusters. This distance should be minimum. As a new sample is added, new computation of
mean vectors of the points for that cluster to which sample is assigned is required. Therefore,
this iterative process is continued until no change of instances to clusters is noticed. This
algorithm then terminates, and the termination is guaranteed.
Here, cₙ is the centroid of the ith cluster, x is the sample or data point and dist is the Euclidean
distance. The aim of the k-means algorithm is to minimize SSE.
Advantages:
1. Simple
2. Easy to implement
Disadvantages:
k is a user-defined number of clusters. There are no strict rules for selecting it. Typically, k-
means is run with various k values, and the within-group variance is plotted as an Elbow
curve. The optimal k is found where the curve flattens.
Questions
Density-based spatial clustering of applications with noise (DBSCAN) is one of the density-
based algorithms. Density of a region represents the region where many points above the
specified threshold are present. In a density-based approach, the clusters are regarded as
dense regions of objects that are separated by regions of low density such as noise. This is
same as a man’s int itive ay o observing cl sters.
The concept of density and connectivity is based on the local distance of neighbours. The
nctioning o t is algorit m is based on t o parameters, t e size o t e neig bo r ood (ε)
and the minimum number of points (m).
1. Core point – A point is called a core point if it has more than specified number of
points (m) it in ε-neighbourhood.
2. Border point – A point is called a border point if it has fewer than 'm' points but is a
neighbour of a core point.
3. Noise point – A point that is neither a core point nor border point.
The main idea is that every data point or sample should have at least a minimum number of
neig bo rs in a neig bo r ood. T e neig bo r ood o radi s ε s o ld ave at least m points.
The notion of density connectedness determines the quality of the algorithm.
o (a) X is t e ε-neighbourhood of Y
2. Densely reachable – The point X is densely reachable from Y, if there is a set of core
points that leads from Y to X.
3. Density connected – X and Y are densely connected if Z is a core point and thus
points X and Y are densely reachable from Z.
Advantages:
Questions
Grid based approach is a space based approach. It partitions space into cells, the given data is
fitted on the cells for cluster information. There are three important concepts that need to be
mastered for understanding the grid based schemes. They are
1. Subspace clustering
2. Concept of dense cells
3. Monotonicity property
Subspace clustering
Grid-based algorithms help cluster high-dimensional data with many attributes. Often, only a
subset of attributes is needed for clustering, depending on the application (e.g., age may
matter for disease profiling, but not address).
Finding subspaces is challenging as N dimensions can ave ⁿ⁻¹ s bspaces. To andle t is,
CLIQUE (Clustering in Quest) is used. It is a grid-based method using a multiresolution grid
to find clusters in subspaces.
CLIQUE partitions each dimension into several overlapping intervals and intervals it into
cells. Then, the algorithm determines whether the cell is dense or sparse. The cell is
considered dense i it exceeds a t res old val e, say τ. Density is de ined as t e ratio o
number of points and volume of the region. In one pass, the algorithm finds the number of
cells, number of points, etc. and then combines the dense cells. For that, the algorithm uses
the contiguous intervals and a set of dense cells.
CLIQUE uses anti-monotonicity property or apriori property of the famous apriori algorithm.
It means that all the subsets of a frequent item should be frequent. Similarly, if the subset is
infrequent, then all its supersets are infrequent as well. Based on the apriori property, one can
conclude that a k-dimensional cell has r points if and only if every (k - 1) dimensional
projections of this cell have at least r points. So like association rule mining that uses apriori
rule, the candidate dense cells are generated for higher dimensions. The algorithm works in
two stages as shown below.
Advantages of CLIQUE:
Disadvantages:
The disadvantage of CLIQUE is that tuning of grid parameters, such as grid size, and finding
optimal threshold for finding whether the cell is dense or not is a challenge.
Questions:
Reinforcement learning (RL) mimics human beings and is a branch of machine learning.
Humans observe the environment through senses such as eye and ear. The inputs are
processed by brain. The brain then suggests the actions and acts voluntarily or involuntarily.
In humans, learning happens in a real world called environment. Take a kid for an example.
How does the kid learn? The kid interacts with the environment and gains valuable
experience. Experience leads to learning. For example, when a kid accidently burns his hand
in fire, he learns to stay away from it in the future and be careful. Similarly, the kid learns to
do good things repeatedly by the encouragement or gifts given by parents or teachers.
Awards can be positive or negative. Awarding a kid is a positive reinforcement and scolding
is a negative reward or deterrent as shown in Figure below. Thus, kids gain experience and
learn many things through rewards and punishments (negative rewards).
In computers, these scenarios can be simulated in many ways. As a kid executes many
actions and receives a mix of positive and negative rewards that lead to gaining experience
and learning, a computer program or robot can learn through experiences of simulated
scenarios. Thus, reinforcement learning is a mathematical framework for learning. There are
two types of reinforcement learning – positive and negative. Positive reinforcement learning
is a recurrence of behaviour due to positive rewards. Rewards increase strength and the
frequency of a specific behaviour. This encourages to execute similar actions that yield
maximum reward. Similarly, in negative reinforcement learning, negative rewards are used as
a deterrent to weaken the behaviour and to avoid it. In a maze game, there may a danger spot
that may lead to loss. Negative rewards can be designed for such spots so that the agent does
not visit that spot. Positive and negative rewards are simulated in reinforcement learning, say
+10 for positive reward and -10 for some danger or negative reward.
What are the situations where reinforcement learning can be used? Consider the following
grid game shown in Figure 14.2, where a robot can move. Assume the starting node is E and
goal node is G, the game is about finding the shortest path from starting to goal state.
Consider another grid game as shown in Figure 14.3. In this grid game, the grey tile indicates
the danger, black is a block and the tile with diagonal lines is the goal. The aim is to start, say
from bottom-left grid, using the actions left, right, top and bottom to reach the goal state.
Reinforcement learning is highly suitable for solving problems like this, especially ones with
uncertainty. Reinforcement learning is not suitable for environments where complete
information is available. For example, problems like object detection can be better solved by
classifiers than reinforcement.
1. Sequential decision making – Consider the Figure 14.3. It can be seen the path from
start to goal is not done in one step. It is a sequence of decisions that leads to the goal.
One wrong move may result in a failure. This is the main characteristic of
reinforcement learning.
2. Delayed feedback – Often, rewards are not immediate. One must spend many moves
to get final success or failure. Feedback in terms of reward is often delayed.
3. The agent actions are interdependent as any action affects the subsequent actions. For
example, one wrong move of an agent may lead to failure.
4. Time related – All actions are associated with time stamps inherently as all actions
are ordered as per the timeline inherently.
1. Reward design is a big challenge as in many games, as determining the rewards and
its value is a challenge.
2. Absence of a model is a challenge – Games like chess have fixed board and rules.
But, many games do not have any fixed environment or rules. There is no underlying
model as well. So, simulation must be done to gather experience.
3. Partial observability of states – Many states are fully observable. Imagine a scenario
in a weather forecasting where the uncertainty or partial observability exists as
complete information about the state is simply not available.
4. Time consuming operations – More state spaces and possible actions may
complicate the scenarios, resulting in more time consumption.
5. Complexity – Many games like GO are complicated with much larger board
configuration and many possibilities of actions. So, labelled data is simply not
available. This adds more complexity to the design of reinforcement algorithms.
There are many applications of RL. Some of the application domains where reinforcement
learning is used are listed below:
Industrial automation
Customized applications
Driverless cars
Questions:
1. What is Reinforcement Learning, and how does it differ from other types of machine
learning?
2. Describe the roles of the agent, environment, actions, and rewards in the context of
Reinforcement Learning.
3. How does the learning process in Reinforcement Learning mimic the way humans
learn from their environment?
4. What are positive and negative reinforcement in the context of RL? Can you provide
examples of each?
5. Explain the significance of sequential decision-making in Reinforcement Learning.
Why is it a crucial characteristic?
Thus, one can observe the difficulties faced by reinforcement learning when compared with
other machine learning algorithms. In supervised classifiers, the choice is one-shot, say
cancer or not cancer. But in reinforcement learning, it is a sequential decision making, say
finding the path from start to goal state. One must wait for more moves to see success or
failure. The reason behind success and failure is found by observing the patterns over a long
time and these decisions have longer consequences. For example, in a game, one wrong move
may result in failure.
The difference between reinforcement learning and unsupervised learning are as follows:
Questions:
The components of reinforcement learning are shown in figure below. These are
environment, agent , actions and rewards.
Reinforcement Problems
There are two types of problems in reinforcement learning – Learning and Planning.
In learning problems, the environment is unknown and the agent learns by trial and error. The
agent interacts with the environment to improve policy.
Planning is another problem where the environment is known and the agent computes with
the model and improves policy.
For example, in a car system, the maps, game rules and obstructions in the road are described
in the environment.
An agent is an autonomous body that looks at the environment and takes an action. It can be
any human or another computer program such as a robot or chatbot.
Consider the following graph in Figure 14.5 generated by two actions, up and down, by an
agent.
In Figure 14.5, the states are A, B, C, D, E, F, G, H and I. These states can be cities of a
shortest path problem or any other state encountered by a robot navigation. Let A be the
starting state and G t e goal or target state to be reac ed. T e symbol ‘S’ is sed to denote t e
general state and ‘s’ t e speci ic state. ‘sₜ’ is sed to denote a state at time ‘t’.
1. GOAL node – This is also known as terminal node and absorbing state. It is the goal
and the agent aims to get as it is the highest discounted cumulative node.
Actions are transitions between states, say the path between A and B is caused by UP action.
Then, the agent collects the rewards. In the game shown in Figure 14.5, the actions are UP
and DOWN only. Symbol A is used to denote the general actions – UP and DOWN. The
symbol ‘a’ is sed to speci y t e speci ic action and ‘aₜ’ is sed to denote t e action at time
‘t’.
‘ pisodes’ is t e n mber o steps necessary to reac t e goal state from start state. For
example, in Figure 14.5, the aim is to find optimal path between A and G. There are two
types of episodes.
One is called episodic and another is called continuous. In episodic tasks, there is a well-
defined starting state and end state. End state is called as terminal state or goal state. An
episode is one which consists of states, actions, and rewards. For example, all the paths from
A to G, such as
are all examples of episodes. An episode is one that starts from a starting state and reaches
the goal state. So, a successful episode would be E-F-C-B-A-D-G.
Sometimes, there may not be any goal node. A continuous task has no terminal state, that is,
no designated goal states.
Policies
The role of algorithms is called a policy in reinforced learning and policy maps states to
actions. In short, a policy takes the input states, processes it and suggests an action. Policies
characterize the agent behaviour function. There are two types of policies – deterministic and
stochastic.
It is t e probability o ta ing an action ‘a’ ile in state ‘s’. T e aim o t e agent is to learn
policy function so that the best actions are selected.
Given multiple policies, how to decide a better policy? There are many policies. A better
policy is the one that has better cumulative expected reward over other policies. More
formally, the value of the state is higher for the best action if the policy is optimal.
Rewards
The system performance is measured by reward system. A reward is something that the
environment produces to the agent. In short, it maps a set of states to a real number. The
symbol ‘r’ is sed or re ard, ic depends on c rrent state ‘s’, action ‘a’ and next state
‘s′’.
Immediate Reward
The environment immediately delivers a reward after the execution of action and the total
reward is associated with the episode. This is the sum of all rewards that come from the first
move to the last mode. The total reward is also called as return or goal.
It can be observed that every game has states. It then selects an action and transits to another
state. As a result of the transition, it collects the rewards too. This continues till the terminal
state. The return is given as:
Long-term Reward
Unlike immediate rewards, long-time rewards are given after the end like the objective of the
game.
The total future re ard is t e s m o t e c rrent move re ard and all ot er moves’ re ards
till the end of the episode. For example, if there are 9 moves in an episode, the total future
reward of the third move will be the sum of reward of that move and rewards of move 4 to
move 9 present in that episode.
More often, the iteration can be without terminal state too. In other words, it is a continuous
tas . To comp te t re re ards or tility, one can introd ce a actor γ called disco nt actor:
Gt is called return or utility or total reward. It is a value that indicates the closeness of the
goal state.
Its value ranges from 0 to 1. It gives more value for the immediate return. The
unpredictability can be tackled by discount factor, which is the sum of the immediate reward
and all ot er s bseq ent re ards m ltiplied by a actor o γ.
Example 14.1:
The reward at 5th step is 1. Therefore, the discounted reward using the Eq. (14.4) is given as:
Questions:
1. What are the primary components of Reinforcement Learning, and how do they
interact with each other?
2. Differentiate between learning problems and planning problems in Reinforcement
Learning.
3. How is the environment defined in the context of Reinforcement Learning, and what
role does it play?
If rewards are added to markov chain, the markov decision process is obtained where edges
are associated with probability as well as reward.
1. A set of states
2. A set of actions
3. Reward function R
4. Policy π
5. Value V
The simplest structure of MDP is Markov chain where the environment is modelled as a
graph. The nodes of graph represent states and edges represent transitions. Each edge is
associated with static probability.
Markov assumption holds good for reinforcement learning. Markovian property states that
the probability of reaching a state sts_tst and getting the reward rtr_trt depends solely on the
preceding states st− s_{t- }st− and rt− r_{t- }rt− . All other previous states are not
important. This is called Markov assumption.
Here, s′ and s are ne and old states and 'a' is t e action, and 'r' is t e re ard.
The aim of the agent is always to maximize the total rewards accumulated over time. The
movement of one state to another is called a transition. The agent manipulates the
environment from one state to another.
It indicates t e moving rom states to s′ a ter ta ing an action 'a'. In ot er ords, given a state
's', it is the probability of t e occ rrence o next state 's′. One can also represent it as a matrix
called state transition matrix where each row indicates the transition probability from one
state to another. The expected value of the reward is given as:
Once the MDP is modeled, the next stage is called training and inference. The agent tries to
accomplish goal repeatedly and modify its parameter over time. This is called learning. Once
the trained model is available, it is put to use and this process is called inference. At
deployment level, training and inference go together and when environment is changed,
training is done again. This is to improve the system.
Reinforcement learning uses trial and error to learn a series of actions that maximize the total
reward. There are two sub-problems in the reinforcement problem.
1. The main problem is the prediction of total reward or return. This is called prediction,
policy evaluation or value estimation. It requires a formulation of a function called
state-value function. The problem of estimating the state value function can be done
using temporal differencing or TD-Learning.
2. The second problem is finding actions that maximize the returns. It is called policy
improvement. Both these things can be combined as policy iteration which
alternatively uses the above to find optimal policy.
Assume a robotic arm in a hypothetical casino that when activated through a lever returns
money in the range, say, $1 to $10. A multi-arm bandit problem can have N levers. The
important issue is that the arm randomly returns the money in the range $1-$10. This is called
a reward. Given a limited chance, the aim of the user is to get as much money as possible.
Let us introduce a problem that is encountered in reinforcement learning very often called
multi-arm (or N-arm) bandit problem. A 5-armed slot machine is shown in Figure 14.7.
The point is which lever is the best one to activate to get maximum profit. A possible answer
to this question is: the lever that returns average return more than other levers is the best slot
machine. Let us formalize the problem: Given k number of chances to access N-arm slot
machine, with actions of pulling the lever with associated rewards, r1,r2,…,rk, the expected
reward given is given as:
This function is called action-value function or Q function. This indicates the value of taking
a particular action 'a'.
The best action is the action that returns the highest average return and is the indicator of the
action quality.
The selection of actions may be based on the concept of exploration and exploitation.
Exploration: the method explores all actions even if it results in sub-optimal decisions. It is
considered to be an adventurous method. In games, it works by exploring all actions and
observing rewards.
Exploitation: It is a naïve approach that uses current knowledge and keeps exercising those
options repeatedly. Its aim is to choose only one action based on experience and always get
the same reward. This is not adventurous and is mostly sub optimal.
There must be a balance between exploration and exploitation for selection. Based on these
principles, selection policies can be designed.
Greedy method:
In the absence of knowledge, the best action is chosen using the greedy method, based on
exploitation. In a multi-arm bandit problem, the agent tries all levers, notes down the rewards,
and repeatedly selects the lever giving the highest reward. This is called greedy approach,
where there's no exploration, only exploitation of known maximum rewards.
Greedy choice property: Making locally optimal choices leads to a global optimal
solution.
Questions:
The mathematical framework required for passive learning is provided by Markov Decision
Process. These model-based reinforcement problems can be solved using dynamic
programming after the construction of the model using MDP.
In reinforcement learning, the focus is to take action a to transit from the current state to the
end state. The aim is to get rewards for the actions. They can be positive or negative.
There are multiple courses of actions for a given state. The agent can prefer any course of
action. So, the behaviour of the agent needs to be understood. The algorithm to do that is
called a policy. A policy is the distribution of all actions with the probability of choosing
these actions. Different actions can have different rewards. It is necessary to find the total
reward. This is possible only if the reward is quantified. It is done using value functions.
Let v represent a value of the state. The state-value function is defined as follows:
It implies that state-val e nction o an MDP is t e expected ret rn rom state ‘s’ or a
policy.
ere, Vπ(s) adds up all expected rewards, possibly discounted rewards. It also indicates how
good the state is! The subscript π indicates that the value depends on policy π.
The value function quantifies the amount of reward for an action or state. It gives the
expected sum of future rewards for a given state under policy π.
Dynamic programming is used to solve this problem. As discussed in Box 14.2, dynamic
programming requires the recursive formulation of the given problem. This recursive
formulation for solving the state-value problem is called Bellman equation and is given as:
This shows the policy π as t e probability o c oosing an action ‘a’ en in state ‘s’.
vₛ is t e maxim m total re ard en starting rom state ‘s’. It ill be a maxim m o Q(s, a)
over all possible actions. Therefore,
If Q is known, then the best action to take optimally can be given as:
After defining the state value and action value functions, one can define the algorithms to
solve reinforcement problems. There are two types of algorithms
1. Value Iteration
2. Policy Iteration
Policy Iteration: The second algorithm is policy iteration. The task of finding an optimal
policy consists of two steps:
Policy Evaluation
Policy Improvement
Policy Evaluation:
Initially, for a given policy, the algorithm starts with value v1v_1v1 function as zero. There is
no reward. The Bellman equation is used to obtain value of v2v_2v2. Then, the value of
v3v_3v3 is obtained from v2v_2v2. This process is continued till the optimal policy
vπv_{\pi}vπ is obtained. In other words, at each iteration, all the state values are updated
using the previous iteration value.
Policy Improvement
The policy improvement can be done as follows:
Questions:
In model free methods, there is no complete knowledge of the environment. So, how to
determine the rewards then?
The simplest way to determine rewards is to play the game. If the game ends in win, then all
actions that are encountered in the episode are given a reward, say +1. If the game is lost,
then all actions are given a value of –1. The only problem is that the game may be lost due to
the last bad move while all other actions may be good.
The second method is the continuous formulation. Here, the reward is determined after the
action. For example, in multi-arm bandit problem, the immediate reward of $1 – $10 can be
an example of continuous formulation. If the actual value is not known, then it can be
estimated.
The third formulation is the discounted returns. The discounted return computed as the long-
term reward can be one solution.
Some of these techniques are used in these model-free techniques such as:
One of the simplest ways of simulating a model is by using Monte-Carlo methods. This is
discussed in the subsequent section.
Monte-Carlo Methods
Monte Carlo (MC) methods do not assume any model. Therefore, complete knowledge of the
environment is not available. This is similar to human and animal experience where the
knowledge is gained by experience, and experience is gained from the interaction with the
environment. MC uses the methods of experience as well as simulation to gain knowledge of
the environment.
The experience is divided into many episodes. An episode, as discussed earlier, is a sequence
of states from starting state to goal state. MC makes the following assumptions:
1. All episodes should terminate. No matter from where we start, the episode should end.
2. MC methods use value-action functions. These values are computed only after the
completion of the episode. So, MC methods are incremental methods.
The MC method collects the reward at the end of the episode. Then, it is used to find
maximum expected future reward. This method does not use expected return. Instead, it uses
the empirical mean as return. The total return over many episodes divided by the number of
episodes gives the average, alternative way to count every time a state is encountered and
reward until termination. Since the problem is non-stationary, the value function is computed
for fixed policy. Then, the policy is revised using dynamic programming.
Thus, the incremental Monte-Carlo update at the end of the episode using the approach -
incremental every-visit Monte-Carlo is given as:
ere, α\alp aα is called learning rate. The algorithm for MC methods is as follows:
Temporal Difference (TD) learning is an alternative to the Monte Carlo method. It is model-
free. It learns from the environment based on experience and interaction, as MC methods are
based on the principle of bootstrapping. TD is called a bootstrapping method because the
update is based on the existing estimate and future reward. The difference is the updated new
estimate. In the TD method, at every step, an estimate of the final reward is calculated at each
state. Then, immediately the state-action is updated for every step.
Unlike the Monte Carlo method that computes the rewards at the end of the episode, TD
Learning computes the rewards at every step. The difference is listed in the following Table
14.4.
Here, αα is a constant parameter, rtrt is the reward after time 't', and stst is the state visited at
time 't'.
Here, λ is called the decay parameter, whose value ranges between 0 and 1.
Questions:
Q-LEARNING
Q-Learning is an off-policy method because Q is updated based on policies that are implicit
to Q and is better guaranteed for maximum returns. Q indicates Quality.
What is Q-value?
Q(s, a) is a numerical value assigned to a state-action pair. It represents the value of the
action that is performed in state 's'.
The main objective is to find the Q-value, which is the immediate reward plus the future
rewards—collectively known as the total return reward. To compute this, the following
information is necessary:
1. Starting state
2. Action
3. Reward
4. New state
Initially, a table called the Q-table is constructed and filled with random initial values. Q-
learning involves two policies:
Learning policy
Update policy
The agent's next move will be the action with the highest reward, i.e., the cell with
the highest Q-value. Since decisions are based on Q-values, their computation and
updating are [Link] Q-value update is done using the Temporal-Difference (TD)
method (as mentioned in section 14.8.2). This update blends the new and old values using a
parameter α, When α = 0, the value does not [Link] α = 1, the new value completely
replaces the old one. Here, α is known as the learning rate. The discount factor (γ) is also
used in the update. Blending is done using temporal difference learning.
Here, r(st,at)r(st,at) is the reward obtained by performing action atat, and Q(st+1,a)Q(st+1
,a) is the estimate of the best action at state st+1st+1, i.e., s′s′, and Q(st,a)Q(st,a) is the Q-
value of the action aiai at state sisi. The best action performed at future states, discounted by
the discount factor γγ, is:
If TD is high, then it is a 'surprise factor' and denotes the highest reward. If TD is less, it
represents the 'frustration' factor and denotes a lesser reward. In other words, TD is a sort of
'reward'. It is initially high and slowly gets minimized as it reaches the end of training and the
final goal.
The update is carried out using the Temporal Difference learning (TD) Bellman equation.
The Bellman equation used to update value is given as follows:
SARSA LEARNING
SARSA is based on episodic formulation and discount factor. Its name is derived from the
function Q(s,a,r,s′,a′)Q(s,a,r,s′,a′), ere:
SARSA is an on-policy method because each action atat is based on QQ, and QQ is updated
based on the actions taken. SARSA converges to the optimal policy, provided all state-action
pairs are visited infinitely often.
The major difference between SARSA and Q-learning is that SARSA does not necessarily
use the maximum reward to update the Q-value. Instead, it is based on estimates, making
SARSA an online method.
Where:
The parameters of the SARSA algorithm as same as the Q-learning algorithm, that is,
learning rate and discount factor.
Questions: