How can I represent any negative value mathematically using summation? - math

For example, I know that I can represent the value -1 in this way, where "n" is the bit size of the type, n-1 is the most significant bit, and the value -2, which gives the complement representation of 2, plus the sum of the sum described below, now, I would like to know how I can use the sum, or any other type of calculation, to represent any negative value, in the form of a mathematical demonstration.
Assuming n has 8 bits, 8 - 1, 7, that is -2^7 = -128 + 127 = -1,
I'm saying the summation will go through all the bits, such as 2^0 + 2^1, ..., 2^6, ie I ignore the most significant bit.

Iiuc, the formula you gave does exactly what you're asking to do. You have answered your own question.
Let's do examples with n=4. For a reference, we can count in the negative direction to get all possibilities:
-1 = 1111
-2 = 1110
-3 = 1101
-4 = 1100
-5 = 1011
-6 = 1010
-7 = 1001
-8 - 1000
So now let's see if the formula gives correct results. By convention, a 4-digit binary number representation is four bits numbered 3 down to 0: b_3 b_2 b_1 b_0.
Example: 1111
So we have b_0 = b_1 = b_2 = b_3 = 1.
Substituting, v = -2^3x1 + 1x1 + 2x1 + 4x1 = -8 + 7 = -1.
Example: 1010
So we have b_0 = 0, b_1 = 1, b_2 = 0, b_3 = 1.
Substituting, v = -2^3x1 + 1x0 + 2x1 + 4x0 = -8 + 2 = -6.
You can work all the other values. They'll be correct.
The formula works for positive numbers, too. In that case b_3 = 0, so the -8 term goes away.

Related

Hoping for help to translate a thought experiment into R code, using randomization

I'm more experienced with R than many of my peers, yet it sometimes takes hours to move a novel-to-me concept into the code line, and usually a few more to get a successful output. I don't know how to describe this in R language, so I hope you can help me- either with sample code, or pointing me in the right direction.
I have c(X1,X2,X3,...Xn) for starting variable, a non-random numeric value.
I have c(Y1,Y2,Y3,...Yn) for change variable, a non-random numeric value denoting by how much to change X, give or take, and a value between 0-10.
I have c(Z1,Z2,Z3,...Zn) which is the min and max range of X.
What I want to observe is the random sampling of all numbers X, which have all randomly had corresponding Y variable subtracted or added to them. What I'm trying to ask in this problem, is how many times will I draw X values which are exactly the X values which I initially input as well as give or take only a low Y value.
For instance,
Exes<-c(135,462,579,222)
Whys<-c(1,3,3,2)
Zees<-c(c(115,155),c(450,474),c(510,648),c(200,244))
First iteration: X=c(135,562,579,222), second iteration: X=c(130,471,585,230)<- as you can see, X of second iteration has changed by (-5*Y1), (+3*Y2), (+2*Y3), and (+11*Y4)
What I want to output is a list of randomized X values which have changed by only a factor of their corresponding Y value, and always fall within the range of given Z values. Further, I want to examine how many times at least one- and only one- X value will be be significantly different from the corresponding,starting input X.
I feel like I'm not wording the question succinctly, but I also feel that this is why I've posted. I'm not trying to ask for hand-holding, but rather seeking advice.
I am not sure that I understood the question, do you want to reiterate the process numerous times? is it for the purpose of simulation?. Here is a start of a solution.
library(dplyr)
x <- c(135,462,579,222)
y <- c(1,3,3,2)
z.lower <- c(115, 450, 510, 200)
z.upper <- c(155, 474, 648, 244)
temp.df <- data.frame(x, y, z.lower, z.upper)
df %>%
mutate(samp = sample(seq(-10, 10, 1), nrow(temp.df))) %>% ### Sample numbers between 0 and 10
mutate(new.val = x + samp * y) %>% ### Create new X
mutate(is.bound = new.val < z.upper & new.val > z.lower) ### Check that falls in bounds
x y z.lower z.upper samp new.val is.bound
1 135 1 115 155 -10 125 TRUE
2 462 3 450 474 10 492 FALSE
3 579 3 510 648 8 603 TRUE
4 222 2 200 244 6 234 TRUE
For this dataset, this is a possibility:
Exes<-c(135,462,579,222)
Whys<-c(1,3,3,2)
Zees<-c(c(115,155),c(450,474),c(510,648),c(200,244))
n = 10000
x_range_l <- split(Zees, rep(seq_len(length(Zees) / 2), each = 2))
mapply(function(y, x_range) sample(seq(from = x_range[1], to = x_range[2], by = y), size = n, replace = T),
Whys, x_range_l)
Note that this option depends more on the Zees than the Exes. A more complete way to do it would be:
Exes<-c(135,462,579,222)
Whys<-c(1,3,3,2)
Why_Range <- c(20, 4, 13, 11)
x_range_l <- Map(function(x, y, rng) c(x - y * rng, x + y * rng), Exes, Whys, Why_Range)
n = 10000
mapply(function(y, x_range) sample(seq(from = x_range[1], to = x_range[2], by = y), size = n, replace = T),
Whys, x_range_l)

How do I get all solutions from this system?

I am new to linear algebra and I am trying to solve a system of three equations with five unknowns. The system I have is the following:
x1 + x2 + x3 + x4 + x5 = 1
-x1 + x2 + x3 - 2x4 - 2x5 = 1
2x1 + 2x2 - x3 - x4 + x5 = 1
So what I did was set up the augmented matrix like this:
1 1 1 1 1 1
-1 1 1 -2 -2 1
2 2 -1 -1 1 1
Then I try to obtain an identity matrix on the left side and end up with the following:
1 0 0 3/2 3/2 0
0 1 0 -3/2 -5/6 2/3
0 0 1 1 1/3 1/3
So I think the answer is x1 = 0, x2 = 2/3 and x3 = 1/3
But when I look in my answer sheet it reads:
(x1, x2, x3, x4, x5) = (0, 2/3, 1/3, 0, 0) + s(−3/2, 3/2, −1, 1, 0) + t(−3/2, 5/6, −1/3, 0, 1)
I have no idea how to interpret this. My x1,x2,x3 seems to match the first three in the first five-tuple but what are the other two five-tuples? Can someone explain what I am missing here? I would highly appreciate it.
A system of equations can be represented in matrix form as
Ax = b
where A is the matrix of coefficients, x is the column vector (x1, ..., xn) and b is the column vector with as many entries as equations are.
When b is not 0 we say that the system is not homogeneous. The associated homogeneous system is
Ax = 0
where the 0 on the right is again a column vector.
When you have a non-homogeneous system, like in this case, the general solution has the form
P + G
where P is any particular solution and G is the generic solution of the homogeneous system.
In your case the vector
P = (0, 2/3, 1/3, 0, 0)
satisfies all the equations and is therefore a valid particular solution.
The other two vectors (−3/2, 3/2, −1, 1, 0) and (−3/2, 5/6, −1/3, 0, 1) satisfy the homogeneous equations (take a moment to check this). And since there are 3 (independent) equations with 5 unknowns (x1..x5), the space of homogenous solutions can be generated by these two vectors (again because they are independent).
So, to describe the space of all homogeneous solutions you need two scalar variables s and t. In other words
G = s(−3/2, 3/2, −1, 1, 0) + t(−3/2, 5/6, −1/3, 0, 1)
will generate all homogeneous solutions as s and t take all posible real values.

Hill Cipher Encryption

I'd have a quick question over hill cipher encrytion.
Say I have an input
[8 9]
I was to encrypt and an encryption matrix
[1 2]
[3 4]
I multiply the matrix by the input as:
[1 2][8] = [26 60]
[3 4][9]
I am now supposed to take mod 26 of both values of the output vector and convert them back to a corresponding letter using the table
1 = 'a', 2 = 'b', ... , 'z' = 26
However, considering 26 mod 26 is 0, how should I proceed?
Hill cipher associates with each letter, a number (generally from 0 to 25) and there's a logic behind modulo 26. We have 26 letters in English alphabet set, hence we do modulo 26.
Moreover, whenever, we do modulo n, the possible remainders are :
0, 1, 2, . . . , n-1.
Example: x mod 4, (x is any positive integer), we can get only 4 remainders: 0, 1,2,3 ....... never 4.
So, I suggest, one should number a=0, b=1, c=2, .. , z=25... you won't face this problem....
Hope this answered your question. :)

Unity3D Math calculation issues

