Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. function [pos_ev_idx,neg_ev_idx,seed,data] = test_negative_type_simple(options)
  2. % TEST_NEGATIVE_TYPE_SIMPLE evaluate the eigenvalues of Gram matrices
  3. %
  4. % This function evaluates the number of positive/negative eigenvalues
  5. % of the Gram matrix M, either constructed from the (1) q-Wasserstein
  6. % distance or (2) the bottleneck distance.
  7. %
  8. % Both distances lead to nonnegative (symmetric) Gram matrices and
  9. % the question is if the Gram matrices are (conditionally) negative
  10. % definite (see suppl. material).
  11. %
  12. % Input:
  13. % ------
  14. %
  15. % Mx1 cell array 'options', e.g.,
  16. %
  17. % options{1}.q = 1; % controls the order
  18. % options{1}.num_sets = 10; % NxN Gram matrix, here: 10x10
  19. % options{1}.type = 'wasserstein'; % use Wasserstein, else: 'bottleneck'
  20. % options{1}.seed = 212575; % RNG seed, -1 for random
  21. % options{1}.squared = 0; % square the distance
  22. % options{1}.disp = 1; % show results
  23. %
  24. % Calling
  25. %
  26. % >> [~,~,seed,data] = test_negative_type_simple(options);
  27. %
  28. % will evaluate the eigenvalues of the 10x10 Gram matrix with elements
  29. %
  30. % g_ij = d_{W,1}(.,.)
  31. %
  32. % i.e., the 1-Wasserstein distance between two point sets.
  33. %
  34. % Returns:
  35. % --------
  36. %
  37. % pos_ev_idx - Positions of positive eigenvalues
  38. % neg_ev_idx - Positions of negative eigenvalues
  39. % seed - Seed of RNG (useful, if -1 initially)
  40. % data - (2 x 2 x num_sets) matrix of input points
  41. %
  42. % Author(s): Anonymous
  43.  
  44. M = length(options);
  45. for m=1:M
  46.  
  47. current_options = options{m};
  48. % if seed == -1, then generate seed and try ...
  49. if current_options.seed == -1
  50. seed = round(sum(100*clock));
  51. current_options.seed = seed;
  52. end
  53. seed = current_options.seed;
  54. rng(current_options.seed);
  55.  
  56. % create data
  57. data = round(rand(2, 2, current_options.num_sets) * 10) - 1;
  58.  
  59. % compute
  60. distance_matrix = zeros(current_options.num_sets);
  61. edge_weights = zeros(2);
  62. for setA = 1 : current_options.num_sets
  63. for setB = 1 : current_options.num_sets
  64. for pointA = 1:2
  65. for pointB = 1:2
  66. startPoint = squeeze(data(:,pointA,setA));
  67. endPoint = squeeze(data(:,pointB,setB));
  68. edge_weights(pointA,pointB) = norm( startPoint - endPoint, inf);
  69. end
  70. end
  71.  
  72. if strcmp(current_options.type,'wasserstein')
  73. distance_matrix(setA,setB) = min( ...
  74. edge_weights(1,2)^current_options.q + ...
  75. edge_weights(2,1)^current_options.q, ...
  76. edge_weights(1,1)^current_options.q + ...
  77. edge_weights(2,2)^current_options.q)^(1/current_options.q);
  78. elseif strcmp(current_options.type,'bottleneck')
  79. distance_matrix(setA,setB) = min( ...
  80. max( edge_weights(1,2), edge_weights(2,1) ), ...
  81. max( edge_weights(1,1), edge_weights(2,2)));
  82. else
  83. error('unknown distance');
  84. end
  85. end
  86. end
  87.  
  88. % square distance or not
  89. if current_options.squared
  90. gram_matrix = distance_matrix.^2;
  91. else
  92. gram_matrix = distance_matrix;
  93. end
  94.  
  95. assert(length(find((gram_matrix>=0)==1)) == numel(gram_matrix));
  96.  
  97. spectrum_of_gram_matrix = eig( gram_matrix );
  98.  
  99. % # positive and negative eigenvalues
  100. pos_ev_idx = find(spectrum_of_gram_matrix>0);
  101. neg_ev_idx = find(spectrum_of_gram_matrix<0);
  102.  
  103. if current_options.disp
  104. fprintf('---------------------------\n');
  105. fprintf('Counterexample #%d\n',m);
  106. if (strcmp(current_options.type,'bottleneck'))
  107. fprintf('Distance: d_B\n');
  108. else
  109. fprintf('Distance: d_{W,%d}\n', ...
  110. current_options.q);
  111. end
  112. fprintf('Gram matrix size: %d x %d\n', ...
  113. current_options.num_sets, ...
  114. current_options.num_sets);
  115. fprintf('---------------------------\n');
  116. fprintf('#pos. EVs: %d\n', length(pos_ev_idx));
  117. for n=1:length(pos_ev_idx)
  118. fprintf('\t%.10f\n', spectrum_of_gram_matrix(pos_ev_idx(n)));
  119. end
  120. fprintf('#neg. EVs: %d\n', length(neg_ev_idx));
  121. for n=1:length(neg_ev_idx)
  122. fprintf('\t%.10f\n', spectrum_of_gram_matrix(neg_ev_idx(n)));
  123. end
  124. end
  125. end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement