% Please rename the file to the function name before use. % Tested on MATLAB R2013a % ------------------------------------------------------------------------- % Function: [TrueLrr,MeanLrrSims,StdLrrSims, MeanLrrApprox,StdLrrApprox] = % meanAndStdLForSubSampling( points, area, samplingRatio ,r) % ------------------------------------------------------------------------- % Aim: Estimate mean L(r)-r function and the standard deviation, if a set of % (true) points are subsampled by a given sampling ratio, based on the % analytical method presented in the paper % ------------------------------------------------------------------------- % Input: points - X and Y coordinates as column vectors (size(points,2)=2) % area - area of analysis % samplingRatio - What fraction of the points should be sampled % between 0 and 1; % r - radius values at which the estimates are to be made(array). % ------------------------------------------------------------------------- % Output: % TrueLrr - true L(r)-r function (all points) % MeanLrrSims - mean L(r)-r function for the subsampled points (simulations) % StdLrrSims - std in L(r)-r function for the subsampled points (simulations) % MeanLrrApprox - mean L(r)-r function for the subsampled points (analytical) % StdLrrApprox - std in L(r)-r function for the subsampled points (analytical) % ------------------------------------------------------------------------- % Comments: The code uses a fast integral approximation of the method % presented in the paper, using binomial theorem, to obtain MeanLrrApprox % and StdLrrApprox. For the method presented in the paper itself(estimateWithBruteMethod), please % uncomment the relevant lines. However, nchoosek runs into computational % problems when number of points become large. % ------------------------------------------------------------------------- % Written by Arun Shivanandan and Jayakrishnan Unnikrishnan EPF Lausanne, % Last updated 9 Dec 2014 by Arun Shivanandan. function [TrueLrr,MeanLrrSims,StdLrrSims, MeanLrrApprox,StdLrrApprox]=meanAndStdLForSubSampling( points, area, samplingRatio ,r) numSims=100; DIST = calcDist(points); L=calcLfunction(DIST,r,area); TrueLrr=L-r'; figure; hold on; plot(r, TrueLrr,'magenta'); [MeanLrrSims, StdLrrSims] = estimateWithSimulations( points, area, samplingRatio ,r,numSims); errorbar(r,MeanLrrSims,StdLrrSims,'green'); [MeanLrrApprox, StdLrrApprox] = estimateWithApproxMethod( points, area, samplingRatio ,r); errorbar(r,MeanLrrApprox,StdLrrApprox,'red'); legend('True points','Simulated subsampling','Approx. analytical'); %[MeanLrrBrute, StdLrrBrute] = estimateWithBruteMethod( points, area, %samplingRatio ,r); %errorbar(r,MeanLrrBrute,StdLrrBrute,'blue'); %legend('True points','Simulated subsampling','Approx. analytical','Exact K. analytical'); end function [MeanLrrSims, StdLrrSims] = estimateWithSimulations( points, area, samplingRatio ,r,numSims) LObserved = zeros(numSims,length(r)); for k=1:numSims subsampledPoints = subsamplePointPattern(points, samplingRatio ); DIST = calcDist(subsampledPoints); LObserved(k,:)=calcLfunction(DIST,r,area); end MeanLrrSims=mean(LObserved)-r; StdLrrSims=std(LObserved); end function [ MeanLrrApprox, StdLrrApprox] = estimateWithApproxMethod( points, area, samplingRatio ,r) DIST = calcDist(points); N=size(DIST,1); beta=samplingRatio/(1-samplingRatio); alphaN=(1-samplingRatio)^N; alpha=1-samplingRatio; funM1 = @(x,N) (((1+x)*alpha).^N ); fun = @(x,N) ((((1+x)*alpha).^N - alphaN)./x); fun1 = @(x,a, N) (((((1+x)*alpha).^N - alphaN)./(x)) .* (x <= a) .* (1./a)); fun2= @(x,a,b, N) (((((1+x)*alpha).^N - alphaN)./(x.*a.*b)) .* (x <= a) .* (a <= b)); qm1=funM1(beta,N)-alphaN; %computes S0 q0 = integral(@(x)fun(x,N),eps,beta); %computes S1 q1= integral2(@(x,a)fun1(x,a,N), eps, beta, eps, beta,'Method','iterated',... 'AbsTol',.0001,'RelTol',.0001); %computes S2 q2= integral3(@(x,a,b)fun2(x,a,b,N), eps, beta, eps, beta,eps, beta,'Method','iterated',... 'AbsTol',.0001,'RelTol',.001); %computes S3 Once=1 * q2 /N; TwiceForMean=1/(N*(N-1)) * (qm1-q0); Twice= 1/(N*(N-1)) * (q1-q2); Thrice= 1/(N*(N-1)*(N-2)) * (q0-3*q1 + 2*q2); Fourth = 1 / (N*(N-1)*(N-2)*(N-3)) * (qm1-6*q0 +11*q1 -6*q2); tt=zeros(1,length(r)); ss=zeros(1,length(r)); for ijk = 1:length(r) B = DIST < r(ijk); B = B.*(1-eye(size(B))); tt(ijk)= sum(sum(B)); a3= sum(sum(B).^2); D =triu(DIST < r(ijk)); D = D.*(1-eye(size(D))); a1 = sum(D(:))^2; a2 = sum(D(:)); ss1 = (a3 )*(Thrice-Fourth) + a1*(Fourth ) + a2*(Twice - 2*Thrice + Fourth); ss(ijk)=4*ss1; end ExpK = tt.'*TwiceForMean*area; ExpKsq =ss.'; var1 =ExpKsq; var=(var1(:)/((1*1/(area*1.0))*(1*1/(area*1.0))))-ExpK.*ExpK; MeanK=ExpK; StdK=sqrt(var); MeanLrrApprox=sqrt((MeanK)/pi)-r'; StdLrrApprox=sqrt((MeanK+StdK)/pi)-sqrt((MeanK)/pi); end function [ DIST ] = calcDist( Points ) A = bsxfun(@minus,transpose(Points),mean(transpose(Points),2)); S = full(dot(A,A,1)); DIST = sqrt(bsxfun(@plus,S,S')-full(2*(A'*A))); end function [MeanLrrBrute, StdLrrBrute] = estimateWithBruteMethod( points, area, samplingRatio ,r) NoOfPts=size(points,1); ExpKsq=zeros(length(r),1); DIST = calcDist(points); %===for variance=== fourtimes=0; thrice=0; twice=0; once = 0; %============== twiceformean = 0;%===for mean=== for kk = 1:NoOfPts oncenow = nchoosek(NoOfPts,kk)*samplingRatio^kk*(1-samplingRatio)^(NoOfPts - kk)/kk^4*kk/(NoOfPts); twiceformean = twiceformean + nchoosek(NoOfPts,kk)*samplingRatio^kk*(1-samplingRatio)^(NoOfPts - kk)/kk^2*kk/(NoOfPts)*(kk-1)/(NoOfPts-1); twicenow = oncenow*(kk-1)/(NoOfPts-1); thricenow = twicenow*(kk-2)/(NoOfPts-2); fourtimesnow = thricenow*(kk-3)/(NoOfPts-3); once = once + oncenow; twice= twice + twicenow; thrice = thrice+thricenow; fourtimes = fourtimes + fourtimesnow; end two =0; three = 0; four = 0; one = 0; count=0; ExpK = zeros(length(r),1); for i=1:NoOfPts for j=1:NoOfPts if i == j continue end t=(DIST(i,j)