Code covered by the BSD License  

Highlights from
power1var

from power1var by Antonio Trujillo-Ortiz
Power and test concerning one-sample variance.

power1var(s2,n,s2h,a)
function [power1var] = power1var(s2,n,s2h,a)
%Power and test concerning one-sample variance. 
% (Besides the power estimation it makes the hypothesis testing.)
%
%   Syntax: function [power1var] = power1var(s2,n,s2h,a) 
%      
%     Inputs:
%         s2 - sample variance.
%          n - sample size.
%        s2h - hypothesized value.
%          a - significance level (default = 0.05).
%     Output (table):
%          Size.
%          Variance.
%          Hypothesized value.
%          Chi-squared statistic.
%          Degrees of freedom.
%          P - Probability that null Ho: is true.
%          Statistical power.
%
%    Example: From the exercise 7.7(c,e) of Zar (1999, p.121) for a one-tailed variance
%             hypothesis test with a significance level = 0.05 (n = 18; sample 
%             variance = 6.4512 and hypothesized value = 4.4).
%                                       
%     Calling on Matlab the function: 
%             power1var(6.4512,18,4.4)
%       Immediately it appears:
%             -One-tailed test (1) or Two-tailed test (2):
%             That for this example we put after the colon, 1
%
%       Answer is:
%
%    It is a right-hand tail hypothesis. 
%
%    ------------------------------------------------------------------------------------
%     Size    Variance    Hypothesized value        X2         df         P         Power
%    ------------------------------------------------------------------------------------
%       18      6.4512         4.4000            24.9251       17      0.0964      0.3392
%    ------------------------------------------------------------------------------------
%    The sample variance is not statistically significative.
%

%  Created by A. Trujillo-Ortiz and R. Hernandez-Walls
%             Facultad de Ciencias Marinas
%             Universidad Autonoma de Baja California
%             Apdo. Postal 453
%             Ensenada, Baja California
%             Mexico.
%             atrujo@uabc.mx
%
%  December 27, 2002.
%
%  To cite this file, this would be an appropriate format:
%  Trujillo-Ortiz, A. and R. Hernandez-Walls. (2002). power1var: Power and 
%    test concerning one-sample variance. A MATLAB file. [WWW document]. 
%    URL http://www.mathworks.com/matlabcentral/fileexchange/
%    loadFile.do?objectId=2892&objectType=file
%
%  References:
% 
%  Zar, J. H. (1999), Biostatistical Analysis. 4th. ed.   
%           New-Jersey:Upper Saddle River. p. 112,114,121,Answers to Exercises(Ans2).

if nargin < 4, 
    a = 0.05; 
end 

if nargin < 3, 
    error('Requires at least three input arguments.'); 
end 

df=n-1;
X2=(df*s2)/s2h;
r=s2h/s2;
     

con=input('One-tailed test (1) or Two-tailed test (2):');     
disp(' ')
if con==1
   a=a;
   if s2>s2h;
      disp('It is a right-hand tail hypothesis.');
                   
      P=1-chi2cdf(X2,df);
      Power=1-(chi2cdf(chi2inv(1-a,df)*r,df));
disp(' ')
fprintf('------------------------------------------------------------------------------------\n');
disp(' Size    Variance    Hypothesized value        X2         df         P         Power')
fprintf('------------------------------------------------------------------------------------\n');
fprintf('%5.i%12.4f%15.4f%19.4f%9.i%12.4f%12.4f\n\n',n,s2,s2h,X2,df,P,Power);
fprintf('------------------------------------------------------------------------------------\n');
           
        if P >= a;
           disp('The sample variance is not statistically significative.');
        else
           disp('The sample variance is statistically significative.');
        end
   

   else s2<s2h;
      disp('It is a left-hand tail hypothesis.');
       
      P=chi2cdf(X2,df);
      Power=chi2cdf(chi2inv(a,df)*r,df);
disp(' ')
fprintf('------------------------------------------------------------------------------------\n');
disp(' Size    Variance    Hypothesized value        X2         df         P         Power')
fprintf('------------------------------------------------------------------------------------\n');
fprintf('%5.i%12.4f%15.4f%19.4f%9.i%12.4f%12.4f\n\n',n,s2,s2h,X2,df,P,Power);
fprintf('------------------------------------------------------------------------------------\n');

        if P >= a;
           disp('The sample variance is not statistically significative.');
        else
           disp('The sample variance is statistically significative.');
        end
   
     
   end 
else   
   a=a/2;
   
      P1=1-chi2cdf(X2,df);
      P2=chi2cdf(X2,df);
      Power=(1-chi2cdf((chi2inv(1-a,df)*r),df))+ chi2cdf((chi2inv(a,df)*r),df);     
disp(' ')
fprintf('----------------------------------------------------------------------------------------\n');
disp(' Size    Variance    Hypothesized value        X2         df      P1       P2      Power')
fprintf('----------------------------------------------------------------------------------------\n');
fprintf('%5.i%12.4f%15.4f%19.4f%9.i%10.4f%9.4f%9.4f\n\n',n,s2,s2h,X2,df,P1,P2,Power);
fprintf('----------------------------------------------------------------------------------------\n');
          
        if (P1 < a | P2 > a);
           disp('The sample variance is not statistically significative.');
        else
           disp('The sample variance is statistically significative.');
        end
           
end  

Contact us at files@mathworks.com