You are on page 1of 2

9/13/23, 11:52 AM Assign 1 Q2.

2 for ISE 508

In [2]: import pandas as pd


import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from scipy.stats import t

In [3]: df = pd.read_csv('Data.csv')

In [4]: # data segregation into S and W


S = df[['Year']]
W = df['Demand']
S_bar=np.mean(S)
f=(S-S_bar)**2
f_bar=np.sum(f)

In [5]: # Logic for Forecasting of year 13


logic = LinearRegression()
logic.fit(S, W)
prediction_Yr= 13
prediction_D= logic.predict(np.array([[prediction_Yr]]))[0]

In [6]: CL = 0.95
alpha = 1 - CL

In [7]: # Error calculation or estimate of Forecast Standard Error


W_pred = logic.predict(S)
remaining = W - W_pred
mean_se = (remaining ** 2)/10
mean_se=np.sum(mean_se)
forecast_error=np.sqrt(mean_se*(1+(1/10)+((13-S_bar)**2)/f_bar))

In [8]: n = len(S)
p = 1
degrees_of_freedom = n - p - 1

In [9]: t_value = t.ppf(1 - alpha / 2, df=degrees_of_freedom)


print(t_value)
error_range = t_value * forecast_error
lower_level = prediction_D - error_range
upper_level = prediction_D + error_range
correlation_coefficient = df['Year'].corr(df['Demand'])

2.2281388519649385

localhost:8888/nbconvert/html/Assign 1 Q2.2 for ISE 508.ipynb?download=false 1/2


9/13/23, 11:52 AM Assign 1 Q2.2 for ISE 508

In [10]: print(f"Prediction of year {prediction_Yr}: {prediction_D:.2f}")


print(f"Confidence level {CL * 100}% Confidence interal around forecast: {lowe
r_level[0]:.2f} , {upper_level[0]:.2f}")
print(f"Correlation Coefficient: {correlation_coefficient:.2f}")

Prediction of year 13: 98116.67


Confidence level 95.0% Confidence interal around forecast: 83267.92 , 112965.
42
Correlation Coefficient: 0.94

In [11]: plt.figure(figsize=(11, 7))


plt.plot(df['Year'], df['Demand'], label='Previous Data', marker='o', color='p
urple')
plt.plot(df['Year'], logic.predict(S), color='red', label='Trendline')
plt.plot(prediction_Yr, prediction_D, marker='s', color='green', label='Predic
ted year')
plt.xlabel('Year')
plt.ylabel('Demand')
plt.title('Forecasting of Demand using Linear Regression')
plt.legend()
plt.grid(True)
plt.show()

In [12]: # Note: here the 10^3 is already multiplied that is why the value is 4292.307
instead of 4.292 where S is the Time period
a1=logic.coef_[0]
a0=logic.intercept_
print(f" Equation of the given line is : {a0:.2f} + {a1:.2f}*S")

Equation of the given line is : 42316.67 + 4292.31*S

localhost:8888/nbconvert/html/Assign 1 Q2.2 for ISE 508.ipynb?download=false 2/2

You might also like