0

I have a mortality risk calculation that requires the determination of natural logs and exp decay. The method is detailed below. I am curious to know why the methods for calculating exp etc are not static and if I use a single Log (or Exp) object for multiple arguments if there is a risk of incorrect calculations. That is can I use a single instance of Log to calculate Log (2.3) and Log (567)?

public Double getROD(Integer score) {

        Double beta0 = new Double(-7.7631);
        Double beta1 = new Double(0.0737);
        Double beta2 = new Double(0.9971);

        Log log = new Log();
        Exp exp = new Exp();

        Double logit = beta0 + (beta1 * score) + (beta2 * log.value(new Double(score + 1)));
        Double mortality = exp.value(logit) / (1 + exp.value(logit));

        return mortality;
    }

I have tried the following:

Log log = new Log();

Double arg1 = log.value(new Double(12.3));
Double arg2 = log.value(new Double(29.12));

System.out.println(arg1.toString() + " : " + arg2.toString());

which results in

2.509599262378372 : 3.3714252233284854

However, there is still a question as to this being the intended behaviour across all uses.

skyman
  • 2,255
  • 4
  • 32
  • 55
  • I should have added that I have tried that and the results are consistent, however does not necessarily mean that the class will always behave that way. I am also interested in the reasons for not using static methods. – skyman Jul 09 '14 at 02:30
  • Mostly, par design. Other libraries such as `GSON` also favors instances for operations instead. – Unihedron Jul 09 '14 at 02:31

2 Answers2

3

The Exp, Log, etc. classes store no state. The reason their value methods are not static is to fulfil the UnivariateFunction interface. So, yes, you can safely reuse the objects.

One of the nice things about the UnivariateFunction interface is that you can write a function that takes such an object, and the user can parameterise your function by passing in an appropriate function object. This concept is called higher-order functions (if you come from the FP camp, as I do) or strategy pattern (if you come from the OOP camp).

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 2
    Thank you - this is really appreciated. There needs to be a level of confidence in calculating risk of death! – skyman Jul 09 '14 at 02:34
1

If you just want to calculate the log and exp of a value it is usually better to use FastMath which provides static method for this and other basic mathematical functions (similar to Math but providing a pure java implementation and being faster for some of them).

T. Neidhart
  • 6,060
  • 2
  • 15
  • 38