# 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.9316 0.3352 0.6555 soln.U{1} = -0.6838 0.8812 0.2861 0.5282 -1.0275 -0.4624 0.4972 -1.9671 0.4757 0.8937 0.7034 0.5956 -0.5005 -0.2736 2.0529 soln.U{2} = 0.0512 0.3304 1.1132 -0.2457 0.3013 -2.0263 0.1092 -0.7632 -0.4522 -0.6993 -0.1056 0.6296 soln.U{3} = 0.1700 0.5900 0.1412 1.2833 1.4220 -0.7117 0.3704 0.9293 -0.2229

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

data is a tensor of size 5 x 4 x 3 data(:,:,1) = 0.1083 0.1175 -0.2063 0.0196 -0.1364 0.0211 0.1989 -0.0883 -0.0484 -0.2644 0.2761 0.0071 0.0477 -0.1516 -0.0547 -0.0628 0.1679 -0.3917 -0.0193 0.1624 data(:,:,2) = -0.0408 0.6115 -0.3387 0.4617 0.1230 -0.7173 0.3733 -0.3137 -0.5623 0.0728 0.9094 -0.4486 -0.2112 0.3320 0.0109 -0.8866 -1.0776 2.1098 0.4472 -0.0767 data(:,:,3) = 0.0374 0.1753 -0.2152 0.1234 -0.0515 -0.2724 0.3336 -0.0639 -0.2461 -0.0366 0.4856 -0.0070 0.0457 0.1841 -0.0748 -0.3450 -0.3147 0.6265 0.1975 -0.0040

% 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.8048 -1.4561 -0.4117 -0.2120 0.3154 1.4983 0.4127 1.6101 -0.8182 soln.core(:,:,2) = 1.1298 -0.1916 -0.8783 0.3180 0.6761 1.0393 0.6760 0.3560 1.0889 soln.U{1} = -1.3048 0.0748 -0.9549 -0.3698 -1.1916 -1.0580 -0.1585 -1.1410 0.6110 -0.2790 0.0284 0.7834 -1.1776 -2.0901 -0.2529 soln.U{2} = 0.2684 0.6049 0.6619 1.1780 -1.3299 0.6825 0.5049 -0.7605 -0.1414 0.6114 1.1650 -1.9804 soln.U{3} = 0.0606 0.6301 0.9471 1.0609 0.2202 -0.0639

```
% 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 = 41.3629 59.4663 65.5213 56.5739 83.7585 71.6542 37.1608 51.1311 42.5253 77.6401

% 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 1 0 0 1 0 1 1 1 0 ans(:,:,2) = 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 0 1 1 ans(:,:,3) = 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 0 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.0037 0.1528 0 0.0534 0.0172 -0.0263 0.0239 0.0253 -0.0201 -0.1125 -0.0059 -0.0896 0 0 -0.0922 0 -0.0064 -0.1906 0.0627 0 ans(:,:,2) = -0.0142 -0.0308 -0.0212 -0.0363 0.0336 0.0480 0 0.0991 -0.0517 -0.0657 -0.0967 -0.1546 0 0 -0.0067 -0.0512 -0.0121 0 -0.0398 -0.0408 ans(:,:,3) = -0.0094 0.0091 -0.0004 -0.0012 -0.0029 0.0023 0.0066 0.0195 0 -0.0068 -0.0033 0 0 0 0 0 -0.0078 -0.0140 0.0111 -0.0176

## 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,2,1) 1 (1,3,2) 1 (1,4,1) 1 (2,3,1) 1 (2,3,2) 1 (3,1,1) 1 (3,2,2) 1 (3,4,1) 1 (4,3,1) 1 (4,4,2) 1 (5,4,2) 1 (5,4,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,2,1) -0.2254 (1,3,2) -0.1904 (1,4,1) -0.2516 (2,3,1) 0.4154 (2,3,2) -0.0804 (3,1,1) -0.0695 (3,2,2) -0.9749 (3,4,1) 1.3513 (4,3,1) -0.7853 (4,4,2) -0.7660 (5,4,2) -0.2515 (5,4,3) -0.0392

## 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.0611 -0.3068 0.0720 0 0 0.2638 0.5122 -0.1113 -0.1459 -0.3128 0 0.5154 0.0426 0.0800 -0.2983 -0.1457 0 0 0.6062 0 ans(:,:,2) = 0.0899 0 -0.0516 0.2154 -0.0479 0 0 -0.2969 0 0 0 0.2190 -0.0166 0 0 0 0 -0.1032 -0.0666 0 ans(:,:,3) = -0.0192 0 0.0017 0 -0.0145 0.0629 0 -0.0989 0.0474 0 0 0.0774 0.0099 0 -0.0066 0 0 0 0 0

## 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 = 326 total_insertions = 500 orig_lambda_vs_rescaled = 54.8801 54.8801 54.8801 54.8801

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