# -*- coding: utf-8 -*-
"""
Created on Fri Jan 29 14:49:34 2016

@author: manuel.pasieka@vbcf.ac.at
"""

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import numpy.random

N = 10
NoiseScale = 0.5

#Generate data points following a line with some noise
[lIntercept, lSlope] = (np.random.random(2) * 2.0) - 1.0
noise = ((np.random.random(N) * 2.0) - 1.0) * NoiseScale
X = np.linspace(-1, 1, N)
Y = []
for i in range(N):
    Y.append(X[i]*lSlope + lIntercept + noise[i])  

print('Data generated by line: Slope %f, Intercept %f + Noise abs %f' % (lSlope, lIntercept, np.abs(noise).sum()) )

slope, intercept, r_value, p_value, std_err = stats.linregress(X, Y)
print('Results: Slope = %f, Intercept = %f, r %f' % (slope, intercept, r_value))

f = plt.figure()
plt.title('Simple Linear Regression Example')
plt.xlabel('X - Indepened Variable')
plt.ylabel('Y - Depended Variable')

#Print Points
plt.scatter(X, Y, label='Points')

#Regression Line
rL_X = [-1.0, 0.0, 1.0]
rL_Y = [(x * slope) + intercept  for x in rL_X]
plt.plot(rL_X, rL_Y, color='r', label='Regression Line')

#Original Line
lY = [(x * lSlope) + lIntercept  for x in rL_X]
plt.plot(rL_X, lY, color='b', linestyle='--', label='Original Line')

plt.legend()