-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_tree.m
More file actions
103 lines (78 loc) · 2.75 KB
/
Copy pathcreate_tree.m
File metadata and controls
103 lines (78 loc) · 2.75 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function tree = create_tree(numberGeneration, offspringDistribution, ...
terminalNodeDistribution, plot)
% CREATE_TREE - returns a Galton-Watson tree of given height in TODO format
% with the branching probabilities given by offspringDistribution and
% terminal node values from terminalNodeDistribution
%
% Inputs:
% numberGeneration - cutoff for tree height. Should be an even number
% offspringDistribution - a structure with a 'type' field, and a 'params'
% field.
% terminalNodeDistribution - a structure with a 'type' field, and a 'params'
% field.
%
% Outputs:
% tree - a structure containing TODO arrays
%
% Example:
% tree1 = create_tree(10, 'geometric')
% returns TODO
% tree1 = create_tree(10, [0,0,1])
% returns TODO
% Based on code by: R.Gaigalas, I.Kaj
parents = 0; % convention: root node has parent 0
% extinct = 0; % tree not extinct
numberKids = 1; % root nodes is a child
for generation = 1:numberGeneration
% kids in previous generation are now parents, with indices
% parentIndices. sample is an array representing the number of kids the
% parents have. We reset the total number of kids.
numberParents = numberKids;
parentIndices = length(parents) - numberKids + 1: length(parents);
sample = random_sample(offspringDistribution, numberParents);
numberKids = 0;
for j = 1:max(sample)
parentsWithJChildren = parentIndices(sample == j);
if ~isempty(parentsWithJChildren)
parents = [parents repmat(parentsWithJChildren, 1, j)]; %TODO avoid changing array size
numberKids = numberKids + length(parentsWithJChildren) * j;
end
end
% if (numberKids == 0)
% extinct = 1;
% break
% end
end
values = NaN(1, length(parents));
value_sample = random_sample(terminalNodeDistribution, numberKids);
values(length(parents) - numberKids + 1: length(parents))=value_sample;
tree.parents = parents;
tree.values = values;
tree = find_children(tree);
% if (extinct)
% fprintf('Extinct in generation %d\n', generation);
% else
% fprintf('Stopped in generation %d\n', numberGeneration);
% end
if plot
close all;
addpath('~/Code/MinimaxProjectCode/ExternalPackages/rtree-2.1/rtree');
index = find(~isnan(tree.values), 1);
[x,y] = trimtreelayout(tree.parents);
x = x(index:end);
y = y(index:end);
labels = strtrim(cellstr(num2str(tree.values(index:end)'))');
figure;
trimtreeplot(tree.parents);
text(x,y+.02,labels);
end
end
function treeWithChildren = find_children(tree)
nNodes = length(tree.parents);
children = cell(1,nNodes);
for i = 2: nNodes
children{tree.parents(i)} = [children{tree.parents(i)}, i];
end
tree.children = children;
treeWithChildren = tree;
end