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 5938909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:45:06+00:00 2026-05-22T15:45:06+00:00

As seen here: http://www.evanmiller.org/how-not-to-sort-by-average-rating.html Here’s the Ruby code itself, implemented in the Statistics2 library:

  • 0

As seen here: http://www.evanmiller.org/how-not-to-sort-by-average-rating.html

Here’s the Ruby code itself, implemented in the Statistics2 library:

# inverse of normal distribution ([2])
# Pr( (-\infty, x] ) = qn -> x
def pnormaldist(qn)
  b = [1.570796288, 0.03706987906, -0.8364353589e-3,
       -0.2250947176e-3, 0.6841218299e-5, 0.5824238515e-5,
       -0.104527497e-5, 0.8360937017e-7, -0.3231081277e-8,
       0.3657763036e-10, 0.6936233982e-12]

  if(qn < 0.0 || 1.0 < qn)
    $stderr.printf("Error : qn <= 0 or qn >= 1  in pnorm()!\n")
    return 0.0;
  end
  qn == 0.5 and return 0.0

  w1 = qn
  qn > 0.5 and w1 = 1.0 - w1
  w3 = -Math.log(4.0 * w1 * (1.0 - w1))
  w1 = b[0]
  1.upto 10 do |i|
    w1 += b[i] * w3**i;
  end
  qn > 0.5 and return Math.sqrt(w1 * w3)
  -Math.sqrt(w1 * w3)
end
  • 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-05-22T15:45:07+00:00Added an answer on May 22, 2026 at 3:45 pm

    This is pretty straightforward to translate:

    module PNormalDist where
    
    pnormaldist :: (Ord a, Floating a) => a -> Either String a
    pnormaldist qn
      | qn < 0 || 1 < qn = Left "Error: qn must be in [0,1]"
      | qn == 0.5        = Right 0.0
      | otherwise        = Right $
          let w3 = negate . log $ 4 * qn * (1 - qn)
              b = [ 1.570796288, 0.03706987906, -0.8364353589e-3, 
                    -0.2250947176e-3, 0.6841218299e-5, 0.5824238515e-5, 
                    -0.104527497e-5, 0.8360937017e-7, -0.3231081277e-8, 
                    0.3657763036e-10, 0.6936233982e-12]
              w1 = sum . zipWith (*) b $ iterate (*w3) 1
          in (signum $ qn - 0.5) * sqrt (w1 * w3)
    

    First off, let’s look at the ruby – it returns a value, but sometimes it prints an error message (when given an improper argument). This isn’t very haskellish, so
    let’s have our return value be Either String a – where we’ll return a Left String with an error message if given an improper argument, and a Right a otherwise.

    Now we check the two cases at the top:

    • qn < 0 || 1 < qn = Left "Error: qn must be in [0,1]" – this is the error condition, when qn is out of range.
    • qn == 0.5 = Right 0.0 – this is the ruby check qn == 0.5 and return * 0.0

    Next up, we define w1 in the ruby code. But we redefine it a few lines later, which isn’t very rubyish. The value that we store in w1 the first time
    is used immediately in the definition of w3, so why don’t we skip storing it in w1? We don’t even need to do the qn > 0.5 and w1 = 1.0 - w1 step, because
    we use the product w1 * (1.0 - w1) in the definition of w3.

    So we skip all that, and move straight to the definition w3 = negate . log $ 4 * qn * (1 - qn).

    Next up is the definition of b, which is a straight lift from the ruby code (ruby’s syntax for an array literal is haskell’s syntax for a list).

    Here’s the most tricky bit – defining the ultimate value of w3. What the ruby code does in

    w1 = b[0]
    1.upto 10 do |i|
      w1 += b[i] * w3**i;
    end
    

    Is what’s called a fold – reducing a set of values (stored in a ruby array) into a single value. We can restate this more functionally (but still in ruby) using Array#reduce:

    w1 = b.zip(0..10).reduce(0) do |accum, (bval,i)|
      accum + bval * w3^i
    end
    

    Note how I pushed b[0] into the loop, using the identity b[0] == b[0] * w3^0.

    Now we could port this directly to haskell, but it’s a bit ugly

    w1 = foldl 0 (\accum (bval,i) -> accum + bval * w3**i) $ zip b [0..10]
    

    Instead, I broke it up into several steps – first off, we don’t really need i, we just need the powers of w3 (starting at w3^0 == 1), so
    let’s calculate those with iterate (*w3) 1.

    Then, rather than zipping those into pairs with the elements of b, we ultimately just need their products, so we can zip them into
    the products of each pair using zipWith (*) b.

    Now our folding function is really easy – we just need to sum up the products, which we can do using sum.

    Lastly, we decide whether to return plus or minus sqrt (w1 * w3), according to whether qn is greater or less than 0.5 (we
    already know it’s not equal). So rather than calculating the square root in two separate locations as in the ruby code,
    I calculated it once, and multiplied it by +1 or -1 according to the sign of qn - 0.5 (signum just returns the sign of a value).

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

Sidebar

Related Questions

problem can be seen here: http://www.studioimbrue.com/beta The code: $(document).ready(function(){ $('div.caption').hide(); $('.captions ul li img').hover(function(){
As seen here: http://www.tsqltutorials.com/pivot.php What if I don't have a column I can perform
Active site can be seen here: http://www.studioimbrue.com/index2.php There are currently two small problems with
Im using the PredicateBuilder as seen here http://www.albahari.com/nutshell/predicatebuilder.aspx , everything works great, and now
I have a pagination script, which can be seen here: http://www.automotori6282.tk/phpsandbox/ This is the
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}
I have a five section accordion list, as seen at http://www.battletech-live.net , which draws
I've seen this questions here . I'm wondering if there exists an official name
I've seen several question on here about exceptions , and some of them hint
I've seen various options for URL rewriting here on Stack Overflow, and other places

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.