# Creating test problems and initial guesses

We demonstrate how to use Tensor Toolbox `create_problem` and `create_guess` functions to create test problems for fitting algorithms.

## Contents

- Creating a CP test problem
- Creating a Tucker test problem
- Recreating the same test problem
- Checking default parameters and recreating the same test problem
- Options for creating factor matrices, core tensors, and lambdas
- Generating data from an existing solution
- Creating dense missing data problems
- Creating sparse missing data problems.
- Create missing data problems with a pre-specified pattern
- Creating sparse problems (CP only)
- Generating an initial guess

## Creating a CP test problem

The `create_problem` function allows a user to generate a test problem with a known solution having a pre-specified solution. The `create_problem` function generates both the solution (as a `ktensor` for CP) and the test data (as a `tensor`). We later show that a pre-specificed solution can be used as well.

    % Create a problem
    info = create_problem('Size', [5 4 3], 'Num_Factors', 3, 'Noise', 0.10);

    % Display the solution created by create_problem
    soln = info.Soln

    soln is a ktensor of size 5 x 4 x 3
    	soln.lambda = [ 0.7764     0.48935      0.1859 ]
    	soln.U{1} =
    		    0.5258   -1.1032   -0.1455
    		    0.1824   -1.7657    0.3118
    		    0.9157   -1.0920    2.1393
    		   -1.4192   -0.6685    0.8185
    		    0.1210   -0.5451    0.7353
    	soln.U{2} =
    		   -1.3058    0.6887   -1.1405
    		    1.3975   -0.5577    0.2800
    		    0.8515    0.5041   -0.0953
    		   -0.2355    0.8043   -0.5103
    	soln.U{3} =
    		   -0.7552    0.3747   -0.9519
    		    0.1934   -0.7063   -1.0122
    		   -0.1293    0.1096   -0.3956

    % Display the data created by create_problem
    data = info.Data

    data is a tensor of size 5 x 4 x 3
    	data(:,:,1) =
    	    0.2167   -0.2578   -0.3358   -0.1229
    	    0.0200    0.0045   -0.2762   -0.1806
    	    0.9620   -0.8177   -0.5460    0.0604
    	   -1.0023    1.2181    0.6154   -0.2238
    	    0.1426   -0.0805   -0.1580   -0.0336
    	data(:,:,2) =
    	    0.0905   -0.1069    0.2350    0.3185
    	    0.5405   -0.3166    0.2999    0.5282
    	    0.5628   -0.1330    0.3590    0.4859
    	    0.5634   -0.4495   -0.0716    0.2860
    	    0.3029   -0.0880    0.1421    0.1826
    	data(:,:,3) =
    	    0.0182    0.0056   -0.1272   -0.0558
    	   -0.0347    0.0068   -0.0934   -0.0245
    	    0.2634   -0.1696   -0.0685    0.0606
    	   -0.0749    0.1920    0.1521   -0.0231
    	    0.0385    0.0496   -0.0615    0.0008

    % The difference between true solution and measured data should match the
    % specified 10% noise.
    diff = norm(full(info.Soln) - info.Data)/norm(full(info.Soln))

    diff =

        0.1000

## Creating a Tucker test problem

