[95bb1e]: / MLFre / gen_sync1.m

Download this file

72 lines (60 with data), 1.9 kB

 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
function [ X, y, beta, ind ] = gen_sync1( p, N, d, nns, ratio )
%% generate synthetic data with zero pair-wise correlation
nl = p./nns; % the lenght of nl is d+1; n[end] = 1 as we only has
% one root node; nl[1] is the number of nodes at d
% layer; nl[2] is the number of nodes at d-1 layer
% construct sparse matrix to vectorize the computation
gind = zeros(d,p);
for l = 1:d
gind(l,nns(l):nns(l):p) = 1;
gind(l,:) = nl(l)+1-cumsum(gind(l,:),'reverse');
end
Gind = cell(1,d);
if nl(1)==p
j=2;
else
j=1;
end
for l=j:d
Gind{l}=sparse(gind(l,:),1:p,ones(1,p),nl(l),p);
end
% generate the data matrix
mu = zeros(1,p);
SIGMA = speye(p);
X = mvnrnd(mu,SIGMA,N);
% generate the response
T = true(p,1);
for l = 1:d
if l==1&&nl(1)==p
Tf = unifrnd(0,1,[p,1])<=ratio(l);
else
Tl = unifrnd(0,1,[nl(l),1])<=ratio(l);
Tf = logical((Gind{l})'*Tl);
end
T = T&Tf;
end
beta = zeros(p,1);
beta(T) = normrnd(0,1,[nnz(T),1]);
y = X*beta + 0.01*normrnd(0,1,[N,1]);
% construct ind
if nl(1) == p % if the nodes at the first layer only has one feature
cumnl = [0,1,cumsum(nl(2:end))+1];
ind = zeros(3,cumnl(end));
ind(:,1)=[-1, -1, 1]';
ind(:,end)=[1,p,0]';
for i = 2:d
ind(1,cumnl(i)+1:cumnl(i+1)) = 1:nns(i):p;
ind(2,cumnl(i)+1:cumnl(i+1)) = ind(1,cumnl(i)+1:cumnl(i+1))+nns(i)-1;
ind(3,cumnl(i)+1:cumnl(i+1)) = sqrt(nns(i));
end
else
cumnl = [0,cumsum(nl)];
ind = zeros(3,cumnl(end));
ind(:,end)=[1,p,0]';
for i = 1:d
ind(1,cumnl(i)+1:cumnl(i+1)) = 1:nns(i):p;
ind(2,cumnl(i)+1:cumnl(i+1)) = ind(1,cumnl(i)+1:cumnl(i+1)) + nns(i);
ind(3,cumnl(i)+1:cumnl(i+1)) = sqrt(nns(i));
end
end
end