User Tools

Site Tools


ecen_370_assignments

This is an old revision of the document!


ECEn 370 Assignments

(new page for answers or jump to the bottom of this page if using graphs, etc. or do we just want to give .m file with solution?) put watermarked graph to give them an idea of what they should be getting. Give them a follow up question that has them analyze what is going on in the plot. plots are a mean to the end.

Assignments, Matlab topic, HW topic

Assignment Related Course Topic Matlab Topic Book Sections
HW 1 Matlab Tutorial Matlab Basic Operations, Matrix Operations,
Array Operations, Script Files
HW 2 Expected Value Bar Graphs, Subplots, load, sprintf, 1.5, 1.6
%u - variables in function calls
HW 3 Random Variables Graph Function 2.1-2.3
HW 4 Joint PMF, Marginal PMF, Expectation, Baye's Rule Matrix Operations, Plotting commands 2.4-2.6
HW 5 Joint PMF, Expectation If-statement, For-loop 2.7-3.1
HW 6 Uniform RV, PDF, Exponential RV Review 3.1-3.3
HW 7 Marginal PDF, Conditional PDF, Expectation Review 3.4-3.5
HW 8 PDF, CDF, Poisson RV Review 3.6-4.1
HW 9 N/A Polynomial Roots, 4.1-5.2
Real and Imaginary Command
HW 10 Central Limit Theorem Review 5.3-6.1

Homework 1

Do the tutorial found accompanying this homework on the Learning Suite 
Content/Homework Supplemental Material page: (HW1_MATLAB_tutorial.pdf). Spend the time 
to understand what is going on so that way you are able to easily write m-les for future 
assignments. Turn in the required materials indicated at the end of the assignment.

Homework 2

Description
Objective: Learn to read in a file, extract information from it, and show the information 
in a graph

Starter: load(sprintf('sequence%u.mat', k));, put inside a for loop of variable k (k replaces the %u in the name)

Functions to Learn: load(); sprintf(); bar(); 
  
Problem:  You have been hired by Coin Flips 'R' Us to investigate a possible case 
of fraud. The company has gotten sequences of coin flips from 6 different outsourced 
companies. Management suspects that some of the companies aren't actually flipping 
fair coins but are using some other procedure. The sequences (of varying lengths) are
found on the web with the homework as .mat files. Your quest is to determine which 
sequences were generated using fair coin flips and which were generated in some 
other fashion.

Hint: To accomplish the goal, consider the probabilities of getting 1 head, 
then 2 heads in a row, 3 heads in a row, 4 heads in a row, and 5 heads in a row.
Then compute the statistics for each sequence. Develop MATLAB code to compute 
the statistics for each sequence (there is example code on the website to get 
you started).

Please submit your code, the head count statistics for each sequence, and which
sequences you suspect are fraudulent.

hw2_matlab_example.m

coin_flip_data.zip

Solution

Homework 3

Description
Objective: Understand the Random variables offered in Matlab and how to plot them.

Starter: x_vector = random('Binomial', n, p, [1, trials]);

Functions to learn: var(), xlabel(), ylabel(), title()

Problem: In this example you will learn about how to simulate various random variables in 
MATLAB. You can generate the discrete random variables that we use in ECEn 370 through
the “random” command. Type “help random” at the MATLAB prompt to read more information about the
“random” command.

For example, you can type “random('Binomial', 10, 0.6, 10, 1)” to simulate a Binomial random
variable with n=10, p=0.6, in a vector of length 10. You can do this for 'Binomial', 'Poisson', 
and 'Geometric' random variables. One way to visualize the probability mass function of the
distribution is shown in the MATLAB example on the website associated with this homework.
Simulate and plot the probability mass functions for the following random variables (turn in your
code and plots):
1. Binomial with parameters n = 20 and p = 0.2 (you will need more bins in your histogram)
2. Geometric random variable with parameter p = 0.1 (choose reasonable number of bins). Note
that they define the geometric as the number of failures after a success, which is why you
can get zero.
3. Poisson random variable with parameter lambda = 3.
Imagine now that you have a binomial random variable, X1, defined by parameters n=4, p=1/4.
Now define the function Y = (X1 - 1)2. What is the probability mass function forY? Simulate and
plot the random variables X1 and Y in MATLAB. Does simulation of the PMF of Y compare well
with the probability mass function you computed analytically?

