Plot regression line in XY scatter chart

I am enjoying the journey of discovering more of the functionality of Amibroker!

Generally, it is useful for me to visualize results. In the below code, I attempt to plot the asymmetrical characteristic of implied volatility in index options. I am attempting to use the XY scatter charts before moving on to learn gfx (which may be the appropriate solution).

The black dots represent pre-processed binned data representing the average relationship between log returns and implied volatility in an index (SPY) for 2017. The orange dots represent a continuous linear regression line for all data plotted (both positive and negative log returns). The green dots represent the regression line for only positive log returns, and the red dots represent the regression line for only negative log returns.

It is common to graph a regression line(s) in XY scatter plots. But, after much research on this forum and google, I cannot discern a way to do this other than with the coordinate dots (as outlined above). That is, I cannot connect the orange dots separately, the red dots separately, the green dots separately and the black dots separately. If I style the XYChartAxis() with styleLine, the connection is programmed to connect all the dots in aggregate and I cannot discern a way to allow separation.

Is it possible to plot separate regression lines (or any lines) in the XY scatter plot? @Tomasz mentioned in this post that he was considering adding a similar feature in a future development. Does anyone know if this has been implemented? Is my requirement something that would be more appropriately handled in gfx?

I truly appreciate the experience and professionalism offered on this forum and look forward to learning more.

Kind regards.

// exploration

posA = posB = posSumX = posSumY = posSumXY = posSumX2 = 0; // regression line for positive logReturns
negA = negB = negSumX = negSumY = negSumXY = negSumX2 = 0; // regression line for negative logReturns
a = b = sumX = sumY = sumXY = sumX2 = 0; // regression line for continuous logReturns

// pre-processed data, binned at $1 wide strikes ( 1/spot0 )
midBin[0] = -0.034091112;
midBin[1] = -0.030502574;
midBin[2] = -0.026914036;
midBin[3] = -0.023325498;
midBin[4] = -0.01973696;
midBin[5] = -0.016148421;
midBin[6] = -0.012559883;
midBin[7] = -0.008971345;
midBin[8] = -0.005382807;
midBin[9] = -0.001794269;
midBin[10] = 0.001794269;
midBin[11] = 0.005382807;
midBin[12] = 0.008971345;
midBin[13] = 0.012559883;
midBin[14] = 0.016148421;
midBin[15] = 0.01973696;
midBin[16] = 0.023325498;
midBin[17] = 0.026914036;

avgVolDiff[0] = 0.100732148;
avgVolDiff[1] = 0.117457709;
avgVolDiff[2] = 0.06869593;
avgVolDiff[3] = 0.05300329;
avgVolDiff[4] = 0.045185022;
avgVolDiff[5] = 0.032521213;
avgVolDiff[6] = 0.026830977;
avgVolDiff[7] = 0.023176704;
avgVolDiff[8] = 0.010523093;
avgVolDiff[9] = 0.002684039;
avgVolDiff[10] = -0.00346206;
avgVolDiff[11] = -0.008633991;
avgVolDiff[12] = -0.020071165;
avgVolDiff[13] = -0.024138759;
avgVolDiff[14] = -0.028698314;
avgVolDiff[15] = -0.043775271;
avgVolDiff[16] = -0.063140221;
avgVolDiff[17] = -0.054909837;

count = 18;
for( i = 0; i < count; i++ )
{
	if( midBin[i] < 0 )
	{   // calculate regression for negative logReturns
		negSumX = negSumX + midBin[i];
		negSumY = negSumY + avgVolDiff[i];
		negSumXY = negSumXY + midBin[i] * avgVolDiff[i];
		negSumX2 = negSumX2 + midBin[i] * midBin[i];	
	}
	else
	{   // calculate regression for positive logReturns
		posSumX = posSumX + midBin[i];
		posSumY = posSumY + avgVolDiff[i];
		posSumXY = posSumXY + midBin[i] * avgVolDiff[i];
		posSumX2 = posSumX2 + midBin[i] * midBin[i];	
	}
	// calculate continuous regression for all logReturns
	sumX = sumX + midBin[i];
	sumY = sumY + avgVolDiff[i];
	sumXY = sumXY + midBin[i] * avgVolDiff[i];
	sumX2 = sumX2 + midBin[i] * midBin[i];
}

