Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
When working with loglikelihoods, I sometimes get somethink like:
alpha*log(x) # log version of log(x^alpha)
In the non-log case, if both x and alpha are zero, R assumes 0^0 =1, which is usually the desired behavior (e.g.: the scenario "zero observations that had zero probability" has a probability 1). But in the alpha*log(x) version gives NaN:
alpha <- 0
x <- 0
log(x^alpha) # gives 0
alpha*log(x) # gives NaN
I've read that 0*Inf = NaN is an IEEE standard, but what are we supposed to do then? Creating a particular case for alpha=0? Not working with logs in this case? Something else?
I guess this is a very common scenario and I wonder how other people deal with it or whether there is some common practice for that.
R completes algebraic methods in steps, these would be the steps
log(0^0) = log(1) = 0
0 * log(0) = 0 * Inf = ?
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have to solve this:
Modify the cumulative risk calculator of exercise 1 so that the
argument t has the value 5 if user does not give it any other value.
cumulative risk = 1-exp(-1/10000*t).
where the exercise 1 was
We can estimate the cumulative risk of an certain event using the
exponential formula 1-exp(-1/10000*t) where t is the time to the
event. Create a function ans(t), which returns the risk at time t.
To assign a pre-specified value to t you can use
function(t=5){
answer<-1-exp(-1/10000*t)
return(answer)
}
You can define a variable as 5, then call the function using that variable, and save the result somewhere, i.e.
function.exp <- function(t) {
answer <- 1 - exp(-1 / 10000 * t)
return(answer)
}
x = 5
m <- function.exp(x)
m
Result
[1] 0.000499875
Hope that helps :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
How would you convert the constraint |x| >= 2 so that it would work in a Linear Program (in particular, solving using Simplex).
I understand how to convert |x| <= 2 as that would become x <= 2 and -x <= 2
However the same logic does not work when you have a minimum constant.
There is just no way to shoehorn an equation like |x|>=2 into a pure (continuous) LP. You need to formulate x <= -2 OR x >= 2 which is non-convex. This will require a binary variable making the problem a MIP.
One formulation can be:
x >= 2 - delta*M
x <= -2 + (1-delta)*M
delta in {0,1}
where M is judiciously chosen large number. E.g. if -100<=x<=100 then you can choose M=102.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a random walk function
random.walk = function(n){
return(cunsum(c(0, sample(c(-1, 1), size = n-1, replace =TRUE))))
}
I would like to make recursive random walk function.
what I did was
recursive.rwalk=function(n){
return(random.walk(n-1) + random.walk(n))
}
but then Im getting warning message.
Please help me out!
Thanks
You are trying to add two vectors of different lengths: n-1 and n in this line: random.walk(n-1) + random.walk(n). R is warning you that the first element of the first vector is added to the last element of the second vector (value recycling).
Altough it's obviously not a good idea to calculate a random walk recursively in R, here is how you would do that. First of all you have to recursively call the recursive.rwalk function, not the random.walk function. Next, you still need to sample each step of the way. And finally you need to tell the function when to stop recursively calling itself. Here's one way that will work:
recursive.rwalk=function(n){
if (n==0) return(0)
rw <- recursive.rwalk(n-1)
return(c(rw, rw[n-1]+sample(c(-1,1), 1)))
}
set.seed(123)
recursive.rwalk(5)
## [1] 0 1 0 1 2
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In R, I would like to create a function that returns the smallest n such that the n-th repetition of the natural logarithm gives a value smaller than one. Ex.: fun(9182) = 3 because ln(ln(ln(9182))) = 0,793 < 1.
Any suggestions will be appreciated!
logstar<-function(x){if (x<1) 0 else 1 + logstar(log(x))}
#mrip's answer works well for single values. If you'd like a function that works for vectors, you'll want to use ifelse() rather than if:
> logstar <- function(x){ifelse(x<1,0,1 + logstar(ifelse(x<1,x,log(x))))}
> x = c(0.5,1,100,10000,1E8)
> logstar(x)
[1] 0 1 3 3 4
The ifelse() in the recursive call to logstar() prevents log() from generating NaN in some cases.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
limit x-->3 2x^2 + 7x-15/x-3
What i Simplified
Step 1 : 2x^2 + 10x -3x -15 / x-3
Step 2 : 2x( x + 5)-3( x + 5)/x-3
Step 3 : (2x - 3)(x + 5)/x-3
but unable to move further.. im thinking either the question ix wrong or there ix some trick which im unable to understand
thanx in advance
as x -> 3, the numerator goes to 3 * 8 = 24 and the denominator goes to 0, so the limit goes to +infinity if you approach 3 from the right, and -infinity if you approach 3 from the left
since you didn't specify which direction, the limit does not exist.
try graphing it: https://www.desmos.com/calculator