sql实现均衡度计算(洛伦兹曲线,基尼系数)

发布时间:2024年01月05日

1.基本介绍

均衡度:使用洛伦兹曲线计算基尼系数,基尼系数代表均衡度,系数值越大证明越不均衡度,值约小证明越均衡。【例:拿贫富差距人口收入举例,将累计人口百分比作X轴,累计收入百分比作Y轴,绘制贫富差距洛伦兹曲线如下图,使用面积法计算:基尼系数=A面积/(A面积+B面积)】(这里基尼系数不会大于1,也不会小于零)。

基尼系数

2.使用场景

(一)职权履行均衡度

假设某执法单位有职权2000项,按照职权触发次数由低到高排序,等分10份,及每200个职权分一个挡位,计算X轴(累计职权数百分比)、Y轴(累计职权触发次数百分比)如下表:

职权分档排序每档职权触发次数X轴(职权累计百分比)y轴(职权累计触发次数百分比)
1-200010%0
201-40020020%0.7%
401-60030030%1.8%
601-80050040%3.6%
801-100055050%5.6%
1001-120099960%9.3%
1201-1400111170%14%
1401-1600176580%20%
1600-1800180090%26%
1800-200020000100%100%

绘制洛伦兹曲线如下图:

散点1

如图计算A、B区域面积得基尼系数为0.8,可得此单位处罚职权履行不均衡。

(二)案件月度分布均衡度

将某一执法单位每月案件量按照由小到大顺序排序,计算X轴(累计月份百分比)、Y轴(累计月案件量百分比)如下表:

月份月检查量(万)X轴(累计月份百分比)Y轴(累计月检查量百分比)
6月18%2%
11月116%3%
12月125%5%
1月233%8.3%
4月442%15%
2月550%23.3%
8月558%31.6%
3月667%42%
7月875%55%
5月983%70%
9月992%85%
10月9100%100%

绘制洛伦兹曲线如下图:

散点2

如图计算A、B区域面积得基尼系数为0.36,可得此单位检查月度分布较为均衡。

3.sql实现(函数)

计算均衡度(基尼系数)自定义函数(十等份计算,参数为查询到数据的sql语句的字符形式)
?
-- 根据批量数据进行分值切割从而计算均衡度
CREATE OR REPLACE FUNCTION JHD_JN(TABLENAME VARCHAR2) RETURN NUMBER 
AS
T_COUNT NUMBER;
QUERY_SQL VARCHAR2(20000);
BEGIN
QUERY_SQL:='
-- 先计算得到各维度不同职权code的被触发数量
WITH POWER_ALL AS (' || TABLENAME || '
),
-- 对上方进行10等分切割得到每段的长度以及需要在第一段补充(0,0)的长度
-- eg: aa:73 bb:2 ? (每段73个但是第一段73-2个)
POWER_SUM AS (
SELECT 
    CASE 
        WHEN MOD((SELECT COUNT(1) FROM POWER_ALL), 10) > 0 
            THEN TRUNC(ROUND((SELECT COUNT(1) FROM POWER_ALL)/10, 1))+1 
        ELSE ROUND((SELECT COUNT(1) FROM POWER_ALL)/10) 
    END aa,
    CASE 
        WHEN MOD((SELECT COUNT(1) FROM POWER_ALL), 10) > 0 THEN
            10-MOD((SELECT COUNT(1) FROM POWER_ALL),10)
        ELSE 0
    END bb
FROM dual
),
-- 如果当前列数不满足等份便在开头增加上一步的余数个0,0
POWER_ALL_DATA AS (
    SELECT tb1.*,ROWNUM SORT FROM (
        SELECT 
            ''0'' NAME,
            CASE 
                WHEN (SELECT bb-aa FROM POWER_SUM) > 0 THEN 0
                ELSE NULL
            END ALL_COUNT 
        FROM (SELECT 1 FROM dual CONNECT BY LEVEL <= (SELECT bb FROM POWER_SUM))
    
        UNION ALL
        
        SELECT NAME,ALL_COUNT FROM POWER_ALL
    ) tb1 WHERE tb1.ALL_COUNT IS NOT NULL
),
-- 先根据上方的aa对数据进行分割,假如有余数的话便先加上这个余数进行分割
POWER_LB1 AS (
SELECT tb1.ll-((SELECT aa FROM POWER_SUM)-1) s,tb1.ll e 
FROM (
    SELECT LEVEL ll FROM dual CONNECT BY LEVEL < (SELECT COUNT(1) FROM POWER_ALL_DATA) + 1 + CASE WHEN (SELECT aa-bb FROM POWER_SUM) > 0 AND (SELECT bb FROM POWER_SUM) > 0 THEN (SELECT bb FROM POWER_SUM) ELSE 0 END
) tb1 WHERE MOD(tb1.ll, (SELECT aa FROM POWER_SUM)) = 0
),
-- 如果有余数的话再次进行处理,从而实现如果有余数的话将第一段减少
POWER_LB AS (
SELECT 
    CASE 
        WHEN (SELECT aa-bb FROM POWER_SUM) > 0 AND (SELECT bb FROM POWER_SUM) > 0 AND s != 1 THEN s - (SELECT bb FROM POWER_SUM)
        ELSE s
    END s,
    CASE 
        WHEN (SELECT aa-bb FROM POWER_SUM) > 0 AND (SELECT bb FROM POWER_SUM) > 0 THEN e - (SELECT bb FROM POWER_SUM)
        ELSE e
    END e,
    ROWNUM*10 r
FROM POWER_LB1
),
-- 获取x和y轴数据(包含原点一共11个)
POWER_RESULT AS (
SELECT tb1.*,ROWNUM so FROM (
    SELECT 0 bfb,0 r FROM dual
?
    UNION
?
    SELECT ?
        CASE 
            WHEN (SELECT SUM(ALL_COUNT) FROM POWER_ALL_DATA) = 0 THEN 0
            ELSE ROUND((SELECT SUM(ALL_COUNT)*100 FROM POWER_ALL_DATA WHERE SORT <= tb1.e)/(SELECT SUM(ALL_COUNT) FROM POWER_ALL_DATA),5)
        END bfb,
        r
    FROM POWER_LB tb1
) tb1 ORDER BY r
),
-- 计算面积一个三角形,九个梯形的和
POWER_DOWN_AREA AS (
SELECT SUM(jhd) DOWN_AREA FROM (
    SELECT 
        CASE 
            -- 三角形面积
            WHEN tb1.r < 1 THEN (SELECT ROUND(bfb*r/2 , 2)FROM POWER_RESULT WHERE so = 2)
            WHEN tb1.r = 11 THEN 0
            -- 九个梯形面积
            ELSE ROUND((tb1.bfb+(SELECT bfb FROM POWER_RESULT WHERE so = tb1.so + 1))*((SELECT r FROM POWER_RESULT WHERE so = tb1.so + 1) - tb1.r)/2,2)
        END jhd,
        tb1.*
    FROM POWER_RESULT tb1
))
?
-- 基尼系数(如果参数查询无数据则返回1,基尼系数最大为1。如果总面积为0则设置基尼系统为1)
-- 返回结果为百分数 eg:0.3 返回30
SELECT 
    CASE 
        WHEN ROUND(bfb*r/2, 2) = 0 THEN 100
        ELSE
            --NVL(ROUND(bfb*r/2, 2) - (SELECT * FROM POWER_DOWN_AREA),100)
            --ROUND(bfb*r/2, 2)
            NVL(ROUND((ROUND(bfb*r/2, 2) - (SELECT * FROM POWER_DOWN_AREA))/ROUND(bfb*r/2, 2),2),1)*100
    END AREA 
