Message Boards Message Boards

Plot with and without `Table` produce different results

Posted 6 months ago

Suppose a function

v = ((i + lambda) Uem - (-1 + e) Uun)/(i (1 - e + i + lambda))

where

n = 1; beta = 0.6; i = 0.05; W = 60;
L = (w/(beta (a e)^beta))^(1/(beta - 1));
lambda = ((1 - e) n L)/(1 - e n L);
Uem = w - 1/(w (1 - e));
b[e_] := b /. Solve[a W (1 + i)^(1/(1 - e) + 1/lambda - 1)- b \!\(\*UnderoverscriptBox[\(\[Sum]\), \(j = 1\), \(\*FractionBox[\(1\), \(lambda\)] - 1\)]\(\((1 + i)\)^j\)\) == b + a W, b][[1]];
Uun = b[e] - 1/b[e];

I would like to plot e that maximizes v with respect to w for a specific value of a, for example, a = 0.9. I tried two different ways, i.e. with and without Table, expecting to get the same result but I didn't.

The first one is

a = 0.9;
brf[w_] := ArgMax[{v, e > 0, e < 1, Uem > Uun}, e];
Plot[brf[w], {w, 0, 12}, PlotRange -> {{0, 12}, {0, 1}}]

which yielded

enter image description here

The second one uses Table:

brf[a_, w_] := ArgMax[{v, e >= 0, e <= 1, Uem > Uun}, e];
Plot[Evaluate@Table[Simplify@brf[a, w], {a, {0.9}}], {w, 0, 12}]

which yielded

enter image description here

Why do they produce different results? Aren't they supposed to produce the same result?

POSTED BY: Ian P
6 Replies

If I evaluate brf[AAA, WWW] there are no instances of AAA or WWW. This is because the definition,

brf[a_, w_] := ArgMax[{v, e >= 0, e <= 1, Uem > Uun}, e];

is a "literal" rewrite rule. It replaces the instances of a and w that literally appear in the (unevaluated) text ArgMax[{v, e >= 0, e <= 1, Uem > Uun}, e]. However there are no symbols a or w present before it is evaluated. The a and w appear only when the RHS is evaluated, which happens after the literal replacement has finished. But I second Gianluca Gorni's suggestion that the plot looks like there were numerical issues.

If you wish not to write the RHS with a and w present, you could try to define brf as follows:

brf[a0_, w0_] := 
 Block[{a = a0, w = w0}, ArgMax[{v, e >= 0, e <= 1, Uem > Uun}, e]];

Note that Plot[] and Table[] effectively use Block[] to give values to w and a respectively, so the functions almost work inside those commands. In the first plot, brf[w] is evaluated when w has been given a value by Plot[]; so it works properly, more or less. I get an error from NMaximize which you don't mention. In the second plot, brf[a, w] is evaluate when Table[] has given a the value 0.9 but w has no value. The result is then evaluated in Plot[] after w has been given a value. Maybe this works, maybe not; it may explain the difference. I couldn't track it down.

I would define brf[] as follows:

brf // ClearAll;
brf[a0_?NumericQ, w0_?NumericQ] := 
  Block[{a = a0, w = w0}, ArgMax[{v, e >= 0, e <= 1, Uem > Uun}, e]];

Or I would make the dependence of Uem on w etc. explicit (define it to be a function Uem[w,...]).

See https://support.wolfram.com/12502 for a discussion. I don't know if the above fixes the plot because Plot[] ran for several minutes, longer than it took to type this up. I wasn't sure when it would finish and had to move on to other things.

POSTED BY: Michael Rogers
Posted 6 months ago

@Michael Rogers, I tried your first suggested code and it worked producing the same plot as the first plot! And it didn't take too long. Thank you so much!! May I ask one more question? Looking at the first plot, I would like to truncate the curve at where it touches the x-axis from the right (that's somewhere around 2.7) to remove the flat portion and spike portion of the curve, eventually leaving that nice curve. Do you know how to do this?

POSTED BY: Ian P
Posted 6 months ago

Hi, If the number where you want the plot to start is 2.7, then change the plot command to

Plot[brf[w], {w, 2.7, 12}, PlotRange -> {{0, 12}, {0, 1}}]

Plot[] will automatically choose the axes origin. If it's not (0,0) and you would like it to be, then you can control the placement of the axes with the option AxesOrigin. For instance,

Plot[brf[w], {w, 0, 12}, PlotRange -> {{0, 12}, {0, 1}}, AxesOrigin -> {0, 0}]
POSTED BY: Updating Name
Posted 6 months ago

@Michael Rogers, I have posted another related question here. If possible, may I ask for your help once more? Thank you very much in advance!

POSTED BY: Ian P

The second version has a Simplify which may raise numerical issues, which I have not tried to investigate.

A curious feature that I noticed in your code is that in your function definition brf[w_]:=rhs the right hand side has no explicit w. This way for example brf[1] does not evaluate to a number, while brf[w] /. w -> 1 does.

A simplified version of the issue is with the "function" x = a; f[a_] := x. Perhaps surprisingly, the following gives False:

{f[0], f[1]} === Table[f[a], {a, 0, 1}]
POSTED BY: Gianluca Gorni
Posted 6 months ago

@Gianluca Gorni, thanks for the comment. In brf[w_]:=rhs, the right hand side does have w; note that the objective function v has w through Uem.

POSTED BY: Ian P
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract