<jupyter_start><jupyter_text>Part (a) [Code debug/check for discrete distribution]<jupyter_code>V = Q
B0 = A0
np.random.seed(100)
for i in range(0,6):
    d = run_hmm(A0, PI0, discrete=True)
    xt = d['xt']
    yt = d['yt']
    plt.scatter(xt, yt, color=colors[i])
plt.title('Y v/s X for discrete distribution[Debug check plot]')
plt.xlabel('x')
plt.ylabel('y')
fig = plt.figure()
np.random.seed(100)
for i in range(0,6):
    d = run_hmm(A0, PI0, discrete=True)
    ax = fig.add_subplot(3,2,i+1)
    xt = d['xt']
    yt = d['yt']
    xtp, delta = viterbi(yt,PI0,A0,B0)
    plt.scatter(xtp, xt, color=colors[i])
    plt.subplots_adjust(hspace = .35)
    plt.title('Run {} Viterbi(X Predicted) vs Original(X) [Debug check]'.format(i+1))
    plt.xlabel("X Predicted")
    plt.ylabel('X Original')<jupyter_output><empty_output><jupyter_text>Part (a) [Actual]<jupyter_code>V = ['0','1','2','3','4','5']
Q = ['-1','0','1']
B0 = {}
for q in Q:
    B0[q] = {}
    for v in V:
        vi = int(v)
        B0[q][v] = b(vi, int(q))

A0 = {'-1': {'-1': 0.990 , '0': 0.005, '1': 0.005} , 
     '0': {'-1': 0.005 , '0': 0.990, '1': 0.005} , 
     '1': {'-1': 0.005 , '0': 0.005, '1': 0.990}}
PI0 = {'-1':1/3,'0':1/3,'1':1/3}

np.random.seed(100)
for i in range(0,6):
    d = run_hmm(A0, PI0, discrete=False)
    xt = d['xt']
    yt = d['yt']
    plt.scatter(xt, yt, color=colors[i])
plt.title('Y v/s X')
plt.xlabel('x')
plt.ylabel('y')
fig = plt.figure()
np.random.seed(100)
for i in range(0,6):
    d = run_hmm(A0, PI0, discrete=False)
    ax = fig.add_subplot(3,2,i+1)
    xt = d['xt']
    yt = d['yt']
    xtp, delta = viterbi(yt,PI0,A0,B0)
    plt.scatter(xtp, xt, color=colors[i])
    plt.subplots_adjust(hspace = .35)
    plt.title('Run {} Viterbi(X Predicted) vs Original(X)'.format(i+1))
    plt.xlabel("X Predicted")
    plt.ylabel('X Original')<jupyter_output><empty_output><jupyter_text>Part (b)<jupyter_code>np.random.seed(100)
for i in range(0,6):
    d = run_hmm(A0, PI0)
    xt = d['xt']
    yt = d['yt']
    alp = alpha_pass2(yt,A0,B0,PI0)
    alpha = alp['alpha']
    c = alp['c']
    sumlogc = sum([log(x) for x in c])
    prob = (-sumlogc)
    print(' Run {}  log P(O|lambda) = {}'.format(i+1, prob))<jupyter_output>Run 1  log P(O|lambda) = -1846.46897755
 Run 2  log P(O|lambda) = -1889.32552012
 Run 3  log P(O|lambda) = -1861.71821553
 Run 4  log P(O|lambda) = -1730.39971565
 Run 5  log P(O|lambda) = -1676.97025488
 Run 6  log P(O|lambda) = -1870.45855227<jupyter_text>Part (c)<jupyter_code>import operator
fig = plt.figure()

np.random.seed(100)
for i in range(0,6):
    d = run_hmm(A0, PI0)
    xt = d['xt']
    yt = d['yt']
    alp = alpha_pass2(yt,A0,B0,PI0)
    alpha = alp['alpha']
    c = alp['c']
    sumlogc = sum([log(x) for x in c])
    prob = (-sumlogc)
    beta = beta_pass2(yt,A0,B0,c)
    gamm = gamma_pass2(yt, A0, B0, c, alpha, beta)
    gamma = gamm['gamma']
    xtp = []
    for t in range(0,T):
        stats = gamma[t]
        ml = max(stats.iteritems(), key=operator.itemgetter(1))[0]
        xtp.append(ml)
    ax = fig.add_subplot(3,2,i+1)    
    plt.scatter(xtp, xt, color=colors[i])
    plt.title('Run {} Viterbi(X Predicted) vs Original(X)'.format(i+1))
    plt.xlabel("X Predicted")
    plt.ylabel('X Original')<jupyter_output><empty_output><jupyter_text>Part (e)<jupyter_code>def restimate(gamma, gammaij, c ):
    pi1 = {i: gamma[0][i] for i in Q}
    Aest = {str(k):{} for k in Q}

    for i in Q:
        for j in Q:
            num = 0
            denom = 0
            for t in range(0,T-1):
                num = num + gammaij[t][i][j]
                denom = denom+ gamma[t][i]
            Aest[i][j] = num/denom
    Best = {k: {} for k in Q}
    
    for i in Q:
        for j in V:
            num = 0
            denom = 0
            for t in range(0,T):
                if yt[t] == j:
                    num = num + gamma[t][i]
                denom = denom + gamma[t][i]
            Best[i][j] = num/denom
    
    logP = -sum(log(x) for x in c)        
    return {'A': Aest, 'B': Best, 'logP': logP, 'PI': pi1}
def run_estimator(itera):
    d = run_hmm(A0, PI0)
    xt = d['xt']
    yt = d['yt']
    notconverge = True
    oldlogP = -10e6
    iters = 0
    Aest = {'-1': {'-1': itera/9 , '0': 1/2-itera/18, '1': 1-itera/18} , 
         '0': {'-1': 1/9 , '0': 7/9, '1': 1/9} , 
         '1': {'-1': 1/9 , '0': 1/9, '1': 7/9}}
    PIest = {'-1':1/3,'0':1/3,'1':1/3}
    Best = {}
    for q in Q:
        Best[q] = {}
        for v in V:
            vi = int(v)
            Best[q][v] = 1/6

    while notconverge and iters<300:
        iters +=1
        alp = alpha_pass2(yt,Aest,Best,PIest)
        alpha = alp['alpha']
        c = alp['c']
        #print(alpha)
        beta = beta_pass2(yt,Aest,Best,c)
        gamm = gamma_pass2(yt, Aest, Best, c, alpha, beta)
        gamma = gamm['gamma']
        gammaij = gamm['gammaij']
        restimated = restimate(gamma, gammaij, c )
        Aest = restimated['A']
        Best = restimated['B']
        PIest = restimated['PI']
        logP = restimated['logP']
        #print(iters, logP)
        if oldlogP < logP and iters<1000:
            oldlogP = logP

        else:
            #print iters
            string=""
            for q in Q:
                aq=Aest[q]
                string +="[{} {} {}]\n".format(aq['-1'],aq['0'],aq['1'])
            print 'A:=', string
            string=""
            for q in Q:
                aq=Best[q]
                string +="[{} {} {} {} {}]\n".format(aq['0'],aq['1'],aq['2'],aq['3'],aq['4'],aq['5'])
            print 'B=', string
            string = "["
            for q in Q:
                string += "{} ".format(PIest[q])
            string+="]"
            print 'PI=', string
            break

np.random.seed(100)

for i in range(1,6):
    print '\n\n*****Run {} *********\n'.format(i)
    run_estimator(i)<jupyter_output>*****Run 1 *********

A:= [0.246924391174 0.0220290077434 0.731046601083]
[0.418139931836 0.282124884073 0.299735184091]
[0.118850384709 0.061229769756 0.819919845535]

B= [0.380563726785 0.000216558825213 0.0276147614056 7.05496793455e-63 0.591604952984]
[0.315304067095 0.369725193723 0.245241318991 1.76886394822e-05 0.0697117315511]
[0.14120994526 0.256276354334 0.234884828422 0.187996414293 0.133903531468]

PI= [0.135684181842 0.324049542843 0.540266275316 ]


*****Run 2 *********

A:= [0.051086077401 0.029376092532 0.919537830067]
[0.449080026465 0.244528379787 0.306391593748]
[0.139119365859 0.0502723278929 0.810608306249]

B= [0.37945918045 0.00196914955032 0.0519365709647 0.0 0.566635099035]
[0.44823827302 0.352883348462 0.174211468739 8.34781698752e-07 0.024666074997]
[0.144 0.258 0.235 0.185 0.133]

PI= [4.45353079512e-65 1.26607471232e-15 1.0 ]


*****Run 3 *********

A:= [0.221869219366 0.255178924529 0.522951856104]
[0.0626076205707 0.795140814661 0.142251564768]
[0.13352[...]
{'fraction_non_alphanumeric': 0.19320656118449703, 'fraction_numerical': 0.15270721439976775, 'mean_word_length': 2.4780413932357397, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '470257', 'license': 'MIT', 'n_tokens_mistral': 3590, 'n_tokens_neox': 2976, 'n_words': 606, 'path': '2015_Fall/MATH-541B/hmm/HMM Problem-2.ipynb', 'repo_name': 'NeveIsa/hatex', 'size': '6889'}