【算法检验】SPA

光谱波长筛选算法 常见光谱波长筛选算法原理及对应代码,包括SPA、LAR、UVE、CARS 阅读详情

一、概念

The multiple comparison procedures all allow for examining aspects of superior predictive ability.

用于检验更优的预测能力

There are three available:

  • SPA - The test of Superior Predictive Ability, also known as the Reality Check (and accessible as RealityCheck) or the bootstrap data snooper, examines whether any model in a set of models can outperform a benchmark.
  • 检验一系列模型能够优于基本模型
  • StepM - The stepwise multiple testing procedure uses sequential testing to determine which models are superior to a benchmark.
  • 使用连续的检验决定哪个模型优于基准模型
  • MCS - The model confidence set which computes the set of models which with performance indistinguishable from others in the set.
  • 计算与模型集中性能相仿的模型

All procedures take losses as inputs. That is, smaller values are preferred to larger values. This is common when evaluating forecasting models where the loss function is usually defined as a positive function of the forecast error that is increasing in the absolute error. Leading examples are Mean Square Error (MSE) and Mean Absolute Deviation (MAD).

所有的方法都以Losses作为输入。即,越小越好。当评估预测模型性能时,Loss函数通常被定义为预测误差的正函数,随着绝对误差上升而上升。典型的例子为均方误差和均值绝对误差。

二、SPA Test

1、概念

This procedure requires a t-element array of benchmark losses and a t by k-element array of model losses. The null hypothesis is that no model is better than the benchmark, or

 SPA算法的假设:没有模型优于基准模型。。

where Li is the loss from model i and Lbm is the loss from the benchmark model.

其中Li为模型i的Loss,公式左侧代表多个模型损失的均值;同理,Lbm代表基准模型损失。

This procedure is normally used when there are many competing forecasting models such as in the study of technical trading rules. The example below will make use of a set of models which are all equivalently good to a benchmark model and will serve as a size study.

该模型一般用于评估多个算法的优劣。

2、例子

Study Design

The study will make use of a measurement error in predictors to produce a large set of correlated variables that all have equal expected MSE.

本例将使用预设误差的预测因子,产生一系列相关变量,且其具有相同的MSE(LOSS)。

The benchmark will have identical measurement error and so all models have the same expected loss, although will have different forecasts.

基准模型和其他模型一样具有形同误差,尽管其预测值不同。

The first block computed the series to be forecast.

(1)首先生成被预测值,真实y。

import statsmodels.api as sm
from numpy.random import randn
t = 1000
#随机生产1000*3的因子,1000为时间序列长度
factors = randn(t, 3)
beta = np.array([1, 0.5, 0.1])
#e为残差
e = randn(t)
#y为被预测值
y = factors.dot(beta)

(2)再计算包含噪音的模型因子:1+500。该模型通过前500个时间序列拟合,再通过后500个时间序列预测,从而计算Loss。

The next block computes the benchmark factors and the model factors by contaminating the original factors with noise. The models are estimated on the first 500 observations and predictions are made for the second 500. Finally, losses are constructed from these predictions.

# 设置基准模型
# 生成基准模型因子=真实因子+噪音 1000*3维
bm_factors = factors + randn(t, 3)
# 通过前500个时间序列回归得beta
bm_beta = sm.OLS(y[:500], bm_factors[:500]).fit().params
# 通过后500个时间序列计算Loss
bm_losses = (y[500:] - bm_factors[500:].dot(bm_beta)) ** 2.0

# 设定500个比较模型
k = 500
model_factors = np.zeros((k, t, 3))
model_losses = np.zeros((500, k))
for i in range(k):
    # 给每个模型加噪音
    model_factors[i] = factors + randn(1000, 3)
    # 计算beta
    model_beta = sm.OLS(y[:500], model_factors[i, :500]).fit().params
    # 预测计算Loss,该矩阵维度为500*500,即时间序列*模型数量,每一列是一个模型的Loss
    model_losses[:, i] = (y[500:] - model_factors[i, 500:].dot(model_beta)) ** 2.0

(3)最终,我们可以使用SPA了。SPA要求基准模型以及竞争模型的Loss作为输入。其他的输入选项可以进行自助抽样法,或者是其他对于Loss的Student化。

Finally the SPA can be used. The SPA requires the losses from the benchmark and the models as inputs. Other inputs allow the bootstrap sued to be changed or for various options regarding studentization of the losses. compute does the real work, and then pvalues contains the probability that the null is true given the realizations.

本例中,我们无法拒绝原假设。三个P值对应于不同Loss的re-centerings。我们通常使用consistent的P值。

In this case, one would not reject. The three p-values correspond to different re-centerings of the losses. In general, the consistent p-value should be used. It should always be the case that