The `create_problem` function can also be used to create Tucker problems by specifying the `'Type'` as `'Tucker'`. In this case, the `create_problem` function generates both the solution (as a `ttensor` for Tucker) and the test data (as a `tensor`).

    % Create a problem
    info = create_problem('Type', 'Tucker', 'Size', [5 4 3], 'Num_Factors', [3 3 2]);

    % Display the Tucker-type solution created by create_problem
    soln = info.Soln

    soln is a ttensor of size 5 x 4 x 3
    	soln.core is a tensor of size 3 x 3 x 2
    		soln.core(:,:,1) =
    	   -0.8988   -0.4175   -0.7923
    	    1.8187   -0.6114   -1.0974
    	   -0.5025   -0.8902   -1.2167
    		soln.core(:,:,2) =
    	   -0.3826    1.8500    0.2016
    	   -1.0911   -1.5579    0.1395
    	    0.3751   -1.3389   -1.0170
    	soln.U{1} =
    		    0.7377    0.5990    0.5210
    		   -2.7981   -0.0262    0.3796
    		   -1.1449   -0.3424   -2.1532
    		   -0.4306    0.6360    0.3872
    		    0.4012    0.7779    0.3920
    	soln.U{2} =
    		    0.6277    0.3257    1.4809
    		   -0.4380   -0.6847   -0.3899
    		    0.3509   -0.2374   -1.4846
    		   -1.5933    2.7611    1.0004
    	soln.U{3} =
    		   -1.0126   -0.2377
    		    0.0346    0.0114
    		    0.9299    0.2068

    % Difference between true solution and measured data (default noise is 10%)
    diff = norm(full(info.Soln) - info.Data)/norm(full(info.Soln))

    diff =

        0.1000

## Recreating the same test problem

We can recreate exactly the same test problem when we use the same random seed and other parameters.

    % Set-up, including specifying random state
    sz = [5 4 3]; %<- Size
    nf = 2; %<- Number of components
    state = RandStream.getDefaultStream.State; %<- Random state

    % Generate first test problem
    info1 = create_problem('Size', sz, 'Num_Factors', nf, 'State', state);

    % Generate second identical test problem
    info2 = create_problem('Size', sz, 'Num_Factors', nf, 'State', state);

    % Check that the solutions are identical
    tf = isequal(info1.Soln, info2.Soln)

    tf =

         1

    % Check that the data are identical
    diff = norm(info1.Data - info2.Data)

    diff =

         0

## Checking default parameters and recreating the same test problem

The `create_problem` function returns the parameters that were used to generate it. These can be used to see the defaults. Additionally, if these are saved, they can be used to recreate the same test problems for future experiments.

    % Generate test problem and use second output argument for parameters.
    [info1,params] = create_problem('Size', [5 4 3], 'Num_Factors', 2);

    % Here are the parameters
    params

    params =

           Core_Generator: 'randn'
         Factor_Generator: 'randn'
         Lambda_Generator: 'rand'
                        M: 0
                    Noise: 0.1000
              Num_Factors: 2
                     Size: [5 4 3]
                     Soln: []
        Sparse_Generation: 0
                 Sparse_M: 0
                    State: {1x6 cell}
                Symmetric: []
                     Type: 'CP'

    % Recreate an identical test problem
    info2 = create_problem(params);

    % Check that the solutions are identical
    tf = isequal(info1.Soln, info2.Soln)

    tf =

         1

    % Check that the data are identical
    diff = norm(info1.Data - info2.Data)

    diff =

         0

## Options for creating factor matrices, core tensors, and lambdas

Any function with two arguments specifying the size can be used to generate the factor matrices. This is specified by the `'Factor_Generator'` option to `create_problem`.

Pre-defined options for `'Factor_Generator'` for creating factor matrices (for CP or Tucker) include:

- `'rand'` - Uniform on [0,1]
- `'randn'` - Gaussian with mean 0 and std 1
- `'orthogonal'` - Generates a random orthogonal matrix. This option only works when the number of factors is less than or equal to the smallest dimension.
- `'stochastic'` - Generates nonnegative factor matrices so that each column sums to one.

Pre-defined options for `'Lambda_Generator'` for creating lambda vector (for CP) include:

- `'rand'` - Uniform on [0,1]
- `'randn'` - Gaussian with mean 0 and std 1
- `'orthogonal'` - Creates a random vector with norm one.
- `'stochastic'` - Creates a random nonnegative vector whose entries sum to one.

Pre-defined options for `'Core_Generator'` for creating core tensors (for Tucker) include:

- `'rand'` - Uniform on [0,1]
- `'randn'` - Gaussian with mean 0 and std 1

    % Here is ane example of a custom factor generator
    factor_generator = @(m,n) 100*rand(m,n);
    info = create_problem('Size', [5 4 3], 'Num_Factors', 2, ...
        'Factor_Generator', factor_generator, 'Lambda_Generator', @ones);
    first_factor_matrix = info.Soln.U{1}

    first_factor_matrix =

       48.4964   56.6773
       11.4613   82.3008
       66.4856   67.3949
       36.5374   99.9447
       14.0044   96.1636

    % Here is an example of a custom core generator for Tucker:
    info = create_problem('Type', 'Tucker', 'Size', [5 4 3], ...
        'Num_Factors', [2 2 2], 'Core_Generator', @tenones);
    core = info.Soln.core

    core is a tensor of size 2 x 2 x 2
    	core(:,:,1) =
    	     1     1
    	     1     1
    	core(:,:,2) =
    	     1     1
    	     1     1

    % Here's another example for CP, this time using a function to create
    % factor matrices such that the inner products of the columns are
    % prespecified.
    info = create_problem('Size', [5 4 3], 'Num_Factors', 3, ...
        'Factor_Generator', @(m,n) tt_ccong(m,n,.9));
    U = info.Soln.U{1};
    congruences = U'*U

    congruences =

        1.0000    0.9000    0.9000
        0.9000    1.0000    0.9000
        0.9000    0.9000    1.0000

## Generating data from an existing solution

