# 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.67955     0.77677     0.43693 ]
    	soln.U{1} =
    		    1.2659   -1.8263    0.1394
    		    1.2285   -0.0288    1.3588
    		    0.3697   -1.2996    0.7381
    		   -1.6088   -0.6544    0.5026
    		   -1.0133   -0.7654    0.7669
    	soln.U{2} =
    		    0.4703   -0.4736    0.0978
    		   -0.1044    1.0593   -0.6853
    		   -1.0185   -1.3621   -1.1100
    		    0.2280    0.8114   -0.5553
    	soln.U{3} =
    		    0.6467    0.4165    0.5587
    		   -1.5888    1.0288    0.5788
    		    0.0075   -1.5957   -0.9820

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

    data is a tensor of size 5 x 4 x 3
    	data(:,:,1) =
    	    0.6249   -0.8121    0.1202   -0.3563
    	    0.2137   -0.4577   -0.9183   -0.0238
    	    0.1200   -0.3537    0.2580   -0.4708
    	   -0.3357   -0.2214    0.6465   -0.5086
    	   -0.1006   -0.3410    0.4312   -0.3290
    	data(:,:,2) =
    	   -0.0748   -1.3344    3.2810   -1.3842
    	   -0.5581   -0.1288    0.9371   -0.3729
    	    0.4566   -1.0594    1.4483   -0.8353
    	    1.0755   -0.8393   -1.1800   -0.2076
    	    0.7119   -0.8674   -0.4740   -0.2466
    	data(:,:,3) =
    	   -1.1341    2.5656   -3.1061    1.9383
    	   -0.2261    0.4430    0.6498    0.4329
    	   -0.9159    1.9396   -1.9271    1.4065
    	   -0.5931    0.9982   -0.6803    0.8009
    	   -0.5887    1.2216   -1.0430    1.1270

    % 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) =
    	    1.6229    0.3461    0.3951
    	   -1.2964   -0.9288    0.6931
    	    1.8595   -1.1376   -1.3269
    		soln.core(:,:,2) =
    	    0.0635   -0.2179    1.8158
    	   -0.2798   -0.5542    1.6557
    	   -0.8710   -0.6151   -1.5281
    	soln.U{1} =
    		    0.1595    0.4311   -0.0799
    		    0.0920    0.0801    1.7521
    		    1.7787    0.4889    1.3487
    		   -0.0043    0.6331   -0.1599
    		   -1.0328   -0.0141   -0.1235
    	soln.U{2} =
    		   -0.5839    1.2308   -0.9507
    		    0.9942    0.9231   -0.0393
    		    1.5200   -0.8304    0.2026
    		    0.2689   -0.4069   -0.1487
    	soln.U{3} =
    		   -0.3726    1.2228
    		    0.4328   -0.3496
    		    0.2976    0.1372

    % 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.getGlobalStream.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 =

      logical

       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 =

      struct with fields:

           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: {1×6 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 =

      logical

       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 =

        1.3961   66.6972
       18.7962   63.1040
       58.2546   63.2120
       82.3247   11.2026
       25.4430   79.1349

    % 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) matrandcong(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 =

      logical

       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     0     1
    	     1     1     1     1
    	     1     1     1     0
    	     1     1     1     1
    	     0     1     1     1
    	ans(:,:,2) =
    	     1     1     1     1
    	     1     0     1     1
    	     1     1     0     1
    	     1     1     1     1
    	     1     0     1     0
    	ans(:,:,3) =
    	     1     1     1     0
    	     1     0     1     0
    	     0     1     1     0
    	     0     1     1     1
    	     0     0     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.1793   -0.2310         0    0.0429
    	   -0.3749   -0.1852    0.2674    0.0013
    	   -1.1206   -0.7617    1.1015         0
    	   -0.1343   -0.0524    0.2058   -0.0447
    	         0    0.2174   -0.2209    0.0131
    	ans(:,:,2) =
    	   -0.0177   -0.3066    0.3508   -0.1254
    	   -0.5150         0    0.2864   -0.0218
    	   -1.7729   -1.1326         0    0.0851
    	   -0.2139   -0.2570    0.4128   -0.1203
    	    0.1954         0   -0.4493         0
    	ans(:,:,3) =
    	   -0.2604   -0.1968    0.2981         0
    	   -0.4944         0    0.4080         0
    	         0   -1.1708    1.3813         0
    	         0   -0.1662    0.2489    0.0002
    	         0         0   -0.4103    0.0034

## 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,1,2)     1
    	(1,2,1)     1
    	(1,3,3)     1
    	(1,4,1)     1
    	(2,3,2)     1
    	(2,4,3)     1
    	(3,4,2)     1
    	(3,4,3)     1
    	(4,3,1)     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,1,2)    0.0208
    	(1,2,1)    0.3199
    	(1,3,3)   -0.4194
    	(1,4,1)    0.2348
    	(2,3,2)    0.6854
    	(2,4,3)    1.9965
    	(3,4,2)    0.0126
    	(3,4,3)    0.3155
    	(4,3,1)    0.1286
    	(5,1,2)   -0.0514
    	(5,2,1)    1.2025
    	(5,2,3)   -2.8011

## 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.0019         0   -0.3582         0
    	         0    0.0407         0         0
    	         0    0.4171         0    0.1042
    	    0.0482         0   -0.1365         0
    	   -0.0504         0         0         0
    	ans(:,:,2) =
    	   -0.0867    0.9799         0   -0.0395
    	         0         0   -0.1445         0
    	    0.0842         0   -0.1508   -0.0263
    	         0   -0.6211         0   -0.1826
    	    0.0999   -1.3011   -0.7726         0
    	ans(:,:,3) =
    	         0         0    0.0384    0.0309
    	         0         0    0.1013         0
    	         0         0   -0.3412    0.0237
    	    0.1481   -0.8781         0    0.2671
    	   -0.0145         0         0   -0.1125

## 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 =

       327

    total_insertions =

       500

    orig_lambda_vs_rescaled =

       59.6414
       59.6414
       59.6414
       59.6414

## 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);