如何在c++/java中生成一组遵循一定分布的值?

发布于 2024-12-08 20:32:51 字数 102 浏览 0 评论 0原文

例如,我有一个 pdf f(x) = 1/25 - x/1250(从 x = 0 到 50); 我的问题是如何生成一组满足给定pdf的值 请给出如何用c++或java实现的想法。 非常感谢。

For example, I have a p.d.f f(x) = 1/25 - x/1250 (from x = 0 to 50);
My question is that how to generate a set of values that satisfy the given p.d.f.
Please give an idea about how to implement in c++ or java.
Thanks so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海里的那抹蓝 2024-12-15 20:32:51

我不知道直接从pdf,但你可以将pdf转换为cdf(通过集成),然后使用

生成这样的 u 后,找到满足 cdf(x) = uxx 就是您想要的值。

I don't know about directly from a pdf, but you can convert a pdf to a cdf (by integrating) and then use inverse transform sampling. This assumes that you have a technique to generate a random number u in [0, 1] (e.g. Math.random() in Java).

Once you have generated such a u, find an x such that cdf(x) = u. That x is the value that you want.

一身软味 2024-12-15 20:32:51

您的函数类似于 f(x) := 1 - x。对于这种情况,我执行了以下操作:

  1. 集成函数 f 来生成 F
  2. 标准化函数F,使其域为[0;1),产生NF
  3. 反转NF 得到dist

然后我使用以下代码来检查结果:

package so7691025;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class DistImage {

  private static double dist(double x) {
    return 1.0 - Math.sqrt(1.0 - x);
  }

  public static void main(String[] args) throws IOException {
    final Random rnd = new Random(0);

    BufferedImage img = new BufferedImage(1000, 1000, BufferedImage.TYPE_BYTE_GRAY);
    int[] distrib = new int[1000];
    for (int i = 0; i < 500000; i++) {
      distrib[(int) (dist(rnd.nextDouble()) * 1000)]++;
    }

    Graphics g = img.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 1000, 1000);
    g.setColor(Color.BLACK);
    for (int i = 0; i < 1000; i++) {
      g.drawLine(i, 1000, i, 1000 - distrib[i]);
    }

    ImageIO.write(img, "png", new File("d:/distrib.png"));
  }
}

生成的图像对我来说看起来相当不错。

Your function is similar to f(x) := 1 - x. For that case I did the following:

  1. integrate the function f to yield F.
  2. normalize the function F so that its domain is [0;1), yielding NF.
  3. invert NF to yield dist.

Then I used the following code to check the result:

package so7691025;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class DistImage {

  private static double dist(double x) {
    return 1.0 - Math.sqrt(1.0 - x);
  }

  public static void main(String[] args) throws IOException {
    final Random rnd = new Random(0);

    BufferedImage img = new BufferedImage(1000, 1000, BufferedImage.TYPE_BYTE_GRAY);
    int[] distrib = new int[1000];
    for (int i = 0; i < 500000; i++) {
      distrib[(int) (dist(rnd.nextDouble()) * 1000)]++;
    }

    Graphics g = img.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 1000, 1000);
    g.setColor(Color.BLACK);
    for (int i = 0; i < 1000; i++) {
      g.drawLine(i, 1000, i, 1000 - distrib[i]);
    }

    ImageIO.write(img, "png", new File("d:/distrib.png"));
  }
}

The resulting image looked pretty good to me.

删除会话 2024-12-15 20:32:51

如果您的值集是离散的,则文章 "A 中描述了有效的算法生成具有给定分布的随机数的线性算法”。该算法包含一个简单的伪代码,时间为 O(n),其中 n 是集合大小。

当 pdf 不是离散的(如您的示例中所示)时,请参阅 “蒙特卡洛简介”中的第 5.1 节模拟”

If your values-set is discrete, a valid algorithm is described in the article "A Linear Algorithm For Generating Random Numbers With a Given Distribution". The algorithm contains a simple pseudo-code, and has O(n) time, where n is the set size.

When the pdf is not discrete, as in your example, see section 5.1 in "Introduction to Monte Carlo Simulation".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文