The Frog Problem (puzzle in YouTube video)

Question: There is an interesting puzzle in YouTube video Can you solve The Frog Problem?. I'll try to give an equivalent formulation here.
A frog is on one side of the pond and wants to get on the other side. There are $n$ lily leaves ahead in a line, the $n$-th leave laying on the other end of the pond and being the destination. Whatever the position the frog is at any time, it will only go ahead and the probability to land on one of the leaves left in front of it (including the destination) is uniform. So, for example, if there are 10 leaves ahead, there is a probability of $\frac{1}{10}$ that it will land on any of them.
What is the expected value for the number of jumps it will take the frog to arrive to the destination leaf? The answer cannot be a recursive expression.
I think I have a solution, I'll report it as an answer below.
Answer: We will call $J_n$ the expected value for the jumps when there are $n$ leaves ahead. We set also $J_0 = 0$, which is consistent with the fact that if the frog has no leaf ahead it needs to do exactly $0$ jumps to arrive at destination.
We will name/number the leaves according to their distance from the destination. So the destination will be leaf $0$, the one immediately before $1$ and so on up to leaf $n-1$ that is the one in front of the frog. There are in total $n$ leaves and the probability to jump on any of them with one leap is $\frac{1}{n}$ according to the puzzle indications.
When the frog takes this first leap, it will land on leaf $k$, with $k \in \{0, ... n-1\}$ and, from that point, the expected value of remaining leaps will be $J_k$. Considering that these events are mutually exclusive, we get the following:
$$J_n = \sum_{k=0}^{n-1}\frac{1}{n}(1 + J_k)$$
where the $1$ represents the first leap to reach position $k$. As there are $n$ elements in the sum, it can be rearranged as:
$$J_n = 1 + \frac{1}{n} \sum_{k=0}^{n-1}J_k$$
which is indeed a bit too recursive. With simple arithmetics we can further rearrange it as follows:
$$n(J_n - 1) = \sum_{k=0}^{n-1}J_k$$
This relation is generic and can be also rewritten with $n-1$ instead of $n$:
$$(n-1)(J_{n-1} - 1) = \sum_{k=0}^{n-2}J_k$$
Subtracting the two relations we get:
$$n(J_n - 1) - (n-1)(J_{n-1} - 1) = \sum_{k=0}^{n-1}J_k - \sum_{k=0}^{n-2}J_k = J_{n-1}$$
that is:
$$n(J_n - 1) = (n-1)(J_{n-1} - 1) + J_{n-1} = nJ_{n-1} - (n-1)$$
$$J_n - 1 = J_{n-1} - \frac{n-1}{n}$$
$$J_n = J_{n-1} + \frac{1}{n}$$
Still recursive, but at least the $n$-th element is expressed in terms of $n-1$-th element only.
Now considering that $J_0 = 0$ the relation above can be collapsed to:
$$J_n = \sum_{k = 1}^{n}\frac{1}{k}$$
which is the answer to the puzzle.
Comment: @whuber the fact that the final expression requires an iterative process taking $O(n)$ does not necessarily make it recurrence relation (e.g. in the article you linked, the recurrence relation is explicitly indicated as $H_{n+1} = H_n + \frac1{n+1}$). You might just as well decide to rearrange the terms during the sum as you see fit, while this would not be possible by leveraging the recurrence relation alone.
Comment: You can also reason more shorter and intuitively: the frog with $n$ leaves ahead of him has the same options in front of him as the frog with $n-1$ leaves ahead *plus* the option that he first needs to make an extra step (land on the first leave in front of him which the frog in front of him does not have) with probability $1/n$. Thus $J_n = J_{n-1} + 1/n $ can be reasoned directly.
Comment: And now do the same puzzle when the frog can also stay in the same place.
Comment: When the frog can also go one step backwards then you have $$J_n = J_{n-1} *(n-1)/(n+1) + (J_{n}+1)/(n+1) + (J_{n+1}+1)/(n+1)$$ leading to $$J_{n+1} = n J_n - (n-1) J_{n-1}$$
Comment: The final equation should have $k$ in the denom instead of $n$
Comment: @MartijnWeterings I can only say wow, if you would turn the comment(s) into an answer it will be my accepted one!
Comment: It is only a small comment that allows you to elliminate that term $\sum J_k $ at once instead of the 'subtracting the two relations' that you do. Your answer is already a sufficient answer. That's why I added it as comment.
Comment: You should check out my new question on the same topic.
Comment: There is an interresting twist to this question. Instead of stating 'the answer cannot *be* a recursive relation' we can also ask 'the derivation of the answer cannot *use* a recursive relation'. We can solve it without the use of a recursive relation. We can plot the probability where the frog is after 1 step (a uniform distribution) after 2 steps (a triangular distribution) after 3 steps (a quadratic function for the distribution) after 4 steps (cubic) and so on. And then use the probability to be in the final leave at 1...10 steps. I will post that as an answer later this weekend.
Comment: I start to feel like I opened Pandora's vase...
Comment: @L.ScottJohnson thanks for the corrections!
Comment: Could you indicate how this answer is not recursive?  *By definition,* the summation is given by $J_1=1$ and $J_{n} = 1/n + J_{n-1}.$  Yes, it's a simple recursion and its solution even has a name (these are the [harmonic numbers](https://en.wikipedia.org/wiki/Harmonic_number)), but that doesn't obviate its recursive nature.
Comment: @whuber the answer uses a recurrence relation, but the final answer can be expressed more elegantly as a sum. So, you can see at once the expression for $J_n$ and do not have to compute it by going manually through computing first all $J_1, J_2, ... J_{n-1} $.
Comment: @Martijn My point is that this sum, because it has a variable endpoint, ultimately has a recursive definition: it is a recursion disguised by the familiar summation notation.  In this sense the request in the question to express the answer non-recursively isn't really satisfied: we have only the illusion of satisfying it. If you want to do better than that, you will have to show how to compute that sum without having to add $O(n)$ terms (which you can indeed do, *e.g.* with the digamma function).
Comment: The digamma function is not disguise? Where is the boundary? To me a sum (individual terms), is not closed, but different from an expression like a recurrence relation (related terms). The latter requires that you either come up with a solution for the equation or compute every term in succession. The harmonic number is a solution to the equation. It just happens that that solution, the harmonic number, happens to coincide with every term in the recurrence relation. The switch between a recurrence relation and it's solution as a series is, imo, a meaningful difference and not just disguise.
Comment: The question stated in the youtube video asks 'what is the explicit formula, (not the recurrence relation)?'. It becomes a bit arguing over semantics and it is ambiguous what type of mathematical equation an explicit formula may refer to. But I guess that the intention of the question is to not end with recurrence relations like $J_n = 1 + \frac{1}{n} \sum_{k=0}^{n-1}J_k$ but at least turn it into the simpler first order recurrence relation $J_n = J_{n-1} + \frac{1}{n}$.
Comment: Polettix and @Martijn, I am only pointing out that whatever you might think of summation, when you look at its underlying mathematical definition it is recursive.  There is a large literature on this in mathematics and computer science.  The summation is not a "solution" but is only one conventional way of expressing the recursion.  If you think it's an "explicit formula," then I invite you to compute it for, say, $n=10^{20},$ to see just how non-explicit it really is, and compare that to the digamma calculation.
Comment: @whuber Is there a closed form expression for the digamma function that can be used to perform the computation? Or is it an approximation?
Comment: @Martijn It all depends on what you mean by "closed" and "approximation." For instance, how do you compute exponentials, logarithms, or trig functions? Apart from some special values, all such calculations are carried out numerically using power series, continued fractions, numerical searches, or whatever.  One advantage is that often such procedures (a) can be made arbitrarily accurate while (b) incurring computational costs that depend only on the accuracy and not on the magnitude of the argument. The digamma function has this property; a summation of arbitrary length does not.
Comment: @whuber I agree that closed form is arbitrary. It depends on the accepted set of functions. You could always define a new function to "close" the equation. I believe the issue for this question is more to not end up with a recurrence relation (that you do not write the expactation value for one case in terms of the expectation values of other cases). Indeed the summation formula that one will end up with has a recursive nature and as $n$ gets bigger you need more terms, but it remains a closed form algebraic expression. The end result is a finite number of known terms and expressions.
Comment: @Martijn There I believe your characterization may be a little misleading, because although the number of terms is finite, it can be arbitrarily large: this not only violates the stated spirit of the question, but also has important implications for computation and analysis. I'm suggesting there is value in recognizing this fact both conceptually and practically. As far as the present thread is concerned, one might argue that the implicit recursion in the sum is simpler than the recursion initially used to obtain it; regardless, banning "recursive expressions" looks artificial and useless.
Comment: I agree that the usefulness of a closed expression when it contains arbitrarily long terms is questionable. And an expression in terms of special functions or a limit might be more useful when it comes to computation. However, to me the spirit seems to be that one finds an explicit formula, and it was stressed that this should not be a recurrence relation. To me that 'explicit' means that one should rewrite any result containing multiple unknown terms such that you end up with a single, isolated, unknown term (explicit contrasting with implicit). $\sum_1^n 1/k$ is not implicit, it is not wrong
Answer: This is an interesting problem, and polettix gives the solution for the immediate problem of finding the expected number of jumps.  I'm going to try looking at the broader issue of the distribution of the time it takes to get to the last lily-pad.  This broader analysis allows us to find the probabilities of any state, and any of the moments of the distribution.
This analysis can be framed as a problem of finding the distribution of the "hitting time" for the absorbing state of a discrete Markov chain.  It is relatively simple to program this Markov chain in statistical software and extract the resulting distribution of the hitting times, thus giving a complete solution to the Frog Problem.

Setting up the problem as a Markov chain: To set up the problem, we use states $x = 0,1,2,...,n$, where state $x=0$ is the frog on the river-bank, and the remaining states are for the frog being on lily-pads $1,...,n$ respectively.  We let $\{ X_t | t =0,1,2,3,... \}$ be the random process in the problem, with the frog being at lily-pad $X_t$ immediately after jump $t$.  The process is a strictly monotone discrete Markov chain with the $(n+1) \times (n+1)$ transition probability matrix:
$$\mathbf{P} = \begin{bmatrix}
0 & 1/n & 1/n & \cdots & 1/n & 1/n & 1/n \\
0 & 0 & 1/(n-1) & \cdots & 1/(n-1) & 1/(n-1) & 1/(n-1) \\
0 & 0 & 0 & \cdots & 1/(n-2) & 1/(n-2) & 1/(n-2) \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \vdots \\
0 & 0 & 0 & \cdots & 0 & 1/2 & 1/2 \\
0 & 0 & 0 & \cdots & 0 & 0 & 1 \\
0 & 0 & 0 & \cdots & 0 & 0 & 1 \\
\end{bmatrix}.$$
The number of jumps to the last lily-pad is the hitting time for state $n$, which is:
$$T \equiv \min \{ t \in \mathbb{N} | X_t = n \}.$$
Our goal will be to find the probability mass function for the random variable $T$, which provides a full solution to the frog problem (i.e., it fully describes the behaviour of the number of jumps to the last lily-pad).

Finding the probability mass function: Since the frog progresses by at least one lily-pad at each jump, it takes at most $n$ jumps to reach the last lily-pad, so we must have $1 \leqslant T \leqslant n$.  The cumulative distribution function for this time is:
$$F_T(t) = \mathbb{P}(T \leqslant t)
= \mathbb{P}(X_t = n)
= [\mathbf{P}^t]_{0,n}.$$
Thus, the probability mass function for the time is:
$$p_T(t) = \begin{cases}
1/n & & \text{for } t = 1, \\[6pt]
[\mathbf{P}^t]_{0,n} - [\mathbf{P}^{t-1}]_{0,n} & & \text{for } t > 1. \\[6pt]
\end{cases}$$
This mass function fully describes the distribution of the time taken for the frog to reach the last lily-pad, and so it can be considered to be a complete solution to the Frog problem.  To facilitate computation, we can program this distribution in R as the dfrog function.  This is a vectorised function that generates values from the probability mass function for an argument vector T and parameter n.
dfrog <- function(n, T = 1:n) {

#Create transition probability matrix
P <- matrix(0, nrow = n+1, ncol = n+1);
for (i in 1:n) {
for (j in i:n) {
    P[i, j+1] <- 1/(n+1-i);  } }
P[n+1, n+1] <- 1;

#Generate CDF and PMF vectors
PP  <- P;
CDF <- rep(0, n);
for (t in 1:n) {
    CDF[t] <- PP[1, n+1];
    PP <- PP %*% P; }
PMF <- diff(c(0, CDF));

#Generate output vector
OUT <- rep(0, length(T));
for (i in 1:length(T)) { OUT[i] <- PMF[T[i]]; }

OUT; }

We can use this function to generate and plot the probability mass function.  The plot below shows the distribution of the number of jumps when there are $n=20$ lily-pads.  As can be seen, the frog will usually take 3-4 jumps to reach the last lily-pad in this case.

#Load ggplot and set theme
library(ggplot2);
THEME <- theme(plot.title    = element_text(hjust = 0.5, size = 14, face = 'bold'),
               plot.subtitle = element_text(hjust = 0.5, face = 'bold'));

#Plot the PMF
n    <- 20;
DATA <- data.frame(Jumps = 1:n, Probability = dfrog(n));
ggplot(aes(x = Jumps, y = Probability), data = DATA) +
    geom_bar(stat = 'identity', fill = 'darkgreen') +
    THEME +
    ggtitle('PMF of number of jumps to last lily-pad') +
    labs(subtitle = paste0('(Frog problem with n = ', n, ' lily-pads)'));

Answer:
Instead of using the recursive relation for the expected number  $J_n = J_{n-1} + \frac{1}{n}$ we could also try a more mechanistic approach by computing every path that the frog can take and the distribution of the probability of the position of the frog after $k$ jumps.
This can be quickly computed using a Markov chain.
# stochastic Matrix
M <- pracma::Toeplitz(c(0,rep(1,10)),rep(0,11)) / c(1,1:10)
M[1,1] <- 1

# positions of frogs after k steps
V <- c(rep(0,10),1)
Vm <- sapply(0:10, FUN = function(k) V %*% (M %^% k))

# mean number of steps by computing 1-F(0)
sum(1-Vm[1,])

giving $2.928968$
The mass distribution, $p(x,k)$, for the probability to be at distance $x$ from the 'finish-leaf' in the $k$-th step would look like the following:

This method has one downside. It is not very easy to derive the final charming result that the expectation value for the number of steps is equal to the n-th harmonic number $\sum_{k=1}^n 1/k$.
In the comments I suggested that these distributions $p(x,k)$ would be like polynomial functions. However that is wrong. It is more complicated.
The distribution follows the relation:
$$p(x,k) = \sum_{y=x+1}^N \frac{p(y,k-1)}{j}$$
where $p(x,k)$ is a sum of the probabilities for the position of the frog in the $(k-1)$-th step, and $N$ is the number of leaves (generalizing from $N=10$). To start this relation we use $p(N,0)=1$.
This could be expanded as
$$p(x,k) = \frac{1}{N} \sum_{l_1=x+1}^{N-k} \sum_{l_2=l_1+1}^{N-k+1} ... \sum_{l_k=l_{k-1}+1}^{N-1} \frac{1}{l_1 \cdot l_2 \cdot ... l_k}$$
which is some sort of generalization of the Harmonic number.
You could describe it more compact as
$$p(x,k) =  \frac{1}{N} \sum_{S \in S_{k,[x,...,N-1]}} \prod_{a \in S}  \frac{1}{a}$$
where the sum is over all k-subsets $S$ in $S_{k,[x,...,N-1]}$, the set of all k-subset of $[x,...,N-1]$, and the product is over all the numbers $a$ in the subset $S$. For instance a subset $\lbrace 3,5,7 \rbrace$ would represent that the frog jumped from position 10 to 7 to 5 and to 3. The probability for the frog to follow this path is $\frac{1}{10 \cdot 7 \cdot 5 \cdot 3}$.
I am not sure yet how to continue from here to obtain the final result... I imagine you could use some recursive relation.
Answer: Like Martijn Weterings, I tried the "compute all possibilities" approach.
At the start, the frog has $n$ choices each with $\frac{1}{n}$ probability. After that, the choices remaining depend on the initial choice. But the set of the probabilities of the remaining steps is easy enough to see: it's the reciprocals of the Power Set on $\{1,...,n-1\}$.
That is, for $n=3$, the probabilities of each step are (in reciprocal):
{ 3 } -- one jump of 3
{ 3, 1 } -- jump of 2 (with a 1/3 probability) then a jump of 1 (with 1/1 probability)
{ 3, 2 } -- 1 then 2
{ 3, 2, 1 } -- 1 then 1 then 1
The expected value of these is just the size of the set divided by the product of the elements of the set.
Since each set always starts with $n$, we move it out of the summation.
The expected number of jumps to get across to the nth leaf is:
$$ \frac{1}{n} \sum_{\textbf{x}\in{\mathbb{P}(\{1,...,n-1\})}} \frac{|\textbf{x}|+1}{\prod \textbf{x}} $$
I'm not sure what approach can be used to simply this form into the $$\sum_{k=1}^n \frac{1}{k}$$ form, but the equivalence of the two checks for the $n$ I've tried (2,3,10,20)


Posição de texto em Tkinter

Question: Olá. Eu estou a trabalhar com tkinter e estou num impasse. Tenho nove botões e cada um imprime um número. Como posso fazer com que depois de carregar num botão, independentemente do botão que eu carregue, ele faça aparecer um número sempre na mesma posição e que quando carrego outro botão, também independentemente do botão que eu carregue, faça aparecer outro número ao lado do primeiro?
Código:
<code># -*- coding: cp1252 -*-

from Tkinter import *
root = Tk()
root.minsize(620, 500)
root.title("Euromilhões virtual")
root.configure(background = 'blue')

def sorteio():
    import time
    import random
    numeros = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
    randw = random.sample(numeros, 1)
    randx = random.sample(numeros, 1)
    randy = random.sample(numeros, 1)
    randz = random.sample(numeros, 1)
    randt = random.sample(numeros, 1)
    mostrar = Label(root, text = randw, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 110, y = 250)
    mostra = Label(root, text = randx, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 190, y = 250)
    amostra = Label(root, text = randy, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 270, y = 250)
    show = Label(root, text = randz, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 350, y = 250)
    visto = Label(root, text = randt, bg = "blue", fg = "black", font = ('arial black', 40)).place(x = 430, y = 250)

#Botões dos números:
def um():
    one = Label(root, text = "1", bg = "blue", font = ('arial', 20))
def dois():
    two = Label(root, text = "2", bg = "blue", font = ('arial', 20))
def tres():
    three = Label(root, text = "3", bg = "blue", font = ('arial', 20))
def quatro():
    four = Label(root, text = "4", bg = "blue", font = ('arial', 20))
def cinco():
    five = Label(root, text = "5", bg = "blue", font = ('arial', 20))
def seis():
    six = Label(root, text = "6", bg = "blue", font = ('arial', 20))
def sete():
    seven = Label(root, text = "7", bg = "blue", font = ('arial', 20))
def oito():
    eight = Label(root, text = "8", bg = "blue", font = ('arial', 20))
def nove():
    nine = Label(root, text = "9", bg = "blue", font = ('arial', 20))

btn_um = Button(root, width = 5, text = "1", fg = "red", command = um).place(x = 90, y = 12)
btn_dois = Button(root, width = 5, text = "2", fg = "red", command = dois).place(x = 140, y = 12)
btn_tres = Button(root, width = 5, text = "3", fg = "red", command = tres).place(x = 190, y = 12)
btn_quatro = Button(root, width = 5, text = "4", fg = "red", command = quatro).place(x = 240, y = 12)
btn_cinco = Button(root, width = 5, text = "5", fg = "red", command = cinco).place(x = 290, y = 12)
btn_seis = Button(root, width = 5, text = "6", fg = "red", command = seis).place(x = 340, y = 12)
btn_sete = Button(root, width = 5, text = "7", fg = "red", command = sete).place(x = 390, y = 12)
btn_oito = Button(root, width = 5, text = "8", fg = "red", command = oito).place(x = 440, y = 12)
btn_nove = Button(root, width = 5, text = "9", fg = "red", command = nove).place(x = 490, y = 12)
#-----------------------------------------------------

nums_aposta = Label(root, text = "Números escolhidos:", bg = "blue", fg = "black").place(x = 250, y = 40)

botao_lotaria = Button(root, width = 20, height = 2, text = "Iniciar Lotaria", bg = "yellow", command = sorteio).place(x = 235, y = 150)
root.mainloop()
</code>
Answer: Pelo que entendi, você precisa de 5 labels para exibir 5 números lado a lado. Se for isso mesmo, em vez de criar 9 labels, um para cada botão, você pode criar 5 sem qualquer definição de texto, mas associado à uma StringVar. Enquanto os valores das StringVar não forem definidos, nada é exibido. Abaixo segue um exemplo bem grosseiro (sem layout) de como a coisa funciona:
<code>from Tkinter import *

cont = 1
lim = 5

def insereNumero(valor):
    global cont
    global lim
    if cont <= lim:
        eval('num' + str(cont) + '.set(' + valor + ')')
        cont += 1
    else:
        num1.set("Chega!!!")
        num2.set("Chega!!!")
        num3.set("Chega!!!")
        num4.set("Chega!!!")
        num5.set("Chega!!!")

gui = Tk()

gui.title("Teste Python+Tkinter")
gui.geometry('300x400')

num1 = StringVar()
num2 = StringVar()
num3 = StringVar()
num4 = StringVar()
num5 = StringVar()

btn1 = Button(gui, text="1", command= lambda: insereNumero('1')).pack()
btn2 = Button(gui, text="2", command= lambda: insereNumero('2')).pack()
btn3 = Button(gui, text="3", command= lambda: insereNumero('3')).pack()
btn4 = Button(gui, text="4", command= lambda: insereNumero('4')).pack()
btn5 = Button(gui, text="5", command= lambda: insereNumero('5')).pack()
btn6 = Button(gui, text="6", command= lambda: insereNumero('6')).pack()
btn7 = Button(gui, text="7", command= lambda: insereNumero('7')).pack()
btn8 = Button(gui, text="8", command= lambda: insereNumero('8')).pack()
btn9 = Button(gui, text="9", command= lambda: insereNumero('9')).pack()

