function [y,x] = twonorm (x, opts, output_mgn)
%TWONORM  The twonorm target in Breiman'96 [TR-460] and
%         Breiman'98 [Arcing Classifiers].
%
%   The default dimension is 20, so
%   [Y,X] = TWONORM(500) gives 500 20-D examples.
%
%   Y = TWONORM(X) gives output (1/-1) with dimension from X.
%   Y = TWONORM(X, .6) also specifies the probability of Class 1.

if nargin < 3, output_mgn = false; end
if nargin < 2, opts = []; end
opts = options([0.5, 20], opts);
p1 = opts(1); dim = opts(2);
if length(x) > 1, dim = size(x, 2); end

if output_mgn, error('Margin part is not implemented.'); end

a = 2 / sqrt(dim);
if length(x) == 1, %% Generate random inputs
    assert(x == round(x));
    y = (rand(x,1) < p1)*2 - 1;
    x = randn(x,dim) + y * (a*ones(1,dim));
else
    d1 = x - a; d1 = sum(d1.^2, 2);
    d2 = x + a; d2 = sum(d2.^2, 2);
    post = p1 ./ (p1 + (1-p1) * exp((d1-d2)/2));
    y = (rand(size(x,1),1) < post) * 2 - 1;
end