from arch.bootstrap import SPA

spa = SPA(bm_losses, model_losses, seed=seed)
spa.compute()
spa.pvalues

 

3、进阶

上述过程可以重复多次以进行模拟。这里因为时间关系,我们只重复100次。并且,在每一次SPA的应用中,我设定250为Bootstrap复制次数,默认值为1000。

The same blocks can be repeated to perform a simulation study. Here I only use 100 replications since this should complete in a reasonable amount of time. Also I set reps=250 to limit the number of bootstrap replications in each application of the SPA (the default is a more reasonable 1000). 

# Save the pvalues
pvalues = []
# 设定重复次数
b = 100
seeds = gen.integers(0, 2 ** 31 - 1, b)
# Repeat 100 times
for j in range(b):
    if j % 10 == 0:
        print(j)
	#设定真实y
    factors = randn(t, 3)
    beta = np.array([1, 0.5, 0.1])
    e = randn(t)
    y = factors.dot(beta)
	
	#计算基准模型损失
    # Measurement noise
    bm_factors = factors + randn(t, 3)
    # Fit using first half, predict second half
    bm_beta = sm.OLS(y[:500], bm_factors[:500]).fit().params
    # MSE loss
    bm_losses = (y[500:] - bm_factors[500:].dot(bm_beta)) ** 2.0
    # Number of models
	
	#计算检验模型损失
    k = 500
    model_factors = np.zeros((k, t, 3))
    model_losses = np.zeros((500, k))
    for i in range(k):
        model_factors[i] = factors + randn(1000, 3)
        model_beta = sm.OLS(y[:500], model_factors[i, :500]).fit().params
        # MSE loss
        model_losses[:, i] = (y[500:] - model_factors[i, 500:].dot(model_beta)) ** 2.0
    
	#使用Bootstrap
	# Lower the bootstrap replications to 250
    spa = SPA(bm_losses, model_losses, reps=250, seed=seeds[j])
    spa.compute()
    pvalues.append(spa.pvalues)

最终,我们得到了100个SPA结果。

最终,我们画出P值。理想情况下,该曲线应为45度,表现size是正确的。

注意:在每次循环中,SPA的值是随着j而变化的,因此希望成45度。

spa = SPA(bm_losses, model_losses, reps=250, seed=seeds[j])

 Finally the pvalues can be plotted. Ideally they should form a 45o line indicating the size is correct. Both the consistent and upper perform well. The lower has too many small p-values.

 4、SPA可以拒绝零假设。

The SPA also has power to reject then the null is violated. The simulation will be modified so that the amount of measurement error differs across models, and so that some models are actually better than the benchmark. The p-values should be small indicating rejection of the null.

# Number of models
k = 500
model_factors = np.zeros((k, t, 3))
model_losses = np.zeros((500, k))
for i in range(k):
    # K越大的模型,噪声越小
    scale = (2500.0 - i) / 2500.0
    model_factors[i] = factors + scale * randn(1000, 3)
    model_beta = sm.OLS(y[:500], model_factors[i, :500]).fit().params
    # MSE loss
    model_losses[:, i] = (y[500:] - model_factors[i, 500:].dot(model_beta)) ** 2.0

spa = SPA(bm_losses, model_losses, seed=seed)
spa.compute()
spa.pvalues

P值很小,可以拒绝零假设。 

我们画出平均Loss,以验证说法。

Here the average losses are plotted. The higher index models are clearly better than the lower index models -- and the benchmark model (which is identical to model.0).

#计算出500个模型的Loss
model_losses = pd.DataFrame(model_losses, columns=["model." + str(i) for i in range(k)])
#取时间序列均值,得到各模型的Loss
avg_model_losses = pd.DataFrame(model_losses.mean(0), columns=["Average loss"])
fig = avg_model_losses.plot(style=["o"])

 显然,K越大的模型Loss越小,且大多优于基准模型(与model一致,其噪声的系数都为1)。

 

matlab连续投影算法SPA使用示例 连续投影算法(Successive Projections Algorithm,SPA)是一种使矢量空间共线性最小化的前向变量选择算法, 它的优势在于提取全波段的几个特征波长,能够消除原始光谱矩阵中冗余的信息,可用于光谱特征波长的筛选。 阅读详情

相关推荐

低密度奇偶校验码LDPC(六)——SPA和积译码算法

SPA 的核心是,在因子图中,一个因子节点在边 𝑥上的输出消息是和沿着除 𝑥之外的所 有其他边输入到该节点的消息的乘积,并在除 𝑥之外的所有变量上求和。

通信系统、数字信号处理、FPGA 2723

光谱特征选择---连续投影算法SPA

