Search code examples
matlabplotstatisticsprobabilityprobability-distribution

how can i shade the rejection region in matlab for f distribution?


i am required to plot an f-distribution for the given degrees of freedom v1 and v2 determined from 4 samples and shade the rejection region for the given alpha(like in the picture below) rejection region shading however i cannot figure out how to do so and anything i tried doesn't seem to work. attached is my code so far.

clear
clc
format short g

%% Samples

s1=[407 411 409];
s2=[404 406 408 405 402];
s3=[410 408 406 408];
s4=[400 413 407 405 403 410 409];

observations=[s1 s2 s3 s4];

%% degrees of freedom
n=4; %no of samples
m=length(observations);

v1=n-1
v2=m-n

%% Level of sig
alpha=0.05
level_of_sig=1-alpha
critical_value=finv(level_of_sig,v1,v2)

%% Plotting F-Dist

x=0:0.01:max(observations); 
fdist=fpdf(x,v1,v2);

fig1 = figure(1);
hold on
plot(x,fdist,'LineWidth',1.5)
xline(0,'Color', [0.5 0.5 0.5])
yline(0,'Color', [0.5 0.5 0.5])
grid on

% shading rejection region <<<<<<<<<<<<<<<< not working
grey  = [127 127 127]./255;
area(x(critical_value:max(observations)),fdist(critical_value:max(observations)),'basevalue',0,'FaceColor',grey);
hold off

Solution

  • Shading a Specific Region of a Plot Using a Logical Array

    Not sure which variable determines the start of the shading but here is something that might be similar to what you're going for. Here a logical array named Region_Indices is created based on a condition that is specified as a range. This logical array, Region_Indices is then used to matrix index x and fdist arrays corresponding to the plot. The area() function is then used to shade the region corresponding to these index coordinates of the plot. In this case I set the Shaded_Region_Start (start of the shaded region) to the critical_value but I'm not totally sure the correct value you expect to use from your script.

    Shaded Region of a Plot

    Code Snippet:

    grey  = [127 127 127]./255;
    
    Shaded_Region_Start = critical_value;
    Region_Indices = x>Shaded_Region_Start & x<=max(x);
    area(x(Region_Indices),fdist(Region_Indices),'FaceColor',grey);
    xlim([0 6]);
    hold off
    

    Full Script:

    clear
    clc
    format short g
    
    %% Samples
    
    s1=[407 411 409];
    s2=[404 406 408 405 402];
    s3=[410 408 406 408];
    s4=[400 413 407 405 403 410 409];
    
    observations=[s1 s2 s3 s4];
    
    %% degrees of freedom
    n=4; %no of samples
    m=length(observations);
    
    v1=n-1;
    v2=m-n;
    
    %% Level of sig
    alpha=0.05;
    level_of_sig=1-alpha;
    critical_value=finv(level_of_sig,v1,v2);
    
    %% Plotting F-Dist
    
    x=0:0.01:max(observations); 
    fdist=fpdf(x,v1,v2);
    
    fig1 = figure(1);
    hold on
    plot(x,fdist,'LineWidth',1.5);
    xline(0,'Color', [0.5 0.5 0.5]);
    yline(0,'Color', [0.5 0.5 0.5]);
    grid on
    
    % shading rejection region <<<<<<<<<<<<<<<< not working
    grey  = [127 127 127]./255;
    
    Shaded_Region_Start = critical_value;
    Region_Indices = x>Shaded_Region_Start & x<=max(x);
    area(x(Region_Indices),fdist(Region_Indices),'FaceColor',grey);
    xlim([0 6]);
    hold off