0% found this document useful (0 votes)
11 views

HW 2

The document describes a Monte Carlo simulation to estimate probabilities. The simulation involves: 1) Generating random grades between 50-100 for multiple assignments and experiments. 2) Sorting and averaging grades after dropping the lowest scores. 3) Tallying average scores from experiments to estimate probabilities of different grades. 4) Plotting results and comparing shapes as number of experiments increases.

Uploaded by

Krage and John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

HW 2

The document describes a Monte Carlo simulation to estimate probabilities. The simulation involves: 1) Generating random grades between 50-100 for multiple assignments and experiments. 2) Sorting and averaging grades after dropping the lowest scores. 3) Tallying average scores from experiments to estimate probabilities of different grades. 4) Plotting results and comparing shapes as number of experiments increases.

Uploaded by

Krage and John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EA1 Homework Program 2: Monte Carlo Simulation

Due Thursday, Oct. 7, 2021, at 6:00am

In this homework you will write a MATLAB program to compute the probability of a random event.
Since probability measures the chance of a particular outcome of an experiment, sometimes it is easy to
perform some experiments directly to calculate the probability of an event. For example, the probability
of a fair (or evenly-balanced) coin landing on Heads is 1/2, and the probability of it landing on Tails
is 1/2. Likewise, when rolling an evenly-weighted die, the probability of getting any particular value is
1/6, since there are six sides.
If you were not sure of the probability of either of these events, you could perform some experiments.
For example, you could toss a coin 100 times, and record how many times it lands on a particular side.
Dividing that value by the number of tosses would give a value somewhere near 1/2. The more tosses
you perform, the more accurate your estimate should be. Likewise with a die: you can roll a die 100
times and record how many times it lands on a particular value. Dividing by the number of rolls would
give a probability near 1/6 for each value. The more rolls you perform, the closer you expect to get to
1/6. This type of simulation to estimate a probability is called “Monte Carlo Simulation”.
Some problems are not so straightforward. For example, suppose we want to estimate the probability
of getting a certain grade given that we drop a number of the lowest grades in the class. Specifically,
suppose that every assignment is weighted equally, and as a simplifying assumption we will assume that
the grades are uniformly distributed between 50% and 100% (i.e. there is an equal chance of getting
any grade between 50% and 100%). To figure this out, we will run many experiments and take averages.
Suppose that we have 10 assignments and run 1000 experiments, assigning a random grade between 50
and 100 for each one. Then, we drop the d lowest grades for each experiment, and see what the average
grade turns out to be. After this, we count up how many times each final grade occurs. Then, we can
divide these tallies by the number of experiments, and we should have our probabilities. Given that we
perform enough experiments, we should get a good estimate of the probability to get any given grade.
In this assignment, we will estimate exactly the probabilities described above, for every possible
grade. Specifically, we will run n tests, there will be a assignments, and we will drop the d lowest
grades. Given that the grades are anywhere between 50 and 100, the possible averages will also lie
between 50 and 100. See Appendix A for an example of each of the important steps.

Using the MATLAB editor, create a script M-file which performs the following steps:

1. Ask the user to input a value of a, and store the result in a variable called a.

2. Ask the user to input a value of d, and store the result in a variable called d.

3. Ask the user to input a value of n, and store the result in a variable called n.

4. We want to create a matrix A whose entries denote individual assignment grades. Specifically,
each column represents a particular assignment (a columns), and each row represents one full
experiment (n rows). All of the entries must be random numbers between 50 and 100, since these
are the possible choices for a grade in our simulation. Use the function rand() to create the
matrix A. Note: rand(x,y) creates an x×y matrix with random values between 0 and 1.

5. Create a matrix B whose rows are the same as A, except that each row is sorted in ascending order.
Read the documentation for the function sort to complete this step.

6. Find the average grade for each row after dropping the d lowest scores. Read the documentation
for the function sum to complete this step, and use matrices A and B. Store the result in a vector
called avg scores. Remember, the scores in B are sorted, with the lowest grades at the left and
the highest at the right.

7. You now have a vector, avg scores, that contains the outcomes of all your experiments. Now it
is time to tally them up. Use the command
[tally, scores] = hist(avg scores, 50:100)
to return the number of times each value in the second argument (50:100) appears in the first
argument (avg scores), along with the possible values of the scores (stored in tally and scores,
respectively). In reality, we are not counting only the number of times that integer average scores
occur, since most will not be integers. Instead, the second argument to hist provides the bin
centers, with all scores falling in a specific bin. For example, if the bin center is 61, then all scores
between 60.5 and 61.5 will be counted toward that count (provided its immediate neighbor centers
are at 60 and 62). For an example, please see Appendix C. Note that MATLAB will advise that
you use histogram instead of hist. For this assignment, you can ignore this suggestion.

8. To make the results look nicer, include the following code. In a comment at the end of your
script, briefly explain what these lines of code do.
min ind = find(tally>0, 1, 'first');
max ind = find(tally>0, 1, 'last');
tally = tally(min ind:max ind);
scores = scores(min ind:max ind);

9. To get a probability from the tally count, we must divide by the total number of trials. Create a
variable probs, which stores tally divided by the number of trials, n.

10. Finally, we have to plot our results. Use the bar() function to create a bar plot of your results.
Read the documentation! Make sure to label your axes. To create a nice title, use the command:
title(sprintf('%i Assignments, %i Drops, %i Trials', a, d, n))
An example of the output is shown in Figure 1.