Imagine now that you have a binomial random variable, X1, defined by parameters n=4, p=1/4.
Now define the function Y = (X1 - 1)2. What is the probability mass function forY? Simulate and
plot the random variables X1 and Y in MATLAB. Does simulation of the PMF of Y compare well
with the probability mass function you computed analytically?

hw3_matlab_example.m

Solution

Homework 4

Description
Objective: Learn to plot a joint PMF

Starter: Refer to HW4_prob_example.m

Functions to Learn: none
  
Problem: Please download all of the files associated with this homework and 
put them into your working directory when you do this problem. The file simulate_joint_pmf.m 
is necessary for hw4_prob_example.m to work correctly.

Read through HW4_prob_example.m and execute it so you can see how a joint PMF can
be described in terms of matrices, simulated, and visualized.
 
You have been employed by McTacoKing to do an analysis of their customer purchasing habits.
They have given you data on 10,000 normal customers: X is the number of burgers each ordered,
and Y is the number of servings of fries. All normal customers order at least one burger and at
least one serving of fries. For all normal customers, the maximum number of burgers is six and
the maximum number of fries is four. This data is given to you as a burgerfry.mat le that you
must load which contains the variable outcomes which has the customer data.
 
a) First, plot the estimated joint PMF of the burger/fry data as a 3-dimensional X, Y graph as
shown in the example code. Turn in your plot and the corresponding matrix. This will serve as
the actual PMF for the rest of the problem.

b) What is the probability that a normal customer will buy three burgers and two servings of
fries?

c) What is the marginal PMF for the number of burgers a normal customer will buy? Plot this
and turn it in.

d) What is the marginal PMF for the number of fries servings a normal customer will buy? Plot
this and turn it in.

e) What is the expected number of burgers that a normal customer will buy?

f) What is the expected number of fries servings that a normal customer will buy?

g) If burgers cost $2.00 and fries servings cost $1.00, what is the expected amount of money that
you will obtain from each normal customer?

h) If a normal customer buys two fries servings, what is the PMF of the number of burgers that
he will buy? Plot this and turn it in.

simulate_joint_pmf.m

hw4_prob_example.m

burgerfry.zip

Solution

Homework 5

Description
Objective: Simulate Joint Random Variables

Starter: P = zeros(2,2);

Functions to Learn: none
  
Problem: There are many different ways to simulate joint random variables. We will explore two 
approaches in this problem. Consider that you have the following joint PMF, pXY(x, y):
                              pXY(0, 0) = 1/8
                              pXY(0, 1) = 1/4
                              pXY(1, 0) = 1/8
                              pXY(1, 1) = 1/2
One way to simulate this is to generate a decent sized vector of random variables.
Since these variables are between 0 and 1, break up the unit interval as follows (to visualize 
this, draw a line and label the end points 0 and 1): For those random numbers obtained between 
0 and 1/8, you assign x = 0, y = 0 (table coordinate point (0,0),note you need two vectors x 
and y); for values between 1/8 and 2/8, you choose (1,0), for values between 2/8 and 4/8 you 
choose (0,1), and for values between 4/8 and 1, you choose (1,1). Code this as a big 'if' 
statement. Pay attention to what is going on here. Note from the joint PMF that (0,0) has a 
probability of 1/8 and so does (1,0), while (1,1) has a probability of a whole half (the second 
half in our unit interval). The script “simulate_joint_PMF.m” from HW4 does this for a much 
more general case.

a) Using this approach, write your own MATLAB script to simulate the joint PMF above. Use 
10,000 trials. Now check if your probability of getting a point (x,y) matches those in the 
table. For example, check the times that both x and y equal zero and divide over your total 
number of trials. Turn in your script and simulation results.

