Web 基尼系数的计算

html 复制代码
<!DOCTYPE html>
<html>

<head>
  <title>计算基尼系数</title>
  <script src="https://cdn.staticfile.org/Chart.js/3.9.1/chart.js"></script>
  <script>
    var myChart;
    function giniCoefficient(data) {
      var cum_data = Array.from({ length: data.length + 1 }, function (_, i) {
        return i === data.length ? 0 : data[i];
      }).sort(function (a, b) { return a - b; }).reduce(function (acc, val) {
        acc.push((acc[acc.length - 1] || 0) + val);
        return acc;
      }, []);
      var sum_data = cum_data[cum_data.length - 1];
      var xarray = Array.from({ length: cum_data.length }, function (_, i) { return i / (cum_data.length - 1); });
      var upper = xarray;
      var yarray = cum_data.map(function (val) { return val / sum_data; });
      console.log(xarray, yarray);
      //pl.plot(xarray, yarray)
      //pl.plot(xarray, upper)
      var B = trapz(yarray, xarray);
      console.log("B的面积:", B);
      var A = 0.5 - B;
      console.log("A的面积:", A);
      var G = A / (A + B);

      // 销毁之前的图表实例
      if (myChart) {
        myChart.destroy();
      }
      // 创建画布
      const canvas = document.getElementById('giniChart');
      canvas.width = 30;
      canvas.height = 20;
      var ctx = canvas.getContext('2d');

      // 绘制基尼系数曲线
      myChart = new Chart(ctx, {
        type: 'line',
        data: {
          labels: xarray,
          datasets: [
            {
              label: '洛伦茨曲线的面积',
              data: yarray,
              borderColor: 'blue',
              backgroundColor: 'rgba(0, 0, 255, 0.5)',
              fill: true
            },
            {
              label: '绝对平等线面积与洛伦茨曲线面积之差',
              data: xarray,
              borderColor: 'red',
              backgroundColor: 'rgba(255, 0, 0, 0.5)',
              fill: true,
            }
          ]
        },
        options: {
          scales: {
            x: {
              display: true,
              title: {
                display: true,
                text: '个数累计百分比'
              }
            },
            y: {
              display: true,
              title: {
                display: true,
                text: '数值累计百分比'
              }
            }
          },
          title: {
            display: true,
            text: 'Gini Coefficient'
          }
        }
      });

      return G;
    }

    function trapz(y, x) {
      if (y.length !== x.length) {
        throw new Error('The length of y and x must be the same');
      }
      var n = y.length;
      var s = 0;
      for (var i = 1; i < n; i++) {
        s += (x[i] - x[i - 1]) * (y[i] + y[i - 1]);
      }
      return s / 2;
    }


    function calculateGini() {
      var values = document.getElementById("inputValues").value;
      var data = values.split(",").map(Number);

      var result = giniCoefficient(data)

      document.getElementById("result").innerHTML = "基尼系数: " + result;
    }
  </script>
</head>

<body>
  <h1>计算基尼系数</h1>
  <label for="inputValues">输入数据(用逗号分隔):</label>
  <input type="text" id="inputValues"><br><br>
  <button onclick="calculateGini()">计算</button><br><br>
  <div id="result"></div>
  <dic style="width: 600px; display:block">
  <canvas id="giniChart"></canvas>
  </dic>
</body>

</html>
相关推荐
QQ1__8115175158 小时前
Spring boot名城小区物业管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
钛态8 小时前
前端微前端架构:大项目的救命稻草还是自找麻烦?
前端·vue·react·web
一粒黑子8 小时前
【实战解析】阿里开源 PageAgent:纯前端 GUI Agent,一行JS让网页支持自然语言操控
前端·javascript·开源
独角鲸网络安全实验室8 小时前
2026微信小程序抓包全解析:从实操落地到合规风控,解锁前端调试新范式
前端·微信小程序·小程序·抓包·系统代理绕过·https证书严格校验·进程隔离
紫微AI8 小时前
前端文本测量成了卡死一切创新的最后瓶颈,pretext实现突破了
前端·人工智能·typescript
GISer_Jing8 小时前
AI前端(From豆包)
前端·aigc·ai编程
IT枫斗者8 小时前
前端部署后如何判断“页面是不是最新”?一套可落地的版本检测方案(适配 Vite/Vue/React/任意 SPA)
前端·javascript·vue.js·react.js·架构·bug
测试修炼手册8 小时前
[测试技术] 深入理解 JSON Web Token (JWT)
前端·json
AI老李8 小时前
2026 年 Web 前端开发的 8 个趋势!
前端
里欧跑得慢8 小时前
15. Web可访问性最佳实践:让每个用户都能平等访问
前端·css·flutter·web