Message Boards Message Boards

Module 'only assignments to symbols' error

Posted 11 years ago
I made this code in order to simulate a pdf using Metropolis-Hastings.  It works fine without "Module" but I need to use it as a function; when i do it does not work:
 Clear["Global`*"];
 Normalconmediayvarianzadesconocida[medida1_, medida2_, medida3_,
   medida4_, medida5_, limiteinferiordemedia_, limitesuperiordemedia_,
   limiteinferiordevarianza_, limitesuperiordevarianza_,
   valorinicialparamedia_, valorinicialparavarianza_, pasoparamedia_,
   pasoparavarianza_, iteraciones_] :=
  Module[{Subscript[x, 1] = medida1, Subscript[x, 2] = medida2,
    Subscript[x, 3] = medida3, Subscript[x, 4] = medida4,
    Subscript[x, 5] = medida5, a = limiteinferiordemedia,
   b = limitesuperiordemedia, c = limiteinferiordevarianza,
   d = limitesuperiordevarianza,
   Subscript[\[Xi], 0] = valorinicialparamedia,
   Subscript[\[Upsilon], 0] = valorinicialparavarianza,
   s = pasoparamedia, w = pasoparavarianza, m = iteraciones},
  t1 = Table[Subscript[\[Xi], 0], {1}];
  t2 = Table[Subscript[\[Upsilon], 0], {1}];
  Do[U1 = RandomReal[];
   \[Xi] = Last[t1];
   If[U1 < 0.5, y1 = \[Xi] + s, y1 = \[Xi] - s];
   U2 = RandomReal[];
   \[Upsilon] = Last[t2];
   If[U2 < 0.5, y2 = \[Upsilon] + w, y2 = \[Upsilon] - w];
   r = \!\(
\*UnderoverscriptBox[\(\[Product]\), \(i = 1\), \(n\)]
\*FractionBox[
SuperscriptBox[\(E\), \(-
\*FractionBox[
SuperscriptBox[\((
\*SubscriptBox[\(x\), \(i\)] - y1)\), \(2\)], \(2  y2\)]\)],
SqrtBox[\(y2\)]]\)/\!\(
\*UnderoverscriptBox[\(\[Product]\), \(i = 1\), \(n\)]
\*FractionBox[
SuperscriptBox[\(E\), \(-
\*FractionBox[
SuperscriptBox[\((
\*SubscriptBox[\(x\), \(i\)] - \[Xi])\), \(2\)], \(2  \
\[Upsilon]\)]\)],
SqrtBox[\(\[Upsilon]\)]]\) (1/(
        b - a) (HeavisideTheta[y1 - a] - HeavisideTheta[y1 - b]) 1/(
        Log[d/c] y2) (HeavisideTheta[y2 - c] -
          HeavisideTheta[y2 - d]))/(1/(
        b - a) (HeavisideTheta[\[Xi] - a] -
          HeavisideTheta[\[Xi] - b]) 1/(
        Log[d/c] \[Upsilon]) (HeavisideTheta[\[Upsilon] - c] -
          HeavisideTheta[\[Upsilon] - d]));
   U3 = RandomReal[]; If[U3 < r, y1 = y1, y1 = \[Xi]];
   t1 = Append[t1, y1];
   U4 = RandomReal[]; If[U4 < r, y2 = y2, y2 = \[Upsilon]];
   t2 = Append[t2, y2], {m - 1}];
  zz = Transpose[Append[{t1}, t2]]
    Histogram[t1]
    Histogram[t2]
    Histogram3D[zz,
     ColorFunction ->
      Function[{height}, ColorData["Rainbow"][height]]]]
Normalconmediayvarianzadesconocida[12, 13, 13, 13, 13, 0, 15, 0.2, 3, 12, 0.2, 0.2, 0.04, 28000]


The original code, without "Module" (obs.: it works) is:
 Clear["Global`*"];
 Subscript[x, 1] = 12;
 Subscript[x, 2] = 13;
 Subscript[x, 3] = 13;
 Subscript[x, 4] = 13;
 Subscript[x, 5] = 13;
 n = 5;
 a = 0;
 b = 15;
c = 0.2;
d = 3;
Subscript[\[Xi], 0] = 12;
Subscript[\[Upsilon], 0] = 0.2;
s = 0.2;
w = 0.04;
m = 28000;
t1 = Table[Subscript[\[Xi], 0], {1}];
t2 = Table[Subscript[\[Upsilon], 0], {1}];
Do[U1 = RandomReal[];
  \[Xi] = Last[t1];
  If[U1 < 0.5, y1 = \[Xi] + s, y1 = \[Xi] - s];
  U2 = RandomReal[];
  \[Upsilon] = Last[t2];
  If[U2 < 0.5, y2 = \[Upsilon] + w, y2 = \[Upsilon] - w];
  r = \!\(
\*UnderoverscriptBox[\(\[Product]\), \(i = 1\), \(n\)]
\*FractionBox[
SuperscriptBox[\(E\), \(-
\*FractionBox[
SuperscriptBox[\((
\*SubscriptBox[\(x\), \(i\)] - y1)\), \(2\)], \(2  y2\)]\)],
SqrtBox[\(y2\)]]\)/\!\(
\*UnderoverscriptBox[\(\[Product]\), \(i = 1\), \(n\)]
\*FractionBox[
SuperscriptBox[\(E\), \(-
\*FractionBox[
SuperscriptBox[\((
\*SubscriptBox[\(x\), \(i\)] - \[Xi])\), \(2\)], \(2  \
\[Upsilon]\)]\)],
SqrtBox[\(\[Upsilon]\)]]\) (1/(
       b - a) (HeavisideTheta[y1 - a] - HeavisideTheta[y1 - b]) 1/(
       Log[d/c] y2) (HeavisideTheta[y2 - c] -
         HeavisideTheta[y2 - d]))/(1/(
       b - a) (HeavisideTheta[\[Xi] - a] -
         HeavisideTheta[\[Xi] - b]) 1/(
       Log[d/c] \[Upsilon]) (HeavisideTheta[\[Upsilon] - c] -
         HeavisideTheta[\[Upsilon] - d]));
  U3 = RandomReal[]; If[U3 < r, y1 = y1, y1 = \[Xi]];
  t1 = Append[t1, y1];
  U4 = RandomReal[]; If[U4 < r, y2 = y2, y2 = \[Upsilon]];
  t2 = Append[t2, y2], {m - 1}];
zz = Transpose[Append[{t1}, t2]];
Histogram[t1]
Histogram[t2]
Histogram3D[zz,
ColorFunction -> Function[{height}, ColorData["Rainbow"][height]]]
POSTED BY: fgarcia
2 Replies
The error message that you get is enough to understand the nature of the problem. You should always try to reduce your massive code to a simple short version pinpointing the issue. It is generally hard for other people to dig through that much of code. For instance, your problem can be already seen here - this version works:
Module[{x1 = x0}, x = x0]
But this one 
Module[{Subscript[x, 1] = x0}, x = x0]
gives an error message
Module::lvset: Local variable specification {Subscript[x, 1]=x0} contains Subscript[x, 1]=x0, which is an assignment to Subscript[x, 1]; only assignments to symbols are allowed. >>
Now it is clear from the message and the short code that the issue is using indexed subscript notation for local Module variables. You are making an assignment to an object more complex than a symbol (a letter like x or a combination like xy2). In future code version I would recommend avoiding using indexed subscript notation and call things like Subscript[x, 1] simple x1. You also have to learn work with error messages - clicking on >> at the end of the message would give you a page with explanation.
POSTED BY: Vitaliy Kaurov
Posted 11 years ago
Hi Vitaliy.; thank you!
POSTED BY: fgarcia
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