Giter VIP home page Giter VIP logo

trun_mvnt's Introduction

Sampling from Truncated Multivariate Normal and t Distributions under linear inequality constraints

This module implements the Efficient sampling algorithm of truncated multivariate (scale) mixtures of normals under linear inequality constraints proposed by Li and Ghosh (2015) (doi:10.1080/15598608.2014.996690) under Python environment (analogous to the R package tmvmixnorm).

The two cores function rtmvn() and rtmvt() are useful to overcoming difficulties in simulating truncated normal and Student's t distribution respectively. Efficient rejection sampling for simulating truncated univariate normal distribution is also included in the modeule, which shows superiority in terms of acceptance rate and numerical stability compared to existing methods. An efficient function for sampling from truncated multivariate normal distribution subject to convex polytope restriction regions based on Gibbs sampler.

author: Ting Fung (Ralph) Ma

email: [email protected] (feel free to email me if you find any bug!)

Summary:

In short, this module can be used to generate random sample of truncated multivariate normal (and Student't). Note that the truncation can be in the form of many (linear) constraints.

Suppose we want to draw sample from p-dimensioanl normal ), and similarly for Student's t with df=,

subject to constranst(s): formula, where formula is formula, even if formula.

The core functions rtmvn() and rtmvt() can solve the problem well.

Examples:

import numpy as np
from trun_mvnt import rtmvn, rtmvt


########## Traditional problem  ##########
##### lower < X < upper #####
# So D = identity matrix

D = np.diag(np.ones(4))
lower = np.array([-1,-2,-3,-4])
upper = -lower
Mean = np.zeros(4)
Sigma = np.diag([1,2,3,4])

n = 10 # want 500 final sample
burn = 100 # burn-in first 100 iterates
thin = 1 # thinning for Gibbs


random_sample = rtmvn(n, Mean, Sigma, D, lower, upper, burn, thin) 
# Numpy array n-by-p as result!
random_sample

# Suppose you want a Student t

nu = 10
random_sample_t = rtmvt(n, Mean, Sigma, nu, D, lower, upper, burn, thin)
# Numpy array n-by-p as result!
random_sample_t

########## Non-full rank problem (more constraints than dimension) ##########
Mean = np.array([0,0])
Sigma = np.array([1, 0.5, 0.5, 1]).reshape((2,2)) # bivariate normal

D = np.array([1,0,0,1,1,-1]).reshape((3,2)) # non-full rank problem
lower = np.array([-2,-1,-2])
upper = np.array([2,3,5])

n = 500 # want 500 final sample
burn = 100 # burn-in first 100 iterates
thin = 1 # thinning for Gibbs

random_sample = rtmvn(n, Mean, Sigma, D, lower, upper, burn, thin) # Numpy array n-by-p as result!




trun_mvnt's People

Contributors

ralphma1203 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.