From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matt Armstrong Newsgroups: gmane.emacs.bugs Subject: bug#58929: 29.0.50; Calc: finding roots utpn doesn't work as advertised Date: Tue, 01 Nov 2022 15:00:38 -0700 Message-ID: <87k04es5bt.fsf@rfc20.org> References: <8773828B-3A5F-4B15-8786-91F6FCB6D2E2@acm.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19174"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 58929@debbugs.gnu.org To: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Nov 01 23:01:21 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1opzJx-0004mw-3p for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 01 Nov 2022 23:01:21 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opzJh-0003o2-9K; Tue, 01 Nov 2022 18:01:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opzJf-0003ne-DV for bug-gnu-emacs@gnu.org; Tue, 01 Nov 2022 18:01:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1opzJe-0003aX-Mq for bug-gnu-emacs@gnu.org; Tue, 01 Nov 2022 18:01:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1opzJe-0002rv-1K for bug-gnu-emacs@gnu.org; Tue, 01 Nov 2022 18:01:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Matt Armstrong Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 01 Nov 2022 22:01:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58929 X-GNU-PR-Package: emacs Original-Received: via spool by 58929-submit@debbugs.gnu.org id=B58929.166734005311003 (code B ref 58929); Tue, 01 Nov 2022 22:01:01 +0000 Original-Received: (at 58929) by debbugs.gnu.org; 1 Nov 2022 22:00:53 +0000 Original-Received: from localhost ([127.0.0.1]:44356 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1opzJV-0002rP-7I for submit@debbugs.gnu.org; Tue, 01 Nov 2022 18:00:53 -0400 Original-Received: from relay6-d.mail.gandi.net ([217.70.183.198]:42097) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1opzJR-0002r7-VO for 58929@debbugs.gnu.org; Tue, 01 Nov 2022 18:00:51 -0400 Original-Received: (Authenticated sender: matt@rfc20.org) by mail.gandi.net (Postfix) with ESMTPSA id 58C50C0004; Tue, 1 Nov 2022 22:00:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rfc20.org; s=gm1; t=1667340041; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=DM1d21dzbEsTqYy1ImjhVBWjR9Wkh3+PH38MdbHNne4=; b=c12GDDgxL0XwlDaxqJZaY8CAh3fWga2cNeTGn5PgPTjanB4KhFRbvtZAzBcXonMjgoekwH N76fqOi2ZdEd1y334Xzc7ESTUTmxwcQb27Y1kjm0PkOcZAh2Gun67KXjRTdDUSPg2wudjH E5uf9M37TLqpYGHKhYDFtVLB9FxceuNrV2P1j09gV/PgZCGDvdCbEGEqhXInQvcOgSDcg3 KSBA2Fe6BvBwwNtA16bthgu6sU0w2gv6IYihR+rPRMS5Js0+NSfS3OS30qkXH6Vr7LT2VL 3xBKsRI/c4OiN6O3kn7XtQPZ3wgYYiGXfo1xyM9jvpTFud2Am6sKlDZy+bPvoA== Original-Received: from matt by naz with local (Exim 4.96) (envelope-from ) id 1opzJG-000H2O-1Z; Tue, 01 Nov 2022 15:00:38 -0700 In-Reply-To: <8773828B-3A5F-4B15-8786-91F6FCB6D2E2@acm.org> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: "bug-gnu-emacs" Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:246809 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mattias Engdeg=C3=A5rd writes: > Obviously the claim to guarantee a solution for "any initial guess" is > fanciful. Yes. See attached patch for what I wish the docs had said all along (would have saved me a lot of time). > Unfortunately straight Newton won't converge for this function (erf, > essentially) if you stray too far from the origin (ie, the mean) so I > suggest you always use that as a starting guess no matter what point > you are trying to find the inverse for. > > Another way is to use an interval as starting guess instead of a > single point; that seems to make Calc switch tactics and persevere. I wonder if the implementation can be improved. https://www.gnu.org/software/emacs/manual/html_node/calc/Root-Finding.html suggests that there are some cases where calc will switch over to the bisection method. Perhaps the heuristics can be tweaked? > One thing that can happen is running out of stack, because many of the > implementations like math-newton-root are recursive. This should be > easy to remedy as they all seem to be tail calls. Good to know. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0002-In-calc.texi-provide-guidance-for-taking-the-inverse.patch >From ba68ca5671f9772994bc5c0b2b0b9216a750b692 Mon Sep 17 00:00:00 2001 From: Matt Armstrong Date: Tue, 1 Nov 2022 14:51:36 -0700 Subject: [PATCH 2/2] In calc.texi provide guidance for taking the inverse of PDFs. * doc/misc/calc.texi (Probability Distribution Functions): Stop claiming that the 'a R' function will "always work" for any guess. Explain the problem with the PDF functions being very flat. Provide recommended guess values for each function. bug#58929 --- doc/misc/calc.texi | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/doc/misc/calc.texi b/doc/misc/calc.texi index 89a340e7343..e6b416a447a 100644 --- a/doc/misc/calc.texi +++ b/doc/misc/calc.texi @@ -19456,9 +19456,39 @@ Probability Distribution Functions While Calc does not provide inverses of the probability distribution functions, the @kbd{a R} command can be used to solve for the inverse. -Since the distribution functions are monotonic, @kbd{a R} is guaranteed -to be able to find a solution given any initial guess. -@xref{Numerical Solutions}. +@xref{Root Finding}. + +The distribution functions, while monotonic, are also be very flat, +which can cause problems for the Newton's root-finding algorithm used +by @kbd{a R}. The problem can be avoided by providing a good starting +guess. Prefer guesses that occur where the slope of the plotted +function is not flat. Alternatively, use an interval range for the +guess. The table below provides a recommendation for each of the +probability distribution functions. + +@multitable @columnfractions .25 .25 .25 +@headitem Function +@tab When solving for +@tab Recommended initial guess +@item @samp{utpb(x,n,p)} +@tab @expr{x} +@tab @code{0.5} +@item @samp{utpc(x,c)} +@tab @expr{x} +@tab @code{1.0} +@item @samp{utpf(F,v1,v2)} +@tab @expr{F} +@tab @code{1.0} +@item @samp{utpn(x,m,s)} +@tab @expr{x} +@tab @expr{m} +@item @samp{utpp(n,x)} +@tab @expr{n} +@tab @expr{x} +@item @samp{utpt(t,v)} +@tab @expr{t} +@tab @code{[0..1e4]} +@end multitable @node Matrix Functions @chapter Vector/Matrix Functions -- 2.35.1 --=-=-=--