The Quest for Musical Intelligence Part 2: Markhov Chains and How They Can Work for You

So, this is a continuation in my hypothetically ongoing series of blogs about the development of music intelligence.

Link to my first entry in the subject:

To briefly summarize that blog: computers have been “writing” music since the 80s.

But now I want to delve deeper into the actual programming techniques and theories that helped drive these mechanisms. And one vocabulary phrase I saw thrown around all the time was “Markov Chain.”

What are Markov Chains?

So what are Markov chains, you might ask? Essentially it is keeping track of collection of states and the probability of which state shall succeed each member.

So the analogy I have seen thrown around has to do with weather. If we have a collection of weather reports that are either cloudy or sunny, and 50% of the days were sunny and vice versa, if we were to generate a randomization based on those stats, it would look something like:


However, what if a sunny day were more likely to be concurrent. Say if there was a 75% that tomorrow would be sunny if today was sunny. Well, then the real-world model might look something more like:


But hey, I made it out like this whole blog series from was going to be about musical intelligence, so how about we depart from the meteorological and focus more on the stars of music.

Generating Markov Model from Music Samples

And for an example of hypothetical musical data that everyone could remember and relate to, how about we use the discography of one of the most influential music groups of the past half century!

Why, Limp Bizkit of Course!

Pictured above, the voice of a generation.

Just kidding, it’s Queen. We’ll be sampling from Queen. I mean, come on, can you even name two Limp Bizkit songs?

Yeah, that feels better.

So specifically, I want to look at a few of Queens songs, and focus on the distribution and probable sequencing of chord progressions.

So, fair bit of warning: These songs were dictated by hand, arbitrarily by me. So this sample of Queen chords is not a holistic representation of the harmony in all of their works. Rather I’m taking a few memorable moments from several of their songs, and recording the frequency of each chord based on Harmonic function.

Also, I hope this is covered under fair use. Please don’t sue me for dictating some of your songs, Ghost of Freddy Mercury.

Yes, I know it’s supposed to be in 6/8, I am just trying to keep it visually simple.

It would be bloated to present every chord progression as sheet music, but below is a dictionary with the ordering of the chords during a few specific moments from Queen’s discography.

The Gooey musical DNA behind Queen’s magic.

Now, just visually you can see a few trends. VI to V to I is a common figure that appears in 3 of the 6 songs. vi goes to IV pretty frequently as well. Now these are actually in line with classic harmony. V to I is the classic cadence to a vast majority of the music pre-Beethoven. vi going to IV also fits within convention of harmony, and if you were to listen to these particular chord progressions, you’d hear how they seem to flow into one another in a familiar way.

And in this respect, we can see why a Markov Chain would be useful in trying to recreate the harmonies in the style of Queen. If we just cataloged the total frequency of each chord and randomly generated based on proportion, there would be none of that flow and voice leading that is so integral to all music.

A Markov Chain can be described as a matrix or table where each row and column are different states (in our example, the chords).

Creating the Matrix:

But just perusing it with our meager human eyes can only yield so much. So I built my own, very crude method intended to gather for each chord the probability of the following chord. I did this in jupyter notebook, with the methods referenced being helper methods created elsewhere in the ipython file.

Now for this, I had to normalize everything display percentages, after all, in a Markov Matrix, the sum of the values across each row must equal one. It would yield a dictionary containing lists of the probabilities, as shown below:


And from here, I could clean it up and construct a pandas Dataframe and Voila!

Behold! A very objective metric of Queen’s creative process!

So now that we have this matrix, let’s take a closer look at it, shall we?

Too close.

Aha! Those trends we thought about before are represented in the table! IV chords have a 66.7% chance to transition to a V immediately after. And from there, there is a 57.1% chance that V will lead back to the I!. But there are also maybe some other trends we can now see clearly. ii also has a tendency to lead into a V chord, and I has a 72% chance to lead into itself!

Wait, that doesn’t seem right.

That song is awesome, but not for its chord progression…

Oh yeah. “We Will Rock You” is just one chord repeated over and over again. We’ll, uh… we’ll call that a musical outlier and remove it shall we?

We still love We Will Rock You.

Ahhhhh. Much better. Jokes aside, we can already see how powerful analyzing chords progressions in this way can be if our ultimate goal is to generate our own Queen-style chord progression.

Generating our Own Queen Song:

And why not do that? With the help of some extra helper methods (that I’m not going to really get into right now), we can actually generate an 8 chord progression based on the Markov Chain Model from above.

Providing the method with the Dataframe above, it generates a sequence of 8 chords probabilistically.
Have another!

Now of course, this is comically basic. There is so much more that influences music than just simple chord progressions. However, this shows the basis of Markov Chains and how they can be useful for generating music. Melodies can function in similar ways, same with rhythms, and even though it can get exponentially more complex, understanding more and more musical concepts this way can finally bring us towards creating that brand new Queen song, almost 30 years after.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store