Lagrangian and Hamiltonian Mechanics
The physics of Hamiltonian Monte Carlo, part 2: Building off the Euler–Lagrange equation, I discuss Lagrangian mechanics, the principle of stationary action, and Hamilton's equations.
This is the second of three posts in a series on Hamiltonian Monte Carlo (HMC). The series is motivated by the observation that the HMC tutorials I have read all assume that the reader already understands the underlying physics: the Euler–Lagrange equation, the calculus of variations, and Hamiltonian dynamics. However, this undercuts any explanation for the uninitiated. Thus, my ambition is to explain Hamiltonian dynamics in sufficient detail such that HMC makes sense.
In the previous post, I explained the Euler–Lagrange equation and the calculus of variations. I recommend reading that post first if you don’t already know what those terms mean. In this post, I will explain how Lagrangian and Hamiltonian mechanics use the Euler–Lagrange equation to reformulate of Newtonian mechanics. In the next and final post, I will present HMC.
Examples
Like all good abstractions, Lagrangian mechanics may feel squishy at first because it is more general than the thing with which you are comfortable, the thing it abstracts. In this case, Lagrangian mechanics models the energies in a system rather than the forces, but it is hard to immediately see why this is a good idea. Because of this, I’ll lead with two simple examples before backing out generalities.
Consider this equation, called the Lagrangian, where is kinetic energy and is potential energy:
I’m presenting this ex niliho now, but we’ll derive it in the next section. For now, just think of it as a scalar quantity representing the energy in a system. Next, let’s rewrite the Euler–Lagrange equation, which we derived in the previous post, as
Here, is the Lagrangian in , is time, is position, and indicates the first derivative of with respect to time . Later, we’ll see , which is the second derivative with respect to time. I’m embracing this dot notation from physics to be consistent with how that community presents this material
Now consider a classic problem from physics: modeling a frictionless spring with a mass and spring constant . Recall from high school physics that the kinetic energy is , and its potential energy is . As a sanity check, the kinetic energy depends on the mass and the first derivative of position w.r.t. time or velocity; for a given spring constant, the potential energy is governed by the position. Then the Lagrangian is
Using the Euler–Lagrange equation , we see that the function for which the Lagrangian is stationary is
The last line of is just a reformulation of . To see that, recall—again from high school physics—that the negative derivative w.r.t. position of potential energy is equal to force; therefore
Was this just a coincidence? A cute mathematical trick? Let’s consider another example, a particle moving in three-dimensions with potential energy . The total kinetic energy decomposes as a sum of the kinetic energy for each dimension, giving us
And this is equivalent to the vector formulation of because
and again . So we have one again recovered Newton’s second law, but in three dimensions.
If this were a physics course, we would probably look at a couple more examples such as a pendulum or double pendulum, although the reader can easily find these in a physics textbook or online. The questions for us are: how did we know that would work for physics problems? Why is this formulation useful? Are there generalizable principles we want to have a good intuition for before learning HMC? Let’s explore these questions.
Deriving Lagrangian’s equation
We want to reformulate classical or Newtonian mechanics into a framework that models energies rather than forces. To show how Joseph–Louis Lagrange may have arrived at his famous equation let’s rederive it directly from Newton’s second law, . For ease of notation, we’ll stick to one dimension.
We saw in that we can write as
which we can further rearrange as
We can then express the right-most term to be a derivative with respect to :
Now notice that the partial derivative of w.r.t. is zero, and the partial derivative of w.r.t. to is also zero. Therefore, we can write as
We’ve just added zeros to both sides of the equation. Finally, note that on both sides of the equation is just , the kinetic energy. We can rewrite as
And is just Euler–Lagrange’s equation where the function is equal to . And this is how we arrive at the Lagrangian in . The function is a consequence of reformulating in terms of the Euler–Lagrange equation in . Thinking of in this way leads to a deep and beautiful idea in physics called the principle of stationary action.
The principle of stationary action
Recall in the previous post that we integrated our function with respect to —in this post is now and is now —and solved for the stationary function that extremized that integral. Intuitively, think of the functional or integral as representing all “paths” between the endpoints of the integral, and the stationary function as the extremizing or “shortest” path.
The Euler–Lagrange equation is general; it is a solution for any such functional. However, let’s now work specifically with the Lagrangian . Then the functional or integral of with respect to time is
This quantity is called the action because it has dimensions . We can produce an action for any function —think: any change in position across time. And we already know that the stationary function of this action is given by the Euler–Lagrange equations in . Finally, we just saw through two examples and by rederiving that the stationary function induced by is in fact .
What does this mean? Speaking loosely, it means that Newton’s laws of motion can be viewed as objects taking the path of “least action” or least energy across time. This is what it meant by the principle of stationary action, also called Hamilton’s principle:
The path taken by a system between two time points is the one for which the action is stationary.
In my mind, the Euler–Lagrange equation isolated from physics is just a generalization of what we learned in calculus: a function’s minimum or maximum is analogous to a functional’s stationary function. However, the fact that is the stationary function for a functional that represents energy across time suggests that when apples fall to the ground or when planets move through the solar system, they are taking the path of least action. This is a really cool idea.
Generalized coordinates and constraints
The Lagrangian reformulation does not introduce any new physics. So why is it useful? First, the principle of stationary action is a beautiful insight into the laws of physics and nature. That alone justifies the effort. However, a second benefit of using Lagrangian mechanics is that we can often solve complicated problems more simply than if we used Newtonian mechanics. This is because Lagrangian mechanics elegantly handles constraints. Understanding this point requires a bit of setup, but I think it’s worth it in the context of understanding HMC.
A constraint is a restriction in the freedom of movement in a particle or a system of particles. For example, suppose a particle is free to move in three dimensions. We can specify its position using different coordinate systems: Cartesian , cylindrical , spherical , and so forth. A holonomic constraint is a constraint that reduces the number of coordinates needed to specify the position of a particle. For example, if the particle can only move on a table top, we might model this as a holonomic constraint to the plane, and the number of Cartesian coordinates needed to fully specify the particle’s position is reduced from three to two. A nonholonomic constraint restricts the range of the particle. For example, requiring that the particle stay within a box or only takes a positive value is a nonholonomic constraint.
The minimal set of required coordinates are called generalized coordinates. Since we can use any coordinate system we like, we denote these generalized coordinates by with . Here, is the number of all coordinates for all particles in the system after subtracting constraints. If there are particles, coordinates which can be eliminated, and coordinates per particle, . Thus, denotes a single particle’s position on a single coordinate.
We can even combine coordinate systems. For example, a two particle system with might have Cartesian coordinates for the first particle and polar coordinates for the second. Each generalized coordinate has an associated generalized velocity . This is called a velocity because it is a change in position w.r.t. to time, but the units are not necessarily .
After we have chosen a set of generalized coordinates for our problem, the Lagrangian can be written as
and the action or time integral over the Lagrangian is
We can trace the same logic as in the previous post to rewrite a generalized Euler–Lagrange equation as
Part of the power of this system is that the functional we extremize, the action, is a scalar quantity. If we reparameterize the coordinate system, we induce the same action. We simply “relabel” the stationary path with the new coordinate system.
Furthermore, since the Lagrangian is only in terms of the energy of the system, any force on the system that does no work, that does not have a net energetic contribution, can simply be dropped from the problem at setup. We can even be clever about choosing an appropriate coordinate system that suits the problem by minimizing the number of generalized coordinates in .
The full benefit of the Lagrangian framework is, I admit, hard to grok without doing problems, but my ultimate goal is to provide sufficient intuition for setting up Hamiltonian mechanics and therefore HMC—which I think we have done at this point. However, if you want to see a problem that is simpler in Lagrangian mechanics, consider the double pendulum. It might look like both solutions are equally tedious, but notice that the Lagrangian formulation does not require worrying about constraints. They are implicitly handled through the problem setup. As a result, the Lagrangian solution is roughly two pages, while the Newtonian solution is roughly four.
Finally, we can (kind of) understand what physicists mean when they say that the Lagrangian formulation can exploit the “symmetry” in a problem. By symmetry, we mean that if we change the coordinates by some small values, another quantity is conserved, meaning it does not change. If we know a problem has such a symmetry, we can use appropriate generalized coordinates to exploit it. This gives some intuition for why the Lagrangian reformulation might make very complex dynamical systems easier to work with. Even when many particles are moving in the system, from the perspective of a conserved quantity, they are still well-behaved.
Hamiltonian mechanics
We are finally ready for Hamiltonian mechanics, which is a reformulation of Lagrangian mechanics. Compared to Newtonian mechanics, both Lagrangian and Hamiltonian mechanics seem similar. However, there are good reasons to prefer the latter reformulation, and I’ll focus on points that make sense in the context of Hamiltonian Monte Carlo.
While the Lagrangian was where is kinetic energy and is potential energy, the Hamiltonian is
Here, is the generalized momentum induced by the generalized coordinates. This is because . Let’s derive this definition from the Lagrangian in . This derivation will highlight a couple of key points about the Hamiltonian reformulation and show us how to get from Lagrangian to Hamiltonian mechanics. The big idea is that we can derive the Hamiltonian by taking the derivative of w.r.t to time and setting that expression equal to zero.
Note that explicitly depends on , but that and also implictly depend on . Therefore, the total derivative of w.r.t. requires the multivariable chain rule:
Now let’s substitute the left-most term in with the right-hand side of the Euler–Lagrange equation while simplifying any derivative w.r.t. using our dot notation:
Now note that everything in the underbrace above can be viewed as a product rule because
Substituting into , we get
We can now move the left-hand side to the right-hand side and factor out the total time derivative,
We’re almost done. Now notice that is the generalized momentum in . So we can write as
The underbraced term in is the Hamiltonian or in . Finally, note that the generalized momentum, can be written as . This is by taking the partial derivative of the Lagrangian in w.r.t. . Thus, we can write the Hamiltonian as
Here, we use the fact that and that can be written in terms of generalized coordinates, velocities, and momenta as
Why is this Hamiltonian a nice object? Notice that in , if , if the Lagrangian is not an explicit function of time, the Hamiltonian is constant or conserved. And the interpretation of is fairly straightforward: it is simply the energy of the particle. In fact, one way physicists can define “energy” is simply as the conserved quantity when a system has time translational symmetry. Thus, the Hamiltonian reformulation emphasizes a particular invariance that is commonly found in physical systems.
Again, none of this breaks Newtonian physics, but hopefully it’s clear that this is a very elegant and useful way to model the world.
Hamilton’s equations
Remember how the real goal of these posts was to understand enough physics to understand Hamiltonian Monte Carlo? Well, now we are finally ready to present the very first set of equations in Radford Neal’s introduction to HMC (Neal, 2011): Hamilton’s equations. Let denote work, and note that we can write the potential energy as
Furthermore, note that we can write the kinetic energy as
because . Taken together, this means
and therefore
are called Hamilton’s equations. For some physical intuition, consider the one-dimensional case of a single particle. Since
the first of Hamilton’s equations says that Newtonian force of the particle is equal to the negative gradient of its potential energy. And since the time derivative of position is generalized velocity, the second equation says that the velocity of the particle is equal to the time derivative of its kinetic energy.
As we will see in the next post, we are going to leverage Hamilton’s equations and some of their properties to build a proposal distribution that reduces correlation between samples by conserving energy across Markov chain steps.
- Neal, R. M. (2011). MCMC using Hamiltonian dynamics.