# How can I determine which of two sequences of coin flips is real and which is fake?

This is an interesting problem I came across. I'm attempting to write a Python program to get a solution to it; however, I'm not sure how to proceed. So far, I know that I would expect the counts of heads to follow a binomial, and length of runs (of tails, heads, or both) to follow a geometric.

Below are two sequences of 300 “coin flips” (H for heads, T for tails). One of these is a true sequence of 300 independent flips of a fair coin. The other was generated by a person typing out H’s and T’s and trying to seem random. Which sequence is truly composed of coin flips?

Sequence 1:

TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHH TTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHH TTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHT THHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHT HTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTT HHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT

Sequence 2:

HTHHHTHTTHHTTTTTTTTHHHTTTHHTTTTHHTTHHHTTHTHTTTTTTH THTTTTHHHHTHTHTTHTTTHTTHTTTTHTHHTHHHHTTTTTHHHHTHHH TTTTHTHTTHHHHTHHHHHHHHTTHHTHHTHHHHHHHTTHTHTTTHHTTT THTHHTTHTTHTHTHTTHHHHHTTHTTTHTHTHHTTTTHTTTTTHHTHTH HHHTTTTHTHHHTHHTHTHTHTHHHTHTTHHHTHHHHHHTHHHTHTTTHH HTTTHHTHTTHHTHHHTHTTHTTHTTTHHTHTHTTTTHTHTHTTHTHTHT

Both sequences have 148 heads, two less than the expected number for a 0.5 probability of heads.

• Naive qustion : Does the question means: can I find with that porbablity that such sequence is generated by human is big enough ? Can I compute such probablity ? Every sequence is possible, so this is not a proof but only possibility. Am I right ?
May 7 at 8:53
• (I find it truly amusing that somebody voted to close this thread because it asks for opinions. If that were true, we should close all threads here on CV that use statistical analysis to compare data--and that would leave us with nothing but lists of references!)
– whuber
May 7 at 13:42
• Compress both strings and see which one comes out shorter. May 7 at 13:58
• @whuber That comment is probably a reference to (approximate) Kolmogorov complexity. See algorithmically random sequence. May 7 at 15:45
• @whuber With very high probability, a truly random sequence cannot be compressed. Compression methods can leverage many kinds of bias, not only if one symbol appears more overall, but also if some subsequences appear more than others (as here, H is more likely to be followed by T and vice versa, per your answer). May 7 at 18:20

This is a variant on a standard intro stats demonstration: for homework after the first class I have assigned my students the exercise of flipping a coin 100 times and recording the results, broadly hinting that they don't really have to flip a coin and assuring them it won't be graded. Most will eschew the physical process and just write down 100 H's and T's willy-nilly. After the results are handed in at the beginning of the next class, at a glance I can reliably identify the ones who cheated. Usually there are no runs of heads or tails longer than about 4 or 5, even though in just 100 flips we ought to see a longer run that that.

This case is subtler, but one particular analysis stands out as convincing: tabulate the successive ordered pairs of results. In a series of independent flips, each of the four possible pairs HH, HT, TH, and TT should occur equally often--which would be $$(300-1)/4 = 74.75$$ times each, on average.

Here are the tabulations for the two series of flips:

   Series 1    Series 2
H   T        H  T
H  46 102       71 76
T 102  49       77 75

The first is obviously far from what we might expect. In that series, an H is more than twice as likely ($$102:46$$) to be followed by a T than by another H; and a T, in turn, is more than twice as likely ($$102:49$$) to be followed by an H. In the second series, those likelihoods are nearly $$1:1,$$ consistent with independent flips.

A chi-squared test works well here, because all the expected counts are far greater than the threshold of 5 often quoted as a minimum. The chi-squared statistics are 38.3 and 0.085, respectively, corresponding to p-values of less than one in a billion and 77%, respectively. In other words, a table of pairs as imbalanced as the second one is to be expected (due to the randomness), but a table as imbalanced as the first happens less than one in every billion such experiments.

(NB: It has been pointed out in comments that the chi-squared test might not be applicable because these transitions are not independent: e.g., an HT can be followed only by a TT or TH. This is a legitimate concern. However, this form of dependence is extremely weak and has little appreciable effect on the null distribution of the chi-squared statistic for sequences as long as $$300.$$ In fact, the chi-squared distribution is a great approximation to the null sampling distribution even for sequences as short as $$21,$$ where the counts of the $$21-1=20$$ transitions that occur are expected to be $$20/4=5$$ of each type.)

If you know nothing about chi-squared tests, or even if you do but don't want to program the chi-square quantile function to compute a p-value, you can achieve a similar result. First develop a way to quantify the degree of imbalance in a $$2\times 2$$ table like this. (There are many ways, but all the reasonable ones are equivalent.) Then generate, say, a few hundred such tables randomly (by flipping coins--in the computer, of course!). Compare the imbalances of these two tables to the range of imbalances generated randomly. You will find the first sequence is far outside the range while the second is squarely within it.

This figure summarizes such a simulation using the chi-squared statistic as the measure of imbalance. Both panels show the same results: one on the original scale and the other on a log scale. The two dashed vertical lines in each panel show the chi-squared statistics for Series 1 (right) and Series 2 (left). The red curve is the $$\chi^2(1)$$ density. It fits the simulations extremely well at the right (higher values). The discrepancies for low values occur because this statistic has a discrete distribution which cannot be well approximated by any continuous distribution where it takes on small values -- but for our purposes that makes no difference at all.

• Why do you say, "This case is subtler" (than just looking for runs)? OP's sequence 1 has length 300 (longer than the 100 you mention) and its longest run is a single HHHH. So it doesn't seem subtle at all, but rather a slam dunk for the "at a glance" method you describe. May 7 at 18:11
• It's not proper to apply the chi squared test to the contingency table given since consecutive pairs are overlapping and not independent, so p-values will be exaggerated. I suspect the result will be similar after addressing this though.
– Paul
May 7 at 18:11
• @Paul That's an excellent point, which I have neglected to discuss (and it's no excuse to claim, as I was hoping to do, that it's implicitly handled correctly in the last paragraph, because I did not advertise that fact).
– whuber
May 7 at 18:13
• @klm123 But as long as you don't actually try a bunch of tests before finding an improbable result (p-hacking), the result is still significant. The test(s) should be chosen in advance. There are also ways of correcting p-values if you run multiple tests. Yes, there is the potential to be misleading if you only report the significant test and not the others -- this has contributed to the "replication crisis". May 7 at 23:07
• My answer talks about why this test in particular is well-motivated. People tend to think that non-repeated values are more random than repeated values. (Which is true in some sense, but people believe it to a greater extent / in more generality than is actually true.)
– Paul
May 7 at 23:25

There are two very good answers as of writing this, and so let me add a needlessly complex yet interesting approach to this problem.

I think one way to operationalize the human generated vs truly random question is to ask if the flips are autocorrelated. The hypothesis here being that humans will attempt to appear random by not having too many strings of one outcome, hence switching from heads to tails and tails to heads more often than would be observed in a truly random sequence.

Whuber examines this nicely with a 2x2 table, but because I am a Bayesian and a glutton for punishment let's write a simple model in Stan to estimate the lag-1 autocorrelation of the flips. Speaking of Whuber, he has nicely laid out the data generating process in this post. You can read his answer to understand the data generating process.

Let $$\rho$$ be the lag 1 autocorrelation of the flips, and let $$q$$ be the proportion of flips which are heads in the sequence. A fair coin should have 0 autocorrelation, so we are looking for our estimate of $$\rho$$ to be close to 0. From there, we only need to count the number of occurrences of $$H,H$$, $$H, T$$, $$T, H$$ and $$T,T$$ in the sequence.

The Stan model is shown below

data{

int y_1_1; //number of concurrent 1s
int y_0_1; //number of 0,1 occurrences
int y_1_0; //number of 1,0 occurrences
int y_0_0; //number of concurrent 0s

}
parameters{
real<lower=-1, upper=1> rho;
real<lower=0, upper=1> q;

}
transformed parameters{

real<lower=0, upper=1> prob_1_1 = q + rho*(1-q);
real<lower=0, upper=1> prob_0_1 = (1-q)*(1-rho);
real<lower=0, upper=1> prob_1_0 = q*(1-rho);
real<lower=0, upper=1> prob_0_0 = 1 - q + rho*q;
}
model{
q ~ beta(1, 1);
target += y_1_1 * bernoulli_lpmf(1| prob_1_1);
target += y_0_1 * bernoulli_lpmf(1| prob_0_1);
target += y_1_0 * bernoulli_lpmf(1| prob_1_0);
target += y_0_0 * bernoulli_lpmf(1| prob_0_0);

}

Here, I've placed a uniform prior on the autocorrelation

$$\rho \sim \mbox{Uniform}(-1, 1)$$

and on the probability of a head

$$q \sim \operatorname{Beta}(1, 1)$$

Our likelihood is Bernoulli, and I have weighted the likelihood by the number of occurrences of each pair of outcomes. The probabilities of each outcome (e.g. probability of observing a heads conditioned on the previous flip being a heads) is provided by Whuber in his linked answer. Let's run our model and compare posterior distributions for the two sequences

The estimated auto correlation for sequence 1 is -0.36, and the estimated autocorrelation for sequence 2 is -0.02 (close enough to 0). If I was a betting man, I'd put my money on sequence 1 being the sequence generated by a human. The negative autocorrelation means that when we see a heads/tails we are more likely to see a tails/heads! This observation lines up nicely with the 2x2 table provided by Whuber.

### Code

The plot I present is made in R, but here is some python code to do the same thing since you asked

import matplotlib.pyplot as plt
import cmdstanpy

# You will need to install cmdstanpy prior to running this code

# Write the stan model as a string.  We will then write it to a file
stan_code = '''
data{

int y_1_1; //number of concurrent 1s
int y_0_1; //number of 0,1 occurences
int y_1_0; //number of 1,0 occurences
int y_0_0; //number of concurrent 0s

}
parameters{
real<lower=-1, upper=1> rho;
real<lower=0, upper=1> q;
}
transformed parameters{
real<lower=0, upper=1> prob_1_1 = q + rho*(1-q);
real<lower=0, upper=1> prob_0_1 = (1-q)*(1-rho);
real<lower=0, upper=1> prob_1_0 = q*(1-rho);
real<lower=0, upper=1> prob_0_0 = 1 - q + rho*q;
}
model{
q ~ beta(1, 1);
target += y_1_1 * bernoulli_lpmf(1| prob_1_1);
target += y_0_1 * bernoulli_lpmf(1| prob_0_1);
target += y_1_0 * bernoulli_lpmf(1| prob_1_0);
target += y_0_0 * bernoulli_lpmf(1| prob_0_0);

}
'''

# Write the model to a temp file
with open('model_file.stan', 'w') as model_file:
model_file.write(stan_code)

# Compile the model
model = cmdstanpy.CmdStanModel(stan_file='model_file.stan', compile=True)

# Co-occuring counts for heads (1) and tails (0) for each sequence
data_1 = dict(y_1_1 = 46, y_0_0 = 49, y_0_1 = 102, y_1_0 = 102)
data_2 = dict(y_1_1 = 71, y_0_0 = 75, y_0_1 = 76, y_1_0 = 77)

# Fit each model
fit_1 = model.sample(data_1, show_progress=False)
rho_1 = fit_1.stan_variable('rho')

fit_2 = model.sample(data_2, show_progress=False)
rho_2 = fit_2.stan_variable('rho')

# Make a pretty plot
fig, ax = plt.subplots(dpi = 240, figsize = (5, 3))

ax.set_xlim(-1, 1)
ax.hist(rho_1, color = 'blue', alpha = 0.5, edgecolor='k', label='Sequence 1')
ax.hist(rho_2, color = 'red', alpha = 0.5, edgecolor='k', label='Sequence 2')
ax.legend()
$$$$

• +1 I developed my tabular explanation by going through the exercise you lay out here. I plotted the PACF functions of random sequences and compared them, visually, to the PACF functions of the two sequences in the question. One plot--the first sequence--stood out: it had a very negative lag-one partial autocorrelation coefficient. But motivating and explaining the PACF seemed like overkill for this problem ;-). The more enduring lesson, illustrated here and in the post by @COOLSerdash, is that by finding a suitable way to visualize data, we can discover otherwise hidden things about them.
– whuber
May 7 at 13:28
• (+1) Interesting resolution that does not rely on insufficient statistics, contrary to the others!, but I would have gone fully Markov and put a prior distribution on both $(p_{11},p_{10})$ and $(p_{01},p_{00})$ rather than introducing a correlation $\rho$, but it is unlikely the result would differ. More generally, the alternative to being an iid Uniform sequence could be anything, so restricting to an order one Markov is favouring the null. May 9 at 8:09
• @Xi'an You're right in that the result is not much different. The reason I prefer the $\rho$, $q$ parameterization is because we actually have good priors on these (despite what my answer uses lol). Humans have an intuitive sense for when they are acting too correlated and when they are acting too biased, so we know $\rho$ is going to be close to 0 and $q$ close to 0.5. But, you could rewrite this model using a multinomial likelihood and place priors on the probabilities directly too. That's what I love about Bayes! May 9 at 17:48

