Giter VIP home page Giter VIP logo

blnm's Introduction

unit-tests

Binomial-Logit-Normal Mixture Model

Example

Here we provide a simple example for generating data from a mixture of bln distributions using blnm.dist, and inferring the sampling parameters using blnm.fit. A script of this example is located in example/simulate_and_fit.py. We begin by importing packages:

import numpy as np

import matplotlib.pyplot as plt

from blnm import dist
from blnm import fit

then setting simulation parameters

COEFS = [0.2, 0.1, 0.3, 0.4]
MEANS = [-3, -1, 1, 3]
VARIANCE = 0.25
N_COUNTS = 100
N_SAMPLES = 1000
N_REPS = 10

Next we generate simulation data using the set parameters and the dist.sample_blnm function.

alt_allele_counts = dist.sample_blnm(COEFS, MEANS, VARIANCE,
                        N_COUNTS, N_SAMPLES)
n_counts = np.array([N_COUNTS for _ in range(N_SAMPLES)])

Note, that each sample is defined by the pair (alt counts, n counts).

To fit the bln mixture model we initialize the the algorithm by selecting parameters at random. In what follows, we fit the blnm model to the data 10 times, choosig the parameters that result in the highest log likelihood of the data. This is useful because the EM algorithm, in general, will converge to a local optimum. By randomly initializing the algorithm at different locations in parameters space we are, in effect, asking the algorithm to find a set local optima. Our hope is that one of these discovered optima is the global optimum, but we can make no guarantees.

tmp_pars = fit.blnm(alt_allele_counts, n_counts, len(MEANS), disp=False)
best_pars = tmp_pars

print("Iter\tLog Like\tConverge Iters")
for i in range(1, N_REPS):
    print(i, tmp_pars["log_likelihood"], tmp_pars["iterations"], sep="\t")

    tmp_pars = fit.blnm(alt_allele_counts, 
            n_counts, len(MEANS), disp=False)

    if tmp_pars["log_likelihood"] > best_pars["log_likelihood"]:
        best_pars = tmp_pars

print("Best log-likelihood", best_pars["log_likelihood"])

Next we focus our attention to plotting the results. First, we generate the true probability mass function and the empirical mass.

x, empirical_prob = hist(alt_allele_counts)

true_prob = dist.mixture_pmf(x,
                        args.n_counts,
                        COEFS,
                        MEANS,
                        VARIANCE)

Then we use matplotlib to fit.

FIGSIZE = (4, 3.5)
FONTSIZE = 15
AX_POSITION = (0.2, 0.2, 0.75, 0.75)


fig, ax = plt.subplots(1,1,figsize=FIGSIZE)
ax.plot(x, empirical_prob, "o", 
        mfc="none", 
        color="k", 
        label="Histogram samples")
ax.plot(x, true_prob, "-", color="k", label="True Density")

for coef_i, mean_i in zip(best_pars["coefs"], best_pars["means"]):

    prob = coef_i * dist.pmf(x, args.n_counts,
                           mean_i,
                           best_pars["variance"])
    ax.plot(x, prob, "-", label="Inferred mixture")

ax.legend(loc=0)
ax.set_position(AX_POSITION)

ax.set_xlabel("Alternative Counts", fontsize=FONTSIZE)
ax.set_ylabel("Probability", fontsize=FONTSIZE)

fig.savefig("pmf.png")

pmf

blnm's People

Contributors

robert-vogel avatar

Watchers

Daniel Munro avatar  avatar Pejman avatar

blnm's Issues

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.