I have some 2-dimensional data of dimension n x m that I suspect has the following structure:
syntheticData[i_, j_] := (a[[i]] + b[[i]] t[[j]] + c[[i]] t[[j]]^2 ) (1 +
RandomReal[{-0.2, 0.2}])
where a, b and c are n-dimensional vectors and t is an m-dimensional vector.
I am trying to use NonLinearModelFit to obtain all the components of a, b and c (it would be nice to get t too, but I could find its value later if I knew a, b and c), but when I use t as the independent variable for the fit I do not know how to tell Mathematica that the value of t must be the same for each row.
For completeness, the code to simulate data similar to mine would be:
a = Table[RandomReal[{0, 5}], 4];
b = Table[RandomReal[{10, 30}], 4];
c = Table[RandomReal[{0, 10}], 4];
t = Table[RandomReal[{0.5, 1.5}], 50];
fullTable = Table[syntheticData[i, j], {i, 4}, {j, 50}]
and I am interested in finding a, b and c (with the associated uncertainty, if possible)
Edit:
My data has some missing data, further complicating the problem. It looks something like this (although each row has at least two non-zero entries)
missingTable = Table[If[RandomReal[] < 0.3, "", fullTable[[i, j]]], {i, 4}, {j, 50}]
My approach right now is to write dummy variables as
{a, b, c} = Transpose @ Table[ToExpression[# <> ToString@index] & /@ {"a", "b",
"c"}, {index, 4}];
t = Table[ToExpression["t" <> ToString@index], {index, 50}];
and then minimize the following expression with respect to them:
NMinimize[
Sum[
(Sum[ If[NumberQ @ fulltable[[n, i]],
(fulltable[[n, i]] - a[[i]] + b[[i]] t[[j]] + c[[i]] t[[j]]^2 )^2,
0], {i, 4}])^2,
{j, 50}],
Join[a, b, c, t]]
The choice of what function to minimize is somewhat arbitrary, but the reasoning behind it is that as I assume the t to be the same for all the points in a given row, I square the sum of the residuals of these components to prioritize this fit.
Another possibility would be to define the residuals as
fulltable[[n, i]] / (a[[i]] + b[[i]] t[[j]] + c[[i]] t[[j]]^2) - 1
which could account for the errors being proportional to the magnitud and not linear.
If anyone has a better idea I would love to try it!
Minimize[Sum[(s[i] - 1)^2, {i, 1, 3}], Array[s, 3]]
$\endgroup$