Permalink
Browse files

Import from http://www.satpathy.in/jxirr/JXIRR-1.0.0.zip

  • Loading branch information...
Martin Kleppmann
Martin Kleppmann committed Jul 10, 2009
0 parents commit 971ef23d10bd79ca589ac153990015aeb9233bac

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,40 @@
+=======================================================================
+JXIRR - v1.0.0 - 19/10/2005 - Readme File -
+=======================================================================
+
+(C) 2005 Gautam Satpathy
+Released under GNU Lesser General Public License (see jxirr-license.txt)
+gautam@satpathy.in
+www.satpathy.in
+
+=======================================================================
+THANKS
+
+I would like to thank all the people who have sent e-mails and
+provided bug fixes and suggestions.
+
+=======================================================================
+UPDATES
+
+I am not actively working on this at this time. Suggestions, bugs, requests for updates
+are welcome. Please write to me at gautam@satpathy.in
+
+Check the web site to stay informed about the updates:
+ http://www.satpathy.in
+
+=======================================================================
+CONTENTS
+
+At the top level you find this file and a text version of the LGPL license.
+
+This distribution contains several sub-directories.
+They are listed and descibed below:
+
+ src\ contains all the sources to make a distribution.
+
+
+=======================================================================
+
+Gautam Satpathy
+gautam@satpathy.in
+www.satpathy.in
@@ -0,0 +1,4 @@
+Manifest-Version: 1.0
+Main-Class: in.satpathy.financial.TestXIRR
+
+
@@ -0,0 +1,79 @@
+/*
+ * TestXIRR.java
+ * Copyright (C) 2005 Gautam Satpathy
+ * gautam@satpathy.in
+ * www.satpathy.in
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package in.satpathy.financial;
+
+/*
+ * Imports
+ */
+import java.util.GregorianCalendar;
+
+/**
+ * @author : gsatpath
+ * @version : 1.0.0 Date: Oct 20, 2005, Time: 4:39:25 AM
+ */
+public class TestXIRR {
+
+ /**
+ *
+ * @param args
+ */
+ public static void main( String[] args ) {
+ log( "Testing XIRR..." ) ;
+
+// GregorianCalendar dateStart = new GregorianCalendar( 1899, 11, 30 ) ;
+ GregorianCalendar dateEnd = new GregorianCalendar( 2005, 9, 20 ) ;
+ int daysBetween = XIRRData.getExcelDateValue( dateEnd ) ;
+ log( "Days Between = " + daysBetween ) ;
+
+// "Let us assume that the cells A1:A5 contain the numbers -6000, "
+// "2134, 1422, 1933, and 1422, and the cells B1:B5 contain the "
+// "dates \"1999-01-15\", \"1999-04-04\", \"1999-05-09\", "
+// "\"2000-03-12\", and \"2000-05-1\". Then\n"
+// "XIRR(A1:A5,B1:B5) returns 0.224838. "
+ double[] values = new double[5] ;
+ double[] dates = new double[5] ;
+ values[0] = -6000 ;
+ values[1] = 2134 ;
+ values[2] = 1422 ;
+ values[3] = 1933 ;
+ values[4] = 1422 ;
+ dates[0] = XIRRData.getExcelDateValue( new GregorianCalendar(1999, 0, 15) ) ;
+ dates[1] = XIRRData.getExcelDateValue( new GregorianCalendar(1999, 3, 4) ) ;
+ dates[2] = XIRRData.getExcelDateValue( new GregorianCalendar(1999, 4, 9) ) ;
+ dates[3] = XIRRData.getExcelDateValue( new GregorianCalendar(2000, 2, 12) ) ;
+ dates[4] = XIRRData.getExcelDateValue( new GregorianCalendar(2000, 4, 1) ) ;
+ XIRRData data = new XIRRData( 5, 0.3, values, dates ) ;
+ double xirrValue = XIRR.xirr( data ) ;
+ log( "XIRR = " + xirrValue ) ;
+
+ log( "XIRR Test Completed..." ) ;
+ }
+
+
+ /**
+ *
+ * @param message
+ */
+ public static void log( String message ) {
+ System.out.println( message ) ;
+ }
+
+} /* End of the TestXIRR class. */
@@ -0,0 +1,81 @@
+/*
+ * XIRR.java
+ * Copyright (C) 2005 Gautam Satpathy
+ * gautam@satpathy.in
+ * www.satpathy.in
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package in.satpathy.financial;
+
+/*
+ * Imports
+ */
+import in.satpathy.math.GoalSeek ;
+import in.satpathy.math.GoalSeekData ;
+import in.satpathy.math.GoalSeekStatus ;
+
+/**
+ * XIRR implementation
+ *
+ * @author : gsatpath
+ * @version : 1.0.0 Date: Oct 19, 2005, Time: 12:09:58 PM
+ */
+public class XIRR {
+
+ /*
+ * Excel stores dates as sequential serial numbers so they can be used
+ * in calculations. By default, January 1, 1900 is serial number 1, and
+ * January 1, 2008 is serial number 39448 because it is 39,448 days
+ * after January 1, 1900.
+ */
+
+ /**
+ * Calculate XIRR.
+ *
+ * @param xirrData
+ * @return
+ */
+ public static double xirr( XIRRData xirrData ) {
+ GoalSeekData data ;
+ GoalSeekStatus status ;
+ double result ;
+ double rate0 ;
+ int n ;
+ int d_n ;
+
+ data = new GoalSeekData() ;
+ GoalSeek.goal_seek_initialize( data ) ;
+ data.xmin = -1;
+ data.xmax = Math.min( 1000, data.xmax ) ;
+ rate0 = xirrData.guess ; //argv[2] ? value_get_as_float (argv[2]) : 0.1;
+
+ status = GoalSeek.goalSeekNewton(
+ new XIRRNPV(), null, data, xirrData, rate0 ) ;
+
+ if (status.seekStatus == GoalSeekStatus.GOAL_SEEK_OK) {
+// result = value_new_float(data.root);
+ result = ((Double) status.returnData).doubleValue() ; //data.root ;
+ }
+ else {
+// result = value_new_error_NUM (ei.pos);
+ result = Double.NaN ;
+ }
+
+ System.out.println( "XIRR Result - " + result ) ;
+ return (result != Double.NaN) ? (result - 1) : result ;
+ }
+
+} /* End of the XIRR class. */
@@ -0,0 +1,149 @@
+/*
+ * XIRRData.java
+ * Copyright (C) 2005 Gautam Satpathy
+ * gautam@satpathy.in
+ * www.satpathy.in
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+package in.satpathy.financial;
+
+/*
+ * Imports
+ */
+import java.util.Calendar ;
+import java.util.GregorianCalendar ;
+
+/**
+ * Data structure to hold XIRR data.
+ *
+ * @author : gsatpath
+ * @version : 1.0.0 Date: Oct 19, 2005, Time: 9:29:49 AM
+ */
+public class XIRRData {
+
+ public int n ;
+ public double guess ;
+ public double[] values ;
+ public double[] dates ;
+
+ /**
+ * Default Constructor.
+ */
+ public XIRRData() {
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param n
+ * @param guess
+ * @param pValues
+ * @param pDates
+ */
+ public XIRRData( int n, double guess, double[] pValues, double[] pDates ) {
+ this.n = n;
+ this.guess = guess ;
+ this.values = pValues;
+ this.dates = pDates;
+ }
+
+ /**
+ * Returns the same value as Excel's DataValue method.
+ *
+ * @param dateStr
+ * @return
+ */
+// public static int getExcelDateValue( String dateStr ) {
+// GregorianCalendar dateStart = new GregorianCalendar( 1899, 11, 30 ) ;
+// return getDaysBetween( dateStart, date ) ;
+// }
+
+ /**
+ * Returns the same value as Excel's DataValue method.
+ *
+ * @param date
+ * @return
+ */
+ public static int getExcelDateValue( Calendar date ) {
+ GregorianCalendar dateStart = new GregorianCalendar( 1899, 11, 30 ) ;
+ return getDaysBetween( dateStart, date ) ;
+ }
+
+ /**
+ * Calculates the number of days between two calendar days in a manner
+ * which is independent of the Calendar type used.
+ *
+ * @param d1 The first date.
+ * @param d2 The second date.
+ *
+ * @return The number of days between the two dates. Zero is
+ * returned if the dates are the same, one if the dates are
+ * adjacent, etc. The order of the dates
+ * does not matter, the value returned is always >= 0.
+ * If Calendar types of d1 and d2
+ * are different, the result may not be accurate.
+ */
+ public static int getDaysBetween( Calendar d1, Calendar d2 ) {
+ if ( d1.after(d2) ) {
+ // swap dates so that d1 is start and d2 is end
+ Calendar swap = d1;
+ d1 = d2;
+ d2 = swap;
+ }
+
+ int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
+ int y2 = d2.get(Calendar.YEAR);
+ if (d1.get(Calendar.YEAR) != y2) {
+ d1 = (Calendar) d1.clone();
+ do {
+ days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
+ d1.add(Calendar.YEAR, 1);
+ } while (d1.get(Calendar.YEAR) != y2);
+ }
+ return days;
+ }
+
+
+
+ /**
+ * Expensive method. Don't call in loops etc.
+ *
+ * @return
+ */
+ public String toString() {
+ String text ;
+ String valuesStr ;
+ String datesStr ;
+
+ text = "XIRRData - n = " + n + ", Guess = " + this.guess ;
+ valuesStr = ", Values = " ;
+ datesStr = ", Dates = " ;
+ for ( int i = 0; i < this.values.length; i++ ) {
+ valuesStr = valuesStr + this.values[i] ;
+ if ( i < this.values.length - 1 ) {
+ valuesStr = valuesStr + "," ;
+ }
+ }
+ for ( int i = 0; i < this.dates.length; i++ ) {
+ datesStr = datesStr + this.dates[i] ;
+ if ( i < this.dates.length - 1 ) {
+ datesStr = datesStr + "," ;
+ }
+ }
+ return text + valuesStr + datesStr ;
+ }
+
+} /* End of the XIRRData class. */
Oops, something went wrong.

0 comments on commit 971ef23

Please sign in to comment.