Example: Internal Rate of Return - Variable Schedule

A farmer buys 10 young cows and a bull for $4500. The first year he does not expect to sell any calves, he just expects to feed them. Thereafter, he expects to be able to sell calves to offset the cost of feed. He expects them to be productive for 9 years, after which time he will liquidate the herd. The internal rate of return is computed after 9 years.

import com.imsl.finance.*;
import java.text.*;
import java.util.*;

public class xirrEx1 {

    static final DateFormat dateFormat
            = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);

    private static Date parse(String s) throws ParseException {
        return dateFormat.parse(s);
    }

    public static void main(String args[]) throws ParseException {
        double[] pmt = {-4500., -800., 800., 800., 600., 600.,
            800., 800., 700., 3000.};
        Date dates[] = {
            parse("1/1/98"), parse("10/1/98"), parse("5/5/99"),
            parse("5/5/00"), parse("6/1/01"), parse("7/1/02"),
            parse("8/30/03"), parse("9/15/04"), parse("10/15/05"),
            parse("11/1/06")
        };
        double xirr = Finance.xirr(pmt, dates);
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMaximumFractionDigits(2);
        System.out.println("After approximately 9 years, the internal rate "
                + "of return on the cows is " + nf.format(xirr));
    }
}

Output

After approximately 9 years, the internal rate of return on the cows is 7.69%
Link to Java source.