I want to calculate some stuff with Poisson. The Problem is, it works just to a value from 183...
my Code:
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
public class CopyOfPoisson {
public static void main(String[] args) {
double anrufe = 360;
double minuten = 30;
double dauer = 240;
double agenten = 0; //m
for(int i = 49; i<=500; i++)
{
agenten = i;
double callsPerSecond = anrufe/(minuten*60);
//u
double trafficIntensitiy = callsPerSecond*dauer;
//p
double p = trafficIntensitiy / agenten;
System.out.println("Index: "+ i + " "+ erlangC(agenten,trafficIntensitiy, p));
}
}
public static BigDecimal erlangC (double m, double u, double p)
{
BigDecimal zaehler = berechnePoisson(m, u, false);
BigDecimal nenner = berechnePoisson(m, u, false).add((BigDecimal.ONE.subtract(new BigDecimal(p))).multiply( berechnePoisson(m, u, true)));
return zaehler.divide(nenner,100,RoundingMode.HALF_EVEN);
}
public static BigDecimal berechnePoisson(double m, double u, boolean va)
{
BigDecimal answer = BigDecimal.ZERO;
if(!va)
{
BigDecimal myOwn = new BigDecimal(Double.toString(Math.exp(-u)* Math.pow(u, m)));
answer = myOwn.divide(fakultaet2(m),100,RoundingMode.HALF_EVEN);
}
if(va)
{
for(int i = 0; i < m; i++)
{
BigDecimal myOwn2 = new BigDecimal(Double.toString(Math.exp(-u)*Math.pow(u, i)));
answer = answer.add(myOwn2.divide(fakultaet2(i),100,RoundingMode.HALF_EVEN));
}
}
return answer;
}
public static BigDecimal fakultaet2 (double n)
{
BigDecimal fct = BigDecimal.valueOf(1);
for(int i = 1; i<=n; i++)
{
fct = fct.multiply(BigDecimal.valueOf(i));
}
return fct;
}
}
After the loop reaches 184 it gives me an Error:
Exception in thread "main" java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:494)
at java.math.BigDecimal.<init>(BigDecimal.java:383)
at java.math.BigDecimal.<init>(BigDecimal.java:806)
at CopyOfPoisson.berechnePoisson(CopyOfPoisson.java:47)
at CopyOfPoisson.erlangC(CopyOfPoisson.java:36)
at CopyOfPoisson.main(CopyOfPoisson.java:26)
on the Line
BigDecimal myOwn = new BigDecimal(Double.toString(Math.exp(-u)* Math.pow(u, m)));
What is the problem? I want to calculate till i <= 500
, but only calculates to i = 183
.
Do everything using BigDecimal. For example, use BigDecimal.pow() instead of Math.pow(). Or clean up the algorithm - there is no need to use pow() or exp() when computing Poisson (I showed how in an earlier thread on Poisson today)