Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Compute a probability. What is the probability of getting a number between \(0.5\) and \(0.6\) when drawing uniformly distributed random numbers from the interval \([0,1)\) ? To answer this question empirically, let a program draw \(N\) such random numbers using Python's standard random module, count how many of them, \(M\), that fall in the interval \((0.5,0.6)\), and compute the probability as \(M / N\). Run the program with the four values \(N=10^{i}\) for \(i=1,2,3,6\). Name of program file: compute_prob.py.

Short Answer

Expert verified
Empirical probability converges to 0.1 as \(N\) increases.

Step by step solution

01

Understanding the Uniform Distribution

A uniform distribution on the interval \([0,1)\) means each number in this interval is equally likely to be selected. Therefore, the theoretical probability of selecting a number between \(0.5\) and \(0.6\) in this distribution is the length of the interval, which is \(0.6 - 0.5 = 0.1\).
02

Generating Random Numbers

To estimate this probability empirically, we will use Python's random module. Using a for loop, we will draw \(N\) random numbers in the interval \([0,1)\) using the function `random.uniform(0, 1)` and store them. This step involves setting up Python to generate these numbers.
03

Counting Numbers in the Interval

For each drawn number, check if it lies within the interval \((0.5,0.6)\). Increment a counter \(M\) for each number found in this range. This process will give us the number of numbers that satisfy our condition.
04

Calculating the Empirical Probability

After iterating through all \(N\) numbers, calculate the probability by dividing \(M\), the number of successful draws, by \(N\). This results in the empirical probability, \(M/N\), for the given \(N\).
05

Running the Program with Different \(N\) Values

Repeat the entire procedure for \(N = 10^1, 10^2, 10^3, 10^6\). Each time, record the values of \(M\) and the computed probability \(M/N\). This step will allow us to see how the estimate improves with larger \(N\).
06

Analyzing the Results

As \(N\) increases, the estimated probability \(M/N\) should converge to the theoretical probability of 0.1. Document your results, noticing the convergence trend in the computed probabilities as \(N\) increases.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Uniform Distribution
In the context of probability, a uniform distribution assigns equal likelihood to events within a specified range. When drawing numbers from the interval \([0,1)\), each value is equally probable. This results in what is called a continuous uniform distribution. For any subinterval within this range, such as \((0.5, 0.6)\), the probability of drawing a number from that subinterval is simply the length of the subinterval, which in this case is calculated as \(0.6 - 0.5 = 0.1\). This simple calculation stems from the uniform nature of the distribution, emphasizing the equal probability of all events within the interval.
Understanding uniform distribution is key to grasping why the theoretical probability aligns with the length of the interval. This knowledge provides the foundation for empirical estimations and simulations.
Empirical Probability
Empirical probability is concerned with approximating probabilities based on observation and data collection. Unlike theoretical probability, it does not rely on calculations from established models, but on actual data resulting from experiments or trials. In our exercise, the aim is to draw \(N\) random numbers and determine how many, \(M\), fall into the specified interval \((0.5, 0.6)\).
The empirical probability is then computed as \(\frac{M}{N}\). This method gives us a practical view of probability by showing how often the desired outcome occurs in practice. Over repeated trials or increased sample sizes, empirical probability tends to converge towards the theoretical probability due to the law of large numbers. This convergence helps validate the consistency and reliability of empirical methods in probability theory.
Python random module
Python's `random` module is a versatile tool for generating pseudo-random numbers. It includes various functions to simulate randomness in Python programs. For tasks such as drawing numbers from a uniform distribution, `random.uniform(a, b)` is utilized to generate numbers in the interval \([a, b)\). In our exercise, using `random.uniform(0, 1)` allows us to obtain numbers uniformly distributed in \([0, 1)\).
This module's ability to efficiently produce random numbers makes it invaluable in simulations where large-scale repetition is necessary, such as trials required for empirical probability calculations. Python's `random` module is easily accessible, intuitive to use, and an essential asset for those diving into probability and statistical simulations.
Random Number Generation
Random number generation is crucial in simulations and modeling when we need to imitate randomness occurring in natural processes or theoretical constructs. It forms the basis for determining empirical probabilities and testing theoretical probability assumptions. While pseudo-random number generators (PRNGs) do not generate true randomness, they produce sequences sufficiently random for most practical applications.
Understanding how to properly utilize random number generation, especially with accessible tools like Python's `random` module, is critical for conducting simulations. These simulated events can be used to empirically confirm theoretical probabilities, explore new models, or solve complex problems not easily accessible analytically. By using large numbers of generated random numbers, one can effectively model and predict the behavior of probabilistic systems.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Muke u cluss fur \(2 D\) nutuluwr wulk. The purpose of this exercise is to reimplement the ualk2D. py program from Chapter \(8.7 .1\) with the aid of classes. Make a class Particle with the coordinates \((x, y)\) and the time step number of a particle as attributes. A method move moves the particle in one of the four directions and updates the \((x, y)\) coordinates. Another class, Particles, holds a list of Particle objects and a plotstep parameter (as in walk2D.py). A method move moves all the particles one step, a method plot can make a plot of all particles, while a method moves performes a loop over time steps and calls move and plot in each step. Equip the Particle and Particles classes with print functionality such that one can print out all particles in a nice way by saying print \(\mathrm{P}\) (for a Particles instance \(\mathrm{p}\) ) or print self (inside a method). Hint: In _str_-, apply the pformat function from the pprint module to the list of particles, and make sure that__repr__ just reuse __str__ in both classes. To verify the implementation, print the first three positions of four particles in the walk2D. py program and compare with the corresponding results produced by the class-based implementation (the seed of the random number generator must of course be fixed identically in the two programs). You can just perform p.move() and print p three times in a verify function to do this verification task. Organize the complete code as a module such that the classes Particle and Particles can be reused in other programs. The test block should call a run(N) method to run the walk for \(N\) steps, where \(N\) is given on the command line. Compare the efficiency of the class version against the vectorized version in ualk2Dv.py, using the techniques of Appendix G.6.1. Name of program file: walk2Dc.py.

