forked from ashleylinder/ThesisCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlassoParSub2.m
79 lines (58 loc) · 1.85 KB
/
lassoParSub2.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
79
function fold = lassoParSub2(dataFolder,newNA)
%%set up data
numTrial = 10;
fileName = [dataFolder filesep 'fixed_data.mat'];
load(fileName)
newRatio2 = newNA;
mode3 = smooth(behaviorZ,20);
dtheta = eigAngleFilt;
nNeuron = length(newRatio2(:,1));
if nNeuron==0
smoothNA = 0;
else
for i =1:nNeuron
smoothNA(i,:) = smooth(newRatio2(i,:),10);
end
end
cutPoint = floor(.6*length(eigAngleFilt));
%% bin dtheta/dt, folds are generated by sampling these bins
binNum = 5;
goalPts = ceil((.1*cutPoint)/binNum);
train = eigAngleFilt(1:cutPoint);
h_train = histogram(train,binNum);
edgesTrain = h_train.BinEdges;
[minCount, minIdx] = min(h_train.Values);
%if there are too few points in the smallest bin, use 4 bins instead of 5
if minCount < goalPts
binNum = 4;
h_train = histogram(train,binNum);
end
[minCount, minIdx] = min(h_train.Values);
edgesTrain = h_train.BinEdges;
percPick = goalPts/minCount;
numPts = ceil(percPick*minCount);
for i = 1:binNum
bin(i).allPtsTrain = find(train>edgesTrain(i) & train< edgesTrain(i+1));
end
%% Generate folds
%folds are generated by random sampling from each of the bins
for j = 1:numTrial
for i = 1:binNum
temp = randperm(length(bin(i).allPtsTrain));
binPts(i,:) = bin(i).allPtsTrain(temp(1:numPts));
end
trainPts(j,:) = sort(reshape(binPts,[1, binNum*numPts]));
end
%% Run lasso
for k = 1:numTrial
training = trainPts(k,:);
alpha = 0.001:.5:1;
alpha = [alpha 1];
timeStep = 1:10;
for j = 1:length(alpha)
for dt = 1:length(timeStep)
[fold(k).alph(j).regress(dt).WeightsdTheta,fold(k).alph(j).regress(dt).FitdTheta,fold(k).alph(j).regress(dt).WeightsMode3,fold(k).alph(j).regress(dt).FitMode3, fold(k).alph(j).regress(dt).DataMat] = lassoPickPts(smoothNA, timeStep(dt), dtheta, mode3, alpha(j),training);
end
end
end
save([dataFolder filesep 'lassoOutput2.mat'], 'fold', '-v7.3');