FROM (SELECT * FROM POWER_RESULT ORDER BY SO DESC) WHERE ROWNUM < 2';
EXECUTE IMMEDIATE QUERY_SQL ?INTO T_COUNT;
RETURN  T_COUNT; ? 
END JHD_JN;
?
?
?
-- 直接根据X,Y数组进行均衡度计算
CREATE OR REPLACE FUNCTION JHD_JN1(TABLENAME VARCHAR2) RETURN NUMBER 
AS
T_COUNT NUMBER;
QUERY_SQL VARCHAR2(20000);
BEGIN
QUERY_SQL:='
WITH POWER_RESULT AS (
' || TABLENAME || '
),
POWER_DOWN_AREA AS (
SELECT SUM(jhd) DOWN_AREA FROM (
    SELECT 
        CASE 
            -- 三角形面积
            WHEN tb1.X < 1 THEN (SELECT ROUND(Y*X/2 , 2)FROM POWER_RESULT WHERE so = 2)
            WHEN tb1.X = (SELECT COUNT(1) FROM POWER_RESULT) THEN 0
            -- 多个梯形面积
            ELSE ROUND((tb1.Y+(SELECT Y FROM POWER_RESULT WHERE so = tb1.so + 1))*((SELECT X FROM POWER_RESULT WHERE so = tb1.so + 1) - tb1.X)/2,2)
        END jhd,
        tb1.*
    FROM POWER_RESULT tb1
))
?
-- 基尼系数(如果参数查询无数据则返回1,基尼系数最大为1。如果总面积为0则设置基尼系统为1)
-- 返回结果为百分数 eg:0.3 返回30
SELECT 
    CASE 
        WHEN ROUND(Y*X/2, 2) = 0 THEN 100
        ELSE
            NVL(ROUND((ROUND(Y*X/2, 2) - (SELECT * FROM POWER_DOWN_AREA))/ROUND(Y*X/2, 2),2),1)*100
    END AREA 
FROM (SELECT * FROM POWER_RESULT ORDER BY SO DESC) WHERE ROWNUM < 2';
EXECUTE IMMEDIATE QUERY_SQL ?INTO T_COUNT;
RETURN  T_COUNT; ? 
END JHD_JN1;
文章来源:https://blog.csdn.net/weixin_51296247/article/details/135410432
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。