-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProbDistGUI.m
More file actions
202 lines (195 loc) · 7 KB
/
ProbDistGUI.m
File metadata and controls
202 lines (195 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function ProbDistGUI
clc , clf , clear , close all ;
x = [] ;
r = [] ;
col = 0 ;
% Getting the pixel of screen
pixel = get(0,'screensize') ;
% Creating a figure and axes
fig = figure('Name','Probability Distribution Using Graphical User Interface',...
'NumberTitle','off','Visible','off','position',pixel);
% Creating push buttons
btn0 = uicontrol('Style','pushbutton','String','Load File',...
'Position',[30 600 80 35],'Callback',@load_file);
set(btn0,'FontSize',13,'FontWeight','Bold');
set(btn0,'backgroundcolor',[1 .2 0]);
btn1 = uicontrol('Style','pushbutton','String','Plot',...
'Position',[30 500 80 35],'Callback',@setplot);
set(btn1,'FontSize',12,'FontWeight','Bold');
set(btn1,'backgroundcolor',[.4 1 .4]);
btn2 = uicontrol('Style','pushbutton','String','Hist',...
'Position',[30 450 80 35],'Callback',@sethist);
set(btn2,'FontSize',12,'FontWeight','Bold');
set(btn2,'backgroundcolor',[1 .4 0]);
btn3 = uicontrol('Style','pushbutton','String','Surf',...
'Position',[30 400 80 35],'Callback',@setsurf);
set(btn3,'FontSize',12,'FontWeight','Bold');
set(btn3,'backgroundcolor',[.8 .2 1]);
btn4 = uicontrol('Style','pushbutton','String','Contourf',...
'Position',[30 350 80 35],'Callback',@setcontourf);
set(btn4,'FontSize',12,'FontWeight','Bold');
set(btn4,'backgroundcolor','c');
btn_C = uicontrol('Style', 'pushbutton', 'String', 'Probability',...
'Position', [30 130 100 35],'Callback',@calprob);
set(btn_C,'FontSize',12,'FontWeight','Bold') ;
set(btn_C,'backgroundcolor',[.2 .6 1]);
% Creating pop-up menu
popup = uicontrol('Style', 'popup',...
'String', {'Parula','Jet','HSV','Hot','Cool','Spring',...
'Summer','Autumn','Winter','Gray','Bone','Copper',...
'Pink','Lines'},'Position', [30 275 100 50],...
'Callback', @setmap);
set(popup,'FontSize',12,'backgroundcolor',[.73 .83 .96]);
% Make figure visble after adding all components
set(fig,'Visible','on') ;
% All Callback Funcitons
function load_file(source,event)
[filename, path] = uigetfile('.xls','Select the data file') ;
if filename ~= 0
data = xlsread(strcat(path, filename)) ;
[row, col] = size(data) ;
end
if col == 1
x = data ; % Univariate data
elseif col == 2
r = data ; % Bivariate data
end
end
function setplot(source,event)
if col == 1
[count,center] = hist(x,15) ;
new_x = round(min(x)) : .1 :round(max(x)) ;
prob = probability(x,new_x) ;
% Plotting normalized histogram
bw = center(2) - center(1) ;
bar(center,count/bw/sum(count),'FaceColor',[.3,.75,.93]) ;
hold on ;
plot(new_x,prob,'LineWidth',2,'color',[.49,.18,.56]) ;
hold off ;
title( 'Probability Density Function','LineWidth',2,...
'FontSize',14,'FontWeight','Bold') ;
xlabel('X Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
ylabel('Probability Density','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
grid on ;
end
if col == 2
x3 = r(:,1) ;
y3 = r(:,2) ;
plot(x3,y3,'O','LineWidth',2,'MarkerFaceColor','c',...
'MarkerEdgeColor',[.49 .18 .56]) ;
title( 'Randomly Distributed Points','LineWidth',2,...
'FontSize',14,'FontWeight','Bold') ;
xlabel('X Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
ylabel('Y Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
grid on ;
end
end
function sethist(source,event)
if col == 1
hist(x,15) ;
title( 'Histogram of the Distributed Points',...
'LineWidth',2,'FontSize',14,'FontWeight','Bold') ;
xlabel('X Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
ylabel('Frequency','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
grid on ;
end
if col == 2
hist3(r,[15,15]) ;
title( 'Histogram of the Distributed Points',...
'LineWidth',2,'FontSize',14,'FontWeight','Bold') ;
xlabel('X Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
ylabel('Y Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
zlabel('Frequency','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
set(get(gca,'child'),'FaceColor','interp','CDataMode','auto') ;
end
end
function setsurf(source,event)
if col == 2
x1 = r(:,1) ;
y = r(:,2) ;
mu = [mean(x1) mean(y)] ;
co = cov(r) ;
mu = mu' ;
new_x = round(min(x1)): .5 :round(max(x1));
y = round(min(y)): .5 :round(max(y));
[new_x,y] = meshgrid(new_x,y) ;
v = [new_x(:) y(:)] ;
v = v' ;
n = length(v) ;
prob = zeros(1,n) ;
for i = 1 : n
prob(i) = exp(-.5 * (v(:,i)-mu)' * pinv(co) * (v(:,i)-mu)) ;
end
k = 2 ;
factor = 1/(sqrt(((2*pi).^k)*det(co))) ;
prob = prob .* factor ;
prob = reshape(prob,size(new_x)) ;
surf(new_x,y,prob) ;
title( 'Probability Density Function',...
'LineWidth',2,'FontSize',14,'FontWeight','Bold') ;
xlabel('X Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
ylabel('Y Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
zlabel('Probability Density','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
pbaspect([1 1 .4]);
end
end
function setcontourf(source,event)
if col == 2
x1 = r(:,1) ;
y = r(:,2) ;
mu = [mean(x1) mean(y)] ;
co = cov(r) ;
mu = mu' ;
new_x = round(min(x1)): .5 :round(max(x1));
y = round(min(y)): .5 :round(max(y));
[new_x,y] = meshgrid(new_x,y) ;
v = [new_x(:) y(:)] ;
v = v' ;
n = length(v) ;
prob = zeros(1,n) ;
for i = 1 : n
prob(i) = exp(-.5 * (v(:,i)-mu)' * pinv(co) * (v(:,i)-mu)) ;
end
k = 2 ;
factor = 1/(sqrt(((2*pi).^k)*det(co))) ;
prob = prob .* factor ;
prob = reshape(prob,size(new_x)) ;
contourf(prob) ;
title( 'Probability Density',...
'LineWidth',2,'FontSize',14,'FontWeight','Bold') ;
xlabel('X Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
ylabel('Y Value','LineWidth',2,'FontSize',12,...
'FontWeight','Bold') ;
end
end
function setmap(source,event)
val = get(source,'Value');
maps = get(source,'String');
newmap = maps{val};
colormap(newmap);
end
function dimension_callback(source,event)
dim = get(source,'Value');
if dim == 1 % Univariate
cla , axis([0 1 0 1]) ;
elseif dim == 2 % Bivariate
cla , axis([0 1 0 1 0 1]) ;
end
end
function calprob(source,event)
user_input(x,r,col) ;
end
end