0

I've a contour plot with code

% This function plots the contours of likelihood values on the scatter plot of a 2 dimensional data.

    function [xgrid,ygrid,Z] = biVariateContourPlotsGMMCopula(givenData,gmmObject,~,numMeshPoints,x_dim,y_dim)
    
%INPUT: givenData (MxN, M=number of points, N=Dimension)
%     : plo = binary variable (1 plot contour plot, 0 do not plot)
%OUTPUT: xgrid,ygrid,Z ( Z contains the likelihood values of the points defined by xgrid and ygrid)
    
%load general_info;

    d = 2;
       if nargin < 5
           x_dim = 1;
           y_dim = 2;
       end
    
    if x_dim == y_dim
        hist(givenData(:,x_dim),10);
        return;
    end
    
    numMeshPoints = min(numMeshPoints,256);
    
    givenData = givenData(:,[x_dim y_dim]);
    alpha = gmmObject.alpha;
    mu = gmmObject.mu(:,[x_dim y_dim]);
    sigma = gmmObject.sigma([x_dim y_dim],[x_dim y_dim],:) + 0.005*repmat(eye(d),[1 1 numel(alpha)]);
    
    gmmObject = gmdistribution(mu,sigma,alpha);
    
    bin_num = 256;
    for j = 1:2
       l_limit = min(gmmObject.mu(:,j))-3*(max(gmmObject.Sigma(j,j,:))^0.5);
       u_limit = max(gmmObject.mu(:,j))+3*(max(gmmObject.Sigma(j,j,:))^0.5);
       xmesh_inverse_space{j} = (l_limit:(u_limit-l_limit)/(bin_num-1):u_limit);
    end
    
    
%if isempty(xmesh)||isempty(pdensity)||isempty(cdensity)
% Following for loop does the non-parameteric estimation of marginal % densities if not provided

    for i = 1:d
        currentVar = givenData(:,i);       
 
% finding non-parametric PDF values at prespecified mesh points

        [~,pdensity{i},xmesh{i}]=kde(currentVar,numMeshPoints);
        pdensity{i}(pdensity{i}<0) = 0;
    
% finding non-parametric CDF values
        cdensity{i} = cumsum(pdensity{i});
        cdensity{i} = (cdensity{i}-min(cdensity{i}))/(max(cdensity{i})-min(cdensity{i})); % scaling the cdensity value to be between [0 1]
     end

%end
    
    [xgrid,ygrid] = meshgrid(xmesh{1}(2:end-1),xmesh{2}(2:end-1));
    
    for k = 1:d
        marginalLogLikelihood_grid{k} = log(pdensity{k}(2:end-1)+eps);
        marginalCDFValues_grid{k} = cdensity{k}(2:end-1);
    end
    [marg1,marg2] = meshgrid(marginalLogLikelihood_grid{1},marginalLogLikelihood_grid{2});
    
    [xg,yg] = meshgrid(marginalCDFValues_grid{1},marginalCDFValues_grid{2});
    inputMatrix = [reshape(xg,numel(xg),1) reshape(yg,numel(yg),1)];
    
    
    copulaLogLikelihoodVals = gmmCopulaPDF(inputMatrix,gmmObject,xmesh_inverse_space);
    Z = reshape(copulaLogLikelihoodVals,size(marg1,1),size(marg1,2));
    Z = Z+marg1+marg2;
    
% Getting the likelihood value from the log-likelihood.

    Z = exp(Z); 
    
    plot(givenData(:,1),givenData(:,2),'b.','MarkerSize',3);hold
    contour(xgrid,ygrid,Z,40,'EdgeColor',[1 0 0]);
    
%title_string = ['GMCM fit (Log-Likelihood = ',num2str(logLikelihoodVal), ')'];
%title(title_string,'FontSize',12,'FontWeight','demi');

    axis tight;

There are data points inside and outside the contour line and I want to eliminate those points lying outside the contour line with level 4e-5.

These are the various Z values that I'm getting when plotting it with a value for Z=40 in the plot figure.figure showing z values

Current Plot: current plot

Desired Plot: example of the plot to be generated

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • 1
    I assume you are calling `scatter` or `plot` too, for the blue dots. You need to remove the points from that call, so withotu a [mcve], we can't help. – Ander Biguri Jan 23 '23 at 10:14
  • Thank you for the response. I've updated the body of the question with the entire function. Is this enough? – Arjun Krishnan Jan 23 '23 at 10:29
  • A bit too much, but we can work with it ;) Basically what we need is the "Z" values for `givenData`. If you are able to provide that, then its an easy solution, as you can just do `givendata2=givendata(givendataZ<4e-5,:)` and then plot that – Ander Biguri Jan 23 '23 at 10:33
  • I've added a new figure when I give 40 as the number of contour lines for the plot. Is this what you mean? – Arjun Krishnan Jan 23 '23 at 11:48
  • 1
    nope. the poitsn have some value of Z attached to them, whatever Z is, it exists in the locations of the points. that is what you need to know to be able to discard points with higher Z than 4e-5, as you asked – Ander Biguri Jan 23 '23 at 11:55
  • Possibly related: [How to superimpose two contour maps onto each other in matlab?](https://stackoverflow.com/a/57007211/8239061) – SecretAgentMan Jan 24 '23 at 18:27

0 Answers0