Let $\theta \sim Gamma(1,2)$ and $X_1,...,X_n$ iid such that $X_i|\theta \sim Poisson(\theta)$. It is asked to determine the best sample size $n^*$ such that the posteriori risk
$$L(\theta, d) = (\theta-d)^2 + log(n+1)$$
is minimum. That is, we must choose such that
$$ \int_X \int_{\Theta} L(\theta, d) f(\theta, x) d\theta dx$$
is minimum for $x = (x_1,...,x_n)$. For the quadratic loss, we know that it is minimized for the decision $$\delta(X) = E(\theta|X)$$ so I must to evaluate the expectation of the posteriori for each $n$, sum with $\log(n+1)$ and check when it is minimized.It is required to calculate the expectation via Monte Carlo.
My attempt: My approach is the following: Generate 1000 values for $\theta$ and, for each $theta_i$, generate 100 values from $X \sim Poisson(\theta_i)$, $i=1,...,1000$. Then, we calculate for each $n=1,...,100$
$$\frac{1}{n}\frac{1}{1000} \sum_{i=1}^{1000} \sum_{j=1}^{n} \theta(i)x(i,j) + log(n+1)$$
The following matlab code is supposed to do this:
function [theta,X] = minRisk()
% Generate 1000 values for theta
theta = gamrnd(1,2,1,1000);
X = [];
for i = 1 : size(theta,2)
% Simulate sample of X|theta for each theta
X = [X; poissrnd(theta(i),1,100)];
end
% Each line i of this matrix contains the values of X|theta(i) generated
% multiplied by theta(i)
for i = 1 : 1000
M(i,:) = theta(i) * X(i, :);
end
E=[];
for n = 1:100
E=[E; 1/(n*1000) * sum(sum(M(:,1:n))) + log(n+1)];
end
E
plot(1:100, E)
end
But plotting E, it have a logarithm behavior. I don't know what I am doing wrong. If one could check my code, I would be glad. R codes are welcome too.