How to Use MCMC with a Custom Log-Probability and Solve for a Matrixsetting up MCMC with log-likelihood and log-normal prior with PyMCPyMC3: How can I code my custom distribution with observed data better for Theano?pymc3: Disaster example with deterministic switchpoint functionGARCH model in pymc3: how to loop over random variables?Theano Scan and Repeatpymc3 theano function usagePYMC3: NUTS has difficulty sampling from a hierarchical zero inflated gamma modelpymc3 Conditional deterministic likelihood functionUse Chi-Squared statistic in pymc3PyMC3- Custom theano Op to do numerical integration

How to have poached eggs in "sphere form"?

Why were contact sensors put on three of the Lunar Module's four legs? Did they ever bend and stick out sideways?

Is it okay for me to decline a project on ethical grounds?

Why force the nose of 737 Max down in the first place?

Exploiting the delay when a festival ticket is scanned

What is a good example for artistic ND filter applications?

What language is Raven using for her attack in the new 52?

Can Lightning Lure be used to knock out a creature like a magical Taser?

Should I accept an invitation to give a talk from someone who might review my proposal?

Why did House of Representatives need to condemn Trumps Tweets?

Can Papyrus be folded?

Why did I lose on time with 3 pawns vs Knight. Shouldn't it be a draw?

Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

What do I do with a party that is much stronger than their level?

Should I intervene when a colleague in a different department makes students run laps as part of their grade?

What clothes would flying-people wear?

Struggling with cyclical dependencies in unit tests

What are the cons of stateless password generators?

What force enables us to walk? Friction or normal reaction?

Why is the Apollo LEM ladder so far from the ground?

Why would a personal invisible shield be necessary?

Why is softmax function used to calculate probabilities although we can divide each value by the sum of the vector?

8086 stack segment and avoiding overflow in interrupts

Problem with Eigenvectors



How to Use MCMC with a Custom Log-Probability and Solve for a Matrix


setting up MCMC with log-likelihood and log-normal prior with PyMCPyMC3: How can I code my custom distribution with observed data better for Theano?pymc3: Disaster example with deterministic switchpoint functionGARCH model in pymc3: how to loop over random variables?Theano Scan and Repeatpymc3 theano function usagePYMC3: NUTS has difficulty sampling from a hierarchical zero inflated gamma modelpymc3 Conditional deterministic likelihood functionUse Chi-Squared statistic in pymc3PyMC3- Custom theano Op to do numerical integration






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















The code is in PyMC3, but this is a general problem. I want to find which matrix (combination of variables) gives me the highest probability. Taking the mean of the trace of each element is meaningless because they depend on each other.



Here is a simple case; the code uses a vector rather than a matrix for simplicity. The goal is to find a vector of length 2, where the each value is between 0 and 1, so that the sum is 1.



import numpy as np
import theano
import theano.tensor as tt
import pymc3 as mc

# define a theano Op for our likelihood function
class LogLike_Matrix(tt.Op):
itypes = [tt.dvector] # expects a vector of parameter values when called
otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)

def __init__(self, loglike):
self.likelihood = loglike # the log-p function

def perform(self, node, inputs, outputs):
# the method that is used when calling the Op
theta, = inputs # this will contain my variables

# call the log-likelihood function
logl = self.likelihood(theta)

outputs[0][0] = np.array(logl) # output the log-likelihood

def logLikelihood_Matrix(data):
"""
We want sum(data) = 1
"""
p = 1-np.abs(np.sum(data)-1)
return np.log(p)

logl_matrix = LogLike_Matrix(logLikelihood_Matrix)

# use PyMC3 to sampler from log-likelihood
with mc.Model():
"""
Data will be sampled randomly with uniform distribution
because the log-p doesn't work on it
"""
data_matrix = mc.Uniform('data_matrix', shape=(2), lower=0.0, upper=1.0)

# convert m and c to a tensor vector
theta = tt.as_tensor_variable(data_matrix)

# use a DensityDist (use a lamdba function to "call" the Op)
mc.DensityDist('likelihood_matrix', lambda v: logl_matrix(v), observed='v': theta)

trace_matrix = mc.sample(5000, tune=100, discard_tuned_samples=True)









