-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultivariateND.m
73 lines (53 loc) · 2.01 KB
/
MultivariateND.m
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
% Sampling from a Multivariate Normal Distribution Using Simplex Sampling
mu = [1; 2; 3]; % Mean vector example (consider your own values)
Sigma = [1, 0.5, 0.3; % Covariance matrix example (consider your own values)
0.5, 2, -0.1;
0.3, -0.1, 0.5];
n = length(mu); % Number of dimensions (variables)
m = 1000; % Number of samples to generate
y = randomSimplexMatrix(n, m); % Generate random samples on (n-1)-dimensional simplex
samples = mvnrnd(mu', Sigma, m); % Transform samples to follow the multivariate normal distribution
%--------------------------------------------------------------------------------------------------------------------
% Scatter plot of generated samples
figure;
scatter3(samples(:,1), samples(:,2), samples(:,3), 10, y(:,1), 'filled') % Color by the first dimension of y
title('Samples from Multivariate Normal Distribution');
xlabel('X1');
ylabel('X2');
zlabel('X3');
colormap('parula');
colorbar;
axis equal;
grid on;
%--------------------------------------------------------------------------------------------------------------------
% Pairwise Scatter Plots
figure;
subplot(1,2,1);
scatter(samples(:,1), samples(:,2), 10, 'filled');
title('X1 vs X2');
xlabel('X1');
ylabel('X2');
axis square;
grid on;
subplot(1,2,2);
scatter(samples(:,1), samples(:,3), 10, 'filled');
title('X1 vs X3');
xlabel('X1');
ylabel('X3');
axis square;
grid on;
%--------------------------------------------------------------------------------------------------------------------
%Histograms of Sampled Variables
figure;
for i = 1:n
subplot(1, n, i);
histogram(samples(:, i), 'Normalization', 'probability');
title(['Histogram of X', num2str(i)]);
xlabel(['X', num2str(i)]);
ylabel('Probability');
grid on;
end
%--------------------------------------------------------------------------------------------------------------------
% Display generated samples
disp('Generated samples from multivariate normal distribution:');
disp(samples);