We have a set of 3 numbers and a number which is function output of these numbers.
For example:
2, 4, 9 = 88
3, 7, 8 = 76
4, 9, 6 = 55
Now the function is for example
( last digit X 10 ) - (second digit - first digit)
Thus
5, 8, 2 = 17
But we have to find out this formula by trial and error method.
Is there any mathematical technique, tool or any program to find out the function associated among the numbers?
Related
I am trying to create a function that buys an N period high. So if I have a vector:
x = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
I want to take the rolling 3 period high. This is how I would like the function to look
x = c(1, 2, 3, 4, 5, 5, 5, 3, 4, 5)
I am trying to do this on an xts object.
Here is what I tried:
rollapplyr(SPY$SPY.Adjusted, width = 40, FUN = cummax)
rollapply(SPY$SPY.Adjusted, width = 40, FUN = "cummax")
rapply(SPY$SPY.Adjusted, width = 40, FUN = cummax)
The error I am receiving is:
Error in `dimnames<-.xts`(`*tmp*`, value = dn) :
length of 'dimnames' [2] not equal to array extent
Thanks in advance
You're close. Realize that rollapply (et al) is this case expecting a single number back, but cummax is returning a vector. Let's trace through this:
When using rollapply(..., partial=TRUE), the first pass is just the first number: 1
Second call, the first two numbers. You are expecting 2 (so that it will append to the previous step's 1), but look at cummax(1:2): it has length 2. Conclusion from this step: the cum functions are naïve in that they are relatively monotonic: they always consider everything up to and including the current number when they perform their logic/transformation.
Third call, our first visit to a full window (in this case): considering 1 2 3, we want 3. max works.
So I think you want this:
zoo::rollapplyr(x, width = 3, FUN = max, partial = TRUE)
# [1] 1 2 3 4 5 5 5 3 4 5
partial allows us to look at 1 and 1-2 before moving on to the first full window of 1-3. From the help page:
partial: logical or numeric. If 'FALSE' (default) then 'FUN' is only
applied when all indexes of the rolling window are within the
observed time range. If 'TRUE', then the subset of indexes
that are in range are passed to 'FUN'. A numeric argument to
'partial' can be used to determin the minimal window size for
partial computations. See below for more details.
Perhaps it is helpful -- if not perfectly accurate -- to think of cummax as equivalent to
rollapplyr(x, width = length(x), FUN = max, partial = TRUE)
# [1] 1 2 3 4 5 5 5 5 5 5
cummax(x)
# [1] 1 2 3 4 5 5 5 5 5 5
I have some value of x:
x <- c(12, 5, 6, 7, 8, 5, 8, 7, 5, 6, 9, 10)
p <- x[order(x)]
p
[1] 5 5 5 6 6 7 7 8 8 9 10 12
The smallest value of x is 5, but I want to choose the second of the smallest x (6) or third (7).
How to get it?
We can write a function to get nth smallest value, by considering only unique values of already sorted vector p.
get_nth_smallest_value <- function(n) {
unique(p)[n]
}
get_nth_smallest_value(2)
#[1] 6
get_nth_smallest_value(4)
#[1] 8
Or if we need in terms of only x, we can sort them first, take only unique values and then get the value by it's index.
get_nth_smallest_value <- function(n) {
unique(sort(x))[n]
}
get_nth_smallest_value(2)
#[1] 6
get_nth_smallest_value(3)
#[1] 7
I have the following multiset X, in which I want to find the distances between all the numbers. Is there any way to integrate this into a FOR LOOP so that If I was given a different sized multiset, I wouldn't have to manually do it like i did below?
the final answer IS [0,2, 2, 3, 3, 4, 5, 6, 7, 8, 10] (sorted) for this example
X=c(0,10,8,3,6)
L=length(X)
print(L)
##for(i in seq(from=1, to=L )){}
print(abs(X[1]-X[2]), abs(X[1]-X[3]),
abs(X[1]-X[4]), abs(X[1]-X[5]),
abs(X[1]-X[6]),
abs(X[2]-X[3]), abs(X[2]-X[4]),
abs(X[2]-X[5]), abs(X[2]-X[6]),
abs(X[3]-X[4]), abs(X[3]-X[5]),
abs(X[3]-X[6]),
abs(X[4]-X[5]), abs(X[4]-X[6]),
abs(X[5]-X[6])
)
You may see this vector as a column vector and apply dist:
sort(dist(X))
# [1] 2 2 3 3 4 5 6 7 8 10
I want to sort an array in increasing way, by the difference of the biggest and smallest number.
Without loops.
I think I need a sort that I can give a condition but i can't find how.
Something like this:
sort(arr, decreasing = FALSE, by = max(a) - min(a))
sort(arr, decreasing = FALSE, condition = max(a) - min(a))
The sorted array have to look like this. The difference from the first and second number is the smallest for all numbers int the array, the difference from the second and the third is the second smallest ......
Example: // I thing is like this
array(22, 2, 32, 3, 6, 9, 7, 23, 11, 13)
sorted_array(9, 11, 7, 13, 6, 22, 3, 23, 2, 32)
I thing another way is to construct the sorted array be putting on the last position the biggest number after that the smallest, the second biggest, the second smallest, ...
Sorry for the bad explanation.
This is a idea how it could work, but only for arrays where the length is even. If you want to use this solution and you have uneven arrays, you can work with if. I need to admit that it have to be urgent, that I would like to use a construction like this instead of a loop.
x <- c(22, 2, 32, 3, 6, 9, 7, 23, 11, 13)
n <- length(x)
m <- floor(n/2)
rev(
as.numeric(
rbind(
sort(x)[n-c(0:(m-1))],
sort(x)[1:m]
)
)
)
I attempted to come up with a non-for-loop construction. So I first sorted the sequence and then split it in two halves by naming the first half "a_N" and second half "b_N", then "folded it " into a two column matrix with the first half reversed, and finally read it out by unfolding with c:
my_arr <- c(22, 2, 32, 3, 6, 9, 7, 23, 11, 13)
names(my_arr) <- paste0( rep( c("a","b"), each=length(my_arr)/2), order(my_arr) )
c( rbind( sort( my_arr[grep("a", names(my_arr))], decreasing=TRUE), #first half
my_arr[grep("b", names(my_arr))]) ) # second half
#[1] 9 11 7 13 6 22 3 23 2 32
You can see the intermediate value of the matrix:
rbind( sort( my_arr[grep("a", names(my_arr))], decreasing=TRUE), my_arr[grep("b", names(my_arr))])
a5 a4 a3 a2 a1
[1,] 9 7 6 3 2
[2,] 11 13 22 23 32
And since R matrices are read out in column order you get the desired interleaving with c() which also removes the names.
How would I create an R sequence that returns
1, 4, 9, 16, 25, . . . , 100
I have tried
seq(from=1,to=100,by=(seq(from=3,to=100,by=2)))
but this gives me the completely wrong answer
Also, to get:
1, −2, 3, 4, −5, 6, 7, −8, . . . , −98, 99, 100
would I use a true false vector?
Try
v <- (1:10)^2
# [1] 1 4 9 16 25 36 49 64 81 100
All of the mathematical operations are vectorized in R, so just square the vector and you're good to go.
For the second sequence the rep function will be helpful. The code
rep(c(1,-1,1),33)*(1:99)
generates the required sequence from 1 to 99.