Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % script file for ee3180 variance calculations for bernoulli trials. uses
- % the function ee3180variance and feeds the parameters p (probability to
- % succeed), n (number of trials), and m (number of elements in each trial)
- % and returns a vector of the variances of each trial.
- % define the 8 cases
- fprintf('Bernoulli Trial Variance Calculator\n');
- p = input('Define your probability (out of 1): ');
- n = input('Define the elements in each trial: ');
- m = input('Define the number of trials: ');
- variance = ee3180variance(p, n, m);
- idealvariance = p * (1 - p) / n;
- fprintf('\nThe variance of the mean of these trials is %f,\nand the ideal variance is %f.\n', variance, idealvariance);
- function[variance] = ee3180variance(p, n, m)
- % step 1: build the data
- for trial = 1:m % there are m trials
- for element = 1:n % there are n elements in each trial
- % set each data(trial, element) to a success (1) or a fail (0)
- if (rand() <= p) % probability p of passing
- data(trial, element) = 1;
- else
- data(trial, element) = 0;
- end
- end
- average(trial) = sum(data(trial, :)) / n;
- end
- % step 3: calculate variance of mean
- sumofdiffsquared = 0; % init the variable
- for trial = 1:m
- sumofdiffsquared = sumofdiffsquared + (average(trial) - mean(average))^2;
- end
- variance = sumofdiffsquared / (m - 1); % calculate the variance of the mean
- end % end the function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement