Advertisement
blurose

ee3180 bernoulli trial variance calculations

Apr 8th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. % script file for ee3180 variance calculations for bernoulli trials. uses
  2. % the function ee3180variance and feeds the parameters p (probability to
  3. % succeed), n (number of trials), and m (number of elements in each trial)
  4. % and returns a vector of the variances of each trial.
  5.  
  6. % define the 8 cases
  7. fprintf('Bernoulli Trial Variance Calculator\n');
  8. p = input('Define your probability (out of 1): ');
  9. n = input('Define the elements in each trial: ');
  10. m = input('Define the number of trials: ');
  11.  
  12. variance = ee3180variance(p, n, m);
  13.  
  14. idealvariance = p * (1 - p) / n;
  15.  
  16. fprintf('\nThe variance of the mean of these trials is %f,\nand the ideal variance is %f.\n', variance, idealvariance);
  17.  
  18.  
  19.  
  20.  
  21. function[variance] = ee3180variance(p, n, m)
  22.  
  23. % step 1: build the data
  24.  
  25. for trial = 1:m % there are m trials
  26. for element = 1:n % there are n elements in each trial
  27. % set each data(trial, element) to a success (1) or a fail (0)
  28. if (rand() <= p) % probability p of passing
  29. data(trial, element) = 1;
  30. else
  31. data(trial, element) = 0;
  32. end
  33. end
  34. average(trial) = sum(data(trial, :)) / n;
  35. end
  36.  
  37. % step 3: calculate variance of mean
  38.  
  39. sumofdiffsquared = 0; % init the variable
  40. for trial = 1:m
  41. sumofdiffsquared = sumofdiffsquared + (average(trial) - mean(average))^2;
  42. end
  43. variance = sumofdiffsquared / (m - 1); % calculate the variance of the mean
  44.  
  45. end % end the function
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement