Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Simulation of Definite Integrals One really cool application of random variables is using them to approximate integrals/area under a curve. This method of approximating integrals

Simulation of Definite Integrals

One really cool application of random variables is using them to approximate integrals/area under a curve. This method of approximating integrals is used frequently in computational science to approximate really difficult integrals that we never want to do by hand. In this exercise you'll figure out how we can do this in practice and test your method on a relatively simple integral.

Part A. Consider the following function.

()=14sin()f(x)=14sin(x)

Compute by hand, the integral of ()f(x) for 020x2. Once you have your expression worked out, you may use python to estimate the integral to the nearest 6th decimal place.

Solution:

Part B. Suppose we sample a random variable X from (0,2)U(0,2) and another random variable Y from (0,1)U(0,1) and think of them as x- and y-coordinates of a point in the box [0,2][0,1][0,2][0,1]. If we compute the ratio of points that fall under the curve ()f(x) to the total points sampled, what does this value estimate?

Solution:

Part C. How could we modify the process outlined in Part B so that our estimator estimates the value of the desired integral? If you need inspiration, check out the in-class notebook on Stochastic Simulation (#4), specifically, the exercise where we estimated !

Solution:

Part D. Complete the function integrate below to compute an approximation to the definition integral ub0()0ubf(x)dx. Your function should take as arguments:

  1. num_samples -- the number of points to sample,
  2. ub -- a number between 00 and , and
  3. isPlot -- a Boolean flag for whether or not to display a plot of the results.

Your function should return an approximation of the definite integral by sampling X from (0,ub)U(0,ub) and Y from (0,1)U(0,1). For each random draw (,)(X,Y), define the random variable Z that represents whether or not the sampled Y value is less than ()f(X) for the corresponding X value.

Your function should perform three tasks:

  1. return the estimate of the value of the integral,
  2. return the sample variance of the random variable Z being simulated, using the np.var function, and
  3. if isPlot is True, plot the curve ()f(x) as well as the location of the points (,)(X,Y), colored according to whether they fall above or below the curve.

Part E. Write your own small unit tests!

First, what should your integrate function output if you plug in ub=0? Set that as int_0_to_0 below.

Second, what should your integrate function output if you plug in ub=pi? Set that as int_0_to_pi below.

Then, you can execute the cell below to run two sanity checks. Write out in math (like, a definite integral) what each of these two tests is checking and why that is a reasonable sanity check.

If these two assertions run without fail, then things seem to be working okay. Note that the upper endpoint test is stochastic, so if it fails, try running it a few times. If it fails a lot, then things are probably going wrong.

In[5]:

# Part E solution here

Part F. Run your simulation with isPlot=True and ub=2 for 1,000 samples so we can see your plot. Then, run your simulation (with isPlot=False) with at least 10,000 samples and estimate the value of the given integral. Be sure to leave your cells evaluated for the graders! Does your estimate agree with the exact value of the integral you computed in Part A?

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

Bicomplex Holomorphic Functions The Algebra, Geometry And Analysis Of Bicomplex Numbers

Authors: M Elena Luna Elizarrarás, Michael Shapiro, Daniele C Struppa, Adrian Vajiac

1st Edition

3319248685, 9783319248684

More Books

Students also viewed these Mathematics questions

Question

Discuss global compensation practices.

Answered: 1 week ago

Question

Summarize global staffing practices.

Answered: 1 week ago

Question

Discuss the evolution of global business.

Answered: 1 week ago