(more) Fun with Markov Chains

(more) Fun with Markov Chains

Expanded & improved version of this talk given at the Memphis Machine Learning & Data Science meetup. http://www.meetup.com/memphis-technology-user-groups/events/230024221/

D57aec10399cbb252bd890c2bb3fe1c9?s=128

Brad Montgomery

May 04, 2016
Tweet

Transcript

  1. Markov Chains! fun with Brad Montgomery @bkmontgomery

  2. what is a markov chain? A stochastic process… sequence of

    random variables… serial dependence only between adjacent periods… …systems that follow a chain of linked events, where what happens next depends only on the current state of the system.
  3. what is a markov chain?

  4. Andrey (Andrei) Andreyevich Markov 1856 - 1922

  5. markov models & text A Mathematical Theory of Communication 1949

    paper by Claude Shannon
  6. Example from Wikipedia: A creature who eats only grapes, cheese,

    or lettuce, based on these rules: • It eats once a day. • If it ate cheese today, tomorrow it will eat lettuce or grapes with equal probability. • If it ate grapes today, tomorrow it will eat • grapes with probability 1/10 • cheese with probability 4/10 • lettuce with probability 5/10. • If it ate lettuce today, tomorrow it will eat • grapes with probability 4/10 • cheese with probability 6/10 • It will not eat lettuce again tomorrow.
  7. funny things on the internet

  8. – King James Programming http://kingjamesprogramming.tumblr.com/ “17:20 O LORD, there is

    none end of the regexp.”
  9. – Erowid Recruiter https://twitter.com/erowidrecruiter “Good morning, I saw your profile

    looks like a very long profile and thought it looked like monsters and I am an executive recruiter”
  10. https://twitter.com/Horse_ebooks/status/372490043320852481

  11. Mark V. Shaney A Usenet bot from the 1980s trolling

    net.singles
  12. How does it work? 1. Train it on a bunch

    of text. 2. Let it generate some text. 3. Commence Hijinks
  13. – Edgar Allen Frost “But tis not true that thus

    I dwelt aloof For the rare and radiant maiden whom the angels name Lenore.”
  14. A Patch of Old Snow There's a patch of old

    snow in a corner That I should have guessed Was a blow-away paper the rain Had brought to rest. It is speckled with grime as if Small print overspread it, The news of a day I've forgotten-- If I ever read it.
  15. A Patch of Old Snow There's a patch of old

    snow in a corner That I should have guessed Was a blow-away paper the rain Had brought to rest. It is speckled with grime as if Small print overspread it, The news of a day I've forgotten-- If I ever read it.
  16. A Patch of Old Snow There's a patch of old

    snow in a corner That I should have guessed Was a blow-away paper the rain Had brought to rest. It is speckled with grime as if Small print overspread it, The news of a day I've forgotten-- If I ever read it.
  17. A Patch of Old Snow There's a patch of old

    snow in a corner That I should have guessed Was a blow-away paper the rain Had brought to rest. It is speckled with grime as if Small print overspread it, The news of a day I've forgotten-- If I ever read it.
  18. A Patch of Old Snow There's a patch of old

    snow in a corner That I should have guessed Was a blow-away paper the rain Had brought to rest. It is speckled with grime as if Small print overspread it, The news of a day I've forgotten-- If I ever read it.
  19. { (‘And', 'the'): ['sweet', 'pear', 'cloud', 'stars', 'silken', 'only', 'Raven',

    ‘lamplight’], } A word pair. Words that always follow the pair
  20. A Patch of Old Snow There's a patch of old

    snow in a corner That I should have guessed Was a blow-away paper the rain Had brought to rest. It is speckled with grime as if Small print overspread it, The news of a day I've forgotten-- If I ever read it.
  21. [ ('my', 'loss.'), ('all', 'windstirred.'), ('to', 'rest.'), ('read', 'it.'), ('time',

    'talk.'), ('friendly', 'visit.'), ('in', 'ice.'), ('favour', 'fire.'), ('would', ‘suffice.'), … ] A list of all line endings.
  22. 1. Pick a random sentence-ending pair. key: ('still', 'abide.')

  23. 1. Pick a random ending pair. 2. Use that as

    a key to pick a random word { (‘still', ‘abide.’): [‘But'] }
  24. 1. Pick a random ending pair. 2. Use that as

    a key to pick a random word { (‘still', ‘abide.’): [‘But'] } Output: But key: (‘abide.’, ‘But’) 3. Keep the word; use it as part of your key.
  25. Output: But lookup: {(‘abide', ‘But’): [‘tis']} 4. Keep doing this…

    Output: But tis lookup: {(‘But', ‘tis’): [‘not']} Output: But tis not lookup: {(‘tis', ‘not’): [‘true']} Output: But tis not true that lookup: {(‘not', ‘true’): [‘that']}
  26. Output: But tis not true that thus lookup: {(‘that', ‘thus’):

    [‘I']} Output: But tis not true that thus I lookup: {(‘thus', ‘I’): [‘dwelt']} Output: But tis not true that thus I dwelt lookup: {(‘I', ‘dwelt’): [‘aloof']} Output: But tis not true that thus I dwelt aloof lookup: {(‘dwelt', ‘aloof’): [‘For']}
  27. Key:(‘read', ‘it.’) 5. Until your key is a sentence ending.

    But tis not true that thus I dwelt aloof For the rare and radiant maiden whom the angels in Heaven above Nor the demons down under the sea In her sepulchre there by the sea A wind blew out of a day I've forgotten If I ever read it.
  28. https://github.com/bradmontgomery/shaney Search: Mark v. Shaney for the original. My pep8-compliant

    (and more readable!) version: ~109 lines of python with comments
  29. 2nd-order markov chain • Generate a word based on the

    TWO preceding words. • Technique used by Mark V. Shaney. • Generates pretty good (almost believable) output.
  30. 3rd-order? • Generate a word based on the THREE preceding

    words. • Should be more realistic!
  31. http://markovian-wizzzdom.herokuapp.com/ A markov chain trained on developer job ads, fox

    news, bodybuilding & startup blogs
  32. Resources • Khan Academy course on Information Theory • Wikipedia

    / Markov Chain • Markov Chains explained visually • http://blog.codinghorror.com/markov-and-you/ • Wikipedia / Mark V. Shaney