CMPM147
Generative Design
Markov Models
Lucas N. Ferreira
University of California, Santa Cruz
Summer 19
UC Santa Cruz
Announcements
Last lecture we talked about:
Upcoming deadlines:
UC Santa Cruz
2
Markov Models
Markov Models
In probability theory, a Markov model is a stochastic model used to model randomly changing systems.
They can be applied to generate sequences of events that follow a given learned probability distribution.
UC Santa Cruz
4
Brief History
Markov models are named after the Russian mathematician Andrey Markov.��Markov studied such processes in the early 20th century, publishing his first paper on the topic in 1906
Markov Models
In probability theory, a Markov model is a statistical model used to model randomly changing systems.
There are four common markov models:
UC Santa Cruz
6
Markov Chains
A Markov Chain is a statistical model of a system that moves sequentially from one state to another. It is defined by:
describing the probability of changing from
state st-1 to a state st.
S1
S2
P(s2|s1 )
P(s1|s2 )
P(s2|ss )
P(s1|s1)
UC Santa Cruz
7
Markov Chains
For example, one can define a Markov chain of a baby's behavior (random variable) as follows:
States
S = { Playing (P), eating (E), sleeping (S) and crying (C) }.
Probability Distribution
| P | E | S | C |
P | 0.5 | 0.3 | 0.2 | 0 |
E | 0.3 | 0.4 | 0.3 | 0.1 |
S | 0.2 | 0.3 | 0.3 | 0.2 |
C | 0 | 0.3 | 0.2 | 0.4 |
UC Santa Cruz
8
Creating Markov Chains from Data
One can create a Markov Chain from a dataset of sequential data as follows:
This process is called training.
UC Santa Cruz
9
Sampling from a Markov Chain to Generate Content
After training a markov chain, one can sample events from it's probability distribution to generate content:
This process is called sampling.
UC Santa Cruz
10
Example: Name Generator
One can train a Markov Chain from a dataset of names (sequential data) to generate new random names:
Training
<start>Frodo<end>
<start>Samwise<end>
<start>Bilbo<end>
UC Santa Cruz
11
Example: Name Generator
One can train a Markov Chain from a dataset of names (sequential data) to generate new random names:
Training
2. Create a state for each character in the dataset (include a state for <start> and <end>).
State | a | b | d | f | i | l | m | o | <start> | <end> |
UC Santa Cruz
12
Example: Name Generator
One can train a Markov Chain from a dataset of names (sequential data) to generate new random names:
Training
3. For each char st, calculate the probability P(st|st-1) of st-1 appear before another char st-1.
State | a | b | d | f | i | l | m | o | <start> | <end> |
a | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
b | 0 | 0 | 0 | 0 | 0.5 | 0 | 0 | 0.5 | 0 | 0 |
... | | | | | | | | | | |
UC Santa Cruz
13
Example: Algorithmic Music Composition
The same idea can applied to generate monophonic music.
Training
[C, quarter], [E, quarter], [F, quarter], [E, 8th], [D, 8th], [C, 8th], [C, quarter], [D, quarter], [C, 8th], [A, 8th], [C, quarter]
UC Santa Cruz
14
Example: Algorithmic Music Composition
The same idea can applied to generate monophonic music.
Training
2. Create a state for each pair [note, duration] in the dataset
(include a state for <start> and <end>).
State | [C, quarter] | [E, quarter] | [F, quarter] | [E, 8th] | [D, 8th] | [C, 8th] | [D, quarter] | [A, 8th] | <start> | <end> |
UC Santa Cruz
15
Example: Algorithmic Music Composition
The same idea can applied to generate monophonic music.
Training
3. For each state s, calculate the probability of s appear after another state y.
State | [C, quarter] | [E, quarter] | [F, quarter] | [E, 8th] | [D, 8th] | [C, 8th] | <start> | <end> |
[C, quarter] | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
[E, quarter] | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
... | | | | | | | | |
UC Santa Cruz
16
Example: Algorithmic Music Composition
To model polyphonic music we can divide the pieces in timesteps (e.g. 16th notes) and create a state that represents end-of-timestep.
Every note in the same timestep is played simultaneously.
[C, quarter], [D, 8th], [E, 8th], <ts>, <ts>, <ts>, [C, quarter], <ts>, <ts>, <ts>, <ts>, [E, quarter], <ts>, <ts>, <ts>, <ts>, ...
UC Santa Cruz
17
High Order Markov Chain
Markov chains restrict the probability distribution to only depend on the previous state.
Higher-order Markov Chains relax this condition by
allowing the probability distribution P(st|st-1,...,st-d )
to include d previous states.
S1,1
S2,2
P(s2,2|s1,1, s1,2,s2,1 )
S1,2
S2,1
UC Santa Cruz
18
Example: Linear Level Generators
One can train a High Order Markov Chain from a dataset of linear levels (sequential data) to generate new random levels:
Training
UC Santa Cruz
19
Example: Linear Level Generators
One can train a High Order Markov Chain from a dataset of linear levels (sequential data) to generate new random levels:
Training
2. Define a level representation:
A level can be represented as a WxH two-dimensional array m, where each cell m[x][y] takes a value from a finite set of tiles T.
UC Santa Cruz
20
Example: Linear Level Generators
One can train a High Order Markov Chain from a dataset of linear levels (sequential data) to generate new random levels:
Training
3. Create a state for each tile in the dataset (include a state for <start> and <end>).
State | - | # | [ | ] | ? | B | p | <S> | ... |
UC Santa Cruz
21
Example: Linear Level Generators
One can train a High Order Markov Chain from a dataset of linear levels (sequential data) to generate new random levels:
Training
4. Calculate the probability P(sx,y|sx-1, y, sx, y-1) of a tile sx,y to be after a tile sx-1, y and above the tile sx, y-1
State | - | # | [ | ] | ? | B | p | <S> | ... |
(-,-) | P(-| -, -) | P(#| -, -) | P([| -, -) | P(]| -, -) | P(?| -, -) | P(B| -, -) | P(p| -, -) | P(<s>| -, -) | ... |
(-,#) | P(-| -, #) | P(#| -, #) | P([| -, #) | P(]| -, #) | P(?| -, #) | P(B| -, #) | P(p| -, #) | P(<s>| -, #) | ... |
(-,[) | P(-| -, [) | P(#| -, [) | P([| -, [) | P(]| -, [) | P(?| -, [) | P(B| -, [) | P(p| -, [) | P(<s>| -, [) | ... |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
UC Santa Cruz
22
Sampling
One can train a High Order Markov Chain from a dataset of linear levels (sequential data) to generate new random levels:
Sampling
UC Santa Cruz
23
How Much Training Data is Required?
In general, data-driven approaches (including Markov Chains) require a "large" amount of examples to learn a representative probability distribution.
The amount of data you need depends both on the complexity of your problem and on the complexity of your chosen algorithm.
UC Santa Cruz
24
References
Markov Chains
Applications
UC Santa Cruz
25