       Re: Can model parameters be global?

• To: mathgroup at smc.vnet.net
• Subject: [mg80261] Re: Can model parameters be global?
• From: Neil Stewart <neil.stewart at warwick.ac.uk>
• Date: Thu, 16 Aug 2007 04:41:54 -0400 (EDT)
• Reply-to: Neil Stewart <neil.stewart at warwick.ac.uk>

```A brief summary of responses to question about whether model parameters are
best implemented as local or global variables. (Original post below)

David Bailey highlighted Needs["Units`"] and Needs["PhysicalConstants`"] and
suggested giving globals long names (e.g., SpeedOfLightInWater, not c) to
minimise confusion. David explained that using LocalizeVariables -> False in
Manipulate is designed to save the final setting after manipulation, and
that this is a positive feature.

Ben suggested using Replace and ReplaceAll for parameters which sometimes
change:
> a1=0 (* a1 never changes *)
> case1:={a2->0} (* a2 varies across scenarios *)
> case2:={a2->1}
> f[a3_]:=a1+a2+a3 (* a3 varies continuously, e.g., in plots *)
> f/.case1

Nasser Abbasi warned of his experience with globals and suggested keeping
all parameters local. David Annetts also warned about globals and suggested
polymorphism and defaults to make things easier:
> Imagine you have a model that in the general case you've implemented, has
> 5 parameters.
>
> ftn[a_, b_, c_, d_, e_]
>
> Often though, you don't need all 5 parameters. You can write
>
> ftn[a_, b_, c_, d_:default, e_:default]
>
> And use ftn[a, b, c] instead of ftn[a, b, c, d, e].
>
> Which is fine if d & e are used less often.
>
> You can also write
>
> ftn[a_, b_, d_] := ftn[a, b, c's default, d, e's default]
>
> If that's a more natural way of ordering the input parameters.

Thank you to everyone who replied!

Original post:
> When implementing a mathematical model in physics or psychology, for
> example, how do other people deal with model parameters in Mathematica?
> Would you represent the speed of light as a global variable or a local
> variable. For example, would you use
>
> Energy[m_]:=m*c^2 (* c is a global variable *)
>
> or
>
> Energy[m_,c_]:=m*c^2 (* c is a local variable *)
>
> ?
>
> The first seems neater. But problems arise in psychology, my domain, where
> the values of model parameters are unknown and are left as free
> parameters,
> adjusted to best-fit the data.
>
> Both local and global methods work well with optimisation. For example,
>
> NMinimize[Energy,{c}]
> {0., {c -> 0.}}
>
> and
>
> NMinimize[Energy[1,c],{c}]
> {0., {c -> 0.}}
>
> But the global variable solution does not work well with Manipulate.
> For example,
>
> Manipulate[Dynamic[Energy], {c, 0, 1}, LocalizeVariables -> False]
>
> works, but looks a right mess and also results in c taking a value that
> needs a Clear[c] before using other functions like NMinimize. On the other
> hand the local variable version
>
> Manipulate[Energy[1, c], {c, 0, 1}]
>
> is nice and simple. But the local variable solution results in having to
> pass all of the model parameters to the function. This is fine in this
> trivial example, but becomes unwieldy when there are ten model parameters
> and the model is defined using a set of functions. (A c-like struct could
> help, but there does not seem to be a neat way to do this in Mathematica.)
>
> So what do other people do? I'd be really interested to hear.

```

• Prev by Date: Re: Evaluating a convolution integral in Mathematica
• Next by Date: Re: Complexity explosion in linear solve
• Previous by thread: Re: Can model parameters be global?
• Next by thread: show residuals of circle fit