[英]Monte Carlo Simulation to Simulate Election
I have been tormented by this problem for the last week or so and cant seem to find a solution..在过去的一周左右的时间里,我一直被这个问题折磨着,似乎找不到解决方案。
My question is, given a presidential election, I am given odds for the 6 candidates, I have to find the probability of a certain three candidates finishing in the last 3 places in any order using a monte carlo simulation.我的问题是,在总统选举中,我得到了 6 位候选人的赔率,我必须使用蒙特卡洛模拟以任意顺序找出某三位候选人以任何顺序排在最后 3 位的概率。
My first approach was to run a simulation x amount of times with 1000000 + votes.. the problem with this was that I was never finding any cases where these candidates finished last because with so many votes and with the random number generator in java, i eliminated the possibility of luck and therefore the placings just stayed with the probability..我的第一种方法是用 1000000 + 票运行模拟 x 次.. 问题是我从来没有发现这些候选人最后完成的任何情况,因为有这么多票和 java 中的随机数生成器,我消除了运气的可能性,因此排名只停留在概率上。
I have been told a better way to do it is to stage an election, and then if the random number falls into the probability of one candidate, i must give him first place, and then with whatever probability is left over i must decide second place and then third place etc etc.. I know the answer has to be 0.7.. does anyone know how I can approach this?有人告诉我一个更好的方法是举行选举,然后如果随机数落入一个候选人的概率,我必须给他第一名,然后无论剩下的概率我必须决定第二名然后是第三名等等。我知道答案必须是 0.7 .. 有谁知道我该如何解决这个问题? many thanks.
非常感谢。
Here is the code I have so far, this is not giving me the answer I am supposed to have but I cant seem to find a fault in it..这是我到目前为止的代码,这并没有给我应该有的答案,但我似乎无法找到它的错误..
import java.util.*;
public class Lab4 {公开课 Lab4 {
public static void main(String[] args) {
Random rd = new Random();
int numElections = 50000000;
int count = 0;
ArrayList<String> ar2 = new ArrayList<String>();
for(int i = 1; i <= numElections; i++)
{
double mh = (double)25/26 / 1.0951899766899766;
double pc = (double)1/10 / 1.0951899766899766;
double sg = (double)1/66 / 1.0951899766899766;
double lnr = (double)1/80 / 1.0951899766899766;
double jf = (double)1/250 / 1.0951899766899766;
double gd = (double)1/500 / 1.0951899766899766;
double sumOdds = (mh + pc + sg + lnr + jf + gd);
for(int j = 1; j <= 6; j++)
{
double randomValue = rd.nextDouble() * sumOdds;
if(randomValue > 0 && randomValue <= mh)
{
ar2.add("mh");
sumOdds -= mh;
mh = 0;
}
else if(randomValue > 0 && randomValue <= mh + pc)
{
ar2.add("pc");
sumOdds -= pc;
pc = 0;
}
else if(randomValue > 0 && randomValue <= mh + pc + sg)
{
ar2.add("sg");
sumOdds-= sg;
sg = 0;
}
else if(randomValue > 0 && randomValue <= mh + pc + sg + lnr)
{
ar2.add("lnr");
sumOdds-= lnr;
lnr = 0;
}
else if(randomValue > 0 && randomValue <= mh + pc + sg + lnr + jf)
{
ar2.add("jf");
sumOdds-= jf;
jf = 0;
}
else if(randomValue > 0 && randomValue <= mh + pc + sg + lnr + jf + gd)
{
ar2.add("gd");
sumOdds-= gd;
gd = 0;
}
}
if(ar2.get(3) == "pc"|| ar2.get(4) == "pc" || ar2.get(5) == "pc")
{
if(ar2.get(3) == "gd"|| ar2.get(4) == "gd" || ar2.get(5) == "gd")
{
if(ar2.get(3) == "sg"|| ar2.get(4) == "sg" || ar2.get(5) == "sg")
{
count++;
}
}
}
ar2.clear();
}
System.out.println((double)count/numElections);
}
} }
The odds for a given candidate are either a percentage of the total vote, or X:Y - like 10:1.给定候选人的几率要么是总选票的百分比,要么是 X:Y——比如 10:1。 You can convert the latter into the former by Y/X = 0.1.
您可以通过 Y/X = 0.1 将后者转换为前者。
Your simulation idea is great.你的模拟想法很棒。 The question is, how do you choose based on the random number generator .
问题是,你如何选择基于随机数生成器。
Let's say we had an array of Candidates, each of which had a normalised chance of winning.假设我们有一组候选人,每个候选人都有一个标准化的获胜机会。 A percentage would be divided by 100 - so 80% = 0.8, and the odds format X:Y would be converted as shown above.
百分比将除以 100 - 所以 80% = 0.8,并且赔率格式 X:Y 将如上所示进行转换。
We know that the array of candidates' chance column adds up to 1.0.我们知道候选人的机会列数组加起来为 1.0。
Using https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextFloat-- we can generate a number between 0. and 1.0.使用https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextFloat--我们可以生成一个介于 0. 和 1.0 之间的数字。
Keeping a running total of the total probability so far, count up the list to find the candidate that the nextFloat
has hit.保持到目前为止的总概率的运行总数,对列表进行计数以找到
nextFloat
已命中的候选者。
Something like this像这样的东西
Random random = new Random();
Candidate[] candidates = ...;
// do this for each vote
float total = 0.0f;
int index = 0;
float randomVote = random.nextFloat();
while (total<randomVote && index<candidates.length) {
total += candidates[index].getProbability();
if (total<randomVote) {
// only increment to the next candidate if this one didn't win the vote
index++;
}
}
// at this point, the index is the candidate who won the vote
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.