作为光谱分析的重要环节,如何从冗余、复杂的变量中选取特征变量直接决定了预测模型的性能,在实际特征选择过程中,通常从两方面考虑特征选择的合理性:一是对目标变量y的解释性,二是不同自变量x间的冗余性,前者是考虑变量自身或变量组合的预测性能,后者考虑变量间的冗余问题,如何在保证对模型性能的同时减少变量冗余对于提高模型精度、降低过拟合风险和提高模型泛化性十分重要。 本周分享连续投影算法(successive projections algorithm,SPA),SPA算法2001年...

qq_35667901的博客 2万+

MATLAB版高光谱SPA波段筛选工具:含投影计算、稳定性验证与特征导出

一套开箱即用的MATLAB高光谱波段筛选工具,基于连续投影算法(SPA)自动识别最具判别能力的光谱变量,剔除高度相关或冗余的波段。包含四个核心脚本:SSPPAA.m执行SPA主流程,projection.m完成逐次波段投影与残差计算,validation.m支持交叉验证或分类器评估(如SVM、PLS-DA)以检验所选波段子集的稳定性与建模效果,SPA final为封装后的统一调用入口。输入数据需为矩阵格式,每行一个样本,最后一列为类别标签,其余列为原始光谱响应值;适用于ROI提取后的高光谱图像或点光谱数据。

cream的博客 194

SPA test

SPA test

m0_61015717的博客 1808

SPA 变量选择工具箱 - 光谱数据分析的得力助手

在SPA工具箱的主界面中,功能区被分为几个主要部分:文件管理区、数据导入区、分析工具区和结果显示区。用户首先在文件管理区打开或创建新的项目,并将需要分析的数据文件导入到软件中。数据导入区允许用户选择不同的光谱数据格式,如CSV或TXT等。分析工具区提供了一系列的光谱分析工具,用户可以根据需求选择不同的算法进行变量选择和模型建立。最后,结果显示区将分析的结果以图表或报告的形式呈现给用户。graph TBA[启动SPA工具箱] --> B[文件管理区]B --> C[数据导入区]

weixin_36078669的博客 773

连续投影算法(SPA)详解:光谱变量筛选原理与Python实战

光谱数据分析中常面临高维、强共线性与样本量不足的矛盾,冗余波长不仅加剧过拟合,还影响模型稳定性。连续投影算法(SPA)通过向量投影原理,在原始光谱中逐步筛选出共线性低、信息冗余小的特征波长,从几何层面消除变量间的重叠信息。该算法无需依赖特定模型,可与偏最小二乘、多元线性回归等常用建模方法配合,作为光谱定量分析的预处理和降维手段。在近红外光谱、化学计量学等场景中,SPA能够显著压缩变量数量、提升模型解释性,并为在线检测等工程应用降低计算负担。本文结合Python实现与案例,展示从原理到实践的完整流程,并讨论样

weixin_30386713的博客 354

【算法检验】StepM和MCS

继上节SPA继续介绍StepM和MSC检验 【算法检验】SPA_Checkmate9949的博客-优快云博客一、概念The multiple comparison procedures all allow for examining aspects of superior predictive ability.用于检验更优的预测能力There are three available:SPA - The test of Superior Predictive Ability, also known as

Checkmate9949的博客 5087

连续投影算法SPA:从原理到Python实现的光谱变量选择

在光谱分析中,高维小样本数据常导致模型过拟合,变量选择是降维和提升模型可解释性的关键手段。连续投影算法(SPA)作为一种经典的前向特征选择方法,通过在向量空间中迭代寻找与已选变量最不共线的波长,有效去除冗余信息,保留最具代表性的特征。该算法不仅适用于近红外光谱的波长优选,也可推广至中红外、拉曼光谱等多元校正场景。结合Python代码实现,SPA能在保留模型精度的同时大幅简化模型结构,为工业在线检测提供低成本解决方案。本文从算法原理出发,详细介绍了SPA的数学基础、完整流程及调参经验,并展示了与PLS、MLR

weixin_33868027的博客 390

SPA连续投影算法:高光谱波长选择的几何降维原理与NumPy实现

连续投影算法(SPA)是一种面向高光谱数据的无监督变量筛选方法,其本质是在高维光谱空间中通过正交投影与残差最大化,识别信息冗余低、解释力独立的波长子集。它不依赖目标变量y,仅基于自变量间结构关系构建正交基,从而有效缓解共线性、提升多元校正模型(如PLS、SVR)的稳定性与泛化能力。相比Lasso或相关系数法,SPA具备更强的几何可解释性与噪声鲁棒性,特别适用于嵌入式光谱分析、便携设备建模及化学计量学预处理场景。本文聚焦SPA的核心原理、纯NumPy手写实现与工程避坑实践。

