Plotting 1D Conditional Distributions with kdensity stacked in a 3D figure.

1 view (last 30 days)
Hi guys. It's my first time here and I am not a native English speaker. So, first of all, apologize.
Here is my problem: I have a T x 2 Matrix, where in the second column I have some daily financial returns and in the first column I have an indicator, which can assume integer values in the interval [1, 9].
I want to extract 9 different conditional distributions of my returns, conditioned on the values assumed by the indicator. At this point, I want to plot the conditional densities through a Gaussian smoothing with the function 'ksdensity' and plot them in the same 3D plot. The final output should be similar to this one:
I tried to reach this result by adapting the answer I found at this thread: function.
Now suppose that x = axis of returns, y = axis of indicator possible values, z = smoothed conditional densities.
My problem is that, while the meshgrid requires for all the values of y to share the same values of x by construction, I have different values of x (the returns) because of the conditioning.
I hope I have been clear.
Thanks you guys in advance, and sorry for any mistake.

Answers (1)

Alan
Alan on 29 May 2024
Hi Mishel,
The waterfall function requires grid data, and it might not be able to construct a grid using the Tx2 matrix you mention. To accomplish a waterfall plot, the following steps can be followed:
  1. Filter the Tx2 based on the indicators to form separate arrays for a given indicator. These arrays could have different lengths (I have assumed each indicator to have a random number of returns)
  2. Form ksdensity estimates from a fixed set of sample points for every indicator.
  3. Stack the ksdensity estimates for each indicator and generate a grid using meshgrid.
  4. Plot the data using the waterfal function.
I’ve created an example where the maximum returns is 1000 and the indicators are 1:9. Assume x to be analogous to the first column of T, containing the indicators, and returns to be analogous to the second column of T, containing the financial returns:
% Fix max returns as 1000
x = randi([1 9], 1, 1000);
returns = 1000.*rand(1000,1);
% Using cell arrays due to different lengths of sub arrays
returns_filtered = repmat({[]}, 1, 9);
% Filter based on indicators
for idx = 1:numel(x)
returns_filtered{x(idx)}(end+1) = returns(idx);
end
% Form pdf approximators evaluated at points 1 to max returns
returns_distribution = zeros([9 1000], "double");
for idx=1:9
returns_distribution(idx, :) = ksdensity(returns_filtered{idx}, 1:1000);
end
[x_plot, y_plot] = meshgrid(1:1000, 1:9);
p = waterfall(x_plot, y_plot, returns_distribution);
p.FaceColor = 'none';
The individual lines shown in the waterfall plot are the conditional PDF approximations for each conditions in [1 9].
I hope this will help you to achieve your solution.

Tags

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!