Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a file named montecarlo.py in which you will write your code. Write the following functions: Part 1. Estimating a It turns out that we

image text in transcribed

image text in transcribed

image text in transcribed

Create a file named montecarlo.py in which you will write your code. Write the following functions: Part 1. Estimating a It turns out that we can estimate the value of n using random numbers. Consider the following image: r = 1 The idea is that we know the area of a circle is given by the expression A = 1 r2. The circle in the image has radius 1, so the circle's area is just . One quarter of the circle's area is then 1/4. If we choose enough random points in the square, then the fraction of them that fall inside the circle gives us /4, which we can then multiply by 4 to estimate . Here is the pseudocode for the idea: Do N times: Generate two random numbers, X and y, between 0 and 1. If the corresonding point falls within a circle of radius 1, then count it as a hit. Multiply the count of hits by 4, and divide by N. Write a function pi_est(n) that will run this algorithm for Niterations and return the estimated value of . Note: For a different explanation of this problem, check out problem 5.1.6 in the textbook. For Above & Beyond credit. write a function that uses turtle graphics to generate a picture similar to the one above. Part 2. Estimating sequence probabilities Part 2. Estimating sequence probabilities Sometimes writing simulations can be helpful in finding answers to complex probability questions. For example, a question such as "how many times, on average, do I need to flip a coin to get 6 heads in a row?" is quite difficult to calculate. But we can simulate this situation. 1. Write a function flipo that simulates a fair coin flip. Half the time, it should return for heads. Half the time, it should return for tails. 2. Write a function six_heads that uses flipo to simulate flipping a coin until six heads in a row are seen. Then return the total number of flips. Your code should call the flipo function you just wrote. 3. Write a function average_six(n) that will run your six_heads function n times and calculate the average of the results, and return that. Part 3. Simulating strange behavior 1. Write a function hnho that will flip a coin until it gets three heads in a row. Return the total number of flips (including the last three). 2. Write another function hhto that will flip a coin until it gets heads followed by heads followed by tails. Again, return the total number of flips (including the last three). (Note that you might see a long string of heads followed by a tails; that would count.) 3. Write a third function simulate_flips(n) that will call each of the previous functions n times and calculate the average number of coin flips required for each situation. Have this function print out the results. Note: You should find that the average numbers of flips differs for these two situations. Does that make sense? Drawing a finite state machine (p. 27) may help you understand what is happening. Submitting your work Document your functions and organize your code per section 3.4 of the textbook. I expect that you will write a main function to test the functions specified above. Before submitting, however, please delete your call to maino. Submit one file, montecarlo.py, to this assignment on Canvas. Point distribution Style and following instructions - 2pt Wildl is happening. Submitting your work Document your functions and organize your code per section 3.4 of the textbook. I expect that you will write a main function to test the functions specified above. Before submitting, however, please delete your call to main() Submit one file, montecarlo.py, to this assignment on Canvas. Point distribution Style and following instructions - 2 pt pi_est - 4 pts six_heads - 4 pts hht - 4 pts Additional functions as specified - 4 pts

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions