Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newspeer1.nac.net!newspump.sol.net!posts.news.twtelecom.net!nnrp3.twtelecom.net!not-for-mail From: CHARLES GILLINGHAM Newsgroups: comp.soft-sys.math.mathematica Subject: Variables in ProbabilityDistribution have strange "scoping" mechanism Date: Fri, 11 Apr 2014 06:08:54 +0000 (UTC) Sender: steve@smc.vnet.net Approved: Steven M. Christensen , Moderator Message-ID: References: <20140410070657.9900D69EE@smc.vnet.net> Lines: 62 Organization: Time-Warner Telecom NNTP-Posting-Date: 11 Apr 2014 06:14:54 GMT NNTP-Posting-Host: 75ca9f1c.news.twtelecom.net X-Trace: DXC=jK@^90T]7OWJfb:@jQ1\c[C_A=>8kQj6];[h;PUXBgbTPm3bW^Q9cmVEFiONJ7[GoV^5jE98WhlSW X-Complaints-To: abuse@twtelecom.net Xref: csiph.com comp.soft-sys.math.mathematica:16768 Bill Rowe brings up an interesting point. Does anyone know why the variables in ProbabilityDistribution use such an unusual form of scoping (forgive me if this is not the correct term). Why doesn't it operate analogously to Sum? Consider these four cases: Table[Sum[f[x], {x, 0, 3}], {y, 0, 3}] Table[Sum[f[x], {x, 0, 3}], {x, 0, 3}] Table[ProbabilityDistribution[f[x], {x, 0, 3}], {y, 0, 3}] Table[ProbabilityDistribution[f[x], {x, 0, 3}], {x, 0, 3}] In case 2, Mathematica correctly determines the scope of the inner variable, and knows that it is a different variable than the outer one. In case 4, Mathematica fails in a bizarre way when there is a collision between internally and externally scoped variables. Mathematica produces these nonsensical distributions: ProbabilityDistribution[f[x],{x,x,3}] ProbabilityDistribution[f[x],{x,0,x}] for reasons that are unclear. Mathematica works correctly for this case. Mathematica knows that globalVariableX is local in both Sum and ProbabilityDistribution, however note that ProbabilityDistribution renames it to x, which is not ideal, but workable. globalVariableX = 21 Sum[f[globalVariableX], {globalVariableX, 0, 3}] ProbabilityDistribution[f[globalVariableX], {globalVariableX, 0, 3}] ClearAll[globalVariableX] On Apr 10, 2014, at 12:06 AM, Bill Rowe wrote: > On 4/9/14 at 4:14 AM, cgillingham1@me.com (CHARLES GILLINGHAM) wrote: > >> Evaluate this: > >> ProbabilityDistribution[ >> Piecewise[{{0, x1 == 1 && x2 == 1}, {1/2, x1 == 1 && x2 == 0}, {0, >> x1 == 0 && x2 == 1}, {1/2, x1 == 0 && x2 == 0}}], {x1, 0, 1, >> 1}, {x2, 0, 1, 1} >> ] >> PDF[%][{1, 1}] > >> And you get: > >> Undefined > >> But the probability is obviously 0 > >> Is there something I am misunderstanding about how simple Boolean >> probability distributions should be set up? > > Start with a fresh session and pay attention to the syntax > coloring. Note that x1 and x2 are undefined and your syntax does > not make these local variables. That is entering > > f[x_]:=2 x > > will cause x to be colored differently than your x1, x2 above. > > You get undefined as a result since x1 and x2 are never defined. > >