The problem I am having has to do with solving for a matrix of coefficients where I am passing a function that calculates the error between a matrix of observed values and those predicted by some model. The following mathematica code is a very simplified and is just a toy example of what I am trying to do. I have read nearly every post about forcing a function to evaluate only when there are specific values for the coefficients by using ?NumericQ, ?VectorQ and ?MatrixQ although none of the posts indicate how this works and why.(It might be helpful to know why testing if something is numeric, a vector or a matrix will force numerical evaluation.) Here's an example of what I am trying to do:
ClearAll[funct, betas, y, xMat, error];
y = RandomReal[{0, 1}, {10, 3}];
xMat = RandomReal[{0, 1}, {10, 3}];
betas = Table[beta[i, j], {i, 1, 3}, {j, 1, 3}];
funct[betas_?(MatrixQ[#, NumericQ] &), y_, xMat_] := Module[{},
error = Total[Abs[(y - xMat.betas)]^2, 2];
error
]
ans = FindMinimum[funct[betas, y, xMat], betas];
The error message I get is: FindMinimum::srect: Value beta[1.,2.] in search specification {beta[1,1],beta[1,2],beta[1,3]} is not a number or array of numbers.FindMinimum::srect: Value beta[1.,2.] in search specification {beta[1,1],beta[1,2],beta[1,3]} is not a number or array of numbers.
After reading the posts about this, my assumption was that using betas_?(MatrixQ[#, NumericQ] &)
would force evaluation of the function when the variables have specific values. So I'm not sure why this doesn't work. It works in the case where there is only a vector of coefficients, such as:
ClearAll[funct, betas, y, xMat, error];
y = RandomReal[{0, 1}, {10}];
xMat = RandomReal[{0, 1}, {10, 3}];
betas = Table[beta[i], {i, 1, 3}];
funct[betas_?(VectorQ[#, NumericQ] &), y_?(VectorQ[#, NumericQ] &),
xMat_?(MatrixQ[#, NumericQ] &)] := Module[{},
error = Total[Abs[(y - xMat.betas)]^2, 2];
error
]
ans = FindMinimum[funct[betas, y, xMat], betas];
So, currently I'm at a loss.