Advertisement
makispaiktis

Statistics.m

Oct 3rd, 2022 (edited)
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.04 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % 1. Uniform Distribution
  5. N = 1000;
  6. x = 1 : N;
  7. y = rand(1, N);
  8. figure();
  9. subplot(2, 1, 1);
  10. scatter(x, y);
  11. title(num2str(N) + " random spots (UNIFORM)");
  12. subplot(2, 1, 2);
  13. hist(y, 20);
  14. title("Histogram of UNIFORM in [0, 1]");
  15. print_stats(y, "UNIFORM");
  16.  
  17. % 2. Normal Distribution
  18. N = 1000;
  19. MEAN_N = 10;
  20. STD_N = 2;
  21. y = MEAN_N + randn(1, N) * STD_N;
  22. id_x = linspace(MEAN_N - 4*STD_N, MEAN_N + 4*STD_N, N+1);
  23. id_y = exp(-(id_x - MEAN_N).^2 / (2*STD_N^2));
  24. figure();
  25. subplot(2, 1, 1);
  26. bar(id_x, id_y);
  27. title("Ideal normal distribution with: x_{avg} = " + num2str(MEAN_N) + " and std = " + num2str(STD_N));
  28. subplot(2, 1, 2);
  29. hist(y, 20);
  30. title("Histogram of NORMAL with: x_{avg} = " + num2str(MEAN_N) + " and std = " + num2str(STD_N));
  31. print_stats(y, "NORMAL");
  32.  
  33.  
  34. % Auxiliary Functions
  35. function MEDIAN = median(x)
  36.     LEN = length(x);
  37.     if mod(LEN, 2) == 1
  38.         MEDIAN = x((LEN+1)/2);
  39.     else
  40.         MEDIAN = 1/2 * (x(LEN/2) + x(LEN/2+1));
  41.     end
  42. end
  43.  
  44. function [RANGE, MAX, MIN] = range(x)
  45.     MAX = max(x);
  46.     MIN = min(x);
  47.     RANGE = MAX - MIN;
  48. end
  49.  
  50. function AAD = aad(x, MEAN)
  51.     AAD = mean(abs(x - MEAN));
  52. end
  53.  
  54. function MAD = mad(x, MEAN)
  55.     y = abs(x - MEAN);
  56.     MAD = median(y);
  57. end
  58.  
  59. function pretty_print(number, name)
  60.     disp(name + " = " + num2str(number));
  61. end
  62.  
  63. function print_stats(x, distribution)
  64.     MEAN = mean(x);
  65.     STD = std(x);
  66.     VAR = var(x);
  67.     MEDIAN = median(x);
  68.     [RANGE, MAX, MIN] = range(x);
  69.     AAD = aad(x, MEAN);
  70.     MAD = mad(x, MEAN);
  71.     display('********************************************************');
  72.     disp("      " + distribution);
  73.     display(' ');
  74.     pretty_print(MIN, "Min");
  75.     pretty_print(MAX, "Max");
  76.     pretty_print(RANGE, "Range");
  77.     pretty_print(MEAN, "Mean");
  78.     pretty_print(MEDIAN, "Median");
  79.     pretty_print(VAR, "Variance");
  80.     pretty_print(STD, "Std");
  81.     pretty_print(AAD, "AAD");
  82.     pretty_print(MAD, "MAD");
  83.     display('********************************************************');
  84.     display(' ');
  85. end
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement