# 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.4326    1.1909   -0.1867
    		   -1.6656    1.1892    0.7258
    		    0.1253   -0.0376   -0.5883
    		    0.2877    0.3273    2.1832
    		   -1.1465    0.1746   -0.1364
    	soln.U{2} =
    		    0.1139   -0.8323    1.6236
    		    1.0668    0.2944   -0.6918
    		    0.0593   -1.3362    0.8580
    		   -0.0956    0.7143    1.2540
    	soln.U{3} =
    		   -1.5937   -0.3999    0.7119
    		   -1.4410    0.6900    1.2902
    		    0.5711    0.8156    0.6686

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

    data is a tensor of size 5 x 4 x 3
    	data(:,:,1) =
    	    0.2975    0.5373    0.3371   -0.2060
    	    0.5010    1.9902    0.4515   -0.2076
    	   -0.1516   -0.0112   -0.2365    0.0400
    	    0.4710   -0.6544    0.3083    0.3917
    	    0.0494    1.5525    0.0441   -0.2274
    	data(:,:,2) =
    	   -0.3259    0.6435   -0.4963    0.1670
    	    0.0907    2.0616   -0.2365    0.3007
    	   -0.2359   -0.1858   -0.1096   -0.1932
    	    0.7182   -0.6433    0.3296    0.6640
    	    0.0439    1.4704    0.0087   -0.1378
    	data(:,:,3) =
    	   -0.4469    0.0068   -0.7364    0.3349
    	   -0.3109   -0.6452   -0.6521    0.4787
    	    0.0003    0.0366    0.0358   -0.1466
    	    0.3220    0.0015    0.0567    0.4522
    	   -0.1002   -0.4936   -0.1108    0.0112

    % 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.6451   -0.9898    1.4789
    	    0.8057    1.3396    1.1380
    	    0.2316    0.2895   -0.6841
    		soln.core(:,:,2) =
    	   -1.2919   -0.8436   -0.5465
    	   -0.0729    0.4978   -0.8468
    	   -0.3306    1.4885   -0.2463
    	soln.U{1} =
    		    0.7812   -2.2023    0.0215
    		    0.5690    0.9863   -1.0039
    		   -0.8217   -0.5186   -0.9471
    		   -0.2656    0.3274   -0.3744
    		   -1.1878    0.2341   -1.1859
    	soln.U{2} =
    		   -1.0559   -0.0412    0.9535
    		    1.4725   -1.1283    0.1286
    		    0.0557   -1.3493    0.6565
    		   -1.2173   -0.2611   -1.1678
    	soln.U{3} =
    		   -0.4606   -1.3194
    		   -0.2624    0.9312
    		   -1.2132    0.0112

    % 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 =
         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) 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 =
         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.0490    0.0557   -0.0049         0
    	    0.0307    0.0188    0.0242   -0.0801
    	         0         0         0    0.0064
    	         0    0.0094         0   -0.0484
    	   -0.0634    0.0198   -0.0066    0.0011
    	ans(:,:,2) =
    	    0.1365   -0.2151   -0.0765    0.4273
    	    0.0434         0   -0.0214    0.2048
    	    0.0302         0         0         0
    	   -0.0369   -0.0055    0.0096    0.0681
    	    0.2854   -0.2360   -0.0864    0.2469
    	ans(:,:,3) =
    	   -0.1409    0.0808    0.0087         0
    	         0    0.0321    0.0258    0.0249
    	         0    0.0201         0         0
    	   -0.0260   -0.0137    0.0121    0.0713
    	   -0.0522    0.0734    0.0341   -0.1614

## 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.0310
    	(1,4,3)    0.1896
    	(2,2,2)   -0.1124
    	(2,3,2)   -0.1144
    	(2,4,1)    0.0217
    	(3,1,2)   -0.0195
    	(4,1,1)   -0.0186
    	(4,2,1)    0.2357
    	(4,4,3)   -0.7525
    	(5,1,2)    0.0230
    	(5,2,1)    0.0261
    	(5,2,3)    0.1402

## 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.7309         0   -0.2616   -0.7462
    	         0    0.2922         0         0
    	         0    0.2339   -0.0864    0.5277
    	         0   -0.5916   -1.4867         0
    	         0   -0.7499         0   -2.0787
    	ans(:,:,2) =
    	         0   -0.4270         0   -0.9807
    	    0.3360         0    0.3856   -0.0981
    	         0    0.4074   -0.9725         0
    	         0    0.3694   -1.5791    1.4145
    	         0         0         0         0
    	ans(:,:,3) =
    	         0         0    0.1516    0.1704
    	         0   -0.2390   -0.1872   -0.2207
    	    0.0111         0         0   -0.0812
    	         0         0         0    0.9075
    	         0         0    0.5276    0.7449

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