I have two sets of functions which I want to implement them in a single function with an option indicating which set of functions should be used.
Suppose that I have two lists like:(updated example)
b = {-1, 1, 0, 2}}
u = {{1, 1, 1, 1}, {1, 0, 0, 1}, {1, 0, 1, 1}, {0, 1, 1, 0}, {1, 0, 1,
1}, {0, 1, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {0, 1, 1, 1}, {1, 1,
0, 0}}
and I have this function:
AbEst[b0_, u0_, method_] :=
Module[{t, b = b0, u = u0, l, a, pos, theta, dif, se},
t = Table[i, {i, -4, 4, .01}];
L[b_, t_, u_] :=
Product[p[b[[i]], t]^u[[i]]*(1 - p[b[[i]], t])^(1 - u[[i]]), {i,
1, Length[u]}];
p[b_, t_] := 1/(1 + Exp[-1.7*(t - b)]);
l = L[b, t, #] & /@ u;
a = PDF[NormalDistribution[0, 1], t];
pos = Table[a, {Dimensions[u][[1]]}]*l;
Which[method == "ML",
theta =
t[[Position[l[[#]], Max[l[[#]]]][[1, 1]] & /@
Range[Dimensions[u][[1]]]]],
method == "MAP",
theta =
t[[Position[pos[[#]], Max[pos[[#]]]][[1, 1]] & /@
Range[Dimensions[u][[1]]]]],
method == "EAP",
theta = pos.t/Total[Transpose[pos]]];
dif = (Table[t, {Dimensions[u][[1]]}] -
Partition[Flatten[Table[theta, {Length[t]}]], Length[t]])^2;
Which[method == "ML",
se = Sqrt[Total[Transpose[l*dif]]/Total[Transpose[l]]],
method == "MAP",
se = Sqrt[Total[Transpose[pos*dif]]/Total[Transpose[pos]]],
method == "EAP",
se = Sqrt[Total[Transpose[pos*dif]]/Total[Transpose[pos]]]];
Return[Transpose[{theta, se}]]]
Now it returns {theta,se}
for given method
. I want to change it such that it returns theta by default (i.e. without computing se
) and computes and returns {theta,se}
if it was requested.
1) How can I do that?
2) How can I re-write more efficient function in which fits my purpose?