Skip to content

Commit e6e7d7b

Browse files
author
OVVO-Financial
committed
NNS 11.2 Beta
1 parent e9ea38f commit e6e7d7b

File tree

6 files changed

+82
-20
lines changed

6 files changed

+82
-20
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: NNS
22
Type: Package
33
Title: Nonlinear Nonparametric Statistics
44
Version: 11.2
5-
Date: 2025-03-17
5+
Date: 2025-03-23
66
Authors@R: c(
77
person("Fred", "Viole", role=c("aut","cre"), email="[email protected]"),
88
person("Roberto", "Spadim", role=c("ctb"))

NNS_11.2.tar.gz

1.13 KB
Binary file not shown.

NNS_11.2.zip

2.66 KB
Binary file not shown.

R/Central_tendencies.R

+54-12
Original file line numberDiff line numberDiff line change
@@ -128,26 +128,68 @@ NNS.gravity <- function (x, discrete = FALSE)
128128

129129
#' NNS rescale
130130
#'
131-
#' Rescale min-max scaling output between two numbers.
131+
#' Rescale a vector using either min-max scaling or risk-neutral adjustment.
132132
#'
133-
#' @param x vector of data.
134-
#' @param a numeric; lower limit.
135-
#' @param b numeric; upper limit.
136-
#' @return Returns a rescaled distribution within provided limits.
133+
#' @param x numeric vector; data to rescale (e.g., terminal prices for risk-neutral method).
134+
#' @param a numeric; defines the scaling target:
135+
#' - For \code{method = "minmax"}: the lower limit of the output range (e.g., 5 to scale to [5, b]).
136+
#' - For \code{method = "riskneutral"}: the initial price \( S_0 \) (must be positive, e.g., 100), used to set the target mean.
137+
#' @param b numeric; defines the scaling range or rate:
138+
#' - For \code{method = "minmax"}: the upper limit of the output range (e.g., 10 to scale to [a, 10]).
139+
#' - For \code{method = "riskneutral"}: the risk-free rate \( r \) (e.g., 0.05), used with \( T \) to adjust the mean.
140+
#' @param method character; scaling method: \code{"minmax"} (default) for min-max scaling, or \code{"riskneutral"} for risk-neutral adjustment.
141+
#' @param T numeric; time to maturity in years (required for \code{method = "riskneutral"}, ignored otherwise; e.g., 1). Default is NULL.
142+
#' @param type character; for \code{method = "riskneutral"}: \code{"Terminal"} (default, mean = \( S_0 e^{r T} \)) or \code{"Discounted"} (mean = \( S_0 \)).
143+
#' @return Returns a rescaled distribution:
144+
#' - For \code{"minmax"}: values scaled linearly to the range \code{[a, b]}.
145+
#' - For \code{"riskneutral"}: values scaled multiplicatively to a risk-neutral mean (\( S_0 e^{r T} \) if \code{type = "Terminal"}, or \( S_0 \) if \code{type = "Discounted"}).
137146
#' @author Fred Viole, OVVO Financial Systems
138147
#' @examples
139148
#' \dontrun{
140149
#' set.seed(123)
150+
#' # Min-max scaling: a = lower limit, b = upper limit
141151
#' x <- rnorm(100)
142-
#' NNS.rescale(x, 5, 10)
152+
#' NNS.rescale(x, a = 5, b = 10, method = "minmax") # Scales to [5, 10]
153+
#'
154+
#' # Risk-neutral scaling (Terminal): a = S_0, b = r # Mean ≈ 105.13
155+
#' prices <- 100 * exp(cumsum(rnorm(100, 0.001, 0.02)))
156+
#' NNS.rescale(prices, a = 100, b = 0.05, method = "riskneutral", T = 1, type = "Terminal")
157+
#'
158+
#' # Risk-neutral scaling (Discounted): a = S_0, b = r # Mean ≈ 100
159+
#' NNS.rescale(prices, a = 100, b = 0.05, method = "riskneutral", T = 1, type = "Discounted")
143160
#' }
144161
#' @export
145162

146-
147-
NNS.rescale <- function (x, a, b) {
163+
NNS.rescale <- function(x, a, b, method = "minmax", T = NULL, type = "Terminal") {
148164
x <- as.numeric(x)
149-
output <- a + (b - a) * (x - min(x))/(max(x) - min(x))
165+
method <- tolower(method)
166+
type <- tolower(type)
167+
168+
if (method == "minmax") {
169+
# Original min-max scaling
170+
if (max(x) == min(x)) stop("Cannot rescale: max(x) equals min(x)")
171+
output <- a + (b - a) * (x - min(x)) / (max(x) - min(x))
172+
} else if (method == "riskneutral") {
173+
# Risk-neutral scaling
174+
if (is.null(T)) stop("T (time to maturity) must be provided for riskneutral method")
175+
if (a <= 0) stop("S_0 (a) must be positive for riskneutral method")
176+
S_0 <- a # Initial price
177+
r <- b # Risk-free rate
178+
179+
if (type == "terminal") {
180+
# Scale to S_0 * e^(r * T)
181+
theta <- log(S_0 * exp(r * T) / mean(x))
182+
output <- x * exp(theta) # Mean = S_0 * e^(r * T)
183+
} else if (type == "discounted") {
184+
# Scale to S_0
185+
theta <- log(S_0 / mean(x))
186+
output <- x * exp(theta) # Mean = S_0
187+
} else {
188+
stop("Invalid type: use 'Terminal' or 'Discounted' for riskneutral method")
189+
}
190+
} else {
191+
stop("Invalid method: use 'minmax' or 'riskneutral'")
192+
}
193+
150194
return(output)
151-
}
152-
153-
195+
}

man/NNS.rescale.Rd

+27-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NNS.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)