[Returnanalytics-commits] r2925 - pkg/Meucci/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 29 11:06:59 CEST 2013


Author: xavierv
Date: 2013-08-29 11:06:58 +0200 (Thu, 29 Aug 2013)
New Revision: 2925

Added:
   pkg/Meucci/R/FitOrnsteinUhlenbeck.R
Log:
- fixed FitOrnsteinUhlenbeck documentation

Added: pkg/Meucci/R/FitOrnsteinUhlenbeck.R
===================================================================
--- pkg/Meucci/R/FitOrnsteinUhlenbeck.R	                        (rev 0)
+++ pkg/Meucci/R/FitOrnsteinUhlenbeck.R	2013-08-29 09:06:58 UTC (rev 2925)
@@ -0,0 +1,55 @@
+#' Fit a multivariate OU process at estimation step tau, as described in  A. Meucci 
+#' "Risk and Asset Allocation", Springer, 2005
+#'
+#'  @param  Y   : [matrix] (T x N)
+#'  @param  tau : [scalar] time step
+#'  
+#'  @return Mu  : [vector] long-term means
+#'  @return Th  : [matrix] whose eigenvalues have positive real part / mean reversion speed
+#'  @return Sig : [matrix] Sig = S * S', covariance matrix of Brownian motions
+#'
+#' @note
+#'	o dY_t = -Th * (Y_t - Mu) * dt + S * dB_t where
+#'	o dB_t: vector of Brownian motions
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "FitOrnsteinUhlenbeck.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+FitOrnsteinUhlenbeck = function( Y, tau )
+{
+	T = nrow(Y);
+	N = ncol(Y);
+
+	X    = Y[ -1,  ];
+	F    = cbind( matrix( 1, T-1, 1 ), Y[ -nrow(Y), ] );
+	E_XF = t(X) %*% F / T;
+	E_FF = t(F) %*% F / T;
+	B    = E_XF %*% solve( E_FF );
+	if( length( B[ , -1 ] ) != 1 )
+	{
+		Th = -logm( B[ , -1 ] ) / tau;
+
+	}else
+	{
+		Th = -log( B[ , -1 ] ) / tau;
+	}
+
+	Mu = solve( diag( 1, N ) - B[ , -1 ] ) %*%  B[ , 1 ] ;
+
+	U  = F %*% t(B) - X;
+	
+	Sig_tau = cov(U);
+
+	N = length(Mu);
+	TsT = kron( Th, diag( 1, N ) ) + kron( diag( 1, N ), Th );
+
+	VecSig_tau = matrix(Sig_tau, N^2, 1);
+	VecSig = ( solve( diag( 1, N^2 ) - expm( -TsT * tau ) ) %*% TsT ) %*% VecSig_tau;
+	Sig = matrix( VecSig, N, N );
+
+	return( list( Mu = Mu, Theta = Th, Sigma = Sig ) )
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list