This is a class activity I've first read about in the book Teaching Statistics. A Bag of Tricks, 2nd ed. by Andrew Gelman and Deborah Nolan (they recommend 100 flips, though). Their reasoning to detect the fabricated sequence is based on the combination of the longest run and the number of runs. For the following plot, I simulated 5000 fair coin tosses of length 300 and plotted the longest run on the y-axis and the number of runs on the x-axis (I once asked a question about the explicit joint probability). Each dot represents the result of 300 fair flips. For better visibility, the points are jittered. The numbers for the two sequences are plotted in color. The conclusion is obvious.

For a quick calculation, recall that a rule of thumb for the longest run of either heads or tails in $$n$$ tosses is$$^{[1]}$$ $$l = \log_{1/p}(n(1-p)) + 1$$. For an approximate 95% prediction interval, just add and subtract $$3$$ from this value. Surprisingly, this number (i.e. $$\pm 3$$) does not depend on $$n$$! Applied to a fair coin with $$n=300, p=1/2$$, we have $$l=\log_2(300/2) + 1=8.22$$. So we expect the longest run to be round $$8$$ and reasonably in the range of $$8\pm 3$$, so between $$5$$ and $$11$$. The longest run in sequence 2 is $$8$$, whereas it is $$4$$ in sequence 1. As this is outside the approximate prediction interval, we'd conclude that sequence 1 is suspicious under the assumption of $$p=1/2$$.

$$[1]$$ Schilling MF (2012): The Surprising Predictability of Long Runs. Math. Mag. 85: 141-149. (link)

• Excellent answer and graph. "Surprisingly, this number does not depend on n!" You're talking about +-3, right? I was confused for a while since you define $l$ just before, which obvisouly depends on $n$. May 9 at 14:48
• @EricDuminil Yes, sorry. The value that does not depend on $n$ is the $\pm 3$ for the prediction interval. May 9 at 15:45

The runs test (NIST page) is a nonparametric test designed to identify unusual frequencies of runs. If we observe $$n_1$$ heads and $$n_2$$ tails, the expected value and variance of the number of runs are:

$$\mu = {2n_1n_2 \over n_1+n_2} + 1$$ $$\sigma^2 = {2n_1n_2(2n_1n_2 - n_1 - n_2) \over (n_1+n_2)^2(n_1+n_2+1)}$$

As a rule of thumb, for $$n_1, n_2 \geq 10$$ the distribution of the observed number of runs is reasonably well-approximated by a Normal distribution.

