[Returnanalytics-commits] r2501 - pkg/PerformanceAnalytics/sandbox/pulkit/week1/code
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jul 5 09:59:11 CEST 2013
Author: pulkit
Date: 2013-07-05 09:59:10 +0200 (Fri, 05 Jul 2013)
New Revision: 2501
Added:
pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/moment.c
Modified:
pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSROpt.py
pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSRopt.R
Log:
Added c code for PSR Optimization
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSROpt.py
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSROpt.py 2013-07-04 18:03:37 UTC (rev 2500)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSROpt.py 2013-07-05 07:59:10 UTC (rev 2501)
@@ -3,6 +3,7 @@
# On 20121128 by MLdP <lopezdeprado at lbl.gov>
import numpy as np
+import time
#-------------------------------------------
#-------------------------------------------
class PSR_Opt:
@@ -143,7 +144,10 @@
#3) Create class and solve
psrOpt=PSR_Opt(series,seed,delta,maxIter,bounds)
+ start = time.time()
psrOpt.optimize()
+ end = time.time()
+ print(end-start)
#4) Optimize and report optimal portfolio
print 'Maximized Z-value: '+str(psrOpt.z)
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSRopt.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSRopt.R 2013-07-04 18:03:37 UTC (rev 2500)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSRopt.R 2013-07-05 07:59:10 UTC (rev 2501)
@@ -47,7 +47,6 @@
while(TRUE){
if(iter == MaxIter) break
dZ = get_d1Zs(mean,weights)
- print(dZ$z)
if(dZ$z>z && checkBounds(weights)==TRUE){
z = dZ$z
d1z = dZ$d1Z
@@ -84,13 +83,13 @@
}
#To get the first differentials
get_d1Zs<-function(mean,weights){
- d1Z = NULL
- m = NULL
+ d1Z = numeric(columns)
+ m = numeric(4)
x_portfolio = x%*%weights
m[1] = get_Moments(x_portfolio,1)
for(i in 2:4){
- m = c(m,get_Moments(x_portfolio,i,m[1]))
+ m[i] = get_Moments(x_portfolio,i,m[1])
}
stats = get_Stats(m)
#mu = mean(x_portfolio)
@@ -104,7 +103,7 @@
sigmaSR = SR$sigmaSR
for(i in 1:columns){
- d1Z = c(d1Z,get_d1Z(stats,m,meanSR,sigmaSR,mean,weights,i))
+ d1Z[i] = get_d1Z(stats,m,meanSR,sigmaSR,mean,weights,i)
}
dZ = list("d1Z"=d1Z,"z"=meanSR/sigmaSR)
@@ -153,15 +152,20 @@
x0 = x0*(mOrder-i)
}
x_mat = as.matrix(na.omit(x))
- for(i in 1:n){
- x1 = 0
- x2 = (x_mat[i,index]-mean[index])^dOrder
- for(j in 1:columns){
- x1 = x1 + weights[j]*(x_mat[i,j]-mean[j])
- }
- sum = sum + x2*x1^(mOrder-dOrder)
- }
- return(x0*sum/n)
+ sum = 0
+ output = .Call("sums",mat = x_mat,index,mean,dOrder,weights,mOrder,sum)
+ #for(i in 1:n){
+ # x1 = 0
+ # x2 = (x_mat[i,index]-mean[index])^dOrder
+ #if(index == 1){
+ # print(x2)
+ #}
+ # for(j in 1:columns){
+ # x1 = x1 + weights[j]*(x_mat[i,j]-mean[j])
+ # }
+ # sum = sum + x2*x1^(mOrder-dOrder)
+ # }
+ return(x0*(output)/n)
}
# TO get meanSR and sigmaSR
@@ -181,7 +185,6 @@
}
return(sum/n)
}
-
weights = optimize()
result = matrix(weights,nrow = columns)
rownames(result) = columnnames
@@ -191,6 +194,3 @@
-
-
-
Added: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/moment.c
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/moment.c (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/moment.c 2013-07-05 07:59:10 UTC (rev 2501)
@@ -0,0 +1,34 @@
+#include<R.h>
+#include<Rinternals.h>
+#include<Rmath.h>
+
+SEXP sums(SEXP mat,SEXP index,SEXP Rmean,SEXP dOrder,SEXP Rweights,SEXP mOrder,SEXP sum){
+ double x1,x2,diff;
+ int a,b,row,column;
+ SEXP Rdim = getAttrib(mat,R_DimSymbol);
+ row = INTEGER(Rdim)[0];
+ column = INTEGER(Rdim)[1];
+ mat = coerceVector(mat,REALSXP);
+ dOrder = coerceVector(dOrder,INTSXP);
+ mOrder = coerceVector(mOrder,INTSXP);
+ index = coerceVector(index,INTSXP);
+ int ind = INTEGER(index)[0];
+ int d = INTEGER(dOrder)[0];
+ int m = INTEGER(mOrder)[0];
+ double *mean = REAL(Rmean);
+ double *weights = REAL(Rweights);
+ double s = 0;
+ for(a = 0;a<row;a++){
+ x1 = 0;
+ diff = REAL(mat)[a+row*(ind-1)]-mean[ind-1];
+ x2 = pow(diff,d);
+ for(b = 0;b<column;b++){
+ x1 = x1 + weights[b]*(REAL(mat)[a + row*b]-mean[b]);
+ }
+ s = s +x2*pow(x1,m-d);
+ }
+ REAL(sum)[0] = s;
+ return sum;
+}
+
+
More information about the Returnanalytics-commits
mailing list