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]]]