label1 = Label(gui, textvariable=num1).pack()
label2 = Label(gui, textvariable=num2).pack()
label3 = Label(gui, textvariable=num3).pack()
label4 = Label(gui, textvariable=num4).pack()
label5 = Label(gui, textvariable=num5).pack()

gui.mainloop()
</code>

Posição de texto em Tkinter


Influential residual vs. outlier

Question: First, I should state that I have searched on this site for the answer.  I either didn't find a question that answered my question or my knowledge level is so low I didn't realize I already read the answer.
I am studying for the AP Statistics Exam.  I have to learn linear regression and one of the topics is residuals.  I have a copy of Introduction to Statistics and Data Analysis on page 253 it states.

Unusual points in a bivariate data set are those that fall away from most of the other points in the scatterplot in either the $x$ direction or the $y$ direction
An observation is potentially an influential observation if it has an $x$ value that is far away from the rest of the data (separated from the rest of the data in the $x$ direction). To determine if the observation is in fact influential, we assess whether removal of this observation has a large impact on the value of the slope or intercept of the least-square line.
An observation is an outlier if it has a large residual. Outlier observation fall far away from the least-square line in the $y$ direction.

Stattreck.com states four methods to determine an outlier from residuals:

Data points that diverge in a big way from the overall pattern are called outliers. There are four ways that a data point might be considered an outlier.

It could have an extreme X value compared to other data points.
It could have an extreme Y value compared to other data points.
It could have extreme X and Y values.
It might be distant from the rest of the data, even without extreme X or Y values.

These two sources seem to conflict each other. Could anyone help clear up my confusion. Also, how does one define extreme. The AP Statistics uses the rule if the data point is outside of (Q1-1.5IQR,Q3+1.5IQR) the it is an outlier.  I don't know how to apply that from just a graph off the residuals.
Answer: The stattrek site seems to have a much better description of outliers and influential points than your textbook but you've only quoted a short passage that may be misleading.  I don't have that particular book so I cannot examine it in context.  Keep in mind though, that the textbook passage you quoted says, "potentially".  It's not exclusive either.  Keeping those points in mind, stattrek and your book don't necessarily disagree.  But it does appear that your book is misleading in the sense that it implies (from this short passage) that the only difference between outliers and influential points is whether they deviate on x or y axis.  That is incorrect.
The "rule" for outliers varies depending on context.  The rule you cite is just a rule of thumb and yes, not really designed for regression.  There are a few ways to use it.  It might be easier to visualize if you imagine multiple y-values at each x and examining the residuals.  Typical textbook regression examples are too simple to see how that outlier rule might work, and in most real cases it is quite useless.  Hopefully, in real life, you collect much more data.  If it's necessary that you may be applying the quantile rule for outliers to a regression problem then they should be providing data for which it is appropriate.
Comment: Actually, I stated it wrong as well... it depends on theory, method, and data... the entire study.
Comment: Thanks for the answer, it just gets annoying that different books try to state these rules without really saying it honestly depends on the data, as you are saying.
Answer: I agree with John. Here are a few more points. An influential observation is (strictly) one that influences the parameter estimates. A small deviation in the Y value gives a big change in the estimated beta parameter(s). In simple regression of 1 variable against another, influential variables are precisely those whose X value is distant from the mean of the X's. In multiple regression (several independent variables), the situation is more complex. You have to look at the diagonal of the so called hat matrix $X(X'X)^{-1}X'$, and regression software will give you this. Google "leverage".
Influence is a function of the design points (the X values), as your textbook states.
Note that influence is power. In a designed experiment, you want influential X values, assuming you can measure the corresponding Y value accurately. You get more bang for the buck that way.
To me, an outlier is basically a mistake - that is, an observation that does not follow the same model as the rest of the data. This may occur because of a data collection error, or because that particular subject was unusual in some way.
I don't much like stattrek's definition of an outlier for several reasons. Regression is not symmetric in Y and X. Y is modelled as a random variable and the X's are assumed to be fixed and known. Weirdness in the Y's is not the same as weirdness in the X's. Influence and outliership mean different things. Influence, in multiple regression, is not detected by looking at residual plots. A good description of outliers and influence for the single variable case should set you up to understand the multiple case as well.
I dislike your textbook even more, for the reasons given by John.
Bottom line, influential outliers are dangerous. They need to be examined closely and dealt with.
Comment: I don't know anything about the AP statistics exam.  True experiments are ones where you manipulate the predictor variable and make groups to test multiple hypothesis or control and experimental groups, etc.  Quasi-experimental designs are pretty much anything else that looks like an experiment.  So, imagine a regression where the x value is weight and the y value is some sport skill.  You don't manipulate either variable, you randomly sample both.  So, Placidia's criticisms of stattrek are quite valid for true experiments but not as much so for quasi.
Comment: Your dislike of the stattrek regression explanation is appropriate if you come from a background where true experiments are the norm.  Your reasons all apply there.  But if you come from a background where quasi-experimental designs are more common then the stattrek site has more relevance.  In those cases both x and y values are often just random samples.
Comment: @John how about the background of wanting to pass the AP Statistics Exam?  What is quasi-experimental design?  Is that using a random number table for a simulation?
Comment: @John ... I do come from a background where designed experiments are seen as the gold standard. In practice, I know that X and Y are often both random samples, which begs the question of why regression is being used, and not some form of latent variable analysis.
Comment: When you've only got two variables... :)  Sometimes you have good theory to suggest one thing predicts another, for example, height and probability of getting into the NBA... both random samples.  In cases with one, or a few (especially uncorrelated) linear relationships regression is good.
Comment: @Placidia what is latent variable analysis.
Comment: That's a pretty easy Google MaoYiyi
Comment: @john I understand what both of you have written better than what I have been reading by googling terms. I just got hungry reading about Chinese restaurant process.

Influential residual vs. outlier


Creating a view with a dynamic link from taxonomy term

Question: Trying to create a view block with a list of five links

Event Details (dynamic)
Cause (dyanmic)
Sponsors (statis)
Volunteer (static)
Register (static)

Dynamic url will look like this:
/events/golf-tournament/[year]/[taxonomy-term]/event-details
/events/golf-tournament/[year]/[taxonomy-term]/cause
I am not able to format the taxonomy term to use it in the url

Taxonomy term prints as: The William Dennis Memorial Golf Tournament (with capitals and spaces)
View looks like this:

** Solution **
*  Not sure if this is the 'right' way *
Added contextual filter for: Content: has taxonomy term ID
But it doesn't appear to use the taxonomy term....so really I providing a value from the url

Then I messed around with tokens, first trying [token_taxonomy_term] <--- not actual token; %1 and !1 <---- all of these exist in the available Replacement Patterns

Here is the rewrite I used to rewrite the url for 'cause'

Cause
Comment: What happens if you try to use one of the validation filters offered by the contextual link to replace spaces with dashes?
Comment: When I add contextual filters all contents of the view disappear.
Comment: What contextual filters are you adding and how are they configured?
Comment: *configuration added to issue*
Comment: What happens if you enter the term in the preview field on the view instead of the default field on the contextual filter? Also, which type of contextual filter is this?
Comment: Type == Content: Has taxonomy term ID
Comment: I'm not able to get anything to work when I enter the contextual filter in the preview
Answer: In the contextual filters you may have to add the Context: Has taxonomy term ID filter.  You can set the Specify validation criteria settings.  The validator option is usually Taxonomy Term, but other validator options are available. Make sure to choose Term Name converted to Term ID, as this is what is needed to be passed for to the url.  Also check the Transform dashes in URL to spaces in term name filter values checkbox underneath.
Choose your action to take if the filter value does not validate dropdown.


How to use Semantic UI's theming in Semantic UI React?

Question: There are detailed instructions on theming on the traditional Semantic UI, but the equivalent section is missing from the Semantic UI React site. Does that mean that it doesn't support theming or can you possibly use the traditional Semantic UI's approach to theming?
I know that some folks in the React world advocate for not using CSS and instead doing you styling programmatically as part of your component design. What's the Semantic UI React philosophy on that?
Answer: The theming part is "missing" in the react semantic ui docs, but thats on purpose.
Thats the way I did it:

Install the full Semantic UI package. Semantic UI includes Gulp build
  tools so your project can preserve its own theme changes, allowing you
  to customise the style variables.
Detailed documentation on theming in Semantic UI is provided here.
<code>$ npm install semantic-ui --save-dev
</code>
After building the project with Gulp, you'll need to include the
  minified CSS file in your index.js file:
<code>import '../semantic/dist/semantic.min.css';
</code>

via: https://react.semantic-ui.com/usage#semantic-ui-package
There are already different ui-frameworks with a more "react" approach. IMHO what semantic ui (react) does is the best real-live solution so far...
Comment: Is this still the correct way to use custom SUI theme in Semantic UI React? I am using CRA. After `npm install semantic-ui --save-dev` it asks for some options and when I try to do `import '../semantic/dist/semantic.min.css';` I get an error that it cannot import files from outside the `src` folder.
Comment: @limitlessriver link to docs still works. but the command to install (especially the flag) changed a little. now it's `yarn add semantic-ui --dev`. Docs are still pretty sweet. Check them out: https://semantic-ui.com/usage/theming.html
Comment: Where are the themes?  my project imports the css, but the only one in there is default.
Answer: One of the questions is "Where should we put Semantic UI inside your project?" and you should put src/semantic which overwrites the default.


Fastest way to list the subgroup which has rank $5$ in $\Bbb Z_{30}$?

Question: What is the probably fastest way to list the subgroup of rank $5$ in $\Bbb Z_{30}$? We know that $\Bbb Z_{30}$ is cyclic, and $5\mid 30$, hence $\Bbb Z_{30}$ has exactly one subgroup $H$ such that $|H|=5$. However, there're several thoughts to be used to list the all elements of $H$. A method I come up in mind is that: since $\langle \overline{1}\rangle=\Bbb Z_{30}$, then $|\langle \overline{1}\rangle^i|=\frac{30}{\gcd(i,30)}=5$. We want $\gcd(i,30)=6$. And we know that $i=6$ is a solution. So $H=\{\overline{0},~\overline{6},\cdots\}$ and so on. However, is there a more elegant way or the probably fastest way? I feel my solution is a bit ugly.
Answer: $H$ being a subgroup of a cyclic group has to be cyclic, and let $a$ be the generator of $H$, then $5a=0$, so the obvious choice for $a$ is $30/5=6$. and $$H=\{0,6,12,18,24\}$$
Comment: yes, the operation on the group $\Bbb{Z}_{30}$ is itself defined as addition modulo $30$, that is why i only wrote $5a=0$
Comment: $5a=0$ do you mean $5a\equiv 0\mod 30$?


Examining a solution of a differential equation without knowing the solution

Question: The differential equation is given by $$\dot x=-x \cos x$$ with $x(0)=x_0\in(0,\frac{\pi}{2})$.
Now I need to show that for each choice of $x_0$ the domain of the solution $x: I\rightarrow \mathbb{R}$  is $\mathbb{R}$, so $I=\mathbb{R}$.
In addition to that I need to show that $x$ is strictly monotonically decreasing and converges for $t\to-\infty$ and $t\to\infty$ respectively to $\frac{\pi}{2}$ and $0$ respectively.
The hint is not to find the explicit solution of the differential equation but to examine the function $x$ qualitativly.
By using the help below I managed to show all these statements about $x$ (little questions remain).
But if there aren't any restrictions for $x_0$, what can I say about $x$ and its maximality?
I think there are six cases (apart from the case above):
$1)~x_0\in(-\frac{\pi}{2},0)\Rightarrow x<0 \wedge \cos(x)>0 \Rightarrow \dot x>0$
$2)~x_0\in(-\frac{(4k-1)\pi}{2},-\frac{(4k-3)\pi}{2}),k\in\mathbb{N}_{\geq1}\Rightarrow x<0 \wedge \cos(x)<0\Rightarrow \dot x<0 $
$3)~x_0\in(-\frac{(4k+1)\pi}{2},-\frac{(4k-1)\pi}{2}),k\in\mathbb{N}_{\geq1}\Rightarrow x<0 \wedge \cos(x)>0 \Rightarrow \dot x>0$
$4)~x_0\in(\frac{(4k-3)\pi}{2},\frac{(4k-1)\pi}{2}),k\in\mathbb{N}_{\geq1}\Rightarrow x>0 \wedge \cos(x)<0\Rightarrow \dot x>0  $
$5)~x_0\in(\frac{(4k-1)\pi}{2},\frac{(4k+1)\pi}{2}),k\in\mathbb{N}_{\geq1}\Rightarrow x>0 \wedge \cos(x)>0 \Rightarrow \dot x<0 $
$6)~x_0=\frac{(2k-1)\pi}{2}, k\in\mathbb{Z}$ or $x_0=0\Rightarrow \dot x\equiv 0 \Rightarrow x\equiv x_0$.
Is it possible to get the same properties in these six cases as in the case above ($I=\mathbb{R}$ and the convergence of $x$ to the limits of the interval in which $x_0$ lies)?
Comment: For me, for these problems it REALLY helps to graph the RHS.
Comment: You may try to prove that $x(t)\in(0,\pi/2)$ for all $t$ in the maximal interval of existence of $x$. Then, it will yield $I=\mathbb{R}$ and $x$ is strictly decreasing.
Answer: The equation satisfies the conditions that guarantee existence and uniqueness of solution. $x(t)=\pi/2$ and $x(t)=0$ are constant solutions. If $x(0)\in(0,\pi/2)$, by uniqueness of solution it must be that
$$
0<x(t)<\frac{\pi}{2}
$$
for all $t$ for which thwe solution is defined. This implies that the solution is global, that is, it is defined on $(-\infty,\infty)$.
Comment: May as well add that $x=0$ is also a constant solution, which proves the limits.
Comment: Right. I just thought of the zeros of $\cos x$. Will modify the answer.
Comment: How do I see that $0<x(t)<\frac{\pi}{2}$ implies that the solution is global? Could you give me a hint? @Julián Aguirre
Comment: Local existence theorem guarantees a solution on some interval $I$. Put $\tau = \sup I$, the solution can be extended to $\tau$ (if not already defined there) by taking limit. If the solution is bounded, again by the local existence theorem the solution can be extended beyond $\tau$ (with $x(\tau)$ being the new initial condition). Now suppose a solution on $I$ cannot be extended beyond $I$. Then by the arguments above it cannot be bounded.
Comment: The solution either is global or blows up in finite time. Since it remains bounded on any interval of existence, it cannot blow up.


If url hash does not contain any string, execute function

Question: I am writing a script to change which element is visible based on string in hash.  The script will go on a blog page where the user can filter blog posts by category.  When the user filters to a certain category, that category is appended to the url in the form of a hash link.
example.com/#categoryA

At that point, I want a text box corresponding to each set of filtered posts to appear alongside the portfolio.  However, if the user has not filtered the posts, or goes from filtering posts to viewing all posts, there is a single hash in the url like this:
example.com/#

In this case, I want a default text box to be shown.
My script (below) does everything except show the default text box when the url ends in just a hash.
var frag = window.location.href.split("#");
var hashClassChange = function() {
  if (window.location.hash) {
    //If the url hash contains commercial, show the commercial box, hide the last active box
    if (window.location.hash.indexOf('commercial') == 1) {
      $('#box1').addClass("active");
      $('#default').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
      $('#box4').removeClass("active");
    }
    //If the url hash contains hospitality, show the hospitality box, hide the last active box
    else if (window.location.hash.indexOf('hospitality') == 1) {
      $('#box2').addClass("active");
      $('#default').removeClass("active");;
      $('#box1').removeClass("active");
      $('#box3').removeClass("active");
      $('#box4').removeClass("active");
    }
    //If the url hash contains municipal-institutional, show the municipal / institutional box, hide the last active box
    else if (window.location.hash.indexOf('municipal-institutional') == 1) {
      $('#box3').addClass("active");
      $('#default').removeClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box4').removeClass("active");
    }
    //If the url hash contains residential, show the residential box, hide the last active box
    else if (window.location.hash.indexOf('residential') == 1) {
      $('#box4').addClass("active");
      $('#default').removeClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
    }
    //If the url hash does not contain any string, show the default box
    else if (!frag[1].length) {
      $('#default').addClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
      $('#box3').removeClass("active");
    }
  }
};
// repeats function every 500 milliseconds to check if the url hash has been changed
setInterval(hashClassChange, 500);

How do I fix this script so that the default text box shows when the url ends in a single hash?
I have a codepen set up (http://codepen.io/ben393/pen/GNVqRX) to show this in action.
Comment: Not an answer but you can use `window.location.hash` which will give the hash in the form "#hospitality". Then, you should create a switch statement ( or if else ) with exact matches. Using indexOf like you are doing now may give false positives.
Answer: The issue is because when you click on default window.location.hash will be an empty string so it will not go inside first if in result the last clicked one will be visible.
remove   if (window.location.hash) {} part and it will work as you are expecting.

var frag;
frag = window.location.href.split("#");
var hashClassChange = function() {

    if (window.location.hash.indexOf('commercial') == 1) {
      $('#box1').addClass("active");
      $('#default').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
      $('#box4').removeClass("active");
    }
    else if (window.location.hash.indexOf('hospitality') == 1) {
      $('#box2').addClass("active");
      $('#default').removeClass("active");;
      $('#box1').removeClass("active");
      $('#box3').removeClass("active");
      $('#box4').removeClass("active");
    }

    else if (window.location.hash.indexOf('municipal-institutional') == 1) {
      $('#box3').addClass("active");
      $('#default').removeClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box4').removeClass("active");
    }
    else if (window.location.hash.indexOf('residential') == 1) {
      $('#box4').addClass("active");
      $('#default').removeClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
    }
    else if (typeof frag[1] == 'undefined' || !frag[1].length) {
      $('#default').addClass("active");
      $('#box1').removeClass("active");
      $('#box2').removeClass("active");
      $('#box3').removeClass("active");
      $('#box3').removeClass("active");
    }
};

setInterval(hashClassChange, 500);
body{
  font-family:sans-serif;
}
#default{
  background:red;
}
#box1{
  background:orange;
}
#box2{
  background:yellow;
}
#box3{
  background:green;
}
#box4{
  background:lightblue;
}
li {
    display: inline-block;
    width: 400px;
    padding: 10px;
    position: absolute;
    opacity: 0;
    transition: opacity 1s;
}
li.active{
  opacity: 1;
}

Links to filter posts:
no category (show all posts) |
Commercial |
Hospitality |
Municipal / Institutional |
Residential

Default text Quisque posuere augue eu vulputate vestibulum. Mauris lacinia diam sit amet magna vulputate sodales et vel magna. Integer eros diam, faucibus nec mi sit amet, suscipit ornare massa. Praesent convallis ut lorem fringilla fringilla. Pellentesque sed nisi sapien. Maecenas sit amet placerat augue. Nullam auctor ligula risus, et laoreet nisl varius ac. Donec euismod erat neque, in viverra sapien luctus eget.

Commercial text Nulla justo quam, sagittis in turpis a, rutrum dapibus tellus. Sed porttitor massa nec urna rutrum finibus. In sit amet sagittis orci. Proin ligula quam, tempus eget ligula nec, gravida venenatis tellus. Quisque vitae nisl a ipsum semper finibus. Proin vehicula molestie arcu, nec facilisis justo tempus id. Morbi lectus dui, luctus non tellus ac, varius aliquam dui. Morbi commodo nulla eget luctus ullamcorper. Nulla facilisi. Fusce dapibus vestibulum sapien, ut laoreet libero pharetra in.

Hospitality text Sed eget nunc lobortis, varius felis ut, scelerisque tortor. Maecenas vitae nunc leo. Mauris non facilisis risus, vel tincidunt enim. Donec pharetra nisi id suscipit consectetur. Integer lacinia in eros at tincidunt. Praesent et enim sem. Sed in viverra erat, vitae tempus felis. Vestibulum id sodales leo.

Municipal / Institutional text Maecenas rutrum diam nec sapien consectetur, a viverra enim commodo. Curabitur a eros risus. Quisque viverra elementum ipsum, sit amet commodo libero tristique non. Nullam blandit interdum dolor non vulputate. Morbi et lacus blandit, blandit ipsum non, mollis eros. Duis sit amet ex sed felis congue vestibulum. Aliquam ultrices viverra est, quis posuere est consectetur vel. Duis viverra magna sed dignissim semper. Pellentesque varius pretium justo, eleifend placerat lectus fringilla non.

Residential text Pellentesque dui enim, pulvinar et dignissim a, ultricies sed sapien. Mauris pulvinar ipsum a est feugiat euismod. Cras sed rutrum lorem. Aenean cursus a augue in laoreet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse non luctus lacus, in dignissim diam. Quisque vitae ipsum ac eros cursus consectetur non id libero. Aenean mauris nisl, sagittis vitae nisi et, sagittis venenatis ligula. Donec libero odio, vulputate et lacinia quis, feugiat quis dui.

Answer: Not sure why you are using this frag variable, it never changes.
You can do the following
if(window.location.hash.length < 2) {
.....
}
Also if there is nothing stopping you, you can use the onhashchange event instead of continuous checking through setInterval
$(window).bind('hashchange', hashClassChange);

Answer: You can check for last char is # or not .
var lastChar = location.pathname.substr(location.pathname.length - 1);
if(lastChar == '#')
 //Your default textbox show hide logic goes here

