Markov chain with extras?

tiissa tiissa at nonfree.fr
Wed May 18 17:10:32 EDT 2005


temp at mclift.fsnet.co.uk wrote:
> I want to use it for music. So given list 1 (melody), list 2 (chords)
> could be generated by a Markov chain. Also, given the chords the melody
> could be generated again by a chain.

So, at each time step you want:
  - chord(t) given melody(t-1), chord(t-1) and chord(t-2);
  - melody(t) given melody(t-1) and chord(t).
(or the other way round)

Is that correct?
If so, one could write:

#### hmm2.py
from random import random

def decision(p):
     """Return a single value according to a probability distribution."""
     # trivial due to our binary variables
     return (random()>p) and 1 or 0

def ct_given_mt1_ct1_ct2(mt1,ct1,ct2):
     """Chord given past melody and chords."""
     p0=0.5*ct1*ct2+0.25*ct1+0.125*ct2+0.0625
     return mt1 and p0 or 1-p0

def mt_given_mt1_ct(mt1,ct):
     """Melody given past melody and present chord."""
     return 0.1+0.5*mt1+0.3*ct

def timestep(chord,melody):
     """Chose next chord and melody."""
     chord.append(decision(
         ct_given_mt1_ct1_ct2(melody[-1],chord[-1],chord[-2])))
     melody.append(decision(mt_given_mt1_ct(melody[-1],chord[-1])))

chord=[0,1]
melody=[0]

for i in range(20):
     timestep(chord,melody)

print "Chord:\t%s"%''.join(map(str,chord[2:]))
print "Melody:\t%s"%''.join(map(str,melody[1:]))
####

This generates some 0-1 string according to the above conditional 
distributions.
What's needed there is to have proper variables for melody and chord 
with proper domains (instead of only binary values) and proper 
probability distributions (and proper decision method also although a 
draw is fair enough).

Usually, specifying your distributions is the hard part (some people 
actually cheat by having their programs learn these distributions ;)).

> I haven't had time to play around with your code and as I've only been
> studying python for about six months I don't quite understand what's
> going on. This might already do what I want, I just need to think in
> terms of notes and chords.

Doing Bayesian stuff myself, I'd give mathematical expressions to what I 
want. Then implementation follows. But I wouldn't rush into my favorite 
editor/python shell before having written some nice equations down. ;)





More information about the Python-list mailing list