# Black-Scholes in R

Here is a simple implementation of the Black-Scholes pricing formula in R. This will return a two-element vector containing the calculated call and put price, respectively.

# Black-Scholes Option Value
# Call value is returned in values[1], put in values[2]
blackscholes <- function(S, X, rf, T, sigma) {
values <- c(2)

d1 <- (log(S/X)+(rf+sigma^2/2)*T)/(sigma*sqrt(T))
d2 <- d1 - sigma * sqrt(T)

values[1] <- S*pnorm(d1) - X*exp(-rf*T)*pnorm(d2)
values[2] <- X*exp(-rf*T) * pnorm(-d2) - S*pnorm(-d1)

values
}


Example use:

> blackscholes(100,110,.05,1,.2) [1] 6.040088 10.675325 

# Useful Regular Expressions, Part 2

Some useful regexes here that I have used recently here, saved in case I ever lose them (especially the last two). Firstly, an expression that parses NTLM credentials from HTTP BASIC authentication responses. Note the non-capturing groups  ?: to keep indexing simple.

/* DOMAIN\\user(:pass)? or DOMAIN/user(:pass)? */
private static final Pattern CREDENTIALS_PATT = Pattern.compile("(?:(\\w+)(?:\\\\{1,2}|/))?(\\w+)(?::(\\w+))?");

Next, a regular expression to parse verbose garbage collection logs from JRockit GC log files:

s/^$memory$$$$[^]]\+$$$.*GC $$[0-9]\+$$K->$$[0-9]\+$$K ($$[0-9]\+$$K), $$[0-9]\+\.[0-9]\+$$ ms/\1,\2,\3,\4,\5/

This will return various columns including memory before and after collection, max heap size, and collection duration.

Similarly, for Sun JVMs:

s/.*GC $$[0-9]\+$$K->$$[0-9]\+$$K($$[0-9]\+$$K), $$[0-9]\+\.[0-9]\+$$ secs]/\1,\2,\3,\4/

Verbose GC logging is usually enabled via the -XXloggc option, e.g. -Xloggc:gc.log

# WordPress 2.5

I just upgraded to WordPress 2.5 and it was super easy. It took about four minutes. And the new admin interface is excellent!

# Commons::Net 2.0 Almost Ready For Release

After a long time, Commons::Net 2.0 is (hopefully) nearly ready for release. I’m cutting RC3 for 2.0 and 1.5.0 (a maintenance release) as I speak, and I have created the latest tags in SVN. This is a major release, which contains a large number of fixes, enhancements, and additions. It will in all likelihood be the last release that I will be able to devote a significant amount of time to, and even getting this far has been a challenge. Luckily, I’ve had great help from others (both Apache and other external developers).

One thing I have learned from developing parts of this package is the importance of a well-defined, unambiguous standard. The File Protocol Transfer standard, as defined in RFC 959 and others, offer a lot of room for interpretation. This means that vendors and implementors are free to fill in the blanks where the standard is fuzzy or unclear, making developing a comprehensive client challenging, and reproducing obscure issues very difficult.

Here is the list of changes in 2.0, plucked from the JIRA release notes (actually the changes report probably is a better indicator of a lot of additions).

## Bug

• [NET-24] – [net] Commons RLogin timeout
• [NET-28] – [PATCH] [net] patch of FTPS
• [NET-31] – [net] Freeze during FTP connect
• [NET-36] – [net] PATCH] FTP and FTPClient changes
• [NET-39] – [net] Solution for ant ftp fails with Nullpointerexception when a symlink is evaluated in method checkRemoteSensitivity in FTP.java for a z/OS ftp server
• [NET-59] – [net] NullpointerException on FTPClient.disconnect() if an Exception occured while FTPClient.connect
• [NET-65] – [net] program hangs while trying to delete a file on a remote FTP Server after downloading same
• [NET-68] – [net] TFTPClient’s send file discards last ack
• [NET-77] – [net] MVSFTPEntryParser setRawListing
• [NET-84] – [net] TelnetClient#disconnect() causes NullPointerException from Linux when connected to Windows 2000 Telnet Server
• [NET-139] – Trunk fails to build under JDK 1.6
• [NET-140] – FTPClient listFiles returns incorrect timestamp on freshly uploaded file but corrects itself after about 15 minutes
• [NET-142] – FTPClient.listFiles() returns null for entries with space in group name
• [NET-145] – Deadlock in TelnetInputStream
• [NET-146] – wrong handling of timeouts
• [NET-148] – Relaxed condition in __getReply causes other failures.
• [NET-155] – Integer is too small to hold article number value (NNTPClient __parseNewsgroupListEntry() function)
• [NET-158] – login in FTPClient does not always read the complete server response
• [NET-159] – FTPFile.getTimestamp() is off by one year
• [NET-161] – TFTP TFTPClient.sendFile() just doesen’t work
• [NET-169] – Cannot return files with listNames() function
• [NET-170] – UnixFTPEntryParser does not handle file owner names with spaces
• [NET-172] – NTP client should not bind to the local port when using UDP
• [NET-174] – if 150 Here comes directory listing comes before 200, then FTPClient throws exception
• [NET-177] – FTPClient fails to parse valid PASV responses
• [NET-178] – Support for unciode character is missing
• [NET-182] – NPE in DefaultFTPFileEntryParserFactory
• [NET-184] – FtpClient.listFiles truncates directory name beginning with a number
• [NET-193] – FTP throw NullPointerException when enountering 02-29-2008 (leap year bug)
• [NET-198] – FTPTimestampParserImpl#parseTimeStamp() is not fully testable
• [NET-201] – UnixFTPEntryParser does not handle character/block special devices properly

## Improvement

• [NET-119] – [net] allow listing of hidden files
• [NET-125] – [net] FTP does not work on zos (ebcdic platform)
• [NET-136] – [net] Request to add WindowSizeOptionHandler to the commons-net package
• [NET-141] – Add connection timeout support to SocketClient and/or SocketFactory/DefaultSocketFactory
• [NET-151] – [PATCH] Allow user to force passive connections to use connected host in FTPClient
• [NET-153] – Add getCause method to CopyStreamException
• [NET-154] – Typo in NNTPClient javadoc comment
• [NET-164] – WhoisClient should not use the platform’s default Encoding
• [NET-171] – Improve MVSFTPEntryParser.java
• [NET-181] – tftp client limited to ~32 MB file sizes
• [NET-183] – SMTPClient – Allow setting of socket send and recieve buffer size
• [NET-186] – [net] [PATCH] RegexFTPFileEntryParserImpl – Removed dependency on org.apache.oro.text.regex packages
• [NET-196] – POP3Reply.OK and .ERROR should be final; likewise the psi fields in TelnetOption
• [NET-205] – Allow FTPFile entries to be generated even if date parsing fails

## New Feature

• [NET-187] – TFTP Server for commons net

• [NET-179] – NET-73 "TelnetInputStream._read() hangs" fix is not included in nightly builds.

## Wish

• [NET-157] – Get "hidden files" over ftp, using commons-vfs

# Useful Vim Regular Expression

I just edited a patch file and needed to change the paths of the patch directories (why do some patch implementations use relative paths, and others use absolute)? Anyway, I wanted to change all paths like ./src/main/org/apache to be ./src/main/java/org/apache and all test class paths to be ./src/test/org/apache to be ./src/test/java/org/apache. Anyways, it occured to me that if we make the assumption that all test classes will have *Test* somewhere in the name, we can construct positive and negative lookahead regexes in Vim, as follows:

Negative Lookahead (change all paths that dont contain “Test”):
:%s/$$src\/main$$$$.*Test$$\@!/src\/main\/java/g

Positive Lookahead (change all paths that do contain “Test”):
:%s/$$src\/test$$$$.*Test$$\@=/src\/test\/java/g

Very handy! The only slight annoyance is that Vim doesnt go with Perl’s more readable lookaround syntax.