weixin_29044157的博客 281

spa通过神经网络在空间中实现快速共指解析-Python开发

:sparkles:NeuralCoref 4.0:与神经网络一起使用时的共指解析。 NeuralCoref是spaCy 2.1+的管道扩展,它使用神经网络注释和解析共指群集。 NeuralCoref是正式产品:sparkles:NeuralCoref 4.0:使用Neural Networks在空间中实现共指解析。 NeuralCoref是spaCy 2.1+的管道扩展,它使用神经网络注释和解析共指群集。 NeuralCoref已投入生产,已集成到spaCy的NLP管道中,并可扩展到新的培训数据集。 有关共指解析和NeuralCoref的简要介绍,请参阅我们的博客文章。 NeuralCoref用Python / Cython编写,并带有针对Eng的预先训练的统计模型

SPA-连续投影算法-教程版.zip

对老版本代码进行了调试,找了一个光谱的数据进行降维测试,效果不错,有需要的小伙伴下载吧,调试不易,谢谢支持。

近红外高光谱特征波段提取算法

不过要注意别陷入算法崇拜,有时候物理化学背景知识选出来的波段比算法更靠谱,毕竟光谱特征峰的位置不是随机分布的。这段代码有个骚操作——每次把候选波段往已选波段空间投影,专治波段间的多重共线性。注意第17行的循环投影,相当于在已选特征构成的空间里做残差筛选,这样选出来的波段携带新信息量最大。接下来看看CARS(竞争性自适应重加权采样),这算法模仿达尔文进化论,通过自适应加权和蒙特卡罗采样筛选变量。拿经典的SPA(连续投影算法)开刀,这货擅长找互不相关的特征波段。近红外,高光谱特征波段提取算法。

IqfQZrhA的博客 284

使用SPA算法计算太阳位置 Qt可执行程序

使用SPA算法,打包了Qt的可执行程序,下载后打开SPA.exe可直接运行。输入年月日时分秒,经纬度,可计算天平坐标系下的太阳位置,另外可对温度、压力及海拔等参数进行设置,太阳位置精度达到0.0003°。

金融领域强化学习:核心算法与实战应用解析

强化学习作为机器学习的重要分支,通过智能体与环境的持续交互实现策略优化,特别适合处理动态变化系统中的决策问题。其核心原理基于马尔可夫决策过程,通过价值函数和策略迭代实现最优决策。在金融科技领域,强化学习的技术价值主要体现在处理非平稳市场环境、优化多目标决策等方面。通过Q-Learning、DDPG等算法的金融化改造,结合波动率感知探索率和双网络结构等技术,可有效应用于资产配置、交易执行等场景。特别是在高频交易和风险管理中,强化学习能动态调整策略参数,实现收益与风险的平衡。本文以量化投资为切入点,深入解析状态

weixin_33212799的博客 266

SPA-连续投影算法-教程版

对老版本代码进行了调试,找了一个光谱的数据进行降维测试,效果不错,有需要的小伙伴下载吧,调试不易,谢谢支持。

SPA提取特征_spa_光谱特征提取_连续投影算法;SPA;特征提取_spa提取_

SPA提取特征,数据为高光谱数据,感兴趣区域数据,最后一列为标签

StegExpose隐写分析工具:原理、实战与自动化集成指南

隐写术作为信息隐藏技术,通过在图像、音频等载体文件中嵌入秘密信息实现隐蔽通信,其核心原理常利用最低有效位(LSB)替换等统计方法修改数据。在数字取证与数据安全领域,检测此类隐蔽信息对防止数据泄露至关重要。隐写分析技术通过卡方检验、RS分析等算法,识别载体文件统计特征的异常,从而发现潜在威胁。StegExpose作为一款基于Java的命令行工具,集成了多种统计检测算法,专门用于检测JPEG、BMP等格式中的LSB隐写数据。该工具通过命令行参数支持批量处理和自动化集成,可生成HTML、CSV等格式报告,便于在服

circularr9834的博客 433

MATLAB配置MinGW编译器的完整指南

本文提供了在Windows系统上为MATLAB配置MinGW编译器的完整指南。详细介绍了通过MATLAB附加功能管理器在线安装和手动离线安装两种核心方法,并重点说明了如何解决常见的“未找到支持的编译器”等错误。指南包含从版本检查、环境变量配置到最终使用mex命令验证的每一步,帮助用户顺利完成MATLAB与MinGW的集成,以支持C/C++代码编译。

weixin_29031057的博客 265
上一篇: 【转载】我们怎么理解导数?
下一篇: 【算法检验】StepM和MCS
Checkmate9949
博客等级 码龄5年 6176粉丝 43原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值