@@ -25,47 +25,47 @@ tfmult <- function(x,y) tf$math$multiply(x,y)
2525# ' with parameters (and corresponding inverse link function in brackets):
2626# '
2727# ' \itemize{
28- # ' \item{"normal": }{normal distribution with location (identity), scale (exp)}
29- # ' \item{"bernoulli": }{bernoulli distribution with logits (identity)}
30- # ' \item{"bernoulli_prob": }{bernoulli distribution with probabilities (sigmoid)}
31- # ' \item{"beta": }{beta with concentration 1 = alpha (exp) and concentration
28+ # ' \item{\code{ "normal": } }{normal distribution with location (identity), scale (exp)}
29+ # ' \item{\code{ "bernoulli": } }{bernoulli distribution with logits (identity)}
30+ # ' \item{\code{ "bernoulli_prob": } }{bernoulli distribution with probabilities (sigmoid)}
31+ # ' \item{\code{ "beta": } }{beta with concentration 1 = alpha (exp) and concentration
3232# ' 0 = beta (exp)}
33- # ' \item{"betar": }{beta with mean (sigmoid) and scale (sigmoid)}
34- # ' \item{"cauchy": }{location (identity), scale (exp)}
35- # ' \item{"chi2": }{cauchy with df (exp)}
36- # ' \item{"chi": }{cauchy with df (exp)}
37- # ' \item{"exponential": }{exponential with lambda (exp)}
38- # ' \item{"gamma": }{gamma with concentration (exp) and rate (exp)}
39- # ' \item{"gammar": }{gamma with location (exp) and scale (exp), following
33+ # ' \item{\code{ "betar": } }{beta with mean (sigmoid) and scale (sigmoid)}
34+ # ' \item{\code{ "cauchy": } }{location (identity), scale (exp)}
35+ # ' \item{\code{ "chi2": } }{cauchy with df (exp)}
36+ # ' \item{\code{ "chi": } }{cauchy with df (exp)}
37+ # ' \item{\code{ "exponential": } }{exponential with lambda (exp)}
38+ # ' \item{\code{ "gamma": } }{gamma with concentration (exp) and rate (exp)}
39+ # ' \item{\code{ "gammar": } }{gamma with location (exp) and scale (exp), following
4040# ' \code{gamlss.dist::GA}, which implies that the expectation is the location,
4141# ' and the variance of the distribution is the \code{location^2 scale^2}}
42- # ' \item{"gumbel": }{gumbel with location (identity), scale (exp)}
43- # ' \item{"half_cauchy": }{half cauchy with location (identity), scale (exp)}
44- # ' \item{"half_normal": }{half normal with scale (exp)}
45- # ' \item{"horseshoe": }{horseshoe with scale (exp)}
46- # ' \item{"inverse_gamma": }{inverse gamma with concentation (exp) and rate (exp)}
47- # ' \item{"inverse_gamma_ls": }{inverse gamma with location (exp) and variance (1/exp)}
48- # ' \item{"inverse_gaussian": }{inverse Gaussian with location (exp) and concentation
42+ # ' \item{\code{ "gumbel": } }{gumbel with location (identity), scale (exp)}
43+ # ' \item{\code{ "half_cauchy": } }{half cauchy with location (identity), scale (exp)}
44+ # ' \item{\code{ "half_normal": } }{half normal with scale (exp)}
45+ # ' \item{\code{ "horseshoe": } }{horseshoe with scale (exp)}
46+ # ' \item{\code{ "inverse_gamma": } }{inverse gamma with concentation (exp) and rate (exp)}
47+ # ' \item{\code{ "inverse_gamma_ls": } }{inverse gamma with location (exp) and variance (1/exp)}
48+ # ' \item{\code{ "inverse_gaussian": } }{inverse Gaussian with location (exp) and concentation
4949# ' (exp)}
50- # ' \item{"laplace": }{Laplace with location (identity) and scale (exp)}
51- # ' \item{"log_normal": }{Log-normal with location (identity) and scale (exp) of
50+ # ' \item{\code{ "laplace": } }{Laplace with location (identity) and scale (exp)}
51+ # ' \item{\code{ "log_normal": } }{Log-normal with location (identity) and scale (exp) of
5252# ' underlying normal distribution}
53- # ' \item{"logistic": }{logistic with location (identity) and scale (exp)}
54- # ' \item{"negbinom": }{neg. binomial with count (exp) and prob (sigmoid)}
55- # ' \item{"negbinom_ls": }{neg. binomail with mean (exp) and clutter factor (exp)}
56- # ' \item{"pareto": }{Pareto with concentration (exp) and scale (1/exp)}
57- # ' \item{"pareto_ls": }{Pareto location scale version with mean (exp)
53+ # ' \item{\code{ "logistic": } }{logistic with location (identity) and scale (exp)}
54+ # ' \item{\code{ "negbinom": } }{neg. binomial with count (exp) and prob (sigmoid)}
55+ # ' \item{\code{ "negbinom_ls": } }{neg. binomail with mean (exp) and clutter factor (exp)}
56+ # ' \item{\code{ "pareto": } }{Pareto with concentration (exp) and scale (1/exp)}
57+ # ' \item{\code{ "pareto_ls": } }{Pareto location scale version with mean (exp)
5858# ' and scale (exp), which corresponds to a Pareto distribution with parameters scale = mean
5959# ' and concentration = 1/sigma, where sigma is the scale in the pareto_ls version}
60- # ' \item{"poisson": }{poisson with rate (exp)}
61- # ' \item{"poisson_lograte": }{poisson with lograte (identity))}
62- # ' \item{"student_t": }{Student's t with df (exp)}
63- # ' \item{"student_t_ls": }{Student's t with df (exp), location (identity) and
60+ # ' \item{\code{ "poisson": } }{poisson with rate (exp)}
61+ # ' \item{\code{ "poisson_lograte": } }{poisson with lograte (identity))}
62+ # ' \item{\code{ "student_t": } }{Student's t with df (exp)}
63+ # ' \item{\code{ "student_t_ls": } }{Student's t with df (exp), location (identity) and
6464# ' scale (exp)}
65- # ' \item{"uniform": }{uniform with upper and lower (both identity)}
66- # ' \item{"zinb": }{Zero-inflated negative binomial with mean (exp),
65+ # ' \item{\code{ "uniform": } }{uniform with upper and lower (both identity)}
66+ # ' \item{\code{ "zinb": } }{Zero-inflated negative binomial with mean (exp),
6767# ' variance (exp) and prob (sigmoid)}
68- # ' \item{"zip": }{Zero-inflated poisson distribution with mean (exp) and prob (sigmoid)}
68+ # ' \item{\code{ "zip": } }{Zero-inflated poisson distribution with mean (exp) and prob (sigmoid)}
6969# ' }
7070# ' @param add_const small positive constant to stabilize calculations
7171# ' @param trafo_list list of transformations for each distribution parameter.
@@ -281,9 +281,9 @@ family_to_tfd <- function(family)
281281 negbinom_ls = tfd_negative_binomial_ls ,
282282 pareto = tfd_pareto ,
283283 pareto_ls = tfd_pareto ,
284- poisson = tfd_poisson ,
284+ poisson = tfd_poisson_fixed ,
285285 poisson_lograte = function (log_rate )
286- tfd_poisson (log_rate = log_rate ),
286+ tfd_poisson_fixed (log_rate = log_rate ),
287287 student_t = function (x )
288288 tfd_student_t(df = x ,loc = 0 ,scale = 1 ),
289289 student_t_ls = tfd_student_t ,
@@ -472,6 +472,15 @@ family_trafo_funs_special <- function(family, add_const = 1e-8)
472472
473473}
474474
475+ tfd_poisson_fixed <- function (rate = NULL , log_rate = NULL , interpolate_nondiscrete = TRUE ,
476+ validate_args = FALSE , allow_nan_stats = TRUE , name = " Poisson" )
477+ {
478+ args <- list (rate = rate , log_rate = log_rate ,
479+ validate_args = validate_args , allow_nan_stats = allow_nan_stats ,
480+ name = name )
481+ do.call(tfp $ distributions $ Poisson , args )
482+ }
483+
475484# ' Implementation of a zero-inflated poisson distribution for TFP
476485# '
477486# ' @param lambda scalar value for rate of poisson distribution
@@ -483,7 +492,7 @@ tfd_zip <- function(lambda, probs)
483492 return (
484493 tfd_mixture(cat = tfd_categorical(probs = probs ),
485494 components =
486- list (tfd_poisson (rate = lambda ),
495+ list (tfd_poisson_fixed (rate = lambda ),
487496 tfd_deterministic(loc = lambda * 0L )
488497 ),
489498 name = " zip" )
@@ -543,56 +552,60 @@ tfd_mvr <- function(loc, scale,
543552
544553}
545554
546- # Implementation of a distribution-like layer for (Quasi-)Tweedie
547- tfd_tweedie <- function (loc , phi , p = 1.5 , quasi = FALSE ,
548- validate_args = FALSE ,
549- allow_nan_stats = TRUE ,
550- name = " Tweedie" )
551- {
552-
553- args <- list (
554- loc = loc ,
555- scale = phi ,
556- var_power = p ,
557- quasi = quasi ,
558- validate_args = validate_args ,
559- allow_nan_stats = allow_nan_stats ,
560- name = name
561- )
562-
563- python_path <- system.file(" python" , package = " deepregression" )
564- distributions <- reticulate :: import_from_path(" distributions" , path = python_path )
565-
566- return (do.call(distributions $ Tweedie , args ))
567-
568- }
569-
570- # ' tfd_distfun for (Quasi-)Tweedie to allow for flexible p
571- # ' @param p integer; defines distribution
572- # ' @param quasi logical; whether to use quasi-likelihood or deviance resids
573- # ' @param output_dim integer; currently only univariate responses supported
574- # ' @export
575555# '
576- tweedie <- function (p , quasi = FALSE , output_dim = 1L )
577- {
578-
579- tfd_dist <- function (l , s ) tfd_tweedie(loc = l , phi = s , p = p , quasi = quasi )
580- trafo_list <- list (function (x ) tf $ add(1e-8 , tfe(x )),
581- function (x ) tf $ add(1e-8 , tfe(x )))
582- dist_dim <- 2L
583- ret_fun <- function (x )
584- do.call(tfd_dist ,
585- lapply(1 : (x $ shape [[2 ]]/ output_dim ),
586- function (i )
587- trafo_list [[i ]](
588- tf_stride_cols(x ,(i - 1L )* output_dim + 1L ,
589- (i - 1L )* output_dim + output_dim )))
590- )
591- attr(ret_fun , " nrparams_dist" ) <- 2L
592-
593- return (ret_fun )
594-
595- }
556+ # ' # Implementation of a distribution-like layer for (Quasi-)Tweedie
557+ # ' tfd_tweedie <- function(loc, phi, p = 1.5, quasi = FALSE,
558+ # ' validate_args = FALSE,
559+ # ' allow_nan_stats = TRUE,
560+ # ' name = "Tweedie")
561+ # ' {
562+ # '
563+ # ' args <- list(
564+ # ' loc = loc,
565+ # ' scale = phi,
566+ # ' var_power = p,
567+ # ' quasi = quasi,
568+ # ' validate_args = validate_args,
569+ # ' allow_nan_stats = allow_nan_stats,
570+ # ' name = name
571+ # ' )
572+ # '
573+ # ' python_path <- system.file("python", package = "deepregression")
574+ # ' distributions <- reticulate::import_from_path("distributions", path = python_path)
575+ # '
576+ # ' return(do.call(distributions$Tweedie, args))
577+ # '
578+ # ' }
579+ # '
580+ # ' #' tfd_distfun for (Quasi-)Tweedie to allow for flexible p
581+ # ' #' @param p integer; defines distribution
582+ # ' #' @param quasi logical; whether to use quasi-likelihood or deviance resids
583+ # ' #' @param output_dim integer; currently only univariate responses supported
584+ # ' #' @export
585+ # ' #'
586+ # ' tweedie <- function(p, quasi = FALSE, output_dim = 1L,
587+ # ' linkfun_mean = function(x) tf$add(1e-8, tf$math$exp(x)),
588+ # ' linkfun_phi = function(x) tf$add(1e-8, tf$math$exp(x)))
589+ # ' {
590+ # '
591+ # ' tfd_dist <- function(l, s) tfd_tweedie(loc = l, phi = s, p = p, quasi = quasi)
592+ # ' trafo_list <- list(linkfun_mean, linkfun_phi)
593+ # ' dist_dim <- 2L
594+ # ' ret_fun <- function(x)
595+ # ' do.call(tfd_dist,
596+ # ' lapply(1:(x$shape[[2]]/output_dim),
597+ # ' function(i)
598+ # ' trafo_list[[i]](
599+ # ' tf_stride_cols(x,(i-1L)*output_dim+1L,
600+ # ' (i-1L)*output_dim+output_dim)))
601+ # ' )
602+ # ' attr(ret_fun, "nrparams_dist") <- 2L
603+ # '
604+ # ' return(ret_fun)
605+ # '
606+ # ' }
607+ # '
608+
596609
597610# ' For using mean squared error via TFP
598611# '
0 commit comments