11. As a last step, create a two-column matrix called results, where the first column contains the
values in scores, and the second column contains the values in probs.

12. Run your script with input values a=10, d=2, and n=1e6. Copy and paste your results matrix
into a comment at the end of your M-file. An example of the results matrix is shown in Figure
2. Hint: to make the results matrix look nicer when you display it, you can use the command
format long g.

13. Repeat for a=10, d=5, and n=1e6. Once again, copy and paste your results matrix into a
comment at the end of your M-file.

14. Try the experiment with increasing numbers of trials, e.g. 10, 100, 1000, 10000. In a comment
at the end of your code, please describe qualitatively what you notice about the shape of the
histogram as the number of trials increases.

Example matrices are shown in Appendix A, and a script template is shown in Appendix B.
Figure 1 Figure 2
Appendix A: Examples
Below are examples of the important steps, using a = 5, d=1, and n = 10.

     
55 58 52 69 70 52 55 58 69 70 63

 61 57 99 97 89 


 57 61 89 97 99 


 86.5 


 81 86 72 53 78 


 53 72 78 81 86 


 79.25 


 73 98 54 70 76 


 54 70 73 76 98 


 79.25 

 96 72 65 92 83   65 72 83 92 96   85.75 
A=  B=  avg scores =  

 57 57 83 50 79 


 50 57 57 79 83 


 69 


 89 55 81 98 85 


 55 81 85 89 98 


 88.25 


 73 98 56 81 62 


 56 62 73 81 98 


 78.5 

 66 80 59 56 64   56 59 64 66 80   67.25 
61 88 78 88 73 61 73 78 88 88 81.75
       
1 63 0.1 63 0.1

 0 


 64 


 0 


 64 0 


 0 


 65 


 0 


 65 0 


 0 


 66 


 0 


 66 0 


 1 


 67 


 0.1 


 67 0.1 


 0 


 68 


 0 


 68 0 


 1 


 69 


 0.1 


 69 0.1 


 0 


 70 


 0 


 70 0 


 0 


 71 


 0 


 71 0 


 0 


 72 


 0 


 72 0 


 0 


 73 


 0 


 73 0 


 0 


 74 


 0 


 74 0 

 0   75   0   75 0 
tally =   scores =   probs =   results =  

 0 


 76 


 0 


 76 0 


 0 


 77 


 0 


 77 0 


 1 


 78 


 0.1 


 78 0.1 


 2 


 79 


 0.2 


 79 0.2 


 0 


 80 


 0 


 80 0 


 0 


 81 


 0 


 81 0 


 1 


 82 


 0.1 


 82 0.1 


 0 


 83 


 0 


 83 0 


 0 


 84 


 0 


 84 0 


 0 


 85 


 0 


 85 0 


 2 


 86 


 0.2 


 86 0.2 

 0   87   0   87 0 
1 88 0.1 88 0.1
Appendix B: Template of script file
hw2 ilya.m
% Homework Program 2
%
% Name: Mikhelson, Ilya
% Section: 21
% Date: 10/07/2021

% Get inputs:
(insert code here)

% Create matrix A:
(insert code here)

% Create matrix B:
(insert code here)

% Calculate average scores after dropping the appropriate assignments.


% Make sure to adjust the divisor appropriately.
(insert code here)

% Calculate tally and scores:


[tally, scores] = hist(avg scores, 50:100);
min ind = find(tally>0, 1, 'first');
max ind = find(tally>0, 1, 'last');
tally = tally(min ind:max ind);
scores = scores(min ind:max ind);

% Find desired probabilities:


(insert code here)

% Plot bar graph of results:


(insert code here)

% Create a results matrix:


(insert code here)

%
% (insert answers to steps 8 and 14, as well as the copy/pasted output
% from steps 12 and 13, commented out)
%
Appendix C: Example of “hist” function
Suppose you have a vector x, defined below.
 
1

 1 


 2 


 6 

 4 
x= 

 1 


 2 


 5 

 3 
5
If you wanted to count the number of occurrences of all of the numbers, you would use the hist
function in MATLAB. An example is shown below.
tally = hist(x, 1:6)
Here, we are tallying up how many times each of the values in the second input argument (i.e. 1:6)
appears in x. The result would be:
[3 2 1 1 2 1]
which says that the value 1 appears 3 times, the value 2 appears 2 times, the value 3 appears 1 time,
and so on. If the numbers in x are not integers, this would still work. However in this case, the values
in the second input argument would represent the centers of the bins. In other words, it would count
up all the values in x that are from halfway to the preceding value up to halfway to the following value.
In the example above, this would mean that all values from 2.5 to 3.5 would be in the “3” bin, values
from 3.5 to 4.5 would be in the “4” bin, and so on. As an example, consider the vector below:
 
1.2
 1.4 
 
 2.1 
 
 6.7 
 
 4.9 
y= 
 1.1 
 
 2.6 
 
 5.7 
 
 3.8 
5.2
Now, the code
tally = hist(y, 1:6)
results in
[3 1 1 1 2 2]
which says that there are 3 values between −∞ and 1.5, 1 value between 1.5 and 2.5, and so on.
Oftentimes, it is also useful to get the bin centers as an output of the function directly, and this can
be done using a second output argument, so the function looks like this:
[tally, bin centers] = hist(y, 1:6)
Even though this may look strange since we manually specified the bin centers, it is a good habit
because there are other ways to call the hist function where the centers are not so clear, and it is
always useful to know what centers were used.

You might also like