如何将曲线上的点与对象数组中的点相关联?

How to associate point on a curve with points in an array of objects?

我有一堆来自网络的名字(名字,姓氏,不同国家的人)。一些国家统计了每个姓有多少人,如here.

等一些地方所示。

好吧,那个日本姓氏列表只列出了前 100 名。我还有其他列表,例如越南人列出了前 20 名,甚至在某些地方也列出了前 50 名或 1000 名。但是我有 real 名单,最多可达 1000 多个。所以我可能有 2000 个日本姓氏,其中只有 100 个列出了该姓氏的实际人数。

我想做的是建立一个“伪造的”类库,根据这些统计数据生成真实的名字。我知道如何在 JavaScript 中 ,所以一旦每个名字都包含“权重”(具有该名字的人数),只需将其插入该算法即可。

我的问题是,如何在没有权重的名称上“完成曲线”?也就是说,假设我们有一条类似指数的曲线,来自 20 或 100 个具有权重的名称。然后我想从剩余的未加权列表中随机选择名称,并给它们一个值,使它们在某种程度上逼真地位于曲线的剩余尾部。怎么办?

例如,这里是越南名字的权重列表:

Nguyen,38
Tran,11
Le,9.5
Pham,7.1
Huynh,5.1
Phan,4.5
Vu,3.9
Đang,2.1
Bui,2
Do,1.4
Ho,1.3
Ngo,1.3
Duong,1
Ly,0.5

这是一个没有权重的列表:

An
Ân
Bạch
Bành
Bao
Biên
Biện
Cam
Cảnh
Cảnh
Cao
Cái
Cát
Chân
Châu
Chiêm
Chu
Chung
Chử
Cổ
Cù
Cung
Cung
Củng
Cừu
Dịch
Diệp
Doãn
Dũ
Dung
Dư
Dữu
Đái
Đàm
Đào
Đậu
Điền
Đinh
Đoàn
Đồ
Đồng
Đổng
Đường
Giả
Giải
Gia
Giản
Giang
Giáp
Hà
Hạ
Hậ
Hác
Hàn
Hầu
Hình
Hoa
Hoắc
Hoạn
Hồng
Hứa
Hướng
Hy
Kha
Khâu
Khổng
Khuất
Kiều
Kim
Kỳ
Kỷ
La
Lạc
Lai
Lam
Lăng
Lãnh
Lâm
Lận
Lệ
Liên
Liêu
Liễu
Long
Lôi
Lục
Lư
Lữ
Lương
Lưu
Mã
Mạc
Mạch
Mai
Mạnh
Mao
Mẫn
Miêu
Minh
Mông
Ngân
Nghê
Nghiêm
Ngư
Ngưu
Nhạc
Nhan
Nhâm
Nhiếp
Nhiều
Nhung
Ninh
Nông
Ôn
Ổn
Ông
Phí
Phó
Phong
Phòng
Phù
Phùng
Phương
Quách
Quan
Quản
Quang
Quảng
Quế
Quyền
Sài
Sầm
Sử
Tạ
Tào
Tăng
Tân
Tần
Tất
Tề
Thạch
Thai
Thái
Thang
Thành
Thảo
Thân
Thi
Thích
Thiện
Thiệu
Thôi
Thủy
Thư
Thường
Tiền
Tiết
Tiêu
Tiêu
Tô
Tôn
Tôn
Tông
Tống
Trác
Trạch
Trại
Trang
Trầm
Trâu
Trì
Triệu
Trịnh
Trương
Từ
Tư
Tưởng
Úc
Ứng
Vạn
Văn
Vân
Vi
Vĩnh
Vũ
Vũ
Vương
Vưu
Xà
Xầm
Xế
Yên

我想在没有权重的情况下随机化列表(很容易做到),然后为每个列表分配一个权重,使其在一定程度上填满曲线的尾部,所以感觉有点逼真。如何才能做到这一点?基本上,我们似乎需要获得初始加权曲线的“曲率”,然后以某种方式用新项目扩展它。它不需要是完美的,但任何可以近似的东西都会很酷。我不是 statistics/math 人,所以我真的不知道从哪里开始。

我没有我想要的确切结果,我只想要某种程度的生成曲线尾部的东西。例如,列表的开头可能如下所示:

An,0.5
Ân,0.45
Bạch,0.42
Bành,0.40
Bao,0.39
...

为了尝试直观地展示我的目标,下面的黑框是提供的数据。虚线框会延伸很长一段时间,但在这里我展示了它的开始。虚线框是我们要在曲线中填充的内容,因此它适合曲线起点的形状。

▐
▐
▐▐
▐▐
▐▐
▐▐▐
▐▐▐ 
▐▐▐▐ 
▐▐▐▐▐▐ 
▐▐▐▐▐▐▐▐▐▐
▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐░░░░
▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐░░░░░░░░░░░░░░░░░░░░░
▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

基本上,曲线左侧是少数几个最高值。当它向右移动时,它会根据“某种”模式变小。我们只需要粗略地继续右边的图案,所以它基本上延长了曲线。

我不是数学家,所以我只是使用 these equations, although Wolfram has some others 将数据拟合到 y=A*x^B 方程,这可能更适合您的数据。也许一些关于 (sur) 名称分布的论文可能暗示了一个更好的等式。

尽管如此,目前的预测似乎还不错:

/** @type {[name: string, weight: number][]} */
const KNOWN_NAMES = [
    ['Nguyen', 38],
    ['Tran', 11],
    ['Le', 9.5],
    ['Pham', 7.1],
    ['Huynh', 5.1],
    ['Phan', 4.5],
    ['Vu', 3.9],
    ['Đang', 2.1],
    ['Bui', 2],
    ['Do', 1.4],
    ['Ho', 1.3],
    ['Ngo', 1.3],
    ['Duong', 1],
    ['Ly', 0.5],
]

/** @type {string[]} */
const UNKNOWN_NAMES = [];
for (let i = 0; i < 20; i++) UNKNOWN_NAMES[i] = `Unknown${i}`;

/**
 * Predicts the A and B for y=A*x^B (y=A*(x**B) in JS notation).
 * Based on https://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
 * @param {number[]} data
 * @returns {[A: number, B: number]}
 */
function expCurveFit(data) {
    const n = data.length;
    let sum_ln_xy = 0;
    let sum_ln_x = 0;
    let sum_ln_y = 0;
    let sum_ln_x_pow = 0;
    for (let i = 1; i <= n; i++) {
        const x = i;
        const y = data[x - 1];
        sum_ln_xy += Math.log(x) * Math.log(y);
        sum_ln_x += Math.log(x);
        sum_ln_y += Math.log(y);
        sum_ln_x_pow += Math.log(x) ** 2;
    }
    const b_nom = (n * sum_ln_xy) - (sum_ln_x * sum_ln_y);
    const b_den = (n * sum_ln_x_pow) - (sum_ln_x ** 2);
    const b = b_nom / b_den;
    const a = (sum_ln_y - b * sum_ln_x) / n;
    return [Math.exp(a), b];
}

// Calculating the prediction function
const [A, B] = expCurveFit(KNOWN_NAMES.map(([, w]) => w));
console.log(`Fit: A=${A} B=${B}`);
/** @param {number} index */
const predict = (index) => A * ((index + 1) ** B);

// Show prediction results
console.log('== Known weights ==');
KNOWN_NAMES.forEach(([name, expected], index) => {
    const predicted = predict(index);
    console.log(`- #${index}: ${expected} VS ${predicted} => ${predicted - expected}`);
});
console.log('== Predicted tail ==');
for (let i = 0; i < 10; i++) {
    const index = KNOWN_NAMES.length + i;
    console.log(`- #${index}: ${predict(index)}`);
}
console.log('...');
console.log(`- #${2000}: ${predict(2000)}`);
console.log('...');
console.log(`- #${5000}: ${predict(5000)}`);

// Shuffle UNKNOWN, otherwise if your array is alphabetically sorted, names that
// are alphabetically "higher" will get higher weights. Wouldn't look natural.
// Based on https://whosebug.com/a/2450976/14274597
for (let index = UNKNOWN_NAMES.length; index;) {
    const random = Math.floor(Math.random() * index--);
    [UNKNOWN_NAMES[index], UNKNOWN_NAMES[random]] = [UNKNOWN_NAMES[random], UNKNOWN_NAMES[index]];
}

/** @type {[name: string, weight: number][]} */
const NAME_WEIGHTS = [
    // If we want to keep the original weights
    ...KNOWN_NAMES,
    // Now our predicted (offset by the number of KNOWN_WEIGHTS one)
    ...UNKNOWN_NAMES.map((name, i) => [name, predict(i + KNOWN_NAMES.length)]),
];

console.log('== Weighted names ==');
for (const [name, weight] of NAME_WEIGHTS) {
    console.log(`- ${name}${' '.repeat(30 - name.length)} ${weight}`);
}

snippet 的控制台切断了很多日志记录,但显示了完整的 NAME_WEIGHTS 部分。反正下面说说结果。

对于提供的数据,这是其预测与原始权重的对比:

