Created
November 29, 2011 18:07
-
-
Save rrguntaka/1405757 to your computer and use it in GitHub Desktop.
StdDev of a portfolio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Matrix { | |
Double[][] data; | |
public Matrix(int m, int n) { | |
data = new Double[n][m]; | |
} | |
Matrix(Double[][] data) { | |
this.data = data; | |
} | |
Matrix transpose() { | |
if (data == null) { | |
System.out.println("invalid Matrix"); | |
} else if (data.length < 1) { | |
System.out.println("Atleast one row should be there"); | |
} else if (data[0].length == 0) { | |
System.out.println("Atleast one column should be there"); | |
} | |
Double[][] temp = new Double[data[0].length][data.length]; | |
for (int i = 0; i < data.length; i++) { | |
for (int j = 0; j < data[0].length; j++) { | |
temp[j][i] = data[i][j]; | |
} | |
} | |
return new Matrix(temp); | |
} | |
public Matrix multiply(Matrix x) { | |
Double[][] temp = new Double[data.length][x.data.length]; | |
for (int i = 0; i < data.length; i++) { | |
for (int j = 0; j < x.data.length; j++) { | |
temp[i][j] = 0.0; | |
for (int j2 = 0; j2 < x.data[0].length; j2++) { | |
temp[i][j] += data[i][j] * x.data[j][j2]; | |
} | |
} | |
} | |
return new Matrix(temp); | |
} | |
@Override | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < data.length; i++) { | |
for (int j = 0; j < data[0].length; j++) { | |
sb.append(data[i][j] + "\t"); | |
} | |
sb.append("\n"); | |
} | |
return sb.toString(); | |
} | |
public static void main(String[] args) { | |
Matrix a = new Matrix(new Double[][] { new Double[] { 0.1, 0.5 } }); | |
System.out.println(a); | |
System.out.println(a.transpose()); | |
System.out.println(a.transpose().multiply(a)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PortfolioAnalysis { | |
public static void main(String[] args) { | |
for (int i = 0; i <= 25; i++) | |
getRetStdDev(i / 25.0); | |
} | |
private static void getRetStdDev(Double weight1) { | |
Double covariance = 0.5; | |
Double[] weights = new Double[] { weight1, 1 - weight1 }; | |
Double[] returns = new Double[] { 0.15, 0.05 }; | |
Double[] sigmas = new Double[] { 0.15, 0.10 }; | |
Double[][] covariances = new Double[][] { | |
new Double[] { 1.0, covariance }, | |
new Double[] { covariance, 1.0 } }; | |
Double pReturn = 0.0; | |
for (int i = 0; i < weights.length; i++) { | |
pReturn += weights[i] * returns[i]; | |
} | |
Double pVariance = 0.0; | |
for (int i = 0; i < weights.length; i++) { | |
for (int j = 0; j < weights.length; j++) { | |
pVariance += weights[i] * weights[j] * sigmas[i] * sigmas[j] | |
* covariances[i][j]; | |
} | |
} | |
System.out.println(String.format("%1$3.5f %2$3.5f", pReturn, | |
Math.sqrt(pVariance))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment