<jupyter_start><jupyter_text>HW0 A.11 1. initiate array A,b,c in Problem 8<jupyter_code>import numpy as np
A = np.array([[0,2,4],[2,4,2],[3,3,1]])
b = np.array([-2,-2,-4])
c = np.array([1,1,1])
print(A, b, c)<jupyter_output>[[0 2 4]
 [2 4 2]
 [3 3 1]] [-2 -2 -4] [1 1 1]<jupyter_text>2. Calculate A^-1<jupyter_code>A_inv = np.linalg.inv(A)
print(A_inv)<jupyter_output>[[ 0.125 -0.625  0.75 ]
 [-0.25   0.75  -0.5  ]
 [ 0.375 -0.375  0.25 ]]<jupyter_text>3. Calculate A^-1*b and Ac<jupyter_code>A_inv_mul_b =  np.dot(A_inv, b)
A_mul_c = np.dot(A, c)
print(A_inv_mul_b)
print(A_mul_c)<jupyter_output>[-2.  1. -1.]
[6 8 7]<jupyter_text>A.12 we need to choose a n large enough<jupyter_code>def calculate_f_bar(n, x, Z):
    return (1/n) * np.sum(np.where(Z <= x,1,0))        
n = 10000
x_array = np.linspace(-3,3,num=n//10)
Z = sorted(np.random.randn(n))
f_bar = np.array([calculate_f_bar(n, x, Z) for x in x_array])
print(f_bar)
import scipy.stats
mean = 0
standard_deviation = 1

y_values = scipy.stats.norm(mean, standard_deviation)
print(np.sqrt(np.mean((f_bar - y_values.pdf(x_array)) ** 2)))<jupyter_output>0.5347660304456603<jupyter_text>5.b<jupyter_code>import math
def generate_y(k):
    B = np.where(np.random.rand(k) >= 0.5,1,-1)
    return (1.0/math.sqrt(k)) * np.sum(B)

def generate_y_bar(k):
    Y = [generate_y(k) for _ in range(n)]
    return np.array([calculate_f_bar(n, x, Y) for x in x_array])

k_array = [1,8,64,512]
Y_bars = [generate_y_bar(k) for k in k_array]
print("Y_bar generate complete")<jupyter_output>Y_bar generate complete<jupyter_text>plot the result<jupyter_code>import matplotlib.pyplot as plt
plt.xlim(-3,3)
plt.ylabel("Probabilities")
plt.grid(axis='x', color='0.95')
plt.xlabel('Observations')
plt.step(x_array, f_bar, label="Gaussian")
plt.step(x_array, Y_bars[0], label="1")
plt.step(x_array, Y_bars[1], label="8")
plt.step(x_array, Y_bars[2], label="64")
plt.step(x_array, Y_bars[3], label="512")
plt.legend(title='Parameter where:')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1904047976011994, 'fraction_numerical': 0.06946526736631684, 'mean_word_length': 5.2367601246105915, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '271880', 'license': 'MIT', 'n_tokens_mistral': 1011, 'n_tokens_neox': 937, 'n_words': 171, 'path': 'hw0/.ipynb_checkpoints/kaiser_hw0-checkpoint.ipynb', 'repo_name': 'bobbydyr/CSE546-Machine-Learning', 'size': '2001'}