MaheshT.com

December 27, 2009

The time value of money

Filed under: Finance — MaheshT @ 10:42 pm

1. The future value of a single cash flow.

For yearly compounding:
 F_{v} = P_{v}(1+r)^N

if compounding is more than once a year
F_{vn} = P_{v}( 1+ \frac{r}{m})^ {rN}

For continues compounding
Fvn = Pv epsilon^(rN)

Where r = interest rate per period.
N= Number of the compounding period.
m= Number of compounding periods per year.

Effective annual interest rate:
EAR = (1+Periodic interest rate)^m - 1

2. Future value of a series of cash flows

Annuity: It is a finite set of fixed payments over a period of time.
An ordinary annuity is an annuity whose payments are made at the end of each period (t = 1)
An annuity-due is an annuity whose payments are made at the beginning of each period. (t = 0)

Future value of annuity
Fvn = A[((1+r)^N -1) /r]

3. Present Value of Single Cash Flow

Pv = Fv[1/(1+r)^N]
which is equivalent to
Pv = Fv(1+r)^(- N)

With Frequency of compounding
Pv = Fv(1+r/m)^(- mN)

4. Present value of series of cash flows

Pv = A[ (1 - 1/(1+r)^N)/r]
which is same as
Pv = A[ (1 - (1+r)^(- N))/r]

5. Growth rate which is same as interest rate

g = (Fv/Pv)^(1/N) -1

Looping from unix command line

Filed under: Linux — MaheshT @ 8:05 pm
You can directly run a loop from the command line instead of running command repeatedly or writing a shell script.
For example to untar all files from the home folder as follows.
for i in ls ~/home/*.tar ; do tar -xvf $i ; done 

Java 5

Filed under: Java — MaheshT @ 7:29 pm

Some of the new features added in Java 5

Generic:
While declaring collection objects you can specify the type of objects the collection can hold. We have the following advantages
1. You don’t have to worry about accidentally putting a different object in the collection which will result in runtime ClassCastException, as the compiler will detect those while compiling the code.
2.  While getting an object from the collection, you don’t have to typecast to the class.

Sample code:


HashMap<String, Country>  countryMap = new HashMap<String, Country> ();
countryMap.put("US", new Country("US","United States Of America","Washington"));
countryMap.put("IN", new Country("IN","India","Delhi"));
//... some code here.............
// now get country by country  code
Country usa = countryMap.get("US");     /* Note: No need to typecast to class here as in java 1.4 */

Enhanced Looping:
Let’s understand from the code sample

HashMap<String, Country>  countryMap = new HashMap<String, Country>();
countryMap.put("US", new Country("US","United States Of America","Washington"));
countryMap.put("IN", new Country("IN","India","Delhi"));
//... some code here.............
// Iterate through each country
// Code in 1.4
Collection countryList = countryMap.values();
for( Iterator countryIte = countryList.iterator();countryIte.hasNext();){
Country country = (Country)countryIte.next();
System.out.print("Country :" + country.getName() + " Capital :" + country.getCapital());
}

// Code in 1.5
for (Country country : countryMap.values()){
System.out.print("Country :" + country.getName() + " Capital :" + country.getCapital());
}

Similarly you can use it in array.

// Returns the sum of the elements of a
int sum(int[] a) {
int result = 0;
for (int i : a)
result += i;
return result;
}

Autoboxing:
In java 1.4, if you have to put primitive type to Collection, you need to wrap that primitive to corresponding wrapper class. For example, if you want to put int key and value to HashMap,
you have to code as follows.

HashMap employeeMap = new HashMap();
employeeMap.put(new Integer(empId), new Employee("Name");

In java 1.5 same code you can write as

HashMap<Integer,Employee> employeeMap = new HashMap<Integer,Employee>();
// put employee
employeeMap.put(empId, new Employee("Name")); // no need to wrap empId in Integer
// get employee
Employee emp = employeeMap.get(empId);

Note: It’s important to note that the Key will be added as Integer to HashMap, but now it’s done by JVM, so there is some performance cost. If your code is performance-critical, then it’s a good idea to wrap primitive type to its corresponding wrapper classes.

Powered by WordPress