I'm not so skilled in the ways of math and retying to write the following formula.
40x^2 + 360x
Results are supposed to be as follows
x = 1 the result should be 400
x = 2 the result should be 900
x = 3 the result should be 1,600
x = 4 the result should be 2,100
x = 5 the result should be 2,800
result = 40 * (int)Mathf.Pow((float)x, 2f) + (360 * x)
x = 1 I get 400
x = 2 I get 880
x = 3 I get 1440
...etc
What am I doing wrong here?
It appears the answers you are getting from the code are the right ones.
As an example, in the top answers, where x = 2, 900 is not possible as an answer. Because; 360 * 2 = 720. To get 900, you need to add 180, which is not any multiple of 40, the nearest would be 160. Which adding that to 720 equals 880, such as in the answer from the code.

Probabilty heatmap in ggplot

I asked this question a year ago and got code for this "probability heatmap":
numbet <- 32
numtri <- 1e5
prob=5/6
#Fill a matrix
xcum <- matrix(NA, nrow=numtri, ncol=numbet+1)
for (i in 1:numtri) {
x <- sample(c(0,1), numbet, prob=c(prob, 1-prob), replace = TRUE)
xcum[i, ] <- c(i, cumsum(x)/cumsum(1:numbet))
}
colnames(xcum) <- c("trial", paste("bet", 1:numbet, sep=""))
mxcum <- reshape(data.frame(xcum), varying=1+1:numbet,
idvar="trial", v.names="outcome", direction="long", timevar="bet")
library(plyr)
mxcum2 <- ddply(mxcum, .(bet, outcome), nrow)
mxcum3 <- ddply(mxcum2, .(bet), summarize,
ymin=c(0, head(seq_along(V1)/length(V1), -1)),
ymax=seq_along(V1)/length(V1),
fill=(V1/sum(V1)))
head(mxcum3)
library(ggplot2)
p <- ggplot(mxcum3, aes(xmin=bet-0.5, xmax=bet+0.5, ymin=ymin, ymax=ymax)) +
geom_rect(aes(fill=fill), colour="grey80") +
scale_fill_gradient("Outcome", formatter="percent", low="red", high="blue") +
scale_y_continuous(formatter="percent") +
xlab("Bet")
print(p)
(May need to change this code slightly because of this)
This is almost exactly what I want. Except each vertical shaft should have different numbers of bins, ie the first should have 2, second 3, third 4 (N+1). In the graph shaft 6 +7 have the same number of bins (7), where 7 should have 8 (N+1).
If I'm right, the reason the code does this is because it is the observed data and if I ran more trials we would get more bins. I don't want to rely on the number of trials to get the correct number of bins.
How can I adapt this code to give the correct number of bins?
I have used R's dbinom to generate the frequency of heads for n=1:32 trials and plotted the graph now. It will be what you expect. I have read some of your earlier posts here on SO and on math.stackexchange. Still I don't understand why you'd want to simulate the experiment rather than generating from a binomial R.V. If you could explain it, it would be great! I'll try to work on the simulated solution from #Andrie to check out if I can match the output shown below. For now, here's something you might be interested in.
set.seed(42)
numbet <- 32
numtri <- 1e5
prob=5/6
require(plyr)
out <- ldply(1:numbet, function(idx) {
outcome <- dbinom(idx:0, size=idx, prob=prob)
bet <- rep(idx, length(outcome))
N <- round(outcome * numtri)
ymin <- c(0, head(seq_along(N)/length(N), -1))
ymax <- seq_along(N)/length(N)
data.frame(bet, fill=outcome, ymin, ymax)
})
require(ggplot2)
p <- ggplot(out, aes(xmin=bet-0.5, xmax=bet+0.5, ymin=ymin, ymax=ymax)) +
geom_rect(aes(fill=fill), colour="grey80") +
scale_fill_gradient("Outcome", low="red", high="blue") +
xlab("Bet")
The plot:
Edit: Explanation of how your old code from Andrie works and why it doesn't give what you intend.
Basically, what Andrie did (or rather one way to look at it) is to use the idea that if you have two binomial distributions, X ~ B(n, p) and Y ~ B(m, p), where n, m = size and p = probability of success, then, their sum, X + Y = B(n + m, p) (1). So, the purpose of xcum is to obtain the outcome for all n = 1:32 tosses, but to explain it better, let me construct the code step by step. Along with the explanation, the code for xcum will also be very obvious and it can be constructed in no time (without any necessity for for-loop and constructing a cumsum everytime.
If you have followed me so far, then, our idea is first to create a numtri * numbet matrix, with each column (length = numtri) having 0's and 1's with probability = 5/6 and 1/6 respectively. That is, if you have numtri = 1000, then, you'll have ~ 834 0's and 166 1's *for each of the numbet columns (=32 here). Let's construct this and test this first.
numtri <- 1e3
numbet <- 32
set.seed(45)
xcum <- t(replicate(numtri, sample(0:1, numbet, prob=c(5/6,1/6), replace = TRUE)))
# check for count of 1's
> apply(xcum, 2, sum)
[1] 169 158 166 166 160 182 164 181 168 140 154 142 169 168 159 187 176 155 151 151 166
163 164 176 162 160 177 157 163 166 146 170
# So, the count of 1's are "approximately" what we expect (around 166).
Now, each of these columns are samples of binomial distribution with n = 1 and size = numtri. If we were to add the first two columns and replace the second column with this sum, then, from (1), since the probabilities are equal, we'll end up with a binomial distribution with n = 2. Similarly, instead, if you had added the first three columns and replaced th 3rd column by this sum, you would have obtained a binomial distribution with n = 3 and so on...
The concept is that if you cumulatively add each column, then you end up with numbet number of binomial distributions (1 to 32 here). So, let's do that.
xcum <- t(apply(xcum, 1, cumsum))
# you can verify that the second column has similar probabilities by this:
# calculate the frequency of all values in 2nd column.
> table(xcum[,2])
0 1 2
694 285 21
> round(numtri * dbinom(2:0, 2, prob=5/6))
[1] 694 278 28
# more or less identical, good!
If you divide the xcum, we have generated thus far by cumsum(1:numbet) over each row in this manner:
xcum <- xcum/matrix(rep(cumsum(1:numbet), each=numtri), ncol = numbet)
this will be identical to the xcum matrix that comes out of the for-loop (if you generate it with the same seed). However I don't quite understand the reason for this division by Andrie as this is not necessary to generate the graph you require. However, I suppose it has something to do with the frequency values you talked about in an earlier post on math.stackexchange
Now on to why you have difficulties obtaining the graph I had attached (with n+1 bins):
For a binomial distribution with n=1:32 trials, 5/6 as probability of tails (failures) and 1/6 as the probability of heads (successes), the probability of k heads is given by:
nCk * (5/6)^(k-1) * (1/6)^k # where nCk is n choose k
For the test data we've generated, for n=7 and n=8 (trials), the probability of k=0:7 and k=0:8 heads are given by:
# n=7
0 1 2 3 4 5
.278 .394 .233 .077 .016 .002
# n=8
0 1 2 3 4 5
.229 .375 .254 .111 .025 .006
Why are they both having 6 bins and not 8 and 9 bins? Of course this has to do with the value of numtri=1000. Let's see what's the probabilities of each of these 8 and 9 bins by generating probabilities directly from the binomial distribution using dbinom to understand why this happens.
# n = 7
dbinom(7:0, 7, prob=5/6)
# output rounded to 3 decimal places
[1] 0.279 0.391 0.234 0.078 0.016 0.002 0.000 0.000
# n = 8
dbinom(8:0, 8, prob=5/6)
# output rounded to 3 decimal places
[1] 0.233 0.372 0.260 0.104 0.026 0.004 0.000 0.000 0.000
You see that the probabilities corresponding to k=6,7 and k=6,7,8 corresponding to n=7 and n=8 are ~ 0. They are very low in values. The minimum value here is 5.8 * 1e-7 actually (n=8, k=8). This means that you have a chance of getting 1 value if you simulated for 1/5.8 * 1e7 times. If you check the same for n=32 and k=32, the value is 1.256493 * 1e-25. So, you'll have to simulate that many values to get at least 1 result where all 32 outcomes are head for n=32.
This is why your results were not having values for certain bins because the probability of having it is very low for the given numtri. And for the same reason, generating the probabilities directly from the binomial distribution overcomes this problem/limitation.
I hope I've managed to write with enough clarity for you to follow. Let me know if you've trouble going through.
Edit 2:
When I simulated the code I've just edited above with numtri=1e6, I get this for n=7 and n=8 and count the number of heads for k=0:7 and k=0:8:
# n = 7
0 1 2 3 4 5 6 7
279347 391386 233771 77698 15763 1915 117 3
# n = 8
0 1 2 3 4 5 6 7 8
232835 372466 259856 104116 26041 4271 392 22 1
Note that, there are k=6 and k=7 now for n=7 and n=8. Also, for n=8, you have a value of 1 for k=8. With increasing numtri you'll obtain more of the other missing bins. But it'll require a huge amount of time/memory (if at all).

Resources