Optimal Bet Fraction of Account Value (short C++ code)

Discussion in 'App Development' started by botpro, Feb 2, 2016.

  1. botpro

    botpro

    Hi,
    here's a short C++ program that computes the optimal fraction of account that one should bet
    for a set of user defined probabilities and their payouts:

    Code:
    /*
    optimal_bet_fraction.cpp
    
    Author: U.M. in Germany (user botpro @ www.elitetrader.com)
    
    2016-02-02-Tu: init
    
    Solution to this online posting:
      http://www.elitetrader.com/et/index.php?threads/how-much-should-you-risk.297565/
        There is a 35% chance that you win   20% of your bet;
        There is a 25% chance that you lose  15% of your bet;
        There is a 20% chance that you win   12% of your bet;
        There is a 15% chance that you lose   6% of your bet;
        There is a  4% chance that you win   50% of your bet;
        There is a  1% chance that you lose 100% of your bet.
        What percentage of your betting account should you risk to maximize your potential gain?
    
    Compile using a C++11 conformant C++ compiler, here using GNU g++ under Linux:
      g++ -Wall -O2 -std=c++11 optimal_bet_fraction.cpp -o optimal_bet_fraction.exe
     
    Run:
      ./optimal_bet_fraction.exe >data.csv 
    
    And the answer is:
      MaxGrowthRate=2.87735% @ fraction=77.79000%
    
    Misc:
      You can change the pre-set conditions, and also add new conditions or del some conditions,
      then recompile and execute the program.
    
    */
    
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    
    using namespace std;
    
    
    int optimal_betfraction()
      { /* creates a csv file of all iterations (this can be analysed in Excel/LibreOffice-Calc),
           and prints the maximum to stderr
        */
     
     
        // set these input data (vp and vr must both be same size):
        const vector<double> vp = { 0.35,  0.25,  0.2,   0.15,  0.04,  0.01 };  // probabilities
        const vector<double> vr = { 0.2,  -0.15,  0.12, -0.06,  0.5,  -1.0  };  // payouts
    
    
        const double PctStep = 0.01;  // 0.001;   // 1.0;
    
        double MaxGrowthRate = -1e9, xPctForMaxGR = 0;   // will be filled below
    
        printf("r,f\n");
        for (double xPct = 0.0; xPct <= 100.0; xPct += PctStep)
          {
            const double x = xPct / 100.0;
           
            double GrowthRate = 1.0;
            for (size_t i = 0; i < vp.size(); ++i)
              {
                if (vr[i] >= 0.0)
                  GrowthRate *= pow(1.0 + x * fabs(vr[i]), vp[i]);
                else 
                  GrowthRate *= pow(1.0 - x * fabs(vr[i]), vp[i]);
              }
               
           printf("%.5f,%.5f\n", (GrowthRate - 1.0) * 100.0, xPct);
    
           if (GrowthRate > MaxGrowthRate)
             MaxGrowthRate = GrowthRate, xPctForMaxGR = xPct;
          }
        fprintf(stderr, "MaxGrowthRate=%.5f%% @ fraction=%.5f%%\n", (MaxGrowthRate - 1.0) * 100.0, xPctForMaxGR);
    
        return 0;
      }
    
    
    int main()
      {
        optimal_betfraction();
    
        return 0;
      }
    
    And the same also in a zip file:
     
  2. Just curious - why does this need C++11? (I didn't read thru the code)
     
  3. botpro

    botpro

    I think it's because of the vector initializer method used here.
    Of course one can change that easily, then C++11 should not be required anymore.
    One can of course also replace the two vectors wholly by classic arrays.