Python: IndexError: list index out of range? - python-3.4

I am trying to form a list using a specific equation with given parameters, but I keep getting the error that "list index out of range". Here is the section of code:
yvalue = [iniPop]
for i in (1,maxIter,1):
yvalue.append(yvalue[i-1]*(1+(rate*(1-(yvalue[i-1]/CC)))))

What you're missing is range(1, maxIter) instead of just (1,maxIter,1). So you'll have
yvalue = [iniPop]
for i in range(1, maxIter):
yvalue.append(yvalue[i - 1] * (1 + (rate * (1 - (yvalue[i - 1] / CC)))))

Related

Create function in R to find coupon rate for bond

So far, I have written the following function:
c_rate <- function (bond_value, par, ttm, y) {
t <- seq(1, ttm, 1)
pv_factor <- 1 / (1 + y)^t
cr <- (bond_value - par / (1+y)^t) / (par*sum(pv_factor))
cr
}
however, this yields multiple results.
How can i update the function to only yield one the final index only?
I think you want to raise (1+y)^t to the power of ttm instead:
cr <- (bond_value - par / (1+y)^ttm) / (par*sum(pv_factor))

Division between complex number to himself

Refers to WolframAlpha and some other calculators:
1i / 1i = -1
i / i = 1
My questions are:
Why division between complex number to himself does'nt return the value 1?
The both expressions should be the same, so why the results are diffrent?
You need to be aware of what the first expression means. In particular, where are the hidden parentheses.
1i/1i is in fact 1 * i / 1 * i, which is (((1 * i) / 1) * i) when all the parentheses are added.
Then, step by step, you get ((i / 1) * i), then (i * i), which is -1.
This is very different from (1i)/(1i), which is effectively 1.

Attempt to apply non-function in a sequence

I'm trying to make a sequence with these conditions:
y<-seq(from=a-2(b-a), to=a+2(b-a), by=4(b-a)/99, length.out=100)
Given:
sample=rnorm(50, mean=0, sd=1)
sample_min=min(sample)
a<-sample_min
sample_max=max(sample)
b<-sample_max
And I get the error "attempt to apply non-function in a sequence." How do I fix this?
Have a look at 2(b-a) in the console:
2(b-a)
# Error: attempt to apply non-function
R doesn't know you want multiplication, so it thinks 2(b-a) is a function 2() with the argument b-a. You need to specify multiplication with the arithmetic operator *. So a-2(b-a) should be a-2*(b-a), and the same for the to and by arguments.
After you do that, you will need to remove one of either the to or length.out arguments because that will also create an error. to and length.out cannot be used together. The final product will be
seq(from = a - 2 * (b - a), to = a + 2 * (b - a), by = 4 * (b - a) / 99)
## or
seq(from = a - 2 * (b - a), by = 4 * (b - a) / 99, length.out = 100)
See help(seq) for more.

Avoid round off error in exponential calculation

Due to rounding error cannot get ratio between two numbers:
Ratio=exp(x)/(exp(x)+exp(y)) such that x=-1.11e4 and y=-1.12e4.
Any mathematical or computational trick to do?
You can simplify it like this:
R = exp(x) / (exp(x) + exp(y))
= exp(x) / (exp(x) * (1 + exp(y) / exp(x)))
= 1 / (1 + exp(y) / exp(x))
= 1 / (1 + exp(y - x))
(This is the same result as derived by DiltihiumMatrix, but obtained without going into the log domain and back again.)
How about some mathematical manipulation in log-space...
R = exp(x)/[exp(x)+exp(y)]
log(R) = log[exp(x)] - log[exp(x)+exp(y)]
= log[exp(x)] - log[exp(x)*(1+exp(y)/exp(x))]
= log[exp(x)] - log[exp(x)*(1+exp(y-x)]
= log[exp(x)] - log[exp(x)] - log[(1+exp(y-x))]
= - log[(1+exp(y-x))]
Now, exp(y-x) should be a reasonable number, so you can calculate that easily. Then convert back to normal space using R = exp(log(R)).
If that still doesn't work, you can actually taylor expand the last line:
log[(1+z)] ~ 1 + z^2/2 - z^3/3 ...
for small z, in this case z = exp(y-x).

translating matlab script to R

I've just been working though converting some MATLAB scripts to work in R, however having never used MATLAB in my life, and not exactly being an expert on R I'm having some trouble.
Edit: It's a script I was given designed to correct temperature measurements for lag generated by insulation mass effects. My understanding is that It looks at the rate of change of the temperature and attempts to adjust for errors generated by the response time of the sensor. Unfortunately there is no literature available to me to give me an indication of the numbers i am expecting from the function, and the only way to find out will be to experimentally test it at a later date.
the original script:
function [Tc, dT] = CTD_TempTimelagCorrection(T0,Tau,t)
N1 = Tau/t;
Tc = T0;
N = 3;
for j=ceil(N/2):numel(T0)-ceil(N/2)
A = nan(N,1);
# Compute weights
for k=1:N
A(k) = (1/N) + N1 * ((12*k - (6*(N+1))) / (N*(N^2 - 1)));
end
A = A./sum(A);
# Verify unity
if sum(A) ~= 1
disp('Error: Sum of weights is not unity');
end
Comp = nan(N,1);
# Compute components
for k=1:N
Comp(k) = A(k)*T0(j - (ceil(N/2)) + k);
end
Tc(j) = sum(Comp);
dT = Tc - T0;
end
where I've managed to get to:
CTD_TempTimelagCorrection <- function(temp,Tau,t){
## Define which equation to use based on duration of lag and frequency
## With ESM2 profiler sampling # 2hz: N1>tau/t = TRUE
N1 = Tau/t
Tc = temp
N = 3
for(i in ceiling(N/2):length(temp)-ceiling(N/2)){
A = matrix(nrow=N,ncol=1)
# Compute weights
for(k in 1:N){
A[k] = (1/N) + N1 * ((12*k - (6*(N+1))) / (N*(N^2 - 1)))
}
A = A/sum(A)
# Verify unity
if(sum(A) != 1){
print("Error: Sum of weights is not unity")
}
Comp = matrix(nrow=N,ncol=1)
# Compute components
for(k in 1:N){
Comp[k] = A[k]*temp[i - (ceiling(N/2)) + k]
}
Tc[i] = sum(Comp)
dT = Tc - temp
}
return(dT)
}
I think the problem is the Comp[k] line, could someone point out what I've done wrong? I'm not sure I can select the elements of the array in such a way.
by the way, Tau = 1, t = 0.5 and temp (or T0) will be a vector.
Thanks
edit: apparently my description is too brief in explaining my code samples, not really sure what more I could write that would be relevant and not just wasting peoples time. Is this enough Mr Filter?
The error is as follows:
Error in Comp[k] = A[k] * temp[i - (ceiling(N/2)) + k] :
replacement has length zero
In addition: Warning message:
In Comp[k] = A[k] * temp[i - (ceiling(N/2)) + k] :
number of items to replace is not a multiple of replacement length
If you write print(i - (ceiling(N/2)) + k) before that line, you will see that you are using incorrect indices for temp[i - (ceiling(N/2)) + k], which means that nothing is returned to be inserted into Comp[k]. I assume this problem is due to Matlab allowing the use of 0 as an index and not R, and the way negative indices are handled (they don't work the same in both languages). You need to implement a fix to return the correct indices.

Resources