Creating First Species Counterpoint using Glauber Dynamics

Jonah Botvinick-Greenhouse, Rachel Tan, and Oliver VanderBerg


For this project, we used Markov chains to create a music generator.

Our generator first creates two random sequences of notes to be played simultaneously. Here is an example:



We then introduce a function  $$H$$  (”Hamiltonian”) which assigns a score to the sequences based on how well they follow the rules of first species counterpoint. Counterpoint is a term used to describe music with independent voices. In first species counterpoint, certain intervals and types of melodic movement are considered desirable. For example, we gave a point for moving in contrary motion and subtracted one for tritones (a harmony undesirable in species counterpoint).

We wrote a Python program that used Glauber Dynamics in order to improve these randomly generated sequences to better follow the rules of first species counterpoint. Our program randomly selects one of the notes in the sequences and replaces it with a new (possibly the same) note according to some probability distribution.

More specifically, notes are represented by integers (in our case between $$0$$ and $$25$$), and the state space for the algorithm is the set of sequence of notes of length $$L$$. In our case, $$L = 20$$. Let $$\sigma$$ be such a sequence. Then,

$$\sigma = (\sigma(1),\sigma(2),\dots , \sigma(L))$$.

Given $$\sigma$$, write $$\sigma^{m,j}$$ for the sequence of notes obtained from $$\sigma$$ by replacing the $$m$$-th note in $$\sigma$$ by $$j$$, keeping all the remaining notes unchanged. That is

$$ \sigma^{m,j}(n) = \begin{cases} j & n=m \\ \sigma(n) &\mbox{otherwise.}\end{cases}$$

Next, we fix $$\beta\in{\mathbb R}$$.

At each time-step, a transition from the current state $$\sigma$$ to any of the possible states $$\sigma^{m,j}$$ is proposed with probability proportional to $$ e^{\beta H(\sigma^{m,j})}$$. In other words,

$$ P(\sigma \to \sigma^{m,j}) = \frac{ e^{\beta H(\sigma^{m,j})}}{\sum_{m’,j’} e^{\beta H(\sigma^{m’,j’})}}.$$

Note that if we let $$\beta = 0$$ the selection process becomes completely random, while positive values of $$\beta$$ favor the selection of more “desirable” notes, and negative values of $$\beta$$ favor the selection of less “desirable” notes. 

With $$\beta > 0$$, notes that increase the overall score have a higher probability of being chosen than notes that decrease the score. Thus, after many iterations, we typically obtain music that better obeys the rules of first species counterpoint than the random sample from which we began.

The resulting sequences of notes are still random but possess more pleasing harmony and melodic direction. Here is an example:

 After: when “higher” scores are preferred


Here is an example with $$\beta < 0$$. The results are definitely less pleasant.

After: when “lower” scores are preferred