Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
858 changes: 858 additions & 0 deletions assMath/ass2B/shorya/.ipynb_checkpoints/Untitled-checkpoint.ipynb

Large diffs are not rendered by default.

Large diffs are not rendered by default.

858 changes: 858 additions & 0 deletions assMath/ass2B/shorya/Untitled.ipynb

Large diffs are not rendered by default.

858 changes: 858 additions & 0 deletions assMath/ass2B/shorya/ass2B_solutions_shorya.ipynb

Large diffs are not rendered by default.

1,001 changes: 1,001 additions & 0 deletions assMath/ass2B/shorya/test_set_A.csv

Large diffs are not rendered by default.

1,001 changes: 1,001 additions & 0 deletions assMath/ass2B/shorya/test_set_B.csv

Large diffs are not rendered by default.

1,001 changes: 1,001 additions & 0 deletions assMath/ass2B/shorya/test_set_C.csv

Large diffs are not rendered by default.

100,001 changes: 100,001 additions & 0 deletions assMath/ass2B/shorya/train_set_A.csv

Large diffs are not rendered by default.

100,001 changes: 100,001 additions & 0 deletions assMath/ass2B/shorya/train_set_B.csv

Large diffs are not rendered by default.

100,001 changes: 100,001 additions & 0 deletions assMath/ass2B/shorya/train_set_C.csv

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions assMath/ass2a/shorya/.ipynb_checkpoints/Q3-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "456148e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number of datapoints: 1000000\n",
"Enter the mean: 2.56\n",
"Enter the standard deviation: 1.2\n",
"The parameters from MLE for the normal are: 2.5590225411793885 and 1.4417001920006909\n"
]
}
],
"source": [
"#The derivation is given in the handwritten solutions\n",
"import numpy as np\n",
"n=int(input(\"Enter the number of datapoints: \"))\n",
"mu=float(input(\"Enter the mean: \"))\n",
"sigma=float(input(\"Enter the standard deviation: \"))\n",
"data=[]\n",
"data=np.random.normal(mu, sigma, n)\n",
"mean_mle=sum(data)/n\n",
"variance_mle=0\n",
"for i in range(n):\n",
" variance_mle+=(data[i]-mean_mle)**2\n",
"variance_mle/=n\n",
"print(\"The parameters from MLE for the normal are:\",mean_mle,\"and\",variance_mle)"
]
},
{
"cell_type": "markdown",
"id": "b2fb48fb",
"metadata": {},
"source": [
"I have provided the explanation in the handwritten solutions"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
109 changes: 109 additions & 0 deletions assMath/ass2a/shorya/.ipynb_checkpoints/Q4-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e5d9ca83",
"metadata": {},
"source": [
"I have provided the explanantion in the handwritten solutions \n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "05e00952",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.00444714 0.25404008]\n"
]
}
],
"source": [
"import numpy as np\n",
"import math\n",
"\n",
"# Define the negative log-posterior function\n",
"def negative_log_posterior(theta, X, y, prior_mean, prior_cov):\n",
" # Compute the log-likelihood\n",
" logits = np.dot(X, theta)\n",
" likelihood = np.sum(y*logits - np.log(1 + np.exp(logits)))\n",
"\n",
" # Compute the log-prior\n",
" prior = -0.5 * np.dot((theta - prior_mean).T, np.linalg.inv(prior_cov)).dot(theta - prior_mean)\n",
"\n",
" # Compute the negative log-posterior\n",
" neg_log_posterior = - (likelihood + prior)\n",
"\n",
" return neg_log_posterior\n",
"\n",
"def sig(w,x):\n",
" return (1/(1 + np.exp(-np.dot(w,x))))\n",
" \n",
"def grad_descent_map(X,y,prior_mean,prior_cov,lr):\n",
" \n",
" X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)\n",
" weights=np.random.normal(0, 1/math.sqrt(X.shape[0]), X.shape[1])\n",
" update=[1e9]*X[0].size\n",
" while(np.max(np.array(update))>2*1e-5):\n",
" update=0\n",
" for i in range(X.shape[0]):\n",
" update+=(y[i]-sig(weights,X[i]))*X[i]\n",
" update/=X.shape[0]\n",
" update-=np.dot((weights-prior_mean),np.linalg.inv(prior_cov))\n",
" update*=lr\n",
" weights+=update\n",
" return weights\n",
"\n",
"#Data for a model which gives 1 for +ve/zero and -1 for -ve numbers \n",
"X=[]\n",
"y=[]\n",
"for x in range(-50,50,1):\n",
" X.append([x])\n",
" if(x>=0):y.append(1)\n",
" else:y.append(0)\n",
"X=np.array(X)\n",
"y=np.array(y)\n",
"\n",
"#Assuming prior to be standard gaussian\n",
"prior_mean = np.zeros(X.shape[1]+1) \n",
"prior_cov = np.eye(X.shape[1]+1) \n",
"\n",
"result=grad_descent_map(X,y,prior_mean,prior_cov,0.05)\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"id": "0ca293b1",
"metadata": {},
"source": [
"Here the first weight is b and second weight is a in ax+b"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
66 changes: 66 additions & 0 deletions assMath/ass2a/shorya/Q3.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "456148e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number of datapoints: 1000000\n",
"Enter the mean: 2.56\n",
"Enter the standard deviation: 1.2\n",
"The parameters from MLE for the normal are: 2.5590225411793885 and 1.4417001920006909\n"
]
}
],
"source": [
"#The derivation is given in the handwritten solutions\n",
"import numpy as np\n",
"n=int(input(\"Enter the number of datapoints: \"))\n",
"mu=float(input(\"Enter the mean: \"))\n",
"sigma=float(input(\"Enter the standard deviation: \"))\n",
"data=[]\n",
"data=np.random.normal(mu, sigma, n)\n",
"mean_mle=sum(data)/n\n",
"variance_mle=0\n",
"for i in range(n):\n",
" variance_mle+=(data[i]-mean_mle)**2\n",
"variance_mle/=n\n",
"print(\"The parameters from MLE for the normal are:\",mean_mle,\"and\",variance_mle)"
]
},
{
"cell_type": "markdown",
"id": "b2fb48fb",
"metadata": {},
"source": [
"I have provided the explanation in the handwritten solutions"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
109 changes: 109 additions & 0 deletions assMath/ass2a/shorya/Q4.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e5d9ca83",
"metadata": {},
"source": [
"I have provided the explanantion in the handwritten solutions \n"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "05e00952",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.00444714 0.25404008]\n"
]
}
],
"source": [
"import numpy as np\n",
"import math\n",
"\n",
"# Define the negative log-posterior function\n",
"def negative_log_posterior(theta, X, y, prior_mean, prior_cov):\n",
" # Compute the log-likelihood\n",
" logits = np.dot(X, theta)\n",
" likelihood = np.sum(y*logits - np.log(1 + np.exp(logits)))\n",
"\n",
" # Compute the log-prior\n",
" prior = -0.5 * np.dot((theta - prior_mean).T, np.linalg.inv(prior_cov)).dot(theta - prior_mean)\n",
"\n",
" # Compute the negative log-posterior\n",
" neg_log_posterior = - (likelihood + prior)\n",
"\n",
" return neg_log_posterior\n",
"\n",
"def sig(w,x):\n",
" return (1/(1 + np.exp(-np.dot(w,x))))\n",
" \n",
"def grad_descent_map(X,y,prior_mean,prior_cov,lr):\n",
" \n",
" X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)\n",
" weights=np.random.normal(0, 1/math.sqrt(X.shape[0]), X.shape[1])\n",
" update=[1e9]*X[0].size\n",
" while(np.max(np.array(update))>2*1e-5):\n",
" update=0\n",
" for i in range(X.shape[0]):\n",
" update+=(y[i]-sig(weights,X[i]))*X[i]\n",
" update/=X.shape[0]\n",
" update-=np.dot((weights-prior_mean),np.linalg.inv(prior_cov))\n",
" update*=lr\n",
" weights+=update\n",
" return weights\n",
"\n",
"#Data for a model which gives 1 for +ve/zero and -1 for -ve numbers \n",
"X=[]\n",
"y=[]\n",
"for x in range(-50,50,1):\n",
" X.append([x])\n",
" if(x>=0):y.append(1)\n",
" else:y.append(0)\n",
"X=np.array(X)\n",
"y=np.array(y)\n",
"\n",
"#Assuming prior to be standard gaussian\n",
"prior_mean = np.zeros(X.shape[1]+1) \n",
"prior_cov = np.eye(X.shape[1]+1) \n",
"\n",
"result=grad_descent_map(X,y,prior_mean,prior_cov,0.05)\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"id": "0ca293b1",
"metadata": {},
"source": [
"Here the first weight is b and second weight is a in ax+b"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added assMath/ass2a/shorya/ass2A_solutions_shorya.pdf
Binary file not shown.
Loading