• API
• FAQ
• Tools
• Archive
daily pastebin goal
58%
SHARE
TWEET

# Untitled

a guest Dec 12th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. % Tomasz GOROL
2. % Magdalena LOREK
3.
4. % Probability and Statistics
5. % --- Laboratory 9
6. % Macrofaculty, group 2
7. % 12.12.2018
8.
9.
10. % =------------------------------------------------------------------------=
11. mu = 100;     % Set mu (mean)
12. sigma = 30;   % Set sigma (standard deviation)
13. N = 10000;    % Set amount of random numbers from normal distribution
14. ci = 0.96;    % Set confidence level
15. R = 3;        % Repetitions
16.
17. % Set amount of sample numbers in subsequent draws
18. N1 = 20;
19. N2 = 50;
20. N3 = 100;
21. N4 = 500;
22. N5 = 1000;
23. N6 = 5000;
24. % =------------------------------------------------------------------------=
25.
26.
27. randoms = normrnd(mu, sigma, [1, N]);
28. n1_vec = [];
29. n2_vec = [];
30. n3_vec = [];
31. n4_vec = [];
32. n5_vec = [];
33. n6_vec = [];
34.
35.
36. for j = 1:R
37. % -------- Draw N1 --------
38. for i = 1:N1
39.   n1_vec = [n1_vec, randoms(randi(length(randoms)))];
40. end
41. n1_mean = mean(n1_vec);
42. n1_std = std(n1_vec);
43. n1_var = var(n1_vec);
44. n1_dof = N1-1;  % Degree of freedom
45. a = 1-ci;
46. z = tinv(1-a/2, n1_dof) %use of tinv requires Statistics Toolbox in MatLab
47. n1_ci_lower_bound = n1_mean - z*(n1_std/sqrt(N1));
48. n1_ci_upper_bound = n1_mean + z*(n1_std/sqrt(N1));
49. % -----------------
50.
51.
52.
53. % -------- Draw N2 --------
54. for i = 1:N2
55.   n2_vec = [n2_vec, randoms(randi(length(randoms)))];
56. end
57. n2_mean = mean(n2_vec);
58. n2_std = std(n2_vec);
59. n2_var = var(n2_vec);
60. n2_dof = N2-1;  % Degree of freedom
61. a = 1-ci;
62. z = tinv(1-a/2, n2_dof) %use of tinv requires Statistics Toolbox in MatLab
63. n2_ci_lower_bound = n2_mean - z*(n2_std/sqrt(N2));
64. n2_ci_upper_bound = n2_mean + z*(n2_std/sqrt(N2));
65. % -------------------------
66.
67.
68.
69. % -------- Draw N3 --------
70. for i = 1:N3
71.   n3_vec = [n3_vec, randoms(randi(length(randoms)))];
72. end
73. n3_mean = mean(n3_vec);
74. n3_std = std(n3_vec);
75. n3_var = var(n3_vec);
76. n3_dof = N3-1;  % Degree of freedom
77. a = 1-ci;
78. z = tinv(1-a/2, n3_dof) %use of tinv requires Statistics Toolbox in MatLab
79. n3_ci_lower_bound = n3_mean - z*(n3_std/sqrt(N3));
80. n3_ci_upper_bound = n3_mean + z*(n3_std/sqrt(N3));
81. % -------------------------
82.
83.
84.
85. % -------- Draw N4 --------
86. for i = 1:N4
87.   n4_vec = [n4_vec, randoms(randi(length(randoms)))];
88. end
89. n4_mean = mean(n4_vec);
90. n4_std = std(n4_vec);
91. n4_var = var(n4_vec);
92. n4_dof = N4-1;  % Degree of freedom
93. a = 1-ci;
94. z = tinv(1-a/2, n4_dof) %use of tinv requires Statistics Toolbox in MatLab
95. n4_ci_lower_bound = n4_mean - z*(n4_std/sqrt(N4));
96. n4_ci_upper_bound = n4_mean + z*(n4_std/sqrt(N4));
97. % -------------------------
98.
99.
100.
101. % -------- Draw N5 --------
102. for i = 1:N5
103.   n5_vec = [n5_vec, randoms(randi(length(randoms)))];
104. end
105. n5_mean = mean(n5_vec);
106. n5_std = std(n5_vec);
107. n5_var = var(n5_vec);
108. n5_dof = N5-1;  % Degree of freedom
109. a = 1-ci;
110. z = tinv(1-a/2, n5_dof) %use of tinv requires Statistics Toolbox in MatLab
111. n5_ci_lower_bound = n5_mean - z*(n5_std/sqrt(N5));
112. n5_ci_upper_bound = n5_mean + z*(n5_std/sqrt(N5));
113. % -------------------------
114.
115.
116.
117. % -------- Draw N6 --------
118. for i = 1:N6
119.   n6_vec = [n6_vec, randoms(randi(length(randoms)))];
120. end
121. n6_mean = mean(n6_vec);
122. n6_std = std(n6_vec);
123. n6_var = var(n6_vec);
124. n6_dof = N6-1;  % Degree of freedom
125. a = 1-ci;
126. z = tinv(1-a/2, n6_dof); %use of tinv requires Statistics Toolbox in MatLab
127. n6_ci_lower_bound = n6_mean - z*(n6_std/sqrt(N6));
128. n6_ci_upper_bound = n6_mean + z*(n6_std/sqrt(N6));
129. % -------------------------
130.
131.
132.
133. % ------- PLOTTING A GRAPH -------
134.   % Using plot() function present relationship between size of the sample
135.   % (on log10 scale) and calculated CI for mean value. Use different colors
136.   % for 3 repetitions of the same N and different graphical symbols for CI.
137.   % Copy the graph on the piece of paper and write a conclusion.
138.   title('Relationship between sample size and length of CI');
139.   xlabel('Size of the sample');
140.   ylabel('CI Interval');
141.
142.   if(j == 1)
143.     semilogx(N1, n1_ci_lower_bound, 'rs', N1, n1_ci_upper_bound, 'rs',
144.              N2, n2_ci_lower_bound, 'rs', N2, n2_ci_upper_bound, 'rs',
145.              N3, n3_ci_lower_bound, 'rs', N3, n3_ci_upper_bound, 'rs',
146.              N4, n4_ci_lower_bound, 'rs', N4, n4_ci_upper_bound, 'rs',
147.              N5, n5_ci_lower_bound, 'rs', N5, n5_ci_upper_bound, 'rs',
148.              N6, n6_ci_lower_bound, 'rs', N6, n6_ci_upper_bound, 'rs');
149.   elseif(j == 2)
150.     semilogx(N1, n1_ci_lower_bound, 'yo', N1, n1_ci_upper_bound, 'yo',
151.              N2, n2_ci_lower_bound, 'yo', N2, n2_ci_upper_bound, 'yo',
152.              N3, n3_ci_lower_bound, 'yo', N3, n3_ci_upper_bound, 'yo',
153.              N4, n4_ci_lower_bound, 'yo', N4, n4_ci_upper_bound, 'yo',
154.              N5, n5_ci_lower_bound, 'yo', N5, n5_ci_upper_bound, 'yo',
155.              N6, n6_ci_lower_bound, 'yo', N6, n6_ci_upper_bound, 'yo');
156.   else
157.     semilogx(N1, n1_ci_lower_bound, 'bx', N1, n1_ci_upper_bound, 'bx',
158.              N2, n2_ci_lower_bound, 'bx', N2, n2_ci_upper_bound, 'bx',
159.              N3, n3_ci_lower_bound, 'bx', N3, n3_ci_upper_bound, 'bx',
160.              N4, n4_ci_lower_bound, 'bx', N4, n4_ci_upper_bound, 'bx',
161.              N5, n5_ci_lower_bound, 'bx', N5, n5_ci_upper_bound, 'bx',
162.              N6, n6_ci_lower_bound, 'bx', N6, n6_ci_upper_bound, 'bx');
163.   end
164.   hold on;
165. % --------------------------------
166. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top