```python
import numpy as np
import math
import matplotlib.pyplot as plt
import random
import sympy as sy
import sympy.physics.units as units
```


```python
k = random.randint(1,9)
end = (2*math.pi*k)/(1.5)
x = np.arange(0, end, end/1000)
n = x.shape[0]

print(n)

X = sy.Symbol('X')

f = X + 2*sy.sin(1.5*X)

y = np.zeros(n)

for i in range(n):
  y[i] = f.subs(X, x[i])

x_modified = np.c_[np.ones((len(x),1)), x]  
true_theta = np.matmul(np.linalg.pinv(x_modified), y)  
print(true_theta)
```

    1000
    [0.31795423 0.97467268]



```python
def train_data(x_train, y_train):
  theta = np.matmul(np.linalg.pinv(x_train), y_train)
  theta.reshape(x_train.shape[1])
  #print(theta.shape)
  return theta
```


```python
# for simplehypo
x_train = np.c_[np.ones((len(x),1)), x]

# for complex hypo
#x_train = np.c_[np.ones((len(x),1)), x, pow(x,2), pow(x,3), pow(x,4)]#, pow(x,5), pow(x,6), pow(x,7)]

m = x_train.shape[1]
print(m)
theta = np.zeros((100,m))

for i in range(100):
  
  y = np.zeros(len(x))
  for j in range(len(x)):
    y[j] = f.subs(X, x[j]) + np.random.normal(0, np.sqrt(2))
  
  theta[i, :] = train_data(x_train, y)
  
bias = true_theta - np.mean(theta, axis = 0)
variance = np.mean(np.square(theta - np.mean(theta)), axis = 0)
mse = np.mean(np.square(true_theta - theta), axis = 0)
cal_mse = np.square(bias) + variance

#print(true_theta - theta)
#print(theta)

print("bias: " + str(bias))
print("variance: " + str(variance))
print("Empirical MSE: " + str(mse))
print("Calculated MSE: " + str(cal_mse))
print("Difference b/w MSE: " + str(cal_mse-mse))
```

    2
    bias: [ 0.00320818 -0.00012217]
    variance: [0.11628647 0.10895396]
    Empirical MSE: [7.38065858e-03 3.78741600e-05]
    Calculated MSE: [0.11629676 0.10895398]
    Difference b/w MSE: [0.1089161 0.1089161]



```python
x_modified = np.c_[np.ones((len(x),1)), x, pow(x,2), pow(x,3), pow(x,4), pow(x,5), pow(x,6), pow(x,7)]  
true_theta = np.matmul(np.linalg.pinv(x_modified), y)  
print(true_theta)

# for complex hypothesis consider following x
x_train = np.c_[np.ones((len(x),1)), x, pow(x,2), pow(x,3), pow(x,4), pow(x,5), pow(x,6), pow(x,7)]

m = x_train.shape[1]
print(m)
theta = np.zeros((100,m))

for i in range(100):
  
  y = np.zeros(len(x))
  for j in range(len(x)):
    y[j] = f.subs(X, x[j]) + np.random.normal(0, np.sqrt(2))
  
  theta[i, :] = train_data(x_train, y)
  
bias = true_theta - np.mean(theta, axis = 0)
variance = np.mean(np.square(theta - np.mean(theta)), axis = 0)
mse = np.mean(np.square(true_theta - theta), axis = 0)
cal_mse = np.square(bias) + variance

#print(true_theta - theta)
#print(theta)

print("bias: " + str(bias))
print("variance: " + str(variance))
print("Empirical MSE: " + str(mse))
print("Calculated MSE: " + str(cal_mse))
print("Difference b/w MSE: " + str(cal_mse-mse))


```

    [ 2.02431694e+00 -8.79001214e-01  5.55916372e-01 -7.66133162e-02
      5.64622204e-03 -2.33729755e-04  5.28104876e-06 -5.23776946e-08]
    8
    bias: [ 6.81695669e-02 -2.86205100e-01  1.14067585e-01 -1.69830302e-02
      1.11701687e-03 -2.91325148e-05  1.78298994e-08  7.34981128e-09]
    variance: [3.13513635 0.92067975 0.10931252 0.08009378 0.04591451 0.04793571
     0.04784377 0.0478461 ]
    Empirical MSE: [1.21190201e-01 3.44006390e-01 7.25452633e-02 2.89361308e-03
     3.05861294e-05 9.36047331e-08 7.48036034e-11 9.58488507e-15]
    Calculated MSE: [3.13978344 1.00259311 0.12232393 0.08038221 0.04591576 0.04793571
     0.04784377 0.0478461 ]
    Difference b/w MSE: [3.01859324 0.65858672 0.04977867 0.07748859 0.04588517 0.04793562
     0.04784377 0.0478461 ]


9d90da05a05130f2a5c097e099fa8ba418431d55
ipynb
Jupyter Notebook
IE/406_machine_learning/m_v_joshi/2019/lab_sessions/solutions/mylab5_final.ipynb
u-square2/AcadVault
571612c928a1357c849cf9a06b47bab15dc5afa3
MIT
2021-11-08T03:54:46.000Z
2021-11-08T04:31:52.000Z
IE/406_machine_learning/m_v_joshi/2019/lab_sessions/solutions/mylab5_final.ipynb
u-square2/AcadVault
571612c928a1357c849cf9a06b47bab15dc5afa3
MIT
2020-02-08T20:16:17.000Z
2020-02-08T20:24:31.000Z
IE/406_machine_learning/m_v_joshi/2019/lab_sessions/solutions/mylab5_final.ipynb
u-square2/AcadVault
571612c928a1357c849cf9a06b47bab15dc5afa3
MIT
2022-01-28T05:27:16.000Z
2022-01-28T05:27:19.000Z
Qwen/Qwen-72B
1. YES
2. YES