Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it posible to optimize (vectorize) these two functions for better performance
    text
    copied!<p>In my first attempts in using R I wrote two functions that are not very performant I guess and would appreciate if I can receive some hints on how to make them more performant (vectorized). Both functions come with "test case" at the end.</p> <p>The first function takes two time series xts objects x and y and returns a series which contains data on how many days x is higher/lower than y.</p> <pre><code>require('xts') require('quantmod') countDaysBelowOrAbove &lt;- function(x, y) { x &lt;- try.xts(x, error=as.matrix) y &lt;- try.xts(y, error=as.matrix) if(is.xts(x) &amp;&amp; is.xts(y)) { xy &lt;- cbind(x,y) } else { xy &lt;- cbind( as.vector(x), as.vector(y) ) } # Count NAs, ensure they're only at beginning of data, then remove. xNAs &lt;- sum( is.na(x) ) yNAs &lt;- sum( is.na(y) ) NAs &lt;- max( xNAs, yNAs ) if( NAs &gt; 0 ) { if( any( is.na(xy[-(1:NAs),]) ) ) stop("Series contain non-leading NAs") } resultDaysLower &lt;- x resultDaysHigher &lt;- x resultDaysLower[!is.na(resultDaysLower)]&lt;-0 resultDaysHigher[!is.na(resultDaysHigher)]&lt;-0 series&lt;-cbind(xy, resultDaysLower, resultDaysHigher) colnames(series) &lt;- c(names(xy), "cumDaysLower", "cumDaysHigher") daysLower = 0 daysHigher = 0 for (i in 1:NROW(xy)) { if (!(is.na(series[,1][i]) | is.na(series[,2][i]))) { if (series[,1][i] &gt;= series[,2][i]) { daysLower = 0 daysHigher = daysHigher + 1 } else { daysHigher = 0 daysLower = daysLower + 1 } } else { daysLower = 0 daysHigher = 0 } series$cumDaysLower[i] = daysLower series$cumDaysHigher[i] = daysHigher } return(series) } getSymbols("SPY", from='2005-01-01') SPYclose = Cl(SPY) getSymbols("QQQQ", from='2005-01-01') QQQQclose = Cl(QQQQ) testData = countDaysBelowOrAbove(SPYclose, QQQQclose) </code></pre> <p>The second function I would appreciate help with performance optimization is below. The function takes as parameter an xts object series and an xts object representing lengths of interval to calculate minimum of series at a specified time. The function returns calculated minimum of series with specified window for minimum calculation set in lengths.</p> <pre><code>minimumWithVaryingLength&lt;-function(series, lengths) { series &lt;- try.xts(series, error=as.matrix) lengths &lt;- try.xts(lengths, error=as.matrix) if(is.xts(series) &amp;&amp; is.xts(lengths)) { serieslengths &lt;- cbind(series,lengths) } else { serieslengths &lt;- cbind( as.vector(series), as.vector(lengths) ) } # Count NAs, ensure they're only at beginning of data, then remove. seriesNAs &lt;- sum( is.na(series) ) lengthsNAs &lt;- sum( is.na(lengths) ) NAs &lt;- max( seriesNAs, lengthsNAs ) if( NAs &gt; 0 ) { if( any( is.na(serieslengths[-(1:NAs),]) ) ) stop("Series contain non-leading NAs") } result &lt;- series result[!is.na(result)]&lt;-0 for (i in 1:NROW(serieslengths)) { if (lengths[i] &gt; 0) { result[i] &lt;- runMin(series, n=lengths[i], cumulative=FALSE)[i] } else { result[i] &lt;- 0 } } return(result) } getSymbols("SPY", from='2005-01-01') SPYclose = Cl(SPY) getSymbols("QQQQ", from='2005-01-01') QQQQclose = Cl(QQQQ) numDaysBelow = countDaysBelowOrAbove(SPYclose, QQQQclose) test = minimumWithVaryingLength(SPYclose, numDaysBelow) </code></pre> <p>Thanks in advance for your kind help.</p> <p>Kind regards, Samo.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload