# Commit b2c1ecfef4b4d168c196a9340553ef7a8e37c3ff

- Diff rendering mode:
- inline
- side by side

orthogonal_sample.m

(192 / 0)

1 | % orthogonal_sample.m - creates an orthogonal sample of 1:k*m in N dimensions | ||

2 | % | ||

3 | % USAGE: | ||

4 | % | ||

5 | % [S, m] = orthogonal_sample(N,k,n, [DEBUG]) | ||

6 | % | ||

7 | % where the parameters are all scalar: | ||

8 | % | ||

9 | % N = number of dimensions | ||

10 | % k = number of "large" subdivisions (subspaces) per dimension | ||

11 | % n = number of samples to place in each subspace | ||

12 | % | ||

13 | % DEBUG: if this parameter exists, the results (projected into the first two dimensions | ||

14 | % by discarding the other dimensions) will be plotted. | ||

15 | % | ||

16 | % Allowed ranges: | ||

17 | % | ||

18 | % N >= 1, N integer | ||

19 | % k >= 1, k integer | ||

20 | % n >= 1, n integer | ||

21 | % | ||

22 | % If k = 1, the algorithm reduces to Latin hypercube sampling. | ||

23 | % If N = 1, the algorithm simply produces a random permutation of 1:k. | ||

24 | % | ||

25 | % Let m = n*k^(N-1) the number of bins for one variable in one subspace. | ||

26 | % The total number of samples is always exactly k*m. | ||

27 | % Each component of a sample can take on values 1, 2, ..., k*m. | ||

28 | % | ||

29 | % S (output) = samples; (k*m)-by-N matrix where each row is an N-tuple of numbers in 1:k*m. | ||

30 | % m (output) = number of bins per parameter in one subspace. | ||

31 | % (This is always n*k^(N-1), but is provided as output for convenience.) | ||

32 | % | ||

33 | % See e.g. Wikipedia for a description of both orthogonal and Latin hypercube sampling: | ||

34 | % http://en.wikipedia.org/wiki/Latin_hypercube_sampling | ||

35 | % | ||

36 | % Briefly, this is a bit like designing the first stage of a Sudoku puzzle: each subspace must | ||

37 | % have exactly the same number of samples, and no two samples may occur on the same hyperplane. | ||

38 | % | ||

39 | % Examples: | ||

40 | % | ||

41 | % N = 2 dimensions, k = 3 subspaces per axis, n = 1 sample per subspace. m will be n*k^(N-1) = 1 * 3^(2-1) = 3. | ||

42 | % [S,m] = orthogonal_sample(2, 3, 1, 'show') | ||

43 | % | ||

44 | % Compare the previous example with this Latin hypercube that has 9 samples in total: | ||

45 | % (We choose 9 because in the previous example, k*m = 3*3 = 9.) | ||

46 | % [S,m] = orthogonal_sample(2, 1, 9, 'show') | ||

47 | % | ||

48 | % JJ 2010-09-23 | ||

49 | % | ||

50 | function [S, m] = orthogonal_sample(N,k,n,DEBUG) | ||

51 | % Parameter parsing. | ||

52 | % | ||

53 | if nargin < 3 || N < 1 || k < 1 || n < 1 | ||

54 | fprintf(1,'Invalid parameters. Please see "help orthogonal_sample" for instructions.\n'); | ||

55 | S = []; | ||

56 | m = 0; | ||

57 | return; | ||

58 | end | ||

59 | |||

60 | if nargin > 3 | ||

61 | debug_vis = 1; | ||

62 | end | ||

63 | |||

64 | % Bullet-proof the parameters. | ||

65 | if N - floor(N) ~= 0 | ||

66 | fprintf(1,'orthogonal_sample.m: WARNING: non-integer N = %0.3g; discarding decimal part\n', N); | ||

67 | N = floor(N); | ||

68 | end | ||

69 | if k - floor(k) ~= 0 | ||

70 | fprintf(1,'orthogonal_sample.m: WARNING: non-integer k = %0.3g; discarding decimal part\n', k); | ||

71 | k = floor(k); | ||

72 | end | ||

73 | if n - floor(n) ~= 0 | ||

74 | fprintf(1,'orthogonal_sample.m: WARNING: non-integer n = %0.3g; discarding decimal part\n', n); | ||

75 | n = floor(n); | ||

76 | end | ||

77 | |||

78 | % Discussion. | ||

79 | |||

80 | % Proof that the following algorithm implements orthogonal sampling: | ||

81 | % | ||

82 | % * Orthogonal sampling has two properties: Latin hypercube sampling globally, and equal density in each subspace. | ||

83 | % * The independent index vector generation for each parameter guarantees the Latin hypercube property: some numbers will have been used, and removed from the index vectors, when the next subspace along the same hyperplane is reached. Thus, the same indices cannot be used again for any such subspace. This process continues until each index has been used exactly once. | ||

84 | % * The orthogonal sampling property is enforced by the fact that each subspace gets exactly one sample generated in one run of the loop. The total number of samples is, by design, divisible by the number of these subspaces. Therefore, each subspace will have the same sample density. | ||

85 | |||

86 | % Run time and memory cost: | ||

87 | % | ||

88 | % * Exactly k*m samples will be generated. This can be seen from the fact that there are k*m bins per parameter, and they all get filled by exactly one sample. | ||

89 | % * Thus, runtime is in O(k*m) = O( k * n*k^(N-1) ) = O( n*k^N ). (This isn't as bad as it looks. All it's saying is that a linear number of bins gets filled. This is much less than the total number of bins (k*m)^N - which is why orthogonal sampling is needed in the first place. Orthogonal sampling gives us a reduction in sample count by the factor (k*m)^(N-1).) | ||

90 | % * Required memory for the final result is (k*m)*N reals (plus some overhead), where the N comes from the fact that each N-tuple generated has N elements. Note that the index vectors also use up k*m*N reals in total (k*N vectors, each with m elements). Thus the memory cost is 2*k*m*N reals plus overhead. | ||

91 | % * Note that using a more complicated implementation that frees the elements of the index vectors as they are used up probably wouldn't help with the memory usage, because many vector implementations never decrease their storage space even if elements are deleted. | ||

92 | % * In other programming languages, one might work around this by using linked lists instead of vectors, and arranging the memory allocations for the elements in a very special way (i.e. such that the last ones in memory always get deleted first). By using a linked list for the results, too, and allocating them over the deleted elements of the index vectors (since they shrink at exactly the same rate the results grow), one might be able to bring down the memory usage to k*m*N plus overhead. | ||

93 | % * Finally, note that in practical situations N, k and m are usually small, so the factor of 2 doesn't really matter. | ||

94 | |||

95 | % Algorithm. | ||

96 | |||

97 | % Find necessary number of bins per subspace so that equal nonzero density is possible. | ||

98 | % A brief analysis shows that in order to exactly fill up all k*m bins for one variable, | ||

99 | % we must have k*m = n*k^N, i.e... | ||

100 | m = n*k^(N-1); | ||

101 | |||

102 | % Create index vectors for each subspace for each parameter. (There are k*N of these.) | ||

103 | I = zeros(N,k,m); | ||

104 | for i = 1:N | ||

105 | for j = 1:k | ||

106 | % We create random permutations of 1:m here so that in the sampling loop | ||

107 | % we may simply pick the first element from each vector. This is conceptually | ||

108 | % somewhat clearer than the alternative of picking a random element from an ordered vector. | ||

109 | % | ||

110 | % Note that the permutation property ensures that each integer in 1:m only appears once in each vector. | ||

111 | I(i,j,:) = randperm(m); | ||

112 | end | ||

113 | end | ||

114 | |||

115 | % Start with an empty result set. We will place the generated samples here. | ||

116 | S = []; | ||

117 | |||

118 | L = k*m; % number of samples still waiting for placement | ||

119 | Ns = k^N; % number of subspaces in total (cartesian product of k subspaces per axis in N dimensions) | ||

120 | while L > 0 | ||

121 | % Loop over all subspaces, placing one sample in each. | ||

122 | for j = 1:Ns % index subspaces linearly | ||

123 | % Find, in each dimension, which subspace we are in. | ||

124 | pj = zeros(N); % multi-index (vector containing an index in each dimension) for this subspace | ||

125 | % Simple example: (N,k,n) = (2,3,1) --> pj = 1 1, 2 1, 3 1, 1 2, 2 2, 3 2, 1 3, 2 3, 3 3 | ||

126 | pj = 1 + mod( floor( (j-1) ./ k.^(0:N-1) ), k ); | ||

127 | |||

128 | % Construct one sample point. | ||

129 | % Note: we must use a for loop because ":" doesn't allow to specify two indices as the same. | ||

130 | % FIXME: better ways to do this? | ||

131 | % | ||

132 | s = zeros(1,N); | ||

133 | for i = 1:N | ||

134 | s(i) = I(i, pj(i), 1); % grab first element in the corresponding index vector | ||

135 | I(i,pj(i),:) = cat(3, I(i,pj(i),2:end), -1); % remove the used element, bump others toward the front | ||

136 | end | ||

137 | |||

138 | % Now s contains a sample from (1:m)^N. By its construction, | ||

139 | % the sample conforms to the Latin hypercube requirement. | ||

140 | |||

141 | % Compute base index along each dimension: the first element of the current subspace | ||

142 | % is at the multi-index (a+1) (where the +1 is scalar, added to each element of a) | ||

143 | % | ||

144 | a = (pj-1)*m; | ||

145 | |||

146 | % Add the new sample to the result set. | ||

147 | S = [ S ; a+s ]; | ||

148 | end | ||

149 | |||

150 | % Note that we placed exactly Ns samples during the for loop. | ||

151 | L = L - Ns; | ||

152 | end | ||

153 | |||

154 | % Result visualization (for debug and illustrative purposes) | ||

155 | % | ||

156 | if debug_vis && N > 1 | ||

157 | figure(1); | ||

158 | clf; | ||

159 | |||

160 | % Plot the picked points | ||

161 | plot(S(:,1), S(:,2),'bo'); | ||

162 | axmax = k*m + 1; | ||

163 | |||

164 | hold on; | ||

165 | |||

166 | % Mark the subspaces onto the figure | ||

167 | for j = 1:k-1 | ||

168 | xy = 0.5 + j*m; | ||

169 | plot( [xy, xy], [0.5, axmax - 0.5], 'k', 'LineWidth', 2.0 ); | ||

170 | plot( [0.5, axmax - 0.5], [xy, xy], 'k', 'LineWidth', 2.0 ); | ||

171 | end | ||

172 | |||

173 | % Mark bins | ||

174 | for j = 1:(k*m)-1 | ||

175 | xy = 0.5 + j; | ||

176 | plot( [xy, xy], [0.5, axmax - 0.5], 'k'); | ||

177 | plot( [0.5, axmax - 0.5], [xy, xy], 'k'); | ||

178 | end | ||

179 | |||

180 | % Make a box around the area | ||

181 | plot( [0.5, axmax - 0.5], [0.5, 0.5], 'k', 'LineWidth', 2.0 ); | ||

182 | plot( [0.5, axmax - 0.5], [axmax - 0.5, axmax - 0.5], 'k', 'LineWidth', 2.0 ); | ||

183 | plot( [0.5, 0.5], [0.5, axmax - 0.5], 'k', 'LineWidth', 2.0 ); | ||

184 | plot( [axmax - 0.5, axmax - 0.5], [0.5, axmax - 0.5], 'k', 'LineWidth', 2.0 ); | ||

185 | |||

186 | hold off; | ||

187 | |||

188 | % Set the axes so that the extreme indices just fit into the view | ||

189 | axis( [0.5 axmax-0.5 0.5 axmax-0.5 ] ); | ||

190 | |||

191 | end | ||

192 | end |