Another approach to this problem is to use conditioning and the fact that 
pXY(x, y) = pY | X(y | x)pX(x).

First, determine the marginal PMF, pX(x). Then determine the conditional PMF, pY | X(y | x).
To simulate you first determine an x by splitting up the uniform random variable. Then given 
that x, you
determine a y by simulating the conditional PMF. Draw another unit line. What portion of 
it corresponds to the marginal PMF of x = 0? What does the conditional PMF, pY | X(y | 0), 
look like?

b) Write a MATLAB script that performs this second approach. Simulate it for 10,000 trials 
and make sure that it matches part (a) and the original joint PMF. Turn in your script and 
simulation results.

c) Calculate the expectation of X analytically. Then take the average of the results from 
your marginal PMF that you simulated above. Does the expectation of X match the average of 
the results from your marginal PMF?

d) Calculate the variance of X analytically. Then calculate the variance of the results 
from your marginal PMF that you simulated above. Does the analytical variance of X match 
the simulated variance?
Solution

Homework 6

Description
Objective: To simulate a 2D uniform random variable and obtain its PDF

Starter: Refer to continuous_sim_commented.m

Functions to Learn: viscircle() may be helpful but not necessary

Bertsekas Chapter 3, Problem 7 
  One way to do this is to simulate a uniform distribution over a circle of radius 1 and then 
  compute the distance to each point:
  a) Define a circle with radius 1 centered at (0, 0);
  b) Generate a pair of uniform random variables on the interval [-1, 1], which you can use to 
     describe a uniform distribution over a square defined by (-1, -1), (-1, 1), (1, 1), and 
     (-1, 1);
  c) Only accept points that fall in the circle, which is equivalent to making sure that all the 
  darts hit within the target;
  d) Compute the distance from the center of the target to each point. This is the value of your 
     random variable. Do this simulation for 10,000 points and see if the distribution matches 
     the PDF, the mean, and the variance that you calculated in part (a). Turn in your code, 
     plots, and calculations!

Bertsekas Chapter 3, Problem 10 
  Implement part (b) in Matlab (look at the solution in the book) to show how you can use this to 
  generate the exponential random variable. Simulate this for 10,000 points and turn in your 
  histogram plot. It should look like the histogram for an exponential random variable!

continuous_sim_commented.m

Solution

Homework 7

Description
Objective: To simulate a 2D uniform random variable and obtain its marginal/conditional PDF

Starters: none

Functions to Learn: line()

Chapter 3, Problem 23 
  From the previous homework assignment, you should be able to define a triangle and then create 
  a uniform distribution within that triangle.
   a) Turn in a plot of your triangle with your vertices at (0,0), (0,1), and (1,0).
   b) Plot an estimate of the marginal PDF of Y (essentially you can just examine the Ys). Show 
      that this is the same as determined analytically.
   c) Plot an estimate of the conditional PDF of X given Y = ½. (To do this, you can select points
      that are +/- some small distance from Y = ½).
   d) Compute E[X] from simulation.
   
Chapter 3, Problem 24 
  Plot the triangle as in the previous problem.
   a) Turn in your plot of the triangle.
   b) Find E[X] from simulation.
   c) Find E[Y] from simulation.
Solution

Homework 8

Description
Objective: To calculate sums and differences of R.V.s   Plot the CDF

Starter: random('type', mean, rows, columns);

Functions to Learn: random()

This will show you how to simulate derived distributions. The file hw8.m on the website will show 
you an example of appropriate graphs. Use 10,000 points for each of these problems. Turn in your 
code, your graphs, and your analysis for each of the following sections.

Chapter 4 Problem 1 (do this for both |X|½ . and –ln|X|)
   a) Plot the PDF of X. This is a histogram of the vector X.
   b) Plot the histograms of the derived distributions for the two cases in the problem.
   c) Compare your graphs with the analytical derivation done above. Do they agree?

Chapter 4 Problem 5
   a) Plot the field of generated X and Y (you've done this many times).
   b) Compute a vector Z which is |X − Y|.
   c) Find the CDF of Z by using a for loop for z from -1 to 3 and increment to finding all of 
      the values less than a particular z.
   d) Plot the CDF of Z that you found above.
   e) Plot the PDF of Z. This is a simple histogram of the vector Z.
   f) Compare your plots with the analytic solutions derived above.

