/////////////////////////////////////////////////////////////////////
//   This contains various scilab commands to explore the stock    //
// price data                                                      //
//     This is the file on the internet                            //
/////////////////////////////////////////////////////////////////////


	clear; lines(0)

	M	= fscanfMat("SomeSP.txt");
		// You could use any datafile here, so long as the 
		// data is presented as an array, one row for each day,
		// the date as a numerical value in the first column, and
		// the prices in the subsquent columns. You can even have
		// non-numeric character strings as column headers.
	exec setup.sci;

	clf(); plot(M(:,2:$) );
	tt	= M(:,1)   ;
	X	= M(:,2:$) ;
	nd	= size(X,1) ;
	na	= size(X,2) ;

/////////////////////////////////////////////////////////////////////////
//                 STUDY 1:  Are log returns Gaussian ?
//  We order the returns, and then plot these ordered values against the
// quantiles of a standard N(0,1) distribution. If the data is Gaussian,
// the plot should look like a straight line ...

	pp	= (0.5:1:nd-1)'/(nd-1) ;
	qq	= cdfnor("X",zeros(pp),ones(pp),1-pp,pp) ;

	lnX	= log(X);
	rr	= lnX(2:$,:) - lnX(1:$-1,:) ;	// ... the log returns ...
	srr	= gsort(rr,'r');
			// Note the economy of this command; it sorts each
			// column of rr in decreasing order.
	str	= input("  Do you want to see qq plots of returns (y/n)?",'s');
	if str=="y" then
	for ii = 1:na,
		clf();plot(qq,srr(:,ii));
		xtitle("QQ plot for "+lbls(ii));
		xpause(2e5)
	end
	end

//////////////////////////////////////////////////////////////////////////
//    STUDY 2: Are log returns IID?
// If the returns are IID, we would expect the plots of the cumulative
// sum of the squared log-returns to grow approximately linearly with time ...

	str	= input("  Do you want to see realized qv returns (y/n)?",'s');
	if str=="y" then
	rr2	= rr.*rr;
	RR2	= cumsum(rr2,'r');
	for ii = 1:na,
		clf();plot(RR2(:,ii));
		xtitle("Realized quadratic variation of "+lbls(ii));
		xpause(2e5)
	end
	end

///////////////////////////////////////////////////////////////////////////
//    STUDY 3: Are the tails of the log-return distribution polynomial?
// There are various claims that we should model the tail of the log-return
// distribution as regularly varying. Here you see what the realized
// quadratic variation would look like if this were what happened.
// For small values of k (up to about 5), we see a much more "steppy"
// plot than the data shows. It's also more visibly linearly growing.

	kk	= input("  Exponent for the tail? (0 to skip this)");
	if kk>0 then
	uu	= grand(nd,1,'unf',0,1);
	zz	= uu.^(-1/kk) - 1;
	zz	= zz.*zz;
	clf();plot(cumsum(zz));
	xtitle("Realized QV if tail is like x^(-"+string(kk)+")");
	end

///////////////////////////////////////////////////////////////////////////
//    STUDY 4: Are log returns IID?
// If log returns are IID, we would expect the ACF to be nearly zero
// at positive lags ....

	str	= input("  Do you want to see ACF of returns (y/n)?",'s');
	if str=="y" then
	for ii = 1:na
		[cv,mn]	= corr(rr(:,ii),40);
		clf();plot([cv/cv(1);0.1*ones(cv);-0.1*ones(cv);zeros(cv)]');
		xtitle("Autocorrelation of returns of  "+lbls(ii));
		xpause(2e5)
	end
	end

//  ... which it is. However, we would also expect if log returns are 
// IID that the ACF of the absolute log-returns should be nearly zero
// at positive lags ....

	str	= input("  Do you want to see ACF of absolute returns (y/n)?",'s');
	if str=="y" then
	for ii = 1:na
		[cv,mn]	= corr(abs(rr(:,ii)),40);
		clf();plot([cv/cv(1);0.1*ones(cv);zeros(cv)]');
		xtitle("Autocorrelation of absolute returns of  "+lbls(ii));
		xpause(2e5)
	end
	end

// .... which it clearly is not ...
	




