sigmoid: Sigmoid function and its derivatives

Description Usage Arguments Value Author(s) Examples

View source: R/sigmoid.R

Description

Calculates the sigmoid function and its derivatives.

Usage

1

Arguments

y

a numeric vector.

deriv

if TRUE alse the first three derivatives of the sigmoid function will be computed.

Value

If deriv==FALSE, it returns a numeric vector equal to 1/(1+exp(-x)). If deriv==TRUE it returns a list where the slot $D0 contains 1/(1+exp(-x)), while $D1, $D2 and $D3 contain its first three derivatives.

Author(s)

Matteo Fasiolo <matteo.fasiolo@gmail.com>.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
library(qgam)
set.seed(90)
h <- 1e-6
p <- rnorm(1e4, 0, 1e6)
sigmoid(p[1:50]) - 1/(1+exp(-p[1:50]))

##### Testing sigmoid derivatives
e1 <- abs((sigmoid(p+h) - sigmoid(p-h)) / (2*h) - sigmoid(p, TRUE)[["D1"]]) / (2*h)
e2 <- abs((sigmoid(p+h, TRUE)$D1 - sigmoid(p-h, TRUE)$D1) / 
      (2*h) - sigmoid(p, TRUE)[["D2"]]) / (2*h)
e3 <- abs((sigmoid(p+h, TRUE)$D2 - sigmoid(p-h, TRUE)$D2) / 
      (2*h) - sigmoid(p, TRUE)[["D3"]]) / (2*h)

if( any(c(e1, e2, e3) > 1) ) stop("Sigmoid derivatives are not estimated accurately")

qgam documentation built on Nov. 23, 2021, 1:07 a.m.