%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                         %
%     STS_Prototype       %
%                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%

% To be called by sts_algorithm

function [V]=m_prototype(U,w,X,a,c,d,e,f);

n_c=size(U,1);
[n_g,n_t]=size(X);

%%%%% Calculate m(i,k)
for i=1:n_c;
    m(i,:)=-sum(((U(i,:).^w)'*ones(1,n_t-2)).*(((ones(n_g,1)*d(2:end)).*X(:,1:n_t-2))+((ones(n_g,1)*e(2:end)).*X(:,2:n_t-1))+((ones(n_g,1)*f(2:end)).*X(:,3:n_t))))./(ones(1,n_t-2)*sum(U(i,:).^w));
end;
m=[zeros(n_c,1) m];

%%%%% Calculate V(i,n)
for i=1:n_c;
    V(i,1)=0;
    V(i,2)=0;
    V(i,3)=m(i,2)/c(2);
    V(i,4)=m(i,2)*(c(1))*[a(3)+c(3)]+m(i,3)*[c(1)*c(2)];   
    for n=5:n_t;
        dum=cumprod(c(2:n-1));
        dummy=dum(end); 
        clear dum;
        for r=2:n-3;
            dum=cumprod(c(1:r-1));
            dummy10=dum(end);
            clear dum;
            dum=cumprod(a(r+1:n-1));
            dummy11=dum(end);
            clear dum;
            dum=cumprod(c(r+1:n-1));
            dummy12=dum(end);  
            clear dum;
            for p=r+3:n;
                dum=cumprod(c(p-1:n-1));
                dum1=dum(end);
                clear dum;
                dum=cumprod(a(r+1:p-2));
                dum2=dum(end);
                clear dum;
                dum13(p)=dum1*dum2;
                clear dum1 dum2;
            end;
            dummy13=sum(dum13);
            clear dum13;
            dummydum1(r)=dummy10*m(i,r)*[dummy11+dummy12+dummy13];
            clear dummy11 dummy12 dummy13;
        end;
        dummy1=sum(dummydum1);
        clear dummydum1;
        dum=cumprod(c(1:n-2));
        dummy2=dum(end)*m(i,n-1);
        clear dum;
        dum=cumprod(c(1:n-3));
        dummy3=dum(end)*m(i,n-2)*[a(n-1)+c(n-1)];
        clear dum;
        V(i,n)=(dummy1+dummy2+dummy3)/dummy;
        clear dummy dummy1 dummy2 dummy3;
    end;
end;                                                                                                                                                  
