Displaying 20 results from an estimated 72 matches for "cumprod".
2008 Aug 18
2
matrix row product and cumulative product
...r more rows than columns.
I started with apply and after some thought realized that a loop of
columns might be faster and it was substantially faster (see below).
Because the likelihood function is called many times I?d like to speed
it up even more if possible.
Below is an example showing the cumprod.matrix and prod.matrix looping
functions that I wrote and some timing comparisons to the use of apply
for different column and row dimensions. At this point I?m better off
with looping but I?d like to hear of any further suggestions.
Thanks ?jeff
> prod.matrix=function(x)
+ {
+ y=x[,1]
+ f...
2003 Mar 22
1
cumprod doesn't work with data frames (PR#2667)
Full_Name: J. Sisk
Version: 1.6.1
OS: Linux (RedHat 8)
Submission from: (NULL) (67.119.41.66)
Suppose you make a data-frame like so:
xxx <- data.frame(a=10,b=20,c=30,d=40)
Then
cumprod(xxx[1,])
returns
> cumprod(xxx[1,])
a b c d
1 10 20 30 40
The documentation for cumprod says that it should work on "numerical objects",
and this is a data-frame, but it seems like it ought to "do the right thing" in
this case. If this is redundant, my apologies.
2011 Nov 27
1
generating a vector of y_t = \sum_{i = 1}^t (alpha^i * x_{t - i + 1})
...feel free to have a laugh but would appreciate if
someone can give me a hint. Otherwise I guess I'll have to give RCpp a
try.....
## Bench mark the recursion functions
loopRec <- function(x, alpha){
n <- length(x)
y <- double(n)
for(i in 1:n){
y[i] <- sum(cumprod(rep(alpha, i)) * rev(x[1:i]))
}
y
}
loopRec(c(1, 2, 3), 0.5)
## This is a crazy solution, but worth giving it a try.
charRec <- function(x, alpha){
n <- length(x)
exp.mat <- matrix(rep(x, each = n), nc = n, byrow = TRUE)
up.mat <- matrix(eval(parse(text = past...
2012 May 25
1
Filling NA with cumprod?
...0.9, 1.1, 0.9, 1.1)
I need to come up with function to get the following output
o[1] = a[1]
o[2] = a[2]
o[3] = a[3]
o[4] = o[3]*[f3] # Because a[3] is NA
o[5] = o[4]*[f4] # Because a[4] is NA; This looks like recursive
calculations; If the rest of the elements we NA, I would use a * c(rep(1,
3), cumprod(f[3:9])), but that's not the case
o[6] = a[6] # Not NA anymore
o[7] = a[7]
o[8] = o[7]*f[7] # Again a[8] is NA
o[9] = o[8]*f[8]
o[10] = a[10] # Not NA
Even though my explanation may seems complex, in reality the requirement is
pretty simple and in Excel is achieved with a very short formula....
2006 Feb 28
2
vector math: calculating a rolling 12 row product?
I have a dataframe of numeric values with 30 ?rows?
and 7 ?columns?.
For each column, beginning at ?row? 12 and down to
?row? 30, I wish to calculate the ?rolling 12 row
product?. I.e., within each column, I wish to
multiply all the values in row 1:12, 2:13,
19:30.
I wish to save the results as a new dataframe, which
will have 19 rows and 7 columns.
2009 Sep 16
2
Generalized cumsum?
Is there anything like cumsum and cumprod but which allows you to
apply an arbitrary function instead of sum and product? In other words,
I want a function cumfunc(x, f) that returns a vector, so that for all n
up to the length of x
cumapply(x,f)[n] = f(x[1:n])
This would give cumsum and cumprod as special cases when f=sum or
f=prod...
2011 Jan 27
1
How do I fix this ?
...63344 -0.0066314769 0.0075063818
[6] -0.0033548024 0.0015647863 0.0119815982 -0.0021430336 0.0044617167
[11] 0.0053447708 -0.0005590323 0.0063195781 0.0073059640 -0.0181872678
[16] -0.0098094568 0.0013679040 -0.0028490887 -0.0131129191 0.0126610358
z1 <- ifelse(is.na(y1), 10000, 10000*cumprod(1+y1))
z1
[1] 9931.369 9830.957 9664.162 9600.074 9672.136 9639.688 9654.772 9770.451
[9] 9749.513 9793.012 9845.354 9839.850 9902.034 9974.378 9792.971 9696.907
[17] 9710.172 9682.506 9555.541 9676.524
y <-c(NA, rnorm(19,0, .013))
y
[1] NA 0.0056258152 -0.0117690116 0.01639...
2010 Jul 09
3
apply is slower than for loop?
...s())
## DEFINE VARIABLES
mu=0.05 ; sigma=0.20 ; dt=.25 ; T=50 ; sims=1e5
timesteps = T/dt
## MAKE PHI AND DS
phi = matrix(rnorm(timesteps*sims), nrow=sims, ncol=timesteps)
ds = mu*dt + sigma * sqrt(dt) * phi
## USE APPLY TO CALCULATE ROWWISE CUMULATIVE PRODUCT
system.time(y1 <- apply(1+ds, 1, cumprod))
## UNTRANSFORM Y1, BECAUSE ROW APPLY FLIPS THE MATRIX
y1=t(y1)
## USE FOR LOOP TO CALCULATE ROWWISE CUMULATIVE PRODUCT
y2=matrix(NA,nrow(ds),ncol(ds))
system.time(
for (i in 1:nrow(ds)){
y2[i,]<-cumprod(1+ds[i,])
}
)
## COMPARE RESULTS TO MAKE SURE THEY DID THE SAME THING
str...
2009 Jul 31
1
what meaning missing value True /False needed
...#####
# Function calculating a scenario of results :
#############################################
ResultScenar <- function(FirstYear,OptimYear,Price,CumulRetentionCoef,Commission,IndividualExp,NbOfFutureSimYear,
IndivMeanLossAmount,CPIRate,LossInflaRate,Alpha)
{
CumulLossInflaRate <- cumprod(1+LossInflaRate)
CumulCPIRate <- cumprod(1+CPIRate)
y <- numeric(NbOfFutureSimYear)
delta <- OptimYear - FirstYear
if (OptimYear==FirstYear)
{
y[1] <CumulRetentionCoef[1]*( (Price[1]*(1-Commission[delta +1])-IndivMeanLossAmount[delta +1])*CumulLossInflaRate[delta +1]
- I...
2009 Sep 22
3
Function similar to cumsum/cumprod
Hello, everyone
I wonder if there is in R somewhere a function similar to cumsum().
The function calculates a statistic (say mean or standard deviation)
buy adding consequtively one more data point.
So, say I have a timeseries of 100 observations.
I start by calculating mean of first 30 observations
Then I add one observation and calculate mean of 31 observations
Then I add one more observation
2009 Oct 13
7
lapply() reccursively
Hi all,
I was wondering whether it is possible to use the lapply() function
to alter the value of the input, something in the spirit of :
a1<-runif(100)
a2<-function(i){
a1[i]<-a1[i-1]*a1[i];a1[i]
}
a3<-lapply(2:100,a2)
Something akin to a for() loop, but using the lapply() infrastructure.
I haven't been able to get rapply() to do this.
The reason is that the "real"
2006 Aug 31
2
cumulative growth rates indexed to a common starting point over n series of observations
What is the R way of computing cumulative growth rates given a series of
discrete values indexed .
For instance, given a matrix of 20 observations for each of 5 series (zz),
what is the most straight forward technique in R for computing cumulative
growth (zzcum) ?
It seems for the solution I'm after might be imbedding the following cum
growth rate calc as a function into a function call
2017 Jan 20
1
NaN behavior of cumsum
Hi!
I noticed that cumsum behaves different than the other cumulative functions wrt. NaN values:
> values <- c(1,2,NaN,1)
> for ( f in c(cumsum, cumprod, cummin, cummax)) print(f(values))
[1] 1 3 NA NA
[1] 1 2 NaN NaN
[1] 1 1 NaN NaN
[1] 1 2 NaN NaN
The reason is that cumsum (in cum.c:33) contains an explicit check for ISNAN.
Is that intentional?
IMHO, ISNA would be better (because it would make the behavior consistent with the other...
2003 Apr 29
1
Shafer's MIX: Query on code
...(d)))))
(d is a vector containing, in effect, the numbers of levels of
the factors in col1, col2, ... of the categorical variables.
Therefore umd is a vector containing the numbers of possible
combinations of factor levels in col1, col1&col2, ... )
But why not do it as
umd <- as.integer(cumprod(d))
?? [It can't be that this number could go out or range, since
that would be equally true of exp(cumsum(log(d)))) ]
With thanks, and best wishes to all,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>...
2009 Oct 29
2
fast cumulative matrix multiplication
Hi all,
I am looking for a function like cumprod() that works for matrix
multiplication.
In other words, I have matrices [M1, M2, ..., Mn], and I want to calculate
[M1, M1%*%M2, M1%*%M2%*%M3, ..., M1%*%...%*%Mn] as quickly as possible.
Right now I'm using a for() loop but it seems like there should be a faster
way.
Any help is appreciated!...
2010 Dec 19
2
Replacing the for loop for time series buid-up
...there a function that replaces the following code?
n=200
boot.x[1]=odhad+boot.res[1] #(boot.x[0]=1)
for (j in 1:(n-1)) {
boot.x[j+1]=odhad*boot.x[j]+boot.res[j+1]
}
This is nested in two other loops, and I am looking for some way to improve
code performance
I tried sapply and cumprod but no success.
Thanks
Jan
--
View this message in context: http://r.789695.n4.nabble.com/Replacing-the-for-loop-for-time-series-buid-up-tp3094421p3094421.html
Sent from the R help mailing list archive at Nabble.com.
2010 Feb 26
7
question to make a vector without loop
Hello all,
I want to define a vector like w[k+1]=w[k]*a/(b+k) for k=1,...,N-1 without
use loop. Is it posible to do in R?
Regards
khazaei
2004 Oct 03
1
How might one write this better?
I am trying to simulate the trajectory of the pension assets of one
person. In C-like syntax, it looks like this:
daily.wage.growth = 1.001 # deterministic
contribution.rate = 0.08 # deterministic 8%
Wage = 10 # initial
Asset = 0 # initial
for (10,000 days) {
Asset += contribution.rate * Wage
2008 Jan 31
3
Log rank test power calculations
Does anyone have any ideas how I could do a power calculation for a log
rank test. I would like to know what the suggested sample sizes would
be to pick a difference when the control to active are in a ratio of 80%
to 20%.
Thanks
Dan
--
**************************************************************
Daniel Brewer, Ph.D.
Institute of Cancer Research
Email: daniel.brewer at icr.ac.uk
2008 Jan 13
3
product of vector elements
All,
I would like to find a simper method that I now have to find the
product of all elements in a vector:
#get product of vector elements: (1,2,3,4,5)
vec.product <- exp(sum(log(c(1,2,3,4,5))))
I have not found a vector product function, if one has been written.
Thanks,
Gerard
[[alternative HTML version deleted]]