-
Notifications
You must be signed in to change notification settings - Fork 0
/
OscarSeqReg.m
78 lines (65 loc) · 2.87 KB
/
OscarSeqReg.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
% X should be a matrix whose rows are the observations and columns are the
% predictors (n by p). The intercept is omitted (so the response and each
% predictor should have mean zero).
function [CoefMatrix dfMatrix SSMatrix] = OscarSeqReg(X, y, cvalues, propvalues, initcoef)
p = length(X(1,:));
% Standardize predictors to mean zero, variance 1, and center response to
% mean zero.
for i = 1:p
X(:,i) = (X(:,i)-mean(X(:,i)))/std(X(:,i));
end;
Xmatrix = [X -X]; % Need for positive and negative parts of beta
y = y-mean(y);
% Order initial estimate by magnitude, including ties.
% Initial estimate is used for first guess at ordering of coefficients and also to set maximum value
% for the bound t used in the constraint. The bound is given as proportion
% of the initial value.
[initcoeford, currorder] = sort(-abs(initcoef));
sameaslast = [0; (initcoeford(2:p) == initcoeford(1:(p-1)))];
startblocksame = [((sameaslast(2:p) - sameaslast(1:(p-1))) > 0); 0];
endblocksame = [((sameaslast(2:p) - sameaslast(1:(p-1))) < 0); sameaslast(p)];
nblocksame = sum(startblocksame);
vi = (1:p)';
visbs = vi(logical(startblocksame));
viebs = vi(logical(endblocksame));
for i = 1:nblocksame;
blockmean = mean(vi(visbs(i):viebs(i)));
vi(visbs(i):viebs(i)) = blockmean * ones(viebs(i) - visbs(i) + 1,1);
end;
[tempinvsort,vind] = sort(currorder);
a1 = vi(vind)';
initcoeford = -initcoeford;
% Grid search over c values, then by proportion of bound. For each c, the
% weighting is recomputed.
CoefMatrix = zeros(p,length(propvalues),length(cvalues));
SSMatrix = zeros(1,length(propvalues),length(cvalues));
dfMatrix = zeros(1,length(propvalues),length(cvalues));
for ccount = 1:length(cvalues)
OrderMatrix = a1;
weighting = a1;
cvalue = cvalues(ccount);
for i=1:p
weighting(i) = (1-cvalue)+cvalue*(p-i);
end;
for propcount = 1:length(propvalues)
tbound = propvalues(propcount)*weighting*initcoeford;
if (ccount == 1)
if (propcount == 1)
start=zeros(2*p,1);
end;
elseif (propcount > 1)
start=[max(CoefMatrix(:,propcount-1,ccount),0);-min(CoefMatrix(:,propcount-1,ccount),0)];
else
start=[max(CoefMatrix(:,propcount,ccount-1),0);-min(CoefMatrix(:,propcount,ccount-1),0)];
end;
[coefs df ssquares conv] = OscarSeqOpt(tbound, cvalue, Xmatrix, y, start, OrderMatrix);
CoefMatrix(:,propcount,ccount) = coefs;
SSMatrix(:,propcount,ccount) = ssquares;
dfMatrix(:,propcount,ccount) = df;
if conv == 0
fprintf('Optimization may not have converged properly for c = %g and prop = %g.\n', cvalue, propvalues(propcount));
else
fprintf('Optimization complete for c = %g and prop = %g.\n', cvalue, propvalues(propcount));
end;
end;
end;