Chapter 4 Problem 8
   a) Plot the field of generated X and Y using the parameter λ = 1. This should look skewed 
      because they are not uniform.
   b) Compute a vector Z which is X + Y.
   c) Plot the histogram of Z.
   d) Compare your plot with the analytic solution you derived above

hw8.m

Solution

Homework 9

Description
Objective: To find correlation between statistical data and understand what correlation means

Starter: rho = corr(X, Y); 

Functions to Learn: corr()

I have taken the following from the CMU DASL statistics website:
http://lib.stat.cmu.edu/DASL/Data_les/carmpgdat.html

Use the following code to import the data using MATLAB: “A=importdata('car_data_1991_for_HW.txt')”
to store the data in a variable A. The headings of the columns are given by “A.textdata(1,2:6)” 
where VOL is the cubic feet of cab space, HP is engine horsepower, MPG is average miles per gallon, 
SP is top speed, and WT is vehicle weight in 100 lb increments. The numerical information is found 
in “A.data”. Use the “corr” command to find the correlation between two columns of data.
   a) Plot weight of the car versus MPG. Are these highly correlated? Are these positively or 
      negatively correlated? Why do you think that is the case?
   b) It turns out that the linear least-mean-square estimator of Y (also called linear regression) 
      can be found by (see page 438 for more information if you wish):
      Yˆ = E[Y ]+ ρ
      σ Y
      σ X
      (X − E[X])
      What is the line that best fits the data found in part (a)? Plot this line on your graph 
      in part (a).
   c) Plot the cab space of the car versus engine horsepower. Plot the best fit line for the data. 
      Are these highly correlated? Are these positively or negatively correlated? Why do you think
      that is the case?
   d) Suppose that ρ = 0 . What kind of estimator does that give you for Y ? Why is that reasonable?


Sum of a Binomial Number of Independent Uniform Random Variables:
Suppose you live in a small town of a hundred people and you own a small store. Every normal day 
(excluding Sundays of course) each person in the town has the probability p = 0.25 of visiting your 
store. A person that visits your store will spend money distributed uniformly from 0 to 50 dollars. 
The amount of money spent in your store on any normal day is Y.
   a) Analytically compute the following: mean of Y, standard deviation of Y (consider page 242 in 
      the book).
   b) Simulate this problem in the following fashion (turn in your code): Do the following loop 
      10,000 times
      - Generate the number of people, n , that visit your store every day by a Binomial random variable.
      - Sum together n random variables with uniform distributions from 0 to 50.
      - Store the sum of money - this is one outcome of the random variable Y. After the loop is complete, 
        then compute the mean and standard deviation of the outcomes for Y.
   c) Plot a histogram representing the PDF of Y .
   d) Do the simulated values you computed in part (b) match the values in part (a)?
Solution

Homework 10

Description
Objective: To calculate the sum of R.V. and prove that their output is Gaussian

Starter: Refer to page 147 in the book

Functions to Learn: 

Viewing the Central Limit Theorem:
Perform the following for N variables = 1,2,3,5,10,100.
Let Xi  be an exponential random variable with parameter "lambda" = 1.
Let Y = X1 + . . . + XN variables.  For each different case of N variables,
   a) Find the mean and standard deviation of Y.
   b) Simulate Y by generating 10,000 points of Y.  Plot the estimate of the PDF by dividing the 
      raw histogram by both the total number of points (estimate of probability per interval) and
      by the length of each interval (estimate of probability density per interval).
   c) On the same graph as the histogram (use the “hold on” and “hold off” commands), plot a
      Gaussian (normal) random variable with the mean and standard deviation for Y.
   d) Compare the plots obtained in part b) and part c).
   e) Do the means and standard deviations agree with what you found in part a)?
Solution
ecen_370_assignments.1435250632.txt.gz · Last modified: 2015/06/25 10:43 by perry