Edit: (incorporating Eric Duminil's work below)

For sequence 1, we have 148 heads, 152 tails, 205 runs, and for sequence 2, we have 148 heads, 152 tails and 154 runs. Plugging these numbers into our formulae above give us $$z$$-scores of 6.5 for the first sequence and 0.58 for the second sequence - extremely strong evidence that the first sequence is fake.

When people fake sequences like this, they tend to greatly underestimate the probability of longer runs, so they don't create as many long(ish) runs as they should. This in turn tends to increase the number of runs beyond that which would be expected. Consequently, when testing for faked data, we might prefer a one-sided test of the alternative hypothesis that there are "too many" runs vs. the null hypothesis that the number of runs is average - at least if we think the sequence was created by a human being.

• Yep, this is a classic application of gambler's fallacy. May 6 at 21:54
• There are 62 runs in the first two rows, AFAICT. For the whole sequence #1 : 148 heads, 152 tails, 205 runs. Vs 148 heads, 152 tails and 154 runs for sequence #2. May 9 at 14:56
• Thanks, @EricDuminil - I've included your efforts in the body of the answer with a citation. May 9 at 16:46
• That's good. But it leaves unanswered why you would formulate a question in terms of longest run and then solve it by looking at an indirect proxy (number of runs). Why not just use the longest run as the test statistic?
– whuber
May 9 at 18:13
• @whuber - it's not just the longest run that's informative, it's the number of longer runs in general, and I don't have a good idea of a cutoff for run length for testing. Having said that, using the longest run as a test statistic with 100,000 randomly generated strings with 148 heads and 152 tails for calculating an approximate p-value gives a p-value of 0.00004 for the first sequence. Maybe I'll expand on that over (my) lunch. May 9 at 18:28

Here's an empirical approach, based on compression as a proxy for algorithmic complexity:

import bz2
import random
import statistics

s1 = "TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT"
s2 = "HTHHHTHTTHHTTTTTTTTHHHTTTHHTTTTHHTTHHHTTHTHTTTTTTHTHTTTTHHHHTHTHTTHTTTHTTHTTTTHTHHTHHHHTTTTTHHHHTHHHTTTTHTHTTHHHHTHHHHHHHHTTHHTHHTHHHHHHHTTHTHTTTHHTTTTHTHHTTHTTHTHTHTTHHHHHTTHTTTHTHTHHTTTTHTTTTTHHTHTHHHHTTTTHTHHHTHHTHTHTHTHHHTHTTHHHTHHHHHHTHHHTHTTTHHHTTTHHTHTTHHTHHHTHTTHTTHTTTHHTHTHTTTTHTHTHTTHTHTHT"

def compressed_len(s):
return len(bz2.compress(s.encode()))

trials = []
for x in range(100000):
sr = "".join(random.choice("HT") for _ in range(300))
trials.append(compressed_len(sr))

mean = statistics.mean(trials)
stddev = statistics.stdev(trials)

print("Random trials:")
print("Mean:", mean)
print("Stddev:", stddev)

l1 = compressed_len(s1)
l2 = compressed_len(s2)

o1 = l1 - mean
o2 = l2 - mean

d1 = o1 / stddev
d2 = o2 / stddev

print("Selected trials:")
print("Seq", "Len", "Dev", sep="\t")
print("S1", l1, d1, sep="\t")
print("S2", l2, d2, sep="\t")

Roughly speaking:

1. Compress a bunch (100k) of random coinflips.
2. Observe the resulting length distribution. (In this case I'm approximating it as a normal distribution of lengths; a more thorough analysis would check and pick an appropriate distribution instead of blithely assuming normality.)
3. Compress the input sequences.
4. Compare with observed distribution.

Result (note: not exactly reproducible due to the use of random trials; if you want to be reproducible add a random seed):

Random trials:
Mean: 105.05893
Stddev: 2.6729774976956002
Selected trials:
Seq Len Dev
S1  88  -6.381995364609942
S2  109 1.4744119632124217

Based on this, I'd say that S1 is the non-random one here. 6.38 standard deviations below the mean is rather improbable.

The nice thing about this approach is that it's relatively generic, and takes advantage of the pre-existing work of a bunch of smart people.

Just be aware of its limitations and quirks:

1. You want a compression algorithm that's designed for space over compression speed. BZ2 works well enough here.
2. This doesn't work if the compression algorithm simply gives up and writes a raw block to the output.
3. A null result does not mean that the sequence is random. It means that this compression algorithm is unable to distinguish this input from random.

This is probably an overcomplicated way of looking at it, but for me it's fun, so I present to you...

## Moran's I

Now, Moran's I was developed to look at spatial autocorrelation (basically autocorrelation with multiple dimensions), but it can be applied to the 1-dimensional case as well. Some of my interpretations might be a little sketchy, but you can consider your coin flips this way.

To summarize Moran's I, it will consider your neighboring values using a pre-defined matrix. How you define the matrix is up to you, but it can actually be used to consider not just the directly neighboring values, but any values beyond that. Moran's I will produce a value ranging from -1 (perfectly dispersed values) to 1 (perfectly clustered values), with 0 being random.

I wrote up some quick R code. First, setup the data (OP's data and a couple generated data sets to test dispersion and clustering):

seq1 = unlist(strsplit("TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT",
split = ""))

seq2 = unlist(strsplit("HTHHHTHTTHHTTTTTTTTHHHTTTHHTTTTHHTTHHHTTHTHTTTTTTHTHTTTTHHHHTHTHTTHTTTHTTHTTTTHTHHTHHHHTTTTTHHHHTHHHTTTTHTHTTHHHHTHHHHHHHHTTHHTHHTHHHHHHHTTHTHTTTHHTTTTHTHHTTHTTHTHTHTTHHHHHTTHTTTHTHTHHTTTTHTTTTTHHTHTHHHHTTTTHTHHHTHHTHTHTHTHHHTHTTHHHTHHHHHHTHHHTHTTTHHHTTTHHTHTTHHTHHHTHTTHTTHTTTHHTHTHTTTTHTHTHTTHTHTHT",
split = ""))

# Alternate T and H. E.g., THTHTHTHT....
# 'perfectly dispersed'
# Moran's I = -1
seq3 = rep(c("T", "H"), times = 50)

# 50 of T followed by 50 of H
# 'perfectly clustered'
# Moran's I approaches 1 as the sample size increases to infinity
seq4 = rep(c("T", "H"), each = 50)

# weights must be a vector with an odd length and the middle value set to 0
# weights are relative and do not have to add to 1
moran <- function(x, weights) {
x = c(T = 0, H = 1)[x] # convert T/H to 0/1

N = length(x)

x_mean = mean(x)

den = sum((x - x_mean)^2)

W = 0
num = 0

offset = floor(length(weights)/2)
for (i in 1:length(x)) {

W = W + as.numeric(!is.na(x_slice)) %*% weights
num = num + (x[i] - x_mean) * sum((x_slice - x_mean) * weights, na.rm = TRUE)
}

return(unname((N * num)/(as.numeric(W) * den)))
}

Next, I test the generated data sets to illustrate/test that my function is working correctly:

# Test the 'perfect dispersion' scenario (should be -1)
moran(seq3, c(1, 0, 1))
## [1] -1

# Test the 'perfect clustering' scenario (should be ~1)
moran(seq4, c(1, 0, 1))
## [1] 0.979798

Now, let's look at OP's sequences:

# Simple look at seq1. The weights test the idea that the current flip
# is based purely on the last flip (a reasonable model for how a person might react)
moran(seq1, c(1, 0, 0))
## [1] -0.3647031
moran(seq2, c(1, 0, 0))
## [1] -0.02359453

I'm defining my weights matrix such that only the previous flip is considered when testing for autocorrelation. We see that the second sequence is very close to 0 (random), whereas the first sequence seems to lean somewhat toward overdispersion.

But maybe we think someone faking coin flips would consider the last two flips, not just the most recent:

# Maybe the person is looking back at the last two flips
moran(seq1, c(1, 1, 0, 0, 0))
## [1] -0.1726056
moran(seq2, c(1, 1, 0, 0, 0))
## [1] 0.0249505

The second sequence is just as close to 0 as before, but the first sequence had a pretty noticeable shift towards 0. This might be interpretable in a couple of different ways. First, if we know that the first sequence is fake, then maybe it means the person wasn't considering two flips back. A second interpretation is that maybe they were considering the last two flips, and somehow this led them to doing a better job at faking randomization. A third option might just be sheer dumb luck at faking the randomization.

Now, maybe the person considers the last two coin flips but gives the most recent flip more importance.

# Same idea, but maybe the more recent of the two is twice as important
moran(seq1, c(1, 2, 0, 0, 0))
## [1] -0.2367095
moran(seq2, c(1, 2, 0, 0, 0))
## [1] 0.008750762

Here, we see the two sequences react differently. The second sequence (already pretty close to 0), gets noticeably closer to 0, whereas the first sequence shifts noticeably away. I'm not sure I want to try and interpret this, but it's an interesting result, and a similar thing happens if we try to model a scenario where the person is not only considering their previous flips but also thinking ahead to their next flip:

# Maybe the person was thinking ahead to their next flip as well
moran(seq1, c(1, 2, 0, 1, 0))
## [1] -0.2687347
moran(seq2, c(1, 2, 0, 1, 0))
## [1] 0.0006576715

Some of my application/interpretation of Moran's I to the coin flip problem might be a little off, but it's definitely an applicable measure to use.

A related metric is Geary's C, which is more sensitive to local autocorrelation

When people try to generate random sequences, they tend to avoid repeating themselves more than random processes avoid repeating themselves. Thus, if we look at consecutive pairs of flips, we would expect a human-generated sequence to have too many HT and TH and too few HH and TT compared to a typical random sequence.

The code below explores this hypothesis. It splits each sequence of 300 flips into 150 consecutive pairs and plots the frequency of the four possible results (HH, HT, TH, TT).*

library(tidyverse)

a <- "TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT"
b <- "HTHHHTHTTHHTTTTTTTTHHHTTTHHTTTTHHTTHHHTTHTHTTTTTTHTHTTTTHHHHTHTHTTHTTTHTTHTTTTHTHHTHHHHTTTTTHHHHTHHHTTTTHTHTTHHHHTHHHHHHHHTTHHTHHTHHHHHHHTTHTHTTTHHTTTTHTHHTTHTTHTHTHTTHHHHHTTHTTTHTHTHHTTTTHTTTTTHHTHTHHHHTTTTHTHHHTHHTHTHTHTHHHTHTTHHHTHHHHHHTHHHTHTTTHHHTTTHHTHTTHHTHHHTHTTHTTHTTTHHTHTHTTTTHTHTHTTHTHTHT"

# split each sequence of 300 into 150 consecutive pairs
# e.g. TTHHTHTT... -> TT, HH, TH, TT, ...
n_pairs <- 150
ap <- tibble(pair = character(n))
bp <- tibble(pair = character(n))
for (i in 1:n) {
ap$$pair[i] <- substring(a, 2*i - 1, 2*i) bp$$pair[i] <- substring(b, 2*i - 1, 2*i)
}

# get the frequencies of each possible pair and plot
apc <- count(ap, pair)
bpc <- count(bp, pair)
bind_rows(
Sequence 1 = apc,
Sequence 2 = bpc,
.id = 'source') %>%
ggplot(aes(x = pair, y = n, group = source, fill = source)) +
geom_col() +
facet_grid(vars(source)) +
theme_minimal() +
geom_hline(yintercept = n_pairs/4, linetype = 'dashed') +
ylab('frequency') +
ggtitle('Frequency of consecutive coin flip pairs')

The dotted line at 150/4 = 37.5 is the expected count of each possible pair assuming the coin flips are independent and fair. By the Law of Large Numbers, we expect the bars not to stray too far from the dotted line. Sequence 1 has an above-average number of HT and TH pairs (especially HT), consistent with our hypothesis about human-generated "randomness". The pairs from Sequence 2 are more consistent with average behavior.

To see how unusual this behavior would be under independent, fair flips, we reformat each sequence's pair count data as a 2x2 contingency table (rows = first flip H/T, columns = second flip H/T) and use Fisher's exact test, which checks whether the data is consistent with a null hypothesis in which the first flip is independent of the second:

for (x in list(apc, bpc)) {
print(x)
x %>%
mutate(f1 = str_sub(pair, 1, 1),
f2 = str_sub(pair, 2, 2)) %>%
select(f1, f2, n) %>%
select(-f1) %>%
as.matrix() %>%
fisher.test() %>%
print()
}

The contingency table for Sequence 1 has a p value of 0.0002842, while the table for Sequence 2 has 0.5127. This means that pair frequencies skewed to the degree seen in Sequence 1 would only occur by random 1/0.0002842 = 1 out of 3,519 times, while something like Sequence 2 would be seen very commonly. It seems sensible to conclude that Sequence 1 is the human-made sequence, since its pair frequency table is not consistent with random chance but is quite consistent with the behavior we'd expect of humans.

There is a caveat to this analysis: we do not expect random sequences to be perfectly consistent with average behavior. In fact, in some contexts people know that long random sequences should follow the Law of Large Numbers, and they create sequences which follow it too perfectly. A different analysis would be needed to explore whether Sequence 2 looks odd from this opposite perspective.

* Other answers look at all 299 consecutive pairs, which gives you more data points but they become dependent, which prevents us from using standard significance tests. (For example in the sequence TTHHT, you can make pairs like this: (TT)HHT, T(TH)HT, TT(HH)T, .... This gives you more pairs but consecutive pairs are not independent of one another, as the second flip of a pair determines the first flip of the next pair.)

# Alternate Analysis

An analysis that uses all 299 pairs of consecutive flips could be more powerful than the one above if the dependency problem can be solved. To do this, @whuber suggests looking at the transitions between consecutive flips, i.e. when H is followed by T or vice versa. If the flips are independent and fair, then after the first flip, each transition can be considered an independent Bernoulli random variable, and there are 299 transitions total. We can use a two-sided test to see whether the number of transitions observed in each sequence is unlikely under fair independent flips. The transitions are counted and the test applied by the code below:

library(tidyverse)

a <- "TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT"
b <- "HTHHHTHTTHHTTTTTTTTHHHTTTHHTTTTHHTTHHHTTHTHTTTTTTHTHTTTTHHHHTHTHTTHTTTHTTHTTTTHTHHTHHHHTTTTTHHHHTHHHTTTTHTHTTHHHHTHHHHHHHHTTHHTHHTHHHHHHHTTHTHTTTHHTTTTHTHHTTHTTHTHTHTTHHHHHTTHTTTHTHTHHTTTTHTTTTTHHTHTHHHHTTTTHTHHHTHHTHTHTHTHHHTHTTHHHTHHHHHHTHHHTHTTTHHHTTTHHTHTTHHTHHHTHTTHTTHTTTHHTHTHTTTTHTHTHTTHTHTHT"

# 299 transitions from flip i to i+1 occur in the sequence of 300
# record these transitions in arrays at and bt
n <- 299
at <- logical(n)
bt <- logical(n)
for (i in 1:n) {
at[i] <- str_sub(a, i + 1, i + 1) != str_sub(a, i, i)
bt[i] <- str_sub(b, i + 1, i + 1) != str_sub(b, i, i)
}

# two-sided exact binomial test (analogous to z-test)
# gives probability of transition count more extreme than the one observed
pbinom(sum(at) - 1, n, 1/2, lower.tail = F) + pbinom(n - sum(at), n, 1/2)
pbinom(sum(bt) - 1, n, 1/2, lower.tail = F) + pbinom(n - sum(bt), n, 1/2)

Running this code, we find that Sequence 1 has 204 transitions out of a possible 299. The probability of observing a number of transitions at least this imbalanced, on either the left or the right side, is equal to the probability of observing at least 204 transitions, plus the probability of observing at most 299 - 204 = 95 transitions. This probability is 2.696248e-10, on the order of 3 in 10 billion. Sequence 2 has 153 transitions, and the probability of observing at least 153 transitions or at most 299 - 153 = 146 transitions is 0.7286639. The number of transitions in Sequence 1 is extremely improbable, more so than the 150 pair test above suggested.

• Your code seems to be doing something more complicated than you describe. It is hard to relate your plots, which are unexplained, to the data: those plots clearly do not report on sequences of length 300.
– whuber
May 7 at 14:43
• I'm splitting each length 300 sequence into 150 pairs of adjacent coin tosses and counting how many of the 150 pairs are HH, HT, TH, or TT. That's what the graphs show. I've added some comments to elaborate on this. If that's not enough, could you be more specific as to what you're struggling to understand?
– Paul
May 7 at 16:55
• I am not struggling to understand anything--I just don't care to have to read through code in order to determine what the content of an answer might be, and I believe most readers will feel the same.
– whuber
May 7 at 17:25
• Thank you: what you are doing is now more apparent. But why? Could you explain what is accomplished with this split?
– whuber
May 7 at 18:10
• I've added a transition-based analysis and it is indeed a more powerful test. Great idea.
– Paul
May 10 at 0:45

This answer is inspired by @user1717828's answer which transforms the sequence of coin flips into a random walk. I don't show the two given sequences as random walks here; see @user1717828's answer for that plot.

The random walk approach is interesting because it examines long-run features of the sequence rather than short-range ones (such as the overabundance of H-T and T-H flips). In a way the two approaches are complementary as they analyze the sequences at different scales. At the "local" scale the fake sequence oscillates which creates autocorrelation; at the "global" scale it keeps within a limited range of positions for long stretches of time. Both of these aspects are non-random and as one occurs, so does the other: staying in place means not moving around and vice versa.

A (one-dimensional) simple random walk $$S_n$$, defined as the sum of n random variables that take value {-1, 1} with probability ½, has many interesting properties, including:

• The probability that a simple random walk returns to the origin is 1. In fact, it visits every integer infinitely often.
• The mean of a simple random walk is $$\text{E}(S_n) = 0$$ and its variance is $$\text{Var}(S_n) = n$$.
• A random walk with nonzero mean (which corresponds to flipping a biased coin) is transient: it makes finitely many visits to the origin before diverging, to +∞ if the mean is positive and to -∞ in the mean is negative.

These properties imply that a simple random walk spends a lot of time far away from 0 before eventually returning to it.

We can use this intuition founded on theory (as well as any other property of random walks) to propose characteristics for comparing the two sequences. Then we generate many simple random walks and use the observed distribution of the features to estimate how "extreme" the two given sequences are.

To capture the variance of a simple random walk I use the maximum distance from 0. To capture its tendency to visit every integer I use the number of times the walk crosses the integers from -8 to +8. To make a symmetric grid of scatterplots and to avoid suspicions of HARKing I don't show the crossings of 0. It's important to look at crossing on both sides of 0 (the starting point), so that we don't make assumptions about how the non-randomness manifests in the data. For example, we don't know whether the coin is fair (p = ½), biased towards heads (p > ½) or biased towards tails (p < ½).

Note: HARKing is the practice of performing many analyses to find an interesting hypothesis. I compute many statistics, grounded in the theory of random walks, and report all of them, thus avoiding HARKing.

Here are the results from the simulation of 500 simple random walks. Sequence #1 (in blue) appears as an outlier compared to sequence #2 (in red) in many panels. Due to its overabundance of H-T and T-H pairs, sequence #1 doesn't explore enough and spends too much time between -5 and -8 (shown in the top row). Sequence #1 doesn't advance beyond the band [-9, 9] and it is rare for a true random walk so stay so close to 0 for 300 steps. Visually, sequence #1 is the overall outlier even if it doesn't appear extremely "unusual" is some panels.

• +1. This explains why I did not examine this statistic in my answer: I had done so, exactly as you have, and came to the same conclusion that the max deviation statistic was suggestive but only weak evidence of a departure from randomness. But in that investigation I noted there were other visual quirks in the plot of the first series and that led to a more effective statistic for assessing the randomness of the series. It's worth noting that extensive computation is unnecessary: you could draw all your conclusions more quickly by simulating as few as 20 random walks..
– whuber
May 9 at 13:44
• @whuber After some (over)thinking, I realized that it works quite well to compute two statistics: one to represent central tendency and the other -- extreme values. Similar to COOLSerdash's solution which looks at number of runs (a kind of average) and length of longest run. With the simple random walk, zero crossings vs maximum distance from zero works very well (visually). But then I don't know how to get a p-value. May 9 at 21:35
• I think the zero crossings observation comes down to HARKing. (For instance, these could well be realizations of Markov processes in which a transition away from a side of the coin is made with probability $p$; $p\approx 2/3$ in one sequence and $p\approx 1/2$ in the other. In general I wouldn't expect zero-crossing counts to distinguish among these.) A better way to approach this would be to do all your exploration on the first half of each sequence; develop a suitable statistic from that; and apply it to the second halves of the sequences.
– whuber
May 9 at 21:39
• @whuber I don't agree it's HARKing. At least not any more than number of runs. A simple random walk returns to the start infinitely often, so given this fact it makes sense to look at zero crossings. Since the fake sequence doesn't move enough any number will do in theory; here because sequences are only 300 long every number between about -10 to 10 will work. May 9 at 22:03
• Ask yourself this: would you have focused on counting zero crossings by exploring just the first halves of the sequences? You might have if both of two things occurred: (1) you observed no zero crossings in one and many zero crossings in the other and (2) in simulated iid sequences, you observed a consistent tendency to have positive numbers of zero crossings. In such a case, you likely have a useful statistic. Unfortunately, you would discover that about 6.5% of the time, a Binomial random walk has no zero crossings. That's why I view this as HARKing: it's accidental.
– whuber
May 10 at 14:32

In addition to statistical approaches, one visual approach is to plot the sequences as a "drunkards walk". Treat H as a step forwards and T as a step back and plot the sequences. One way in Python is:

import altair as alt
import pandas as pd

seq1 = "HTHHHTHTTHHTTTTTTTTHHHTTTHHTTTTHHTTHHHTTHTHTTTTTTHTHTTTTHHHHTHTHTTHTTTHTTHTTTTHTHHTHHHHTTTTTHHHHTHHHTTTTHTHTTHHHHTHHHHHHHHTTHHTHHTHHHHHHHTTHTHTTTHHTTTTHTHHTTHTTHTHTHTTHHHHHTTHTTTHTHTHHTTTTHTTTTTHHTHTHHHHTTTTHTHHHTHHTHTHTHTHHHTHTTHHHTHHHHHHTHHHTHTTTHHHTTTHHTHTTHHTHHHTHTTHTTHTTTHHTHTHTTTTHTHTHTTHTHTHT"

seq2 = "TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT"

def encode(stream):
return [1 if c == "H" else -1 for c in stream]

df = pd.DataFrame({"seq1": encode(seq1), "seq2": encode(seq2)})
df_cumsum = pd.melt(
df.cumsum(), var_name="sequence", value_name="cumsum", ignore_index=False
).reset_index()

chart = alt.Chart(df_cumsum).mark_line().encode(x="index", y="cumsum", color="sequence")
chart.show()

When comparing a truly random sequence to a human generated one, it is often easy to tell them apart based on inspection of the walk.

• This is a good approach. It was the first thing I tried. However, I created a context for evaluating these random walks: I also generated 18 more sequences using iid flips of a fair coin and plotted all 20. Generally, whenever I repeated this procedure, there was a truly random walk that looked like the orange one here, at least in terms of the relatively small range. Exactly which characteristic(s) suggest to you that the orange curve is not random but the blue is?
– whuber
May 8 at 12:53
• The orange one is what I think is the truly random one based on this visual. I would have guessed based on just the number of zero-crossings alone that blue is human-generated. But you're right, there's always going to be a chance that any sequence is randomly-generated, and the analytical methods are better for quantifying the odds of that. May 8 at 20:17
• Unfortunately for your theory, the orange one is the non-random one. It has an unusually low range; but more to the point, its thickened appearance is due to a huge overabundance of H-T-H oscillations.
– whuber
May 8 at 20:32
• Ahh! In that case, I'm going to rethink my approach of visualizing these things before looking at the statistics next time! May 8 at 22:29
• The thickened appearance is the feature analyzed in the answers by whuber, Dmetri Pananos and Paul. The distance from 0 in a simple random walk (which I think is what this answer is getting at) would be a different feature altogether. A (true) simple random walk will spend a lot of time away from zero until eventually returning (with probability 1). The sequences in this exercise though seem too short for this kind of argument. May 8 at 23:55

Looking at the HH, HT, TH, TT frequencies is probably the most straightforward way to approach the two series presented, given people's tendency to apply HT and TH more frequently when trying to appear random. More generally, however, that approach will fail to detect non-randomness even in sequences with obvious patterns. For instance, a repeating sequence of HHHTHTTT will produce balanced counts of pairs as well as triples (HHH, HHT, etc.).

Here's an idea for a more general solution that was initially inspired by answers discussing random walks. Start with the observation that for a random sequence, the number of heads in any given subsequence is binomially distributed. We can count the number of heads, $$n_{i,j}$$, between flip number $$i$$ and flip number $$j>i$$ for all values of $$i,j$$. Then compare these to the expected number of counts if all the $$n_{i,j}$$ were independent. Although they are obviously not independent, the comparison gives rise to a useful statistic: the maximum absolute difference between the observed counts and the expected counts.

Applying this approach to sequence 1 gives us 98.26 as our test statistic: there are 257 subsequences of length 44. If they were all composed of independent Bernoulli trials, the expected number of the 257 that contained exactly 22 heads is ~30.74, whereas sequence 1 contains 129 subsequences with exactly 22 heads (very underdispersed). 129 - 30.74 = 98.26, which is the maximum of these differences for sequence 1.

Performing the same calculations on sequence 2 gives a test statistic of 48.30: there are 197 subsequences of length 104. The expected number containing exactly 54 heads would be ~13.70. Sequence 2 contains 62, so the test statistic is 62 - 13.70 = 48.30.

The test statistics can be compared to those from a large number of random sequences of the same size. In this case, no samples are greater than the test statistic from sequence 1, and about 14% of samples are greater than the statistic from sequence 2.

Here it is all together with R code:

library(parallel)
set.seed(16)

seq1 <- utf8ToInt("TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT") == utf8ToInt("H")
seq2 <- utf8ToInt("HTHHHTHTTHHTTTTTTTTHHHTTTHHTTTTHHTTHHHTTHTHTTTTTTHTHTTTTHHHHTHTHTTHTTTHTTHTTTTHTHHTHHHHTTTTTHHHHTHHHTTTTHTHTTHHHHTHHHHHHHHTTHHTHHTHHHHHHHTTHTHTTTHHTTTTHTHHTTHTTHTHTHTTHHHHHTTHTTTHTHTHHTTTTHTTTTTHHTHTHHHHTTTTHTHHHTHHTHTHTHTHHHTHTTHHHTHHHHHHTHHHTHTTTHHHTTTHHTHTTHHTHHHTHTTHTTHTTTHHTHTHTTTTHTHTHTTHTHTHT") == utf8ToInt("H")

# function to calculate test statistic S
fS <- function(m, p = 0.5) {
if (!is.matrix(m)) m <- matrix(m, ncol = 1)
n <- nrow(m)
steps <- rep.int(1:(n - 1L), 2:n)
nEx <- dbinom(sequence(2:n, from = 0L), steps, p)*(n - steps)
idx <- sequence((n - 1L):1)
idx <- idx*(idx + 1L)/2L
S <- numeric(ncol(m))
for (i in 1:ncol(m)) S[i] <- max(abs(nEx - tabulate(idx + dist(cumsum(m[,i])), length(nEx))))
S
}

(S1 <- fS(seq1))
#> [1] 98.26173
(S2 <- fS(seq2))
#> [1] 48.30014
# calculate S from 1e6 random sequences of length n (probably overkill)
n <- length(seq1)
cl <- makeCluster(detectCores() - 1L)
clusterExport(cl, list("fS", "n"))
system.time(simS <- unlist(parLapply(cl, 1:100, function(i) fS(matrix(sample(0:1, 1e4L*n, TRUE), n)))))
#>    user  system elapsed
#>    0.00    0.03  339.42
stopCluster(cl)
nsim <- 1e6L
# calculate approximate p-values
sum(simS > S1)/nsim
#> [1] 0
sum(simS > S2)/nsim
#> [1] 0.139855
max(simS)
#> [1] 91.01078

This problem specifies that we have the following information:

• We observed two coin flip sequences: sequence $$S_1$$ and sequence $$S_2$$.
• Each of these sequences could have been generated by either mechanism $$R$$ corresponding to independent flips of a fair coin or some other mechanism $$\bar{R}$$.
• Exactly one of these two sequences was generated by the mechanism $$R$$.

Since both sequences $$S_1$$ and $$S_2$$ could possibly have been generated by $$R$$ or $$\bar{R}$$, we cannot be sure which of these two sequences was generated by $$R$$. However, probability theory provides us with the tools to quantify our belief $$P(R_i|S_i)$$ that sequence $$S_i$$ was generated by mechanism $$R$$. Then, given that we have been provided with the information $$I$$ that exactly one of these two sequences has been generated by mechanism $$R$$, then the probability that sequence 1 was generated by mechanism $$\bar{R}$$ and sequence 2 was generated by mechanism $$R$$ is:

$$P(\bar{R_1}R_2|S_1 S_2 I) = \frac{P(R_2|S_2)}{P(R_1|S_1) + P(R_2|S_2)}$$

This problem explicitly specifies what it means for a sequence to have been generated by mechanism $$R$$: the sequence $$S_i$$ is described by independent flips $$y_{ij}$$ of a fair coin, such that the likelihood is:

$$P(S_i|R_i) = \prod_j \mathrm{Bernoulli}(y_{ij} | 0.5)$$

However, in order to determine the probability that a given sequence was generated by mechanism $$R$$, $$P(R_i|S_i)$$, we also need to specify alternative mechanisms $$\bar{R}$$ by which the sequences may have been generated. At this point we have to use our own creativity and external information to develop models that could reasonably describe how these sequences might have been generated, if they were not generated by mechanism $$R$$.

The other answers to this question do a great job at describing various mechanisms $$\bar{R}$$ that describe how sequences could be generated:

• Sequences are generated by sampling pairs of coin flips from a distribution where the probability of different pairs of coin flips deviates from uniform: 1 2 3
• Sequences are generated in a way such that they do not have long runs of only heads or tails: 1
• Sequences are generated in such a way so that a given compression algorithm results in a small compressed size: 1
• Sequences are generated by a random walk such that a given coin flip outcome is affected by recent coin flip outcomes: 1 2 3

Note: we can always develop a hypothesis that appears to result in our observed sequence having been "inevitable", according to our likelihood, and if our prior probability for that mechanism of sequence generation is high enough, then using this hypothesis can always result in a low probability that the sequence had been generated by mechanism $$R$$. This practice is also referred to as HARKing, and Jaynes refers to this as a "sure-thing hypothesis". In general, we may have multiple hypotheses that can describe how a sequence may be generated, but the more contrived hypotheses such as "sure-thing hypotheses" should generally have sufficiently low prior probabilities such that we generally would not infer that those hypotheses have the highest probability of having generated a given sequence.

Here's an example using this procedure with one possible model for $$\bar{R}$$ sequence generation to quantify the probability that sequence 2 was the sequence that was generated by mechanism $$R$$. This stan model defines model_r_lpmf as the log likelihood for sequence generation mechanism $$R$$, and model_not_r_lpmf as the log likelihood for sequence generation mechanism $$\bar{R}$$. In this case, we are testing a hypothesis $$\bar{R}$$ in which coin flips can be correlated, and there is a fairly well defined correlation length. Then, we are modeling the probability that the sequence was generated by model $$R$$ as p_model_r, such that our prior probability considers mechanisms $$R$$ and $$\bar{R}$$ sequence generation equally probable.

// model for hypothesis testing whether a sequence of coin flips is generated by either:
// - model R: independent flips of a fair coin, or
// - model not R: unfair coin with correlations

functions {
// independent flips of a fair coin
real model_r_lpmf(int[] sequence, int N) {
return bernoulli_lpmf(sequence | 0.5);
}

// unfair coin with correlations
real model_not_r_lpmf(int[] sequence, int N, int n, vector beta) {
real tmp = 0;

real offset = 0;
for (i in 1:N) {
offset = beta[1];
for (j in 1:min(n - 1, i - 1)) {
offset += beta[j + 1] * (2 * sequence[i - j] - 1);
}
tmp += bernoulli_logit_lpmf(sequence[i] | offset);
}
return tmp;
}
}
data {
int N;                                 // the length of the observed sequence
int<lower=0, upper=1> sequence[N];     // the observed sequence
}
transformed data {
int n = N / 2 + 1;                     // number of correlation parameters to model
}
parameters {
real<lower=0, upper=1> p_model_r;       // probability that the sequence was generated by model R
// not R model parameters
real log_scale;                        // log of the scale of the correlation coefficients beta
real log_decay_rate;                   // log of the decay rate for the correlation coefficients beta
vector[n] alpha;                       // pretransformed correlation coefficients
}
transformed parameters {
// transformed not R model parameters
real scale = exp(log_scale);           // typical scale of the correlation coefficients
real decay_rate = exp(log_decay_rate); // typical decay rate of correlation coefficients
vector[n] beta;                        // correlation coefficients
for (j in 1:n) {
beta[j] = alpha[j] * scale * exp(-(j - 1) * decay_rate);
}
}
model {
// uninformative haldane prior for the probability that the sequence was generated by model R
target += - log(p_model_r) - log1m(p_model_r);

// priors for not R model parameters
log_scale ~ normal(-1, 1);
log_decay_rate ~ normal(-1, log(n + 1) / 2.0);
alpha ~ normal(0, 1);

// the sequence is was generated by model R with probability p_model_r, otherwise it was generated by model not R
target += log_mix(
p_model_r,
model_r_lpmf(sequence | N),
model_not_r_lpmf(sequence | N, n, beta)
);
}

Using pystan with this model, we can evaluate the probability p_model_r that each of the two provided sequences were generated by mechanism $$R$$.

model = pystan.StanModel(model_code=MODEL_CODE)

sequence_1 = "TTHHTHTTHTTTHTTTHTTTHTTHTHHTHHTHTHHTTTHHTHTHTTHTHHTTHTHHTHTTTHHTTHHTTHHHTHHTHTTHTHTTHHTHHHTTHTHTTTHHTTHTHTHTHTHTTHTHTHHHTTHTHTHHTHHHTHTHTTHTTHHTHTHTHTTHHTTHTHTTHHHTHTHTHTTHTTHHTTHTHHTHHHTTHHTHTTHTHTHTHTHTHTHHHTHTHTHTHHTHHTHTHTTHTTTHHTHTTTHTHHTHHHHTTTHHTHTHTHTHHHTTHHTHTTTHTHHTHTHTHHTHTTHTTHTHHTHTHTTT"
samples_1 = model.sampling(
data=dict(N=len(sequence_1), sequence=[int(c == "H") for c in sequence_1]),
chains=16,
)
p_model_r_1 = samples_1["p_model_r"].mean()
# p_model_r_1 = 0.027

sequence_2 = "HTHHHTHTTHHTTTTTTTTHHHTTTHHTTTTHHTTHHHTTHTHTTTTTTHTHTTTTHHHHTHTHTTHTTTHTTHTTTTHTHHTHHHHTTTTTHHHHTHHHTTTTHTHTTHHHHTHHHHHHHHTTHHTHHTHHHHHHHTTHTHTTTHHTTTTHTHHTTHTTHTHTHTTHHHHHTTHTTTHTHTHHTTTTHTTTTTHHTHTHHHHTTTTHTHHHTHHTHTHTHTHHHTHTTHHHTHHHHHHTHHHTHTTTHHHTTTHHTHTTHHTHHHTHTTHTTHTTTHHTHTHTTTTHTHTHTTHTHTHT"
samples_2 = model.sampling(
data=dict(N=len(sequence_2), sequence=[int(c == "H") for c in sequence_2]),
chains=16,
)
p_model_r_2 = samples_2["p_model_r"].mean()
# p_model_r_2 = 0.790
`

Here we have quantified the probability that each sequence was generated according to the sequence generating model $$R$$ as the following:

\begin{aligned} P(R_1|S_1) &= 0.027 \\ P(R_2|S_2) &= 0.790 \\ P(\bar{R_1}R_2|S_1 S_2 I) &= 0.967 \\ \end{aligned}

So, we are fairly certain that sequence 2 was generated by independent coin flips and sequence 1 was not, relative to the model of sequence generation $$\bar{R}$$ that we defined, and we quantify this belief with a probability of 0.967. This is a high probability, but of course we could still be wrong -- both sequences could have been generated by either model. Additionally, we could have selected a model of sequence generation for $$\bar{R}$$ that can not describe the procedure that was actually used to generate the sequences. This process for quantifying our belief that one of these two sequences was generated by a fair coin with independent flips is the piece of reasoning that currently appears to be missing from the other answers that are currently available.

• I have to reject your initial dichotomy, because it (a) is too vague to be actionable (what can one do, in any objective and quantifiable way, to characterize any "non-random process"?) and (b) ignores a huge set of other possible mechanisms: namely, random mechanisms that are not iid.. In fact, it is almost surely the case that whoever generated these sequences used a random mechanism for both--they are just different random mechanisms.
– whuber
May 31 at 12:00
• @whuber I framed this problem in terms of hypothesis testing between a model $R$ by which sequences are generated by independent flips of a fair coin, and models of other possible ways that we might believe that sequences could be generated $\bar{R}$. I used labels "random", and "not random" to describe these models, which appears to have been confusing, so I have edited the post to remove that terminology. Jun 8 at 3:18


Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?

We have the first N natural numbers. Our task is to get one permutation of them where the absolute difference between every two consecutive elements is > 1. If no such permutation is present, return -1.

The approach is simple. We will use the greedy approach. We will arrange all odd numbers in increasing or decreasing order, then arrange all even numbers in decreasing or increasing order

Algorithm

arrangeN(n)

Begin
if N is 1, then return 1
if N is 2 or 3, then return -1 as no such permutation is not present
even_max and odd_max is set as max even and odd number less or equal to n
arrange all odd numbers in descending order
arrange all even numbers in descending order
End

Example

#include <iostream>
using namespace std;
void arrangeN(int N) {
if (N == 1) { //if N is 1, only that will be placed
cout << "1";
return;
}
if (N == 2 || N == 3) { //for N = 2 and 3, no such permutation is available
cout << "-1";
return;
}
int even_max = -1, odd_max = -1;
//find max even and odd which are less than or equal to N
if (N % 2 == 0) {
even_max = N;
odd_max = N - 1;
} else {
odd_max = N;
even_max = N - 1;
}
while (odd_max >= 1) { //print all odd numbers in decreasing order
cout << odd_max << " ";
odd_max -= 2;
}
while (even_max >= 2) { //print all even numbers in decreasing order
cout << even_max << " ";
even_max -= 2;
}
}
int main() {
int N = 8;
arrangeN(N);
}

Output

7 5 3 1 8 6 4 2
Published on 01-Aug-2019 07:36:08


1. ## Solving Congruences

If you are to solve for x in the congruence $7x=20(mod50)$
What is the easier way to do it without using a calculator or any other type of electronic device. I know you can add 7 to 20 until the number is divisible by 7 and then divide by 7 to reduce it to a solution but that takes a while. I need to figure out how to get it done in just a minute or two. Thanks.

2. Originally Posted by diddledabble
If you are to solve for x in the congruence $7x=20(mod50)$
What is the easier way to do it without using a calculator or any other type of electronic device. I know you can add 7 to 20 until the number is divisible by 7 and then divide by 7 to reduce it to a solution but that takes a while. I need to figure out how to get it done in just a minute or two. Thanks.
You mean 50 (the modulus).

For small moduli and $(a,m) = 1$, adding the modulus until cancellation is probably the best way to solve $ax \equiv b \ (\text{mod } m)$. For this particular congruence, add the modulus once to 20 to get: $7x \equiv 70 \ (\text{mod } 50)$

Since $(7, 50) = 1$, you can 'divide' by 7 to get your solution.

_____________

For a more general case where $(a,m) \neq 1$, as long as $(a,m) \mid b$, then we can always use the Euclidean algorithm to find one solution modulo $\tfrac{m}{(a,m)}$ and use it to find all solutions modulo m. Here's an example in this wiki entry: Linear congruence theorem

3. ## @dibbledabble

Well, I guess I have an easier method to solve this congruence equation.

7x=20(mod50)

The equation simply means that 7x, a multiple of 7, is 20 more than a multiple of 50 [By modulo logic]

If we list down numbers which are 20 more than a multiple of 50, we get
70, 120, 170, 220, 270, 320, 370, 420, and so on... From this which it is quite evident that 70, 420, 770, 1120... etc are multiples of 7 which are 20 more than multiples of 50.

So, the smallest positive number is 70. 7x = 70 which means x = 10.
In fact, 350n - 280 would be the general form for the number 7x.
So, the general solution of x would be 50n - 40 where n can range from 1,2,3,.. and so on.

Hope it helped,
MAX

4. ## General theory

An equation of the form
$ax\equiv b$ (mod n)
has a solution if and only if $gcd(a,n)|b$.

Here is how you do it in general when these requirements are met.

Let $d=gcd(a,n)$ and by assumption $d|b \Rightarrow b=dk$ for some integer $k$. Then by the Euclidean Algorith, there exist integers $s$ and $t$ such that
$as+nt=d$.

We now multiply through by k to get.

$ask+ntk=dk=b$

reduce mod n to see
$a(sk)\equiv b$ (mod n).

There is your $x$, namely, $x=sk$ (mod n).

5. 7x7=49=-1 (mod 50)

so -x=140 (mod 50)=-10 (mod 50)

hence x=...

6. Helo, diddledabble!

Solve: . $7x \:\equiv\:20\text{ (mod 50)}$
Your "brute force" method is tedious, but it is commendable.
It shows that you understand the congruence statement.
Here is a primitive method . . .

We have: . $7x \:\equiv\:20 \text{ (mod 50)}$

This means: . $7x - 20 \:=\:50a\;\text{ for some integer }a.$

Solve for $x\!:\quad x \:=\:\frac{50a+20}{7} \quad\Rightarrow\quad x \:=\:7a + 2 + \frac{a+6}{7}$

Since $x$ is an integer, $a+6$ must be divisible by 7.
The first time this happens is $a = 1.$

So we have: . $x \;=\;7(1) + 2 + \frac{1+6}{7}\;=\;10$

Therefore: . $x \:\equiv\:10\text{ (mod 50)}$

7. ## @dibbledabble

Hi,

I think the solution offered by Soroban is the closest to the perfect procedure for your question. I guess mine was a bit too illustrative.

8. So using Soroban's method and a different congruence 35x=20(mod50) I get x=2(mod50) right?

9. ## Yes

If you haven't checked already, values of 'x' such as 52 and 102 satisfy the equation as, in 52 * 35 = 1820
= 1800 + 20
= 36*50 + 20
Similarly, 102*35 = 3570
= 3550 +20
= 71*50 + 50

MAX

10. Given the congruence $ax \equiv b \ (\text{mod } m)$, if $(a,m) \mid b$, then there are $(a,m)$ solutions exactly.

So for the congruence $35x \equiv 20 \ (\text{mod } 50)$, we can see that there should be 5 solutions modulo 50.

___________

Reduce your congruence: $5 \cdot 7x \equiv 5 \cdot 4 \equiv (\text{mod } 50) \ \Rightarrow \ 7x \equiv 4 (\text{mod } \tfrac{50}{(5, 50)}) \ \Leftrightarrow \ 7x \equiv 4 \ (\text{ mod} 10)$

Quickly by inspection, by adding the modulus to 4, we get: $7x \equiv 14 \ (\text{mod } 10)$

which is satisfied by all integers such that $x \equiv 2 \ (\text{mod } 10) \ \ (\star)$.

Returning to our original congruence, all 5 solutions are least residues that satisfy $(\star)$. Therefore, modulo 50: 2, 12, 22, 32, 42 are all solutions.


# Expected number of people sitting in the right seats.

There was a popular interview question from a while back: there are $n$ people getting seated an airplane, and the first person comes in and sits at a random seat. Everyone else who comes in either sits in his seat, or if his seat has been taken, sits in a random unoccupied seat. What is the probability that the last person sits in his correct seat?

The answer to this question is $1/2$ because everyone looking to sit on a random seat has an equal probability of sitting in the first person's seat as the last person's.

My question is: what is the expected number of people sitting in their correct seat?

My take: this would be $\sum_{i=1}^n p_i$ where $p_i$ is the probability that person $i$ sits in the right seat..

$X_1 = 1/n$

$X_2 = 1 - 1/n$

$X_3 = 1 - (1/n + 1/n(n-1))$

$X_4 = 1 - (1/n + 2/n(n-1) + 1/n(n-1)(n-2))$

Is this correct? And does it generalize to $X_i$ having an $\max(0, i-1)$ term of $1/n(n-1)$, a $\max(0, i-2)$ term of $1/n(n-1)(n-2)$ etc?

Thanks.

-
Could the general probability not be, for $m>1$, $X_m=1-\left(\sum_{k=0}^{m-2}\binom{m-2}{k}\frac{(n-k-1)!}{n!}\right)$? Perhaps prove by induction? –  Alyosha Aug 10 '13 at 23:19
The MSE treatment of the original problem: math.stackexchange.com/questions/5595/taking-seats-on-a-plane –  Byron Schmuland Aug 23 '13 at 17:41

Sorry for the second answer (I will delete the first one):

I will aim to show that the expected number of people sitting in their correct seats is given by:

$$n-1-\frac12-\frac13-\dots-\frac1{n-1}=n-H_{n-1}$$

To do this, we will first find the expected number of people in the wrong seats, which we shall call $s_n$.

Suppose passenger number $1$ sits in seat $i\ne1$. At this point, passengers $2,\dots,i-1$ all sit in their correct seats. We now have a situation where there are $n-i+1$ empty seats left, and passenger $i$ is going to sit in a random seat.

This is very similar to the situation we had at the start, just with fewer seats. The only difference is that passenger $i$'s seat is taken, and there's a seat (seat $1$) which belongs to none of the people standing up.

This is a bit of a problem, so we'll change things around a bit. We'll pretend that seat number $1$ doesn't, in fact, belong to any of the passengers. So wherever passenger $1$ sits, they're in the wrong seat. We'll use the letter $t_n$ to denote the expected number of people sitting in the wrong seat if seat $1$ doesn't belong to anybody.

What we now get, is that the situation after passenger $1$ has sat in seat $i\ne1$, and passengers $2,\dots,n-1$ have sat in their correct seats is exactly the same as the situation at the beginning, but with $n-i+1$ seats rather than $n$: there's a passenger about to choose a random seat which doesn't belong to him (I decided the sex of passenger $i$ by tossing a coin): there's a seat ($1$) which doesn't belong to anybody, and the rest of the seats belong to the remaining passengers. So at this point, the expected number of passengers sitting in the wrong seats is $1+t_{n-i+1}$: $1$ for passenger $1$, who's sat in the wrong seat, and $t_{n-i+1}$ because afterwards we're in exactly the same situation as before, but with $n-i+1$ seats.

What if passenger $1$ sits in seat $1$? Then all the remaining passengers sit in the right seats, so the expected number of people sitting in the wrong seats at the end is just $1$ (remember, passenger $1$ no longer owns seat $1$).

Passenger $1$ chooses between the $n$ seats at random, so this gives us the recurrence:

\begin{align} t_1 &= 1\\ t_n &= 1+\frac1n\sum_{i=2}^nt_{n-i+1} = 1+\frac1n\sum_{i=1}^{n-1}t_i \end{align}

We are now ready to prove the following:

Claim: $t_n=H_n$

Proof of claim: induction on $n$. $\mathbf{n=1}$ : $t_1=1=H_1$.

$\mathbf{n>1}$ : $t_n=1+\frac1n\sum_{i=1}^{n-1}t_i=1+\frac1n\sum_{i=1}^{n-1}H_i$ (by the inductive hypothesis). A well known identity involving harmonic numbers tells us that:

$$\sum_{i=1}^{n-1}H_i = nH_{n-1}-(n-1)$$

So $t_n=1+H_{n-1}-1+\frac1n=H_n$. $\Box$

How do we get from here to $s_n$? The difference now is that passenger $1$ does own seat $1$, which means that the answer will be smaller by $1$ if and only if passenger $1$ sits in seat $1$. Since passenger $1$ sits in seat $1$ with probability $\frac1n$, we need to subtract $\frac1n$ from $t_n$ to get $s_n$:

$$s_n=t_n-\frac1n=H_n-\frac1n=H_{n-1}$$

Finally, to get the expected number of people in the right seats, we subtract $s_n$ from $n$ to get:

$$n-H_{n-1}$$

Note 1: Since $H_n$ grows logarithmically, the proportion of people sitting in their correct seats converges to $1$ as $n\to\infty$.

Note 2: I still find this proof rather unsatisfying, since it uses the identity $\sum_{i=1}^{n-1}H_i = nH_{n-1}-(n-1)$, which I still don't really understand. I'm sure it's easy enough to prove by induction, but if someone could come up with a really nice explanation of why that works, it might yield an even slicker proof of this fact.

-
There are a few things I do not follow in your explanation. Firstly, if passenger 1 sits in seat 1, the expected number of correct seats is n, because everyone sits in their correct seat if it is available, so excluding that possibility materially affects the answer. Secondly, it can be shown (see below where I answered the wrong question) that regardless of of the number of people, the probability that the last person sits in his or her proper seat is \frac{1}{2}. As any one person could be the last one, the long-term proportion should approach 1/2. In my attempt at a correct answer it does. –  Avraham Aug 23 '13 at 19:19
What do you mean by 'any one person could be the last one'? And why does that mean that the long term proportion should approach $1/2$? –  Donkey_2009 Aug 24 '13 at 10:54
If you read my answer carefully, you will see that I do not exclude the possibility that passenger $1$ sits in seat $1$; instead, I treat it separately. The long term proportion approaches $1$, so I think you might be answering a different question. –  Donkey_2009 Aug 24 '13 at 10:55

Your overall approach looks technically correct, you are using linearity of expectation for counting and then writing down a formula for each $p_i$ (although it seems you switched notation and started using $X_i$ in your formulas instead of $p_i$). However I believe the equation for $X_4$ i.e. $p_4$ is actually

$$X_4 = 1 - (1/n + 1/n(n-1) + 1/n(n-2) + 1/n(n-1)(n-2))$$

and the generalization for $X_i$ for arbitrary $i$ is obtained by your basic reasoning, just writing down the probability for each way the $i$th person's seat might already be taken, taking into account each case e.g. for $X_4$ the term $1/n(n-2)$ corresponds to the case that the first person takes the third person's seat, and the third person takes the fourth person's seat. I'm not sure if/what an easy formula would be for when you add up all the $X_i$ terms to get the total answer. However it is easy to see that the general rule to get the equation for $X_i$ when $i > 1$ is to have $X_i = 1 - q_i$ where $q_i$ is the sum of all terms you can make of the form $1/n(n-k_1)(n-k_2)\ldots$ where you have $0$ or more distinct values $k_j$, and $k_1 < k_2 \ldots < i-1$.

-
Oops, sorry about the $p_i \rightarrow X_i$ change. And you are write about $X_4$. Thanks for your answer, that seems correct. –  narcissa Aug 10 '13 at 23:59

I found this question and the answer might be relevant.

Seating of $n$ people with tickets into $n+k$ chairs with 1st person taking a random seat

The answer states that the probability of a person not sitting in his seat is $\frac{1}{k+2}$ where $k$ is the number of seats left after he takes a seat. This makes sense because for person $i$, if anyone sits in chairs $1, i+1, ... n$ then he must sit in his own seat, so the probability of that happening is $\frac{n-i+1}{n-i+2}$. So $k = 0$ for the last person and $k = n-1$ for the second person. The answer then should just be

$1/n + \sum_{i = 2}^{n} \frac{n-i+1}{n-i+2}$

-
The answer (see my post) is $n-H_{n-1}$, but this is probably the right way to go. –  Donkey_2009 Aug 11 '13 at 0:56

The answer is $\frac{1}{2}$ as was said.

The general pattern is that for $n$ people, there is a $\frac{1}{n}$ probability of success, $\frac{1}{n}$ probability of failure, and an $\frac{n-2}{n}$ probability that the problem repeats itself on the $n-1$ scale.

Case $n=2$: The probability that the first picks the correct seat is $\frac{1}{2}$, and then the last person sits in proper seat with probability 1. The probability that the first picks the wrong seat is $\frac{1}{2}$, and then the last person sits in proper seat with probability 0. So the probability in total is: $$\frac{1}{2}\cdot1+\frac{1}{2}\cdot0=\frac{1}{2}$$

Case $n=3$: The probability that the first picks the correct seat is $\frac{1}{3}$, and then the last person sits in proper seat with probability 1. The probability that the first picks the last person's seat is $\frac{1}{3}$, and then the last person sits in proper seat with probability 0. The probability that the first picks the middle person's seat is $\frac{1}{3}$, and now there is a $\frac{1}{2}$ that the second person picks the last seat or the first seat (Case 2) since two non-last people switching seats means everyone else takes their own seat. So: $$\frac{1}{3}\cdot1+\frac{1}{3}\cdot0+\frac{1}{3}\cdot\frac{1}{2} = \frac{2}{6} + \frac{1}{6} = \frac{3}{6} = \frac{1}{2}$$

Proof by induction.

Assume it holds true for $n$ that the probability of the last person sitting in the proper seat is $\frac{1}{2}$. Now if there are $n+1$ people, we have a $\frac{1}{n+1}$ chance that the last-seat probability is 1 (correct seat), a $\frac{1}{n+1}$ chance that the last-seat probability is 0 (last seat), and an $\frac{n-1}{n+1}$ chance that the probability is $\frac{1}{2}$, since we know the $n$ case. $$\frac{1}{n+1} + \frac{0}{n+1} + \frac{n-1}{2(n+1)}\\ =\frac{2}{2(n+1)}+\frac{n-1}{2(n+1)}\\ =\frac{n+1}{2(n+1)}\\ =\frac{1}{2}$$ QED

-
I feel bad for all your work, but the question was the expected number of people in correct seats. –  Lord_Farin Aug 23 '13 at 8:17

Now that I have cleaned my glasses, I'll try again. Thank you, Lord Farin.

Some observations.

The answer must be greater than 1. The probability that the first person, regardless of the number of people, sits in the proper seat is $\frac{1}{n}$, and there are $n$ people, so that expectation is 1. Even if the first person sits in the wrong seat, there is non-zero probability of other people sitting in the correct seat, so the answer must be greater than or equal to 1, and I'm pretty sure equality only exists in the case $n=2$.

There is some form of recurrence going on here. Enumerating the possibilities, I get (if I haven't erred): $$E_2 = \frac{1}{2}\cdot 2 + \frac{1}{2}\cdot 0 = 1\\ E_3 = \frac{1}{3}\cdot 3 + \frac{2}{3}\left[\frac{1}{2}\cdot 1 + \frac{1}{2}\cdot 0\right] = 1+\frac{1}{3}=\frac{4}{3}\\ E_4 = \frac{1}{4}\cdot 4 + \frac{3}{4}\left[\frac{1}{3}\cdot2 + \frac{2}{3}\left[\frac{1}{2}\cdot 1 + \frac{1}{2}\cdot 0\right]\right] = 1+\frac{3}{4}\cdot\left(\frac{2}{3}+\frac{1}{3}\right)=\frac{7}{4}\\ E_5 = \frac{1}{5}\cdot 5 + \frac{4}{5}\left[\frac{1}{4}\cdot3 + \frac{3}{4}\left[\frac{1}{3}\cdot2 + \frac{2}{3}\left[\frac{1}{2}\cdot 1 + \frac{1}{2}\cdot 0\right]\right]\right] = 1+\frac{4}{5}\left(\frac{3}{4} + \frac{3}{4}\cdot\left(\frac{2}{3}+\frac{1}{3}\right)\right)=\frac{11}{5}\\$$ Let $E_k$ be the expected number of people in the correct seats when the starting population is k. The relationship seems to be: $$E_k = 1 + \left(E_{k-1} - 1 + \frac{k-2}{k-1}\right)\frac{k-1}{k}$$ The first 1 is the expected value of person 1 taking the correct seat. The next term is broken into two parts. If the first person did not take the correct seat, then the second person can "fix" the error by swapping and taking the previous person's seat, leaving the remaining $n-2$ people their proper seats. If the second person also takes the wrong seat, the problem restarts on the $n-1$ scale. In both the latter two cases, there is "one less" correct seat than the initial, since the previous term used up a seat with an incorrect choice. If the above supposition is correct, the expected value would just be the sum of the recurrence relation applied to $n$ or $\sum_{k=1}^n E_k$.

Generating the first few terms using this relationship gives: $$1, 1, \frac{4}{3}, \frac{7}{4}, \frac{11}{5}, \frac{16}{6}, \frac{22}{7}, \frac{29}{8}, \frac{37}{9}, \frac{46}{10}, \ldots$$

The numerator is a quadratic and the denominator is just $n$ so the expected value should be: $$E_n = \frac{n^2-n+2}{2n}$$

Edit: The long-term proportion of people sitting in the proper seats intuitively would be $$\lim_{n \to \infty} \frac{E_n}{n}\\ =\lim_{n \to \infty} \frac{n^2-n+2}{2n^2}\\ =\frac{1}{2}$$ Which dovetails nicely with the long-term probability of the last person sitting in his or her proper seat.

-
Your recurrence is wrong, as are your values for $E_k$ where $k\ge3$. I'm not really sure what your argument is; are you free to take this to chat some time today? –  Donkey_2009 Aug 24 '13 at 12:06
Sure. If I'm wrong, I'm eager to learn why and how to correct it. If you want, you can e-mail me directly as well; thank you. –  Avraham Aug 26 '13 at 1:18
It's 1. The probability that the $i$th person is in the right seat is $1/n$ for every $i$. This is clearly true for $i=1$, for $i=2$ you get $$P(\text{1st person not in 2nd person's seat, 2nd person in right seat}) = \frac{n-1}{n} \frac{1}{n-1} = 1/n,$$ and so on down the line as you can check for yourself.
This answer is wrong. The probability that the second person is in the right seat is $1$ if the first person didn't sit in their seat. I think kilgol was assuming that people just sit in their seats randomly, whereas they in fact always sit in their own seat if it is free (and they're not the first person). –  Donkey_2009 Aug 11 '13 at 0:49


# Why is the answer to this probability question not $\frac{1}{2}$?

I was trying to solve the following homework probability question which has the following setup:

We have $$2$$ dice: $$A$$ and $$B$$. Die $$A$$ has $$4$$ red faces and $$2$$ white faces, whereas die $$B$$ has $$2$$ red faces and $$4$$ white faces. On each turn, a fair coin is tossed. If the coin lands on heads then die $$A$$ is thrown, but if the coin lands on tails then die $$B$$ is thrown. After this the turn ends, and on the next turn the coin is once again tossed to determine the die (i.e. we throw the coin and the corresponding die on each turn).

From this game I'm asked to answer $$2$$ questions:

1. Show that the probability of obtaining a red face on any $$n$$-th throw is $$\frac{1}{2}$$.
2. If the first $$2$$ consecutive die throws result in red faces, what is the probability that the third throw is also red?

To answer part $$1$$ I used the law of total probability. Denoting obtaining a red face on the $$n$$-th die throw as $$P(R_n)$$ I get

\begin{align*} P(R_n) &= P(R_n \vert A) P(A) + P(R_n \vert B) P( B) \\ & = \left(\frac{4}{6}\right)\left(\frac{1}{2}\right) + \left(\frac{2}{6}\right)\left(\frac{1}{2}\right)\\ & = \frac{1}{2} \end{align*}

But on question $$2$$ is where I started running into trouble. Using the same notation, what I want to calculate is $$P(R_3 \vert R_2 R_1)$$. And recalling that for events $$E_1$$ and $$E_2$$ we can say that $$P(E_2 \vert E_1) = \frac{P(E_2E_1)}{P(E_1)}$$ I get that $$P(R_3 \vert R_2 R_1) = \frac{P(R_3 R_2 R_1)}{P(R_2 R_1)} \tag{1}$$ and from here I obtain 2 different solutions using $$2$$ distinct methods:

Using that on the first part of the question we showed that $$P(R_n) = \frac{1}{2}$$, and noticing that the die throws are independent since what I threw before does not affect how I throw the next coin toss or how I roll the next die, from equation $$(1)$$ I get \begin{align*} P(R_3 \vert R_2 R_1) &= \frac{P(R_3 R_2 R_1)}{P(R_2 R_1)}\\ &= \frac{P(R_3) P(R_2) P(R_1)}{P(R_2) P(R_1)}\\ & = P(R_3) = P(R_n) = \frac{1}{2} \end{align*}

Using the law of total probability on $$(1)$$ I get \begin{align*} P(R_3 \vert R_2 R_1) & = \frac{P(R_1 R_2 R_3 \vert A)P(A)+ P(R_1 R_2 R_3 \vert B)P(B)}{P(R_1 R_2 \vert A ) P(A)+ P(R_1 R_2 \vert B) P(B)}\\ &= \frac{\left[\left(\frac{2}{3}\right)\left(\frac{2}{3}\right)\left(\frac{2}{3}\right)\right]\frac{1}{2}+ \left[\left(\frac{1}{3}\right)\left(\frac{1}{3}\right)\left(\frac{1}{3}\right)\right]\frac{1}{2}}{\left[\left(\frac{2}{3}\right)\left(\frac{2}{3}\right)\right]\frac{1}{2}+\left[\left(\frac{1}{3}\right)\left(\frac{1}{3}\right)\right]\frac{1}{2}}\\ & = \frac{3}{5} \end{align*}

To me both of the previous answers seem to be following coherent logic, but since I didn't get the same answer I knew one of them was wrong. I decided to write a program to simulate the game and I found out that the correct solution was $$P(R_3 \vert R_2 R_1) = \frac{3}{5}$$. But even though I verified this answer to be correct I couldn't seem to understand what part of my analysis is wrong on Answer 1.

So my question is, why is $$P(R_3 \vert R_2 R_1) \neq \frac{1}{2}\quad ?$$

• I'm confused about the nature of this experiment. Are you repeatedly tossing a coin then rolling the corresponding die $n$ times (i.e. $n$ coin tosses and $n$ dice rolls), or are you tossing a coin once, then rolling the designated die $n$ times (i.e. $1$ coin toss, and $n$ die rolls)? – Theo Bendit Mar 23 at 5:56
• It's the first option. We have one coin toss and one dice roll per turn. I'll edit the problem to clarify this, thank you! – Robert Lee Mar 23 at 5:59
• In your computation, $A$ determines the successive $3$ roll of the die. – Oolong milk tea Mar 23 at 6:07
• If it's one coin toss and one dice roll per turn, then every (coin toss + dice roll) is independent of all the others, and the probability is $1/2$ every time. If one coin toss determined multiple dice rolls, then those dice rolls would not be mutually independent, and you would get something greater than $1/2$ (because there would be a positive correlation between rolls using the same coin toss). But if you're saying it's one coin toss and one dice roll per turn, then your simulation is wrong. – mjqxxxx Mar 23 at 6:24
• I don’t quite get the logic in answer 2. To get consecutively 2 times red, you have AA, BB, AB, BA 4 paths with their own probabilities, would expect 1/2x1/2x2/3x2/3 for AA path,... and we should have 4 terms in the denominator. Do I miss something? – BStar Mar 23 at 6:38

The flaw in your second answer is that it is not necessarily the same die that is thrown after each coin toss. The way you have written the law of total probability is acceptable in your first answer, but not in the second, because it is only in the second answer that you conditioned on the events $$A$$ and $$B$$, which represent the outcomes of the coin toss. Consequently, the result is incorrect because it corresponds to a model in which the coin is tossed once, and then the corresponding die is rolled three times.

First, let us do the calculation the proper way. We want $$\Pr[R_3 \mid R_1, R_2] = \frac{\Pr[R_1, R_2, R_3]}{\Pr[R_1, R_2]}$$ as you wrote above. Now we must condition on all possible outcomes of the coin tosses, of which there are eight: \begin{align} \Pr[R_1, R_2, R_3] &= \Pr[R_1, R_2, R_3 \mid A_1, A_2, A_3]\Pr[A_1, A_2, A_3] \\ &+ \Pr[R_1, R_2, R_3 \mid A_1, A_2, B_3]\Pr[A_1, A_2, B_3] \\ &+ \Pr[R_1, R_2, R_3 \mid A_1, B_2, A_3]\Pr[A_1, B_2, A_3] \\ &+ \Pr[R_1, R_2, R_3 \mid A_1, B_2, B_3]\Pr[A_1, B_2, B_3] \\ &+\Pr[R_1, R_2, R_3 \mid B_1, A_2, A_3]\Pr[B_1, A_2, A_3] \\ &+ \Pr[R_1, R_2, R_3 \mid B_1, A_2, B_3]\Pr[B_1, A_2, B_3] \\ &+\Pr[R_1, R_2, R_3 \mid B_1, B_2, A_3]\Pr[B_1, B_2, A_3] \\ &+ \Pr[R_1, R_2, R_3 \mid B_1, B_2, B_3]\Pr[B_1, B_2, B_3] \\ \end{align} and since each of the $$2^3 = 8$$ triplets of ordered coin tosses has equal probability of $$1/8$$ of occurring, $$\Pr[R_1, R_2, R_3] = \tfrac{1}{8}\left((\tfrac{2}{3})^3 + 3(\tfrac{2}{3})^2(\tfrac{1}{3}) + 3(\tfrac{2}{3})(\tfrac{1}{3})^2 + (\tfrac{1}{3})^3\right) = \tfrac{1}{8}.$$ A similar (but simpler) calculation for the denominator yields $$1/4$$, and the result follows.

Of course, none of this is necessary; it is only shown here to illustrate how the calculation would be done if it were to be done along the lines of your second answer.

• I think I'm starting to understand where my lack of understanding is. I believe I may not understand what $P(R_3 \vert R_2 R_1)$ is to begin with. Let's say I list all my throws as red or white accordingly. If I then count how many chains of 3 consecutive reds there are on my list, and I also count the number of times 2 reds are succeded by a white, is $$P(R_3 \vert R_2 R_1) \sim \frac{\text{#RRR}}{\text{#RRW} + \text{#RRR}}$$ in the sense that $\sim$ means the RHS approaches $P(R_3 \vert R_2 R_1)$ as I take more throws? Or does $P(R_3 \vert R_2 R_1)$ mean something else? – Robert Lee Mar 23 at 7:41
• I ran out of space in my previous comment, but #RRR stands for the number of chains of 3 consecutive reds on my list of throws, and #RRW is the number of chains where the first 2 places are red and the third consecutive throw is white, again on my list of throws. Just to clarify what I meant in the last comment. – Robert Lee Mar 23 at 7:46
• @RobertLee It is unnecessary and mathematically inappropriate to think of the problem in terms of long-run averages of more than three rolls, because the entire question can be answered precisely under a strict interpretation of the model, in which there are only ever three rolls, each of which is preceded by a coin toss. – heropup Mar 23 at 7:57
• I agree that it's not necessary to think of this mathematically. I only ask this because of my previously described attempt to verify the theoretical result with a simulation, which led me to an incorrect result. I'm trying to understand if what I programmed is in fact not trying to calculate the probability $P(R_3 \vert R_2 R_1)$ that I want, or if I'm still not understanding what the probability in itself means. – Robert Lee Mar 23 at 8:07
• @RobertLee The frequentist approach via simulation would be as follows. [1] Locate all occurrences of two consecutive red dice rolls. [2] For each such occurrence, look at the outcome of the die roll immediately after the pair of reds. [3] Tally the number of outcomes where the third die is red. Divide the number obtained in step 3 by the total number of red pairs observed in step 1. – heropup Mar 23 at 8:18

With the second approach:

To have first two die throws resulting in red, there are 2x2 paths: AA,AB,BA, BB $$\to$$

$$P(R1\cap R2)=(\frac{1}{2})^2(\frac{2}{3})^2$$+2$$(\frac{1}{2})^2\frac{2}{3}\frac{1}{3}$$+$$(\frac{1}{2})^2(\frac{1}{3})^2$$ =$$\frac{1}{4}$$

To have three die throws resulting in red, there are 2x2x2 paths:AAA,BBB,AAB,ABA,BAA,BBA,BAB,ABB $$\to$$

$$P(R1\cap R2\cap R3)= (\frac{1}{2})^3(\frac{2}{3})^3$$+3$$(\frac{1}{2})^3(\frac{2}{3})^2\frac{1}{3}$$+3$$(\frac{1}{2})^3(\frac{1}{3})^2\frac{2}{3}$$+ $$(\frac{1}{2})^3(\frac{1}{3})^3$$ =$$\frac{1}{8}$$

$$\frac{P(R1\cap R2\cap R3)}{P(R1\cap R2)}$$ =$$\frac{1}{2}$$

In this way, you get consistent result compared to first approacch.


# Number of bit strings of length four do not have two consecutive 1s

I came across following problem:

How many bit strings of length four do not have two consecutive 1s?

I solved it as follows:

Total number of bit strings of length: $$2^4$$
Total number of length 4 bit strings with 4 consecutive 1s: 1
Total positions for three consecutive 1s in length 4 bit string: 2 (111X, X111)
Number of bit strings for each of above positions: 2 (X can be 0 or 1)
Total positions for two consecutive 1s in length 4 bit string: 3 (11XX, X11X, XX11)
Number of bit strings for each of above positions: 4
By inclusion exlcusion principle, the desired count $$=2^4-3\times 4+2\times 2-1=16-12+4-1=7$$

However the correct solution turns out to be 8. It seems that I incorrectly applied inclusion exclusion principle. Where did I go wrong?

• You should be doing inclusion-exclusion on the number of pairs of consecutive ones, not on the length of a string of consecutive ones. – Gerry Myerson Sep 16 '19 at 6:27

If I were doing this by inclusion-exclusion, I'd go: $$16$$ strings of length four; $$12$$ with at least one pair of consecutive ones ($$11xx,x11x,xx11$$ with $$x$$s arbitrary); five with at least two pair of consecutive ones ($$111x,1111,x111$$); one with three pair of consecutive ones; so $$16-12+5-1=8$$.

To count the number of bit strings with $$2$$ consecutive one bits (bad strings), I would let \begin{align} S_1&=11xx&4\\ S_2&=x11x&4\\ S_3&=xx11&4\\ N_1&=&12 \end{align} Then \begin{align} S_1\cap S_2&=111x&2\\ S_1\cap S_3&=1111&1\\ S_2\cap S_3&=x111&2\\ N_2&=&5 \end{align} and \begin{aligned} S_1\cap S_2\cap S_3&=1111&1\\ N_3&=&1 \end{aligned} The count of bad strings is $$N_1-N_2+N_3=8$$.
The count of good strings is $$16-8=8$$.

Generating Functions

Let $$x$$ represent the atom '$$0$$' and $$x^2$$ represent the atom '$$10$$' and build all possible strings by concatenating one or more atoms and removing the rightmost '$$0$$'. \begin{align} \overbrace{\vphantom{\frac1x}\ \ \ \left[x^4\right]\ \ \ }^{\substack{\text{strings of}\\\text{length 4}}}\overbrace{\ \quad\frac1x\ \quad}^{\substack{\text{remove the}\\\text{rightmost '0'}}}\sum_{k=1}^\infty\overbrace{\vphantom{\frac1x}\left(x+x^2\right)^k}^\text{k atoms} &=\left[x^4\right]\frac{1+x}{1-x-x^2}\\ &=\left[x^4\right]\left(1+2x+3x^2+5x^3+8x^4+13x^5+\dots\right)\\[9pt] &=8 \end{align} Note that the denominator of $$1-x-x^2$$ induces the recurrence $$a_n=a_{n-1}+a_{n-2}$$ on the coefficients.

Recurrence

Good strings of length $$n$$ can be of two kinds: a good string of length $$n-1$$ followed by '$$0$$' or a good string of length $$n-2$$ followed by '$$01$$'. That is, $$a_n=a_{n-1}+a_{n-2}$$ Starting with
$$a_0=$$ the number of good strings of length $$0=1$$.
$$a_1=$$ the number of good strings of length $$1=2$$.
we get $$a_4=8$$.


# When is a function bounded using differentiation

1. Jan 16, 2008

### sara_87

1. The problem statement, all variables and given/known data
how do i determine whether a function is bounded using differentiation

eg: f(x)=x/(2^x)
2. Relevant equations

3. The attempt at a solution

i know it has something to do with maximums and minimums but i cant figure out how to do it.

any help would be appreciated. thank you

2. Jan 16, 2008

### Dick

You could look at the limits of the function as it approaches plus and minus infinity. If both exist and are finite, and if the function is defined and continuous for all x, then it is bounded.

3. Jan 16, 2008

### EnumaElish

You can use differentiation to investigate the behavior of f. Say, the function is f(x) = x/2^x on x > 0. Then f'(x) = 2^-x (1 - x Log[2]), which has roots 1/Log[2] and +infinity. At x = 1/Log[2], f''(x) = 2^-x Log[2] (-2 + x Log[2]) is < 0, so you have the maximum. Note that f(x) > 0 for x > 0 and f(0) = 0. As x --> +infinity, f(x) --> 0 from above; but f(0) = 0 so x = 0 is the minimum. Since you can "account for" both the maximum and the minimum, f is bounded on x > 0.

Last edited: Jan 16, 2008
4. Jan 16, 2008

### sara_87

thank you very much.
what if we have:
f(x)=(-2x^2)/(4x^2-1)
i know that it's not bounded but i dont know why

5. Jan 16, 2008

### Tom Mattson

Staff Emeritus
The graph of that function has two vertical asymptotes. Functions don't get much more "unbounded" than that!

6. Jan 16, 2008

### sara_87

actually i think it is bounded because there's no value of x that would make that function greater than 1
or is there?

7. Jan 16, 2008

### Tom Mattson

Staff Emeritus
Sure there is. As I said, the graph of that function has 2 vertical asymptotes. You can find values of x for which the function blows up to infinity, and down to negative infinity.

Do you know what I mean when I say "vertical asymptote"?

8. Jan 16, 2008

### sara_87

yes i do know what vertical assymptotes are.
umm but i still didnt understand what u meant. you can find values of x for which the function blows down to -ve infinity but not up.
?

9. Jan 16, 2008

### Tom Mattson

Staff Emeritus
The function certainly does blow up to positive infinity, as you approach -1/2 from the right and as you approach +1/2 from the left.

10. Jan 16, 2008

### sara_87

oh thank u very much
that helps.
just one last question:
same question as before but with function:
sqrt(x)/1000

is it not bounded since n continues to increase to infinity?

11. Jan 17, 2008

### Tom Mattson

Staff Emeritus
You're right that it's not bounded (on $[0,\infty)$ that is--we really should be specifying an interval when making these statements).

But what's "n"?

12. Jan 17, 2008

### HallsofIvy

Staff Emeritus
Now, I'm confused as to what function you are talking about. The original function was f(x)= x/(2^x) which is definitely bounded on $[0, \infty)$. It is bounded "above" but not bounded "below" so is not bounded. I don't see any asymptotes when I graph it.

13. Jan 17, 2008

### Tom Mattson

Staff Emeritus
Posts 1 through 3 pertain to f(x)=x/(2^x).
Posts 4 through 9 pertain to f(x)=(-2x^2)/(4x^2-1).
Posts 10 and 11 pertain to f(x)=sqrt(x)/1000.

14. Jan 17, 2008

### EnumaElish

Post 3 pertains to f(x)=x/(2^x) for x > 0.


# Math Help - Integrate cos(x)cos(4x) dx

1. ## Integrate cos(x)cos(4x) dx

I'm trying to integrate:

$\int\cos(x)\cos(4x) dx$

Could some one tell me what I should be seeing about this to help me solve it. I thought I needed to change the $4x$ into $x$ and tried doing that using the double angle formula, but I have to use it twice and end up with a integrand that looks too complicated. I'd like to know what a good first step would be.

Thank you.

2. ## Re: Integrate cos(x)cos(4x) dx

this is easy...but you have to use a formula that transforms the product cosxcos4x into a sum.

HERE cos(4x)cos(x)=(1/2)[cos(5x)-cos(3x)}

Minoas

3. ## Re: Integrate cos(x)cos(4x) dx

Originally Posted by Furyan
I'm trying to integrate:

$\int\cos(x)\cos(4x) dx$

Could some one tell me what I should be seeing about this to help me solve it. I thought I needed to change the $4x$ into $x$ and tried doing that using the double angle formula, but I have to use it twice and end up with a integrand that looks too complicated. I'd like to know what a good first step would be.

Thank you.
\displaystyle \begin{align*} I &= \int{\cos{(x)}\cos{(4x)}\,dx} \\ I &= \frac{1}{4}\cos{(x)}\sin{(4x)} - \int{ -\frac{1}{4}\sin{(x)}\sin{(4x)}\,dx } \\ I &= \frac{1}{4}\cos{(x)}\sin{(4x)} + \frac{1}{4}\int{\sin{(x)}\sin{(4x)}\,dx} \\ I &= \frac{1}{4}\cos{(x)}\sin{(4x)} + \frac{1}{4} \left[ -\frac{1}{4}\sin{(x)}\cos{(4x)} - \int{ -\frac{1}{4}\cos{(x)}\cos{(4x)} \,dx} \right] \\ I &= \frac{1}{4}\cos{(x)}\sin{(4x)} - \frac{1}{16}\sin{(x)}\cos{(4x)} + \frac{1}{16}I \\ \frac{15}{16}I &= \frac{1}{4}\cos{(x)}\sin{(4x)} - \frac{1}{16}\sin{(x)}\cos{(4x)} \\ I &= \frac{4}{15}\cos{(x)}\sin{(4x)} - \frac{1}{15}\sin{(x)}\cos{(4x)} + C \end{align*}

4. ## Re: Integrate cos(x)cos(4x) dx

Thank you Prove It,

I haven't seen that before, integrating by parts twice and then letting the second integrand be I and solving that way. That's a very useful way of solving difficult integration problems. Thank you very much for showing me that. I tried this method with a similar problem that I had solved another way and found that I had to be very careful with signs and factors. I kept getting the wrong answer because I was getting a sign and factor wrong during the second integration. So I'm going to have to work on it, but it's great to know that if I can't see what else to do I can try this method.

Thanks again for all your efforts

5. ## Re: Integrate cos(x)cos(4x) dx

Originally Posted by Furyan
I'm trying to integrate:

$\int\cos(x)\cos(4x) dx$

Could some one tell me what I should be seeing about this to help me solve it. I thought I needed to change the $4x$ into $x$ and tried doing that using the double angle formula, but I have to use it twice and end up with a integrand that looks too complicated. I'd like to know what a good first step would be.

Thank you.
Hi Furyan!

As you can see here, we have the identity:
$\cos \theta \cos \varphi = {{\cos(\theta - \varphi) + \cos(\theta + \varphi)} \over 2}$

In other words:
$\int\cos(4x)\cos(x) dx = \int{{\cos(4x - x) + \cos(4x + x)} \over 2} = \int{{\cos(3x) + \cos(5x)} \over 2}$

This is what Minoas already suggested (except for the typo ).

6. ## Re: Integrate cos(x)cos(4x) dx

Hi ILikeSerena

Thank you very much for that. I did, in fact, also solve this problem using the factor formula, as Minoanman suggested, and it was very much simpler. However, although there are only four factor formulae in my book I'm worried that I might not remember them in an exam. Although I found Prove It's method more difficult I feel I'm more likely to remember it and at least get some method marks, even if I make some mistakes.

I'm amazed how different the solutions look, depending on which method you use and yet they're equivalent. I solved one question using Prove It's method, the factor formula and a simple identity. All the solutions were equivalent, but they looked very different. I wouldn't have been able to rewrite any of them in terms of any of the others.

7. ## Re: Integrate cos(x)cos(4x) dx

Here's a trick that I like to use when I can't properly remember the formulas.

The only formula you need to remember is Euler's formula:
$e^{ix} = \cos x + i \sin x$

together with its rewritten forms (that you can both deduce from Euler's formula if you forget):
$\cos x = \dfrac 1 2 (e^{ix} + e^{-ix})$
$\sin x = \dfrac 1 {2i} (e^{ix} - e^{-ix})$

$\cos(4x)\cos(x) = \frac 1 2 (e^{i4x} + e^{-i4x}) \cdot \frac 1 2 (e^{ix} + e^{-ix})$
$= \frac 1 4 ((e^{i5x} + e^{-i5x}) + (e^{i3x} + e^{-i3x}))$

$= \frac 1 2 (\cos(5x) + \cos(3x)) \qquad \blacksquare$

8. ## Re: Integrate cos(x)cos(4x) dx

whoa! $\blacksquare$

That's a whole new level, but I'm liking it. I'm much better at deducing formualae than I am at remember them. Euler's formula, eh! I'm sure I came across that earlier today, but I glazed over when I saw $e^{ix}$. Is that i ,the i, $\sqrt{-1}?$. I'm going to look that up and try and find out why it's so fundamental. It looks very interesting.

Thank you very much for that

9. ## Re: Integrate cos(x)cos(4x) dx

Originally Posted by Furyan
whoa!

That's a whole new level, but I'm liking it. I'm much better at deducing formualae than I am at remember them. Euler's formula, eh! I'm sure I came across that earlier today, but I glazed over when I saw $e^{ix}$. Is that i ,the i, $\sqrt{-1}?$. I'm going to look that up and try and find out why it's so fundamental. It looks very interesting.

Thank you very much for that
Yep. It is that $i$.

According to wiki:
Euler's formula is ubiquitous in mathematics, physics, and engineering. The physicist Richard Feynman called the equation "our jewel" and "one of the most remarkable, almost astounding, formulas in all of mathematics."[2]

10. ## Re: Integrate cos(x)cos(4x) dx

Originally Posted by ILikeSerena
Yep. It is that $i$.

According to wiki:
Euler's formula is ubiquitous in mathematics, physics, and engineering. The physicist Richard Feynman called the equation "our jewel" and "one of the most remarkable, almost astounding, formulas in all of mathematics."[2]
Wow, it looks like a jewel. How exciting.

Thank you very much


# Given a decreasing function s.t. $\int_0^\infty f(x)\,dx<\infty,$ prove $\sum_{n=1}^\infty f(na)$ converges

Let $f\in C([0,\infty))$ be a decreasing function such that $\int_0^\infty f(x)\,dx$ converges.

Prove $\sum_{n=1}^\infty f(na)$ converges, $\forall a>0$

My attempt:

By the Cauchy criterion, there exists $M>0,$ such that for $t-1>M:$

$$f(t)=\int_{t-1}^t f(t) \, dx \leq \int_{t-1}^t f(x)\,dx\xrightarrow{t \to \infty} 0$$

Hence, $f$ is non-negative.

$f$ is decreasing $\implies f(na)\leq f(a), \forall a>0, n\in \mathbb{N}.$

By integral monotonicity and non-negativity of $f$:

$$\int_1^\infty f(nx)\,dx \leq \int_1^\infty f(x)\,dx \leq \int_0^\infty f(x)\,dx$$

Hence $\int_1^\infty f(nx)\,dx$ converges and therefore $\sum_{n=1}^\infty f(na)$ converges.

Is that correct? If so, why is continuity necessary ? Is there a simpler way to prove it?

Any help appreciated.

• $$\int_{(n-1)a}^{na}f(x)dx\geq af(na)$$ – MAN-MADE Aug 22 '17 at 18:33

Hint: The following thing you wrote is the key:

$$f(t)=\int_{t-1}^{t}f(t)dx \leq \int_{t-1}^{t}f(x)dx$$

After that, just pick $t=an$ and sum over all $n$.

PS: and no, the assumption on continuity is not needed. Just integrability for the well-definedness.

As I commented before:

Since $f$ is decreasing and continuous, $$\int_{(n-1)a}^{na}f(x)dx\geq [na-(n-1)a]\inf_{(n-1)a< x\leq na}\{f(x)\}=af(na)$$

Then $$\int_{0}^{\infty}f(x)dx=\sum_{n=1}^{\infty}\int_{(n-1)a}^{na}f(x)dx\geq a\sum_{n=1}^{\infty}f(na)$$

Hence, since $a\in\mathbb{R}^+,$ $\sum_{n=1}^{\infty}f(na)<\infty$

\begin{align} & af(a) + af(2a) + af(3a) +\cdots \\[10pt] \le {} & \int_0^a f + \int_a^{2a} f + \int_{2a}^{3a} f + \cdots <\infty. \end{align}

This works if $f \ge0$ everywhere. If $f<0$ somewhere, then an easy argument shows the integral does not converge.

This is the integral test for convergence of series. Do a change of variable x --- to --- ax. The integral converges so does series g(n)=f(na).