假设我有一个标准||x||.有没有一个R函数或方法可以让我计算对偶范数

||y||_* = \max_{x} x^Ty : ||x|| ≤ 1

我最初的 idea 是这属于一个约束优化问题,所以我想有一些有用的包来解决这个问题,但我还不能阐明如何做到这一点.

推荐答案

Analytical Dual Norm Solution

确实存在解析的对偶范数表示(见Wiki page)

enter image description here so we can code the dual norm function like below. It should be noted that, the code below gives a scalar value of dual norm only, rather than the solution of z that enables the supremum (as $par in the outcome of dual_norm_numeric.

dual_norm_analytic <- function(x, p) {
    if (p < 1) {
        stop("Invalide value of p: Shouldn be >= 1!")
    }
    if (p == 1) {
        return(max(abs(x)))
    }
    if (is.infinite(p)) {
        return(sum(abs(x)))
    }
    q <- 1 / (1 - 1 / p)
    sum(abs(x)^q)^(1 / q)
}

使用输入数据

set.seed(0)
n <- 10
x <- rnorm(n, 5)

我们将获得这一点

> dual_norm_analytic(x, 1)
[1] 7.404653

> dual_norm_analytic(x, 2)
[1] 17.3279

> dual_norm_analytic(x, 3)
[1] 25.15705

Numerical Solution by fmincon Optimization

如果你使用的是vector x(而不是矩阵,因为矩阵范数是另一回事,但你可以用类似的方式try ,如下面的代码所示),也许你可以try pracma包中的fmincon,并定义一个自定义的对偶范数函数,例如,

library(pracma)
dual_norm_numeric <- function(x, p) {
    if (p < 1) {
        stop("Invalide value of p: Shouldn be >= 1!")
    }
    if (p == 1) {
        list(value = max(abs(x)), par = +(abs(x) == max(abs(x))))
    } else {
        constr <- \(y) sum(abs(y)^p)^(1 / p) - 1
        fobj <- \(y) -sum(x * y)
        out <- fmincon(x * 0, fobj, hin = constr)
        list(value = -out$value, par = out$par)
    }
}

Output (Example)

x如下

> set.seed(0)

> n <- 10

> x <- rnorm(n, 5)

> set.seed(0)

> n <- 10

> x <- rnorm(n,5)

你会看到

> dual_norm_numeric(x, 1)
$value
[1] 7.404653

$par
 [1] 0 0 0 0 0 0 0 0 0 1


> dual_norm_numeric(x, 2)
$value
[1] 17.3279

$par
 [1] 0.3614374 0.2697252 0.3652950 0.3619841 0.3124811 0.1996814 0.2349643
 [8] 0.2715437 0.2882193 0.4273251


> dual_norm_numeric(x, 3)
$value
[1] 25.15705

$par
 [1] 0.4989528 0.4310259 0.5016088 0.4993309 0.4639326 0.3708614 0.4022939
 [8] 0.4324763 0.4455585 0.5425285

R相关问答推荐

使用spatVector裁剪网格数据时出现的问题

在R底座中更改白天和夜晚的背景 colored颜色

使用gggrassure减少地块之间的空间

如何按排序顺序打印一个框架中所有精确的唯一值?

如何在R中合并和合并多个rabrame?

多个过滤器内的一个盒子在仪表板Quarto

非线性混合效应模型(NLME)预测变量的置信区间

根据元素和前一个值之间的差值过滤矩阵的元素

在R中使用Scale_y_Break后更改y轴标签

将多个列值转换为二进制

是否有可能从边界中找到一个点值?

主题(Legend.key=Element_RECT(Fill=&Quot;White&Quot;))不起作用

R-如何在ggplot2中显示具有不同x轴值(日期)的多行?

如何在R中使用因子行求和?

如何使用list_rind在列表中保留已命名但不包含第0行的记录?

具有由向量定义的可变步长的序列

如何在一种 colored颜色 中设置数值变量的 colored颜色 和高于阈值的 colored颜色 点?

Package emMeans:如果emmip模型中包含的变量较少,emMeans模型中的其他变量设置为什么?

如何将一列相关性转换为R中的相关性矩阵

移除y轴断开的geom_bar图的外框