简体   繁体   English

如何设计掷硬币游戏的“机器学习”方法

[英]How to design a “machine learning” method for a coin flip game

I am finishing up an algorithms class and the professor wants us to do an exercise in "machine learning" I pretty much have free reign in the project, but, it has to fall under the general umbrella of supervised learning. 我正在完成算法课程,而教授希望我们在“机器学习”中进行练习,我几乎可以完全控制该项目,但是,它必须属于监督学习的一般范畴。 So, I decided to do a coin flip simulation with bets being made by a computer player. 因此,我决定用计算机玩家下注进行硬币翻转模拟。 My idea is to make the coin biased and see if the computer can discover that bias when placing bets by using training data. 我的想法是使硬币产生偏差,并查看计算机在使用训练数据下注时是否能发现偏差。 I'm really not sure how to approach this.. 我真的不知道该如何处理。

An idea I had was to increment a win counter and pass that as a parameter into a method when determining the bets after a training round of say 100 bets. 我的想法是增加一个获胜计数器,并将其作为参数传递给方法,以便在进行了100次下注的训练后确定下注。 That way the computer player could make a decision to place more heads, if for example heads came up with 90% probability. 这样,计算机玩家可以决定放置更多的头,例如,如果头出现的机率达到90%,则可以决定。 After sufficient training I think the computer would discover the bias. 经过充分的培训后,我认为计算机会发现偏差。

I have code here to increment a win counter depending on the outcome, but, I'm not really sure where to go from there. 我这里有代码,可以根据结果增加胜利计数器,但是,我不确定从那里到哪里。 IE how to hold the corresponding data for if it was a win and if heads or tails came up and then use that information to place the bets in the next round. IE如何保存相应的数据,以判断是否获胜,是否出现正面或反面,然后使用该信息在下一轮下注。 Any advice would be greatly appreciated. 任何建议将不胜感激。 I am also open to using a different method if anyone thinks I am going about this the wrong way. 如果有人认为我会以错误的方式处理问题,我也愿意使用其他方法。

Please keep in mind that I am a second year undergrad and have very little knowledge of and exposure to actual machine learning techniques, hence the quotation marks in my question. 请记住,我是本科二年级,对实际的机器学习技术了解甚少,因此在我的问题中加引号。

public class CoinFlip {

  public static void main(String[] args) {
    int bank = 500;
    int headsCount=0;
    int tailsCount=0;
    int winCounter=0;
    int lossCounter=0;
    String[] flipResults = new String[100];

    ComputerPlayer randomPlayer = new ComputerPlayer();
    Coin coin = new Coin();

    for (int i = 0; i < 100; i++) {
        randomPlayer.placeRandomBet();
        flipResults[i] = coin.flip();
        if(flipResults[i].equals("heads")){
            headsCount += 1;
        }
        else{
            tailsCount +=1;
        }
        if(flipResults[i].equals(randomPlayer.placeRandomBet())){
            bank += 50;
            winCounter+=1;
        }
        if(!flipResults[i].equals(randomPlayer.placeRandomBet())){
            bank -=50;
            lossCounter+=1;
    }
  }
}

public static class ComputerPlayer{

      double bet;
      String heads = "heads";
      String tails = "tails";

    public String placeRandomBet(){
        bet = Math.random();
        if(bet < .5){
            return heads;
        }
        else return tails;
    }

    public static String placeLearnedBet(int wins; int losses){
        //not sure where to start
    }

public static class Coin {

        double coin;
        String heads = "heads";
        String tails = "tails";

