Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:05:49+00:00 2026-06-04T19:05:49+00:00

I’m trying to generate random masses for hypothetical planets in Haskell. I want to

  • 0

I’m trying to generate random masses for hypothetical planets in Haskell. I want to produce these masses by sampling a bi-modal distribution (ideally the superposition of two normal distributions: one corresponding to small planets and one corresponding to gas giants). I’ve looked at the statistics package, which provides the quantile function, which can turn a uniformly distributed Double into a Double on a number of distributions. But there doesn’t seem to be any support for composing distributions.

This particular case could be hacked around by picking one distribution or the other to sample before-hand, but I’d like to do it with a single distribution, especially since I might need to tweak the overall distribution later. Eventually I might replace the normal distribution with real data from sky surveys.

I’m considering implementing rejection sampling myself, which can handle arbitrary distributions fairly simply, but it seems rather inefficient, and it certainly wouldn’t be a good idea to implement it if a solution exists already as a library.

Is there a Haskell library that supports sampling from composed or explicitly specified distributions? Or an existing Haskell implementation of rejection sampling? Alternatively, is there an explicit formula for the inverse of the CDF of the sum of two normal distributions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:05:50+00:00Added an answer on June 4, 2026 at 7:05 pm

    In the case of a simple mixture of distributions, you can get an efficient sampler via the ‘hack’ you first mentioned:

    This particular case could be hacked around by picking one distribution or the other to sample before-hand, but I’d like to do it with a single distribution, especially since I might need to tweak the overall distribution later.

    This is actually a case of Gibbs sampling, which is very prevalent in statistics. It’s very flexible, and if you know the number of mixtures you’re using, it will probably be hard to beat. Choose one individual distribution from the entire ensemble to sample from, and then sample from that conditional distribution. Rinse and repeat.

    Here’s a simple, unoptimized Haskell implementation for a mixture-of-Gaussians Gibbs sampler. It’s pretty basic, but you get the idea:

    import System.Random
    import Control.Monad.State
    
    type ModeList = [(Double, Double)]                 -- A list of mean/stdev pairs, for each mode.
    
    -- Generate a Gaussian (0, 1) variate.
    boxMuller :: StdGen -> (Double, StdGen)
    boxMuller gen = (sqrt (-2 * log u1) * cos (2 * pi * u2), gen'')
        where (u1, gen')  = randomR (0, 1) gen 
              (u2, gen'') = randomR (0, 1) gen'
    
    sampler :: ModeList -> State StdGen Double
    sampler modeInfo = do
        gen <- get
        let n           = length modeInfo
            (z0, g0)    = boxMuller gen
            (c,  g1)    = randomR (0, n - 1) g0        -- Sample from the components.
            (cmu, csig) = modeInfo !! c                
        put g1
        return $ cmu + csig * z0                       -- Sample from the conditional distribution.
    

    Here’s a example run: sampling 100 times from a one-dimensional mixture of two Gaussians. The modes are at x = -3 and x = 2.5, and each mixture component has its own separate variance. You could add as many modes as you want here.

    main = do
    let gen      = mkStdGen 42
        modeInfo = [(2.5, 1.0), (-3, 1.5)]
        samples     = (`evalState` gen) . replicateM 100 $ sampler modeInfo
    print samples
    

    Here’s a smoothed density plot of those 100 samples (using R and ggplot2):

    a mixture of gaussians

    A more general purpose algorithm would be a rejection or importance sampler, and in the case of more complicated distributions you’re probably going to want to hand-roll an appropriate MCMC routine. Here is a good introduction to Monte Carlo and MCMC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.