1
$\begingroup$

I am running a lot of simulations to fit a model to randomly generated data. Sometimes the fitting converges properly sometimes it does not, but it will give you the fitting parameters anyway. I would like to know in which case the model works best by counting occasions of exceptions. The actual code is too long to put here, so I wrote a simple do loop to demonstrate the problem. I just want to know how many exceptions happened in the do loop.

nLoop = 100;
Int = RandomInteger[{0, 10}, nLoop];
reciprical = Table[0, {nLoop}];(*initialzation*)
Do[reciprical[[iR]] = 1/Int[[iR]], {iR, 1, nLoop}]

Power::infy: Infinite expression 1/0 encountered. >>

Power::infy: Infinite expression 1/0 encountered. >>

Power::infy: Infinite expression 1/0 encountered. >>

General::stop: Further output of Power::infy will be suppressed during this calculation. >>

It can bee seen that after three times of warning, the further output of warning message will be suppressed. If not suppressed, what is the actual times of occurrence of the warning message? Obviously you should not get the times by checking if the result is a real number or not. There may be different types of warning messages in different problems. I want to know how many times a warning message will occur in a loop if it is not suppressed.

Thanks a lot!

$\endgroup$
  • $\begingroup$ In Mathematica, these are called messages, not exceptions. You can use Check to check for a particular message. $\endgroup$ – Szabolcs Oct 5 '15 at 16:29
  • $\begingroup$ Or you can check: Position[Int, 0] shows you when you will be dividing by zero, and Length[Position[Int, 0]] shows how many there are. $\endgroup$ – bill s Oct 5 '15 at 16:31
  • $\begingroup$ @ bill s, yours is too specified for this problem. $\endgroup$ – yanfyon Oct 5 '15 at 16:37
  • $\begingroup$ @ Szabolcs. Thanks a lot. But I still can not find a way to "check" how many times this message will occur if not suppressed. $\endgroup$ – yanfyon Oct 5 '15 at 16:39
  • $\begingroup$ Checking whether the result is a real number doesn't sound like such a bad idea. Why are you saying that this is obviously not the right way? It doesn't seem any worse than counting error messages. I agree with Bill that if we're looking for the "right" solution then we should check for the condition that causes the error in the first place. $\endgroup$ – Szabolcs Oct 5 '15 at 16:40
3
$\begingroup$

As suggested by Szabolcs, use Check

nLoop = 1000; (* larger to increase likelihood of message *)
Int = RandomInteger[{0, 100}, nLoop];

failCount = 0; (* initialize *)

SetAttributes[recip, {Listable}];

recip[x_] := Check[1/x, failCount++; $Failed]

reciprocal = recip[Int] // Quiet; (* suppress messages *)

failCount

(*  8  *)

Use of failCount is unnecessary, you can just Count the occurrences of $Failed

Count[reciprocal, $Failed] === failCount

(*  True  *)

EDIT: If you do not want to define a separate helper function then just use

reciprical = Check[1/#, $Failed] & /@ Int // Quiet;

Count[reciprical, $Failed]

Use of Do loops are generally inefficient and best avoided.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.