Comment: I think I changed the script correctly to reflect your suggestion [link](http://codepen.io/ben393/pen/MbNbod), but my script does not work.  Perhaps my logic or placement of variables is bad, because I'm a newbe.
Answer: Could you please try with below code:
//If the url hash does not contain any string, show the default box
else if (frag.length < 1 || (frag.length > 1 && frag[1].length == 0) ) {
    //if we don't have # in URL then frag will have only single 0th index
    //if we have # in URL but nothing after that then its length will be zero
  $('#default').addClass("active");
  $('#box1').removeClass("active");
  $('#box2').removeClass("active");
  $('#box3').removeClass("active");
  $('#box3').removeClass("active");
}

Comment: I have updated my [codepen](http://codepen.io/ben393/pen/Vmomrb) to include your changes, I cannot get that to work.


Drush command to show aliases?

Question: Is there a drush command to show existing aliases? Some Googling didn't turn up anything.
Comment: `$ drush site-alias @self`
Comment: 'drush sa @self --full --with-db --with-optional'
will generate a template that you can append to ~/.drush/aliases.drushrc.php for further use. Do not forget to change the key 'self' in the table by an unique alias name for your site.
Answer: You can use this in the terminal
drush sa

For example on my dev server:
- drush sa
@8080
@commons_3_dev
@none
@w2

The aliases are set in file called aliases.drushrc.php. For me this is located in /usr/local/share/drush/. Depending on how you installed Drush and on which distro you're on this may differ.
You can set up aliases in this fashion inside that file
$aliases['8080'] = array(
'uri'=>'server.com:8080',
'root'=>'/var/www/html_webtest2',
);

$aliases['w2'] = array(
'uri'=>'webtest2.server.com.edu',
'root'=>'/var/www/html_webtest2',
);

$aliases['commons_3_dev'] = array(
'uri'=>'yourserver.com/c3d7/',
'root'=>'/var/www/html_commons_3_dev',
);

For more fun, you can chain these together for server wide backups. For example, I do a big back up to all aliased sites like this
 drush sa | egrep "(@|default)" | egrep -v "(@none|@self) | xargs -L1 drush arb

Taken individually
 drush sa  # list site aliases
 | egrep "(@|default)"  # include default and any line containing a @ mark
 | egrep -v "(@none|@self)" # remove any lines with @none or @self
 | xargs -L1 drush arb # with each line run drush archive-backup

To find path and other info, use a bit o' pipping:
drush sa | xargs -I {}  sh -c "echo {}; drush {} st"

To narrow it down to finding paths:
    drush sa | xargs -I {}  sh -c "echo {}; drush {} st" | egrep "(@|path)"
@8080
 Site path                     :  sites/8080.webtest2
 File directory path           :  sites/8080.webtest2/files
 Private file directory path   :  /var/www/drupal_private_files
@commons_3_dev
 Site path              :  sites/default
 File directory path    :  sites/default/files
@none
@w2
 Site path              :  sites/default
 File directory path    :  sites/default/files

update
A bit late but I wanted to say don't forget about drush @sites st. I believe the @sites parameter will act on everything found under the sites folder. Think
drush @sites cron -y # runs cron on each root/sites/site
drush @sites rf

Comment: Try this: drush sa | xargs -I {}  sh -c "drush {} st"
Comment: Try drush @alias st (for status)
Comment: Get creative with pipes and grep. Sometimes the simplest bash commands strung together will give you some wonderful information.
Comment: It would be handy also to know what filesystem path these sites map to from the `sa` command :)
Comment: yeah, but I don't want to have to check each alias individually to see if it's the one I'm looking for. I'm dealing with a lot of aliases in a multi-site, multi-user environment. The one I want may not even exist!
Comment: For 74 aliases that sure burns through a lot of screen lengths! :D
Answer: If you're like me you want all the details too…
Try drush site-alias --table or the shortcut drush sa --table.
Check GitHub or drush topic aliases for further info.
Comment: I know this is an ancient thread but if anyone lands here via google - the parameter is not --table but `drush sa --format=table`.  And to save you some awk weight lifting you can use the --fields and --field-labels=0 options to keep things concise.
Comment: @DanShumaker I haven't worked in on Drupal in a few years now. Feel free to give a new answer, and I'll upvote yours.

Drush command to show aliases?


Decmial glitches when multiplying

Question: Every time I do something like x186.733 it gives something like 396.328679362906736. How can I avoid?
I tried 1.3*2.68 and it gives 2 extra 0s at the end, its very annoying.
Comment: Answered here: https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript
Comment: `1.3*2.68 and it gives 2 extra 0s at the end` ... no it doesn't ... it gives `0000000000004` at the end - you must be doing something very different
Answer: Use Math.round() to get the nearest one.

Decmial glitches when multiplying


Cello A5 possible?

Question: I am working on a composition of mine. When I transposed an already high section on the Cello to a different key it ended up having an A5 in it.
Is this possible to play for a good Cello player?
Answer: It is easy to play A5 (or, indeed, even A6) on cello as a natural flageolett. However (even if the airy sound of a flageolett is ok for your application), it won't help with any tones merely close to A5. So if that's just the very tip of a melodic line, you'll need to think of a position where also the other notes are feasible as non-flageoletts. The 11th position might be suitable, it has the thumb resting nicely on the third harmonic (A4 on the D string) and the A5 sitting quite reachable for the 3rd finger. This position works well for the keys of G to A; for ♭-keys it's not so nice (for instance the F5 lies uncomfortably close to the thumb).
So – it is certainly possible, though how well it can work out depends on the exact context. In general, please try to think about how passages can be played, not just if it's possible.
Answer: Yes. It is on the very top of the cello's range but it's possible. I would not write a lot that high in their range and if possible I would transpose that part down slightly.
The picture bellow shows you the typical range of the cello:
Comment: @NicolasAntor I agree with Dom that this is starting to be at the highest I would ever regularly write for cello. Actually, I start getting nervous, as a player, when it gets to C5 or D5. However, I've played pieces that even go as high as A6, the context matters a huge amount. If the line is mostly stepwise and takes its time approaching A5, or, alternatively, if I can go straight from A4 to A5 (and thus hear the octave leap as a glide up), then it isn't a particularly big deal at all. If I'm consistently in that range, but jumping all over the place it's extremely difficult.
Comment: Also, as @leftaroundabout points out, it's quite easy as a natural harmonic (normally produced by lightly touching the string at the position of D4 on the A-string).
Comment: Thanks guys for your quick answers. Acctually I wrote the composition for a team which has a pretty skilled Cello Player. I really just wasn't sure if it is physically possible to do so, because Sibelius marked the Note as red ;)
Comment: @NicolasAntor: Sibelius is apparently not to be trusted with deciding what's playable and what's not! Really, the absolute pitch of a single note is almost completely irrelevant to this.

Cello A5 possible?


Will the limit exist?

Question: If $$f(x) = \begin{cases} \frac{\sin([x]+x)}{[x]+x} &\textrm{if } x \neq 0, \\ 1 &\textrm{if } x = 0, \end{cases}$$
where [.] is the greatest integer function, then does
$\lim_{x \to 0}f(x)$ exist or not?

I thought of using the property that  $\lim_{x \to 0} \frac{\sin g(x)}{g(x)}=1$ by which I thought that the limit should exist but my book said otherwise. What did I miss? Is there anything else that should be applied?
Comment: This could be confusing, since $[x]$ is sometimes used to mean rounding towards $0$ (especially in computer science, where this is a common behaviour of standard functions), one reason that I prefer $\lfloor x\rfloor$ instead. This might lead to the impression that $[x]=0$ for $x\approx0$, but that is not correct when $x<0$. Thus the right-sided limit is $\lim\limits_{x\to0^+}\frac{\sin(0+x)}{0+x}=\lim\limits_{x\to0}\frac{\sin x}x=1$, but the left-sided limit is $\lim\limits_{x\to0^-}\frac{\sin(-1+x)}{-1+x}=\frac{\sin(-1)}{-1}=\sin1<1$.
Answer: The left and right limits at $0$ must be equal to $1$ for continuity. But you can see that as $x\to 0^-$, $$\frac{\sin([x] +x)}{[x] + x}= \frac{\sin(-1+x)}{-1+x} \to \sin 1 \ne 1 $$
Answer: What you missed was the fact that the function $x\mapsto\lfloor x\rfloor+x$ is discontinuous.
Not, that, if $x\in(-1,0)$, $\lfloor x\rfloor+x=x-1$ and that therefore$$\lim_{x\to0^-}\frac{\sin(\lfloor x\rfloor+x)}{\lfloor x\rfloor+x}=\frac{\sin(-1)}{-1}=\sin(1).$$But$$\lim_{x\to0^+}\frac{\sin(\lfloor x\rfloor+x)}{\lfloor x\rfloor+x}=1\ne\sin(1).$$
Answer: $\lim_{x \to 0} \frac{\sin g(x)}{g(x)}=1$ is only true if $g(x)$ is continuous and if $\lim_{x\to 0} g(x) = 0$.  Neither of those are true for $g(x) = [x]+x$.
.....
On the intervals $[0,1)$ and $(-1,0)$ then $[x]$ is a constant value.  If $0\le x < 1$ then $[x]= 0$.  And if $-1 < x < 0$ then $[x] = -1$.
$\lim_{x\to 0^+} f(x)$ if it exists will be equal to
$\lim_{x\to 0^+}\frac{\sin([x]+x)}{[x]+x} = \lim_{x\to 0^+}\frac {\sin (0 + x)}{0 + x} =\lim_{x\to 0}\frac {\sin x}x$ which be l'hopital is equal to $\lim_{x\to 0} \frac {\cos x}1 = 1$.
Meanwhile $\lim_{x\to 0^-} f(x)$ if it exists well be equal to
$\lim_{x\to 0^-}\frac{\sin([x]+x)}{[x]+x} = \lim_{x\to 0^-}\frac {\sin (-1 + x)}{-1 + x} =\frac {\sin (-1)}{-1}\ne 1$.

Will the limit exist?


How can Activation function do its task?

Question: I have a simple question. I know that the main purpose of activation function is to convert an input signal of a node to an output signal. And that output signal is gonna used as an input in the next layer. But I dont have any idea about the way that activation function such as sigmoid do this in classification problem.
All I know is about converting.
Could any one pleas clarify this to me?
Thanks!
Comment: I don't understand the question at all, can you clarify?
Comment: I think you have a confusion, because activations don't do tasks, they are just functions (like max(0, x)) used to introduce non-linear behavior in the network. How the output is defined is just the mathematical expression of the activation function.
Comment: Sure. I want to know that how do activation functions do their tasks. How can their define the output of a neuron given an input or set of input.  Or simply explain that what is the role of activatom function. Thanks
Comment: Thank you for your answer. I edited my quesion with a picture of an slide in Andrew Ng machine learning class. As you see there are 3 layers input hidden and output. As you see in the hidden layer we have activation of each unit. I want to know tabout the definition of it and discover why he wrote activation into each neuron
Comment: Sorry but that is not really on-topic here, its not a programming question. Your question belongs to http://stats.stackexchange.com/
Answer: In simple terms the function gets inputs and assigns some weights to the inputs.
Then the activation function calculates the value (eg :- Sigmoid).
Then it compares the value with the threshold value assigned . If its more than the threshold value then it backtracks (back propagation Algorithm). and adjusts the weights. You can find more details at https://en.wikipedia.org/wiki/Backpropagation


Mail Enabled Public Folders routing to offline server Exchange 2010

Question: Exchange 2010 Databases with replicas.
The hubs keep trying to deliver to offline server.
Things Ive tried:
Applied Rollup that supposedly allowed for Smartrouting
Mail Disabled then Re enabled
Both solved the issue but temporarily, then reverted back to reattempting the bad DB. I'm contemplating removing the PFDB from Exchange since it's offline anyway. Then the question is the deletion process for that so that it doesnt touch the replicas on the live servers.
Answer: You need to remove that offline server correctly.
Exchange will always try to use all servers, even ones that are offline. That is how it is designed to work.
Manually removing the server is not a supported action, you either need to start the server up so it can be removed correctly, or if not possible do a recover server installation of Exchange on to a VM and then remove that recovered installation.
Have you removed the server from the list of replicas? If not you should do that. However it will not stop transport from trying to use that other server. Exchange doesn't "know" that it is offline - for the same reason the "turn off and see what breaks" method doesn't work during migrations.
Comment: After removing the server from replicas, since I couldn't resurrect the server (yet) I opted to remove the PFDB via ADSI. It's been 72 hours so far so good.


QSslCertificate crashes when i try to load a cer file

Question: i have this code in Qt
QFile file(address);
file.open(QIODevice::ReadOnly);
const QByteArray bytes = file.readAll();

qDebug()<<"Length : " + QString::number(bytes.length());

const QSslCertificate cert(bytes,QSsl::Der);

when i try this code in windows 7 64bit it works perfectly but when i compile it for linux it crashes every time when it hits the last line even it gave me the right Lenght but it crashes with these lines in terminal.
qt.network.ssl: QSslSocket: cannot resolve TLSv1_1_client_method
qt.network.ssl: QSslSocket: cannot resolve TLSv1_2_client_method
qt.network.ssl: QSslSocket: cannot resolve TLSv1_1_server_method
qt.network.ssl: QSslSocket: cannot resolve TLSv1_2_server_method
qt.network.ssl: QSslSocket: cannot resolve SSL_select_next_proto
qt.network.ssl: QSslSocket: cannot resolve SSL_CTX_set_next_proto_select_cb
qt.network.ssl: QSslSocket: cannot resolve SSL_get0_next_proto_negotiated
qt.network.ssl: QSslSocket: cannot resolve EC_KEY_new_by_curve_name
qt.network.ssl: QSslSocket: cannot resolve EC_KEY_free
Segmentation fault (core dumped)

whats going on here i didn't use any QSslSocket in my code, i'm stuck in this problem for days now.
qt version 5.4.2 tested on Ubuntu 13.10 64bit and CentOS 6.4 64bit
UPDATE
i used openssl-1.0.2d for Windows and Linux both.
Comment: did you figured out this issue? i have the same annoying issue
Comment: @mihaipop sadly no. i'm sort of leaving this issue behind but I'll be glad if someone solve it. although i suspect maybe there is a conflict in openssl and qt. what is your version of qt and openssl?
Answer: I found a solution for this, what I did was:
OpenSSL 1.1.0 is not compatible with QT so i cloned the 1.0.1 stable version:
Here is the bug: https://bugreports.qt.io/browse/QTBUG-52905
git clone https://github.com/openssl/openssl -b OpenSSL_1_0_1-stable

then install it in custom location:
./config --prefix=/opt/openssl-stable
make
make test
sudo make install

Get the QT Sources, then when you want to built them you have to link the installed OpenSSL:
OPENSSL_LIBS='-L/opt/ssl/lib -lssl -lcrypto' ./configure -openssl-linked

see: http://doc.qt.io/qt-5/ssl.html
This is my configuration:
OPENSSL_LIBS='-L/opt/openssl-stable/lib -lssl -lcrypto' ./configure -prefix /opt/qt5.7_linux_staticssl -opensource -confirm-license -release -openssl-linked -I /opt/openssl-stable/include/openssl -L /opt/openssl-stable/lib

and if the qt build gives you an error that it doesn't find some ssl struct, just copy the /opt/openssl-stable/include/openssl headers to /usr/include/openssl or wherever qt is looking for them(you can see that when qt build fails)
And that's it
Comment: Great man! Thought some conflict between openssl and qt cause it. Tnx


Integration of $\sqrt{1-x^2}$ by parts. Interpretation of 2nd term.

Question: I was wondering about the anti-derivative of $\sqrt{1-x^2}$
Method for solving it is given here. This solution is fine, but I was bit confused about last term in first line i.e.
$$\int \left(\frac{d}{dx}\sqrt{1-x^2}\int dx\right)dx$$
$$=\int x\cdot\frac{d}{dx}\sqrt{1-x^2}\cdot\ dx$$
Can't it be written as
$$\int x\cdot\ d\sqrt{1-x^2}$$
i.e. cancelling out both $dx$
Now if we substitute $1-x^2 = t^2$ then the integral would become
$$\int \sqrt{1-t^2}\cdot\ dt$$
Which is integral $I$ itself
This implies
$$I=\int 1\cdot\sqrt{1-x^2}dx=\int dx \sqrt{1-x^2}-\int \left(\frac{d}{dx}\sqrt{1-x^2}\int dx\right)dx$$
$$=x \sqrt{1-x^2}-I$$
Or
$$I=\frac{x}{2}\sqrt{1-x^2}+C$$
I know there is something wrong. Might be cancelling $dx$ was the wrong step. But same result is obtained by this alternate method:
$$I=\int 1\cdot\sqrt{1-x^2}dx=\int dx \sqrt{1-x^2}-\int \left(\frac{d}{dx}\sqrt{1-x^2}\int dx\right)dx$$
$$=x\sqrt{1-x^2}+\int\frac{x^2}{\sqrt{1-x^2}}dx$$
Now in second term again if we put $1-x^2=t^2$ which gives
$$dx=\frac{t}{-\sqrt{1-t^2}}dt$$
And so
$$I=x\sqrt{1-x^2}+\int\frac{1-t^2}{t}\cdot\frac{t}{-\sqrt{1-t^2}}dt$$
$$=x\sqrt{1-x^2}-\int \sqrt{1-t^2}\cdot\ dt$$
$$=x\sqrt{1-x^2}-I$$
Which again give same result
$$I=\frac{x}{2}\sqrt{1-x^2}+C$$
What step am I doing incorrectly? Is there an algebraic mistake? Please help. Thanks in advance. :)
Answer: \begin{align}
I & = \displaystyle\int \sqrt{1-x^2} \\
 & = x\sqrt{1-x^2}+\displaystyle \int \dfrac{x^2}{\sqrt{1-x^2}} \\
 & = x\sqrt{1-x^2}+\displaystyle \int \dfrac{x^2+1-1}{\sqrt{1-x^2}} \\
 & = x\sqrt{1-x^2}+\arcsin\left(x\right)-I \\
 & =\dfrac{x\sqrt{1-x^2}}{2}+\dfrac{\arcsin\left(x\right)}{2}+C
\end{align}
EDIT:
in your first method you were cancelling dx which is wrong and in your so called alternative method you assumed $\sqrt{1-t^2}=I$ which is wrong because you are working with indefinite integral here not definite
so,
you can't say $\displaystyle\int {f(x)} dx=\displaystyle\int {f(t)} dt$ where $t$ is some transformation of $x$
But yes in case it was definite integral then above will stand true because changing  variable i.e, from $x$ to $t$ will also change the bounds (aka limits ) in a way that value of integral(area under curve) remain the same and indefinite integrals lacks those bounds.so both integrals (integrals before change of variable and that after) wouldn't be equal
Comment: now you have completely answered the question! Pretty subtle point, the antiderivative on right is in t so after integration, t needs be replaced with whatever function it is of x. So the two antiderivatives cannot be combined. Good answer!
Comment: Asker is asking for his mistake, though your answer is fine
Comment: @ King Tut : thank you...i have edited :)


finite simple groups and free groups

Question: It is well known that any group is a homomorphic image of a free group.
I want to know more about this theorem when $G$ is a finite simple group.
Does there exist any reference to state about it? Thanks in advance!
Comment: I think the question is too broad or too vague. You need to say more precisely what you would like to know. There is a lot of literature on presentations of the finite simple groups.
Comment: The phrase you should look up is group presentations.
Comment: The kernel is maximal of finite index?... I wonder what happens when you intersect all possible kernels for all such groups...hmm...
Comment: I think this is a fact that "any finite simple group is generated by two elements, which one of them is an involution". I think there is a relationship between this fact and this question. Am I right?
Comment: Dear user1729, is it possible to explain your mean more? thanks!
Answer: Based on the comment you added to your question, one relationship is that any group that is generated by $k$ elements is a homomorphic image of a free group of rank $k$. This is an immediate consequence of the universal property for free groups.

finite simple groups and free groups


Convert Azure Blob object to MP3

Question: I'm actually retrieving audio files from Azure Blob Storage and I'd like to convert them back to the original format (mp3 format).
I'm retrieving the audio using Pyspark in Databricks as shown in the following code:
input_audio = spark.read.format("binaryFile").load("test.mp3")

the input_audio is a PySpark Dataframe, I'd like to convert it to mp3.
How can I achieve it ? Thanks!
I've tried to write the content (Binary data) into a new .mp3 file but it didn't work.
Comment: why not just read file as binary using the "normal" file API?
Comment: Actually I'm reading it without a problem but as a Binary file and I want to convert it to mp3, do you have any suggestions to convert it back to mp3 ? Thanks
Comment: Do you just want to do file actions (copying, moving etc.) with the mp3 or actually do some sort of pyspark transformations on the mp3 data?
Comment: @restlessmodem Actually I'd like to convert it into .wav so I'd like to restore it as mp3 then convert it to .wav. So, it's not really Pyspark
Comment: @manou.MHD pyspark is not really the right tool for this, with pyspark you can work great with structured data like CSVs. Maybe look for a library that is specialized on audio file conversion
Answer: I've fixed my problem :
First I've used Pyspark to get the content of the audio file
input_audio = spark.read.format("binaryFile").load("test.mp3")
# extracting audio binary content (there's several methods to extract column value)
binary_data = input_audio.collect()[0][3]

Then I've just written the binary data into a new mp3 file, as shown here:
with open('new.mp3', 'wb') as file:
    file.write(binary_data)

Thank you everyone !

Convert Azure Blob object to MP3


What do the terms "arco" and "ten." mean?

Question: I am following along with a score of Beethoven's first symphony. I came across two abbreviations, arco. and ten. What do those stand for?
Answer: Arco (which is not an abbreviation) means to return to bowing after pizzicato (abbreviated pizz.) or col legno. Pizzicato means you pluck the strings with your fingers instead of using the bow, col legno using the wooden backside of the bow instead of the hairs.
Ten. is short for tenuto which means holding. In Beethoven it probably means you should hold the note for its full length instead of leaving a little gap between the notes. In later music (and also in Beethoven when played in a more romantic style) it also often means that you hold the note for longer than notated (using rubato) and/or play the note a bit louder. In any case it's a sort of "slow accent".
Comment: @luser: Tenuto is not very well defined so it could also mean that in some cases, especially when used for every note in a passage. However, I would still associate *portato* or *non-legato* with that.
Comment: I've always understood *tenuto* to be midway between *staccato* and *legato*.


hibernate lazy fetch,how to add @ManytoMany associations?

Question: I have two entities: Movie and Cinema, and they have a @ManyToMany association. Cinema is the owning side, in both side, I keep a collection to store fks, and both side is lazy-fetched:
<code>@Table(name="Cinema")
@Entity
public class Cinema {
@ManyToMany(cascade = CascadeType.ALL,
            fetch=FetchType.LAZY)
@JoinTable(name = "cinema_movie",
            joinColumns = {@JoinColumn(name = "cinema_id", nullable = false, updatable = false)},
            inverseJoinColumns = {@JoinColumn(name = "movie_id", nullable = false, updatable = false)})
private @Getter @Setter Set<Movie> movies = new HashSet<Movie>();
...
}

@Entity
@Table(name="Movie")
public class Movie {
@ManyToMany(cascade = CascadeType.ALL,
            mappedBy = "movies")
private @Getter @Setter Set<Cinema> cinemas = new HashSet<Cinema>();
...
}
</code>
Because I'm using lazy-fetch, If I test the following code, I will get a
"LazyInitializationException":
<code>//mp and cp means MovieRepo and CinemaRepo
Cinema c2 = new Cinema();
        c2.setCity(1);
        c2.setAddress("chigang");
        c2.setName("feiyang");
    cp.save(c2);
    m = mp.findByName("Zootopia");
    //m = mp.fetchCinemas(m.getId());
    m.addCinema(c2);
    c2.addMovie(m);
    cp.save(c);
</code>
If I uncomment <code>//m = mp.fetchCinemas(m.getId());</code> the exception is gone. As you can predict, I execute the following sql query in this method:
<code>@Query(
        "SELECT c FROM Cinema c INNER JOIN FETCH c.movies "+
        "WHERE c.id = :cid"
      )
Cinema fetchMovies(@Param("cid") Long cid);
</code>
But when I just want to associate a Movie with a Cinema, it's cumbersome to explicitly fetch All the Cinemas, and then add one to it, and then save them all.
How can I add a Cinema to m's collection without fetching all Cinemas associated with m??
Or maybe I have problem understanding lazy-fetched collections in hibernate. I'm new to hibernate and Spring. Any Help is appreciated. (If you read my question to this line, thank you whatever <3 )
Answer: If you don`t want to fetch all the collection items before adding a new element, you have use Bag Collection instead of Set.
Please refer to the post below which explains all various ways of achieving it:
Hibernate - How to persist a new item in a Collection without loading the entire Collection


Why have I never found any music written in the key of C Sharp Major?

Question: Music is written in almost every key that corresponds to every note that is on my piano keyboard.  There is clearly a C Sharp (black key right of C) on my piano keyboard but I don't recall ever seeing any music written in the key of C-Sharp Major.
Surely the key of C-Sharp Major must exist!  So why is it never or rarely used?
Are there situations where the key of C-Sharp Major would be more appropriate than say the key of D Flat Major?
EDIT: I never meant to suggests that you can't find examples of music written in C-Sharp major on Google (as Todd Wilcox pointed out in his comment).  It's that it is comparatively rare and I don't find it in any of my many music books among many thousands of songs.
Comment: You haven't done a web search for "in c sharp major", apparently: http://imslp.org/wiki/Prelude_and_Fugue_in_C-sharp_major,_BWV_848_(Bach,_Johann_Sebastian) is the second hit on Google. If you look, you'll see that there are several double sharps. Transcribing it in Db would make those naturals but might involve double flats.
Comment: @ToddWilcox  It's interesting trivia brought to my attention by a comment to my answer on another question wherein it was pointed out that my chart left off the C sharp major scale.  I attempted to explain why in a comment then it occurred to me that others may have this question at some point.   I hope my answer to this question succinctly summarizes the issue in plain English without getting too deep into music theory.
Comment: You haven't found the Well-Tempered Clavier P&F III then (both books)?
Comment: @RockinCowboy - Bach's idea was that with well-tempered tuning a keyboard could play fairly well in tune in any of the 24 keys. Pretty close, I think, to equal temperament for the time. Unlike, as you say, to only being able to play in tune with a traditionally tuned clavier in one ( perhaps two) keys.
Comment: Similarly, C flat major/A flat minor is rarely used in the musical literature. A notable exception to this is Poulenc's ["C"](http://musicalanalysisforsingers.blogspot.com/2010/08/c-from-poulencs-deux-poemes-de-louis.html), which was intentionally written by Poulenc in A flat minor.
Comment: @EJP No I am not familiar with the Well-Tempered Clavier.  From the title I assume it contains music written for an instrument tuned in other than Equal Temperament.  I know that in certain tunings of Clavier that not all keys are available.
Answer: C sharp major has seven sharps, D flat major has five flats.  Out of the box, the latter is preferable.  The former may be more appropriate when there is more material requiring "flattening" the key signature than otherwise.  Now major is a rather sharp mode, so it's not quite unlikely.  For example, a "proper" fully diminuished chord in C sharp features C-sharp, E-natural, G-natural, B-flat.  The same in D flat is D-flat, F-flat, A-doubleflat, C-doubleflat.  If we are talking about the "functional" dominant seventh (G#7, namely G#, B#, D#,F#) replacement, it's actually D sharp dim (D#, F#, A, C) with its enharmonic cousin E flat dim (Eb, Gb, Bbb, Dbb).  The former has a lot more reserves for modulation than the latter.
Comment: Wouldn't the "proper" fully diminished chord be represented an altered chord on the 7th degree of a major or harmonic minor scale, meaning C#-E-G-Bb would use the key signature of D major or D harmonic minor?
Comment: @awelotta indeed.  The prototypical diminished seventh chord is built on the leading tone of the minor scale, so in A sharp minor it comprises G double sharp, B sharp, D sharp, and F sharp.  Built on the leading tone of C sharp major it would be B sharp, D sharp, F sharp, and A natural.
Answer: Yes indeed there is a key of C Sharp Major (C# Major).  But the key of C Sharp Major is the "enharmonic equivalent" of the key of D Flat Major.
What that means is that all of the notes in the C sharp major scale sound pretty much exactly the same (to the human ear) as all the notes in the D Flat major scale – only they are notated (written) differently.
The black key to the right of any C key on a piano keyboard is in fact a "C-sharp" and will be most likely be referred to as such when playing a piece in any key that contains a C sharp in the scale or in any key with sharps in the key signature for that matter.  But the exact same black key can also be identified as a D flat – and will be referred to as such in pretty much any key with flats in the key signature (with occasional  exceptions).
So it may be helpful to understand that on any keyboard instrument (such as piano) or fretted instrument (such as guitar), the notes played in the D-Flat Major Scale will be exactly the same as the notes in the C-Sharp Major scale.
Therefore – given that a particular melody or arrangement of note pitches can be written either as D Flat Major or C Sharp Major and will be played on the same keys or frets either way (and sound the same), most composers will choose D Flat because instead of seven incidentals (7 sharps) to deal with in the key signature for C Sharp Major, there are only five incidentals (5 flats) in the key of D Flat Major.
However, in a case where a musical composition were to modulate (change keys) from another key containing sharps in the key signature, it might be more appropriate to use C Sharp instead of D Flat.
The foregoing also applies to the key of C Flat Major as well - which is the enharmonic equivalent of B Major  – only in C Flat Major there are seven flats in the key signature compared to five sharps in the key of B Major.
Interestingly the key of C Flat Major is the only case where the enharmonic equivalent of a Flat key is a Natural key.
Also interesting to note is that the C-Flat Natural Minor Scale contains three double flats in addition to four regular flats (quite cumbersome to notate in a key signature to say the least).  Whereas the enharmonic equivalent B Natural Minor scale only has two sharps – yet contains the same note pitches in the same order with the same intervals and sounds the same.  But it's obviously much easier to write.
So given a choice between notating a musical composition using a key that includes 7 incidentals (sharps or flats) in the key signature versus notating the same note pitches using a key with only 5 incidentals, most composers will choose the one with only five (in most cases)!
Comment: @ToddWilcox Well I actually meant to use incidental (as in incidental to the key) to contrast with "accidental" which Wikipedia defines as "In music, an accidental is a note of a pitch (or pitch class) that is not a member of the scale or mode indicated by the most recently applied key signature".  I know that it is common to refer to sharps and flats as "accidentals" regardless - but I think that usage likely evolved from the fact that whenever a # or b or natural symbol is written on a staff outside the key signature it is called an accidental - so the name stuck (albeit incorrectly).
Comment: @ToddWilcox  Check out the new answer I just posted to that age old question on Music SE that I stumbled across on Google search. (http://music.stackexchange.com/a/43296/16897)
Comment: Did you mean "accidental" instead of "incidental"? I'm not used to seeing the latter.
Comment: @ToddWilcox  Searching on Google for the word for the collective sharps or flats in a key signature which can't be accidentals because they are on purpose - I came across this on Stack Exchange (http://music.stackexchange.com/a/5240/16897)
Comment: Hahaha. Those two comments are so much like click bait. "Three answers to a question you never thought to ask, the accepted answer will shock you!" "You'll never guess who asked a question combining group theory with 12 tone composition. Or who argues about which stack it should be on!"
Comment: Before reading the comments, I liked the term 'incidental'. Rockin, I think (and hope) you've coined a new term which actually succinctly does its job well! +1.
Comment: This answer would gain accuracy by mentioning that by saying C# and Db are equal "on keyboard instruments", what you really mean to say is that they are equal when tuning in [equal temperament](https://en.wikipedia.org/wiki/Equal_temperament). Not all keyboard instruments are tuned that way.
Comment: @reinierpost  True - but all keyboard instruments use the same key for both C# and Db so regardless of tuning they will play the exact same pitch (and depending on tuning that pitch may well be either a C# or Db but will still be the same regardless).
Comment: @ToddWilcox - was not intended as "click bait"!  it was an elaboration on my reply to your original comment about whether I meant to call sharps /flats in key signature accidentals vs incidentals.  I searched the web to see if others called them incidentals (since they are on purpose not accidental) & what showed up was that question from 2012 on Music SE.  So I explained my logic for "incidentals" by posting an answer to that question since comments are limited characters.  That answer was just for you brother Todd ;-).  I doubt anyone else is going to find it on a 4 year old question Lol.
Comment: @Rockin Cowboy: You're probably right. To disprove it, you'd need to find a piece in C# and one in Db that are clearly written to be played in a non-equal temperament tuning system and without tuning the instrument specifically for the key. I don't think such pieces can be found.
Comment: @Tim Thanks for the comment on "incidentals".  I'm assuming somehow you found the "click bait" Todd referenced and read my new answer to the question from 4 years ago where nobody knew what the sharps and flats in a key signature should be called other than "the sharps and/or flat symbols in the key signature".  Now we can all call them "incidentals".  It's Todd's fault (he challenged me on the use of the term suggesting I meant "accidentals" and "incidentals" was used by accident) - Lol.
Comment: @reinierpost What I mean is they are equal on any given keyboard instrument (most folks only have one piano).  What you mean I think is that two different keyboard instruments can each be tuned differently so that one plays the black key to the right of C as C# and the OTHER one plays that same key as a Db (and a third keyboard can be tuned in equal temperament and split the difference).  You are correct about that.  But the same keyboard will play the same pitch when you strike that black key no matter how it's tuned.
Comment: @reinierpost - for any one given keyboard tuned in 12EDO, or in fact to any tuning, that black key to the right of C will always be called C# - or Db. Do you believe there is a keyboard out there with an extra key?
Comment: @Tim: Such keyboards are very rare. Indeed, I'm not aware of any keyboard that will play the same key differently depending on whether a C# or a Db is "meant".
Comment: Not sure that I agree that Db major is any easier than C# major.  In Db major you have to remember which notes are flat and which are natural.  In C# major everything is sharp so its easier to remember.
Comment: @JimM  That's an excellent point from the musician's perspective.
Comment: It really helped me to solidify this answer to think that one could write C major as D double-flat major. It wouldn't change the music at all right? It would just be nearly impossible to read!
Comment: "all of the notes in the C sharp major scale sound pretty much exactly the same (to the human ear) as all the notes in the D Flat major scale": they don't just sound "pretty much the same"; in the 12-tone system they _are_ the same, regardless of tuning or temperament.
Answer: The other upvoted answers here are good and I don't want to repeat them, but I think that there is a lot to add.

Surely the key of C-Sharp Major must exist! So why is it never or rarely used?

There are several factors that need to be taken into consideration. Here I list a few:
Historical
To understand modern scales you need to look at how they originated. This can be fairly technical so I will give the short version. Before the major/minor system there were hexachords - scales built from 6 pitches with 1 tone steps except for a semi-tone in the middle, 3-4. In the Middle Ages, music used 3 hexachords:

hexachordum naturale: C D E F G A (semitone in E-F)
hexachordum durum: G, A, B♮, C, D, E (semitone in B♮-C)
hexachordum molle: F, G, A, Bb, C, D (semitone in A-Bb)

In the 15th century or so more hexachords were added using the same process which results in sharps and flats being added one by one. Eventually, you could have one that looks like this: C#, D#, E#, F#, G#, A# (semitone in E#-F#). It's a bit like the circle of fifths in equal temperament where you add a sharp (flat) for each modulation up (down) a fifth.
Note that in these eras there is no equal temperament yet, so C# and Db hexachords (or even major scales) do not sound the same. In general, I will consider equal temperament when answering your question.
The point is that not all scales were treated equally. Those with less accidentals had historical preference and it took time until those with more accidentals were "discovered" and used. And we all know that old standards stick and have heard at some point the answer: "because of historical reasons".
"Color" and meaning of a scale
This is a bit more fuzzy. There were (are?) notions of different "colors" and meanings for scales.
"Color"
You might have heard things like "F major is very bright [compared to, say, Bb major]" or "C# minor is very dark [compared to A minor]". It could be that this was the acoustic result of the tuning system and instrument building at the time (remember that the instruments evolved with the expansion of hexachords, so before the 14th century instruments didn't need/have a D# pitch, for example).
Meaning
Sometimes different scales mean or symbolize something for composers. You sometimes hear "Beethoven chose insert scale here because it was associated with freedom [or something]". For example, Beethoven saw C minor as "powerful and emotionally stormy".
It will require digging to figure out what each scale was associated with at each musical era or for different composers. I have yet to see any such association/symbolism for C# major, nor a "color".
The scale in which we hear
Again, this topic is a bit fuzzy, but I will make my point clear. Most (all?) of us, when we compose music in our heads, do it in one of just a few scales. Try to compose a few tunes in major and minor on different days (don't listen to anything tonal beforehand) and be faithful to the key you hear them in. You "should" find out that you don't hear the music in your head in 10 different scales - you stick to a few.
I'm not sure what kind of bias it has or what the statistics are. I'm just noting that, again, not all scales are treated equally.
Ease of use
Scales with less accidentals are easier to read and play. It's also easier to modulate around them. Just as an example from A-sharp minor: "A♯ major, usually replaced by B♭ major, since A-sharp major's three double-sharps make it impractical to use." I wouldn't take Wikipedia as a gospel, but "impractical to use" is somewhere in the right direction. Nevertheless, Chopin modulates to A# major in one of his pieces.
Both C# and Db major have a lot of accidentals.
Conclusion
Any combination of the above can give you the answer. There are certainly many instances where those keys are used, either as the base of the piece or in a modulation, but they are truly less abundant than the scales we usually see (hear). Just remember that C# major is not the scale with most sharps as you can always take enahrmonic scales like A# major and these are used even less.

Are there situations where the key of C-Sharp Major would be more appropriate than say the key of D Flat Major?

While the previous question would be more in the area of musicology, this one is much more technical and one has to look at theory. I won't go deep, but the answer is "yes". Here is a simple example:
You write a piece in Binary form in F# major. You modulate to the fifth, which is C# (not Db) major. You then modulate back to F# major and finish the piece. This is a regular I-V-I form.
Comment: Wow - such a detailed and comprehensive answer.  And great job for going beyond what some of the other answers mentioned.  Hope to see you participating more on this site in the future.
Comment: @RockinCowboy Thanks, this is more or less my standard for answers (on other SE sites as well). I would like to participate more, but most of the questions on this site are about amps for electrical guitars and the like and only a handful are about musicology, performance or theory.
Comment: Stick around.  They come in spurts.  There will be more questions about musicology and theory.  As you probably know you can set up e-mail alerts for questions with certain tags such as "theory", "Key", "Scales" etc.
Comment: I'll second the Cowboy here- a great answer.  If I may, I'd just like to add a bit on the point about ease of play.  Especially for many woodwinds, there's a real physical difference between playing a C major scale and a C # major scale, since most have their "natural scale" somewhere close to C major, where you don't have to use those side keys as much.  Even though any good woodwind player can rattle off a C # major scale about as fast as C major, it's not as easy, and the further you go back in history, the less easy it becomes.  This of course ties in with the evolution of the scale.
Comment: @ScottWallace Not only is does it become harder, it can become impossible. The very early clarinet did not have the ability to produce its middle B note (the long left pinky bridge wasn't invented yet). Molter, having written what might be the first clarinet concertos, had to content with not using the middle B in the solo part, affecting his choice of scale. Indeed, D major was used for the first 4 concertos, where B is not as dominant as in other scales of the time.
Comment: The "colors" of scales is an interesting topic! The ranges of different instruments likely also have some part - certain scales are more convenient for some instruments, and the tones of the instruments convey the emotional association. Also famous compositions and their moods may influence how their scales are perceived.
Comment: "Note that in these eras there is no equal temperament yet": lutes and other fretted instruments used equal temperament or at least something close enough to it that 12-tone enharmonic equivalence was well established -- as it also was for the standard 12-tone keyboard.  And nobody was using C sharp hexachords.  C sharp would arise as the _mi_ of a hexachord on A, and no other pitches would be used from that hexachord. By the time people were writing music in such "distant" keys as would make a sharp-side hexachord useful for pitches other than its _mi,_ hexachord theory was no longer current.
Answer: one reason could be - especially for brass instrument arrangements:
Cornets, Flugelhorns, Baritones, Trombones are tuned in Bb, Tenor Horns (Alto-instruments) in Eb. Bass in Bb and Eb. Mind that they have already to read with 3 sharps while other instruments in C have no signs ...
Why didn't  Bach notate the C#-prelude in Db?

I would prefer to read the first page in Db major but near the end as it modulates to c# minor there there are some chords: e,c#,a,f#,d#,b# this would be Fb,Db,Bbb,Gb,Eb,C and more difficult for reading and analyzing.
a propos analyzing: This is a lot easier in C# major - as you can drop all signs (7 sharps and read it in C major and understand it much  better.
btw. there's actually a transposition in Db on the IMSLP
Answer: Shorter answer: Every note in a scale must be named A, B, C... etc. in ascending order but with as many accidentals like sharp and flat as necessary.
Therefore, the note before C# (major 7th) is B sharp (the same as C natural), and E# (same as F natural) is the third which is annoying to work with.
Comment: Actually B double-sharp is a C#, since there is no separate B# key. B# is the same as C natural. In the key of C# major, E and B are both sharp, so one plays notes enharmonic to F natural and C natural, respectively.
Comment: Yes, you're right. You get both E# and B#.  Also annoying
Answer: Im a violinist and guitarist, so I find C-sharp to be easier than D-flat on this instrument. My best friend is a pianist, who prefers D-flat. Same with one of my other harpist friends, who prefers D-flat unanimously, because C-sharp just doesn't go well with the harp (neither does d-flat tho :/). HOnestly, I think that the gap in use just depends on the kind of instrument you use. Guitar and violins tend to find playing in c# major easier than Db, while the opposite goes for piano and harp.
Comment: Hi Sagnik - welcome. Please read other posts before adding your own, especially on a very old post with multiple answers.


What is the best approach to work with nested object in this.state?

Question: I was reading the react documentation State Updates are Merged and theres a part it says when we set a object like this eg: this.setState({comments}) it completely replaces this.state.comments.
So, I wonder if the approach to change name of user object on event onChangeText is valid, because the whole object it'll be replaced everytime onChangeText is called.
Is that a good practice or have side effects like bad perfomance?
example code:

this.state = {
    user:{
        name: "",
        surname: "",
    },
    isLoading: false
};
/***/
onChangeText={name => this.setState({
  user: {
    ...user,
    name
  }
})}
Comment: take a look at: https://stackoverflow.com/a/51136076/5066625
Answer: Inmutability is one point performance is another.
Inmutability meas that you should not use the same reference to update an object but to replace with a new one. In React this is recommended because plenty of updates only happen if the object is new. You could create a problem if you re-use the user in the state, change the name and then update the state with it. And that is why it is a recommendation, in most cases React magic will work on others it can't tell the difference. So you would have to
const copy = Object.assign({}, this.state.user);

You can see this explanation here in the power of not mutating data
Performance in this case is concerning you because is a text listener, nevermind about it, React keeps track of the UI in a virtual DOM so it can update only what is different. This means everything will remain the same except for the new or deleted character.
You can try the clock example in the doc for that.
Answer: I'd recommend set from lodash/fp:
https://gist.github.com/jfmengels/6b973b69c491375117dc#_setpath-value-object
Answer: Your example is perfectly valid, provided the user variable is up-to-date.
To ensure that, you might want to use the function arg form of setState. This will call your function with the current value of this.state and set state to the returned value.
onChangeText={
  name =>
    this.setState(
      ({ user }) => ({
        user: { ...user, name }
      })
    )
}

Comment: I understand, but i don't think is a good pratice replace whole object every time `onChangeText` is called.
Comment: you need to replace the top level object, or React won't detect the change and re-render. It's the premise of immutibility and change detection. With that code you swap the `user` object for a new object, but keep the same references for it's key values, except for name which you change.
Comment: actually, I'm thinking about PureComponent/Memo shallow prop checking. i think `setState` triggers re-render regardless. But if you pass those state values to child components that connect to Redux, or are PureComponents, or Memoed, then depending on where they are in the component props, mutating an object may mean re-renders don't always occur when they should.


Retrieving thread id's upon execution and completion? Java

Question: How would you go about retrieving the id of a thread when it has been executed and then again when its completed.
Thus printing them in order of their execution/completion from within the main thread.
I am using a forkjoinpool and executing a callable class.
Thanks
Answer: You should add to your task class something like this:
executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("Running task in" + Thread.currentThread().getId());
                <your code>
                System.out.println("Finished task in" + Thread.currentThread().getId());
            }
        });

Comment: Cheers, but I need to return the id to the main thread for printing there. (I should of specified.)
Comment: In this case you can put this information into blocking queue from tasks and fetch these values and print them in main thread, but if main thread will waits until some data to print arrives, it will be locked.
Answer: Create wrapper that implements Callable and calls another callable inside. But also retrieves the Thread ID and do with it what you want, e.g.:
class ThreadTrackingCallable<V> implements Callable<V> {
    private Callable<V> payload;
    ThreadTrackingCallable(Callable<V> payload) {
        this.payload = payload;
    }
    V call() throws Exception {
       jobStarted(getCurrentThreadId());
       V result = payload.call();
       jobEnded(getCurrentThreadId());
       return result;
    }

    private int getCurrentThreadId() {
        return Thread.currentThread().getId();
    }
}