|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "1d21a625-bc05-4cf9-80a4-04debf3ee038", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stderr", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "\u001b[32m\u001b[1m Activating\u001b[22m\u001b[39m project at `~/.julia/dev/AdvancedHMC/research/tests`\n" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "name": "stdout", |
| 18 | + "output_type": "stream", |
| 19 | + "text": [ |
| 20 | + "Julia Version 1.9.0-rc2\n", |
| 21 | + "Commit 72aec423c2a (2023-04-01 10:41 UTC)\n", |
| 22 | + "Platform Info:\n", |
| 23 | + " OS: macOS (x86_64-apple-darwin21.4.0)\n", |
| 24 | + " CPU: 20 × Apple M1 Ultra\n", |
| 25 | + " WORD_SIZE: 64\n", |
| 26 | + " LIBM: libopenlibm\n", |
| 27 | + " LLVM: libLLVM-14.0.6 (ORCJIT, westmere)\n", |
| 28 | + " Threads: 2 on 20 virtual cores\n", |
| 29 | + "\u001b[32m\u001b[1mStatus\u001b[22m\u001b[39m `~/.julia/dev/AdvancedHMC/research/tests/Project.toml`\n", |
| 30 | + " \u001b[90m[c75e803d] \u001b[39mAdaptiveRejectionSampling v0.1.1\n", |
| 31 | + " \u001b[90m[0bf59076] \u001b[39mAdvancedHMC v0.4.5\n", |
| 32 | + " \u001b[90m[6e4b80f9] \u001b[39mBenchmarkTools v1.3.2\n", |
| 33 | + " \u001b[90m[863f3e99] \u001b[39mComonicon v1.0.4\n", |
| 34 | + " \u001b[90m[163ba53b] \u001b[39mDiffResults v1.1.0\n", |
| 35 | + " \u001b[90m[31c24e10] \u001b[39mDistributions v0.25.87\n", |
| 36 | + " \u001b[90m[366bfd00] \u001b[39mDynamicPPL v0.22.2\n", |
| 37 | + " \u001b[90m[6a86dc24] \u001b[39mFiniteDiff v2.19.0\n", |
| 38 | + " \u001b[90m[f6369f11] \u001b[39mForwardDiff v0.10.35\n", |
| 39 | + " \u001b[90m[6d524b87] \u001b[39mMCMCDebugging v0.2.1 `https://github.com/TuringLang/MCMCDebugging.jl#master`\n", |
| 40 | + " \u001b[90m[91a5bcdd] \u001b[39mPlots v1.38.9\n", |
| 41 | + " \u001b[90m[d330b81b] \u001b[39mPyPlot v2.11.1\n", |
| 42 | + " \u001b[90m[e0db7c4e] \u001b[39mReTest v0.3.2\n", |
| 43 | + " \u001b[90m[37e2e3b7] \u001b[39mReverseDiff v1.14.4\n", |
| 44 | + " \u001b[90m[a8a75453] \u001b[39mStatProfilerHTML v1.5.0\n", |
| 45 | + " \u001b[90m[8a639fad] \u001b[39mVecTargets v0.2.0 `https://github.com/xukai92/VecTargets.jl#main`\n" |
| 46 | + ] |
| 47 | + } |
| 48 | + ], |
| 49 | + "source": [ |
| 50 | + "using InteractiveUtils, Pkg\n", |
| 51 | + "using AdvancedHMC; Pkg.activate(pkgdir(AdvancedHMC) * \"/research/tests\"); Pkg.instantiate()\n", |
| 52 | + "versioninfo(); Pkg.status()" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": 2, |
| 58 | + "id": "3d0c7de5-8dc2-477a-bde7-57ebc7bcb671", |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "using LinearAlgebra, FiniteDiff, VecTargets" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 3, |
| 68 | + "id": "017c5260-fc75-41b5-b886-f8ab3ad52432", |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [ |
| 71 | + { |
| 72 | + "data": { |
| 73 | + "text/plain": [ |
| 74 | + "#9 (generic function with 1 method)" |
| 75 | + ] |
| 76 | + }, |
| 77 | + "execution_count": 3, |
| 78 | + "metadata": {}, |
| 79 | + "output_type": "execute_result" |
| 80 | + } |
| 81 | + ], |
| 82 | + "source": [ |
| 83 | + "target = Funnel()\n", |
| 84 | + "\n", |
| 85 | + "ℓπ = x -> logpdf(target, x)\n", |
| 86 | + "neg_ℓπ = x -> -logpdf(target, x)\n", |
| 87 | + "\n", |
| 88 | + "H = x -> VecTargets.gen_hess(ℓπ, x)(x)[3]\n", |
| 89 | + "G = x -> VecTargets.gen_hess(neg_ℓπ, x)(x)[3]" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 4, |
| 95 | + "id": "2f694d5b-54e8-4793-8999-a0ffec9eae0b", |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [ |
| 98 | + { |
| 99 | + "name": "stdout", |
| 100 | + "output_type": "stream", |
| 101 | + "text": [ |
| 102 | + "H(xt) = [-0.1111111111111111 0.0; 0.0 -1.0]\n", |
| 103 | + "G(xt) = [0.1111111111111111 0.0; 0.0 1.0]\n", |
| 104 | + "FiniteDiff.finite_difference_gradient(Hamiltonian_partial, xt) = [-0.9999999999502168, 0.0]\n", |
| 105 | + "[tr(inv(G(xt)) * Jt[1:2, 1:2]), tr(inv(G(xt)) * Jt[3:4, 1:2])] = [-0.9999999664723873, 0.0]\n" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "data": { |
| 110 | + "text/plain": [ |
| 111 | + "2-element Vector{Float64}:\n", |
| 112 | + " -0.9999999664723873\n", |
| 113 | + " 0.0" |
| 114 | + ] |
| 115 | + }, |
| 116 | + "execution_count": 4, |
| 117 | + "metadata": {}, |
| 118 | + "output_type": "execute_result" |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "xt = [0.0, 0.0] # x test\n", |
| 123 | + "\n", |
| 124 | + "@show H(xt) G(xt)\n", |
| 125 | + "\n", |
| 126 | + "# Hamiltonian_partial(x) = begin x\n", |
| 127 | + "# lad, s = logabsdet(G(x))\n", |
| 128 | + "# lad * s\n", |
| 129 | + "# end # WRONG implementation of the second term of (13)\n", |
| 130 | + " # `s` returned is the sign of `det(G)`, not the whole thing\n", |
| 131 | + "Hamiltonian_partial(x) = begin x\n", |
| 132 | + " logdet(G(x))\n", |
| 133 | + "end # second term of (13)\n", |
| 134 | + "\n", |
| 135 | + "@show FiniteDiff.finite_difference_gradient(Hamiltonian_partial, xt)\n", |
| 136 | + "\n", |
| 137 | + "Jt = FiniteDiff.finite_difference_jacobian(G, xt)\n", |
| 138 | + "@show [tr(inv(G(xt)) * Jt[1:2,1:2]), tr(inv(G(xt)) * Jt[3:4,1:2])]" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": null, |
| 144 | + "id": "c5ec162e-0114-44a7-b23f-3366286c5414", |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [] |
| 148 | + } |
| 149 | + ], |
| 150 | + "metadata": { |
| 151 | + "kernelspec": { |
| 152 | + "display_name": "julia 1.9.0-rc2", |
| 153 | + "language": "julia", |
| 154 | + "name": "julia-1.9" |
| 155 | + }, |
| 156 | + "language_info": { |
| 157 | + "file_extension": ".jl", |
| 158 | + "mimetype": "application/julia", |
| 159 | + "name": "julia", |
| 160 | + "version": "1.9.0" |
| 161 | + } |
| 162 | + }, |
| 163 | + "nbformat": 4, |
| 164 | + "nbformat_minor": 5 |
| 165 | +} |
0 commit comments