#!/usr/bin/env python
# coding: utf-8

# In[9]:


import numpy as np
#Q1) without numpy
trans_matrix = [[0.75,0.15,0.1],[0.45,0.45,0.1],[0.3,0.1,0.6]]
dist = [1,0,0]
for i in range(0,60):
    dist = [distribution[0]*trans_matrix[0][0]+dist[1]*trans_matrix[1][0]+dist[2]*trans_matrix[2][0],dist[0]*trans_matrix[0][1]+dist[1]*trans_matrix[1][1]+dist[2]*trans_matrix[2][1],dist[0]*trans_matrix[0][2]+dist[1]*trans_matrix[1][2]+dist[2]*trans_matrix[2][2]]
dist = [round(dist[0],2),round(dist[1],2),round(dist[2],2)]
print("Q1 Ans)",'\n',"the stationary distribution for the Transition Probability Matrix is:",dist)


# In[14]:


#Q2) without numpy
xvalue = [3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.20]
yvalue = [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(y_hat,y):
    sqr = []
    n = len(y)
    for t in range(0,n):
        sqr.append((y_hat[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 = []
a1 = 0
a2 = 0
ms = 1000000
for i in float_range(37,39,0.01):
    for j in float_range(-5,2,0.01):
        a = regressor(xvalue,float(i),float(j))
        b = SSE(a, yvalue)
        if (b<ms):
            ms = b
            a1,a2 = 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)))


# In[17]:


#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]
sum_x = 0
sum_y = 0
for i in d1:
    sum_x = sum_x + i
mean_x = sum_x/5
for j in d2:
    sum_y = sum_y + j
mean_y = sum_y/5  
squaredev_x = 0
for k in d1:
    squaredev_x = squaredev_x + ((k - mean_x)**2)
squaredev_y = 0
for l in d2:
    squaredev_y = squaredev_y + ((l - mean_y)**2)
sdx = ((squaredev_x)/5)**0.5
sdy = ((squaredev_y)/5)**0.5
semidev_x = 0
semidev_y = 0
for d in d1:
    if (d<mean_x):
        semidev_x = semidev_x + ((d-mean_x)**2)
for e in d2:
    if (e<meany):
        semidev_y = semidev_y + ((e-mean_y)**2)
semivar_x = semidev_x/5
semivar_y = semidev_y/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")


# In[12]:


#Q4) with numpy cause found no other way to do e^x
X = [2.29, 19.98, 0.06, 12.01, 7.04, 2.44]
L = 0
lamda = 0
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("Q4 Ans)",'\n',"the value which maximizes the likelihood function of an Exponential Distribution for the given range for lambda is:",lamda)


# In[ ]:




