Question

In: Computer Science

Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in...

Implement the Metropolis-Hastings algorithm below¶

Make sure to read this: Implement the algorithm described above (in the "How it works" section), where you take some user-defined number of steps that randomly step in W and I and are scaled by a step_size. This means you don't want to take steps of a discrete size, but instead use a random distribution of step sizes that are scaled by your step_size variable. You'll want to take steps that have an equal chance of either decrementing or incrementing the values of W and I. Also, W and I don't have to incremented by the exact same amount, in fact, it would best if they weren't so that you can explore more of the parameter space.

Keep track of your accepted guesses for width and intercepts in the lists that are provided below. We have included variables with reasonable values of these quantities to get you started.

# Total number of points we're going to sample (start out with at least 10^4)
num_sample_points = 100000

# Weight factor in front of the random step
step_size = 0.1

# As we move the walker around, we'll use these same lists to
# store our new values so that we can visualize the path later!

# (Note: that means you'll want to append new values to these lists!)


widths = [2]
intercepts = [2]

## PUT YOUR CODE HERE ###

Solutions

Expert Solution

SOURCE CODE IN PYTHON:

# Total number of points we're going to sample (start out with at least 10^4)

num_sample_points = 100000

# Weight factor in front of the random step

step_size = 0.1

# As we move the walker around, we'll use these same lists to

# store our new values so that we can visualize the path later!

# (Note: that means you'll want to append new values to these lists!)

widths = [2]

intercepts = [2]

## PUT YOUR CODE HERE ###

import random

for i in range(num_sample_points):

w_change=random.randint(-10, 10)*step_size

i_change=random.randint(-10, 10)*step_size

widths.append(widths[len(widths)-1]+w_change)

intercepts.append(intercepts[len(intercepts)-1]+i_change)

#output the first 10 values of each

for i in widths[:10]:

print('%.2f ' %i, end='')

print()

for i in intercepts[:10]:

print('%.2f ' %i, end='')

print()

OUTPUT:

Refer to screenshot of code for indentations.


Related Solutions

implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve...
implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve the classes that programming problems 4 and 5 describe This is to be written in C++. I cannot provide anymore information, I cannot provide any class information, etc. This is all the problem that book gave me. This is for Data Structures and algorithms.
You are provided with a StackInterface to implement your stack, make sure to implement the data...
You are provided with a StackInterface to implement your stack, make sure to implement the data structure using java.util.ArrayList and naming your implementation ArrayListStack (make sure to use generics like you have been for the data structures so far). The provided StackTester assumes you name your implementation that way. Note that you have also been provided a PalindromeTester with the specific isPalindrome() method you are required to implement, following the approach above. The method should take whitespace, case-sensitivity, digits, and...
Your team will make a modification of the program #1 above, that will read in the...
Your team will make a modification of the program #1 above, that will read in the string as a “command-line argument” to your program, instead ofhaving the user type it while your program is running. Your program should print out the inverted string to the screen. For example, if you are running your program in the command window (by clicking the Start button, then typing in “cmd”, then pressing Enter, then using the “cd ” command to change to the...
Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters...
Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters and frequencies as listed below. The total number of nodes is n = 6.
You are about to implement the wireless network security measures and policies you just described above,...
You are about to implement the wireless network security measures and policies you just described above, and your boss sends you to talk with two operations managers. The managers are concerned that new restrictions will inhibit their workers and lower performance, which would create a risk to the company in meeting its minimum production levels. Your task (to answer this question) is to explain to the managers how the wireless security measures and policies relate to risk management for the...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. 2)You must implement a recursive Mergesort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. My List.txt Values 7 3 4 1 4 4 9 9 4 8 4 5 3 9 2 3 7 0 6 4 4 5 0 1 9 2 1 7...
Read about the Sieve of Sundaram. Implement the algorithm using function composition. Given an integer n,...
Read about the Sieve of Sundaram. Implement the algorithm using function composition. Given an integer n, your function should generate all the odd prime numbers up to 2n+2. sieveSundaram :: Integer -> [Integer] sieveSundaram = ... To give you some help, below is a function to compute the Cartesian product of two lists. This is similar to zip, but it produces all possible pairs instead of matching up the list elements. For example, cartProd [1,2] ['a','b'] == [(1,'a'), (1,'b'), (2,'a'),...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
In Java Describe an algorithm that given a matrix described below determines whether matrix contains a...
In Java Describe an algorithm that given a matrix described below determines whether matrix contains a value k. The input matrix A[1...n, 1...n] has all rows and columns arranged in an non-descending order A[i, j] < A[i, j+1], A[j, i] < A[j + 1, i] for all 1 < i < n and 1 < j < n
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for...
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for homework first... Thank you!!! Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT