<jupyter_start><jupyter_text>Hypothesis Testing - Chi2<jupyter_code>critical_value = stats.chi2.ppf(q = 0.95, df = 11)
critical_value
return_rating = data.morningstar_return_rating.value_counts()
risk_rating = data.morningstar_risk_rating.value_counts()
observed = pd.concat([return_rating.transpose(), risk_rating.transpose()], axis=1, keys=['return', 'risk'])
observed
chi2, p, dof, ex = chi2_contingency(observed)
print(critical_value, chi2, p)
correlation = data.corr().abs()
us_correlation = correlation.unstack().sort_values(ascending=False)
max_correlated = us_correlation[(us_correlation<1) & (us_correlation>0.75)]
max_correlated
for column in ['morningstar_rating', 'portfolio_stocks', 'category_12', 'sharpe_ratio_3y']:
    if column in data.columns:
        data.drop(columns=column, axis=1, inplace=True)
data.shape
fig, [ax_1, ax_2] = plt.subplots(nrows=1, ncols=2,figsize=[10,10])
data.price_earning.plot(kind='box',ax=ax_1)
data.net_annual_expenses_ratio.plot(kind='box',ax=ax_2)
fig
X = data[data.columns[data.columns != 'bonds_aaa']]
y = data['bonds_aaa']
X_train,X_test,y_train,y_test = train_test_split(X, y, test_size = 0.3, random_state = 3)
lr = LinearRegression()
lr.fit(X_train,y_train)
y_pred = lr.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
rmse
ridge_lambdas = [0.01, 0.03, 0.06, 0.1, 0.3, 0.6, 1, 3, 6, 10, 30, 60]
lasso_lambdas = [0.0001, 0.0003, 0.0006, 0.001, 0.003, 0.006, 0.01, 0.03, 0.06, 0.1, 0.3, 0.6, 1]
ridge_model = Ridge()
ridge_grid = GridSearchCV(estimator=ridge_model, param_grid=dict(alpha=ridge_lambdas))
ridge_grid.fit(X_train, y_train)
ridge_rmse = np.sqrt(mean_squared_error(y_test, ridge_grid.predict(X_test)))
print(ridge_rmse)
lasso_model = Lasso()
lasso_grid = GridSearchCV(estimator=lasso_model, param_grid=dict(alpha=lasso_lambdas))
lasso_grid.fit(X_train, y_train)
lasso_rmse = np.sqrt(mean_squared_error(y_test, lasso_grid.predict(X_test)))
print(lasso_rmse)<jupyter_output>15.719153628852961
{'fraction_non_alphanumeric': 0.15060851926977686, 'fraction_numerical': 0.058823529411764705, 'mean_word_length': 7.768888888888889, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '285309', 'license': 'MIT', 'n_tokens_mistral': 907, 'n_tokens_neox': 810, 'n_words': 128, 'path': 'ipynb/Mutual Fund Returns.ipynb', 'repo_name': 'abhishekkumaribt/Grey_Atom', 'size': '1972'}