KS,AUC 和 PSI 是风控算法中最常计算的几个指标,本文记录了多种工具计算这些指标的方法。

生成本文的测试数据:

import pandas as pd
import numpy as np
import pyspark.sql.functions as F
from pyspark.sql.window import Window
from pyspark.sql.types import StringType, DoubleType
from pyspark.sql import SparkSession, functions
from sklearn.metrics import roc_auc_score,roc_curve tmptable = pd.DataFrame({'y':[np.random.randint(2) for i in range(1000000)]})
tmptable['y'] = tmptable['score'].apply(lambda x:1 if np.random.rand()+x>0.8 else 0)
tmp_sparkdf = spark.createDataFrame(tmptable)
tmp_sparkdf.craeteOrReplaceTempView('tmpview')

一、KS

​KS 指标来源于 Kolmogorov-Smirnov 检验,通常用于比较两组样本是否来源于同一分布。在建模中划分训练集与测试集后,通常运用 KS 检验来检验训练集与测试集的分布差异,如果分布差异过大,那可能就会因为训练集、测试集划分不合理而降低模型的泛化性。(关于 KS 检验的更多细节

在风控中,KS 指标通过来衡量模型对于好坏样本的区分能力,其具体的算法为:

  1. 按模型分从小到大排序,并分为 n 组(等频分组或每个不同的分值作为一组)
  2. 计算截至每一组的累积好样本(y=0)占比与累积坏样本(y=1)占比,记为 \(cumgoodratio_i\) 和 \(cumbadratio_i\)

    如第 k 组:

    累积好样本占比=第 k 组前包括第 k 组 y=0 样本数量 / 全部 y=0 样本的数量

    累积坏样本占比=第 k 组前包括第 k 组 y=1 样本数量 / 全部 y=1 样本的数量
  3. 则 \(KS=max(abs(cumgoodratio_i-cumbadratio_i))\)

1. SQL 计算 KS

select max(abs(cumgood/totalgood-cumbad/totalbad)) as ks
from (
select score,
sum(totalbad)over(order by score) as cumbad,
sum(totalgood)over(order by score) as cumgood,
sum(totalbad) over() as totalbad,
sum(totalgood) over() as totalgood
from (
select
score,
sum(y) as totalbad,
sum(1-y) as totalgood
from tmpview
group by score
)
)

2. Python 计算 KS

def get_ks(y_true:pd.Series,y_pred:pd.Series):
'''
A staticmethod to caculate the KS of the model.
Args:
y_true: true value of the sample
y_pred: pred value of the sample Returns:
max(tpr-fpr): KS of the model
'''
fpr,tpr,_ = roc_curve(y_true,y_pred)
return str(max(abs(tpr-fpr)))
ksdata = spark.sql('select * from tmpview').toPandas()
print(get_ks(ksdata['y'],ksdata['score']))

3. Pyspark 计算 KS

有两种方法,1 是对用 pyspark 的语法把 SQL 的逻辑给写出来,可以算出来 KS;2 就是包装成 UDF 函数,这样当需要 groupby 后计算 KS 时,可以直接调用 UDF 函数分组计算 KS

a. SQL 逻辑改写

ksdata = spark.sql('select * from tmpview')

def calks(df,ycol='y',scorecol='score'):
return df.withColumn(ycol,F.col(ycol).cast('int')).withColumn(scorecol,F.col(scorecol).cast('float'))\
.withColumn('totalbad',F.sum(F.col(ycol)).over(Window.orderBy(F.lit(1))))\
.withColumn('totalgood',F.sum(1-F.col(ycol)).over(Window.orderBy(F.lit(1))))\
.withColumn('cumgood',F.sum(1-F.col(ycol)).over(Window.orderBy(F.col(scorecol).asc())))\
.withColumn('cumbad',F.sum(F.col(ycol)).over(Window.orderBy(F.col(scorecol).asc())))\
.select(F.max(F.abs(F.col('cumgood')/F.col('totalgood')-F.col('cumbad')/F.col('totalbad'))).alias('KS'))
calks(ksdata).show()

b. python 转 UDF 函数

def get_ks(y_true:pd.Series,y_pred:pd.Series):
'''
A staticmethod to caculate the KS of the model.
Args:
y_true: true value of the sample
y_pred: pred value of the sample Returns:
max(tpr-fpr): KS of the model
'''
fpr,tpr,_ = roc_curve(y_true,y_pred)
return str(max(abs(tpr-fpr)))
get_ks_udfs = F.udf(get_ks, returnType=StringType())
ksdata = spark.sql('select * from tmpview')
print(ksdata.withColumn('eval metrics',F.lit('KS'))\
.groupby('eval metrics')\
.agg(get_ks_udfs(F.collect_list(F.col('y')),F.collect_list(F.col('score'))).alias('KS'))\
.select('KS').toPandas())

二、AUC

AUC(Area Under Curve)被定义为 ROC 曲线下与坐标轴围成的面积,通常用来衡量二分类模型全局的区分能力。在 python 和 pyspark 中可以直接调包计算,在 SQL 中可以根据公式计算获得,其计算方法如下:

  1. 对 score 从小到大排序

  2. 根据公式计算:

    \[AUC=\frac{\sum_{i\in{positiveClass}}rank_i-\frac{M(1+M)}{2}}{M\times N}
    \]

    其中,\(rank_i\) 代表第 i 个正样本的排序序号,M 和 N 分别代表正样本和负样本的总个数。

关于该公式的详细理解,可参考 AUC 的计算方法(及评论)

1. SQL 计算 AUC

select (sumpositivernk-totalbad*(1+totalbad)/2)/(totalbad*totalgood) as auc
from
(
select sum(if(y=1,rnk,0)) as sumpositivernk,
sum(y) as totalbad,
sum(1-y) as totalgood
from
(
select y,row_number() over (order by score) as rnk
from tmpview
)
)

2. Python 计算 AUC

ksdata = spark.sql('select * from tmpview').toPandas()
print(roc_auc_score(ksdata['y'],ksdata['score']))

3. Pyspark 计算 AUC

同 KS 的计算,除了提到的两种方式,还可以调用 pyspark 的 ML 包下二分类评价,来计算 AUC

a. SQL 逻辑改写

aucdata = spark.sql('select * from tmpview')

def calauc(df,ycol='y',scorecol='score'):
return df.withColumn(ycol,F.col(ycol).cast('int')).withColumn(scorecol,F.col(scorecol).cast('float'))\
.withColumn('totalbad',F.sum(F.col(ycol)).over(Window.orderBy(F.lit(1))))\
.withColumn('totalgood',F.sum(1-F.col(ycol)).over(Window.orderBy(F.lit(1))))\
.withColumn('rnk2',F.row_number().over(Window.orderBy(F.col(scorecol).asc())))\
.filter(F.col(ycol)==1)\
.select(((F.sum(F.col('rnk2'))-0.5*(F.max(F.col('totalbad')))*(1+F.max(F.col('totalbad'))))/(F.max(F.col('totalbad'))*F.max(F.col('totalgood')))).alias('AUC'))\ calauc(aucdata).show()

b. UDF 函数

def auc(ytrue,ypred):
return str(roc_auc_score(ytrue,ypred))
get_auc_udfs = F.udf(auc, returnType=StringType())
aucdata = spark.sql('select * from tmpview')
aucdata.withColumn('eval metrics',F.lit('AUC'))\
.groupby('eval metrics')\
.agg(get_auc_udfs(F.collect_list(F.col('y')),F.collect_list(F.col('score'))).alias('AUC'))\
.select('AUC').show()

c. 调包

from pyspark.ml.evaluation import BinaryClassificationEvaluator
evaluator = BinaryClassificationEvaluator(rawPredictionCol='score',labelCol='y')
aucdata = spark.sql('select * from tmpview')
evaluator.evaluate(aucdata)

三、PSI

PSI(Population Stability Index:群体稳定性指标),通常被用于衡量两个样本模型分分布的差异,在风控建模中通常有两个作用:

  1. 用于建模时筛选掉不稳定的特征
  2. 用于建模后及上线后评估和监控模型分值的稳定程度

个人认为该指标无一个比较明确的标准,在样本量较大的条件下,筛选特征时尽量控制特征 PSI<0.1,或更严格。

计算 PSI 首先需要一个分箱基准,假定本文随机生成的模型分的分箱切分点为\([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]\)

1. SQL 计算 PSI

select
sum(grouppsi) as psi
from (
select g
,log(count(1) / sum(count(1))over() / 0.1)*(count(1) / sum(count(1))over() - 0.1) as grouppsi
from (
select
case when score<cutpoint[1] then 1
when score<cutpoint[2] then 2
when score<cutpoint[3] then 3
when score<cutpoint[4] then 4
when score<cutpoint[5] then 5
when score<cutpoint[6] then 6
when score<cutpoint[7] then 7
when score<cutpoint[8] then 8
when score<cutpoint[9] then 9
when score<cutpoint[10] then 10 else 'error' end as g
from (
select *
,array(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1) as cutpoint
from tmpview
)
)
group by g
)

2. Python 计算 PSI

psidata = spark.sql('select * from tmpview').toPandas()
psidata['g'] = pd.cut(psidata['score'],cut_point)
psitable = psidata.groupby('g')['y'].count()
psitable /= psitable.sum()
standratio = 1/(len(cut_point)-1)
psi = sum((psitable-standratio)*np.log(psitable/standratio))

3. Pyspark 计算 PSI

参考Pyspark 实现连续分桶映射并自定义标签,调包分箱后按公式计算 PSI

from pyspark.ml.feature import Bucketizer

def psi(df, splits, inputCol, outputCol):
if len(splits) < 2:
raise RuntimeError("splits's length must grater then 2.") standratio = 1 / (len(splits)-1)
bucketizer = Bucketizer(
splits=splits, inputCol=inputCol, outputCol='split')
with_split = bucketizer.transform(df)
with_split = with_split.groupby('split')\
.agg((F.count(F.col(inputCol))/F.sum(F.count(F.col(inputCol))).over(Window.orderBy(F.lit(1)))).alias('groupratio'))\
.select(F.sum((F.col('groupratio')-standratio)*F.log(F.col('groupratio')/standratio)).alias('PSI')) return with_split
psi(aucdata,cut_point,'score','group').show()

参考资料

深入理解 AUC​

SQL 计算多模型分的 PSI

Pyspark 实现连续分桶映射并自定义标签

使用 pyspark dataframe 的 groupby 计算 AUC

SQL->Python->PySpark计算KS,AUC及PSI的更多相关文章

  1. 模型监控指标- 混淆矩阵、ROC曲线,AUC值,KS曲线以及KS值、PSI值,Lift图,Gain图,KT值,迁移矩阵

    1. 混淆矩阵 确定截断点后,评价学习器性能 假设训练之初以及预测后,一个样本是正例还是反例是已经确定的,这个时候,样本应该有两个类别值,一个是真实的0/1,一个是预测的0/1 TP(实际为正预测为正 ...

  2. pyspark计算最大值、最小值、平均值

    需求:使用pyspark计算相同key的最大值.最小值.平均值 说明: 最大值和最小值好计算,直接reduceByKey后使用python内置的max.min方法 平均值计算提供两种计算方法,直接先上 ...

  3. windows下安装python科学计算环境,numpy scipy scikit ,matplotlib等

    安装matplotlib: pip install matplotlib 背景: 目的:要用Python下的DBSCAN聚类算法. scikit-learn 是一个基于SciPy和Numpy的开源机器 ...

  4. Python TF-IDF计算100份文档关键词权重

    上一篇博文中,我们使用结巴分词对文档进行分词处理,但分词所得结果并不是每个词语都是有意义的(即该词对文档的内容贡献少),那么如何来判断词语对文档的重要度呢,这里介绍一种方法:TF-IDF. 一,TF- ...

  5. Python科学计算(二)windows下开发环境搭建(当用pip安装出现Unable to find vcvarsall.bat)

    用于科学计算Python语言真的是amazing! 方法一:直接安装集成好的软件 刚开始使用numpy.scipy这些模块的时候,图个方便直接使用了一个叫做Enthought的软件.Enthought ...

  6. 目前比较流行的Python科学计算发行版

    经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...

  7. Python科学计算之Pandas

    Reference: http://mp.weixin.qq.com/s?src=3&timestamp=1474979163&ver=1&signature=wnZn1UtW ...

  8. Python 科学计算-介绍

    Python 科学计算 作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文 ...

  9. Python科学计算库

    Python科学计算库 一.numpy库和matplotlib库的学习 (1)numpy库介绍:科学计算包,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换.随机数生成 ...

随机推荐

  1. Python基础(迭代)

    # from collections import Iterable#collections模块的Iterable类型判断 # dict1 = {'a':111,'b':222,'c':333} # ...

  2. CrawlSpider_获取图片名称地址,及入库

    1.继承自scrapy.Spider 2.独门秘笈 CrawlSpider可以定义规则,再解析html内容的时候,可以根据链接规则提取出指定的链接,然后再向这些链接发送请求 所以,如果有需要跟进链接的 ...

  3. oracle 连接数据库并查询,返回List<Map<String, Object>> 数据

    package JDBC; import java.sql.Clob; import java.sql.Connection; import java.sql.DriverManager; impor ...

  4. ndarray 数组的创建和变换

    ndarray数组的创建方法 1.从python中的列表,元组等类型创建ndarray数组 x = np.array(list/tuple) x = np.array(list/tuple,dtype ...

  5. ErrorProvider与CheckedListBox

    http://www.cnblogs.com/myshell/archive/2010/09/24/1834184.html 最近因为做WinForm的项目,遇到这个问题,当时以为CheckedLis ...

  6. [hdu7000]二分

    不妨假设$x\le y$,可以通过翻转整个格子序列来调整 令$a_{i}$​​为$i$​​到$y$​​的期望步数,显然有方程$a_{i}=\begin{cases}0&(i=y)\\\frac ...

  7. Python学习手册(第四版)——使用入门(自学用)

    Python的优点 -可读性 -可移植性 -不是一个独立的工具,可以调用各种库,同时也可以被调用等等 -使编程变得很有趣 -面向对象 -可混合 Python的理念 随便找的一张图 脚本语言? Pyth ...

  8. 一类利用队列优化的DP

    I.导入: 这是一个\(O(n^2)\)的状态和转移方程: \[f(i,j)=\left\{ \begin{aligned} f(i-1,j-1)+k \ (1\leq j)\\ \max_{k \i ...

  9. 【基因组注释】同源注释比对软件tblastn、gamp和exonerate比较

    基因结构预测中同源注释策略,将mRNA.cDNA.蛋白.EST等序列比对到组装的基因组中,在文章中通常使用以下比对软件: tblastn gamp exonerate blat 根据我的实测,以上软件 ...

  10. 『与善仁』Appium基础 — 16、APPium基础操作API

    目录 1.前置代码 2.安装和卸载APP 3.判断APP是否已安装 4.关闭APP软件和关闭驱动对象 5.发送文件到手机和获取手机中的文件 6.获取当前屏幕内元素结构(重点) 7.脚本内启动其他APP ...