function [ estV,estFrac ] = sphereVolMCImpExp( dim,nn,lambda )
%sphereVolMCImpExp : MC for hypersphere volume with exp importance sampling
% Monte Carlo estimate of volume of hypersphere in d dimensions
% outputs the volume and the fraction of the corresponding hypercube
% we choose our points x with independent Cartesian components,
% the prob density for each component xi is
% exp(-lam.xi) / [ lam*((1-exp(-lam)) ]
% we achieve this by taking xi = (1/lam) log (1/y)
% where y is chosen uniformly in (exp(-lam),1)
estV = 0.0;
for i=1:nn
x = ranExp(dim,lambda);
xNorm2 = x' * x; % squared norm of vector
% (remember x' is transpose of x)
if ( xNorm2 < 1 )
% compute also the density at the relevant point
% this is the product of the densities of the indpt components
rho = 1;
for i=1:dim
% note that y(i) = exp( - lambda x(i) )
rho = rho * exp(-lambda*x(i)) * lambda / ( 1-exp(-lambda) );
end
estV = estV + 1/rho;
end
end
estV = estV/nn;
% this is the integral (volume of one "quadrant")
estFrac = estV;
% mult by 2^dim to get the vol of the full hypersphere
estV = estV * 2^dim;
end