Pages

Friday, December 25, 2015

Java Native Interface

JNI supports two way interaction between Java and other languages, such as C++, C, Assembly.


Here I describe how to use JNI for Java/C++ 2-way Calls:
 1) Create a Java Project with MyExample.java with native methods.
     run "javah -jni MyExample" to generate MyExample.h

 2) Create a C++ DLL Project with MyDLL.cpp(#include "MyExample.h")
     In Project Properties, add Additional Include Directories:
            C:\Java\jdk1.7.0_79\include\win32;C:\Java\jdk1.7.0_79\include

 3) In Java project, add MyDLL.dll to Native Library Location.
     In Java code, add System.loadLibrary("MyDLL");


Note: A common run-time error when using JNI is java.lang.UnsatisfiedLinkError, which may be caused by 32-bit/64-bit DLL version mismatch or function signature mismatch. Here is a few tips to deal with this issue.
  • 64-bit JDKs usually work with 64-bit DLLs, while 32-bit JDKs work with 32-bit DLLs.
    Although java provides options -d32 or -d64, it's not always working.
  • To check if a DLL is a 32-bit, run "vcvarsall.bat amd64" and
    "dumpbin.exe \headers MyDLL.dll" 
  • One code base can support both 32-bit and 64-bit platforms.
The tools used in the above discussion: Java 1.7+, Eclipse(Mars) for Java, VC++(2013) for C++.

For best practice of using JNI, I recommend one article on IBM website: "Techniques and tools for averting the 10 most common JNI programming mistakes".

Note: Java is newer than C++, but they are influencing each other since the beginning. Wikipedia provides an excellent comparison of the big two, here is the link.

Thursday, December 24, 2015

QuantLib

1, QuantLib can be built with VC++ 2013, as instructed at http://quantlib.org/install/vc10.shtml

2, QuantLib is a well-designed C++ library and ported to R and Python.
    Check out the training material at http://www.implementingquantlib.com/p/training.html

3, Serious about QuantLib? Get a copy of <<Implementing QuantLib>> written by Luigi Ballabio.

4, Quick view of Class Hierarchy: http://yetanotherquant.de/QuantLib/book/BookQuantLib.pdf





Monday, December 21, 2015

Fun with Linux

1) Remove ^M
    $vi file.txt
    >:%s/^V^M//g

2) 64 bit or 32 bit?
    $uname -a
    $lscpu
    $more /proc/cpuinfo
    $nproc

3) Copy and preserving the same mode including timestamp
    $cp -p a.txt b.txt
    $cp -a a b

4) Who logged, from where?
    $w
    $last myuserid

5) Compiling C++ code?
    $g++ -shared ...
    $g++ -static-libgcc -L. -o hello hello.cpp
    $ar mylib.a mylib.o

6) Sort in reverse order (find, grep,...)
    $sort -r file.txt

7) To show free, total, and swap memory info in bytes
    $free -t

8) List top processes?
    $top -u myuserid

9) Backup or transfer files?
    $tar cvf a.tar /subdir
    $tar xvf a.tar

10) List all open files?
    $lsof -u myuserid

Sunday, December 13, 2015

Month-End Time Series vs Monthly Candle Chart

library(quantmod)
getSymbols("WMT")
wmt = do.call(rbind, lapply(split(WMT, "months"), last))

chartSeries(to.monthly(WMT), 
            major.ticks='months',
            subset='last 10 years')
addBBands()



Thursday, December 10, 2015

Random Walk in R

M = 150
N = 360

m = matrix(0,nrow=M,ncol=N)
m1 = apply(m, c(1,2), function(x) rnorm(1)) #runif, rnorm
m2 = apply(m1, 1, cumsum)
m2 = t(m2)

hist(m2[,N], 30)

a = mean(m2[,N])
s = sd(m2[,N])^2

sd2 = apply(m2, 1, sd)
matplot(t(m2), type="l")



Saturday, November 28, 2015

Sargan Test and Hausman Test

 Sargan Test
 Hausman Test
 H0: Valid Instrument
 H0: X1 exogeneity 
2SLS 
y ~ X 
 e2SLS = y - Xb2SLS
 e = y-Xb
e2SLS ~ Z 
X1 ~ Z 
 m instruments in Z
 v = X1 - Zb2SLS1
 k explanatory variables in X
e ~ X + v 
nR2 – χ2 (m-k)
nR2 – χ2 (k1) 

Note:


X = (X1, X2)

Z = (Z*, X2)

X1: potentially endogenous variables

X2: exogenous variables
Z:   Instrumental variables

2SLS: Two Stage Least Square Method


Stage 1:  X1 = Z*ϒ1 + X2ϒ2 + η

Stage 2:  y = fitted_X1β + X2β + ε

Friday, October 2, 2015

Lexical Scoping in R

Lexical Scoping in R

 ---------------------------------- What is the output? -----------------------

y = "World!"

f = function(x) {
  y = "Weird!"
  g(x)
}

g = function(x) {
  cat(x, y)
}

f("Hello")

-----------------------------------------------------------------------------------

 > search()
 [1] ".GlobalEnv"        "package:RMySQL"    "package:DBI"    
 [4] "tools:rstudio"     "package:stats"     "package:graphics"
 [7] "package:grDevices" "package:utils"     "package:datasets"
 [10] "package:methods"   "Autoloads"         "package:base"

R, like Perl, Python and Lisp, is lexical(static) scoping. It searches for free variables in various environments with a specific order.

In this case, y is the free variable of function g(x).

The search order is:

 1, The environment where g(x) is defined: Global environment
 2, Parent environment (package, namespace, import)
 3, Repeat 2 till it reaches the top level
 4, Empty environment, error.

 Empty environment has not parent.

Here is a function closure in R:

lcg<-function(a,c,m) {
return( function(x) {
(a*x+c) %% m
})
}
my_runif=lcg(214013,2531011, 2^32)
my_runif(2)

 -------------------------------------------------------------------------------

 A well known question: "Does all computer languages converge to LISP?"