# WhatIf

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## Whatif(expr, v, x)

Returns the value of expression «expr», after assigning value «x» to variable «v».

Whatif helps you explore the effects of a change to a variable, without making it permanent. After evaluation, it returns «v» back to its original definition, and so makes no permanent change to the model.

Whatif(expr: Expression; v: Variable; x ; preserve : optional boolean)

## WhatIf(expr, v, x, i)

(requires Analytica 5.0)

Returns the result of evaluating «expr» after altering one slice at a time of «v» along «i», setting that slice to the corresponding slice of «x» along «i». This results in «expr» being evaluated once for every element of «I», plus one addition time for the base value. Any number of indexes can be listed, for example WhatIf(expr, v, x, i, j, k), in which case it re-evaluates «expr» for every combination of indexes. The result will have all the listed indexes.

Use WhatIf with one or more indexes when you want to generate a tornado diagram or other local sensitivity analyses when you want to see the effect on «expr» of changing one slice at a time of the input «v», assigning the slice to the corresponding slice in «x».

## WhatifAll(expr, vars, newVals)

Returns an array of values indexed by «vars», a list of variables, obtained by evaluating expression «expr», after assigning to each variable in «vars» the corresponding value from «newVals». Usually, «newVals» is an array indexed by «vars».

Use WhatifAll for generating tornado diagrams and other sensitivity analyses when you want to see the effect on «expr» of changing each variable one-at-a-time. Whatif helps you explore the effects of a change to a variable, without making it permanent. After evaluation, it returns the variables in «vars» back to their original definition, and so makes no permanent change to the model.

Whatifallexpr: Expression; vars: Variable; newVals ; preserve : optional Boolean)

We generally recommend using WhatIfAll only when all your input variables have the same indexes. More specifically, when the new value for each input has the same dimensionality. Otherwise, when your form «newVals», all the indexes are combined. The following pattern using WhatIf is equivalent to WhatIfAll and, we think, is actually easier to understand, more flexible, and can be easily tailored to avoid this combination of dimensionalities:

LocalAlias X := vars Do WhatIf( expr, X, newVal )

When using this, the value of «vars» must be a list of handles. Another useful pattern here is to make vars an index node and set its MetaOnly attribute, and then make it a list (not a list of text) and enter the identifier of each input in each cell of the list. The MetaOnly flag ensures the value of vars is a list of handles, rather than the values of each input variable (which you would get if the MetaOnly flag was off).

## Preserve Computed Values

By default, Whatif and WhatIfAll preserve the initial value of «V», and any variables that depend on it, and restore these values after evaluation. This avoids having to recompute «V» if the results are required later, inclduing Monte Carlo samples (which do not need to be resampled). Two related WhatIf's descending from the same varying variable don't invalidate each other.

There is a downside to preserving previously computed values -- it increases memory consumption. In a large model that uses most available memory, preserving these values during WhatIf or WhatIfAll might exhaust memory. In this case, you can ask WhatIf or WhatIfAll not to preserve these values by setting the optional «preserve» parameter to false, e.g.:

WhatIf(expr, v, x, preserve: false)

## Changing two or more input variables

If you need to change two or more inputs at the same time, then you should nest calls to WhatIf. For example, to set variable x1 to 10 while simultaneously setting x2 to 20 while evaluating y, use:

WhatIf( WhatIf( y, x1, 10 ), x2, 20 )

You should take note of the distinction between WhatIfAll and nested WhatIf, which both change multiple inputs. The difference is that WhatIfAll changes one variable at a time, whereas nested WhatIfs cause all variables to be changed at the same time.

## History

• Prior to Analytica 4.1, use of WhatIf or WhatIfAll caused previously computed results downstream of the varied variable(s) to be invalidated, requiring a recomputation of these results the next time they are requested. Starting with Analytica 4.2, this behavior has changed -- WhatIf and WhatIfAll now preserve previously computed values.
• The optional repeated index parameter was introduced in Analytica 5.0.