{"action":"created","comment":{"url":"https://api.github.com/repos/improbable-research/keanu/pulls/comments/242640661","pull_request_review_id":186201347,"id":242640661,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDI0MjY0MDY2MQ==","diff_hunk":"@@ -0,0 +1,359 @@\n+package io.improbable.keanu.algorithms.mcmc.nuts;\n+\n+import io.improbable.keanu.algorithms.SaveStatistics;\n+import io.improbable.keanu.algorithms.Statistics;\n+import io.improbable.keanu.tensor.dbl.DoubleTensor;\n+import io.improbable.keanu.vertices.ProbabilityCalculator;\n+import io.improbable.keanu.vertices.Vertex;\n+import io.improbable.keanu.vertices.VertexId;\n+import io.improbable.keanu.vertices.dbl.KeanuRandom;\n+import io.improbable.keanu.vertices.dbl.nonprobabilistic.diff.LogProbGradientCalculator;\n+\n+import java.util.List;\n+import java.util.Map;\n+\n+import static io.improbable.keanu.algorithms.mcmc.SamplingAlgorithm.takeSample;\n+\n+\n+/**\n+ * Used by NUTS as a balanced binary tree to compute and store information\n+ * about leapfrogs that are taken forwards and backwards in space.\n+ * <p>\n+ * The tree is reset for each sample.\n+ */\n+class Tree implements SaveStatistics {\n+\n+    private static final double DELTA_MAX = 1000.0;\n+    private static final int STARTING_ACCEPTED_LEAPFROG = 1;\n+    private static final boolean STARTING_SHOULD_CONTINUE = true;\n+    private static final double STARTING_DELTA_LIKELIHOOD = 0.;\n+    private static final int STARTING_TREE_SIZE = 1;\n+\n+    private Leapfrog leapfrogForward;\n+    private Leapfrog leapfrogBackward;\n+    private Map<VertexId, DoubleTensor> acceptedPosition;\n+    private Map<VertexId, DoubleTensor> gradientAtAcceptedPosition;\n+    private double logOfMasterPAtAcceptedPosition;\n+    private Map<VertexId, ?> sampleAtAcceptedPosition;\n+    private int acceptedLeapfrogCount;\n+    private boolean shouldContinueFlag;\n+    private double deltaLikelihoodOfLeapfrog;\n+    private double treeSize;\n+\n+    /**\n+     * @param leapfrog                  The result of the forward leapfrog\n+     * @param logProbAtAcceptedPosition The log prob of the network at the accepted position\n+     * @param sampleAtAcceptedPosition  The sample value at the accepted position\n+     * @param acceptedLeapfrogCount     The number of accepted leapfrogs\n+     * @param shouldContinueFlag        False if we are U-Turning\n+     * @param deltaLikelihoodOfLeapfrog The change in log prob as a result of the latest leapfrog\n+     * @param treeSize                  The size of the tree\n+     */\n+    Tree(Leapfrog leapfrog,\n+         double logProbAtAcceptedPosition,\n+         Map<VertexId, ?> sampleAtAcceptedPosition,\n+         int acceptedLeapfrogCount,\n+         boolean shouldContinueFlag,\n+         double deltaLikelihoodOfLeapfrog,\n+         double treeSize) {\n+\n+        this.leapfrogForward = leapfrog;\n+        this.leapfrogBackward = leapfrog;\n+        this.acceptedPosition = leapfrog.getPosition();\n+        this.gradientAtAcceptedPosition = leapfrog.getGradient();\n+        this.logOfMasterPAtAcceptedPosition = logProbAtAcceptedPosition;\n+        this.sampleAtAcceptedPosition = sampleAtAcceptedPosition;\n+        this.acceptedLeapfrogCount = acceptedLeapfrogCount;\n+        this.shouldContinueFlag = shouldContinueFlag;\n+        this.deltaLikelihoodOfLeapfrog = deltaLikelihoodOfLeapfrog;\n+        this.treeSize = treeSize;\n+    }\n+\n+    /**\n+     * A helper method to create a basic tree\n+     *\n+     * @param position                 the starting position\n+     * @param momentum                 the starting momentum\n+     * @param gradient                 the starting gradient\n+     * @param initialLogOfMasterP      the initial log prob\n+     * @param sampleAtAcceptedPosition the initial sample\n+     * @return a basic tree\n+     */\n+    public static Tree createBasicTree(Map<VertexId, DoubleTensor> position,","path":"keanu-project/src/main/java/io/improbable/keanu/algorithms/mcmc/nuts/Tree.java","position":82,"original_position":82,"commit_id":"8d402985c148997416c42078681ef0c1416887ab","original_commit_id":"8d402985c148997416c42078681ef0c1416887ab","user":{"login":"gordoncaleb","id":1253165,"node_id":"MDQ6VXNlcjEyNTMxNjU=","avatar_url":"https://avatars3.githubusercontent.com/u/1253165?v=4","gravatar_id":"","url":"https://api.github.com/users/gordoncaleb","html_url":"https://github.com/gordoncaleb","followers_url":"https://api.github.com/users/gordoncaleb/followers","following_url":"https://api.github.com/users/gordoncaleb/following{/other_user}","gists_url":"https://api.github.com/users/gordoncaleb/gists{/gist_id}","starred_url":"https://api.github.com/users/gordoncaleb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gordoncaleb/subscriptions","organizations_url":"https://api.github.com/users/gordoncaleb/orgs","repos_url":"https://api.github.com/users/gordoncaleb/repos","events_url":"https://api.github.com/users/gordoncaleb/events{/privacy}","received_events_url":"https://api.github.com/users/gordoncaleb/received_events","type":"User","site_admin":false},"body":"Oh wait. Maybe initialTree?","created_at":"2018-12-18T17:54:20Z","updated_at":"2018-12-18T17:54:21Z","html_url":"https://github.com/improbable-research/keanu/pull/391#discussion_r242640661","pull_request_url":"https://api.github.com/repos/improbable-research/keanu/pulls/391","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/improbable-research/keanu/pulls/comments/242640661"},"html":{"href":"https://github.com/improbable-research/keanu/pull/391#discussion_r242640661"},"pull_request":{"href":"https://api.github.com/repos/improbable-research/keanu/pulls/391"}},"in_reply_to_id":242640181},"pull_request":{"url":"https://api.github.com/repos/improbable-research/keanu/pulls/391","id":237770484,"node_id":"MDExOlB1bGxSZXF1ZXN0MjM3NzcwNDg0","html_url":"https://github.com/improbable-research/keanu/pull/391","diff_url":"https://github.com/improbable-research/keanu/pull/391.diff","patch_url":"https://github.com/improbable-research/keanu/pull/391.patch","issue_url":"https://api.github.com/repos/improbable-research/keanu/issues/391","number":391,"state":"open","locked":false,"title":"Feature/nuts","user":{"login":"GeorgeNash","id":5854544,"node_id":"MDQ6VXNlcjU4NTQ1NDQ=","avatar_url":"https://avatars1.githubusercontent.com/u/5854544?v=4","gravatar_id":"","url":"https://api.github.com/users/GeorgeNash","html_url":"https://github.com/GeorgeNash","followers_url":"https://api.github.com/users/GeorgeNash/followers","following_url":"https://api.github.com/users/GeorgeNash/following{/other_user}","gists_url":"https://api.github.com/users/GeorgeNash/gists{/gist_id}","starred_url":"https://api.github.com/users/GeorgeNash/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GeorgeNash/subscriptions","organizations_url":"https://api.github.com/users/GeorgeNash/orgs","repos_url":"https://api.github.com/users/GeorgeNash/repos","events_url":"https://api.github.com/users/GeorgeNash/events{/privacy}","received_events_url":"https://api.github.com/users/GeorgeNash/received_events","type":"User","site_admin":false},"body":"Breaks NUTS out from the original mega-file\r\n\r\n- Pulled out the `TreeBuilder`, `Leapfrog` and `Stepsize` classes\r\n- Tests for `Leapfrog`\r\n- Tests for `Stepsize`\r\n- You can now record sampling statistics for all the samples in your NUTS run. Step size, log prob, mean tree acceptance prob and tree size. These are just stored as a simple dictionary with a list of doubles.\r\n\r\nAll issues I found while testing could be resolved through careful tweaking of the parameters (especially the relationship between `Target_acceptance_prob` and the tuning of the step size. This is especially apparent when starting in low likelihood areas). Hopefully the sampling statistics will help people see what's going wrong with their run and can tune their parameters accordingly.\r\n\r\nNext...\r\n\r\n- Tests for `TreeBuilder` (oh lord)\r\n- Go through the applied dev diary for use cases that NUTS is currently failing on and add these to `NUTSTest`","created_at":"2018-12-11T17:46:52Z","updated_at":"2018-12-18T17:54:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"86b5d878499fe1af2510f5cbd8cba96d5336423c","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/improbable-research/keanu/pulls/391/commits","review_comments_url":"https://api.github.com/repos/improbable-research/keanu/pulls/391/comments","review_comment_url":"https://api.github.com/repos/improbable-research/keanu/pulls/comments{/number}","comments_url":"https://api.github.com/repos/improbable-research/keanu/issues/391/comments","statuses_url":"https://api.github.com/repos/improbable-research/keanu/statuses/8d402985c148997416c42078681ef0c1416887ab","head":{"label":"improbable-research:feature/nuts","ref":"feature/nuts","sha":"8d402985c148997416c42078681ef0c1416887ab","user":{"login":"improbable-research","id":37903994,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM3OTAzOTk0","avatar_url":"https://avatars1.githubusercontent.com/u/37903994?v=4","gravatar_id":"","url":"https://api.github.com/users/improbable-research","html_url":"https://github.com/improbable-research","followers_url":"https://api.github.com/users/improbable-research/followers","following_url":"https://api.github.com/users/improbable-research/following{/other_user}","gists_url":"https://api.github.com/users/improbable-research/gists{/gist_id}","starred_url":"https://api.github.com/users/improbable-research/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/improbable-research/subscriptions","organizations_url":"https://api.github.com/users/improbable-research/orgs","repos_url":"https://api.github.com/users/improbable-research/repos","events_url":"https://api.github.com/users/improbable-research/events{/privacy}","received_events_url":"https://api.github.com/users/improbable-research/received_events","type":"Organization","site_admin":false},"repo":{"id":128393918,"node_id":"MDEwOlJlcG9zaXRvcnkxMjgzOTM5MTg=","name":"keanu","full_name":"improbable-research/keanu","private":false,"owner":{"login":"improbable-research","id":37903994,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM3OTAzOTk0","avatar_url":"https://avatars1.githubusercontent.com/u/37903994?v=4","gravatar_id":"","url":"https://api.github.com/users/improbable-research","html_url":"https://github.com/improbable-research","followers_url":"https://api.github.com/users/improbable-research/followers","following_url":"https://api.github.com/users/improbable-research/following{/other_user}","gists_url":"https://api.github.com/users/improbable-research/gists{/gist_id}","starred_url":"https://api.github.com/users/improbable-research/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/improbable-research/subscriptions","organizations_url":"https://api.github.com/users/improbable-research/orgs","repos_url":"https://api.github.com/users/improbable-research/repos","events_url":"https://api.github.com/users/improbable-research/events{/privacy}","received_events_url":"https://api.github.com/users/improbable-research/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/improbable-research/keanu","description":"A probabilistic approach from an Improbabilistic company","fork":false,"url":"https://api.github.com/repos/improbable-research/keanu","forks_url":"https://api.github.com/repos/improbable-research/keanu/forks","keys_url":"https://api.github.com/repos/improbable-research/keanu/keys{/key_id}","collaborators_url":"https://api.github.com/repos/improbable-research/keanu/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/improbable-research/keanu/teams","hooks_url":"https://api.github.com/repos/improbable-research/keanu/hooks","issue_events_url":"https://api.github.com/repos/improbable-research/keanu/issues/events{/number}","events_url":"https://api.github.com/repos/improbable-research/keanu/events","assignees_url":"https://api.github.com/repos/improbable-research/keanu/assignees{/user}","branches_url":"https://api.github.com/repos/improbable-research/keanu/branches{/branch}","tags_url":"https://api.github.com/repos/improbable-research/keanu/tags","blobs_url":"https://api.github.com/repos/improbable-research/keanu/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/improbable-research/keanu/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/improbable-research/keanu/git/refs{/sha}","trees_url":"https://api.github.com/repos/improbable-research/keanu/git/trees{/sha}","statuses_url":"https://api.github.com/repos/improbable-research/keanu/statuses/{sha}","languages_url":"https://api.github.com/repos/improbable-research/keanu/languages","stargazers_url":"https://api.github.com/repos/improbable-research/keanu/stargazers","contributors_url":"https://api.github.com/repos/improbable-research/keanu/contributors","subscribers_url":"https://api.github.com/repos/improbable-research/keanu/subscribers","subscription_url":"https://api.github.com/repos/improbable-research/keanu/subscription","commits_url":"https://api.github.com/repos/improbable-research/keanu/commits{/sha}","git_commits_url":"https://api.github.com/repos/improbable-research/keanu/git/commits{/sha}","comments_url":"https://api.github.com/repos/improbable-research/keanu/comments{/number}","issue_comment_url":"https://api.github.com/repos/improbable-research/keanu/issues/comments{/number}","contents_url":"https://api.github.com/repos/improbable-research/keanu/contents/{+path}","compare_url":"https://api.github.com/repos/improbable-research/keanu/compare/{base}...{head}","merges_url":"https://api.github.com/repos/improbable-research/keanu/merges","archive_url":"https://api.github.com/repos/improbable-research/keanu/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/improbable-research/keanu/downloads","issues_url":"https://api.github.com/repos/improbable-research/keanu/issues{/number}","pulls_url":"https://api.github.com/repos/improbable-research/keanu/pulls{/number}","milestones_url":"https://api.github.com/repos/improbable-research/keanu/milestones{/number}","notifications_url":"https://api.github.com/repos/improbable-research/keanu/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/improbable-research/keanu/labels{/name}","releases_url":"https://api.github.com/repos/improbable-research/keanu/releases{/id}","deployments_url":"https://api.github.com/repos/improbable-research/keanu/deployments","created_at":"2018-04-06T12:48:36Z","updated_at":"2018-12-18T16:43:36Z","pushed_at":"2018-12-18T17:31:32Z","git_url":"git://github.com/improbable-research/keanu.git","ssh_url":"git@github.com:improbable-research/keanu.git","clone_url":"https://github.com/improbable-research/keanu.git","svn_url":"https://github.com/improbable-research/keanu","homepage":"","size":10739,"stargazers_count":115,"watchers_count":115,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":24,"mirror_url":null,"archived":false,"open_issues_count":21,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":24,"open_issues":21,"watchers":115,"default_branch":"develop"}},"base":{"label":"improbable-research:develop","ref":"develop","sha":"27b1617eb7c4e472a05a3ed277c1eaa3424cbb67","user":{"login":"improbable-research","id":37903994,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM3OTAzOTk0","avatar_url":"https://avatars1.githubusercontent.com/u/37903994?v=4","gravatar_id":"","url":"https://api.github.com/users/improbable-research","html_url":"https://github.com/improbable-research","followers_url":"https://api.github.com/users/improbable-research/followers","following_url":"https://api.github.com/users/improbable-research/following{/other_user}","gists_url":"https://api.github.com/users/improbable-research/gists{/gist_id}","starred_url":"https://api.github.com/users/improbable-research/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/improbable-research/subscriptions","organizations_url":"https://api.github.com/users/improbable-research/orgs","repos_url":"https://api.github.com/users/improbable-research/repos","events_url":"https://api.github.com/users/improbable-research/events{/privacy}","received_events_url":"https://api.github.com/users/improbable-research/received_events","type":"Organization","site_admin":false},"repo":{"id":128393918,"node_id":"MDEwOlJlcG9zaXRvcnkxMjgzOTM5MTg=","name":"keanu","full_name":"improbable-research/keanu","private":false,"owner":{"login":"improbable-research","id":37903994,"node_id":"MDEyOk9yZ2FuaXphdGlvbjM3OTAzOTk0","avatar_url":"https://avatars1.githubusercontent.com/u/37903994?v=4","gravatar_id":"","url":"https://api.github.com/users/improbable-research","html_url":"https://github.com/improbable-research","followers_url":"https://api.github.com/users/improbable-research/followers","following_url":"https://api.github.com/users/improbable-research/following{/other_user}","gists_url":"https://api.github.com/users/improbable-research/gists{/gist_id}","starred_url":"https://api.github.com/users/improbable-research/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/improbable-research/subscriptions","organizations_url":"https://api.github.com/users/improbable-research/orgs","repos_url":"https://api.github.com/users/improbable-research/repos","events_url":"https://api.github.com/users/improbable-research/events{/privacy}","received_events_url":"https://api.github.com/users/improbable-research/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/improbable-research/keanu","description":"A probabilistic approach from an Improbabilistic company","fork":false,"url":"https://api.github.com/repos/improbable-research/keanu","forks_url":"https://api.github.com/repos/improbable-research/keanu/forks","keys_url":"https://api.github.com/repos/improbable-research/keanu/keys{/key_id}","collaborators_url":"https://api.github.com/repos/improbable-research/keanu/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/improbable-research/keanu/teams","hooks_url":"https://api.github.com/repos/improbable-research/keanu/hooks","issue_events_url":"https://api.github.com/repos/improbable-research/keanu/issues/events{/number}","events_url":"https://api.github.com/repos/improbable-research/keanu/events","assignees_url":"https://api.github.com/repos/improbable-research/keanu/assignees{/user}","branches_url":"https://api.github.com/repos/improbable-research/keanu/branches{/branch}","tags_url":"https://api.github.com/repos/improbable-research/keanu/tags","blobs_url":"https://api.github.com/repos/improbable-research/keanu/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/improbable-research/keanu/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/improbable-research/keanu/git/refs{/sha}","trees_url":"https://api.github.com/repos/improbable-research/keanu/git/trees{/sha}","statuses_url":"https://api.github.com/repos/improbable-research/keanu/statuses/{sha}","languages_url":"https://api.github.com/repos/improbable-research/keanu/languages","stargazers_url":"https://api.github.com/repos/improbable-research/keanu/stargazers","contributors_url":"https://api.github.com/repos/improbable-research/keanu/contributors","subscribers_url":"https://api.github.com/repos/improbable-research/keanu/subscribers","subscription_url":"https://api.github.com/repos/improbable-research/keanu/subscription","commits_url":"https://api.github.com/repos/improbable-research/keanu/commits{/sha}","git_commits_url":"https://api.github.com/repos/improbable-research/keanu/git/commits{/sha}","comments_url":"https://api.github.com/repos/improbable-research/keanu/comments{/number}","issue_comment_url":"https://api.github.com/repos/improbable-research/keanu/issues/comments{/number}","contents_url":"https://api.github.com/repos/improbable-research/keanu/contents/{+path}","compare_url":"https://api.github.com/repos/improbable-research/keanu/compare/{base}...{head}","merges_url":"https://api.github.com/repos/improbable-research/keanu/merges","archive_url":"https://api.github.com/repos/improbable-research/keanu/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/improbable-research/keanu/downloads","issues_url":"https://api.github.com/repos/improbable-research/keanu/issues{/number}","pulls_url":"https://api.github.com/repos/improbable-research/keanu/pulls{/number}","milestones_url":"https://api.github.com/repos/improbable-research/keanu/milestones{/number}","notifications_url":"https://api.github.com/repos/improbable-research/keanu/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/improbable-research/keanu/labels{/name}","releases_url":"https://api.github.com/repos/improbable-research/keanu/releases{/id}","deployments_url":"https://api.github.com/repos/improbable-research/keanu/deployments","created_at":"2018-04-06T12:48:36Z","updated_at":"2018-12-18T16:43:36Z","pushed_at":"2018-12-18T17:31:32Z","git_url":"git://github.com/improbable-research/keanu.git","ssh_url":"git@github.com:improbable-research/keanu.git","clone_url":"https://github.com/improbable-research/keanu.git","svn_url":"https://github.com/improbable-research/keanu","homepage":"","size":10739,"stargazers_count":115,"watchers_count":115,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":24,"mirror_url":null,"archived":false,"open_issues_count":21,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":24,"open_issues":21,"watchers":115,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/improbable-research/keanu/pulls/391"},"html":{"href":"https://github.com/improbable-research/keanu/pull/391"},"issue":{"href":"https://api.github.com/repos/improbable-research/keanu/issues/391"},"comments":{"href":"https://api.github.com/repos/improbable-research/keanu/issues/391/comments"},"review_comments":{"href":"https://api.github.com/repos/improbable-research/keanu/pulls/391/comments"},"review_comment":{"href":"https://api.github.com/repos/improbable-research/keanu/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/improbable-research/keanu/pulls/391/commits"},"statuses":{"href":"https://api.github.com/repos/improbable-research/keanu/statuses/8d402985c148997416c42078681ef0c1416887ab"}},"author_association":"COLLABORATOR"}}
{"actor":{"display_login":"gordoncaleb"}}