function [val]=asianvaluestr(a,b,r,S0,sig,T,K,R,n)
% computes the value of an asian option val using importance sampling
%returns:
%val=vector of simulated values over strata given by interval [a,b]
%Input parameters:
% S0=initial price, K=strike price
% sig = sigma, R=number of time increments in interval [0,T] (for valuation of asian option)
% [a,b] ; 0<=a<b<=1 [a,b] is the strata from which the final stock price is simulated
%  Repeats for a total of n simulations in the strata where U is in [a,b].
%returns  vector val containing n options prices for the given strata


L=unifrnd(a,b,1,n);
%sc=[]; 
WWT=norminv(L);
WT=sqrt(T)*WWT;
ZT=r*T-sig^2*T/2+sig*WT;
ST=S0*exp(ZT);

%Z=normrnd(0,1,n,R);

eps=T/R;

ZZ=[];

WCUR=repmat(0,1,n);

WPREV=repmat(0,1,n);
cumval=repmat(0,1,n);
for i=1:R
    Z=normrnd(0,1,1,n);
   % ZZ=[ZZ;Z];
    WCUR=((T-T*i/R)/(T-T*(i-1)/R))*WPREV+(((T*i/R)-(T*(i-1)/R))/(T-T*(i-1)/R))*WT+sqrt(((T-T*i/R)*(T*i/R-T*(i-1)/R))/(T-T*(i-1)/R))*Z;
    cumval=[cumval;WCUR];
    WPREV=WCUR;
end

cumval=cumval(2:end,1:end);
%cumval contains the WT for stock prices
matZ=repmat(0,R,n);
for i=1:R
   matZ(i,:)= cumval(i,:)*(sig)+(i/R)*T*(r-(sig^2)/2);
end

matS=S0*exp(matZ);

valm=mean(matS,1);
vv=exp(-r*T)*max(valm-K,0);
val=vv;
mean(vv);
stderror=sqrt(var(vv)/n);



