[Returnanalytics-commits] r2504 - pkg/PerformanceAnalytics/sandbox/pulkit/week1/code
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jul 5 17:26:01 CEST 2013
Author: pulkit
Date: 2013-07-05 17:26:00 +0200 (Fri, 05 Jul 2013)
New Revision: 2504
Modified:
pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSRopt.R
pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/moment.c
Log:
updated the c code
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSRopt.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSRopt.R 2013-07-05 13:52:31 UTC (rev 2503)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSRopt.R 2013-07-05 15:26:00 UTC (rev 2504)
@@ -27,8 +27,8 @@
columns = ncol(x)
n = nrow(x)
columnnames = colnames(x)
+ k<-rep(0,5)
-
if(is.null(bounds)){
message("Bounds not given assuming bounds to be (0,1) for each weight")
bounds = matrix(rep(c(0,1),columns),nrow = columns,byrow = TRUE)
@@ -101,10 +101,11 @@
SR = get_SR(stats,n)
meanSR = SR$meanSR
sigmaSR = SR$sigmaSR
+ for(i in 1:columns){
- for(i in 1:columns){
d1Z[i] = get_d1Z(stats,m,meanSR,sigmaSR,mean,weights,i)
}
+
dZ = list("d1Z"=d1Z,"z"=meanSR/sigmaSR)
return(dZ)
@@ -148,9 +149,11 @@
get_dnMoments<-function(mean,weights,mOrder,dOrder,index){
sum = 0
x0 = 1
+
for(i in 0:(dOrder-1)){
x0 = x0*(mOrder-i)
}
+
x_mat = as.matrix(na.omit(x))
sum = 0
output = .Call("sums",mat = x_mat,index,mean,dOrder,weights,mOrder,sum)
@@ -180,12 +183,15 @@
}
get_Moments<-function(series,order,mean = 0){
sum = 0
- for(i in series){
- sum = sum + (i-mean)^order
- }
+ mat = as.matrix(series)
+ sum = .Call("sums_m",mat,mean,order)
+ # for(i in series){
+ # sum = sum + (i-mean)^order
+ # }
return(sum/n)
}
weights = optimize()
+ print(k)
result = matrix(weights,nrow = columns)
rownames(result) = columnnames
colnames(result) = "weight"
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/moment.c
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/moment.c 2013-07-05 13:52:31 UTC (rev 2503)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/moment.c 2013-07-05 15:26:00 UTC (rev 2504)
@@ -31,4 +31,30 @@
return sum;
}
-
+SEXP sums_m(SEXP mat,SEXP Rmean,SEXP order){
+ int i,j,row,column;
+ SEXP Rsum;
+ SEXP Rdim = getAttrib(mat,R_DimSymbol);
+ row = INTEGER(Rdim)[0];
+ column = INTEGER(Rdim)[1];
+ mat = coerceVector(mat,REALSXP);
+ order = coerceVector(order,INTSXP);
+ int o = INTEGER(order)[0];
+ Rmean = coerceVector(Rmean,REALSXP);
+ double m = REAL(Rmean)[0];
+ PROTECT(Rsum = allocVector(REALSXP,1));
+ double s = REAL(Rsum)[0];
+ s = 0;
+ for(i = 0;i<row;i++){
+ for(j = 0;j<column;j++){
+ s = s + pow(REAL(mat)[i +row*j]-m,o);
+ }
+ }
+ REAL(Rsum)[0] =s;
+ UNPROTECT(1);
+
+ return Rsum;
+}
+
+
+
More information about the Returnanalytics-commits
mailing list