bootvar.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. function varargout=bootvar(varargin)
  2. % [p,ci]=bootvar(A,B,['boots'])
  3. % When passed in a single vector, tests whether the variance is different from 1
  4. % When passed in two vectors does a permutation test to see whether the variances
  5. % are significantly different
  6. % Optional inputs:
  7. % 'boots' the number of shuffles to perform (5000)
  8. if isnumeric(varargin{2})
  9. A=varargin{1};
  10. B=varargin{2};
  11. one_dist=false;
  12. if nargin>2
  13. varargin=varargin(3:end);
  14. else
  15. varargin={};
  16. end
  17. else
  18. A=varargin{1};
  19. one_dist=true;
  20. if nargin>1
  21. varargin=varargin(2:end);
  22. else
  23. varargin={};
  24. end
  25. end
  26. boots=5000;
  27. utils.overridedefaults(who,varargin);
  28. if one_dist
  29. % assume test whether variance of the population is differenct from one.
  30. dist=A;
  31. if isvector(dist)
  32. dist=dist(:);
  33. end
  34. n=size(dist,1);
  35. [B]=bootstrp(boots, @nanvar, varargin{1});
  36. ps=[0:0.01:100];
  37. sd_ps=prctile(B,ps);
  38. sd_p= stats.get_p(1,B);
  39. varargout{1}=sd_p;
  40. varargout{2}=prctile(B,[2.5 97.5]);
  41. varargout{3}=B;
  42. elseif ~one_dist
  43. sA=size(A,1);
  44. sB=size(B,1);
  45. sd=nanvar(A)-nanvar(B);
  46. if min(sA,sB)<=7
  47. warning('Not meaningful to compute 5000 bootstraps when n=<7');
  48. boots=factorial(min(sA,sB));
  49. end
  50. ALL_DATA=[A;B];
  51. boot_score=zeros(boots,size(ALL_DATA,2));
  52. for bx=1:boots
  53. shuff_d=ALL_DATA(randperm(sA+sB),:);
  54. A=shuff_d(1:sA,:);
  55. B=shuff_d(sA+1:end,:);
  56. boot_score(bx,:)=nanvar(A)-nanvar(B);
  57. end
  58. sd_p= stats.get_p(sd, boot_score);
  59. end
  60. varargout{1}=sd_p;
  61. varargout{2}=prctile(B,[2.5 97.5]);