0001 function [yth, A ,hy ,ylist] = vb_gamma_dist_param(y, p_val)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 debug=0;
0023
0024
0025 p_0 = 2/3;
0026 rate = 0.1;
0027
0028 T = length(y);
0029 y = abs(y(:));
0030
0031 Nbin = min(5000,fix(T/10));
0032
0033
0034 [hy ,ylist] = hist(y,Nbin);
0035
0036
0037 if debug==1
0038 ymin = ylist(1)
0039 end
0040
0041
0042 py = cumsum(hy)/T;
0043
0044
0045 ix = find( py > p_0 );
0046 y0 = ylist(ix(1));
0047 jj = find(y < y0);
0048 ym = mean(y(jj));
0049 yd = sqrt(mean((y(jj)-ym).^2));
0050
0051
0052 ylow = max(ym-yd, eps);
0053 yup = ym + 2*yd;
0054
0055
0056 if debug==1
0057 fprintf('ym=%g, yup=%g, ylow=%g\n',ym, y0,ylow)
0058 end
0059
0060 ii = find((ylist <= yup));
0061
0062
0063
0064
0065
0066 z = hy(ii);
0067 x = ylist(ii);
0068
0069
0070 ix = find( z > max(z)*rate );
0071
0072 if debug==1
0073 Nhist = length(ix)
0074 end
0075
0076 x = x(ix);
0077 z = z(ix);
0078 x = x(:);
0079 z = z(:);
0080 N = length(x);
0081
0082
0083 Z = log(z);
0084 X = [ x , log(x), ones(N,1)];
0085 XX = X' * X;
0086 XZ = X' * Z;
0087 A = XX \ XZ;
0088
0089
0090
0091 errA = mean(abs(Z - X * A));
0092
0093
0094 X = [ x , ones(N,1)];
0095 XX = X' * X;
0096 XZ = X' * Z;
0097 B = XX \ XZ;
0098
0099
0100
0101 errB = mean(abs(Z - X * B));
0102
0103 if A(1) > 0 || A(2) < 0 || errA > errB
0104 fprintf('ym=%g, ylow=%g\n',ym/max(y), ylow/max(y))
0105 fprintf('Max Y for PDF = %g\n', yup/max(y))
0106 fprintf('Exponential PDF seems to be better than Gamma PDF\n')
0107 fprintf('Estimated Gamma PDF = exp( %g *y + %g *log(y) )\n',A(1:2))
0108 fprintf('Estimated Exp PDF = exp( %g *y )\n',B(1))
0109 A = [B(1) 0 B(2)];
0110 end
0111
0112 if A(1) > 0
0113 fprintf('gamma pdf estimation error\n')
0114 end
0115
0116
0117 if A(2)==0
0118 ii = find( ylist >= 0);
0119
0120 yl = ylist(ii);
0121 p = exp( A(1)*yl + A(3));
0122 else
0123 ii = find( ylist > 0);
0124
0125 yl = ylist(ii);
0126 p = exp( A(1)*yl + A(2)*log(yl) + A(3));
0127 end
0128
0129 psum = cumsum(p)/sum(p);
0130
0131
0132 ii = find(psum > 1 - p_val);
0133 yth = yl(ii(1));
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 if debug == 2
0146 ix = find( (y >= ylow) & (y <= yup));
0147 B = gamfit(y(ix));
0148 A = [ - 1/B(2) , B(1) - 1, 1];
0149 yth0 = yth;
0150 yth = gaminv(1-p_val, A(2)+1, -1/A(1))/max(y);
0151 dif_yth = (yth - yth0)
0152 end
0153
0154 return
0155
0156
0157
0158
0159 C = (y'*py) / sum(py.^2);
0160
0161 py = C * py;