{"action":"created","comment":{"url":"https://api.github.com/repos/lmfit/lmfit-py/pulls/comments/38489064","id":38489064,"diff_hunk":"@@ -499,6 +507,124 @@ def scalar_minimize(self, method='Nelder-Mead', params=None, **kws):\n \n         return result\n \n+    def emcee(self, params=None, steps=1000, nwalkers=100, burn=0, thin=1,\n+              ntemps=1):\n+        \"\"\"\n+        Bayesian sampling of the posterior distribution for the parameters\n+        using the `emcee` Markov Chain Monte Carlo package.\n+        The method assumes that the prior is Uniform.\n+        You need to have `emcee` and `pandas` installed to use this method.\n+\n+        Parameters\n+        ----------\n+        steps : int, optional\n+            How many samples you would like to draw from the posterior\n+            distribution?\n+        burn : int, optional\n+            Discard this many samples from the start of the sampling regime.\n+        thin : int, optional\n+            Only accept 1 in every `thin` samples.\n+        ntemps : int, optional\n+            If `ntemps > 1` perform a Parallel Tempering.\n+\n+        Returns\n+        -------\n+        result, chain : MinimizerResult, pandas.DataFrame\n+            MinimizerResult object contains updated params, fit statistics, etc.\n+            The `chain` contains the samples. Has shape (steps, ndims). Access\n+            chain values for a particular parameter using chain[parname].\n+        \"\"\"\n+        if not HAS_EMCEE:\n+            raise NotImplementedError('You must have emcee to use the emcee '\n+                                      'method')\n+\n+        result = self.prepare_fit(params=params)\n+        vars   = result.init_vals\n+        params = result.params\n+\n+        # Removing internal parameter scaling. We could possibly keep it,\n+        # but I don't know how this affects the emcee sampling.\n+        bounds_varying = []\n+        for i, par in enumerate(params):\n+            param = params[par]\n+            vars[i] = param.from_internal(param.value)\n+            param.from_internal = lambda val: val\n+            lb, ub = param.min, param.max\n+            if lb is None or lb is np.nan:\n+                lb = -np.inf\n+            if ub is None or ub is np.nan:\n+                ub = np.inf\n+            bounds_varying.append((lb, ub))\n+\n+        bounds_varying = np.array(bounds_varying)\n+\n+        self.nvarys = len(result.var_names)\n+\n+        def lnlike(theta):\n+            # log likelihood\n+            return -0.5 * self.penalty(theta)\n+\n+        def lnprior(theta):\n+            # stay within the prior specified by the parameter bounds\n+            if (np.any(theta > bounds_varying[:, 1])\n+                    or np.any(theta < bounds_varying[:, 0])):\n+                return -np.inf\n+            return 0\n+\n+        def lnprob(theta):\n+            lp = lnprior(theta)\n+            if not np.isfinite(lp):\n+                return -np.inf\n+            return lp + lnlike(theta)\n+\n+        if ntemps > 1:\n+            # jitter the starting position by scaled Gaussian noise (1% level)\n+            p0 = 1 + np.random.randn(ntemps, nwalkers, self.nvarys) * 1.e-2","path":"lmfit/minimizer.py","position":91,"original_position":91,"commit_id":"b6f03f1ef95cabea36d8d46bf0196499e40402c9","original_commit_id":"b6f03f1ef95cabea36d8d46bf0196499e40402c9","user":{"login":"andyfaff","id":702934,"avatar_url":"https://avatars.githubusercontent.com/u/702934?v=3","gravatar_id":"","url":"https://api.github.com/users/andyfaff","html_url":"https://github.com/andyfaff","followers_url":"https://api.github.com/users/andyfaff/followers","following_url":"https://api.github.com/users/andyfaff/following{/other_user}","gists_url":"https://api.github.com/users/andyfaff/gists{/gist_id}","starred_url":"https://api.github.com/users/andyfaff/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andyfaff/subscriptions","organizations_url":"https://api.github.com/users/andyfaff/orgs","repos_url":"https://api.github.com/users/andyfaff/repos","events_url":"https://api.github.com/users/andyfaff/events{/privacy}","received_events_url":"https://api.github.com/users/andyfaff/received_events","type":"User","site_admin":false},"body":"I had it that way originally. \r\nIt is clarity vs having two lines of code.  No problem to change it.","created_at":"2015-09-02T00:32:45Z","updated_at":"2015-09-02T00:32:45Z","html_url":"https://github.com/lmfit/lmfit-py/pull/249#discussion_r38489064","pull_request_url":"https://api.github.com/repos/lmfit/lmfit-py/pulls/249","_links":{"self":{"href":"https://api.github.com/repos/lmfit/lmfit-py/pulls/comments/38489064"},"html":{"href":"https://github.com/lmfit/lmfit-py/pull/249#discussion_r38489064"},"pull_request":{"href":"https://api.github.com/repos/lmfit/lmfit-py/pulls/249"}}},"pull_request":{"url":"https://api.github.com/repos/lmfit/lmfit-py/pulls/249","id":43834728,"html_url":"https://github.com/lmfit/lmfit-py/pull/249","diff_url":"https://github.com/lmfit/lmfit-py/pull/249.diff","patch_url":"https://github.com/lmfit/lmfit-py/pull/249.patch","issue_url":"https://api.github.com/repos/lmfit/lmfit-py/issues/249","number":249,"state":"open","locked":false,"title":"ENH: adding an emcee sampler to Minimizer","user":{"login":"andyfaff","id":702934,"avatar_url":"https://avatars.githubusercontent.com/u/702934?v=3","gravatar_id":"","url":"https://api.github.com/users/andyfaff","html_url":"https://github.com/andyfaff","followers_url":"https://api.github.com/users/andyfaff/followers","following_url":"https://api.github.com/users/andyfaff/following{/other_user}","gists_url":"https://api.github.com/users/andyfaff/gists{/gist_id}","starred_url":"https://api.github.com/users/andyfaff/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andyfaff/subscriptions","organizations_url":"https://api.github.com/users/andyfaff/orgs","repos_url":"https://api.github.com/users/andyfaff/repos","events_url":"https://api.github.com/users/andyfaff/events{/privacy}","received_events_url":"https://api.github.com/users/andyfaff/received_events","type":"User","site_admin":false},"body":"This PR proposes to add the `emcee` Markov Chain Monte Carlo ensemble sampler to lmfit.  This is a Bayesian statistical technique for sampling the posterior distribution for the parameters. \r\nAmongst other things it is a robust way of examining parameter correlations, etc. Use of this method would require the user to install `emcee` and `pandas`.  However, if they are not installed then an Error is raised (no need to add extra dependencies for lmfit).\r\nThis method does not require upper and lower bounds for each parameter, but it would probably converge faster if it did.","created_at":"2015-09-01T03:46:25Z","updated_at":"2015-09-02T00:32:45Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b1450fb1d10fa19d10fe39fa7981a1ffd4dea951","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/lmfit/lmfit-py/pulls/249/commits","review_comments_url":"https://api.github.com/repos/lmfit/lmfit-py/pulls/249/comments","review_comment_url":"https://api.github.com/repos/lmfit/lmfit-py/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lmfit/lmfit-py/issues/249/comments","statuses_url":"https://api.github.com/repos/lmfit/lmfit-py/statuses/b6f03f1ef95cabea36d8d46bf0196499e40402c9","head":{"label":"andyfaff:emcee","ref":"emcee","sha":"b6f03f1ef95cabea36d8d46bf0196499e40402c9","user":{"login":"andyfaff","id":702934,"avatar_url":"https://avatars.githubusercontent.com/u/702934?v=3","gravatar_id":"","url":"https://api.github.com/users/andyfaff","html_url":"https://github.com/andyfaff","followers_url":"https://api.github.com/users/andyfaff/followers","following_url":"https://api.github.com/users/andyfaff/following{/other_user}","gists_url":"https://api.github.com/users/andyfaff/gists{/gist_id}","starred_url":"https://api.github.com/users/andyfaff/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andyfaff/subscriptions","organizations_url":"https://api.github.com/users/andyfaff/orgs","repos_url":"https://api.github.com/users/andyfaff/repos","events_url":"https://api.github.com/users/andyfaff/events{/privacy}","received_events_url":"https://api.github.com/users/andyfaff/received_events","type":"User","site_admin":false},"repo":{"id":27204178,"name":"lmfit-py","full_name":"andyfaff/lmfit-py","owner":{"login":"andyfaff","id":702934,"avatar_url":"https://avatars.githubusercontent.com/u/702934?v=3","gravatar_id":"","url":"https://api.github.com/users/andyfaff","html_url":"https://github.com/andyfaff","followers_url":"https://api.github.com/users/andyfaff/followers","following_url":"https://api.github.com/users/andyfaff/following{/other_user}","gists_url":"https://api.github.com/users/andyfaff/gists{/gist_id}","starred_url":"https://api.github.com/users/andyfaff/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andyfaff/subscriptions","organizations_url":"https://api.github.com/users/andyfaff/orgs","repos_url":"https://api.github.com/users/andyfaff/repos","events_url":"https://api.github.com/users/andyfaff/events{/privacy}","received_events_url":"https://api.github.com/users/andyfaff/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/andyfaff/lmfit-py","description":"Simple Least Squares Minimization, with flexible Parameter settings, based on scipy.optimize.leastsq ","fork":true,"url":"https://api.github.com/repos/andyfaff/lmfit-py","forks_url":"https://api.github.com/repos/andyfaff/lmfit-py/forks","keys_url":"https://api.github.com/repos/andyfaff/lmfit-py/keys{/key_id}","collaborators_url":"https://api.github.com/repos/andyfaff/lmfit-py/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/andyfaff/lmfit-py/teams","hooks_url":"https://api.github.com/repos/andyfaff/lmfit-py/hooks","issue_events_url":"https://api.github.com/repos/andyfaff/lmfit-py/issues/events{/number}","events_url":"https://api.github.com/repos/andyfaff/lmfit-py/events","assignees_url":"https://api.github.com/repos/andyfaff/lmfit-py/assignees{/user}","branches_url":"https://api.github.com/repos/andyfaff/lmfit-py/branches{/branch}","tags_url":"https://api.github.com/repos/andyfaff/lmfit-py/tags","blobs_url":"https://api.github.com/repos/andyfaff/lmfit-py/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/andyfaff/lmfit-py/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/andyfaff/lmfit-py/git/refs{/sha}","trees_url":"https://api.github.com/repos/andyfaff/lmfit-py/git/trees{/sha}","statuses_url":"https://api.github.com/repos/andyfaff/lmfit-py/statuses/{sha}","languages_url":"https://api.github.com/repos/andyfaff/lmfit-py/languages","stargazers_url":"https://api.github.com/repos/andyfaff/lmfit-py/stargazers","contributors_url":"https://api.github.com/repos/andyfaff/lmfit-py/contributors","subscribers_url":"https://api.github.com/repos/andyfaff/lmfit-py/subscribers","subscription_url":"https://api.github.com/repos/andyfaff/lmfit-py/subscription","commits_url":"https://api.github.com/repos/andyfaff/lmfit-py/commits{/sha}","git_commits_url":"https://api.github.com/repos/andyfaff/lmfit-py/git/commits{/sha}","comments_url":"https://api.github.com/repos/andyfaff/lmfit-py/comments{/number}","issue_comment_url":"https://api.github.com/repos/andyfaff/lmfit-py/issues/comments{/number}","contents_url":"https://api.github.com/repos/andyfaff/lmfit-py/contents/{+path}","compare_url":"https://api.github.com/repos/andyfaff/lmfit-py/compare/{base}...{head}","merges_url":"https://api.github.com/repos/andyfaff/lmfit-py/merges","archive_url":"https://api.github.com/repos/andyfaff/lmfit-py/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/andyfaff/lmfit-py/downloads","issues_url":"https://api.github.com/repos/andyfaff/lmfit-py/issues{/number}","pulls_url":"https://api.github.com/repos/andyfaff/lmfit-py/pulls{/number}","milestones_url":"https://api.github.com/repos/andyfaff/lmfit-py/milestones{/number}","notifications_url":"https://api.github.com/repos/andyfaff/lmfit-py/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/andyfaff/lmfit-py/labels{/name}","releases_url":"https://api.github.com/repos/andyfaff/lmfit-py/releases{/id}","created_at":"2014-11-27T01:42:11Z","updated_at":"2015-01-12T00:10:18Z","pushed_at":"2015-09-01T03:42:31Z","git_url":"git://github.com/andyfaff/lmfit-py.git","ssh_url":"git@github.com:andyfaff/lmfit-py.git","clone_url":"https://github.com/andyfaff/lmfit-py.git","svn_url":"https://github.com/andyfaff/lmfit-py","homepage":"http:/lmfit.github.io/lmfit-py/","size":8855,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"lmfit:master","ref":"master","sha":"800f3e40ef8ba977005d27a5d05fd231688a5749","user":{"login":"lmfit","id":6190878,"avatar_url":"https://avatars.githubusercontent.com/u/6190878?v=3","gravatar_id":"","url":"https://api.github.com/users/lmfit","html_url":"https://github.com/lmfit","followers_url":"https://api.github.com/users/lmfit/followers","following_url":"https://api.github.com/users/lmfit/following{/other_user}","gists_url":"https://api.github.com/users/lmfit/gists{/gist_id}","starred_url":"https://api.github.com/users/lmfit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lmfit/subscriptions","organizations_url":"https://api.github.com/users/lmfit/orgs","repos_url":"https://api.github.com/users/lmfit/repos","events_url":"https://api.github.com/users/lmfit/events{/privacy}","received_events_url":"https://api.github.com/users/lmfit/received_events","type":"Organization","site_admin":false},"repo":{"id":2136524,"name":"lmfit-py","full_name":"lmfit/lmfit-py","owner":{"login":"lmfit","id":6190878,"avatar_url":"https://avatars.githubusercontent.com/u/6190878?v=3","gravatar_id":"","url":"https://api.github.com/users/lmfit","html_url":"https://github.com/lmfit","followers_url":"https://api.github.com/users/lmfit/followers","following_url":"https://api.github.com/users/lmfit/following{/other_user}","gists_url":"https://api.github.com/users/lmfit/gists{/gist_id}","starred_url":"https://api.github.com/users/lmfit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lmfit/subscriptions","organizations_url":"https://api.github.com/users/lmfit/orgs","repos_url":"https://api.github.com/users/lmfit/repos","events_url":"https://api.github.com/users/lmfit/events{/privacy}","received_events_url":"https://api.github.com/users/lmfit/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lmfit/lmfit-py","description":"Non-Linear Least Squares Minimization, with flexible Parameter settings, based on scipy.optimize.leastsq, and with many additional classes and methods for curve fitting ","fork":false,"url":"https://api.github.com/repos/lmfit/lmfit-py","forks_url":"https://api.github.com/repos/lmfit/lmfit-py/forks","keys_url":"https://api.github.com/repos/lmfit/lmfit-py/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lmfit/lmfit-py/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lmfit/lmfit-py/teams","hooks_url":"https://api.github.com/repos/lmfit/lmfit-py/hooks","issue_events_url":"https://api.github.com/repos/lmfit/lmfit-py/issues/events{/number}","events_url":"https://api.github.com/repos/lmfit/lmfit-py/events","assignees_url":"https://api.github.com/repos/lmfit/lmfit-py/assignees{/user}","branches_url":"https://api.github.com/repos/lmfit/lmfit-py/branches{/branch}","tags_url":"https://api.github.com/repos/lmfit/lmfit-py/tags","blobs_url":"https://api.github.com/repos/lmfit/lmfit-py/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lmfit/lmfit-py/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lmfit/lmfit-py/git/refs{/sha}","trees_url":"https://api.github.com/repos/lmfit/lmfit-py/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lmfit/lmfit-py/statuses/{sha}","languages_url":"https://api.github.com/repos/lmfit/lmfit-py/languages","stargazers_url":"https://api.github.com/repos/lmfit/lmfit-py/stargazers","contributors_url":"https://api.github.com/repos/lmfit/lmfit-py/contributors","subscribers_url":"https://api.github.com/repos/lmfit/lmfit-py/subscribers","subscription_url":"https://api.github.com/repos/lmfit/lmfit-py/subscription","commits_url":"https://api.github.com/repos/lmfit/lmfit-py/commits{/sha}","git_commits_url":"https://api.github.com/repos/lmfit/lmfit-py/git/commits{/sha}","comments_url":"https://api.github.com/repos/lmfit/lmfit-py/comments{/number}","issue_comment_url":"https://api.github.com/repos/lmfit/lmfit-py/issues/comments{/number}","contents_url":"https://api.github.com/repos/lmfit/lmfit-py/contents/{+path}","compare_url":"https://api.github.com/repos/lmfit/lmfit-py/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lmfit/lmfit-py/merges","archive_url":"https://api.github.com/repos/lmfit/lmfit-py/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lmfit/lmfit-py/downloads","issues_url":"https://api.github.com/repos/lmfit/lmfit-py/issues{/number}","pulls_url":"https://api.github.com/repos/lmfit/lmfit-py/pulls{/number}","milestones_url":"https://api.github.com/repos/lmfit/lmfit-py/milestones{/number}","notifications_url":"https://api.github.com/repos/lmfit/lmfit-py/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lmfit/lmfit-py/labels{/name}","releases_url":"https://api.github.com/repos/lmfit/lmfit-py/releases{/id}","created_at":"2011-08-01T12:13:19Z","updated_at":"2015-08-20T10:22:06Z","pushed_at":"2015-09-01T03:46:26Z","git_url":"git://github.com/lmfit/lmfit-py.git","ssh_url":"git@github.com:lmfit/lmfit-py.git","clone_url":"https://github.com/lmfit/lmfit-py.git","svn_url":"https://github.com/lmfit/lmfit-py","homepage":"http:/lmfit.github.io/lmfit-py/","size":10670,"stargazers_count":92,"watchers_count":92,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":51,"mirror_url":null,"open_issues_count":28,"forks":51,"open_issues":28,"watchers":92,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lmfit/lmfit-py/pulls/249"},"html":{"href":"https://github.com/lmfit/lmfit-py/pull/249"},"issue":{"href":"https://api.github.com/repos/lmfit/lmfit-py/issues/249"},"comments":{"href":"https://api.github.com/repos/lmfit/lmfit-py/issues/249/comments"},"review_comments":{"href":"https://api.github.com/repos/lmfit/lmfit-py/pulls/249/comments"},"review_comment":{"href":"https://api.github.com/repos/lmfit/lmfit-py/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lmfit/lmfit-py/pulls/249/commits"},"statuses":{"href":"https://api.github.com/repos/lmfit/lmfit-py/statuses/b6f03f1ef95cabea36d8d46bf0196499e40402c9"}}}}