I have a problem where I launch this code in NMinimize and after a minute, the CPU usage of Mathematica drops to 0. The notebook still shows as evaluating.
Here's some context and explanation of my code. I am trying to fit the accuracy data obtained from a cognitive task. I first show l
letters to a participant, then another set of l
letters out of which d
are different from the first set of letters. For example, showing ABCD followed by AECF is l = 4, d = 2
. Participants answer "same" if they think all the letters match, and "different" if at least 1 letter mismatches.
Matching letters are encoded as 1 and mismatching letters as -1. If all letters are considered to be matching, then the sum of all letters will be l
and the participant will answer "same". This will result in a correct answer when d = 0
. Conversely, if the total is not l
, then the participant will answer "different", which is a correct answer if d > 0
.
I'm testing whether noise during different steps of the cognitive process could explain the accuracy of participants. My function has 4 parameters, each indicating a probability that noise will interfere (real numbers between 0 and 1). After making sure NMinimize does not use invalid values (constraints don't work as "hard constraints"), I compute the expected accuracies for all types of trials with up to 4 letters (a total of 14 conditions). I then give the result of my objective function, which is how far from the real data each calculated accuracy is, plus a penalty that favorizes smaller differences at the cost of a larger global offset.
Clear[fNumericalToFit]
fNumericalToFit[Pe_Real, Pt_Real, Prs_Real, Prd_Real, iN_Integer: 10000] :=
Block[{bProbTotal,bMAsM,bMAsD,bNormalAnswer,bAcc,bErrMism,bErrMatch,bScore},
(* Verify that NMinimize does not use invalid values *)
bProbTotal = Total[Which[
# < 0, -100*#,
# > 0.2, 100*(# - 0.2),
True, 0] & /@ {Pe, Pt, Prs, Prd}];
If[bProbTotal > 0, Return[bProbTotal]];
bMAsM = (1 - Pe)*(1 - Pt) + Pe*Pt; (* Prob of a match being interpreted as match *)
bMAsD = (1 - Pe)*Pt + Pe*(1 - Pt); (* Prob of a match being interpreted as mismatch *)
bNormalAnswer = (1 - Prs - Prd); (* Prob of not making a biased answer *)
bAcc = ParallelTable[
Which[
d == 0,
bErrMatch = RandomChoice[{bMAsM, bMAsD} -> {1, -1}, {iN, l}].ConstantArray[1, l];
(Count[bErrMatch, l]/iN)*bNormalAnswer + Prs,
d == l,
bErrMism = RandomChoice[{1 - Pt, Pt} -> {1, -1}, {iN, d}].ConstantArray[-1,d];
(1 - Count[bErrMism, l]/iN)*bNormalAnswer + Prd,
True,
bErrMism = RandomChoice[{1 - Pt, Pt} -> {1, -1}, {iN, d}].ConstantArray[-1,d];
bErrMatch = RandomChoice[{bMAsM, bMAsD} -> {1, -1}, {iN,l - d}].ConstantArray[1, l - d];
(1 - Count[bErrMism + bErrMatch, l]/iN)*bNormalAnswer + Prd],
{l, 4}, {d, 0, l}];
bScore = Abs[Flatten[bAcc] - {0.97`, 0.971`, 0.96`, 0.955`, 0.975`, 0.955`, 0.909`, 0.97`, 0.975`, 0.947`, 0.785`, 0.945`, 0.973`, 0.983`}];
Total[bScore] + 2*Max[bScore]
]
Does anyone have a clue why running this function in NMinimize results in not using any CPU after a minute while still evaluating?
NMinimize[fNumericalToFit[EncodeErr,TestErr,RespSame,RespDiff],{EncodeErr,TestErr,RespSame,RespDiff}]
bProbTotal
rather thanbScore
. $\endgroup$ – bbgodfrey May 4 '18 at 12:18bProbTotal
is given byTotal[Which[# < 0, -100*#, # > 0.2, 100*(# - 0.2), True, 0] & /@ {0.20000000000965154, 0.07745625850428044, 0.03822638935911683, 0.019227848497057474}]
for the solution given by @MarcoB and is equal to9.65153*10^-10
, which is greater than0
, triggering theReturn
statement.ParallelTable
, when it works, behaves similarly. $\endgroup$ – bbgodfrey May 4 '18 at 16:58bScore
. It had values of order3
, and then the calculation returned an answer of3.16211*10^-9
, which could only have come frombProbTotal
. $\endgroup$ – bbgodfrey May 4 '18 at 17:12