{"action":"created","comment":{"url":"https://api.github.com/repos/gonum/stat/pulls/comments/44373720","id":44373720,"diff_hunk":"@@ -118,3 +121,107 @@ func (n *Normal) Rand(x []float64) []float64 {\n \tfloats.Add(x, n.mu)\n \treturn x\n }\n+\n+// ConditionNormal returns the Normal distribution that is the receiver conditioned\n+// on the input evidence. The returned multivariate normal has dimension\n+// n - len(dims), where n is the dimension of the original receiver. The updated\n+// mean and covariance are\n+//  mu = mu_1 + sigma_{2,1}^T * sigma_{2,2}^-1 (v - mu_2)\n+//  sigma = sigma_{1,1} - sigma_{2,1}^T * sigma_{2,2}^-1 * sigma_{2,1}\n+// where the elements of x_2 have been observed with values v. The dimension order\n+// is preserved during conditioning, so if the value of dimension 1 is observed,\n+// the returned normal represents dimensions {0, 2, ...} of the original Normal\n+// distribution.\n+//\n+// ConditionalNormal returns {nil, false} if there is a failure during the update.\n+// Mathematically this is impossible, but can occur with finite precision arithmetic.\n+func (n *Normal) ConditionNormal(observed []int, values []float64, src *rand.Rand) (*Normal, bool) {\n+\tif len(observed) != len(values) {\n+\t\tpanic(\"normal: input slice length mismatch\")\n+\t}\n+\n+\tn.setSigma()\n+\n+\tob := len(observed)\n+\tunob := n.Dim() - ob\n+\tobMap := make(map[int]struct{})\n+\tfor _, v := range observed {\n+\t\tif _, ok := obMap[v]; ok {\n+\t\t\tpanic(\"normal: observed dimension occurs twice\")\n+\t\t}\n+\t\tobMap[v] = struct{}{}\n+\t}\n+\tunobserved := make([]int, 0, unob)\n+\tfor i := 0; i < n.Dim(); i++ {\n+\t\tif _, ok := obMap[i]; !ok {\n+\t\t\tunobserved = append(unobserved, i)\n+\t\t}\n+\t}\n+\tmu1 := make([]float64, unob)\n+\tfor i, v := range unobserved {\n+\t\tmu1[i] = n.mu[v]\n+\t}\n+\tmu2 := make([]float64, ob) // really v - mu2\n+\tfor i, v := range observed {\n+\t\tmu2[i] = values[i] - n.mu[v]\n+\t}\n+\n+\tvar sigma11, sigma22 mat64.SymDense\n+\tsigma11.SubsetSym(n.sigma, unobserved)\n+\tsigma22.SubsetSym(n.sigma, observed)\n+\n+\tsigma21 := mat64.NewDense(ob, unob, nil)\n+\tfor i, r := range observed {\n+\t\tfor j, c := range unobserved {\n+\t\t\tv := n.sigma.At(r, c)\n+\t\t\tsigma21.Set(i, j, v)\n+\t\t}\n+\t}\n+\n+\tvar chol mat64.Cholesky\n+\tok := chol.Factorize(&sigma22)\n+\tif !ok {\n+\t\treturn nil, ok\n+\t}\n+\n+\t// Compute sigma_{2,1}^T * sigma_{2,2}^-1 (v - mu_2).\n+\tv := mat64.NewVector(ob, mu2)\n+\tvar tmp, tmp2 mat64.Vector\n+\terr := tmp.SolveCholeskyVec(&chol, v)\n+\tif err != nil {\n+\t\treturn nil, false\n+\t}\n+\ttmp2.MulVec(sigma21.T(), &tmp)\n+\n+\t// Compute sigma_{2,1}^T * sigma_{2,2}^-1 * sigma_{2,1}.\n+\t// TODO(btracey): Should this be a method of SymDense?\n+\tvar tmp3, tmp4 mat64.Dense\n+\terr = tmp3.SolveCholesky(&chol, sigma21)\n+\tif err != nil {\n+\t\treturn nil, false\n+\t}\n+\ttmp4.Mul(sigma21.T(), &tmp3)\n+\n+\tfor i := range mu1 {\n+\t\tmu1[i] += tmp2.At(i, 0)\n+\t}\n+\n+\t// TODO(btracey): If tmp2 can constructed with a method, then this can be\n+\t// replaced with SubSym.\n+\tfor i := 0; i < len(unobserved); i++ {\n+\t\tfor j := i; j < len(unobserved); j++ {\n+\t\t\tv := sigma11.At(i, j)\n+\t\t\tsigma11.SetSym(i, j, v-tmp4.At(i, j))\n+\t\t}\n+\t}\n+\treturn NewNormal(mu1, &sigma11, src)\n+}\n+\n+// sigma computes and stores the covariance matrix of the distribution, or","path":"distmv/normal.go","position":119,"original_position":119,"commit_id":"52e396fd4f3a7e8f6d1ef7c898fb8027e62f6e4e","original_commit_id":"52e396fd4f3a7e8f6d1ef7c898fb8027e62f6e4e","user":{"login":"btracey","id":3680859,"avatar_url":"https://avatars.githubusercontent.com/u/3680859?v=3","gravatar_id":"","url":"https://api.github.com/users/btracey","html_url":"https://github.com/btracey","followers_url":"https://api.github.com/users/btracey/followers","following_url":"https://api.github.com/users/btracey/following{/other_user}","gists_url":"https://api.github.com/users/btracey/gists{/gist_id}","starred_url":"https://api.github.com/users/btracey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/btracey/subscriptions","organizations_url":"https://api.github.com/users/btracey/orgs","repos_url":"https://api.github.com/users/btracey/repos","events_url":"https://api.github.com/users/btracey/events{/privacy}","received_events_url":"https://api.github.com/users/btracey/received_events","type":"User","site_admin":false},"body":"Done.","created_at":"2015-11-10T06:26:38Z","updated_at":"2015-11-10T06:26:38Z","html_url":"https://github.com/gonum/stat/pull/61#discussion_r44373720","pull_request_url":"https://api.github.com/repos/gonum/stat/pulls/61","_links":{"self":{"href":"https://api.github.com/repos/gonum/stat/pulls/comments/44373720"},"html":{"href":"https://github.com/gonum/stat/pull/61#discussion_r44373720"},"pull_request":{"href":"https://api.github.com/repos/gonum/stat/pulls/61"}}},"pull_request":{"url":"https://api.github.com/repos/gonum/stat/pulls/61","id":50213193,"html_url":"https://github.com/gonum/stat/pull/61","diff_url":"https://github.com/gonum/stat/pull/61.diff","patch_url":"https://github.com/gonum/stat/pull/61.patch","issue_url":"https://api.github.com/repos/gonum/stat/issues/61","number":61,"state":"open","locked":false,"title":"Add ConditionNormal","user":{"login":"btracey","id":3680859,"avatar_url":"https://avatars.githubusercontent.com/u/3680859?v=3","gravatar_id":"","url":"https://api.github.com/users/btracey","html_url":"https://github.com/btracey","followers_url":"https://api.github.com/users/btracey/followers","following_url":"https://api.github.com/users/btracey/following{/other_user}","gists_url":"https://api.github.com/users/btracey/gists{/gist_id}","starred_url":"https://api.github.com/users/btracey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/btracey/subscriptions","organizations_url":"https://api.github.com/users/btracey/orgs","repos_url":"https://api.github.com/users/btracey/repos","events_url":"https://api.github.com/users/btracey/events{/privacy}","received_events_url":"https://api.github.com/users/btracey/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-10T04:28:29Z","updated_at":"2015-11-10T06:26:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d9f7cb7e0ac8c7755354497ac9336d48046b347a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/gonum/stat/pulls/61/commits","review_comments_url":"https://api.github.com/repos/gonum/stat/pulls/61/comments","review_comment_url":"https://api.github.com/repos/gonum/stat/pulls/comments{/number}","comments_url":"https://api.github.com/repos/gonum/stat/issues/61/comments","statuses_url":"https://api.github.com/repos/gonum/stat/statuses/52e396fd4f3a7e8f6d1ef7c898fb8027e62f6e4e","head":{"label":"gonum:normalcondition","ref":"normalcondition","sha":"52e396fd4f3a7e8f6d1ef7c898fb8027e62f6e4e","user":{"login":"gonum","id":3771091,"avatar_url":"https://avatars.githubusercontent.com/u/3771091?v=3","gravatar_id":"","url":"https://api.github.com/users/gonum","html_url":"https://github.com/gonum","followers_url":"https://api.github.com/users/gonum/followers","following_url":"https://api.github.com/users/gonum/following{/other_user}","gists_url":"https://api.github.com/users/gonum/gists{/gist_id}","starred_url":"https://api.github.com/users/gonum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gonum/subscriptions","organizations_url":"https://api.github.com/users/gonum/orgs","repos_url":"https://api.github.com/users/gonum/repos","events_url":"https://api.github.com/users/gonum/events{/privacy}","received_events_url":"https://api.github.com/users/gonum/received_events","type":"Organization","site_admin":false},"repo":{"id":22622955,"name":"stat","full_name":"gonum/stat","owner":{"login":"gonum","id":3771091,"avatar_url":"https://avatars.githubusercontent.com/u/3771091?v=3","gravatar_id":"","url":"https://api.github.com/users/gonum","html_url":"https://github.com/gonum","followers_url":"https://api.github.com/users/gonum/followers","following_url":"https://api.github.com/users/gonum/following{/other_user}","gists_url":"https://api.github.com/users/gonum/gists{/gist_id}","starred_url":"https://api.github.com/users/gonum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gonum/subscriptions","organizations_url":"https://api.github.com/users/gonum/orgs","repos_url":"https://api.github.com/users/gonum/repos","events_url":"https://api.github.com/users/gonum/events{/privacy}","received_events_url":"https://api.github.com/users/gonum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/gonum/stat","description":"Statistics package for Go","fork":false,"url":"https://api.github.com/repos/gonum/stat","forks_url":"https://api.github.com/repos/gonum/stat/forks","keys_url":"https://api.github.com/repos/gonum/stat/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gonum/stat/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gonum/stat/teams","hooks_url":"https://api.github.com/repos/gonum/stat/hooks","issue_events_url":"https://api.github.com/repos/gonum/stat/issues/events{/number}","events_url":"https://api.github.com/repos/gonum/stat/events","assignees_url":"https://api.github.com/repos/gonum/stat/assignees{/user}","branches_url":"https://api.github.com/repos/gonum/stat/branches{/branch}","tags_url":"https://api.github.com/repos/gonum/stat/tags","blobs_url":"https://api.github.com/repos/gonum/stat/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gonum/stat/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gonum/stat/git/refs{/sha}","trees_url":"https://api.github.com/repos/gonum/stat/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gonum/stat/statuses/{sha}","languages_url":"https://api.github.com/repos/gonum/stat/languages","stargazers_url":"https://api.github.com/repos/gonum/stat/stargazers","contributors_url":"https://api.github.com/repos/gonum/stat/contributors","subscribers_url":"https://api.github.com/repos/gonum/stat/subscribers","subscription_url":"https://api.github.com/repos/gonum/stat/subscription","commits_url":"https://api.github.com/repos/gonum/stat/commits{/sha}","git_commits_url":"https://api.github.com/repos/gonum/stat/git/commits{/sha}","comments_url":"https://api.github.com/repos/gonum/stat/comments{/number}","issue_comment_url":"https://api.github.com/repos/gonum/stat/issues/comments{/number}","contents_url":"https://api.github.com/repos/gonum/stat/contents/{+path}","compare_url":"https://api.github.com/repos/gonum/stat/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gonum/stat/merges","archive_url":"https://api.github.com/repos/gonum/stat/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gonum/stat/downloads","issues_url":"https://api.github.com/repos/gonum/stat/issues{/number}","pulls_url":"https://api.github.com/repos/gonum/stat/pulls{/number}","milestones_url":"https://api.github.com/repos/gonum/stat/milestones{/number}","notifications_url":"https://api.github.com/repos/gonum/stat/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gonum/stat/labels{/name}","releases_url":"https://api.github.com/repos/gonum/stat/releases{/id}","created_at":"2014-08-04T23:07:41Z","updated_at":"2015-10-17T11:10:57Z","pushed_at":"2015-11-10T06:13:33Z","git_url":"git://github.com/gonum/stat.git","ssh_url":"git@github.com:gonum/stat.git","clone_url":"https://github.com/gonum/stat.git","svn_url":"https://github.com/gonum/stat","homepage":"","size":1018,"stargazers_count":56,"watchers_count":56,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":9,"forks":10,"open_issues":9,"watchers":56,"default_branch":"master"}},"base":{"label":"gonum:master","ref":"master","sha":"74a6648c88e9b33cbe3ed6db5cfbb440be5c2358","user":{"login":"gonum","id":3771091,"avatar_url":"https://avatars.githubusercontent.com/u/3771091?v=3","gravatar_id":"","url":"https://api.github.com/users/gonum","html_url":"https://github.com/gonum","followers_url":"https://api.github.com/users/gonum/followers","following_url":"https://api.github.com/users/gonum/following{/other_user}","gists_url":"https://api.github.com/users/gonum/gists{/gist_id}","starred_url":"https://api.github.com/users/gonum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gonum/subscriptions","organizations_url":"https://api.github.com/users/gonum/orgs","repos_url":"https://api.github.com/users/gonum/repos","events_url":"https://api.github.com/users/gonum/events{/privacy}","received_events_url":"https://api.github.com/users/gonum/received_events","type":"Organization","site_admin":false},"repo":{"id":22622955,"name":"stat","full_name":"gonum/stat","owner":{"login":"gonum","id":3771091,"avatar_url":"https://avatars.githubusercontent.com/u/3771091?v=3","gravatar_id":"","url":"https://api.github.com/users/gonum","html_url":"https://github.com/gonum","followers_url":"https://api.github.com/users/gonum/followers","following_url":"https://api.github.com/users/gonum/following{/other_user}","gists_url":"https://api.github.com/users/gonum/gists{/gist_id}","starred_url":"https://api.github.com/users/gonum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gonum/subscriptions","organizations_url":"https://api.github.com/users/gonum/orgs","repos_url":"https://api.github.com/users/gonum/repos","events_url":"https://api.github.com/users/gonum/events{/privacy}","received_events_url":"https://api.github.com/users/gonum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/gonum/stat","description":"Statistics package for Go","fork":false,"url":"https://api.github.com/repos/gonum/stat","forks_url":"https://api.github.com/repos/gonum/stat/forks","keys_url":"https://api.github.com/repos/gonum/stat/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gonum/stat/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gonum/stat/teams","hooks_url":"https://api.github.com/repos/gonum/stat/hooks","issue_events_url":"https://api.github.com/repos/gonum/stat/issues/events{/number}","events_url":"https://api.github.com/repos/gonum/stat/events","assignees_url":"https://api.github.com/repos/gonum/stat/assignees{/user}","branches_url":"https://api.github.com/repos/gonum/stat/branches{/branch}","tags_url":"https://api.github.com/repos/gonum/stat/tags","blobs_url":"https://api.github.com/repos/gonum/stat/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gonum/stat/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gonum/stat/git/refs{/sha}","trees_url":"https://api.github.com/repos/gonum/stat/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gonum/stat/statuses/{sha}","languages_url":"https://api.github.com/repos/gonum/stat/languages","stargazers_url":"https://api.github.com/repos/gonum/stat/stargazers","contributors_url":"https://api.github.com/repos/gonum/stat/contributors","subscribers_url":"https://api.github.com/repos/gonum/stat/subscribers","subscription_url":"https://api.github.com/repos/gonum/stat/subscription","commits_url":"https://api.github.com/repos/gonum/stat/commits{/sha}","git_commits_url":"https://api.github.com/repos/gonum/stat/git/commits{/sha}","comments_url":"https://api.github.com/repos/gonum/stat/comments{/number}","issue_comment_url":"https://api.github.com/repos/gonum/stat/issues/comments{/number}","contents_url":"https://api.github.com/repos/gonum/stat/contents/{+path}","compare_url":"https://api.github.com/repos/gonum/stat/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gonum/stat/merges","archive_url":"https://api.github.com/repos/gonum/stat/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gonum/stat/downloads","issues_url":"https://api.github.com/repos/gonum/stat/issues{/number}","pulls_url":"https://api.github.com/repos/gonum/stat/pulls{/number}","milestones_url":"https://api.github.com/repos/gonum/stat/milestones{/number}","notifications_url":"https://api.github.com/repos/gonum/stat/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gonum/stat/labels{/name}","releases_url":"https://api.github.com/repos/gonum/stat/releases{/id}","created_at":"2014-08-04T23:07:41Z","updated_at":"2015-10-17T11:10:57Z","pushed_at":"2015-11-10T06:13:33Z","git_url":"git://github.com/gonum/stat.git","ssh_url":"git@github.com:gonum/stat.git","clone_url":"https://github.com/gonum/stat.git","svn_url":"https://github.com/gonum/stat","homepage":"","size":1018,"stargazers_count":56,"watchers_count":56,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":9,"forks":10,"open_issues":9,"watchers":56,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/gonum/stat/pulls/61"},"html":{"href":"https://github.com/gonum/stat/pull/61"},"issue":{"href":"https://api.github.com/repos/gonum/stat/issues/61"},"comments":{"href":"https://api.github.com/repos/gonum/stat/issues/61/comments"},"review_comments":{"href":"https://api.github.com/repos/gonum/stat/pulls/61/comments"},"review_comment":{"href":"https://api.github.com/repos/gonum/stat/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/gonum/stat/pulls/61/commits"},"statuses":{"href":"https://api.github.com/repos/gonum/stat/statuses/52e396fd4f3a7e8f6d1ef7c898fb8027e62f6e4e"}}}}