'''
Created by Deniz Turan
Oracle Java (tested on 7), young generation garbage collection activity scraper.
Extracts following fields from GC log file and save to a csv file.
Count,LogTime,logGCOffsetTime,logGCOffsetTime2,
YGPreSize,YGPostSize,YGTotalSize, YGElapsedTime, # Young generation
OLDPreSize,OLDPostSize,OLDTotalSize,OLDElapsedTime # Old generation
'''
from subprocess import call
import glob
import os
logDir="C:\\temp\\gc\\"
finalResultFileName=logDir+"finalResults.csv"
filterExtension="*.log";
def getLogFileList(search_dir):
files = filter(os.path.isfile, glob.glob(search_dir + filterExtension))
files.sort(key=lambda x: os.path.getmtime(x))
return files
def openResultFile():
print "Creating result file : %s"% (finalResultFileName)
# remove previous file
call("rm "+finalResultFileName,shell=True)
resultFileFD = open( finalResultFileName ,"a")
## create header
resultFileFD.write("Count,LogTime,logGCOffsetTime,logGCOffsetTime2,")
resultFileFD.write("YGPreSize,YGPostSize,YGTotalSize, YGElapsedTime,")
resultFileFD.write("OLDPreSize,OLDPostSize,OLDTotalSize,OLDElapsedTime\n")
return resultFileFD
def closeResultFile(resultFileFD):
print "Closing result file "
resultFileFD.close();
def getFieldValue(strVal):
index=strVal.index("K")
index2=strVal.index("K", index+1)
index3=strVal.index("K", index2+1)
part1=strVal[:index]
part2=strVal[index+3:index2]
part3=strVal[index2+2:index3]
return (part1,part2,part3)
#####################################################
# Main
#####################################################
if __name__ == '__main__':
# prepare result file
resultFileFD=openResultFile ()
print "Started to process log files"
logFileList=getLogFileList(logDir)
count=0
for f in logFileList:
print "Processing GC Log file %s"%f
logFD = open(f)
line = logFD.readline()
while (line != "" ):
if "ParNew" in line :
count=count+1
fields=line.split(" ")
logTime=fields[0]
logGCOffsetTime=fields[1]
logGCOffsetTime2=fields[3]
res=getFieldValue(fields[5])
YGPreSize,YGPostSize,YGTotalSize=res
YGElapsedTime=fields[6]
res=getFieldValue(fields[8])
OLDPreSize,OLDPostSize,OLDTotalSize=res
OLDElapsedTime=fields[9]
print line
print "%d %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"%(count,logTime,logGCOffsetTime,logGCOffsetTime2, \
YGPreSize,YGPostSize,YGTotalSize, YGElapsedTime,\
OLDPreSize,OLDPostSize,OLDTotalSize,OLDElapsedTime)
# print to file as CSV now
resultFileFD.write("%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n"%(count,logTime,logGCOffsetTime,logGCOffsetTime2, \
YGPreSize,YGPostSize,YGTotalSize, YGElapsedTime,\
OLDPreSize,OLDPostSize,OLDTotalSize,OLDElapsedTime))
line = logFD.readline()
continue
logFD.close();
closeResultFile(resultFileFD);
print "finished processing log files"
pass
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Friday, November 22, 2013
Java Garbage Collection Log File Scraper
Labels:
Garbage Collection Log File,
java,
Python
Wednesday, August 19, 2009
Pricing European Options by using Binomial Model
Binomial Model is one of the simplest pricing model for European options. Below you can find an implementation of Cox-Ross-Rubinstein (CRR) approach [1] in java for pricing of put and call European options.
With following parameters, sensitivity of the binomial pricing model as function of number of time steps can be seen in figure 1. With high number of time steps, binomial model with CRR approach converges with optimal Black-Scholes formula (e.x: 30.741 for call option)
With following parameters, sensitivity of the binomial pricing model as function of number of time steps can be seen in figure 1. With high number of time steps, binomial model with CRR approach converges with optimal Black-Scholes formula (e.x: 30.741 for call option)
1: package com.denizstij.finance.pricing;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: /**
7: *
8: * Estimate European Option price based on Cox, Ross and Rubinstein model
9: * @author denizstij (http://denizstij.blogspot.com/)
10: *
11: */
12: public strictfp class EuropeanOptionPricingByBinomial {
13:
14: /**
15: * Estimate European Option price based on Cox, Ross and Rubinstein model
16: *
17: * @param asset Current Asset Price
18: * @param strike Exercise Price
19: * @param volatility Annual volatility
20: * @param intRate Annual interest rate
21: * @param expiry: Time to maturity (in terms of year)
22: * @param steps : Number of steps
23: * @return Put and call price of european options based on Cox, Ross and Rubinstein model
24: */
25: public List<Double> estimatePrice(double asset,
26: double strike,
27: double volatility,
28: double intRate,
29: double expiry,
30: int steps) {
31: List<Double> results = new ArrayList<Double>();
32:
33: List<Double> stockPrices = new ArrayList<Double>();
34: List<Double> callOptionPrices = new ArrayList<Double>();
35: List<Double> putOptionPrices = new ArrayList<Double>();
36:
37: double time_step = (expiry) / steps;
38: double R = Math.exp(intRate * time_step);
39: double dF = 1 / R; // discount Factor
40:
41: double u = Math.exp(volatility * Math.sqrt(time_step)); // up boundary
42: double d = 1 / u; // down boundary (Cox, Ross and Rubinstein constraint)
43: // at leaf node, price difference factor between each node
44: double uu = u * u; // (u*d)
45: double p_up = (R - d) / (u - d); // up probability
46: double p_down = 1 - p_up; // down probability
47:
48: // initiliaze stock prices
49: for (int i = 0; i <= steps; i++) {
50: stockPrices.add(i, 0.0d);
51: }
52:
53: double sDown = asset * Math.pow(d, steps);
54: stockPrices.set(0, sDown);
55:
56: // Estimate stock prices in leaf nodes
57: for (int i = 1; i <= steps; i++) {
58: double sD = uu * stockPrices.get(i - 1);
59: stockPrices.set(i, sD);
60: }
61:
62: // estimate option's intrinsic values at leaf nodes
63: for (int i = 0; i <= steps; i++) {
64: double callOptionPrice = callPayOff(stockPrices.get(i), strike);
65: callOptionPrices.add(i, callOptionPrice);
66: double putOptionPrice = putPayOff(stockPrices.get(i), strike);
67: putOptionPrices.add(i, putOptionPrice);
68: }
69:
70: // and lets estimate option prices
71: for (int i = steps; i > 0; i--) {
72: for (int j = 0; j <= i - 1; j++) {
73: double callV = dF*(p_up * callOptionPrices.get(j + 1) +
74: p_down* callOptionPrices.get(j));
75: callOptionPrices.set(j, callV);
76: double putV = dF*(p_up * putOptionPrices.get(j + 1) +
77: p_down * putOptionPrices.get(j));
78: putOptionPrices.set(j, putV);
79: }
80: }
81:
82: // first elements holds option's price
83: results.add(callOptionPrices.get(0));
84: results.add(putOptionPrices.get(0));
85: return results;
86: }
87:
88: // Pay off method for put options
89: private double putPayOff(double stockPrice, double strike) {
90: return Math.max(strike - stockPrice, 0);
91: }
92:
93: // Pay off method for call options
94: private double callPayOff(double stockPrice, double strike) {
95: return Math.max(stockPrice - strike, 0);
96: }
97:
98: public static void main(String args[]) {
99:
100: EuropeanOptionPricingByBinomial euOptionPricing = new EuropeanOptionPricingByBinomial();
101: List<Double> results = euOptionPricing.estimatePrice(
102: 230,
103: 210,
104: 0.25,
105: 0.04545,
106: 0.5, // In terms of year
107: 10);
108: Double callOptionPrice = results.get(0);
109: Double putOptionPrice = results.get(1);
110: System.out.println("call Option Price:" + callOptionPrice);
111: System.out.println("put Option Price:" + putOptionPrice);
112: }
113: }
114:
Labels:
algorithms,
derivatives,
finance,
java,
options,
pricing,
source code
Subscribe to:
Posts (Atom)
