From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: Normal distribution random numbers Date: Sat, 30 May 2020 22:42:43 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="110131"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Icedove/60.9.0 To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat May 30 22:43:03 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jf8Jo-000SVc-BE for guile-user@m.gmane-mx.org; Sat, 30 May 2020 22:43:00 +0200 Original-Received: from localhost ([::1]:48608 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jf8Jn-0005wI-DL for guile-user@m.gmane-mx.org; Sat, 30 May 2020 16:42:59 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58966) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jf8Jd-0005wA-At for guile-user@gnu.org; Sat, 30 May 2020 16:42:49 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:44003) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jf8Jb-00056X-Jj for guile-user@gnu.org; Sat, 30 May 2020 16:42:48 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id 3BD0F2400FC for ; Sat, 30 May 2020 22:42:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1590871365; bh=TvaIi04SfjYTp4WU33PwQ1apqb+V/zPhR/jhOwPVw9Y=; h=Subject:To:From:Date:From; b=KWezoCMZdf41QyeXT2fuASa1xrgVPW+Aeb/qRUxUInLMKnZ36gY+TxDCT9Cb7Afuo FkVfk/xF4yUpBwUz19m+J4o8EKenidnHW6Ud4MikV7eExv8kZJd8jM44feqE5akVJ+ iXE32/Y8bptgEivuf6bUeJsVJNnwvPbt6zRScyrxXUhmqH02XYzf/IJfl0aPytRqrh SDt1Pm1sRDQVwtLcayYZdrvGGq4ba6W02AkIdPBohimCfVsXnlRqmFRmXqG7kMZ+Sy O4+livnFNQJ9G8y+yrxISiivk/zM60G1wfMhlV2vWLsEbaDkU/r2e9NvvmgDsvss6s 5w/9xLswzLNcA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 49ZD044Q2Xz6tmN for ; Sat, 30 May 2020 22:42:44 +0200 (CEST) In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=185.67.36.66; envelope-from=zelphirkaltstahl@posteo.de; helo=mout02.posteo.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/30 16:21:58 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16538 Archived-At: I just realized, that I did not check what Guile implements as non-SRFIs. I found: https://www.gnu.org/software/guile/manual/html_node/Random.html which has `random:normal`! I should have checked that first. Still good to know, what a can of worms normal distribution implementation can be. On 30.05.20 22:21, Zelphir Kaltstahl wrote: > Hi Guile Users! > > I recently wrote a little program involving lots of uniformly > distributed random integers. For that I used SRFI-27 and it works fine. > > Then I thought: How would I get normal distributed random numbers? I > don't have a project or program in mind for this, but it struck me, that > I do not know, how to get a normal distribution from a uniform > distribution. So I dug into the matter … > > Turns out the math is not really my friend: > > * https://stackoverflow.com/a/3265174 – OK, if that's true, then don't > use Box-Muller-Transform > * https://stackoverflow.com/a/86885 – The what? I need to somehow > inverse the Gaussian distribution to get a function to calculate normal > distributed values from uniformly distributed values? Something like > that. Safe to say it is above my current math skills. > * The wiki page also does not help me much: > https://en.wikipedia.org/wiki/Inverse_transform_sampling Seems too > complicated. > > So I thought: "OK, maybe I can simply copy, how other languages > implement it!" The wiki page mentions, that R actually makes use of the > inverse thingy. So I set out to look at R source code: > > * https://github.com/wch/r-source/blob/master/src/nmath/rnorm.c – OK, > looks simple enough … Lets see what `norm_rand` is … > * https://github.com/wch/r-source/blob/master/src/nmath/snorm.c#L62 – > yeah … well … I'm not gonna implement _that_ pile of … Just look at the > lines > https://github.com/wch/r-source/blob/master/src/nmath/snorm.c#L135-L196 > what a mess! Not a single comment to help understanding in it. Such a > disappointment. > * Python also seems to only use an approximation with magic constants: > https://github.com/python/cpython/blob/3.8/Lib/random.py#L443 > > So it seems, that there is no easy way to implement it properly with > correct tails to the left and right side of the distribution, something > clean and not made with mathematical traps built-in. Or is there? > > I found a post about using 2 normal distributions to do > Box-Muller-transform: > https://www.alanzucconi.com/2015/09/16/how-to-sample-from-a-gaussian-distribution/ > > However, it seems to require a uniform float not integer and it is the > Box-Muller-transform, which is said to clamp between -6 and 6 according > to the people writing the answers on stackoverflow. > > So my question is: Is there a good implementation in the Guile universe > already? (Or a simple way to implement it?) I don't really need it right > now, but I think this thing could be an obstacle for many people without > serious math knowledge and it would be good to know, where to find it, > should one have need for normal distributed random numbers. > > Regards, > Zelphir > >