Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Tomasz GOROL
- % Magdalena LOREK
- % Probability and Statistics
- % --- Laboratory 9
- % Macrofaculty, group 2
- % 12.12.2018
- % =------------------------------------------------------------------------=
- mu = 100; % Set mu (mean)
- sigma = 30; % Set sigma (standard deviation)
- N = 10000; % Set amount of random numbers from normal distribution
- ci = 0.96; % Set confidence level
- R = 3; % Repetitions
- % Set amount of sample numbers in subsequent draws
- N1 = 20;
- N2 = 50;
- N3 = 100;
- N4 = 500;
- N5 = 1000;
- N6 = 5000;
- % =------------------------------------------------------------------------=
- randoms = normrnd(mu, sigma, [1, N]);
- n1_vec = [];
- n2_vec = [];
- n3_vec = [];
- n4_vec = [];
- n5_vec = [];
- n6_vec = [];
- for j = 1:R
- % -------- Draw N1 --------
- for i = 1:N1
- n1_vec = [n1_vec, randoms(randi(length(randoms)))];
- end
- n1_mean = mean(n1_vec);
- n1_std = std(n1_vec);
- n1_var = var(n1_vec);
- n1_dof = N1-1; % Degree of freedom
- a = 1-ci;
- z = tinv(1-a/2, n1_dof) %use of tinv requires Statistics Toolbox in MatLab
- n1_ci_lower_bound = n1_mean - z*(n1_std/sqrt(N1));
- n1_ci_upper_bound = n1_mean + z*(n1_std/sqrt(N1));
- % -----------------
- % -------- Draw N2 --------
- for i = 1:N2
- n2_vec = [n2_vec, randoms(randi(length(randoms)))];
- end
- n2_mean = mean(n2_vec);
- n2_std = std(n2_vec);
- n2_var = var(n2_vec);
- n2_dof = N2-1; % Degree of freedom
- a = 1-ci;
- z = tinv(1-a/2, n2_dof) %use of tinv requires Statistics Toolbox in MatLab
- n2_ci_lower_bound = n2_mean - z*(n2_std/sqrt(N2));
- n2_ci_upper_bound = n2_mean + z*(n2_std/sqrt(N2));
- % -------------------------
- % -------- Draw N3 --------
- for i = 1:N3
- n3_vec = [n3_vec, randoms(randi(length(randoms)))];
- end
- n3_mean = mean(n3_vec);
- n3_std = std(n3_vec);
- n3_var = var(n3_vec);
- n3_dof = N3-1; % Degree of freedom
- a = 1-ci;
- z = tinv(1-a/2, n3_dof) %use of tinv requires Statistics Toolbox in MatLab
- n3_ci_lower_bound = n3_mean - z*(n3_std/sqrt(N3));
- n3_ci_upper_bound = n3_mean + z*(n3_std/sqrt(N3));
- % -------------------------
- % -------- Draw N4 --------
- for i = 1:N4
- n4_vec = [n4_vec, randoms(randi(length(randoms)))];
- end
- n4_mean = mean(n4_vec);
- n4_std = std(n4_vec);
- n4_var = var(n4_vec);
- n4_dof = N4-1; % Degree of freedom
- a = 1-ci;
- z = tinv(1-a/2, n4_dof) %use of tinv requires Statistics Toolbox in MatLab
- n4_ci_lower_bound = n4_mean - z*(n4_std/sqrt(N4));
- n4_ci_upper_bound = n4_mean + z*(n4_std/sqrt(N4));
- % -------------------------
- % -------- Draw N5 --------
- for i = 1:N5
- n5_vec = [n5_vec, randoms(randi(length(randoms)))];
- end
- n5_mean = mean(n5_vec);
- n5_std = std(n5_vec);
- n5_var = var(n5_vec);
- n5_dof = N5-1; % Degree of freedom
- a = 1-ci;
- z = tinv(1-a/2, n5_dof) %use of tinv requires Statistics Toolbox in MatLab
- n5_ci_lower_bound = n5_mean - z*(n5_std/sqrt(N5));
- n5_ci_upper_bound = n5_mean + z*(n5_std/sqrt(N5));
- % -------------------------
- % -------- Draw N6 --------
- for i = 1:N6
- n6_vec = [n6_vec, randoms(randi(length(randoms)))];
- end
- n6_mean = mean(n6_vec);
- n6_std = std(n6_vec);
- n6_var = var(n6_vec);
- n6_dof = N6-1; % Degree of freedom
- a = 1-ci;
- z = tinv(1-a/2, n6_dof); %use of tinv requires Statistics Toolbox in MatLab
- n6_ci_lower_bound = n6_mean - z*(n6_std/sqrt(N6));
- n6_ci_upper_bound = n6_mean + z*(n6_std/sqrt(N6));
- % -------------------------
- % ------- PLOTTING A GRAPH -------
- % Using plot() function present relationship between size of the sample
- % (on log10 scale) and calculated CI for mean value. Use different colors
- % for 3 repetitions of the same N and different graphical symbols for CI.
- % Copy the graph on the piece of paper and write a conclusion.
- title('Relationship between sample size and length of CI');
- xlabel('Size of the sample');
- ylabel('CI Interval');
- if(j == 1)
- semilogx(N1, n1_ci_lower_bound, 'rs', N1, n1_ci_upper_bound, 'rs',
- N2, n2_ci_lower_bound, 'rs', N2, n2_ci_upper_bound, 'rs',
- N3, n3_ci_lower_bound, 'rs', N3, n3_ci_upper_bound, 'rs',
- N4, n4_ci_lower_bound, 'rs', N4, n4_ci_upper_bound, 'rs',
- N5, n5_ci_lower_bound, 'rs', N5, n5_ci_upper_bound, 'rs',
- N6, n6_ci_lower_bound, 'rs', N6, n6_ci_upper_bound, 'rs');
- elseif(j == 2)
- semilogx(N1, n1_ci_lower_bound, 'yo', N1, n1_ci_upper_bound, 'yo',
- N2, n2_ci_lower_bound, 'yo', N2, n2_ci_upper_bound, 'yo',
- N3, n3_ci_lower_bound, 'yo', N3, n3_ci_upper_bound, 'yo',
- N4, n4_ci_lower_bound, 'yo', N4, n4_ci_upper_bound, 'yo',
- N5, n5_ci_lower_bound, 'yo', N5, n5_ci_upper_bound, 'yo',
- N6, n6_ci_lower_bound, 'yo', N6, n6_ci_upper_bound, 'yo');
- else
- semilogx(N1, n1_ci_lower_bound, 'bx', N1, n1_ci_upper_bound, 'bx',
- N2, n2_ci_lower_bound, 'bx', N2, n2_ci_upper_bound, 'bx',
- N3, n3_ci_lower_bound, 'bx', N3, n3_ci_upper_bound, 'bx',
- N4, n4_ci_lower_bound, 'bx', N4, n4_ci_upper_bound, 'bx',
- N5, n5_ci_lower_bound, 'bx', N5, n5_ci_upper_bound, 'bx',
- N6, n6_ci_lower_bound, 'bx', N6, n6_ci_upper_bound, 'bx');
- end
- hold on;
- % --------------------------------
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement