The code below generates some data and defines a function f[x,cb]:=NMaximize[expr,x] that upon being evaluated returns: {function value at max $x$,the function maximizing $x$}.
Nobs = 100; wi = RandomVariate[BetaDistribution[2, 2], Nobs]; c = 0.1; \[Lambda]i = RandomVariate[BetaDistribution[2, 4], Nobs]; f[pbar_, cb_] := NMaximize[ Sum[((((1 - \[Lambda]i[[i]])*Min[wi[[i]], pbar] + \[Lambda]i[[i]]* c) - c)* Boole[wi[[ i]] - ((1 - \[Lambda]i[[i]])* Min[wi[[i]], pbar] + \[Lambda]i[[i]]*c) - cb >= Max[wi[[i]], 0]] + (pbar - c)* Boole[wi[[i]] - pbar >= Max[wi[[i]] - ((1 - \[Lambda]i[[i]])* Min[wi[[i]], pbar] + \[Lambda]i[[i]]*c) - cb, 0]]), {i, 1, Nobs}]*(1/Nobs), pbar ]; Evaluating the functions yields:
In[197]:= f[x, .1] Out[197]= {0.146006, {x -> 0.30858}} I want two plots:
- plot f[x,cb][[1]] in the {X,Y} space where X = cb / (Y - c)
- plot f[x,cb][[2]] in the {X,Y} space where X = cb / (Y - c)
Any suggestions on how I can do this? Since the function is not continuous I have tried working with DiscretePlot[], but I am having a hard time getting the plotting to work.
PS: the function is rather slow to evaluate--so any comments on how to write the function more efficiently are also appreciated.