share|improve this question






























    1















    The code is in PyMC3, but this is a general problem. I want to find which matrix (combination of variables) gives me the highest probability. Taking the mean of the trace of each element is meaningless because they depend on each other.



    Here is a simple case; the code uses a vector rather than a matrix for simplicity. The goal is to find a vector of length 2, where the each value is between 0 and 1, so that the sum is 1.



    import numpy as np
    import theano
    import theano.tensor as tt
    import pymc3 as mc

    # define a theano Op for our likelihood function
    class LogLike_Matrix(tt.Op):
    itypes = [tt.dvector] # expects a vector of parameter values when called
    otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)

    def __init__(self, loglike):
    self.likelihood = loglike # the log-p function

    def perform(self, node, inputs, outputs):
    # the method that is used when calling the Op
    theta, = inputs # this will contain my variables

    # call the log-likelihood function
    logl = self.likelihood(theta)

    outputs[0][0] = np.array(logl) # output the log-likelihood

    def logLikelihood_Matrix(data):
    """
    We want sum(data) = 1
    """
    p = 1-np.abs(np.sum(data)-1)
    return np.log(p)

    logl_matrix = LogLike_Matrix(logLikelihood_Matrix)

    # use PyMC3 to sampler from log-likelihood
    with mc.Model():
    """
    Data will be sampled randomly with uniform distribution
    because the log-p doesn't work on it
    """
    data_matrix = mc.Uniform('data_matrix', shape=(2), lower=0.0, upper=1.0)

    # convert m and c to a tensor vector
    theta = tt.as_tensor_variable(data_matrix)

    # use a DensityDist (use a lamdba function to "call" the Op)
    mc.DensityDist('likelihood_matrix', lambda v: logl_matrix(v), observed='v': theta)

    trace_matrix = mc.sample(5000, tune=100, discard_tuned_samples=True)









    share|improve this question


























      1












      1








      1








      The code is in PyMC3, but this is a general problem. I want to find which matrix (combination of variables) gives me the highest probability. Taking the mean of the trace of each element is meaningless because they depend on each other.



      Here is a simple case; the code uses a vector rather than a matrix for simplicity. The goal is to find a vector of length 2, where the each value is between 0 and 1, so that the sum is 1.



      import numpy as np
      import theano
      import theano.tensor as tt
      import pymc3 as mc

      # define a theano Op for our likelihood function
      class LogLike_Matrix(tt.Op):
      itypes = [tt.dvector] # expects a vector of parameter values when called
      otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)

      def __init__(self, loglike):
      self.likelihood = loglike # the log-p function

      def perform(self, node, inputs, outputs):
      # the method that is used when calling the Op
      theta, = inputs # this will contain my variables

      # call the log-likelihood function
      logl = self.likelihood(theta)

      outputs[0][0] = np.array(logl) # output the log-likelihood

      def logLikelihood_Matrix(data):
      """
      We want sum(data) = 1
      """
      p = 1-np.abs(np.sum(data)-1)
      return np.log(p)

      logl_matrix = LogLike_Matrix(logLikelihood_Matrix)

      # use PyMC3 to sampler from log-likelihood
      with mc.Model():
      """
      Data will be sampled randomly with uniform distribution
      because the log-p doesn't work on it
      """
      data_matrix = mc.Uniform('data_matrix', shape=(2), lower=0.0, upper=1.0)

      # convert m and c to a tensor vector
      theta = tt.as_tensor_variable(data_matrix)

      # use a DensityDist (use a lamdba function to "call" the Op)
      mc.DensityDist('likelihood_matrix', lambda v: logl_matrix(v), observed='v': theta)

      trace_matrix = mc.sample(5000, tune=100, discard_tuned_samples=True)









      share|improve this question














      The code is in PyMC3, but this is a general problem. I want to find which matrix (combination of variables) gives me the highest probability. Taking the mean of the trace of each element is meaningless because they depend on each other.



      Here is a simple case; the code uses a vector rather than a matrix for simplicity. The goal is to find a vector of length 2, where the each value is between 0 and 1, so that the sum is 1.



      import numpy as np
      import theano
      import theano.tensor as tt
      import pymc3 as mc

      # define a theano Op for our likelihood function
      class LogLike_Matrix(tt.Op):
      itypes = [tt.dvector] # expects a vector of parameter values when called
      otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)

      def __init__(self, loglike):
      self.likelihood = loglike # the log-p function

      def perform(self, node, inputs, outputs):
      # the method that is used when calling the Op
      theta, = inputs # this will contain my variables

      # call the log-likelihood function
      logl = self.likelihood(theta)

      outputs[0][0] = np.array(logl) # output the log-likelihood

      def logLikelihood_Matrix(data):
      """
      We want sum(data) = 1
      """
      p = 1-np.abs(np.sum(data)-1)
      return np.log(p)

      logl_matrix = LogLike_Matrix(logLikelihood_Matrix)

      # use PyMC3 to sampler from log-likelihood
      with mc.Model():
      """
      Data will be sampled randomly with uniform distribution
      because the log-p doesn't work on it
      """
      data_matrix = mc.Uniform('data_matrix', shape=(2), lower=0.0, upper=1.0)

      # convert m and c to a tensor vector
      theta = tt.as_tensor_variable(data_matrix)

      # use a DensityDist (use a lamdba function to "call" the Op)
      mc.DensityDist('likelihood_matrix', lambda v: logl_matrix(v), observed='v': theta)

      trace_matrix = mc.sample(5000, tune=100, discard_tuned_samples=True)






      pymc3 pymc mcmc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 20:34









      user1581390user1581390

      6772 gold badges7 silver badges24 bronze badges




      6772 gold badges7 silver badges24 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1














          If you only want the highest likelihood parameter values, then you want the Maximum A Posteriori (MAP) estimate, which can be obtained using pymc3.find_MAP() (see starting.py for method details). If you expect a multimodal posterior, then you will likely need to run this repeatedly with different initializations and select the one that obtains the largest logp value, but that still only increases the chances of finding the global optimum, though cannot guarantee it.



          It should be noted that at high parameter dimensions, the MAP estimate is usually not part of the typical set, i.e., it is not representative of typical parameter values that would lead to the observed data. Michael Betancourt discusses this in A Conceptual Introduction to Hamiltonian Monte Carlo. The fully Bayesian approach is to use posterior predictive distributions, which effectively averages over all the high-likelihood parameter configurations rather than using a single point estimate for parameters.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365787%2fhow-to-use-mcmc-with-a-custom-log-probability-and-solve-for-a-matrix%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            If you only want the highest likelihood parameter values, then you want the Maximum A Posteriori (MAP) estimate, which can be obtained using pymc3.find_MAP() (see starting.py for method details). If you expect a multimodal posterior, then you will likely need to run this repeatedly with different initializations and select the one that obtains the largest logp value, but that still only increases the chances of finding the global optimum, though cannot guarantee it.



            It should be noted that at high parameter dimensions, the MAP estimate is usually not part of the typical set, i.e., it is not representative of typical parameter values that would lead to the observed data. Michael Betancourt discusses this in A Conceptual Introduction to Hamiltonian Monte Carlo. The fully Bayesian approach is to use posterior predictive distributions, which effectively averages over all the high-likelihood parameter configurations rather than using a single point estimate for parameters.






            share|improve this answer





























              1














              If you only want the highest likelihood parameter values, then you want the Maximum A Posteriori (MAP) estimate, which can be obtained using pymc3.find_MAP() (see starting.py for method details). If you expect a multimodal posterior, then you will likely need to run this repeatedly with different initializations and select the one that obtains the largest logp value, but that still only increases the chances of finding the global optimum, though cannot guarantee it.



              It should be noted that at high parameter dimensions, the MAP estimate is usually not part of the typical set, i.e., it is not representative of typical parameter values that would lead to the observed data. Michael Betancourt discusses this in A Conceptual Introduction to Hamiltonian Monte Carlo. The fully Bayesian approach is to use posterior predictive distributions, which effectively averages over all the high-likelihood parameter configurations rather than using a single point estimate for parameters.






              share|improve this answer



























                1












                1








                1







                If you only want the highest likelihood parameter values, then you want the Maximum A Posteriori (MAP) estimate, which can be obtained using pymc3.find_MAP() (see starting.py for method details). If you expect a multimodal posterior, then you will likely need to run this repeatedly with different initializations and select the one that obtains the largest logp value, but that still only increases the chances of finding the global optimum, though cannot guarantee it.



                It should be noted that at high parameter dimensions, the MAP estimate is usually not part of the typical set, i.e., it is not representative of typical parameter values that would lead to the observed data. Michael Betancourt discusses this in A Conceptual Introduction to Hamiltonian Monte Carlo. The fully Bayesian approach is to use posterior predictive distributions, which effectively averages over all the high-likelihood parameter configurations rather than using a single point estimate for parameters.






                share|improve this answer













                If you only want the highest likelihood parameter values, then you want the Maximum A Posteriori (MAP) estimate, which can be obtained using pymc3.find_MAP() (see starting.py for method details). If you expect a multimodal posterior, then you will likely need to run this repeatedly with different initializations and select the one that obtains the largest logp value, but that still only increases the chances of finding the global optimum, though cannot guarantee it.



                It should be noted that at high parameter dimensions, the MAP estimate is usually not part of the typical set, i.e., it is not representative of typical parameter values that would lead to the observed data. Michael Betancourt discusses this in A Conceptual Introduction to Hamiltonian Monte Carlo. The fully Bayesian approach is to use posterior predictive distributions, which effectively averages over all the high-likelihood parameter configurations rather than using a single point estimate for parameters.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 1 at 17:05









                mervmerv

                27.7k6 gold badges77 silver badges119 bronze badges




                27.7k6 gold badges77 silver badges119 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365787%2fhow-to-use-mcmc-with-a-custom-log-probability-and-solve-for-a-matrix%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해