## Markov chain (2 states)¶

In [1]:
import numpy as np

In [6]:
np.random.rand(5)

Out[6]:
array([ 0.42971084,  0.81056205,  0.82071884,  0.03796935,  0.3911168 ])
In [13]:
a = np.random.rand(5)
a

Out[13]:
array([ 0.78319013,  0.86375969,  0.08341404,  0.86768919,  0.51557637])
In [14]:
a < 0.7

Out[14]:
array([False, False,  True, False,  True], dtype=bool)
In [15]:
r = np.random.rand()
r < 0.7

Out[15]:
True
In [18]:
s = 0  # state
states = [s]
#print(s)
for i in range(100):  # iterate over "time"
r = np.random.rand()
if s == 0:
if r < .7:
s = 0  # (actually not necessary)
else:
s = 1
else: # s==1
if r < .8:
s = 0  # (actually not necessary)
else:
s = 1 # (actually not necessary)
#print(s)
states.append(s)
print(states)

[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1]


## Markov chain: more than 2 states¶

Create a function that takes in an array of probabilities, and returns an index with the specified probability.

In [27]:
def next(pin):
# pin might be a list not a numpy array
p = np.array(pin)
print(p)
csp = p.cumsum()
print(csp)
r = np.random.rand()
print('r=',r)
print(r < csp)
i = (r<csp).argmax()  # index of first True in r<csp
return i

next([.1,.5,.4])

[ 0.1  0.5  0.4]
[ 0.1  0.6  1. ]
r= 0.12572396778380068
[False  True  True]

Out[27]:
1
In [22]:
a = np.random.randint(0,10,12)
a

Out[22]:
array([6, 2, 6, 2, 7, 5, 5, 7, 3, 5, 9, 8])
In [23]:
a.max()

Out[23]:
9
In [24]:
a.argmax()

Out[24]:
10

# My Title¶

## Introduction¶

### Subsection A¶

Plain text or formatted math: $$\int_0^\infty e^{-x}dx$$

# Word play¶

In [34]:
with open('words.txt') as f:
words = [word for word in words if "'" not in word and len(word)>0]  # remove all possessives
words = [word for word in words if word[0].islower() ]  # remove all proper nouns
words[:10]

Out[34]:
['a',
'aardvark',
'aardvarks',
'abaci',
'aback',
'abacus',
'abacuses',
'abaft',
'abalone',
'abalones']
In [32]:
words[50000:50010]

Out[32]:
["hairstyle's",
'hairstyles',
'hairstylist',
'hairstylists',
'hairy',
'hake',
"hake's",
'hakes',
'halberd',
"halberd's"]
In [29]:
'drawer'[::1]

Out[29]:
'drawer'
In [30]:
'drawer'[::2]

Out[30]:
'dae'
In [31]:
'drawer'[::-1]

Out[31]:
'reward'
In [35]:
words.index('abacus')

Out[35]:
5
In [36]:
words.index('dog')

Out[36]:
16721
In [ ]:
sorted with a "key", index,

In [37]:
'{:>20}'.format('hello')

Out[37]:
'               hello'
In [38]:
for word in ['log','prolog']:
print('{:>20}'.format(word))

                 log
prolog

In [39]:
swords = sorted(words, key=lambda x:x[::-1] )
swords[:10]

Out[39]:
['dB', 'pH', 'kW', 'a', 'baa', 'ameba', 'amoeba', 'samba', 'marimba', 'rumba']
In [40]:
for word in swords[:20]:
print('{:>20}'.format(word))

                  dB
pH
kW
a
baa
ameba
amoeba
samba
marimba
rumba
scuba
tuba
alpaca
maraca
mecca
yucca
silica
basilica
replica
mica

In [45]:
def neighborhood(word,n):
i = swords.index(word)
for word in swords[i-n:i+n+1]:
print('{:>20}'.format(word))

In [49]:
neighborhood('muck',7)

           woodchuck
upchuck
shuck
luck
cluck
pluck
potluck
muck
amuck
schmuck
snuck
puck
truck
struck
awestruck

In [41]:
from IPython.display import Image

In [42]:
Image('import_kidney.png')

Out[42]: