def run_metropolis_MCMC(path): chain = [[0 for x in xrange(12)] for x in xrange(iterations)] chain[0][0] = startvalue[0] chain[0][1] = startvalue[1] chain[0][2] = startvalue[2] chain[0][3] = startvalue[3] chain[0][4] = startvalue[4] chain[0][5] = startvalue[5] chain[0][6] = startvalue[6] chain[0][7] = startvalue[7] chain[0][8] = startvalue[8] chain[0][9] = startvalue[9] chain[0][10] = startvalue[10] chain[0][11] = startvalue[11] prediction = run_eplus(path, totalarea) chain[0].append(prediction) make_csv(chain[0]) for i in range(1,iterations): while True: try: current_dir = os.getcwd() #proposal = proposalfunction(low_limit, upper_limit, chain[i-1][:-1], proposal_sd) proposal = proposalfunction(chain[i-1][:-1]) # prepare jobs #chainlist = make_chainlist(proposal) #markup_values_pairs = generate_markup_values_pairs(markup,chain) #markup_value_pairs = generate_markup_value_pairs(markup_values_pairs, count) markup_value_pairs = generate_markup_value_pairs(markup, proposal) path = prepare_job_folders(output_folder, template_idf_path, eplus_basic_folder, markup_value_pairs) print "___________________________",i, "th iteration____________________________________" prediction = run_eplus(path, totalarea) proposal.append(prediction) break except: os.chdir(current_dir) pass probab = min(1, math.exp(posterior(proposal) - posterior(chain[i-1]))) if np.random.uniform() < probab : chain[i] = list(proposal) print chain[i] add_chain(chain[i]) else: chain[i] = list(chain[i-1]) print chain[i] add_chain(chain[i]) return chain
# Set the range for proposal function #low_limit = [0.01, 0.01, 0.1, 0.1, 1, 1, 17, 17, 1, 0.1, 0.5, 2] #upper_limit = [0.5, 0.5, 8, 1, 100, 80, 28, 28, 50, 4, 0.99, 5] #proposal_sd = [0.02, 0.015, 3.5, 3.5, 1.3, 1.3, 4, 0.15, 0.06, 0.15] iterations = 4000 totalarea = 10336.99 count = 1 markup = [ '@@ROOF@@', '@@WALL@@', '@@EPD@@', '@@LPD@@', '@@HSP@@', '@@CSP@@', '@@OCC@@', '@@INF@@', '@@Boiler@@', '@@COP@@' ] markup_value_pairs = generate_markup_value_pairs(markup, startvalue) path = prepare_job_folders(output_folder, template_idf_path, eplus_basic_folder, markup_value_pairs) # RUN!! import time start_time = time.time() chain = run_metropolis_MCMC(path) print "Simulation time:", time.time() - start_time, "seconds" # Acceptance import itertools print "Acceptance: ", len(list( chain for chian, _ in itertools.groupby(chain))) / float(iterations) # make CSV file
startvalue = [0.116, 0.046, 11.67, 12.43, 21, 24, 14.37, 0.56, 0.72, 2.65] # Set the range for proposal function #low_limit = [0.01, 0.01, 0.1, 0.1, 1, 1, 17, 17, 1, 0.1, 0.5, 2] #upper_limit = [0.5, 0.5, 8, 1, 100, 80, 28, 28, 50, 4, 0.99, 5] #proposal_sd = [0.02, 0.015, 3.5, 3.5, 1.3, 1.3, 4, 0.15, 0.06, 0.15] iterations = 5 totalarea = 10336.99 count = 1 markup = ['@@ROOF@@','@@WALL@@','@@EPD@@','@@LPD@@', '@@HSP@@','@@CSP@@','@@OCC@@','@@INF@@','@@Boiler@@','@@COP@@'] markup_value_pairs = generate_markup_value_pairs(markup,startvalue) path = prepare_job_folders(output_folder, template_idf_path, eplus_basic_folder, markup_value_pairs) # RUN!! import time start_time = time.time() chain = run_metropolis_MCMC(path) print "Simulation time:",time.time() - start_time, "seconds" # Acceptance import itertools print "Acceptance: ",len(list(chain for chian,_ in itertools.groupby(chain))) / float(iterations) # make CSV file #make_csv(chain) # ____________________________________________________________________________________