posA = ( posSumY * posSumX2 - posSumX * posSumXY ) / ( count * posSumX2 - posSumX * posSumX );
posB = ( count * posSumXY - posSumX * posSumY ) / ( count * posSumX2 - posSumX * posSumX );
negA = ( negSumY * negSumX2 - negSumX * negSumXY ) / ( count * negSumX2 - negSumX * negSumX );
negB = ( count * negSumXY - negSumX * negSumY ) / ( count * negSumX2 - negSumX * negSumX );
a = (sumY * sumX2 - sumX * sumXY ) / ( count * sumX2 - sumX * sumX );
b = ( count * sumXY - sumX * sumY ) / ( count * sumX2 - sumX * sumX );

changePer1PctPos = .01 * posB + posA;
changePer1PctNeg = -.01 * negB + negA;

j = 0;
for( j = 0; j < count; j++ )
{
	XYChartAddPoint( "pvSlope", "", midBin[j], avgVolDiff[j], colorBlack, colorBlack ); // avgVolDiff by $1 bins
	if( midBin[j] <=0 )
	{
		XYChartAddPoint( "pvSlope", "", midBin[j], midBin[j] * negB + negA, colorRed, colorRed ); // discontinuous regression line identifying negative logReturn
	}
	else
	{
		XYChartAddPoint( "pvSlope", "", midBin[j], midBin[j] * posB + posA, colorGreen, colorGreen ); // discontinuous regression line identifying positive logReturn
	}
	XYChartAddPoint( "pvSlope", "", midBin[j], midBin[j] * b + a, colorOrange, colorOrange ); // continuous regression line
}

XYChartSetAxis( "pvSlope", "[logReturn]", "[IVchange]" );

Here for example (multi symbol line plot).


You have to add Null point to begin new line.

XYChartAddPoint( chartname, "", Null, Null ); // add a NULL point to begin new line	

So,

/// your code (without xychart* code) from
/// @link https://forum.amibroker.com/t/plot-regression-line-in-xy-scatter-chart/21223
/// below


/// Exploration plot snippet
XYChartSetAxis( "pvSlope", "[logReturn]", "[IVchange]", styleLine );

for( j = 0; j < count; j++ )
	XYChartAddPoint( "pvSlope", "", midBin[j], avgVolDiff[j], colorBlack, colorBlack); // avgVolDiff by $1 bins

XYChartAddPoint( "pvSlope", "", Null, Null );	// add a NULL point to begin new line	

for( j = 0; j < count; j++ )
{
	if( midBin[j] <=0 )
		XYChartAddPoint( "pvSlope", "", midBin[j], midBin[j] * negB + negA, colorRed, colorRed ); // discontinuous regression line identifying negative logReturn
	else
		XYChartAddPoint( "pvSlope", "", midBin[j], midBin[j] * posB + posA, colorGreen, colorGreen ); // discontinuous regression line identifying positive logReturn
}

XYChartAddPoint( "pvSlope", "", Null, Null );	// add a NULL point to begin new line	

for( j = 0; j < count; j++ )
	XYChartAddPoint( "pvSlope", "", midBin[j], midBin[j] * b + a, colorOrange, colorOrange ); // continuous regression line

17

6 Likes

@fxshrat,

Fantastic! I actually looked at the example you mentioned but didn't notice that adding a NULL point would begin a new line. Now I do! Thank you so much for your answer.

What I have noticed is that you consistently give on-point, detailed, and comprehensive insights to many people. My understanding of Amibroker is growing because of this forum.

Kind regards.

2 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.