- #0: 38 VS 43.69867297773659 => 5.698672977736592
- #1: 11 VS 15.989864279951354 => 4.9898642799513535
- #2: 9.5 VS 8.880479579268258 => -0.6195204207317424
- #3: 7.1 VS 5.850881554721921 => -1.2491184452780786
- #4: 5.1 VS 4.233113801814277 => -0.866886198185723
- #5: 4.5 VS 3.2494731198295947 => -1.2505268801704053
- #6: 3.9 VS 2.5984310535459065 => -1.3015689464540934
- #7: 2.1 VS 2.140907162689773 => 0.04090716268977301
- #8: 2 VS 1.8046982250005454 => -0.19530177499945456
- #9: 1.4 VS 1.548946697010319 => 0.14894669701031904
- #10: 1.3 VS 1.34896041963157 => 0.04896041963157005
- #11: 1.3 VS 1.1890208701279552 => -0.11097912987204483
- #12: 1 VS 1.0586914703306205 => 0.0586914703306205
- #13: 0.5 VS 0.9507968333083715 => 0.4507968333083715

远非完美,但这是一个好的开始。此外,对于已知名称,您可以使用原始权重。不过,预测的尾巴看起来还不错:

- #14: 0.8602568021432264
- #15: 0.7833833989610162
- #16: 0.717440740163343
- #17: 0.6603605491345175
- #18: 0.6105530322360989
- #19: 0.5667780226345167
- #20: 0.5280552551540235
- #21: 0.4936006647141039
- #22: 0.462780366132426
- #23: 0.4350768809172298
- #24: 0.4100639959533769
- #25: 0.38738780313885823
- #26: 0.3667522293417049
- #27: 0.3479078719427935
- #28: 0.33064329762683886
- #29: 0.3147781974794336
- #30: 0.30015795563424275
- #31: 0.2866493047726054
- #32: 0.27413682483865165
- #33: 0.2625201014677103
...
- #2000: 0.000711597758736851
...
- #5000: 0.0001884684445732886

这是 reusing/predicting 权重并将它们分配给名称的最终结果:

- Nguyen                         38
- Tran                           11
- Le                             9.5
- Pham                           7.1
- Huynh                          5.1
- Phan                           4.5
- Vu                             3.9
- Đang                           2.1
- Bui                            2
- Do                             1.4
- Ho                             1.3
- Ngo                            1.3
- Duong                          1
- Ly                             0.5
- Unknown9                       0.8602568021432264
- Unknown17                      0.7833833989610162
- Unknown10                      0.717440740163343
- Unknown1                       0.6603605491345175
- Unknown7                       0.6105530322360989
- Unknown16                      0.5667780226345167
- Unknown18                      0.5280552551540235
- Unknown12                      0.4936006647141039
- Unknown14                      0.462780366132426
- Unknown15                      0.4350768809172298
- Unknown6                       0.4100639959533769
- Unknown13                      0.38738780313885823
- Unknown0                       0.3667522293417049
- Unknown2                       0.3479078719427935
- Unknown11                      0.33064329762683886
- Unknown3                       0.3147781974794336
- Unknown19                      0.30015795563424275
- Unknown8                       0.2866493047726054
- Unknown5                       0.27413682483865165
- Unknown4                       0.2625201014677103

请注意,我先打乱了未知名称,否则您会将最高预测权重分配给 Unknown0,将第二高分配给 Unknown1,...这会让人感觉很不自然。例如。如果您使用按字母顺序排序的数组执行此操作,则以 A 开头的名称将非常常见,而以 Z 开头的名称将是最罕见的。

同样,Ly (0.5) 和 Unknown9 (0.86) 之间的突然跳跃显示了拟合曲线的不准确性,但话又说回来,现实主义并不'要求名称的完美分布。

我无法添加任何 javascript 代码(我不精通 js),但我可以为您指出来源以获取更多信息,并提供解决方案的概要。

基本上,你要推导出的信息需要O(C2N)的复杂度,according to this answer, or O(n3) according to this one but once you've found the derivation, the answer is relatively easy. If you'd like to derive it yourself, in javascript, you can use TensorFlow才能推导出你想要的信息。否则,使用涉及 WolframAlpha 的 Kelvin Schoofs 解决方案。

之后,就像 Kelvin 所做的那样,只需采用导出的方程式并插入新的“x”值即可。

请注意,由于名称的分布可能因多种因素而异,您可能需要注意这一点。这些因素可以用一个整数 K according to this paper. If I've understood correctly, the "best-fit" of your data would follow a "Yule-Simon" distribution 来概括,您可以使用它来获得更准确的“创建”数据分布。

即:

f(k;ρ) can be used to model, for example, the relative frequency of the kth most frequent word in a large collection of text, which according to Zipf's law is inversely proportional to a (typically small) power of k.

或者,according to this paper, 名称可以建模为

P(n)=a⋅n−b⋅e−(n/c)d, where P(n) represents the proportion of surnames whose sizes are no less than n, b is the power exponent, c is the cutoff size of power‐law part, and d is the stretch parameter in the stretched exponential function.

此外,如果您有访问权限,this paper might provide more information, but I don't have access to it, so I can't actually verify its utility. I think it can be summarized as saying that surnames can be modeled according to a Pareto distribution, 在这种情况下,您可以将您的名字插入该发行版。我相信你会使用相应的概率密度函数。