Skip to content

Instantly share code, notes, and snippets.

@MLWhiz
Created June 20, 2019 18:37
Show Gist options
  • Select an option

  • Save MLWhiz/8aa5e7761a44e4cc87db3c0d0c5c1dc6 to your computer and use it in GitHub Desktop.

Select an option

Save MLWhiz/8aa5e7761a44e4cc87db3c0d0c5c1dc6 to your computer and use it in GitHub Desktop.
# Takes as input a text to decrypt and runs a MCMC algorithm for n_iter. Returns the state having maximum score and also
# the last few states
def MCMC_Golddigger(n_iter,W,G,W_max, Beta_start = 0.05, Beta_increments=.02):
M = len(W)
Beta = Beta_start
current_X = [0]*M # We start with all 0's
state_keeper = []
best_state = ''
score = 0
for i in range(n_iter):
state_keeper.append(current_X)
proposed_X = create_proposal(current_X,W,W_max)
score_current_X = score_state_log(current_X,G,Beta)
score_proposed_X = score_state_log(proposed_X,G,Beta)
acceptance_probability = min(1,math.exp(score_proposed_X-score_current_X))
if score_current_X>score:
best_state = current_X
if random_coin(acceptance_probability):
current_X = proposed_X
if i%500==0:
Beta += Beta_increments
# You can use these below two lines to tune value of Beta
#if i%20==0:
# print "iter:",i," |Beta=",Beta," |Gold Value=",np.dot(current_X,G)
return state_keeper,best_state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment