
#                                 "ASSIGNMENT - 1"
#                                    Roll no 421
# Q1 - 

 


trans_mat = [[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]*trans_mat[0][0]+distribution[1]*trans_mat[1][0]+
                    distribution[2]*trans_mat[2][0],distribution[0]*trans_mat[0][1]+
                    distribution[1]*trans_mat[1][1]+distribution[2]*trans_mat[2][1],
                    distribution[0]*trans_mat[0][2]+distribution[1]*trans_mat[1][2]+
                    distribution[2]*trans_mat[2][2]]

distribution = [round(distribution[0],2),round(distribution[1],2),round(distribution[2],2)]

print("Solution:",'\n',"The stationary distribution for the Transition Probability Matrix is:",distribution)
    
 
# Q2 - 
# Find the optimal parameter values ( 𝛽0 , 𝛽1 ) for the Simple Linear Regression model 
# built on the above data, from the list of values given

"Solution:"

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]

"Defining required functions~"
# Building regressor function from first principles
def regressor(X,b0,b1):
    list = []
    for u in X:
        list.append((u*b1)+b0)
    return list

# Building Sum of Squared Errors function from first principles
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

# Building function for predicted {fitted} values from first principles
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("Solution",'\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 - 

"Solution:"

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

# Using loops for each set of data given

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("Solution:",'\n',"Standard deviation for X :",round(sdx,3),
      '\n',"Standard deviation for Y :",round(sdy,3),'\n',"Semi variance for X :",
      round(semivarx,3),'\n',"Semi variance for y :",round(semivary,3),'\n',"Scale factor for the given variables is:",
      round(scale,3),'\n',"Thus, proved that SD isn't independent of scale;",'\n',
      "since var(nx) = var(x)**2, also the semi variance of y is scale factor squared multiplied by semi variance of x")


# Q4 - 
import numpy as np
X = [2.29, 19.98, 0.06, 12.01, 7.04, 2.44]
L = 0
lamda = 0
#Looping for the exponential fn:
    
for y in float_range(0.34,0.36,0.0001):
    
    q = 1
    for i in X:
        q = q*((float(y))*(np.exp(-(float(y))*i)))
    if (q>L):
        L = q
        lamda = y

print("Solution:",'\n',"The value which maximizes the likelihood function of an Exponential "
      "Distribution for the given range for lambda is:",lamda)




































































































