
#Q1) without numpy
transition_matrix = [[0.75,0.15,0.1],[0.45,0.45,0.1],[0.3,0.1,0.6]]
distribution = [1,0,0]
for i in range(0,60):
    distribution = [distribution[0]*transition_matrix[0][0]+distribution[1]*transition_matrix[1][0]+distribution[2]*transition_matrix[2][0],distribution[0]*transition_matrix[0][1]+distribution[1]*transition_matrix[1][1]+distribution[2]*transition_matrix[2][1],distribution[0]*transition_matrix[0][2]+distribution[1]*transition_matrix[1][2]+distribution[2]*transition_matrix[2][2]]
distribution = [round(distribution[0],2),round(distribution[1],2),round(distribution[2],2)]
print("Q1 Ans)",'\n',"the stationary distribution for the Transition Probability Matrix is:",distribution)

#Q2) without numpy
xval = [3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.20]
yval = [19.2,17.8,16.4,17.3,15.2,10.4,10.4,14.7,32.4]
def regressor(X,b0,b1):
    list = []
    for u in X:
        list.append((u*b1)+b0)
    return list
def SSE(yhat,y):
    sqr = []
    n = len(y)
    for t in range(0,n):
        sqr.append((yhat[t]-y[t])**2)
    sums = 0
    for r in sqr:
        sums = sums + r
    return sums
def float_range(A, L=None, D=None):
    if L == None:
        L = A + 0.0
        A = 0.0
    if D == None:
        D = 1.0
    while True:
        if D > 0 and A>=L:
            break
        elif D<0 and A<=L:
            break
        yield ("%g" % A)
        A = A + D
pred = []
v1 = 0
v2 = 0
ms = 1000000
for i in float_range(37,39,0.01):
    for j in float_range(-5,2,0.01):
        a = regressor(xval,float(i),float(j))
        b = SSE(a, yval)
        if (b<ms):
            ms = b
            v1,v2 = float(i),float(j)
print("Q2 Ans)",'\n',"The optimal parameter values (𝛽0, 𝛽1) for the Simple Linear Regression model built on the above data for the given range is:",(round(v1,2),round(v2,2)))
        
        
#Q3) without numpy
d1 = [2.76,1.8,2.5,10.64,12.01]
d2 = [7.4,4.81,6.69,28.52,32.18]
sumx = 0
sumy = 0
for i in d1:
    sumx = sumx + i
meanx = sumx/5
for j in d2:
    sumy = sumy + j
meany = sumy/5  
sqdevx = 0
for k in d1:
    sqdevx = sqdevx + ((k - meanx)**2)
sqdevy = 0
for l in d2:
    sqdevy = sqdevy + ((l - meany)**2)
sdx = ((sqdevx)/5)**0.5
sdy = ((sqdevy)/5)**0.5
semidevx = 0
semidevy = 0
for d in d1:
    if (d<meanx):
        semidevx = semidevx + ((d-meanx)**2)
for e in d2:
    if (e<meany):
        semidevy = semidevy + ((e-meany)**2)
semivarx = semidevx/5
semivary = semidevy/5
d3 = []
for x in range(0,5):
    d3.append(d2[x]/d1[x])
ss = 0
for s in d3:
    ss = ss + s
scale = ss/5
print("Q3 Ans)",'\n',"Standard deviation of X is:",round(sdx,3),'\n',"Standard deviation of Y is:",round(sdy,3),'\n',"Semi variance of X is:",round(semivarx,3),'\n',"Semi variance of y is:",round(semivary,3),'\n',"Scale factor for the given variables is:",round(scale,3),'\n',"sd is not independent of scale",'\n',"as var(nx) = var(x)**2, the semi variance of y is scale factor squared multiplied by semi variance of x")

        
 
            
    

