Facebook
From Whipped Duck, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 75
  1. Using this function, I created the graph showing all 4 PDF functions.
  2. function[] = mygraph1(Nmax)
  3.  
  4. % Variables
  5.  
  6. xvals = linspace(0,3,Nmax);
  7. alpha = [0.5,1,1.5,2];
  8. it = 0;
  9. hold on
  10.  
  11. %iteration
  12.  
  13. while it < 4
  14.     it = it + 1;
  15.     a = alpha(it);
  16.     pdfx = a.*exp(a+xvals-a.*exp(xvals));
  17.     plot(xvals,pdfx)
  18. end
  19.  
  20. hold off
  21.  
  22. end
  23.  
  24.  
  25.  
  26.  
  27.  
  28. This gave the following graph:
  29.  
  30. Figure 3 PDF plot of question 2)b)i)
  31. ii)
  32. I made a function that takes a value for alpha and the number of datapoints as an input and then outputs a CDF graph. Using this code and a command line I plotted the 4 CDFs
  33. Here is the function:
  34. function[] = mygraph2(a,Nmax)
  35.  
  36. % Variables
  37.  
  38. xvals = linspace(0,3,Nmax);
  39. it = 0;
  40. cdfxvals = zeros(1,Nmax);
  41.  
  42. % Iteration
  43.  
  44. while it < Nmax
  45.     it = it + 1;
  46.     xval = xvals(it);
  47.     cdfx = integral(@(xval) a.*exp(a+xval-a.*exp(xval)),0,xval);
  48.     cdfxvals(it) = cdfx;
  49. end
  50.  
  51. %Graph Plot
  52.  
  53. plot(xvals,cdfxvals)
  54.  
  55. end
  56.  
  57. And the command line:
  58.  
  59. To give the graph:
  60.  
  61. Figure 4 Graph showing the CDF for 4 different values of alpha
  62.  
  63. iii)
  64. For this question I used two functions, mymeans, and mymodes. These functions plot the means or modes respectively against the different values of alpha.
  65. mymodes:
  66. function[] = mymodes(Nmax)
  67.  
  68. %Variables
  69.  
  70. modevals = zeros(1,Nmax);
  71. avals = linspace(0.1,2,Nmax);
  72. it = 0;
  73. xvals = linspace(0,3,Nmax);
  74.  
  75. %Iterate
  76.  
  77. while it < Nmax
  78.     it = it+1;
  79.     a = avals(it);
  80.     pdf = a.*exp(a+xvals-a.*exp(xvals));
  81.     mode = max(pdf);
  82.     modevals(it)=mode;
  83. end
  84.  
  85. mymeans:
  86. function[] = mymeans(Nmax)
  87.  
  88. % Variables
  89.  
  90. avals = linspace(0.1,2,Nmax);
  91. it = 0;
  92. meanvals = zeros(1,Nmax);
  93.  
  94. % Iteration
  95.  
  96. while it < Nmax
  97.     it = it + 1;
  98.     a = avals(it);
  99.     mean = integral(@(x) x.*a.*exp(a+x-a.*exp(x)),0,3);
  100.     meanvals(it)=mean;
  101. end
  102.  
  103. %Graph Plot
  104.  
  105. plot(avals,meanvals)
  106.  
  107. end
  108.  
  109. Then using a command line, we get a graph showing the means and modes as a function of a.
  110.  
  111.  
  112. Figure 5 A graph showing the mean and mode of the PDF for a varying value of a.
  113. iv)
  114. Firstly I made the function, mysd, that takes the number of desired iterations as an input and plots a function of standard deviation against a.
  115.  
  116. function[] = mysd(Nmax)
  117.  
  118. % Variables
  119.  
  120. avals = linspace(0.1,2,Nmax);
  121. it = 0;
  122. sdvals = zeros(1,Nmax);
  123.  
  124. % Iteration
  125.  
  126. while it < Nmax
  127.     it = it + 1;
  128.     a = avals(it);
  129.     mean = integral(@(x) x.*a.*exp(a+x-a.*exp(x)),0,3);
  130.     sd = sqrt(integral(@(x) ((x-mean).^2).*a.*exp(a+x-a.*exp(x)),0,3));
  131.     sdvals(it) = sd;
  132. end
  133.  
  134. % Plot Graph
  135.  
  136. plot(avals,sdvals)
  137.  
  138. end
  139.  
  140.  
  141. And then I made the function myIQR which plots interquartile range as a function of a.
  142.  
  143.  
  144. function[]=myIQR(Nmax)
  145.  
  146. % Variables
  147.  
  148. xvals = linspace(0,3,Nmax);
  149. avals = linspace(0.1,2,Nmax);
  150. iqrvals = zeros(1,Nmax);
  151. it = 0;
  152.  
  153.  
  154. %iteration
  155.  
  156. while it < Nmax
  157.     it = it + 1;
  158.     a = avals(it);
  159.     iqrvals(it) = iqr(a.*exp(a+xvals-a.*exp(xvals)));
  160. end
  161.  
  162. plot(avals,iqrvals)
  163.  
  164. end
  165.  
  166.  
  167. And using these two functions I plotted the standard deviation and the interquartile range as a function of a.