//================================================================C // 2株式のリスク、リターン及び相関係数の計算 // Input File: P-code.txt : プログラムと同じディレクトリ // 株価コード(20件以内)を記載 // Output File: P2-out.txt : プログラムと同じディレクトリ //----------------------------------------------------------------C // javac Correlation.java : classファイルの作成 // java Correlation : 実行 //----------------------------------------------------------------C // Written by Yasunori Ushiro (後 保範), 2011/11/09 // ( Waseda University, 早稲田大学 ) //================================================================C import java.io.*; import java.util.*; public class MCorrelation { public static double marume(double x, int n, int id) { double y, nd, s; nd = n; // id=1 --> % value (y=100*x) if (id == 1) { nd = n/100; } s = 0.5; if (x < 0.0) { s = -0.5; } y = (double)( (int)(x*n + s) ) / nd; return y; } public static void main (String[] args) throws Exception { String s, std, code; String[] Ary, Wk; int i, j, k, n, NO, sno; int start, end; String scode[] = new String[20]; double ret[][] = new double[20][300]; double myu[] = new double[20]; double sigm[] = new double[20]; double rr[][] = new double[20][20]; // Input stock-code FileInputStream fic = new FileInputStream("P-code.txt"); InputStreamReader inc = new InputStreamReader(fic); BufferedReader bc = new BufferedReader(inc); // Output File FileOutputStream fo = new FileOutputStream("P2-out.txt"); PrintWriter q = new PrintWriter(fo,true); // Main loop sno = 0; n = 0; for (k=0; k<20; k++) { String incode = bc.readLine(); if ( incode== null ) { break; } Wk = incode.split(" "); Ary = Wk[0].split(" "); code = Ary[0]; scode[k] = code; // Stock file FileInputStream fi = new FileInputStream("M-"+code+".txt"); InputStreamReader in = new InputStreamReader(fi); BufferedReader b = new BufferedReader(in); int nn = 0; // file read and 月別リターン(ret)の計算 while ( (s = b.readLine()) != null) { StringTokenizer st = new StringTokenizer(s); NO = 0; start = end = 0; while (st.hasMoreTokens()) { std = st.nextToken(); if(NO==1) { start = Integer.parseInt(std); } if(NO==4) { end = Integer.parseInt(std); } NO++; } ret[k][nn] = (double)(end - start) / (double)start; nn++; } sno++; n = nn; } // 各株のリターンの計算 for (k=0; k