Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated mcmc docs #73

Merged
merged 3 commits into from
Sep 13, 2024
Merged
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
60 changes: 40 additions & 20 deletions docs/source/methodology.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"id": "a849431d-372b-444b-8d36-39b4b0edb988",
"metadata": {
"tags": []
Expand Down Expand Up @@ -323,7 +323,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"id": "9758f623-06e9-4750-ba82-02d5fdab059e",
"metadata": {
"tags": []
Expand Down Expand Up @@ -380,31 +380,43 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"id": "de35f746-2b61-4dd9-966a-65676978238d",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from scipy.linalg import pinvh\n",
"def log_prior(theta):\n",
" \"\"\"\n",
" Get the log-prior uniform distribution\n",
" :param theta: Value of the gradient and intercept of the straight line.\n",
" :return: Log-prior value.\n",
" \"\"\"\n",
" if theta[0] < 0:\n",
" return -np.inf\n",
" return 0\n",
"\n",
"_, logdet = np.linalg.slogdet(cov)\n",
"logdet += np.log(2 * np.pi) * msd.size\n",
"inv = pinvh(cov)\n",
"\n",
"def log_posterior(theta: np.ndarray) -> float:\n",
"def log_likelihood(theta):\n",
" \"\"\"\n",
" Get the log likelihood for multivariate normal distribution.\n",
" Get the log-likelihood for multivariate normal distribution.\n",
" :param theta: Value of the gradient and intercept of the straight line.\n",
" :return: Log-likelihood value.\n",
" \"\"\"\n",
" if theta[0] < 0:\n",
" return -np.inf\n",
" model = dt * theta[0] + theta[1]\n",
" diff = (model - msd)\n",
" logl = -0.5 * (logdet + np.matmul(diff.T, np.matmul(inv, diff)))\n",
" return logl"
" logl = gp.logpdf(model)\n",
" return logl\n",
"\n",
"\n",
"def log_posterior(theta: np.ndarray) -> float:\n",
" \"\"\"\n",
" Summate the log-likelihood and log prior to produce the log-posterior value\n",
" :param theta: Value of the gradient and intercept of the straight line.\n",
" :return: Log-posterior value.\n",
" \"\"\"\n",
" logp = log_likelihood(theta) + log_prior(theta)\n",
" return logp"
]
},
{
Expand All @@ -417,7 +429,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"id": "23c97983-1068-4d72-9e1d-cd7d36084e06",
"metadata": {
"tags": []
Expand All @@ -426,9 +438,9 @@
"source": [
"def nll(*args) -> float:\n",
" \"\"\"\n",
" General purpose negative log-likelihood.\n",
" General purpose negative log-posterior.\n",
"\n",
" :return: Negative log-likelihood\n",
" :return: Negative log-posterior\n",
" \"\"\"\n",
" return -log_posterior(*args)\n",
"\n",
Expand Down Expand Up @@ -457,7 +469,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"id": "a904bd03-662e-4db3-bd79-e6cf34f96221",
"metadata": {
"tags": []
Expand All @@ -481,7 +493,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 36,
"id": "3150d420-8fd1-441b-bb7b-8fa7187ede2b",
"metadata": {
"tags": []
Expand Down Expand Up @@ -547,6 +559,14 @@
"plt.ylim(0, None)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d77d1df8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -565,7 +585,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
Loading