Search code examples
matlabcontourdensity-plot

Graphically represent density of iterations


Kind all,

I am working in MATLAB and I'm using Monte Carlo techniques to fit a model. Basically, if we assume that my model is a simple function such as

y=m*x^2+c

And that both my parameters m and c vary between 0.5 and 10, I may randomly draw from such a parameter space and obtain each time a new y. If I plot all my realizations of y I obtain something similar to the following figure:

Multiple Iterations of y, colours are drawn by the hsv colormap

Is there a way to represent the DENSITY of the realizations? I mean, is there a way (instead of plotting all the realizations) to obtain some kind of contour plot that lies between the minimum of my iterations and the maximum for which its color represents the amount of realizations that fall within a certain interval?

Thanks all!


Solution

  • You can calculate y for discrete points of x, while setting random values for c and m. Then using hist function you can find a "not-normalized density" of function values for a given x. You can then normalize it to get a real density of the values, so that the overall area under the distribution curve sums up to 1.

    In order to visualize it, you construct a mesh grid [X, Y] along the values of x and y and put the density values as Z. Now you can either plot the surf or its contour plot.

    enter image description here

    enter image description here

    enter image description here

    Here is the code:

    clear;
    
    n = 1000000; %number of simulation steps
    
    %parameter ranges
    m_min = 0.5; m_max = 10;
    c_min = 0.5; c_max = 10;
    
    %x points
    x_min = 1; x_max = 4; x_count = 100;
    x = linspace(x_min, x_max, x_count);
    x2 = x.^2;
    
    y_min = 0; y_max = m_max*x_max*x_max + c_max; y_step = 1;
    
    m = rand(n, 1)*(m_max - m_min) + m_min;
    c = rand(n, 1)*(c_max - c_min) + c_min;
    
    c = repmat(c, 1, x_count);
    
    y = m*x2 + c;
    
    x_step = (x_max- x_min)/(x_count-1);
    [X, Y] = meshgrid(x_min:x_step:x_max, y_min-y_step:y_step:y_max+y_step);
    Z = zeros(size(X));
    
    bins = y_min:y_step:y_max;
    
    for i=1:x_count
    
        [n_hist,y_hist] = hist(y(:, i), bins);
    
        %add zeros on both sides to close the profile
        n_hist = [0 n_hist 0];
        y_hist = [y_min-y_step   y_hist   y_max+y_step];
    
        %normalization
        S = trapz(y_hist,n_hist); %area under the bow
        n_hist = n_hist/S; %scaling of the bow
    
        Z(:, i) = n_hist';
    end
    
    surf(X, Y, Z, 'EdgeColor','none');
    colormap jet;
    xlim([x_min x_max]);
    ylim([y_min y_max]);
    xlabel('X');
    ylabel('Y');
    
    figure
    contour(X,Y,Z, 20);
    colormap jet;
    colorbar;
    grid on;
    title('Density as function of X');
    xlabel('X');
    ylabel('Y');
    

    Another interesting view is a plot of each section depending on the x value:

    enter image description here

    Here is the code for this plot:

    clear;
    
    n = 1000000; %number of simulation steps
    
    %parameter ranges
    m_min = 0.5; m_max = 10;
    c_min = 0.5; c_max = 10;
    
    %x points
    x_min = 1; x_max = 4; x_count = 12;
    x = linspace(x_min, x_max, x_count);
    x2 = x.^2;
    
    m = rand(n, 1)*(m_max - m_min) + m_min;
    c = rand(n, 1)*(c_max - c_min) + c_min;
    
    c = repmat(c, 1, x_count);
    
    y = m*x2 + c;
    
    %colors for the plot
    colors = ...
    [ 0 0 1; 0 1 0; 1 0 0; 0 1 1; 1 0 1; 0 0.75 0.75; 0 0.5 0; 0.75 0.75 0; ...
    1 0.50 0.25; 0.75 0 0.75; 0.7 0.7 0.7; 0.8 0.7 0.6; 0.6 0.5 0.4; 1 1 0; 0 0 0 ];
    
    %container for legend entries
    legend_list = cell(1, x_count);
    
    for i=1:x_count
        bin_number = 30; %number of histogramm bins
        [n_hist,y_hist] = hist(y(:, i), bin_number);
        n_hist(1) = 0; n_hist(end) = 0; %set first and last values to zero
    
        %normalization
        S = trapz(y_hist,n_hist); %area under the bow
        n_hist = n_hist/S; %scaling of the bow
        plot(y_hist,n_hist, 'Color', colors(i, :), 'LineWidth', 2);
        hold on;
    
        legend_list{i} = sprintf('Plot of x = %2.2f', x(i));
    end
    
    xlabel('y');
    ylabel('pdf(y)');
    legend(legend_list);
    title('Density depending on x');
    grid on;
    hold off;