Grades on this exam: 91, 90, 76, 75, 72, 58, 54, 54 October 18, 2011 Page 1 of 5 PubH 7460 - Fall 2011 - Exam 1 Name:___________________________________ ================================================================================= 1. You have 25 coins in your pocket. The distribution of coins is: 1 cent: 9 5 cent: 3 10 cent: 6 25 cent: 7 You reach into your pocket without looking and take out 7 coins. Write a program which estimates the probability that you have enough money in your hand to buy a cup of coffee which costs 80 cents. [20 points] options linesize = 80 ; footnote "~john-c/5421/coins.sas &sysdate &systime" ; data coins ; do i = 1 to 1000 ; total = 25 ; n1 = 9; n5 = 3; n10 = 6; n25 = 7 ; sum = 0 ; do j = 1 to 7 ; r = ranuni(-1) ; if r < n1/total then do ; sum = sum + 1 ; n1 = n1 - 1 ; total = total - 1 ; goto end7 ; end ; if n1/total < r < (n1 + n5)/total then do; sum = sum + 5 ; n5 = n5 - 1 ; total = total - 1 ; goto end7 ; end ; if (n1 + n5)/total < r < (n1 + n5 + n10)/total then do; sum = sum + 10 ; n10 = n10 - 1 ; total = total - 1 ; goto end7 ; end ; if (n1 + n5 + n10)/total < r then do ; sum = sum + 25 ; n25 = n25 - 1 ; total = total - 1 ; goto end7 ; end ; end7: end ; output ; end ; run ; proc freq data = coins ; tables sum ; run ; The SAS System 1 12:51 Sunday, October 23, 2011 The FREQ Procedure Cumulative Cumulative sum Frequency Percent Frequency Percent -------------------------------------------------------- 11 1 0.10 1 0.10 15 1 0.10 2 0.20 16 2 0.20 4 0.40 19 1 0.10 5 0.50 20 3 0.30 8 0.80 24 4 0.40 12 1.20 25 5 0.50 17 1.70 28 4 0.40 21 2.10 29 15 1.50 36 3.60 31 1 0.10 37 3.70 33 7 0.70 44 4.40 34 7 0.70 51 5.10 35 8 0.80 59 5.90 37 2 0.20 61 6.10 38 13 1.30 74 7.40 39 5 0.50 79 7.90 40 17 1.70 96 9.60 42 6 0.60 102 10.20 43 5 0.50 107 10.70 44 27 2.70 134 13.40 47 3 0.30 137 13.70 48 20 2.00 157 15.70 49 29 2.90 186 18.60 52 1 0.10 187 18.70 53 46 4.60 233 23.30 55 5 0.50 238 23.80 57 27 2.70 265 26.50 58 26 2.60 291 29.10 59 10 1.00 301 30.10 61 3 0.30 304 30.40 62 38 3.80 342 34.20 63 5 0.50 347 34.70 64 27 2.70 374 37.40 66 5 0.50 379 37.90 67 8 0.80 387 38.70 68 61 6.10 448 44.80 71 7 0.70 455 45.50 72 32 3.20 487 48.70 73 47 4.70 534 53.40 76 4 0.40 538 53.80 77 80 8.00 618 61.80 79 13 1.30 631 63.10 *********** 81 23 2.30 654 65.40 ********* 82 25 2.50 679 67.90 83 16 1.60 695 69.50 85 1 0.10 696 69.60 86 31 3.10 727 72.70 87 9 0.90 736 73.60 88 40 4.00 776 77.60 90 2 0.20 778 77.80 ~john-c/5421/coins.sas 23OCT11 12:51 The SAS System 2 12:51 Sunday, October 23, 2011 The FREQ Procedure Cumulative Cumulative sum Frequency Percent Frequency Percent -------------------------------------------------------- 91 7 0.70 785 78.50 92 44 4.40 829 82.90 96 9 0.90 838 83.80 97 47 4.70 885 88.50 101 29 2.90 914 91.40 103 3 0.30 917 91.70 105 2 0.20 919 91.90 106 12 1.20 931 93.10 107 7 0.70 938 93.80 110 4 0.40 942 94.20 111 1 0.10 943 94.30 112 16 1.60 959 95.90 115 1 0.10 960 96.00 116 10 1.00 970 97.00 120 1 0.10 971 97.10 121 12 1.20 983 98.30 125 5 0.50 988 98.80 127 2 0.20 990 99.00 130 1 0.10 991 99.10 131 2 0.20 993 99.30 136 5 0.50 998 99.80 140 1 0.10 999 99.90 145 1 0.10 1000 100.00 ~john-c/5421/coins.sas 23OCT11 12:51 October 18, 2011 Page 2 of 5 PubH 7460 - Fall 2011 - Exam 1 Name:___________________________________ ================================================================================= 2. You are on a 9 by 9 grid (like a checkerboard). The squares have horizontal and vertical coordinates, both ranging from 1 to 9. You are located at the square with coordinates (5, 5). You can take a step in any of the 4 directions: up, down, left or right. Your steps are random - that is, you are equally likely to step in each of the 4 directions. a) Write a program which estimates the probability that you will step off the grid within 20 random steps. See part (b) before you write this program. b) The Euclidean distance that you traveled from where you started is d = sqrt((x - 5)^2 + (y - 5)^2) where (x, y) are the coordinates of the square where you end up. In your simulation program, include an estimate of the mean and standard deviation of distance d after 20 random steps (for this part, assume the grid goes on forever in all directions, i.e., no boundaries). [20 pts] options linesize = 80 ; footnote "~john-c/5421/brown.sas &sysdate &systime" ; data grid ; n = 1000 ; sumdist = 0 ; sumdist2 = 0 ; sumout = 0 ; do i = 1 to n ; x = 5 ; y = 5 ; out = 0 ; do j = 1 to 20 ; r = ranuni(-1) ; if r < .25 then x = x - 1 ; if .25 < r < .50 then x = x + 1 ; if .50 < 5 < .75 then y = y - 1 ; if .75 < r then y = y + 1 ; dist = sqrt((x - 5)**2 + (y - 5)**2) ; if (x < 1 or x > 9 or y < 1 or y > 9) then out = 1 ; end ; sumdist = sumdist + dist ; sumdist2 = sumdist2 + dist * dist ; sumout = sumout + out ; output ; end ; meandist = sumdist / n ; vardist = (sumdist2 - sumdist * sumdist / n) / (n - 1) ; stddevdist = sqrt(vardist) ; outpercent = sumout / n ; output ; run ; proc print data = grid ; var x y dist out outpercent meandist stddevdist ; run ; The SAS System 1 13:14 Sunday, October 23, 2011 Obs x y dist out outpercent meandist stddevdist 1 4 12 7.0711 1 . . . 2 3 12 7.2801 1 . . . 3 0 11 7.8102 1 . . . 4 5 10 5.0000 1 . . . 5 3 9 4.4721 0 . . . 6 8 12 7.6158 1 . . . 7 3 10 5.3852 1 . . . 8 2 11 6.7082 1 . . . 9 5 9 4.0000 0 . . . 10 4 9 4.1231 0 . . . ....................................................................... 997 4 9 4.1231 0 . . . 998 6 9 4.1231 0 . . . 999 4 10 5.0990 1 . . . 1000 13 9 8.9443 1 . . . 1001 13 9 8.9443 1 0.688 5.95718 1.94236 ~john-c/5421/brown.sas 23OCT11 13:14 October 18, 2011 Page 2 of 5 PubH 7460 - Fall 2011 - Exam 1 Name:___________________________________ ================================================================================= 3. You are given a file of data. Each observation includes a person's height and weight. There may be some missing values. Write a SAS program which does not use any SAS procedures and which calculates the minimum and the maxiumum of the the body mass index in one pass through the file. Body mass index is defined as BMI = weight / height^2, where you can assume weight is in kilograms and height is in meters. [20 pts] options linesize = 80 ; footnote "~john-c/5421/bmi.sas &sysdate &systime" ; data weights_heights ; retain minbmi 9999 maxbmi 0 ; input weight height ; heightm = height / 100 ; bmi = weight / heightm**2 ; if bmi ne . and bmi < minbmi then minbmi = bmi ; if bmi ne . and bmi > maxbmi then maxbmi = bmi ; cards ; 83 171 75 140 66 190 91 133 . 140 . , 67 122 73 . 76 145 78 156 90 180 . . 79 145 88 201 . 203 56 109 65 144 67 190 82 150 ; run ; proc print data = weights_heights ; var weight heightm bmi minbmi maxbmi ; title1 'The last line of this output contains the min and max bmi.' ; run ; The last line of this output contains the min and max bmi. 1 13:26 Sunday, October 23, 2011 Obs weight heightm bmi minbmi maxbmi 1 83 1.71 28.3848 28.3848 28.3848 2 75 1.40 38.2653 28.3848 38.2653 3 66 1.90 18.2825 18.2825 38.2653 4 91 1.33 51.4444 18.2825 51.4444 5 . 1.40 . 18.2825 51.4444 6 . . . 18.2825 51.4444 7 67 1.22 45.0148 18.2825 51.4444 8 73 . . 18.2825 51.4444 9 76 1.45 36.1474 18.2825 51.4444 10 78 1.56 32.0513 18.2825 51.4444 11 90 1.80 27.7778 18.2825 51.4444 12 . . . 18.2825 51.4444 13 79 1.45 37.5743 18.2825 51.4444 14 88 2.01 21.7816 18.2825 51.4444 15 . 2.03 . 18.2825 51.4444 16 56 1.09 47.1341 18.2825 51.4444 17 65 1.44 31.3465 18.2825 51.4444 18 67 1.90 18.5596 18.2825 51.4444 19 82 1.50 36.4444 18.2825 51.4444 ~john-c/5421/bmi.sas 23OCT11 13:26 October 18, 2011 Page 2 of 5 PubH 7460 - Fall 2011 - Exam 1 Name:___________________________________ ================================================================================= 4. A linear transformation U : R^2 ---> R^2 is defined as the composition of two other transformations of R^2, S and T, i.e., U = S(T). T is the linear transformation which takes (1, 0) to (1, 1) and takes (0, 1) to (0, 1). a) Draw a picture of what T does to the unit square. Parallelogram, 2 sides parallel to y-axis, lower right corner at (1, 1), upper left corner at (0, 1). [5 pts] The linear transformation S is rotation counterclockwise by 30 degrees. b) Draw a picture what what U = S(T) does to the unit square. Tilts the parallelogram to the left by 30 degrees. [5 pts] c) Find the 2 x 2 matries corresponding to T, S, and U. | 1 0 | | cos(30) -sin(30) | T = | | S = | | | 1 1 | | sin(30) cos(30) | [10 pts] | cos(30)-sin(30) -sin(30) | S * T = | | | sin(30)+cos(30) cos(30) |. October 18, 2011 Page 2 of 5 PubH 7460 - Fall 2011 - Exam 1 Name:___________________________________ ================================================================================= 5. X and Y are independent are both have a standard normal distribution. Z = max(X, Y). Write a simulation program which estimates var(Z) and corr(X, Z). [20 pts] options linesize = 80 ; footnote "~john-c/5421/maxxy.sas &sysdate &systime" ; data xyz ; n = 100000 ; sumx = 0 ; sumz = 0 ; sumxz = 0 ; sumxx = 0 ; sumzz = 0 ; do i = 1 to n ; x = rannor(-1) ; y = rannor(-1) ; z = max(x, y) ; sumx = sumx + x ; sumxx = sumxx + x*x ; sumz = sumz + z ; sumzz = sumzz + z*z ; sumxz = sumxz + x*z ; end ; varx = (sumxx - sumx * sumx / n)/(n - 1) ; varz = (sumzz - sumz * sumz / n)/(n - 1) ; stddevx = sqrt(varx) ; stddevz = sqrt(varz) ; covarxz = (sumxz - sumx * sumz / n) / n ; meanx = sumx / n ; meanz = sumz / n ; corrxz = covarxz / sqrt(varx * varz) ; output ; run ; proc print data = xyz ; var meanx varx stddevx meanz varz stddevz covarxz corrxz ; title1 'Summary Statistics ...' ; title2 'Theory problem: what is E(z)?' ; run ; Summary Statistics ... 1 Theory problem: what is E(z)? 13:45 Sunday, October 23, 2011 Obs meanx varx stddevx meanz varz stddevz covarxz corrxz 1 .000446964 0.99973 0.99987 0.56313 0.68407 0.82709 0.50264 0.60780 ~john-c/5421/maxxy.sas 23OCT11 13:45