Compute with option prices in finance. In this exercise we are going to consider the pricing of so-called Asian options. An Asian option is a financial contract where the owner earns money when certain market conditions are satisfied. The contract is specified by a strike price \(K\) and a maturity time \(T .\) It is written on the average price of the underlying stock, and if this average is bigger than the strike \(K\), the owner of the option will earn the difference. If, on the other hand, the average becomes less, the owner recieves nothing, and the option matures in the value zero. The average is calculated from the last trading price of the stock for each day. From the theory of options in finance, the price of the Asian option will be the expected present value of the payoff. We assume the stock price dynamics given as, $$ S(t+1)=(1+r) S(t)+\sigma S(t) \epsilon(t) $$ where \(r\) is the interest-rate, and \(\sigma\) is the volatility of the stock price. The time \(t\) is supposed to be measured in days, \(t=0,1,2, \ldots\), while \(\epsilon(t)\) are independent identically distributed normal random variables with mean zero and unit standard deviation. To find the option price, we must calculate the expectation $$ p=(1+r)^{-T} \mathrm{E}\left[\max \left(\frac{1}{T} \sum_{t=1}^{T} S(t)-K, 0\right)\right] $$ The price is thus given as the expected discounted payoff. We will use Monte Carlo simulations to estimate the expectation. Typically, \(r\) and \(\sigma\) can be set to \(r=0.0002\) and \(\sigma=0.015 .\) Assume further \(S(0)=100\) a) Make a function that simulates a path of \(S(t)\), that is, the function computes \(S(t)\) for \(t=1, \ldots, T\) for a given \(T\) based on the recursive definition in (8.17). The function should return the path as an array. b) Create a function that finds the average of \(S(t)\) from \(t=1\) to \(t=T\). Make another function that calculates the price of the Asian option based on \(N\) simulated averages. You may choose \(T=100\) days and \(K=102\) c) Plot the price \(p\) as a function of \(N\). You may start with \(N=1000\). d) Plot the error in the price estimation as a function \(N\) (assume that the \(p\) value corresponding to the largest \(N\) value is the "right"price). Try to fit a curve of the form \(c / \sqrt{N}\) for some \(c\) to this error plot. The purpose is to show that the error is reduced as \(1 / \sqrt{N}\). Name of program file: option_price.py. If you wonder where the values for \(r\) and \(\sigma\) come from, you will find the explanation in the following. A reasonable level for the yearly interest- rate is around \(5 \%\), which corresponds to a daily rate \(0.05 / 250=0.0002 .\) The number 250 is chosen because a stock exchange is on average open this amount of days for trading. The value for \(\sigma\) is calculated as the volatility of the stock price, corresponding to the standard deviation of the daily returns of the stock defined as \((S(t+1)-S(t)) / S(t)\). "Normally", the volatility is around \(1.5 \%\) a day. Finally, there are theoretical reasons why we assume that the stock price dynamics is driven by \(r\), meaning that we consider the riskneutral dynamics of the stock price when pricing options. There is an exciting theory explaining the appearance of \(r\) in the dynamics of the stock price. If we want to simulate a stock price dynamics mimicing what we see in the market, \(r\) in Equation \((8.17)\) must be substituted with \(\mu\), the expected return of the stock. Usually, \(\mu\) is higher than \(r \cdot \diamond\)

Probabilities of throwing two dice. Make a computer program for throwing two dice a large number of times. Record the sum of the eyes each time and count how many times each of the possibilities for the sum \((2,3, \ldots, 12)\) appear. A dictionary with the sum as key and count as value is convenient here. Divide the counts by the total number of trials such that you get the frequency of each possible sum. Write out the frequencies and compare them with exact probabilities. (To find the exact probabilities, set up all the \(6 \times 6\) possible outcomes of throwing two dice, and then count how many of them that has a sum \(s\) for \(s=2,3, \ldots, 12\).) Name of program file: freq_2dice.py.

Independent vs. dependent random numbers. Generate a sequence of \(N\) independent random variables with values 0 or 1 and print out this sequence without space between the numbers (i.e., as 001011010110111010\() .\) The next task is to generate random zeros and ones that are dependent. If the last generated number was 0 , the probability of generating a new 0 is \(p\) and a new 1 is \(1-p\). Conversely, if the last generated was 1, the probability of generating a new 1 is \(p\) and a new 0 is \(1-p\). Since the new value depends on the last one, we say the variables are dependent. Implement this algorithm in a function returning an array of \(N\) zeros and ones. Print out this array in the condense format as described above. Choose \(N=80\) and try the probabilities \(p=0.5, p=0.8\) and \(p=0.9\). Can you by visual inspection of the output characterize the differences between sequences of independent and dependent random variables? Name of program file: dependent_random_variables.py.

Flip a coin \(N\) times. Make a program that simulates flipping a coin \(N\) times. Print out "tail" or "head" for each flip and let the program count the number of heads. (Hint: Use \(\mathrm{r}=\) random random () and define head as \(\mathrm{r} \Leftrightarrow 0.5\) or draw an integer among \(\\{1,2\\}\) with \(r=\) random . randint \((1,2)\) and define head when \(r\) is 1.) Name of program file: flip_coin.py.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free