Posted By

eiger_824 on 11/11/15


Tagged

asdf


Versions (?)

assignment


 / Published in: MatLab
 

URL: asdfasdf

asdfasdf

  1. format shortG
  2. %%Assignment 1
  3. %For the figures
  4. countries = ['ARG';'AUS';'AUT';'BEL';'BER';'BRA';'CAN';'CHI';'CHN';'COL';'COK';'CRC';'CZE';'DEN';'DOM';'FIN';'FRA';'GER';'GBR';'GRE';'GUA';'HUN';'INA';'IND';'IRL';'ISR';'ITA';'JPN';'KEN';'KRS';'KRN';'LUX';'MAS';'MRI';'MEX';'MYA';'NED';'NZL';'NOR';'PNG';'PHI';'POL';'POR';'ROM';'RUS';'SAM';'SIN';'ESP';'SWE';'SUI';'TPE';'THA';'TUR';'USA'];
  5. %1.a]
  6. %Import the 7 variables:
  7. [V1,V2,V3,V4,V5,V6,V7] = textread('T1-9.txt')
  8. %Means:
  9. mu_1 = mean(V1);
  10. mu_2 = mean(V2);
  11. mu_3 = mean(V3);
  12. mu_4 = mean(V4);
  13. mu_5 = mean(V5);
  14. mu_6 = mean(V6);
  15. mu_7 = mean(V7);
  16.  
  17. %Standard deviations:
  18. delta_1 = std(V1);
  19. delta_2 = std(V2);
  20. delta_3 = std(V3);
  21. delta_4 = std(V4);
  22. delta_5 = std(V5);
  23. delta_6 = std(V6);
  24. delta_7 = std(V7);
  25.  
  26. %Variances:
  27. var_1 = var(V1);
  28. var_2 = var(V2);
  29. var_3 = var(V3);
  30. var_4 = var(V4);
  31. var_5 = var(V5);
  32. var_6 = var(V6);
  33. var_7 = var(V7);
  34.  
  35. %Plot variables
  36. plot((1:length(V1))',V1,'--')
  37. xlabel('Countries')
  38. ylabel('Records')
  39. title('1984 National Track Records for Women - 100m')
  40. axis([1 54 min(V1) max(V1)])
  41. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  42. hold on
  43. plot(1:54,mu_1,'*-')
  44. hold on
  45. plot(1:54,mu_1+delta_1,'g*-')
  46. hold on
  47. plot(1:54,mu_1-delta_1,'r*-')
  48. legend('Scores by country','Mean score (\mu_1 = 11.36)','\mu_1 + \delta_1','\mu_1 - \delta_1')
  49.  
  50. plot((1:length(V2))',V2,'--')
  51. xlabel('Countries')
  52. ylabel('Records')
  53. title('1984 National Track Records for Women - 200m')
  54. axis([1 54 min(V2) max(V2)])
  55. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  56. hold on
  57. plot(1:54,mu_2,'*-')
  58. hold on
  59. plot(1:54,mu_2+delta_2,'g*-')
  60. hold on
  61. plot(1:54,mu_2-delta_2,'r*-')
  62. legend('Scores by country','Mean score (\mu_2 = 23.12)','\mu_2 + \delta_2','\mu_2 - \delta_2')
  63.  
  64. plot((1:length(V3))',V3,'--')
  65. xlabel('Countries')
  66. ylabel('Records')
  67. title('1984 National Track Records for Women - 400m')
  68. axis([1 54 min(V3) max(V3)])
  69. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  70. hold on
  71. plot(1:54,mu_3,'*-')
  72. hold on
  73. plot(1:54,mu_3+delta_3,'g*-')
  74. hold on
  75. plot(1:54,mu_3-delta_3,'r*-')
  76. legend('Scores by country','Mean score (\mu_3 = 51.99)','\mu_3 + \delta_3','\mu_3 - \delta_3')
  77.  
  78. plot((1:length(V4))',V4,'--')
  79. xlabel('Countries')
  80. ylabel('Records')
  81. title('1984 National Track Records for Women - 800m')
  82. axis([1 54 min(V4) max(V4)])
  83. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  84. hold on
  85. plot(1:54,mu_4,'*-')
  86. hold on
  87. plot(1:54,mu_4+delta_4,'g*-')
  88. hold on
  89. plot(1:54,mu_4-delta_4,'r*-')
  90. legend('Scores by country','Mean score (\mu_4 = 2.02)','\mu_4 + \delta_4','\mu_4 - \delta_4')
  91.  
  92. plot((1:length(V5))',V5,'--')
  93. xlabel('Countries')
  94. ylabel('Records')
  95. title('1984 National Track Records for Women - 1500m')
  96. axis([1 54 min(V5) max(V5)])
  97. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  98. hold on
  99. plot(1:54,mu_5,'*-')
  100. hold on
  101. plot(1:54,mu_5+delta_5,'g*-')
  102. hold on
  103. plot(1:54,mu_5-delta_5,'r*-')
  104. legend('Scores by country','Mean score (\mu_5 = 4.19)','\mu_5 + \delta_5','\mu_5 - \delta_5')
  105.  
  106. plot((1:length(V6))',V6,'--')
  107. xlabel('Countries')
  108. ylabel('Records')
  109. title('1984 National Track Records for Women - 3000m')
  110. axis([1 54 min(V6) max(V6)])
  111. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  112. hold on
  113. plot(1:54,mu_6,'*-')
  114. hold on
  115. plot(1:54,mu_6+delta_6,'g*-')
  116. hold on
  117. plot(1:54,mu_6-delta_6,'r*-')
  118. legend('Scores by country','Mean score (\mu_6 = 9.08)','\mu_6 + \delta_6','\mu_6 - \delta_6')
  119.  
  120. plot((1:length(V7))',V7,'--')
  121. xlabel('Countries')
  122. ylabel('Records')
  123. title('1984 National Track Records for Women - Marathon')
  124. axis([1 54 min(V7) max(V7)])
  125. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  126. hold on
  127. plot(1:54,mu_7,'*-')
  128. hold on
  129. plot(1:54,mu_7+delta_7,'g*-')
  130. hold on
  131. plot(1:54,mu_7-delta_7,'r*-')
  132. legend('Scores by country','Mean score (\mu_7 = 153.62)','\mu_7 + \delta_7','\mu_7 - \delta_7')
  133.  
  134. %%Assignment 2
  135. %Covariance and Correlation matrices
  136. %First, let's create a matrix with all the seven variables:
  137. variables = [V1,V2,V3,V4,V5,V6,V7];
  138. cov_matrix = cov(variables);
  139. RHO_matrix = corr(variables);
  140. %Scatter plots
  141. subplot(3,4,1)
  142. scatter(V1,V1,'r')
  143. title('Scatter of V1 and V1')
  144. subplot(3,4,2)
  145. scatter(V1,V2)
  146. title('Scatter of V1 and V2')
  147. subplot(3,4,3)
  148. scatter(V1,V3)
  149. title('Scatter of V1 and V3')
  150. subplot(3,4,4)
  151. scatter(V1,V4)
  152. title('Scatter of V1 and V4')
  153. subplot(3,4,5)
  154. scatter(V1,V5)
  155. title('Scatter of V1 and V5')
  156. subplot(3,4,6)
  157. scatter(V1,V6)
  158. title('Scatter of V1 and V6')
  159. subplot(3,4,7)
  160. scatter(V1,V7)
  161. title('Scatter of V1 and V7')
  162. subplot(3,4,8)
  163. scatter(V2,V2,'r')
  164. title('Scatter of V2 and V2')
  165. subplot(3,4,9)
  166. scatter(V2,V3)
  167. title('Scatter of V2 and V3')
  168. subplot(3,4,10)
  169. scatter(V2,V4)
  170. title('Scatter of V2 and V4')
  171. subplot(3,4,11)
  172. scatter(V2,V5)
  173. title('Scatter of V2 and V5')
  174. subplot(3,4,12)
  175. scatter(V2,V6)
  176. title('Scatter of V2 and V6')
  177.  
  178. subplot(3,4,1)
  179. scatter(V2,V7)
  180. title('Scatter of V2 and V7')
  181. subplot(3,4,2)
  182. scatter(V3,V3,'r')
  183. title('Scatter of V3 and V3')
  184. subplot(3,4,3)
  185. scatter(V3,V4)
  186. title('Scatter of V3 and V4')
  187. subplot(3,4,4)
  188. scatter(V3,V5)
  189. title('Scatter of V3 and V5')
  190. subplot(3,4,5)
  191. scatter(V3,V6)
  192. title('Scatter of V3 and V6')
  193. subplot(3,4,6)
  194. scatter(V3,V7)
  195. title('Scatter of V3 and V7')
  196. subplot(3,4,7)
  197. scatter(V4,V4,'r')
  198. title('Scatter of V4 and V4')
  199. subplot(3,4,8)
  200. scatter(V4,V5)
  201. title('Scatter of V4 and V5')
  202. subplot(3,4,9)
  203. scatter(V4,V6)
  204. title('Scatter of V4 and V6')
  205. subplot(3,4,10)
  206. scatter(V4,V7)
  207. title('Scatter of V4 and V7')
  208. subplot(3,4,11)
  209. scatter(V5,V5,'r')
  210. title('Scatter of V5 and V5')
  211. subplot(3,4,12)
  212. scatter(V5,V6)
  213. title('Scatter of V5 and V6')
  214.  
  215. figure(10)
  216. subplot(3,4,1)
  217. scatter(V5,V7)
  218. title('Scatter of V5 and V7')
  219. subplot(3,4,2)
  220. scatter(V6,V6,'r')
  221. title('Scatter of V6 and V6')
  222. subplot(3,4,3)
  223. scatter(V6,V7)
  224. title('Scatter of V6 and V7')
  225. subplot(3,4,4)
  226. scatter(V7,V7,'r')
  227. title('Scatter of V7 and V7')
  228.  
  229. %3D-scatterplots
  230. %Good correlation: variables V1,V2 and V3
  231. figure(11)
  232. scatter3(V1,V2,V3)
  233. xlabel('V1')
  234. ylabel('V2')
  235. xlabel('V3')
  236. title('3D-Scatter of V1,V2 and V3')
  237.  
  238. %"Bad" correlation: variables V1, V3 and V7
  239. figure(12)
  240. scatter3(V1,V3,V7)
  241. xlabel('V1')
  242. ylabel('V3')
  243. xlabel('V7')
  244. title('3D-Scatter of V1,V3 and V7')
  245.  
  246. %%Assignment 3
  247. %3.a)
  248. %First, calculate distances to the mean
  249. aux1 = V1-mu_1;
  250. aux2 = V1-mu_2;
  251. aux3 = V1-mu_3;
  252. aux4 = V1-mu_4;
  253. aux5 = V1-mu_5;
  254. aux6 = V1-mu_6;
  255. aux7 = V1-mu_7;
  256. %Now, create a second column with the index of each country (to sort them)
  257. V1_prime = [abs(aux1), (1:54)'];
  258. V2_prime = [abs(aux2), (1:54)'];
  259. V3_prime = [abs(aux3), (1:54)'];
  260. V4_prime = [abs(aux4), (1:54)'];
  261. V5_prime = [abs(aux5), (1:54)'];
  262. V6_prime = [abs(aux6), (1:54)'];
  263. V7_prime = [abs(aux7), (1:54)'];
  264. %Next, we sort from the greatest to the least value to get the most extreme
  265. %values (with Greatest2Least function)
  266. [V1_sorted,countriesV1] = Greatest2Least(V1_prime);
  267. [V2_sorted,countriesV2] = Greatest2Least(V2_prime);
  268. [V3_sorted,countriesV3] = Greatest2Least(V3_prime);
  269. [V4_sorted,countriesV4] = Greatest2Least(V4_prime);
  270. [V5_sorted,countriesV5] = Greatest2Least(V5_prime);
  271. [V6_sorted,countriesV6] = Greatest2Least(V6_prime);
  272. [V7_sorted,countriesV7] = Greatest2Least(V7_prime);
  273. %Finally, we select the top four most-extreme candidates
  274. top4_V1 = countriesV1(1:4,:);
  275. top4_V2 = countriesV2(1:4,:);
  276. top4_V3 = countriesV3(1:4,:);
  277. top4_V4 = countriesV4(1:4,:);
  278. top4_V5 = countriesV5(1:4,:);
  279. top4_V6 = countriesV6(1:4,:);
  280. top4_V7 = countriesV7(1:4,:);
  281. %And we present the output "beautifully" for a better understading
  282. pos = ['#1:';'#2:';'#3:';'#4:'];
  283. header = ['---V1----V2----V3----V4----V5----V6----V7-'];
  284. most_ext_countries_perCathegory = [pos top4_V1 pos top4_V2 pos top4_V3 pos top4_V4 pos top4_V5 pos top4_V6 pos top4_V7];
  285. top4 = [header; most_ext_countries_perCathegory]
  286. %3.b)
  287. %First normalize variables
  288. V1_norm = V1-mu_1;
  289. V2_norm = V2-mu_2;
  290. V3_norm = V3-mu_3;
  291. V4_norm = V4-mu_4;
  292. V5_norm = V5-mu_5;
  293. V6_norm = V6-mu_6;
  294. V7_norm = V7-mu_7;
  295. %Now, calculate distances but first create standardized matrix
  296. standardized_matrix = [V1_norm V2_norm V3_norm V4_norm V5_norm V6_norm V7_norm]
  297. dist = zeros(1,54);
  298. for i=1:54
  299. dist(i) = StandardEuclideanDistance(standardized_matrix(i,:),standardized_matrix(i,:));
  300. end
  301. dist = dist'
  302. figure(13)
  303. plot(1:54,dist)
  304. xlabel('Countries')
  305. ylabel('Standardized Euclidean Distance')
  306. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  307. %3.c)
  308. V = diag([var_1 var_2 var_3 var_4 var_5 var_6 var_7]);
  309. dist2 = zeros(1,54);
  310. for i=1:54
  311. dist2(i) = ScaleIndependentDistance(standardized_matrix(i,:),V);
  312. end
  313. dist2 = dist2'
  314. figure(14)
  315. plot(1:54,dist2)
  316. xlabel('Countries')
  317. ylabel('Scale-Independent Distance')
  318. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')
  319.  
  320. %3.d)Mahalanobis Distance
  321. dist3 = zeros(1,54);
  322. for i=1:54
  323. dist3(i) = MahalanobisDistance(standardized_matrix(i,:),cov_matrix);
  324. end
  325. dist3 = dist3'
  326. figure(15)
  327. plot(1:54,dist3)
  328. xlabel('Countries')
  329. ylabel('Mahalanobis Distance')
  330. set(gca,'XTick',1:54,'XTickLabel',countries,'FontSize',5.0,'XGrid','on')

Report this snippet  

You need to login to post a comment.