It's possible to skip the solution generation altogether and instead just generate appropriate test data.

    % Manually generate a test problem (or it comes from some
    % previous call to |create_problem|.
    soln = ktensor({rand(50,3), rand(40,3), rand(30,3)});

    % Use that soln to create new test problem.
    info = create_problem('Soln', soln);

    % Check whether solutions is equivalent to the input
    iseq = isequal(soln,info.Soln)

    iseq =

         1

## Creating dense missing data problems

It's possible to create problems that have a percentage of missing data. The problem generator randomly creates the pattern of missing data.

    % Specify 25% missing data as follows:
    [info,params] = create_problem('Size', [5 4 3], 'M', 0.25);

    % Here is the pattern of known data (1 = known, 0 = unknown)
    info.Pattern

    ans is a tensor of size 5 x 4 x 3
    	ans(:,:,1) =
    	     1     1     1     0
    	     1     1     1     1
    	     0     0     0     1
    	     0     1     0     1
    	     1     1     1     1
    	ans(:,:,2) =
    	     1     1     1     1
    	     1     0     1     1
    	     1     0     0     0
    	     1     1     1     1
    	     1     1     1     1
    	ans(:,:,3) =
    	     1     1     1     0
    	     0     1     1     1
    	     0     1     0     0
    	     1     1     1     1
    	     1     1     1     1

    % Here is the data (incl. noise) with missing entries zeroed out
    info.Data

    ans is a tensor of size 5 x 4 x 3
    	ans(:,:,1) =
    	   -0.2001   -0.0428    0.0169         0
    	    0.0984    0.0376    0.0064    0.0217
    	         0         0         0   -0.1019
    	         0   -0.0470         0   -0.1454
    	   -0.2135   -0.0442    0.0256   -0.1270
    	ans(:,:,2) =
    	   -0.0499   -0.1091    0.0518   -0.2269
    	    0.0259         0    0.0031    0.0035
    	    0.0015         0         0         0
    	    0.0505    0.0474   -0.0248    0.0934
    	   -0.0615   -0.0962    0.0362   -0.1420
    	ans(:,:,3) =
    	    0.3410    0.0422   -0.0272         0
    	         0   -0.0425    0.0322   -0.0508
    	         0    0.0560         0         0
    	    0.6094    0.0934   -0.0624    0.2853
    	    0.3731    0.0512   -0.0468    0.1645

## Creating sparse missing data problems.

If `Sparse_M` is set to true, then the data returned is sparse. Moreover, the dense versions are never explicitly created. This option only works when M >= 0.8.

    % Specify 80% missing data and sparse
    info = create_problem('Size', [5 4 3], 'M', 0.80, 'Sparse_M', true);

    % Here is the pattern of known data
    info.Pattern

    ans is a sparse tensor of size 5 x 4 x 3 with 12 nonzeros
    	(1,3,1)     1
    	(1,4,3)     1
    	(2,2,2)     1
    	(2,3,2)     1
    	(2,4,1)     1
    	(3,1,2)     1
    	(4,1,1)     1
    	(4,2,1)     1
    	(4,4,3)     1
    	(5,1,2)     1
    	(5,2,1)     1
    	(5,2,3)     1

    % Here is the data (incl. noise) with missing entries zeroed out
    info.Data

    ans is a sparse tensor of size 5 x 4 x 3 with 12 nonzeros
    	(1,3,1)    0.0238
    	(1,4,3)    0.2621
    	(2,2,2)    0.0957
    	(2,3,2)   -0.0345
    	(2,4,1)    0.0449
    	(3,1,2)   -0.0623
    	(4,1,1)    0.0599
    	(4,2,1)   -0.0122
    	(4,4,3)   -0.3202
    	(5,1,2)    0.1128
    	(5,2,1)    0.0091
    	(5,2,3)   -0.6924

## Create missing data problems with a pre-specified pattern

It's also possible to provide a specific pattern (dense or sparse) to be used to specify where data should be missing.

    % Create pattern
    P = tenrand([5 4 3]) > 0.5;
    % Create test problem with that pattern
    info = create_problem('Size', size(P), 'M', P);
    % Show the data
    info.Data

    ans is a tensor of size 5 x 4 x 3
    	ans(:,:,1) =
    	   -0.1424         0   -0.0851    0.2725
    	         0   -0.2695         0         0
    	         0    0.8689   -0.7481    1.1611
    	         0    0.1564    0.2956         0
    	         0   -0.2786         0   -0.2237
    	ans(:,:,2) =
    	         0   -0.3994         0   -0.6760
    	   -0.4490         0   -0.7050    0.9680
    	         0   -2.8810    1.6518         0
    	         0    0.4997    0.0797    0.2565
    	         0         0         0         0
    	ans(:,:,3) =
    	         0         0    1.1786   -0.3976
    	         0    0.1840   -0.4430    0.3818
    	    1.1431         0         0   -1.1505
    	         0         0         0    0.1828
    	         0         0   -0.5579    0.2783

## Creating sparse problems (CP only)

If we assume each model parameter is the input to a Poisson process, then we can generate a sparse test problems. This requires that all the factor matrices and lambda be nonnegative. The default factor generator ('randn') won't work since it produces both positive and negative values.

    % Generate factor matrices with a few large entries in each column; this
    % will be the basis of our soln.
    sz = [20 15 10];
    nf = 4;
    A = cell(3,1);
    for n = 1:length(sz)
        A{n} = rand(sz(n), nf);
        for r = 1:nf
            p = randperm(sz(n));
            idx = p(1:round(.2*sz(n)));
            A{n}(idx,r) = 10 * A{n}(idx,r);
        end
    end
    S = ktensor(A);
    S = normalize(S,'sort',1);

    % Create sparse test problem based on provided solution. The
    % 'Sparse_Generation' says how many insertions to make based on the
    % provided solution S. The lambda vector of the solution is automatically
    % rescaled to match the number of insertions.
    info = create_problem('Soln', S, 'Sparse_Generation', 500);
    num_nonzeros = nnz(info.Data)
    total_insertions = sum(info.Data.vals)
    orig_lambda_vs_rescaled = S.lambda ./ info.Soln.lambda

    num_nonzeros =

       302

    total_insertions =

       500

    orig_lambda_vs_rescaled =

       73.8481
       73.8481
       73.8481
       73.8481

## Generating an initial guess

The `create_guess` function creates a random initial guess as a cell array of matrices. Its behavior is very similar to `create_problem`. A nice option is that you can generate an initial guess that is a pertubation of the solution.

    info = create_problem;

    % Create an initial guess to go with the problem that is just a 5%
    % pertubation of the correct solution.
    U = create_guess('Soln', info.Soln, 'Factor_Generator', 'pertubation', ...
        'Pertubation', 0.05);