from java.awt import Color,Font
from java.util import *
from jhplot  import *
from org.apache.commons.math.stat.regression import SimpleRegression

print "\napache.commons.math:"
c1 = HPlot("Canvas",600,400,0.12,1, 1)
c1.visible(1)
c1.gTitle("Linear regression")
c1.setAutoRange()


reg=SimpleRegression()
p1= P1D("data")

rand = Random()
for i in range(200):
      x=10*rand.nextGaussian()
      y=20*x +50*rand.nextGaussian()+5.0
      p1.add(x,y)
      reg.addData(x,y) 
 
a1=reg.getIntercept()
a2=reg.getInterceptStdErr() 
b1=reg.getSlope()
b2=reg.getSlopeStdErr() 

func="P1*x+P0"
f1 = F1D(func, -40.0, 40.0)
f1.setPar("P0",a1)
f1.setPar("P1",b1)



f1.setColor(Color.blue)
p1.setStyle("p")
p1.setSymbolSize(3)
c1.setNameX("X")
c1.setNameY("Y")
# c1.setLegendFont(Font("SansSerif", Font.BOLD, 16))
c1.draw(p1)
c1.draw(f1)


print "Intercept=",a1,"+/-",a2
print "Slope=",a1,"+/-",a2
# jHepWork @S.Chekanov