If I know, for example,
DOGE/EUR=1:1.323
EUR/USD=1:1.12
JPY/Silver=1:0.81
EUR/JPY=1:12
How can I calculate any other value pair I might be interested, given the known value pairs?
DOGE/USD=?
DOGE/Silver=?
Nils.
DOGE/EUR=1:1.323
EUR/USD=1:1.12 => EUR=USD:1.12
DOGE/EUR=DOGE/(USD:1.12)=1:1.323 => DOGE/USD=1:(1.12*1.323)
DOGE/USD=1:1.48176
analogically
DOGE/Silver=1:(1.323*12*0.81)=1:12.85956
Ок?
Related
Say I am tossing a fair coin where 'tails' is assigned the value x = -1/2 and 'heads' is assigned x = 1/2.
I do this N times and I want to obtain the sum. This is what I have tried:
p = 0.5
N = 1e4
X(N,p)=(rand(N).<p)
I know this is incomplete but when I check (rand(N).<p) I see an array consisting of true, false. I interpret this as 'Tails' or 'Heads'. However, I don't know how to assign the values 1/2 and -1/2 to each of these elements in order for me to find the sum. If I simply use sum((rand(N).<p)) I do get an integer value, but I don't think this is the right way to do it because I haven't specified the values 1/2 and -1/2 anywhere.
Any help is greatly appreciated.
As indicated by the comments already, you want to do
sum(rand([-0.5, 0.5], N))
where N must be an integer (you wrote N=1e4, therefore typeof(N) == Float64 and rand won't work).
The documentation of rand (obtained by ?rand) describes what rand(S, N) does:
Pick a random element or array of random elements from the set of
values specified by S
Here, S can be an optional indexable collection, an array of values in your case (or a type like Int). So, above S = [-0.5, 0.5] and rand draws N random elements from this collection, which we can afterwards sum up.
Assigning specific values to a boolean array
Since this is the title of your question, and the answer above doesn't actually address this, let me comment on this as well.
You could do sum((rand(N).<p)-0.5), i.e. you shift all the ones to 0.5 and all the zeros to -0.5 to get the wanted result. Note that this is a general strategy: Let's say you want true to be a and false to be b, where a and b are numbers. You achieve this by (rand(N).<p)*(a-b) + b.
However, beyond being more "complicated", sum((rand(N).<p)-0.5) will allocate temporary arrays, first one of booleans, then one of numbers, the latter of which will eventually go into sum. Because of these unnecessary allocations this approach will be slower than the solution above.
I am new to R! I would like to know how to check a vector for values greater than 1.96 and then increment a variable n by 1 for every value greater than 1.96. I know an example in C++ for an array
for(i<101)
{
if(samplespace[i]>1.96)
n=n+1;
}
Now I just need to know how to do the same thing with a vector in R. Any help would be much appreciated!
Why should I use the sum()-Function here and not the length() Function?
sum(age<=20)/length(age)
Age is a vector with numeric values representing the age.
Why should I not use:
length(age<=20)/length(age)
These function should return the same values, don't they?
No, they shouldn't. The result of:
age<=20
is a boolean vector with the same dimensions of the variable 'age'.
Therefore,
sum(age<=20)
counts how many values are lower than or equal to 20, while
length(age<=20)
will return the length of 'age'
BTW,
sum(age<=20)/length(age)
can be more simply obtained via:
mean(age<=20)
I'm not sure what language you're referring to, but, in general principles:
age<=20 is a condition, returning yes/no, and presumably 1/0, on each item (or record)
sum(age<=20) will sum up the Ones. It gives you the number of items where the condition is satisfied.
On the other hand, length returns a number: the count of your array/vector/recordset. age is a vector, age<=20 is not, it's a boolean expression.
If you ask length(age<=20), you will most likely get 1 as it is the length of the result of age<=20, which is a scalar value.
I would like to create a function that looks at a column of values. from those values look at each value individually, and asses which of the other data points value is closest to that data point.
I'm guessing it could be done by checking the length of the data frame, making a list of the respective length in steps of 1. Then use that list to reference which cell is being analysed against the rest of the column. though I don't know how to implement that.
eg.
data:
20
17
29
33
1) is closest to 2)
2) is closest to 1)
3) is closest to 4)
4) is closest to 3)
I found this example which tests for similarity but id like to know what letter is assigns to.
x=c(1:100)
your.number=5.43
which(abs(x-your.number)==min(abs(x-your.number)))
Also if you know how I could do this, could you expain the parts of the code and what they mean?
I wrote a quick function that does the same thing as the code you provided.
The code you provided takes the absolute value of the difference between your number and each value in the vector, and compares that the minimum value from that vector. This is the same as the which.min function that I use below. I go through my steps below. Hope this helps.
Make up some data
a = 1:100
yourNumber = 6
Where Num is your number, and x is a vector
getClosest=function(x, Num){
return(which.min(abs(x-Num)))
}
Then if you run this command, it should return the index for the value of the vector that corresponds to the closest value to your specified number.
getClosest(x=a, Num=yourNumber)
Hello I am new to R and I can't find the way to do exactly what I want to. I have a vector of x numbers, and what i want to do is order it in increasing order, and then start making subtractions like this (let's say the vecto has 100 numbers for example):
[x(100)-x(99)]+[x(99)-x(98)]+[x(98)-x(97)]+[x(97)-x(96)]+...[x(2)-x(1)]
and then divide all that sum by the number of elements the vector has, in this case 100.
The only thing that I am able to do at the moment is order the vector with:
sort(nameOfTheVector)
Sorry for my bad English.
diff returns suitably lagged and iterated differences. In your case you want the default single lag. sum will return the sum any arguments passed to it, so....
sum(diff(sort(nameOfTheVector))) / length(nameOfTheVector)