## Re: [xsl] Is this the best way to compute a sigmoid function?

 Subject: Re: [xsl] Is this the best way to compute a sigmoid function? From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" Date: Sat, 2 May 2020 19:27:01 -0000
```Hi Roger,

In XPath 3 one can use the standard exp() function, see:
https://www.w3.org/TR/xpath-functions-30/#func-math-exp

As for the accuracy, here is what I get using the "Sigmoid calculator (
https://keisan.casio.com/exec/system/15157249643325):

0.6456563062257954529091

With FXSL out of the box I get this result:

0.645656306189717261802

and these two results have the same digits up to the 10th digit. Not bad,
isn't it? :)

And this is with the default precision!

Now, if someone wants higher precision, they can increase the default one,
which is:

<xsl:param name="pEps" select=".00000001"/>

and this is exactly what I did. If you modify your code to this:

<xsl:function name="f:e" as="xs:decimal">
<xsl:param name="x" as="xs:decimal" />

<xsl:call-template name="exp">
<xsl:with-param name="pX" select="\$x" />
<xsl:with-param name="pEps" select=".0000000000001"/>
</xsl:call-template>

the result is now:

0.6456563062257953085797423    Compared to the "Sigmoid function calculator
result:
0.6456563062257954529091

we see that the first digit mismatch is now in the 16th digit. Is this
enough for you? If not you can still increase the precision when calling
f:exp.
I got even this:

0.645656306225795453040607452  -- 17 digits are identical

My guess is that further increases of the precision will not work, as FXSL
uses floating point type numbers / arithmetic.

However, anyone who needs it can try to replace these with decimal types.

I read somewhere that using precision for O  (pi) greater that 5 decimal
digits is impractical -- it is only 0.000084 percent off the "real value"

And Susan Gomez, manager of the International Space Station Guidance
Navigation and Control (GNC) subsystem for NASA, said that calculations
involving pi use 15 digits for GNC code and 16 for the Space Integrated
Global Positioning System/Inertial Navigation System (SIGI). SIGI is the
program that controls and stabilizes spacecraft during missions  (
https://blogs.scientificamerican.com/observations/how-much-pi-do-you-need/)

So, it appears that NASA could use FXSL without problems :)

Cheers,
Dimitre

On Sat, May 2, 2020 at 10:55 AM Costello, Roger L. costello@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hi Folks,
>
> I have a variable \$x that has a value such as 0.6
>
> I want to compute the result of evaluating this sigmoid function:
>
>      1
> ----------------
> (1 + e**-x)
>
> That is, compute 1 divided by (1 + e raised to the -x power)
>
> I am using the exp template in Dimitre's FXSL library.
>
> [Dimitre, incredible library]
>
> I created a wrapper function that simply calls Dimitre's exp template:
>
> <xsl:function name="f:e" as="xs:decimal">
>     <xsl:param name="x" as="xs:decimal" />
>
>     <xsl:call-template name="exp">
>         <xsl:with-param name="pX" select="\$x" />
>     </xsl:call-template>
> </xsl:function>
>
> Then I computed the sigmoid function this way:
>
> <xsl:variable name="e-to-the-minus-x-power" select="f:e(-\$x)"
> as="xs:decimal" />
> <xsl:value-of select="1 div (1 + \$e-to-the-minus-x-power)"/>
>
> When \$x has the value 0.6 I get this result: 0.645656306189717285
>
> Do you see any errors in how I am computing the sigmoid function?
>
> Is there a better way to do it? That is, one that returns more accurate
> results? (However, it already appears to be mighty accurate -- look at all
> those digits to the right of the decimal point) Notice that for the
> variable \$e-to-the-minus-x-power I specified it this way: as="xs:decimal".
> Should I have specified it this way: as="xs:float" instead? Would that
> yield better/more-accurate/more-reliable/more-whatever results?
>
> Any suggestions you might have would be greatly appreciated.
>
> /Roger
>
>

--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

```