Advertisement
makispaiktis

Probability of being chosen (n/2 from a set with length n)

Dec 19th, 2021 (edited)
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.07 KB | None | 0 0
  1. clc
  2. clear all
  3.  
  4. % Main Function
  5. nList = 2 : 2 : 20;
  6. prob100List = zeros(1, length(nList));
  7. for i = 1:length(nList)
  8.     n = nList(i);
  9.     k = n/2;
  10.     prob100 = probabilityMisoi(n, k);
  11.     prob100List(i) = prob100;
  12. end
  13. nList;
  14. prob100List;
  15. plot(nList, prob100List);
  16. title('Probability of being chosen of a set length n, when n/2 are chosen');
  17. xlabel('n = length of a set');
  18. ylabel('Probability');
  19.  
  20. % Auxiliary Functions
  21. function factorial = getFactorial(n)
  22.     if n == 0 || n == 1
  23.         factorial = 1;
  24.     elseif n == 2
  25.         factorial = 2;
  26.     else
  27.         factorialList = zeros(1, n);
  28.         factorialList(1) = 1;
  29.         factorialList(2) = 2;
  30.         for i = 3:n
  31.             factorialList(i) = i * factorialList(i-1);
  32.         factorial = factorialList(n);
  33.         end
  34.     end
  35. end
  36.  
  37. function result = c(n, k)
  38.     if k <= n && mod(n, 2) == 0
  39.         result = factorial(n) / (factorial(k) * factorial(n-k));
  40.     end
  41. end
  42.  
  43. function prob100 = probabilityMisoi(n, k)
  44.     if k <= n && mod(n, 2) == 0
  45.         prob100 = 100 * ((n-1) / c(n, k));
  46.     end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement