Monday, December 2, 2013

ZeroMQ Latency Test

ZeroMQ latency test in my ubuntu machine. It is indeed a super fast MQ.


root@ubuntu:~/zeromq-4.0.3/perf# ./remote_lat tcp://192.168.248.130:5555 1 100000
message size: 1 [B]
roundtrip count: 100000
average latency: 19.039 [us]

root@ubuntu:~/zeromq-4.0.3/perf# ./inproc_thr 1 100000
message size: 1 [B]
message count: 100000
mean throughput: 4535147 [msg/s]
mean throughput: 36.281 [Mb/s]

root@ubuntu:~/zeromq-4.0.3/perf# ./inproc_lat 1 100000
message size: 1 [B]
roundtrip count: 100000
average latency: 2.391 [us]

root@ubuntu:~/zeromq-4.0.3/perf# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:6d:53:d8
          inet addr:192.168.248.130  Bcast:192.168.248.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe6d:53d8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:130057 errors:0 dropped:0 overruns:0 frame:0
          TX packets:25415 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:187822650 (187.8 MB)  TX bytes:2002840 (2.0 MB)

Sunday, December 1, 2013

Friday, November 29, 2013

New Features of C++11 and C++14 in Visual C++ Compiler November 2013 CTP



  • Implicit move special member function generation (thus also completing =default)
  • Reference qualifiers on member functions (a.k.a. “& and && for *this“)
  • Thread-safe function local static initialization (a.k.a. “magic statics”)
  • Inheriting constructors
  • alignof/alignas
  • __func__
  • Extended sizeof
  • constexpr (except for member functions)
  • noexcept (unconditional)
  • C++14 decltype(auto)
  • C++14 auto function return type deduction
  • C++14 generic lambdas (with explicit lambda capture list)
  • (Proposed for C++17) Resumable functions and await

http://blogs.msdn.com/b/vcblog/archive/2013/11/18/announcing-the-visual-c-compiler-november-2013-ctp.aspx


Wednesday, November 20, 2013

[转载]金融数学书单

金融数学是一门应用性极强的学科,其特殊之处在于,与许多其他应用学科如生物相比,它的难度更类似于数学物理,而另一方面,它的应用性可以和 engineering相提并论,因为好的结果必须是"有利可图"的,you may cheat a Journal, but you cannot cheat the Market...而更加独特的是,它要求一个人有极其博杂的知识,所以一份好的书单很重要
  
大体而言,所需要的知识分为三类
  1.数量
  2.经济金融
  3.编程(略)
 
现在我列一下数量方面的书单

1.概率论

很不幸的事实是,概率论基本上没有好的中文教材(1998之前,之后我就不清楚了)
Ross的书适合本科和硕士生,胜在例子详尽
Billingsley的概率论和弱收敛的两本教材是非常好的入门书
chung的概率论教材很严格,读起干巴巴的来会有点累,不过是真长工夫的密籍
Durrett的书很流行,不过里面的小错误很多
如果你真的想理解概率论,feller的两本书是不可不读的,可以说,从高中水平到博士以上学位的读者,都会从中获益---如果要推选概率论里面最有影响的教材,feller的书无可比拟,不过读时要一路自己算,feller书里面错误非常多,虽然都显然是笔误
Breiman的书也是经典,概率味比chung的浓
loeve的书可以作为工具书使用

2.随机分析

黄志远的随机分析入门是一本很好的书
严加安的鞅论可以做工具书用
Ross的Inrto to probability model可以做本科生随机过程入门,例子很多
Karlin & Taylor的两本书非常适合硕士生用
resnick的几本书概率味很不错,应用性也很强
oksendal的书是SDE里面最简单的
Karatzas Shreve有好几本书,金融数学的博士不可不读
Revuz Yor的连续鞅是很好的书
Protter的书是严格随机分析里面最容易读的,文笔很好
williams的书深入浅出,入门很合适
Chung Williams的书比oksendal稍微难一点,作为应用随机分析的标准教材很不错

3-控制论

控制论在portfolio selection problem和risk management里面有很多的应用,optimal stopping在美式derivative非常重要
金融数学里面用的主要是随机控制,和粘性解(因为operator is often degenerate)
经典的随机控制书是

1.FLEMING and RISHEL, (1975) Deterministic and Stochastic Optimal Control.2.KRYLOV, (1980) Controlled diffusion processes3.BORKAR, (1989) Optimal control of diffusion processes.4.BENSOUSSAN and LIONS, (1982) Controle Impulsionnel et Inequations Variationnelles
粘性解的标准文献是
1. Crandall, Ishii and Lions, User's guide to viscosity solutions of second order partial differential equations, Bull. Amer. Math. Soc. 27 (1992),2.Fleming and Soner, Controlled Markov Processes and Viscosity Solutions, 1992.

4.数值算法

首先,finite difference是极其常用的算法,这方面书籍很多,比如Ames的经典教材
计算矩阵: Golub and Van Loan, Matrix Computations, 1996Kushner and Dupuis, Numerical Methods for Stochastic Control Problems in Continuous Time, 1992. Kushner's Markov chain approximation method是控制论里最有用的算法
ROGERS and TALAY, Numerical Methods in Financial Mathematics. 1997.论文集
Kloeden and Platen, Numerical Solution of Stochastic Differential Equations, 1997. 偏理论,实用性差一点
Glasserman, Monte Carlo Methods in Financial Engineering, 2003这本书非常非常实用,可以说是金融数学数值算法的最新经典

5-时间序列

当然,学习时间序列之前,统计特别是多变量统计要先学好

A Guide to Econometrics: by Peter Kennedy可能是最通俗易懂的入门书
Econometric Analysis,by William H. Greene和Time Series Analysis by James Douglas Hamilton是非常标准的教材,许多学校都在用
Box Jerkins的Time Series Analysis: Forecasting & Control,当之无愧的经典
Time Series and Dynamic Models by Christian Gourieroux,Gourieroux写了许多书,但似乎他的书不如他的研究文章水准高
The Econometrics of Financial Markets,by John Y. Campbell, Andrew W. Lo, A. Craig MacKinlay,新经典

现在我们来看一下经济金融方面的书单

首先要强调,金融不是经济,经济考虑的是国计民生,环球宇宙之类的大问题,而金融考虑的是money making, risk control之类的充满铜臭味的小问题

当然,经济背景也是需要的,比如说
Varian: Microeconomic Analysis(1992)Samuelson: Economics如果有时间,最有价值的书大概是Keynes的general principle,看的时候的感觉会跟第一次学微积分差不多

现在我们进入金融书单

1.理论金融

Merton: Continuous time financeHuang Litzenberger: Foundation for financial economicsIngersoll: Theorey of financial decision makingRoss: Neoclassical Finance Ross, Westerfield, Jaffe: Corporate FinanceDuffie: security marketDuffie: Dynamic Asset Pricing Theory当然,金融文献浩如烟海,上面的书单是针对ASSET PRICING一块的,因为这一块最为定量化.至于做underwriting, M&A,一般不是很需要数量出身的人,至少到目前为止

2.入门和综合类

然后就要开始看一些实际的入门书了
Hull, Options, Futures and Other Derivatives 
Baxter and Rennie, Financial Calculus
Shreve:Stochastic Calculus Models for Finance vol 1 & 2
Wilmott: quantitative finance
然后

Bjork: Arbitrage theory in continuous time
Cvitanic, Zapatero: Introduction to the economics and mathematics of financial markets
Elliott, Kopp: Mathematics of Financial markets
Karatzas Shreve: Method of math finance
Musiela and Rutkowski: martingale method for finance
Bielecki, Rutkowski: Credit Risk : Modeling , Valuation and Hedging
Duffie Singleton: Credit Risk
Amman: Credit risk valuation
 
3. Fixed income
Tuckman: Fixed Income Securities: Tools for Today's Markets是入门的最佳选择

然后,就不得不面对Fabozzi的无数厚书乐
Fixed Income MathematicsFixed Income SecuritiesBond Markets : Analysis and StrategiesThe Handbook of Fixed Income Securities,Handbook of Mortgage Backed SecuritiesCollateralized Debt Obligations: Structures and AnalysisInterest Rate, Term Structure, and Valuation Modeling
Jessica James, Nick Webber Interest Rate Modelling: Financial Engineering,这本书乱而全
Brigo, Mercurio:Interest Rate Models 数学上难一些
Tavakoli: Collateralized Debt Obligations and Structured FinanceTavakoli: Credit Derivatives & Synthetic Structures: A Guide to Instruments and ApplicationsHayre: Salomon Smith Barney Guide to Mortgage-Backed and Asset-Backed Securities

4:其他类
Rebonato有几本很好的书:
Volatility and Correlation : The Perfect Hedger and the Fox
Modern Pricing of Interest-Rate Derivatives : The LIBOR Market Model and Beyond
Interest-Rate Option Models : Understanding, Analysing and Using Models for Exotic Interest-Rate Options
Schönbucher:Credit Derivatives Pricing Models: Model, Pricing and Implementation
写得很乱但是无可替代
GENCAY: An Introduction to High-Frequency Finance第一本关于high frequency的书
O'Hara:Market Microstructure Theory
Harris:Trading and Exchanges: Market Microstructure for Practitioners
这些书主要是供研究用的。开这个单子的人在美国做Assistant Professor,所以重点肯定是研究了。

他的单子里面最实际的一部分是下面这部分:

2.入门和综合类

然后就要开始看一些实际的入门书了
Hull, Options, Futures and Other Derivatives
Baxter and Rennie, Financial Calculus
Shreve:Stochastic Calculus Models for Finance vol 1 & 2
Wilmott: quantitative finance
然后
Bjork: Arbitrage theory in continuous timeCvitanic, Zapatero: Introduction to the economics and mathematics of financial marketsElliott, Kopp: Mathematics of Financial marketsKaratzas Shreve: Method of math financeMusiela and Rutkowski: martingale method for financeBielecki, Rutkowski: Credit Risk : Modeling , Valuation and HedgingDuffie Singleton: Credit RiskAmman: Credit risk valuationTalebynamic Hedging

3. Fixed income
Tuckman: Fixed Income Securities: Tools for Today's Markets是入门的最佳选择

里面的大部分书依然不太适合实际工作,里面较适合实际工作的书是:

Hull, Options, Futures and Other Derivatives
Baxter and Rennie, Financial Calculus
Wilmott: quantitative finance
Tuckman: Fixed Income Securities: Tools for Today's Markets
这几本里面的数学相对比较简单。

Hull这本书被称为“华尔街圣经”,确实不假,里面数学用的非常简单,很容易懂,而且覆盖面广,有很多关于实际工作的内容。如果想出去工作,绝对应该精读。

Baxter and Rennie的Financial Calculus也是一本经典,目的是把大家一般认为艰深的Stochastic Calculus介绍给在工作中需要懂一些这方面知识的人,应该说,这个任务是非常棒的完成了。书里面把一些重要的Stochastic Calculus中的概念和定理写的简单易懂,尽管所用数学比Hull深,却依然易读。这本书的副标题是An Introduction to Derivative Pricing,主要是Financial Derivatives,包括interest rate derivative,没有credit derivative方面的内容。

Wilmott的那本quantitative finance(上下册,现在又新出了3卷本的)写的很人性化,也很好上手。你可以找来看看,应该是有关于credit derivative的内容的。

Tuckman的那本我不了解,听别人说比较实用,也比较易读。
Shreve的那本并没有上面几本好上手,数学用的相对还是比较多的,尽管这本书已经是他写过的最简单、最不数学化的书了。

Thursday, October 31, 2013

Negatively Skewed Trading Strategies

1. Two stocks A and B

A - mean return 1%, skew of return PDF -2%, hit rate 50%
B - mean return 0.8%, skew of return PDF 0, hit rate 50%

You have 100 dollars. Which one you should invest in?

One can think of skew in relation to volatility in the same way one thinks of acceleration in relation to speed. It is not enough to know your speed when driving but also whether you are capable of decelerating when approaching an obstacle and accelerating when you need to.

Positive skew is the ability to have lower volatility than average when losing money and higher volatility when making money. Negative skew is the opposite; it is the characteristic of having higher volatility than average when losing money and lower volatility when making money.

A set of returns made up of frequent small, lower than average, returns and occasional large gains would be positively skewed. Conversely, a set of returns with frequent small, above average, returns and occasional large losses would be negatively skewed.

One additional characteristic of skew that must be mentioned is its invariance with respect to volatility, (i.e., the value of skew for a set of returns stays unchanged if all the returns are multiplied by a constant.)


Thursday, October 10, 2013

bind2nd & bind1st and operator keyword


1. bind1st and bind2nd (replaced by bind in C++11)

This function constructs an unary function object from the binary function object op by binding its second parameter to the fixed value x.
bind2nd(greater(),"Baz")
X>Y
the second parameter Y is bound to "Baz", so it is like X>“Baz”.
If it is:
bind1st(greater(),"Baz")
X>Y
X is the first parameter and is bound as "Baz", so it is like "Baz" > Y
-----------------------

class ABA{
public:
    ABA(){}
    ABA(ABA& a){}
    ABA& operator=(const ABA& a){return *this;}
};
const ABA a1;
ABA a2(a1);// Wong, there is no appropriate constructor available!
-----------------------


2.keyword "operator" in C++


1. operator overloading
typename operator ... (){}

2. implicit conversion - conversion operator
operator typename(){...}

If i want to convert A to B, the conversion operator is used as:
class A{
  operator B(){}
};
-------------------

3.how to use throw

1. throw A;

2. throw;
The throw-expression without an operand may only be used inside a catch block (it calls std::terminate if used otherwise). It abandons the execution of the catch block and passes control to the next matching catch clause up the call stack (but not to another catch clause after the same try block), reusing the existing exception object: no new objects are made.

Thursday, October 3, 2013

R code to verify Volatility Forecast Theory by Robert Engle

The Paper from Christian Brownlees, Rob Engle and Bryan Kelly can be downloaded from here:
http://faculty.chicagobooth.edu/bryan.kelly/research/pdf/volfor.pdf‎

Based on the real market data of SPX, I wrote the following R code to verify that GARCH model ranking doesn't change as the forecast horizon changes:

rm(list=ls())

library(rugarch)
library(timeSeries)
library(xts)

fname="spx.csv";
res=readSeries(fname,header=T,sep=",",format="%m/%d/%Y");
colnames(res)=c("O", "H", "L", "C", "V", "A")
sp500=as.xts(res[,6]);
rtn=na.omit(returns(sp500));
rtnsquare=rtn^2;
ord=ar(as.numeric(rtn),method='mle');
armaorder_=c(ord$order,0)
spec1=ugarchspec(mean.model = list(armaOrder = armaorder_),
                 variance.model = list(model='fGARCH', submodel='GARCH'));
spec2=ugarchspec(mean.model = list(armaOrder = armaorder_),
                 variance.model = list(model='fGARCH', submodel='NGARCH'));
spec3=ugarchspec(mean.model = list(armaOrder = armaorder_),
                 variance.model = list(model='fGARCH', submodel='TGARCH'));
spec4=ugarchspec(mean.model = list(armaOrder = armaorder_),
                 variance.model = list(model='eGARCH', submodel=NULL));
spec5=ugarchspec(mean.model = list(armaOrder = armaorder_),
                 variance.model = list(model='fGARCH', submodel='APARCH'));

#Quasi Loss Function
QL=function(squaredR,VarForecast){
  tmp=squaredR/VarForecast;
  return(tmp-log(tmp)-1);
}
specs=c(spec1,spec2,spec3,spec4,spec5)
sname=c("GARCH","NGARCH","TARCH","eGARCH","APARCH")

Cal=function(specs){
  horizons=c(1,10,20)
  for(horizon in horizons){
    tmp=0
    count=0;
    index=1;
    for(spec_ in specs){
      cat(sname[index],": ")
      index=index+1;
      out_sample_len=c(50:50);
      for(i in out_sample_len){
        fit1=ugarchfit(rtn, spec = spec_, out.sample=i,solver='hybrid')
        fcast1=ugarchforecast(fit1,n.ahead=horizon)
        squaredR=rtnsquare[dim(rtnsquare)[1]-i+horizon];
        VarForecast=sigma(fcast1)[horizon]^2;
        loss=QL(squaredR,VarForecast);
        tmp=tmp+loss;
        count=count+1;
      }
      cat("Horizon =",horizon,"; LQ =",(tmp/count),"\n");
    }
  }
}

Cal(specs)

The result is as follows:

GARCH : Horizon = 1 ; LQ = 1.234608
NGARCH : Horizon = 1 ; LQ = 1.091949
TARCH : Horizon = 1 ; LQ = 1.067449
eGARCH : Horizon = 1 ; LQ = 1.060457
APARCH : Horizon = 1 ; LQ = 1.02967

GARCH : Horizon = 10 ; LQ = 2.259468
TARCH : Horizon = 10 ; LQ = 2.123502
NGARCH : Horizon = 10 ; LQ = 2.141799
eGARCH : Horizon = 10 ; LQ = 2.107577
APARCH : Horizon = 10 ; LQ = 2.089418

GARCH : Horizon = 20 ; LQ = 1.25515
NGARCH : Horizon = 20 ; LQ = 1.179098
TARCH : Horizon = 20 ; LQ = 1.170422
eGARCH : Horizon = 20 ; LQ = 1.150264
APARCH : Horizon = 20 ; LQ = 1.144654

The GarchFit takes very long time to finish as my notebook is not so powerful. I might need a cluster or grid to do this kind of verification.