【问题标题】:Optimisation in Java Using Apache Commons Math使用 Apache Commons Math 在 Java 中进行优化
【发布时间】:2021-12-12 23:17:56
【问题描述】:

我正在尝试使用commons-math 最小化 Java 中的值。我看过他们的documentation,但我真的不知道如何实现它。

基本上,在我下面的代码中,我有一个Double,其中包含足球比赛中的预期进球,我想将一场比赛中发生少于 3 个进球的概率值优化为 0.5。


import org.apache.commons.math3.distribution.PoissonDistribution;

public class Solver {

    public static void main(String[] args) {

        final Double expectedGoals = 2.9d;
        final PoissonDistribution poissonGoals = new PoissonDistribution(expectedGoals);

        Double probabilityUnderThreeGoals = 0d;

        for (int score = 0; score < 15; score++) {
            final Double probability =
                    poissonGoals.probability(score);
            if (score < 3) {
                probabilityUnderThreeGoals = probabilityUnderThreeGoals + probability;
            }
        }

        System.out.println(probabilityUnderThreeGoals); //prints 0.44596319855718064, I want to optimise this to 0.5
    }
}

【问题讨论】:

  • expectedGoals = 2.67406 怎么样(如果我理解正确的话)?另外,请注意您可以使用 cumulativeProbability(2) 并且不需要循环。
  • @Sweeper 是的,2.67406 是正确的值——但我想知道当试错不是那么简单时,如何为未来的案例实现优化算法。

标签: java optimization apache-commons solver apache-commons-math


【解决方案1】:

泊松随机变量的累积概率 (

在您的情况下,x 为 2,您希望找到 lambda(均值),使其为 0.5。您可以type this into WolframAlpha 并让它为您解决。因此,这不是一个优化问题,而是一个寻根问题(尽管有人可能会说优化问题只是寻根。)

您也可以使用 Apache Commons Maths 执行此操作,使用 root finders 之一。

int maximumGoals = 2;
double expectedProbability = 0.5;
UnivariateFunction f = x -> {
  double sum = 0;
  for (int i = 0; i <= maximumGoals; i++) {
    sum += Math.pow(x, i) / CombinatoricsUtils.factorialDouble(i);
  }
  return sum * Math.exp(-x) - expectedProbability;
};

// the four parameters that "solve" takes are:
// the number of iterations, the function to solve, min and max of the root
// I've put some somewhat sensible values as an example. Feel free to change them
double answer = new BisectionSolver().solve(Integer.MAX_VALUE, f, 0, maximumGoals / expectedProbability);
System.out.println("Solved: " + answer);
System.out.println("Cumulative Probability: " + new PoissonDistribution(answer).cumulativeProbability(maximumGoals));

打印出来:

Solved: 2.674060344696045
Cumulative Probability: 0.4999999923623868

【讨论】:

    猜你喜欢
    • 2014-05-01
    • 2014-08-06
    • 1970-01-01
    • 2015-01-21
    • 2012-04-23
    • 2016-02-28
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    相关资源
    最近更新 更多