    public String flip() {
        if (Math.random() < .9) {
            return heads;
        } else {
            return tails;
        }
    }

}

If you know that coin is distributed uniformly with a bias then you should always go with the majority. 如果您知道硬币是有偏差均匀分布的,那么您应该始终选择大多数。 If you do not know the distribution of the coin then you want to use an algorithm called randomized weighted majority ( Regret Minimization ). 如果您不知道硬币的分布,那么您想使用一种称为随机加权多数( Regret Minimization )的算法。 This algorithm basically states that you should choose heads with probability #heads/#flips. 该算法基本上表明您应该选择概率为#heads /#flips的heads。 As far as the weights are concerned if you know that the coin is distributed uniformly then you can estimate the bias and calculate the variance of your estimate. 就权重而言,如果您知道硬币是均匀分布的,则可以估计偏差并计算估计值的方差。 As the number of flips goes up the variance goes down like the square root of the number of flips. 随着翻转次数的增加,方差下降,就像翻转次数的平方根一样。 Your wagers should go up appropriately. 您的投注应适当增加。 IMO the weights make the problem a little less clear. 国际海事组织的权重使问题变得不太清楚。 What is your goal? 你的目标是什么? To optimize your total expected gain? 要优化您的总预期收益? If so then it is not clear whether you should wager at all until the last coin flip at which point your variance is lowest and wager all your money then. 如果是这样,那么尚不清楚是否应该在最后一次硬币翻转之前完全下注,此时您的方差最低,然后下注所有钱。 This strategy however does not minimize your variance because all your money lies on one wager. 但是,此策略不会使您的差异最小化,因为您的所有钱都只能押一次。 Also if the coin is unbiased is it better to not wager at all or to wager all your money. 同样,如果硬币没有偏见,则最好根本不下注或下注所有钱。 In both cases you have 0 expected gain but with very different variances. 在这两种情况下,您的预期增益均为0,但差异非常大。

As far as finding the bias, that's not really all that difficult. 就发现偏见而言,这实际上并不那么困难。 Basically, you would just guess the bias based on the number of heads out of the total number of plays and this ratio would give you the average bias. 基本上,您只需要根据总局数中的正面人数来猜测偏差,则该比率将为您提供平均偏差。 So for instance if you got 51 heads and 49 tails, then you would guess the bias towards heads is 51%. 因此,例如,如果您有51个正面和49个反面,那么您可能会认为正面的偏向是51%。

Ultimately, I think Benjy is correct that you should bet (heads or tails) based on which is more probable (according to your bias). 归根结底,我认为Benjy是正确的,您应该基于哪个更可能(根据您的偏见)下注(正面或反面)。 As you get more and more samples, your guessed bias will converge to the actual bias. 随着您获得越来越多的样本,您的猜测偏差将收敛到实际偏差。 As far as the betting goes, from an expectation stand point, you should bet as much as possible; 从预期的角度来看,您应该尽可能多地下注。 but from a realistic standpoint, this is not a good strategy (since you will eventually lose everything if you always bet everything unless the coin is 100% biased one way or the other). 但是从现实的角度来看,这不是一个好的策略(因为如果您始终押注所有赌注,除非您将硬币以一种或另一种方式100%偏倚,否则最终将输掉所有钱)。

A possible betting strategy could be "forward looking", ie assume you are going to make the same bet for the next n rounds and calculate the probability that you will win vs. lose. 一种可能的下注策略可能是“前瞻性”,即假设您接下来的n轮将进行相同的下注,并计算出您获胜与失败的概率。 You aren't directly trying to find the amount to bet rather you are trying to find the number n (the number of rounds to look forward) that give a certain probability of success. 您并不是直接尝试寻找下注金额,而是试图寻找具有一定成功可能性的数字n(期待的回合数)。 The riskier the betting scheme, the smaller the accepted probability of success (ie the riskier the scheme) and the more conservative the betting scheme, then the larger accepted probability of success. 投注计划的风险越高,可接受的成功概率就越小(即该计划的风险越大),并且投注方案越保守,则可接受的成功概率就越大。 Once you find the number of rounds needed to guarantee you success (within your selected probability) you can then divide your pot by the number of expected rounds, that is bet the maximum amount such that if you lose every one of the next n rounds, you can still make a bet. 一旦找到了确保成功所需的回合数(在您选择的概率之内),您就可以将底池除以预期的回合数,即最大赌注数,这样,如果您在接下来的n回合中每输一次,您仍然可以下注。

You can see that with the above, a conservative scheme would require more and more forward rounds and thus the bet would get smaller and smaller whereas with a risky scheme, the probability of success is small and thus would require fewer rounds and allow for a larger bet. 您可以看到,采用上述方法,保守的计划将需要越来越多的前轮回合,因此赌注将越来越小;而对于有风险的方案,成功的可能性将很小,因此将需要较少的回合并允许更大的回合打赌。

The last interesting thing is to decide how risky of a scheme you should choose. 最后一件有趣的事情是决定您应该选择哪种方案的风险。 Based on what I did, the less biased the coin (ie the closer to 50% chance of heads or tails), the riskier you should be and the more biased the coin (ie say 90% chance of tails or 90% chance of heads) the less risky your betting should be. 根据我的操作,硬币的偏差越小(即正面或反面的几率接近50%),您应该冒险,硬币的偏差就越大 (即,90%的反面几率或90%的正面几率) )您的下注风险应更低 This sort of makes sense because if the coin is completely fair, then there isn't a betting scheme that will usually win so your best just making large bets and taking the 50% chance that you come out on top. 之所以这样说是有道理的,因为如果硬币是完全公平的,那么就没有通常可以获胜的下注方案,因此,您的最佳选择只是进行大额下注,并抓住50%的机会获得冠军。 On the other hand, if the coin is extremely biased then the game itself isn't very risky and thus your betting scheme doesn't need to be very risky. 另一方面,如果硬币有很大的偏见,那么游戏本身就不会有很大的风险,因此您的下注计划也不必太冒险。 In the latter case, if you tend to make large wagers then you increase the chance that you lose everything vs. smaller wagers that all but guarantee a large payoff. 在后一种情况下,如果您倾向于下大笔赌注,那么您输掉所有东西的机会就会增加,而较小的赌注却保证了大笔的回报。 Keep in mind that as your pot grows, you can make larger and larger wagers without increasing your risk of losing money and thus if the coin is very biased it's quite easy to essentially get exponential growth in your pot. 请记住,随着锅的增长,您可以在不增加输钱风险的情况下下更大的赌注,因此,如果硬币有很大的偏见,那么从根本上就很容易在锅中获得指数增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM