closed form of $\int_{0}^{2\pi}\frac{dx}{(a^2\cos^2x+b^2\sin^2x)^n}$

Question: closed form of $$\int_{0}^{2\pi}\frac{dx}{(a^2\cos^2x+b^2\sin^2x)^n}$$
for $a,b>0$
n=1 we get
$$\int_{0}^{2\pi}\frac{dx}{(a^2\cos^2x+b^2\sin^2x)^1}=\frac{2\pi}{ab}$$
n=2 we get
$$\int_{0}^{2\pi}\frac{dx}{(a^2\cos^2x+b^2\sin^2x)^2}=\frac{\pi(a^2+b^2)}{a^3b^3}$$
but what the integral for n ????
I hope to be there two different solution one by contour integration and the other by real analysis and thanks for all
Answer: In the following we assume that $n\geq 1$.
Thanks to parity, the integral can be written as
\begin{align}
I_n=\int_0^{2\pi}\frac{dx}{(a^2\cos^2x+b^2\sin^2 x)^n}&=2\int_0^{\pi}\frac{dx}{(a^2\cos^2x+b^2\sin^2 x)^n}=\\
&=2\int_0^{\pi}\frac{dx}{(\frac{a^2+b^2}{2}+\frac{a^2-b^2}{2}\cos 2x)^n}=\\
&=\frac{1}{i}\oint_{|z|=1}\frac{z^{-1}dz}{(\frac{a^2+b^2}{2}+\frac{a^2-b^2}{2}\frac{z+z^{-1}}{2})^n}=\\
&=\frac{2^{2n}}{(a^2-b^2)^n}\frac{1}{i}\oint_{|z|=1}\frac{z^{-1}dz}{(z+z^{-1}-c-c^{-1})^n}=\\
&=\frac{2^{2n}}{(a^2-b^2)^n}\frac{1}{i}\oint_{|z|=1}\frac{z^{n-1}dz}{(z-c)^n(z-c^{-1})^n}
\end{align}
where the parameter $c$ is defined by the equation
$$c+c^{-1}=2\frac{b^2+a^2}{b^2-a^2}.$$
In particular, we can (and will) set $\displaystyle c=\frac{b-a}{b+a}$. Now for $b>a>0$ the point $z=c$ is inside, and $z=c^{-1}$ outside the unit circle. Therefore, by residues,
\begin{align}
I_n&=2\pi\cdot\frac{2^{2n}}{(a^2-b^2)^n}\cdot\mathrm{res}_{z=c}\frac{z^{n-1}}{(z-c)^n(z-c^{-1})^n}=\\
&=\frac{2\pi}{(n-1)!}\cdot\frac{2^{2n}}{(a^2-b^2)^n}\cdot\left[\frac{d^{n-1}}{dz^{n-1}}
\frac{z^{n-1}}{(z-\frac{b+a}{b-a})^n}\right]_{z=\frac{b-a}{b+a}}
\end{align}
In particular,
\begin{align}
I_1&=\frac{2\pi}{ab},\\
I_2&=\frac{\pi(a^2+b^2)}{a^3b^3},\\
I_3&=\frac{\pi(3a^4+2a^2b^2+3b^4)}{4a^5b^5},\\
&\ldots
\end{align}
Explicit computation (by RGB)
We can compute the residue explicitly by using the binomial formula.
We have that $$z^{n-1}=c^{n-1}\left(1+\frac{z-c}{c}\right)^{n-1}=\sum_{k=0}^{\infty}\binom{n-1}{k}\frac{(z-c)^k}{c^{k-n+1}},$$
and that $$(z-c^{-1})^{-n}=(c-c^{-1})^{-n}\left(1+\frac{z-c}{c-c^{-1}}\right)^{-n}=\sum_{k=0}^{\infty}\binom{-n}{k}\frac{(z-c)^k}{\left(c-c^{-1}\right)^{k+n}}.$$
We multiply these two to get a series $\sum_{k=0}^{\infty}A_k(z-c)^k$. We need then to divide by $(z-c)^n$ and get the coefficient of the term with degree $-1$. This will be the coefficient $A_{n-1}$.
Using the formula for the product of two series we get
$$A_{n-1}=\sum_{r=0}^{n-1}\frac{\binom{n-1}{r}}{c^{r-n+1}}\frac{\binom{-n}{n-1-r}}{\left(c-c^{-1}\right)^{2n-r-1}}.$$
Finally we get $$I_n=2\pi\cdot\frac{2^{2n}}{(a^2-b^2)^n}\cdot\sum_{r=0}^{n-1}\frac{\binom{n-1}{r}}{c^{r-n+1}}\frac{\binom{-n}{n-1-r}}{\left(c-c^{-1}\right)^{2n-r-1}}.$$
Explicit computation II (by O.L.)
Let us write
 $$\frac{z^{n-1}}{(z-c^{-1})^n}=\frac{((z-c^{-1})+c^{-1})^{n-1}}{(z-c^{-1})^n}=\sum_{k=0}^{n-1}{n-1\choose k}c^{k+1-n}(z-c^{-1})^{k-n}$$
 We have to compute $(n-1)$th derivative of this expression and then evaluate it at $z=c$. This gives the formula
 $$I_n=2\pi\cdot\frac{2^{2n}}{(a^2-b^2)^n}\cdot\sum_{k=0}^{n-1}(-1)^{n-1}{n-1\choose k}{2n-2-k\choose n-1}c^{k+1-n}(c-c^{-1})^{k-2n+1}$$
which is equivalent to that of RGB.
Comment: He want's the closed form. You are so close. Compute residue 'the right way' and you will get it. Expand in powers of $z-c$ by using the binomial formula for $z^{n-1}=(c+(z-c))^{n-1}$ and for $(c-c^{-1}+(z-c))^{-n}$.  Then multiply.
Comment: @RGB An equivalent way is to use the binomial formula to expand $z^{n-1}$ in powers of $(z-c^{-1})$. The derivatives are then easily computable, but I was too lazy to finish this. Thanks for the edit, but there should be some misprints in your final formula. I will try to figure this out.
Comment: @RGB Sorry, there was no misprint, I misunderstood what is $A_{n-1}$.
Comment: Why $z-c^{-1}$? Aren't you computing residue at $c$? How do you make the derivatives easily computable? Is there some trick that works for this particular problem. I always advise against that formula for the residue. It is usually more pretty than useful. Please, check my computations. I typed them without checking. Notice that $A_{n-1}$ is only the residue.
Comment: (+1) nice work. By the way, you may be interested in working on this [problem](http://math.stackexchange.com/questions/374274/complex-fourier-series/374332#374332) which I just for The OP to use the technique you used in this problem but I have not worked out.
Comment: @RGB I added some details. I obtain slightly differently looking formula but it seems to be equivalent to yours.
Comment: Sorry if my comment seems stupid but there is something I do not understand. Aren't those integrals called "elliptic integrals" ? I think I was once told that the value of such an integral can only be approximated using a computer ? What am I missing here ? Thanks !
Comment: @jibounet Elliptic integrals contain square roots - e.g. if $n$ were equal to $1/2$ instead of being integer, that would be precisely complete elliptic integral of the 1st kind.
Comment: That's it ! Thanks ! I knew I was missing something.
Answer: Here is a different solution using differentiation under integral sign.
We firstly look at the the case $n=1$, then
\begin{align*}
I_1(\alpha,\beta) & = \int_{0}^{2\pi}\frac{dx}{\alpha \cos^2x+\beta \sin^2x} \\
                  & = \int_{0}^{2\pi} \frac{\sec^2x}{\alpha + \beta \tan^2x} \,\mathrm{d}x\\
                  & = \frac{1}{\beta} \int_0^{2\pi} \frac{1}{\left(\sqrt{\frac{\alpha}{\beta}}\right)^2 + \tan^2 (x)}\; \mathrm{d}(\tan x)\,\\
& = \frac{1}{\sqrt{\alpha \beta}} \left[ \left(\tan^{-1}\left(\sqrt{\frac{\beta}{\alpha}}\tan (x)\right)\right) \right]_0^{2\pi} = \frac{2\pi}{\sqrt{\alpha\beta}}
\end{align*}
Where $a^2 = \alpha$ and $b^2 = \beta$.
Differentiating $I_n$ with respect to $\alpha$ and $\beta$ yields
\begin{align*}
\frac{\mathrm{d}}{\mathrm{d}\alpha} I_n & = - \int_0^{2\pi} \frac{n \cos^2x}{(\alpha \cos^2x + \beta \sin^2x)^{n+1}}  \\
\frac{\mathrm{d}}{\mathrm{d}\beta} I_n & = - \int_0^{2\pi} \frac{n \sin^2x}{(\alpha \cos^2x + \beta \sin^2x)^{n+1}}
\end{align*}
Addition gives that
\begin{align}
\nabla I_n = \left( \frac{\mathrm{d}}{\mathrm{d}\alpha} +
             \frac{\mathrm{d}}{\mathrm{d}\beta} \right) I_1
           = -n \int_0^{2\pi} \frac{(\sin^2x + \cos^2x)\mathrm{d}x}{(\alpha \cos^2x + \beta \sin^2x)^2}
           = -n I_{n+1}
\end{align}
Where $\nabla$ is the nabla-operator. This gives us the recurrence relation
\begin{align}
\nabla I_{n-1} = (1-n) I_n \tag{1}
\end{align}
Which can be used to evaluate each $n \in \mathbb{N} / \{ 1 \}$.
This is a fine answer, but if one wants an explicit expression it can
be shown by induction that
$$ \boxed{ \displaystyle
   I_{n+1} = \frac{2\pi(-1)^{n}}{n!} \left( \frac{\partial }{\partial \alpha} + \frac{\partial}{\partial \beta} \right)^n \frac{1}{\sqrt{\alpha\beta}}
}
$$
Where the last part is the binomial expansion of the partial derivatives, and this holds for all $n \in \mathbb{N}$.
Example for $n=3$ we have
$$
\left( \frac{\partial }{\partial \alpha} + \frac{\partial}{\partial \beta} \right)^2 f(\alpha,\beta) = \left(\frac{\partial^2}{\partial \alpha^2}+2\frac{\partial^2}{\partial \alpha \, \partial \beta}+\frac{\partial^2}{\partial \beta^2}\right)f(\alpha,\beta)
$$
We can also express the partial derivatives as a sum like
$$
\left( \frac{\partial }{\partial \alpha} + \frac{\partial}{\partial \beta} \right)^n
                               =
\sum_{k=0}^b \binom{n}{k} \frac{\partial^{n-k} }{\partial \alpha^{n-k} }
                  \frac{\partial^{k} }{\partial \beta^{k} }
                               =
\sum_{k=0}^\infty \binom{n}{k} \frac{\partial^{n} }{\partial \alpha^{n-k} \,\partial^k \beta}
$$
without that doing us much good. For actually computing explicit values of $n$ the recurrence relation is more convenient.
EDIT: To give an intuition of the "closed" expression one can first rewrite
$(1)$ to
$$
I_n = - \frac{1}{n-1} \nabla I_{n-1}
$$
Direct computation of the first values gives

One can also show that the expression satisfies $(1)$ directly by insertion, and the use of the chain rule.
Comment: You can prove that expression by checking that it satisfies the expression $\nabla I_{n-1} = (1-n) I_n$. As in plugging it into the relation. I also added some information about the intuition behind the formula. Hope this helps.
Comment: Where you perform the differentiation, you say that you're going to do so with respect to $a$ and $b$, but you write $\alpha$ and $\beta$ in the derivative operators.  Which are you doing?
Comment: Of course differentiating $\alpha$ and $\beta$, fixed the minor typos. Fixed the formula as well, I can not be bothered writing out the induction proof, but if someone else wants to feel free =)
Comment: thanks but how can you prove $$ \boxed{ \displaystyle
   I_{n+1} = \frac{2\pi(-1)^{n}}{n!} \left( \frac{\partial }{\partial \alpha} + \frac{\partial}{\partial \beta} \right)^n \frac{1}{\sqrt{\alpha\beta}}
}
$$ with inuction at first then if you can prove it without induction


Criar objetos dentro de uma lista sem for/foreach C#

Question: private List<Compra> CriarCompras(int numComprasParaGerar)
{
    List<Compra> lstCompras = new List<Compra>();
    for (int i = 0; i < numComprasParaGerar; i++)
       lstCompras.Add(new Compra());

    return lstCompras;
 }

Seguindo o exemplo, existe alguma outra forma de criar N objetos dentro de uma lista do mesmo tipo?
EDIT
Todas as formas que estão nas repostas funcionam de maneira igual ou similar. Porém, como foi citado pelo @Maniero há um problema enorme de performance em todos eles quando comparados com o for.
Teste realizado pelo @Maniero no .NET Fiddle
Comment: As respostas que usam `Enumerable.Repeat` têm um problema: ele cria uma repetição de **valores**, ou seja, da referência ao objeto passado por parâmetro. De modo que **todos os itens na lista serão o mesmo item**. Ou seja: será criada uma única instância de `Compra` e a referência a esta instância será copiada para cada item na lista - alterando uma das compras, altera-se todas.
Answer: Você não usou um foreach, e nem poderia varrer esta lista. Use o Enumerable.Repeat() que é um método LINQ.
private List<Compra> CriarCompras(int numComprasParaGerar) {
    var lstCompras = new List<Compra>();
    lstCompras.AddRange(Enumerable.Repeat(new Compra(), numComprasParaGerar));
    return lstCompras;
}

Veja funcionando no ideone. E no .NET Fiddle. Também coloquei no GitHub para referência futura.
Já que todo mundo arrumou, pra resposta não ficar errada vou escolher a solução que achei mais elegante para resolver a questão.
private static List<Compra> CriarCompras(int numComprasParaGerar) => new int[numComprasParaGerar].Select(i => new Compra()).ToList();

Veja realmente funcionando no ideone. E no .NET Fiddle. Também coloquei no GitHub para referência futura.
Veja como é problemático no ideone fazendo de outra forma. E no .NET Fiddle. Também coloquei no GitHub para referência futura.
Já que teve várias respostas e no fundo a pergunta pede algo que não tem tanta necessidade assim, eu um teste rápido de performance. Eu sei que nem sempre deve-se penar na performance, deve-se pensar no que é simples, no que é legível. Mas a performance não pode ser negligenciada em todos os casos.
O que é simples e legível não é unânime. Por exemplo, tem que ache o LINQ como mais simples e legível. Tem quem ache que o código tradicional melhor neste aspecto.
No teste concluí que o código original do AP é o mais rápido com larga margem em relação aos demais.
Por ser a mais rápida e a mais simples na minha opinião, eu ficaria com a solução original do AP. E é o conselho que dou a ele, não mude.
Veja funcionando no ideone. E no .NET Fiddle. Também coloquei no GitHub para referência futura.
Só quero lembrar que o teste em uma máquina pública assim tem pouca confiabilidade. Aconselho rodar em seu computador em condições mais controladas.
Comment: @Omni normalmente sim, neste caso não vai fazer diferença porque está apenas usando um `AddRange` em seguida. Se tivesse usando o `for` como ele estava fazendo, seria altamente interessante usar o construtor que já aloca o espaço necessário para todos elementos e não criar o problema do [Shlemiel the painter](http://www.joelonsoftware.com/articles/fog0000000319.html)
Comment: Desculpe o erro na pergunta, editei a mesma.
Comment: Neste caso, dado que sabe o numero de elementos ante mão, não seria benéfico passar o numero de elementos no constructor de forma a evitar uma outra alocação?
Comment: Eu já havia feito esses testes. Resultado: Mantive a forma original, com for.
Comment: @JonathanBarcela Minha resposta voltou ser a aceita por você então :D Nem estou pedindo pra mudar a aceitação, mas de fato o que você resolveu fazer é o que está na minha resposta. Eu quase coloquei isto logo no início, mesmo sem fazer testes de performance. Optaria por isto só porque é o mais simples.
Comment: @bigown Engraçadinho :D Sua resposta original causou problemas ao AP porque ignorou que `Enumerable.Repeat` só copia valores, lembra?
Comment: @Caffé e daí, a atual foi a que ele escolheu de fato. Se eu tivesse ido no meu instinto a resposta seria justamente, fique com seu código, mas achei que iriam considerar resposta que não responde. Mas pode ficar com aceitação, não estou querendo ela. Pra mim basta que ele fez a escolha certa, mesmo que o verdinho não demonstre isto.
Comment: LoL (realmente rindo aqui hehehe) Por não ter seguido seu infalível instinto ou por qualquer outro motivo, o fato é que você ofereceu uma resposta com erros e aprendeu isso depois que leu a minha resposta. É pra isso que estou aqui: aprender, me divertir e  ajudar. Se pontos fossem do meu interesse eu tava lascado - seria só frustração. Por exemplo, você recebeu 5 votos por uma resposta que não funciona e enquanto ela ainda não funcionava :D (e claro, deve ter recebido muito menos pontos por outras respostas mais relevantes). It's always good to talk to you @bigown (still LoL )
Answer: As respostas que usam Enumerable.Repeat têm um potencial problema: ele cria uma repetição de valores, ou seja, se o objeto passado por parâmetro é um reference type (uma classe, por exemplo), serão feitas cópias da referência ao objeto passado por parâmetro e não cópias do objeto de fato.
De modo que todos os itens na lista seriam o mesmo item. Ou seja: se Compra é uma classe, será criada uma única instância de Compra e a referência a esta instância será copiada para cada item na lista - alterando uma das compras, altera-se todas.
Se isso é um problema para você, apresento abaixo uma alternativa que de fato cria X diferentes instâncias.
Solução que funciona tanto para reference types quanto para value types:
private List<Compra> CriarCompras(int numComprasParaGerar)
{
    return (from i in new int[numComprasParaGerar] select new Compra()).ToList();
}

Ou ainda:
private List<Compra> CriarCompras(int numComprasParaGerar)
{
    return new int[numComprasParaGerar].Select(i => new Compra()).ToList();
}

(eu prefiro a primeira opção, acho mais expressiva).
Edit:
Concordo em grau e gênero com a edição do @Maniero que diz que o código original do AP é a melhor opção; não por performance mas porque é mais expressivo e tudo nele revela apenas a real intenção sem ter que criar por exemplo um array que só serve de artifício para garantir as iterações.
Eu ainda adicionaria o número de compras no construtor da lista e talvez fosse mais além: devolvesse um array em vez de devolver uma lista, retornando no método um IList<Compra> ou um IEnumerable<Compra> (mas daí tem que ver também o lado do código consumidor - o que é mais cômodo para ele).
Eu usaria portanto um código mais ou menos assim:
private IList<Compra> CriarCompras(int qtdComprasParaGerar)
{
    IList<Compra> compras = new Compra[qtdComprasParaGerar];

    for (int i = 0; i < qtdComprasParaGerar; i++)
        compras[i] = new Compra();
    return compras;
}

Portanto, minha opção usando LINQ é apenas para responder diretamente a pergunta (que quer dispensar a declaração do for) e para alertar sobre o uso do Enumerable.Repeat.
Comment: Muito, muito, muito bem observado.
Comment: Sua resposta salvou vidas agora kkk Eu estava fazendo das outras formas e tentando a edição das compras, não sabia que era criado uma mesma referencia do objeto. Isso realmente gerou problemas. Mesmo tendo várias respostas válidas, a sua resposta foi o que deu origem a edição em todas as outras, então eu escolho a sua. Vlw :D
Answer: Sim, você pode usar o método AddRange() e Enumerable.Repeat() para isso.
private List<Compra> CriarCompras(int numComprasParaGerar)
{
    var lista = new List<Compra>();
    lista.AddRange(Enumerable.Repeat(new Compra(), numComprasParaGerar);
    return lista;
}

EDIT
Como minha internet falhou bem na hora que eu estava postando a resposta e minha resposta ficou praticamente igual a do @Maniero, aqui vai uma alternativa.
private List<Compra> CriarCompras(int numComprasParaGerar)
{
    return Enumerable.Repeat(new Compra(), numComprasParaGerar).ToList();
}

EDIT 2
Como foi dito na resposta do @Caffé o Enumerable.Repeat() faz com que todos os itens dentro da List sejam o mesmo item. Então, se você precisar que os itens da lista não apontem para o mesmo lugar (o que é muito provável), aqui vai uma alternativa.
private static List<Compra> CriarCompras(int numComprasParaGerar)
{
    return new List<Compra>(Enumerable.Range(0, numComprasParaGerar).Select(i => new Compra()));
}

Veja funcionando no dotNetFiddle
Answer: Pode usar Enumerable.Range() para gerar uma sequencia de valores e com um .Select() criar as novas instancias e por fim passa-las para o constructor da lista da seguinte forma:
private List<Compra> CriarCompras(int numComprasParaGerar)
{
    return new List<Compra>(Enumerable.Range(0, numComprasParaGerar).Select(i => new Compra()));
}

Comment: Ia para editar a resposta, mas invocar o método `.ToList()` (como na resposta do Jeferson), faz o mesmo que a minha resposta (isto e, cria uma nova lista e usa o constructor).
Answer: Além das outras respostas, também possível fazer usando a recursão:
private List<Compra> CriarLista(int numComprasParaGerar)
{
    var lstCompras = new List<Compra>();
    CriarCompras(lstCompras, numComprasParaGerar);
    return lstCompras;
}

void CriarCompras(List<Compra> lstCompras, int numComprasParaGerar)
{
   lstCompras.Add(new Compra());

   if(lstCompras.Count < numComprasParaGerar)
       CriarCompras(lstCompras, numComprasParaGerar);
}


Let $A$ be a commutative ring in which the only ideals are $0$ and $A$. Prove that $A$ is a field in the following cases

Question: Let $A$ be a commutative ring in which the only ideals are $0$ and $A$. Prove that $A$ is a field in the following cases.
i) $A$ has no divisors of zero.
ii) $A$ has unit.
Comment: The English term is "field", not "body".
Comment: @RobertLewis I am not the OP, just edited his answer
Comment: Alright, it's done.
Comment: Since you have answered your own question below, I need to ask you if you mean for us to *assume* both (i.) and (ii.) *concurrently*?  Because the way your question is stated, it appears you want us to assume (i.) and (ii.) one at a time . . . but your answer indicates otherwise!
Answer: Suppose $A$ is a commutative ring with $1$, and the only ideals of $A$ are itself and the trivial ideal. Suppose to the contrary that $A$ (a commutative ring) is not a field. Then there exists $u \in A \setminus \{ 0 \}$ such that $ua \neq 1$ for all $a \in A$. Then $(u)$ is a nonzero ideal, as $u 1 = u \neq 0$, and $(u) \neq A$, as $1 \not \in (u)$. But this violates our assumption that $0$ and $A$ are the only ideals of $A$.
EDIT: This proof does not draw upon assumption (i), only (ii), though of course a field must have property (i). Look at Robert Lewis for an answer that uses just (i).
EDIT 2: If we assume (i), then as Robert Lewis pointed out, we can get (ii) out. This is essentially his argument, with some details made more explicit; I'm just putting it here so both solutions can be on a single answer. Let $a \in A \setminus \{ 0 \}$, and consider the principal ideal $(a)$. Then $(a)$ is not the trivial ideal, so $(a) = A$. Thus for every $x \in A$ exists $y \in A$ such that $ a y = x $. Let $1_a$ be such that $a 1_a = a$.
There's a property that holds in rings with conditions (i) called the cancellation property. It says that if $x, y, z$ are nonzero, and $xy = xz $, then $y = z$. To see this, notice
\begin{align*}
xy & = xz \\
\Rightarrow x(y - z) & = 0 .
\end{align*}
Thus if there are no zero divisors, either $x$ or $y - z$ must be $0$. It's not $x$, so $y - z = 0 \Rightarrow y = z$.
It follows readily that $1_a$ is unique, that there exists one and only one element $x$ for which $ax = a$. Now I claim that $1_a = 1_b$ for all $a, b \in A$. In other words, there's only a single $1$, so I'll be able to drop the subscripts and talk about the singular $1$ of $A$. Let $y$ such that $ay = b$. Then
\begin{align*}
b & = a y \\
= 1_b b & = (1_a a) y \\
& = 1_a (a y) \\
& = 1_a b .
\end{align*}
So $1_a b = 1_b b \Rightarrow 1_a = 1_b$. So we can write just $1$, and $1 a = a 1 = a$ for all $a \in A$. Thus $A$ has a unity. Now we're in the situation of my original argument.
Comment: Thanks. I was going back to fix it and my app crashed.
Comment: @RobertLewis A good point. I'd figured that could be shown. But I also see now that my proof operated on (ii) alone, not the nonexistence of a zero divisor (though that of course falls out from being a field).
Comment: Edited my answer accordingly.
Comment: Thank you for your help Robert Lewis and AJY
Comment: I fixed a little $LaTex$ in your post.
Comment: I solved your problem only using item (i).  See my answer.
Answer: Upon first reading this question, I interpreted it as asking its readers to prove the conclusion, that $A$ is a field, under each hypothesis (i) and (ii) separately.  Then I read AJY's answer to his own question, and saw that he was apparently allowing both (i) and (ii) to be assumed together, as he did in his self-response.  But after mulling it over a little longer, I realized we can indeed reduce case (i) to case (ii) by showing that, under the given givens,
$A \; \text{has no zero divisors} \Longrightarrow A \; \text{has a unit}, \tag 1$
i.e., ceterus paribus, (i) implies (ii).  This may be accomplished as follows:
For $0 \ne a \in A$, consider the principal ideal $(a) = aA$; we have by hypothesis $(a) = \{0\}$ or $(a) = A$.  Suppose $A$ has no zero divisors.  Then $(a) = aA = A$, since otherwise $(a) = \{0\}$, and this implies every $0 \ne b \in A$ must satisfy $ba = 0$, a contradiction.  Thus $(a) = aA = A$, and thus there is some $0 \ne b \in A$ such that $ab = a$.  Furthermore, we have for any $c \in A$, $c \in (a)$ which means $c = da$ for some $d \in A$.  Then
$bc = cb = (da)b = d(ab) = da = c, \tag 2$
which, since $b \ne 0$, shows that $b$ is a multiplicative unit for the ring $A$; that such units are unique is well known.  So $A$ is unital; we now denote its unit by $1_A$.
We have reduced case (i) to case (ii).
Once again, considering the ideal $(a) = aA = A$, we see that we must have $ba = ab = 1_A$ for some $b \in A$.  Thus $a^{-1} = b$ and we are done; $A$ has a unit $1_A$ and every element $a \in A$ is possessed of an $a^{-1}$ with $aa^{-1} = a^{-1}a = 1_A$.
$A$ is thus a body, that is, a K$\ddot o$rper, a field.
Comment: The overall argument is fine but there is a minor thing I thought is worth mentioning. Here we're writing $(a)=aA$ almost as if the equation always held, but in fact it doesn't necessarily hold in rings without identity, which is the subject here. But of course it doesn't matter since there are only trivial ideals, and $(a)$ is forced equal to $aA$ since both are nonzero. Anyhow, that notation isn't even necessary and could be removed.
Comment: @rschwieb:  worth noting, I guess I lucked out insofar as $A$ only has two ideals!


How to initialize a nested anonymous array double in Java?

Question: I have a constructor that expects a nested array of doubles like this:
<code>public class Thing {

    public Thing (double[][] doubles) {
    }
}
</code>
I would like to overload the constructor with a default constructor that takes no arguments like this:
<code>    public Thing () {
        this(/* TODO */);
    }
</code>
Answer: You have to nest a new one-dimensional double array into another two dimensional double array. That way, you can initialize the inner, one-dimensional one manually and the outer two dimensional one with it.
<code>    public Thing () {
        this(new double[][]{new double[]{1}});
    }
</code>
You can also initialize more than one double:
<code>        this(new double[][]{new double[]{1, 2, 3}});
</code>
or even:
<code>        this(new double[][]{new double[]{1, 2, 3},
                            new double[]{4, 5, 6}});
</code>
Comment: Or ``new double[0][0]`` or size you want in the brackets.


$L^p$-limit and pointwise limit

Question: For $p\ge1$, I proved that if $f_n\stackrel{L^p}{\to} f$ and $f_n\to g$ a.e then $f=g$ a.e. But, how about the case $0<p<1$? Is it also true?
Comment: For $0<p<1$, "$L^p"$ isn't a normed space.
Comment: But is a metrizable topological vector space (not locally convex!). See http://math.stackexchange.com/questions/648862/why-do-we-consider-lebesgue-spaces-for-p-greater-than-and-equal-to-1-only.
Answer: For $p<1$, we say that $f_n\to f$ in $L^p$ if $\lim_{n\to\infty}\int |f_n-f|^p=0$.
Define $g_n:=|f_n-f|^p$: it converges to $0$ in $\mathbb L^1$ by assumption and $g_n\to |f-g|^p$ almost everywhere. We deduce from the case $p=1$ that $|f-g|^p=0$ a.e. hence $f=g$ a.e.

$L^p$-limit and pointwise limit


Search filter to show results on search page for custom post type

Question: I have a filter that has select options for Wheel base and price. Wheel base is a custom taxonomy assigned to the vehicles post type. This taxonomy has two options SWB and LWB.
Here is the markup for my form:

<form class="filterOffers__form" role="search" action="<?php echo site_url('/search'); ?>" method="get" id="searchform">

  <fieldset class="filterOffers__fieldset filterOffers__wheelbase">
    <select class="filterOffers__select">
      <option value="">Wheel base</option>
      <option value="swb">Short wheel base</option>
      <option value="lwb">Long wheel base</option>
    </select>
  </fieldset>

  <fieldset class="filterOffers__fieldset filterOffers__minprice">
    <select class="filterOffers__select" name="min-price">
      <option value="">Min £ price</option>
      <option value="5000">£5000</option>
      <option value="10000">£10,000</option>
      <option value="20000">£20,000</option>
      <option value="30000">£30,000</option>
      <option value="40000">£40,000</option>
    </select>
  </fieldset>

  <fieldset class="filterOffers__fieldset filterOffers__maxprice">
    <select class="filterOffers__select" name="max-price">
      <option value="">Max £ price</option>
      <option value="10000">£10,000</option>
      <option value="20000">£20,000</option>
      <option value="30000">£30,000</option>
      <option value="40000">£40,000</option>
      <option value="50000">£50,000</option>
    </select>
  </fieldset>

  <fieldset class="filterOffers__fieldset d-none">
    <input type="hidden" name="post_type" value="vehicles" />
  </fieldset>

  <fieldset class="filterOffers__fieldset">
    <input class="filterOffers__submit" type="submit" alt="Search" value="Search" />
  </fieldset>

</form>

When the search button is clicked, I want to search the vehicles post type and display those vehicles that match the filtered query. For example, if I have Short wheel base and £5000 - £10,000 selected form the dropdowns, I want to display all the vehicles that match that query.
To do this, I have created a search.php file, which looks like this:

<?php
/*
Template Name: Search Page
*/

get_header();

  global $query_string;

  $query_args = explode("&", $query_string);
  $search_query = array();

  foreach ($query_args as $key => $string){
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
  }

  $the_query = new wp_query($search_query);

  if( $the_query -> have_posts() ) : ?>

Search Result for : <?php echo htmlentities($s, ENT_QUOTES, 'UTF-8'); ?>

    <?php while ( $the_query->have_posts() ) : $the_query->the_post();
      echo the_title();
    endwhile; wp_reset_postdata();

  else: ?>

<?php _e("no posts found"); ?>

  <?php endif;

get_footer(); ?>

Current behaviour:
If from the dropdown I select Short wheel base and £5000 - £10,000 and click the search button, it takes me to:
/search?wheel-base=swb&min-price=5000-10000&max-price=&post_type=vehicles
The above url is correct based on my search parameters. However, the page 404s.

When I'm on /search, it'll echo the "Search Result for :" text.
When I'm on /search?wheel-base=swb&min-price=5000-10000&max-price=&post_type=vehicles - it'll 404.

I have followed the WordPress codex to create a search page (a page for search exists in the admin with the URL /search and the search page template is applied).
Ideas on what I'm missing here?
Comment: I don't know well wordpress, have you any middleware wich can return a 404 in case of error ? if you're not authenticated for example ?
Comment: @Ruvee - Unfortunately no, adding the trailing slash (`/search/?`) still results in a `404`
Comment: @mbesson - No, no plugins or such have been added that mess with permalinks or authentication
Comment: It's true that `post_type` (as well as `s`) is a reserved term or query var name, but you really should not make the custom `WP_Query` in your `search.php` template - it should just display the *main loop* (or the posts that already retrieved from the database via the main query - the global `$wp_query`) without having to make that custom query. You can check [the Twenty Twenty-One theme](https://themes.trac.wordpress.org/browser/twentytwentyone/1.3/search.php) to see an example of a good Search **Results** template.
Comment: And if you wish to have a custom search page at `/search/` (which is a Page - post type `page`), then you should use a template with another name like `template-search.php`, and then re-select the template from the Template dropdown on the Pages admin page.
Comment: On the Wordpress Codex page that you followed says `Note: The filename search.php is reserved as a special template name, so avoid its usage; the suggested searchpage.php just makes it easy to recognize in the list of files.`.

After you change your file from `search.php` to `something-else.php` don't forget to login into your dashboard and create a new page and choose *Search Page* from the `Page Attribute -> Template` section.

So basically your `something-else.php` file is just a template not a page.
Answer: Based on this post : https://wordpress.stackexchange.com/a/291661/205128
post_type appears to be a reserved name in wordpress (list)
Can you try to change post_type input name, or prefix it like mynamespace_post_type ?


Serialize to JSON string tracemalloc in Python 3.7

Question: I have to serialize to JSON string the result of tracemalloc.
current_mem, peak_mem = tracemalloc.get_traced_memory()
overhead = tracemalloc.get_tracemalloc_memory()
stats = tracemalloc.take_snapshot().statistics('traceback')[:top]

summary = "traced memory: %d KiB  peak: %d KiB  overhead: %d KiB" % (
    int(current_mem // 1024), int(peak_mem // 1024), int(overhead // 1024)
)

logging.info("%s", summary)

out_lines = [ summary ]
for trace in stats:
    out_lines.append("---")
    out_lines.append( "%d KiB in %d blocks" % (int(trace.size // 1024), int(trace.count)) )

    logging.info("%s", out_lines)

    out_lines.extend( trace.traceback.format() )

out_lines.append('')

data = {}
data['traceback'] = '\n'.join(out_lines).encode('utf-8')

res = json.dumps(data)

print(res)

When I dump data to JSON I get a
Object of type bytes is not JSON serializable

From logging I can see the string output:
2020-01-08 11:54:25 - INFO - traced memory: 35 KiB  peak: 91 KiB  overhead: 31 KiB
2020-01-08 11:54:25 - INFO - ['traced memory: 35 KiB  peak: 91 KiB  overhead: 31 KiB', '---', '1 KiB in 4 blocks']

and then in the loop:
2020-01-08 11:54:26 - ERROR - ['traced memory: 35 KiB  peak: 91 KiB  overhead: 31 KiB', '---', '1 KiB in 4 blocks', '  File "/usr/local/lib/python3.7/site-packages/tornado/routing.py", line 256', '    self.delegate.finish()', '  File "/usr/local/lib/python3.7/site-packages/tornado/web.py", line 2195', '    self.execute()', '  File "/usr/local/lib/python3.7/site-packages/tornado/web.py", line 2228', '    **self.path_kwargs)', '  File "/usr/local/lib/python3.7/site-packages/tornado/gen.py", line 326', '    yielded = next(result)', '  File "/usr/local/lib/python3.7/site-packages/tornado/web.py", line 1590', '    result = method(*self.path_args, **self.path_kwargs)', '  File "/tornado/handlers/memTraceHandler.py", line 56', '    self.write(json.dumps(response.getData()))', '---', '0 KiB in 2 blocks']

So which is the b"" string I cannot serialize?
Comment: There is no code that dumps anything to JSON, so your question lacks a [mcve].
Comment: I'd happily vote to reopen your question. However, concerning my vote to close, ATM it still stands. You are a regular here and should have known that your question was bad. Further, it still is bad. If you had extracted a [mcve], you could have easily found the error yourself. Also, searching for the error message could have given you a bunch of duplicates.
Comment: @UlrichEckhardt modified please consider to remove your close request. This does not make any help to the SF community! You should ask first to add more info and then if anything happens close. In my opinion this way helps users to improve their questions and help SF definitively.
Answer: YOU are creating the bytes object here:
data['traceback'] = '\n'.join(out_lines).encode('utf-8')
That's what calling encode does.
Simply do:
data['traceback'] = '\n'.join(out_lines)
And it will dump out fine.


Fitting a von Mises Distribution over the Global Population

Question: For a statistics course I'm taking this semester, we decided to conduct a worldwide survey. One of our questions, is an approximate coordinate of where the respondent lives. Our instructor recently introduced me to the circular von Mises distribution and it has been fun seeing how we can wrap a normal distribution around a circle or sphere.
But something I have been wondering is, if it is possible to fit a von Mises distribution, or a mixture thereof, on the global population. The tricky thing is that population statistics are usually recorded on a per-country basis, and projected onto 2D Mercator-Projection maps. So the first problem to solve is finding adequate "samples".
Something that I thought of, would be to derive a sample from each country, and to center these samples on the "center of gravity" for said country's population (ex. the most populous city or capital).
Ex. To represent Singapore: $X_i = [1°19′24.96″N, 103°55′38.42″E],\ Y_i=545$ Million, for some index $i$, where $X_i$ are the coordinates for Bedok, the most populous town. Coordinates $X_i$ can be transformed into $[1.81 \text{rad}, 0.0231\text{rad}]$ to represent them as radians instead.
Has no one done anything like this before? Is it just too impractical, or tedious? And at the very least, is there adequate justification to use this "center of gravity" approach to sample each country as a population center? (Would it make more sense to work on a city/town's population rather than overloading the entire country's population on one point?)
Comment: the population distribution is highly multimodal (most people live in big cities) so it wouldn't make sense to fit a single von mises. it might be better to instead fit a separate distribution to each city of sufficient size; but then you might as well use a normal rather than a von mises, because you are dealing with spatial scales where the topology of the earth is a negligible consideration.
Comment: could you say a bit more about what exactly your goals are in doing this fit, and how you envision it differing from existing population density maps such as https://www.luminocity3d.org/WorldPopDen/#3/19.23/35.42 ?
Comment: Yeah, after looking at population distribution datasets like World Pop and GSFC, I have also concluded that one-size fitting all is unfeasible, a mixture is certainly needed. And that so-called "negligibility" has been eating at me from the back of my mind for about a month now. Human settlement won't loop around the Poles, that's for sure. But West-to-East migration patterns are a whole other story. The Pacific does stand out as an anomaly which can't be represented accurately with a Normal Distribution, at least without playing with the projection of the earth.
Comment: Using a Von Mises distribution here or there, might not be the only solution; but somehow a concept of angularness is relevant at some point. Maybe a layered model; with Normal Distributions around clusters of big city population centers could be the basis, and a series of mixed-in Von Mises distr.'s could function as a regulariser. But more than anything right now, I want to see if anyone else has tried something on this scale before. Because this feels like a problem that should not be uncharted territory.
Comment: Well simple, grid-maps are only intuitive to look at, but not very descriptive. f.x. To describe where in the world population is concentrated, you have to list arbitrary geographic regions (which btw, different countries label differently). Similar for urbanization rates. It's un-systemic, and that limits our ability to generalize local observations to global trends. The next best thing, I guess, is to work with a time series of grid-maps, because that at least gives some sense of momentum for flows and growths of population. That seems to be the preferred approach if Covid has shown
Answer: Fitting the distribution is not so difficult. For the sphere you can use the von Mises-Fisher distribution) which expresses the probability in terms of the 3d Cartesian coordinates and the estimation of the parameters (when using maximum likelihood) is not so difficult.

Has no one done anything like this before? Is it just too impractical, or tedious?

I have come across descriptions of population as a function of latitude and longitude. I don't know a good image with the proper usage rights for sharing here, but you can see many in a google search.
The mean of those two (when you apply the summation in Cartesian coordinates) would coincide with the estimate of the mean for your von Mises distribution.
The variance might also make some sense for the distribution in the latitude, which has several noisy peaks but is roughly distributed in a single bulge. For the longitude there are however several peaks and the distribution is multimodal. The variance might not describe the entire distribution very well.
This also means that you might wish to use the Kent distribution, which is a generalization that allows a different degree of spread in different directions.

And at the very least, is there adequate justification to use this "center of gravity" approach to sample each country as a population center?

Using the center of gravity would be like having more coarse observations. You could do it but it would be less precise. It is the same for non-spherical distributions.
For the statistics course you might do the exercise by taking the data from counties population and center of gravities. And compare it with the result from more fine grained distributions/datasets that are available.
Using the data for the marginal distribution of latitude and longitude you should be able to fit the von Mises-Fisher distribution, but to fit the Kent distribution you would need to have the joint distribution.


Yates continuity correction for 2 x 2 contingency tables

Question: I would like to gather input from people in the field about the Yates continuity correction for 2 x 2 contingency tables. The Wikipedia article mentions it may adjust too far, and is thus only used in a limited sense. The related post here doesn't offer much further insight.
So to the people who use these tests regularly, what are your thoughts? Is it better to use the correction or not?
And a real world example which would yield different results at the 95% confidence level. Note this was a homework problem, but our class does not deal with the Yates continuity correction at all, so sleep easy knowing you aren't doing my homework for me.
samp <- matrix(c(13, 12, 15, 3), byrow = TRUE, ncol = 2)
colnames(samp) <- c("No", "Yes")
rownames(samp) <- c("Female", "Male")

chisq.test(samp, correct = TRUE)
chisq.test(samp, correct = FALSE)

Answer: Yates' correction results in tests that are more conservative as with Fisher's "exact" tests.
Here is an online tutorial on the use of Yates's continuity correction, by Stefanescu et al, which clearly points to various flaws of systematic correction for continuity (pp. 4-6). Quoting Agresti (CDA 2002), "Yates (1934) mentioned that Fisher suggested the hypergeometric to him for an exact test", which led to the continuity-corrected version of the $\chi^2$. Agresti also indicated that Fisher's test is a good alternative now that computers can do it even for large samples (p. 103). Now, the point is that choosing a test really depends on the question that is asked and the assumptions that are made by each of them (e.g., in the case of the Fisher's test we assume that margins are fixed).
In your case, Fisher test and corrected $\chi^2$ agree and yield $p$-value above 5%. In the case of the ordinary $\chi^2$, if $p$-values are computed using a Monte Carlo approach (see simulate.p.value), then it fails to reach significance too.
Other useful references dealing with small sample size issues and the overuse of Fisher's test, include:

I. Campbell, Chi-squared and Fisher–Irwin tests of two-by-two tables with small sample recommendations, Statistics in Medicine 26(19): 3661–3675, 2007.
Mark G. Haviland, Yates's correction for continuity and the analysis of 2 × 2 contingency tables, Statistics in Medicine 9(4): 363–367, 1990.
Comment: Thanks for the references. I was able to find a "pre-print" version of the Campbell paper [here](http://www.iancampbell.co.uk/twobytwo/files/campbell_twobytwo_preprint.pdf) for those who can't access Pub Med.
Answer: If you have counts low enough that the Yates Correction is a worry (as in your example), you probably should be using Fisher's exact test.  Otherwise, I recommend that after you use the chi-square test on a 2x2 table, you confirm your test with a log odds-ratio z-test.
Comment: Why check against a log odds ratio *z*-test? That's a Wald test, and Wald tests typically perform worse than score tests such as Pearson's chi-squared test. Is this known to be an exception?
Comment: thanks for the information! Fisher's test does seem to be a more robust method for questions like these. I don't think the course I am currently taking will address Fisher's test, but I'll definitely keep it in mind for real world applications.


WYSIWYG edit view of text is below image in content but node view isnt.

Question: In my WYSIWYG edit view ive hit return to make some text apear below an image in the content. However when I save the text is alongside the image, half way down but below.
Why does the edit and node view look different? How can I solve this? Thanks
Answer: It is quite possible that the text format with which your field is being displayed is restrictive i.e. it only allows a subset of HTML tags.
I'm going to assume that you are talking about the Body field or more generally a text field attached to a Content type.

Ensure the field_ui module is enabled and go to the Manage display page for your content type. For the field in question, check if the formatter says Default.
Now, go to Administration > Configuration > Content Authoring > Text formats and inspect the first text format in the list.

If you are going to view as a different user, remember that Text formats are linked to permissions and an user's default text format is the first from the list in 2. that he has access to use.


Importance Sampling derivation

Question: I'm learning about importance sampling from p139 of this book
which has the following derviation:

What I am confused about is the second step in the derivation, though the rest makes sense to me. I understand that you apply Bayes rule and that derive the bottom fraction by conditioning. What I am stuck on is why the initial integral doesn't go over the entire thing? i.e why dont we have
$$ \int \normalsize{\frac{g(x)p(y_{1:T}|x)p(x)}{\int p(y_{1:T}|x)p(x) dx}} dx$$
My only guess is that you can, because $p(y)$ is independent of $x$, do this:
\begin{align*}
\int \normalsize{\frac{g(x)p(y_{1:T}|x)p(x)}{p(y_{1:T})}} dx
&\stackrel{?}{=} \frac{1}{p(y_{1:T})} \int \normalsize{g(x)p(y_{1:T}|x)p(x)} dx \\
&= \normalsize{\frac{\int g(x)p(y_{1:T}|x)p(x) dx}{\int p(y_{1:T}|x)p(x) dx}}
\end{align*}
is this true? If so, it could seem that the follow is true, in general
$$ \int \frac{f(x,y)}{\int g(y|x)p(x) dx} dx = \frac{\int f(x,y) dx}{\int g(y|x)p(x) dx} $$
But its not obvious to me that this is the case. Is there something am I missing or is the final statement true?
Answer: Indeed
$$\int_\mathcal X \frac{f(x,y)}{\underbrace{\int_\mathcal X g(y|x)p(x) \text{d}x}_\text{constant in $x$}} \text{d}x = \frac{\int_\mathcal X f(x,y) \text{d}x}{\int_\mathcal X g(y|x)p(x) \text{d}x}$$
since the integral is a linear operator.

Importance Sampling derivation


Using F-tests for variance in non-normal populations

Question: I'm fairly new to stats, so please excuse me if this problem is hopelessly elementary or misinformed. Basically, I'm wondering if you can help me understand whether I'm using the F-Test for variance correctly.
I have two fairly small populations (n=15), and I'd like to employ a statistical test to determine their respective variances differ significantly. Group A is relatively normally distributed, and group B is skewed left. Originally, I used an F-Test for variance to test for a difference in variance, but then I learned that the F-test can yield false positives if a population isn't normally distributed (as is the case with group B). To remedy this, I created a distribution of sample means for each of the two groups and then performed the F-Test to compare the variance of those distributions. Those distributions of sample means are obviously normal. However, is it appropriate to do an F-Test for variance on those distributions? And how exactly should I interpret the test results?
Just in case it's important, the software I'm using is R and the F-test in R is accessed through var.test()
Comment: Presumably you have two small *samples* of much larger populations or processes, for otherwise applying a statistical test makes no sense. If one of those samples looks strongly skewed, and you believe that implies the population it represents must be skewed, too, then please pause to ponder why you are comparing variances in the first place. You already believe the population distributions differ (in shape) and the variance has a different interpretation for a skewed population than it does for a symmetrical one. What, then, would the point of testing the variances be?
Comment: How exactly did you create "a distribution of sample means for each of the two groups"?
Answer: You started out wanting a test of equality of variances of the raw data, which seems reasonable.  Then you switched to considering variances of means.  No motivation was provided for doing this, and I suspect this step is not logical.
To your original problem, if a distribution is skewed, one could argue that the variance is not an optimum dispersion measure.  What was the motivation for wanting to use variance as the dispersion measure, and what root problem are you trying to solve?  If you are ultimately just wanting to know of observations from population A are bigger than those in B, the Wilcoxon test may be in order.
Comment: What do you think would be an optimal measure of dispersion if a distribution is skewed? (I can ask this as a new question, if that would be better.)
Comment: @Frank_Harrell Thanks for the help! To back up a bit and give some context for this test, I am analyzing how states' past experience with fossil fuel extraction is related to the stringency of their regulations for hydraulic fracturing (fracking). A scatterplot of this relationship shows that states with less experience have a wider range of regulatory stringency (some states have many regulations; others have very few), while experienced states all seem to have a "medium" number of regulations. I wanted to use the F-test to confirm that the pattern I'm eyeballing is statistically significant.
Comment: I see.  You may want to create a customized statistical model for variance or log variance as a function of quantitative past experience.


Multicollinearity and perfect separation in logistic regression: what should I do?

Question: I have a dataset composed of 61 variables a qualitative one y=(0 or 1) and 60 other quantitative variables and 40000 observations.  I want to do logistic regression, Lda, svm, rpart of the model y~.
When I use the vif function of package car it shows multicollinearity. The following is a part of the output:
>  vif(glm(y~., data=mydat,family=binomial))
>>   n1                 direc1            n2        direc2            n3
>>  2.25               1.87               3.28        2.35           3.27
>>  direc3           P1                  P2          P3                 P4
>> 2.49              158.87             190.24        143.28           13.74
>>    P5            P6                   P7             P8                 P9
>>  119.93        212.23               616.43          146.59           169.48

Should I keep only n1 ,direc1, n2 ,direc2 , n3 ,direc3  in the model?
Or there is another solution to the problem ?

In fact, I found this in some internet pages: "perfect separation is related to collinearity."
I want to address both problems. I  read that collinearity between variables gives wrong coefficient estimates in a logistic regression model for example.
And perfect separation gives wrong coefficients estimates also.
Really I am searching the best model prediction for a month but I did not find the good one till now because of these problems!
To solve multicolinearity problem I did a PCA, but I did not know how to use the results obtained in regression. In fact, the image below did not show a separation between the two classes (0 and 1). How can I interpret it? which command in R allows me to get the new variables to use in regressions?
thanks a lot in advance for any help
Comment: It is difficult to see how collinearity can cause perfect separation.  What, then, is the problem you really want to address?  Collinearity or perfect separation?
Comment: also see my question and answer [here](http://stats.stackexchange.com/questions/239928/is-there-any-intuitive-explanation-of-why-logistic-regression-will-not-work-for)
Comment: Possible duplicate of [How to prevent collinearity?](http://stats.stackexchange.com/questions/141499/how-to-prevent-collinearity)
Comment: I don't think it's a dupicate of that one; this is about what to do, not how to prevent.
Comment: @whuber hi, in fact i found this in some internet pages "perfect separation is related to collinearity"
i want to adress both of problems .Iread that collinearity between ariables gives wrong coefficients estimates in a logistic regression model for example . And perfect separation gives wrong coefficients estimates also
really I am searching the best model prediction for a  month  but i did not found the good one till now because of these problems !... It is ambiguous for me Iread plenty of articles bu could not find a solution !
thanks a lot in advance for any help
Comment: Depends what you want from your models. Inference on coefficients/unbiased marginal effects? Or prediction?
Comment: @generic_user in fact i will predict a model for each method then i will choose the best model (the lowest error probability)
Comment: Multicollinearity is only a problem when you're interested in marginal effect estimation.  If pure prediction, don't worry about it.  (more or less)
Comment: hi @generic_user but it causes perfect separation in model ? so what should i do ?
Comment: After having chosen "the best model" by lowest error, do you only want to use it to make predictions?
Comment: @gung y variable take 1 if patient is abnormal and 0 if patient is normal I am searching the  best combination from the 60 quantitative variables  to express y : y=x1+0.5x2-0.39x3..... and i will say that the most x1 increase the most patient is considered abnormal , etc ....
should i make soething more clear ?
Comment: @hxd1011 I've added a link to that page in my answer. I hadn't appreciated previously how L2 regularization could help with separation.
Answer: If you intend to use your model to predict normal/abnormal status in a new set of patients, you might not have to do anything about perfect separation or multicollinearity.
Say that you had one variable that perfectly predicted normal/abnormal status. A model based on that predictor would show perfect separation, but wouldn't you still want to use it?
Your perfect separation, however, might come from the large number of predictor variables, which might make perfect separation almost unavoidable. Then, even if no particular variables are perfectly related to disease state, you will have problems in numerical convergence of your model, and the particular combinations of variables that predict perfectly in this data set might not apply well to a new one. In that case, this page provides concise help on how to proceed. Also, this question and answer by @hxd1011 shows that ridge regression (under the name of "L2 regularization" on that page) can solve the problem of perfect separation.
This page is a good introduction to multicollinearity in the logistic regression context. Multicollinearity poses problems in getting precise estimates of the coefficients corresponding to particular variables. With a set of collinear variables all related to disease status, it's hard to know exactly how much credit each of them should get individually. If you don't care about how much credit to give to each, however, they can work very well together for prediction. In general, you typically lose in predictions if you throw away predictors, even predictors that don't meet individual tests of "statistical significance." Ridge regression tends to treat (and penalize) sets of correlated variables together, providing a principled approach to multicollinearity.
Ridge regression (as provided for example by the glmnet package in R) thus could solve both the perfect-separation and the multicollinearity problems, particularly if your interest is in prediction. This answer shows an example of using glmnet functions for logistic regression. Its example is for LASSO, but you simple set the parameter value alpha=0 for ridge instead. As another example, ISLR starting on page 251 has a worked through case of ridge regression for a standard linear model; specify the parameter family="binomial" instead for logistic regression.
In my experience, however, this type of model in clinical science sometimes isn't used for predicting new cases, but rather to try to argue that certain variables are the ones most closely related to disease status in general. I think that many of the comments on your question were getting at that possibility. The temptation is that the variables included in the "best model" for explaining the present data are then taken to be the most important in general. That can be a dangerous interpretation. Follow the feature-selection tag on this site for extensive discussion.

In an update to your question, you show that the first 2 principal components of your predictor matrix do not separate the 2 groups. That's not so surprising, as these are only the first 2 dimensions of a 60-dimension space, and it's hard to know exactly where among those dimensions the perfect separation arises. I don't think that your PCA helps here at all for variable selection. Ridge regression is the best way to try to proceed. Be warned, however, that if you are looking for p-values you do not get them directly from ridge regression. If p-values are important to you, repeat the process on multiple bootstrap samples of the data.
Comment: hi @EdM     it seems that the two classes are superposed no ? or they are separated in other space but i could not found it no ?
Comment: @prep Trying to do this manually with PCA is unlikely to find the space in which the perfect separation occurs. The separation has to do with the relation of the predictor variables to the disease status, while PCA only considers relations among the predictor variables. So the superposition of classes in PCA dimensions isn't surprising. Ridge regression, or maybe elastic net (uses both L1 and L2 regularization), will probably work best; elastic net does provide some variable selection if you really need to cut down on your number of predictors.
Comment: @prep Also note that ridge regression bears a close relation to principal components regression; principal components regression does an all-or-none selection of which components to include in the final model, while ridge regression down-weights the components with the least variance. See page 79 of [ESLII](http://web.stanford.edu/~hastie/Papers/ESLII.pdf) and the figures and equations noted there. So ridge regression might automatically do what you are trying to do with your PCA plots, in all 60 dimensions at once.
Comment: thanks a lot @EdM so as I understood I should
 1- apply ridge to the data to select the best variables
 2- apply logistic regression, svm, nnet,to the selected variables and choose the best model resulted ?
this is what should I do or not ?
thanks a lot in adavnce for any help
Comment: @prep ridge regression _does not_ select variables; it uses all of them, weighted differently. A ridge logistic regression (provided by `glmnet`) directly provides the types of predictions that you want. An elastic net logistic regression (also available in `glmnet`) provides variable selection, but it might not be wise to use the variables selected that way as the ones to use in svm, nnet, etc.; I don't have much experience with svm or nnet approaches. Validating such a process would require repeating _all steps including variable  selection_ on multiple bootstrap samples.
Comment: thanks a lot for all of these explanations please did you have a reference in which I will find the steps to follow in my case .
In addition when I apply LDA it gives me a message error colinearity.
To sum things up for logistic regression i will apply glmnet
and for the other methods I wil make data samples  for example apply svm to each 0.25 of the data then keep the variables selected in the majority of them

thanks a lot in advance for any help
Comment: @prep added reference links to 5th paragraph of the answer.
Comment: what did you mean by " If p-values are important to you, repeat the process on multiple bootstrap samples of the data." indeed p-values are always important to decide of variables's significance.
as a conclusion I can ran the normal model by keeping "error multicolinearity" and "fitted probability numerically 0 or 1 occurred" ? with no worry about coefficients significance ?
thanks a lot in adavnce
Comment: You do need to validate your model building process, but p-values aren't always helpful if your emphasis is on prediction rather than hypothesis testing. See [this answer](http://stats.stackexchange.com/a/17596/28500) for example. For prediction, keeping multicollinear and "insignificant" predictors in the model can improve performance, particularly with an approach like ridge regression.
Comment: what about using logistf or bayesglm they are solutions to perfect separation , which one is good ? thank you in advance.
Comment: I don't have any direct experience with either logistf or bayesglm. The penalization in logistf helps solve the separation issue, but not the multicollinearity. Logistic ridge regression, as with the glmnet package, might solve both your issues at one, as it both penalizes and treats collinear predictors together.
Comment: hi thank you , there is no function that combines firth logistic regression with ridge parameter in r ? what about brglm ?in fact i used it the message fitted probabilities disappear but the odd ratios are too high ? !   I found this article talking about the problem but I don't know wich function he used in addition he used SAS not R?jds-online.com/files/JDS-395.pdf Thanks a lot in advance for any help
Comment: Ridge regression on its own may solve the separation problem, as in the answer from hxd1011 linked from my own answer. Similarly, see the `pordlogist` function in the R `plotOrdinalVariable` package, which uses a ridge penalty to handle separation in a more general multi-category outcome situation.
Comment: hi EdM i used the pordlogist function but it gives me the message error:
"Error in pordlogistfit(y, x, penalization = penalization, tol = tol, maxiter = maxiter,  :
  There is a variable with the same value for all the items. Revise the data set"
in order to revise the data set I used the function anyDeplicated(x) and it gives me 0
what should i do please ?
thanks a lot in advance for any help
Comment: The default of anyDuplicated reports reports duplicated rows (cases); the error message instead notes a column (predictor variable) that only has a single value. Look at the output of (summary) applied to a data frame of your predictor matrix (which should be an initial step in any modeling of this type), or use anyDuplicated on an array of your predictors with MARGIN=2 for columns. And have you simply tried to do a logistic ridge regression with glmnet? It's not clear that pordlogistf does much more for you than ridge when you have a dichotomous outcome variable.
Comment: hi yes i did a glmnet but i can't get p-values in fact after each method applied i should say glmnet method gives that x1+x2+... best predict y or I don't have p-values to say that !
what should we conclude from the summary of the data ? can we exclude some of variables ? thanks a lot in advance for any help
Comment: For making predictions you don't need p-values. If you want p-values, one approach is to repeat the ridge regression on multiple (many hundred) bootstrap samples of your data, determine the standard deviation of each regression coefficient among the analyses of the bootstraps, and do a z-test on each coefficient based on its standard deviation. Do NOT, however, throw out the predictors that are "not significant" as all of them contribute to the quality of the predictive model.
Comment: See [this answer](http://stats.stackexchange.com/a/171462/28500) for a more detailed description of the limitations of p-values in this context, and some links for further study.
Comment: thank you and for summay(data) ?


ArrayList and Array as parameters for Constructor

Question: I'm doing an assignment for my Computer Science class, but I got stuck in this one part. I am supposed to code the constructor for the given main method and instance variables. The constructor needs to have an ArrayList and an Array as parameters, but I'm not entirely sure how to.
The instructions and main method is as follows:

Use the comments and the code provided to complete the constructor

import java.util.ArrayList;
class RectangleStats
 {
 //instance variables go here

 //The constructor for the RectangleStats class takes an ArrayList and an array as parameters for

 // width and length, respectively.

 // code for constructor goes here
 // The RectangleStatsTesterClass assigns the width of two rectangles to an ArrayList and assigns the
 // length of two rectangles to an array. The calcRectangleArea() and printArea() methods are invoked to
 // calculate and print the area of the two rectangles.
 public class RectangleStatsTesterClass
 {
       public static void main(String[] args)

 {

       ArrayList dblWidth = new ArrayList();

       dblWidth.add(5.2);

       dblWidth.add(9.3);

       double [ ] dblLength = {11.1, 4.7};

       RectangleStats rectStats = new RectangleStats(dblWidth, dblLength);

       rectStats.calcRectangleArea();

       rectStats.printArea();

       }

  }

The constructor I currently have is this:
     public RectangleStats(double w, double l)
     {
         width = w;
         length = l;
         area = 0;
     }
but im not sure if its right or not.. Help?
Comment: where is your constructor ?
Comment: well thats what I gotta figure out.. I wrote down what I thought the constructor should be but im not so sure..
Answer: If the actual parameters you pass are an ArrayList and array respectively, then your formal parameters in your method signature should also be the same. That is:
public RectangleStats(ArrayList<Double> arg0, double[] arg1)
You would then assign arg0 and arg1 (or whatever your identifiers are) to your instance variables.
That being said, the entire idea behind the implementation is very poor (eg: using an ArrayList and an array rather than one of either).
Also, when you declare your ArrayList, do this as follows:
ArrayList<Double> dblWidth = new ArrayList<Double>();
It's almost always better to specify the class of the objects that you're going to be adding.
Comment: thanks for the answer

Yeah i guess the implementation of using them isn't all that great, but unfortunately I'm not the one making the questions :/
Comment: No problem. I understand this is homework and you can't change your teacher's question, but in case you're interested, it would make more sense to have a `Rectangle` class with a `width` and `length` parameter - then you could simply declare an array holding 2 of them, rather having an object such as the one in your question to hold two rectangle widths and two lengths (which makes much less sense).
Answer: With a good java book or tutorial you might have easily solved your problem but this is how you'd do it:
ArrayList<Double> w;
double []length;
public RectangleStats(ArrayList<Double> width, double length[])
 {
     this.width = width;
     this.length = length;

 {

I'm guessing these variables are used outside the constructor hence the initialization.


How can I add a normal distribution curve into geom_density?

Question: I want to add a normal distribution curve in to my density curve to compare between them.
heights %>% filter(sex == "Male") %>% ggplot() +
  aes(x = height) +
  geom_density(fill = "blue")

What should I do after this?
I want it to be seen like this when it's done:
Answer: You could use stat_function:
heights %>% filter(sex == "Male") %>% ggplot() +
  aes(x = height) +
  geom_density(fill = "blue") +
  stat_function(fun = dnorm, n = 101, size = 1.5, args = list(mean = mean(heights$height[heights$sex == "Male"]), sd = sd(heights$height[heights$sex == "Male"])))


Accessing all taxonomy terms from one specific vocabulary through url

Question: I want do display all my terms from one vocabulary over drupals pathauto url scheme.
So f.e. I have example.com/taxonomy/term/314 or after pathauto: example.com/screwdriver/slot or example.com/screwdriver/torx. This works good.
But if I visit example.com/screwdriver, this page doesn't exist.
Actually I'm kind of puzzled, that this is not working out of the box. Do I really have to implement a custom module for this?
Answer: Is there a functionality made for that particular page? Is that a term namde, a node or a view? Any kind of entity? Because if it is not, nothing will show up, obviously. Create a view page showing all taxonomy terms within that vocabulary. Taxonomy pages are for taxonomy terms only, not for taxonomy vocabulary based on some url scheme.
Comment: Is there really no possibility to build this functionality without using the super fat views module?
Comment: You need content on that page. A piece of content is an entity that can be a views page, a node, a term, a comment or a user. There are ways to add content programatically, but you need to write your own code.
Other than that, I dont see Views super fat. With proper customization, it works great, loads really fast and it offers limitless possibilities.


Convert many raw strings to Pathlib Path

Question: So I have many raw strings to different files, and I'd like my script to automatically convert them to Path's using Pathlib. The obvious solution is:
from Pathlib import Path

path1 = r'/path/to/my/file1'
path2 = r'/path/to/my/file2'

path1 = Path(path1)
path2 = Path(path2)

But this becomes quite unreadable once there are many different paths involved. I was thinking about putting the variables in a list, and then loop over this list and converting them. Something like this:
path_list = [path1, path2]

for raw_path in path_list:
   variable = Path(raw_path)

Obviously this doesn't work, as variable in the code above needs to be path1 in the 1st iteration and path2 in the 2nd iteration.
Do you have any tips how to solve this in the most Pythonic way?
Answer: Since you have a list of paths, you should deal with them all at once inside a Python list:
raw_path_list = [path1, path2]

path_list = [Path(raw_path) for raw_path in raw_path_list]

# access path1
path_list[0]

If you actually need the path1 terminology to hold, use a dict instead:
path_list = {f'path{i}':Path(raw_path) for i, raw_path in enumerate(raw_path_list)}

# access path1
path_list['path1']


Normalizing each column in R

Question: Normalize each column.
the data starts off with the first row having x and y then continues with numbers and i believe that is the problem.
> normalize <- function(number) {
+ return ((number - min(number)) / (max(number) - min(number)))
+ }
> data$V1_norm<-normalize(data$V1)
> data$V2_norm<-normalize(data$V2)

> data$V1_norm<-normalize(data$V1)
Error in Summary.factor(c(69L, 1L, 22L, 9L, 29L, 44L, 65L, 21L, 61L, 4L,  :
  'min' not meaningful for factors

> data$V2_norm<-normalize(data$V2)
Error in Summary.factor(c(53L, 27L, 46L, 36L, 33L, 41L, 8L, 15L, 12L,  :
  'min' not meaningful for factors

Comment: You have unordered `factor` variables, so you can't take a min or max. You need to convert to a numeric or integer first. At the moment it is like literally deciding which is less, "cat" or "apple"?
Comment: Having a string like `"x"` in the first row of a column of numbers is often a sign that you should have read the data in with a setting like `header = TRUE`.
Answer: The code seems fine. I believe your problem is in the V1_norm and V2_norm in the data object. Both variables are factors.
normalize <- function(number) {
    return ((number - min(number)) / (max(number) - min(number)))}

data$V1_norm <- normalize(data$V1)
data$V2_norm <- normalize(data$V2)

data <- data.frame(V1_norm = c(6,1,10,2), V2_norm = c(10,20,100,40))

normalize(data$V1) # returns 0.5555556 0.0000000 1.0000000 0.1111111
normalize(data$V2) # returns 0.0000000 0.1111111 1.0000000 0.3333333

Try this in your dataset:
class(data$V1)
class(data$V2)

Be careful with functions such as as.numeric(),you should double check where is the inconsistency in your data.

Normalizing each column in R


How to install pear mail package on php app deployed on heroku?

Question: I have a php app which is deployed on Heroku.  How do I get the PEAR mail package to work for my app?  I see two main options:
1) install pear mail package on heroku
2) Bundle the package into my app before deployment
Unfortunately I'm more of a rails guy so not sure how to do either of these (or which one is right).
Thanks,
Antony
Comment: This solution to this question would also work for me (though I'm looking for a more general solution): http://stackoverflow.com/questions/13041733/how-to-make-a-simple-php-app-send-emails-from-heroku-cedar-stack
Answer: OK, I finally figured this out myself.  Here's what I did:
1.You need to use a custom buildpack which installs the pear packages mail and Net_SMTP.  You can use the one I created by running the following command:
heroku config:add BUILDPACK_URL=https://github.com/antonyevans/heroku-buildpack-php.git

The key changes are the addition of the lines:
php/bin/pear install mail
php/bin/pear install Net_SMTP

Into bin/compile.
2.Then you need to tell your application to load the mail package:
require_once 'Mail.php';

3.Finally heroku blocks the mail port so you need to configure to use an external mail server.  For example if you have added the SendGrid addon ('heroku addons:add sendgrid:starter') then you could use the following:
$wgSMTP = array(
    'host' => 'tls://smtp.sendgrid.net',
    'IDHost' => 'heroku.com',
    'port' => 587,
    'username' => getenv("SENDGRID_USERNAME"),
    'password' => getenv("SENDGRID_PASSWORD"),
    'auth' => true
 );

Comment: TechnoTony's answer is correct. But buildpack he posted is outdated. I followed his advice and modified the current one. I've placed it for use at `https://github.com/blindstuff/heroku-buildpack-php.git`


Why cant Office 365 find my CName record for autodiscover?

Question: The DNS is at Cloudflare.
The setup wizard gave me the choice
1) Set up my online services for me. (Recommended)
Next, you'll update your name server DNS records to give Office 365 permission to set up the rest of your online services.
2) I'll manage my own DNS records.
We will provide a list of DNS records that you will need to add for your domain at your DNS hosting provider.

I selected 2 because I am also using mailgun.
I have added 3 DNS records as instructed by advice at portal.office.com
The MX and TXT records verify quickly when I add them
However the CName record does not verify
I get the message
Some DNS records are missing. Please make sure you have added them correctly

for host name autodiscover with value autodiscover.outlook.com
The record shows at Cloudflare DNS as

What could be wrong?
Comment: The office 365 wizard in the admin ssection will give the detailed info on why it display the error. Please check the detailed log
Comment: @yagmoth555 Can you tell me more on how I can find this log?
Answer: I believe the issue here is that you have enabled the Cloudflare CDN services for this record (the orange cloud button on the right in your screenshot).
This causes the address record (A/AAAA) or CNAME record you entered to be used as internal configuration for the Cloudflare CDN (ie, configuration for their HTTP(S) proxies and whatnot), while the records published in DNS are plain address records pointing to the Cloudflare CDN.
This is almost certainly not what you want for Outlook autodiscover services (surely not what you want for that validation to be happy), and simply disabling the CDN services should do the trick (same "cloud button" mentioned earlier toggles CDN services).


Javascript getTime() returns different time than time object itself

Question: Javascript getTime() returns different time than time object itself.
function doTime(){
  var data = [];
  var newDate = new Date('2020-01-01');

  for (var i = 0; i < 10; i++) {
    newDate.setTime(newDate.getTime() + (1000 * 60 * 60));
    console.log(newDate);                //<<----these times are not the same for some reason, why?
    data.push({'date': newDate});        //<<----these times are not the same for some reason, why?
    }
  console.log(data);
  }

And the output!!
Wed Jan 01 2020 03:00:00 GMT+0200 (Eastern European Standard Time)
Wed Jan 01 2020 04:00:00 GMT+0200 (Eastern European Standard Time)
Wed Jan 01 2020 05:00:00 GMT+0200 (Eastern European Standard Time)
Wed Jan 01 2020 06:00:00 GMT+0200 (Eastern European Standard Time)
Wed Jan 01 2020 07:00:00 GMT+0200 (Eastern European Standard Time)
................

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {date: Wed Jan 01 2020 12:00:00 GMT+0200 (Eastern European Standard Time)}
1: {date: Wed Jan 01 2020 12:00:00 GMT+0200 (Eastern European Standard Time)}
2: {date: Wed Jan 01 2020 12:00:00 GMT+0200 (Eastern European Standard Time)}
3: {date: Wed Jan 01 2020 12:00:00 GMT+0200 (Eastern European Standard Time)}
4: {date: Wed Jan 01 2020 12:00:00 GMT+0200 (Eastern European Standard Time)}
................

btw I already solved this problem with a slightly different code, but can someone please explain why is this happening?
Comment: The array has 5 references to the same Date object: `data[0] === data[1] === data[2] ...`. Use `data.push({'date': new Date(newDate)})`.
Answer: You aren't creating a new reference in your loop, so you are mutating the same newDate each time. You are also calling console.log so at that time, it's converted to its console format.


File modifications lagging behind

Question: We are having a weird issue where file creation/removal from a NFS client takes a very long time to propagate out to the other client(s).
Mount options we have on the clients are:
defaults,rsize=32768,wsize=32768,intr,noatime,cto

The export has:
*(rw,sync,no_root_squash,no_wdelay)

We verify this by on one client doing:
watch -n0.1 stat foofile

And on another client we touch foofile (or rm it). Modifications take 1+ seconds to propagate out.
cto and no_wdelay are options we've just recently added to see if they solve the issue (they dont). What else can we look at?
Comment: disabling lookupcache setting `lookupcache=none` mount option might be of interest (see http://serverfault.com/q/380303/117245)
Comment: try with mount options acdirmin= and acdirmax= to control times client caches directory attributes.
Answer: I'm not going to answer your question directly.
NFS clients are not guaranteed to see updates very fast.  Yes, you can tune parameters to control the delay but the result will be worse caching (and therefore terrible performance) on the clients.
Usually when I find myself needing NFS clients to see changes faster I take a step back and ask, "ok, what am I really trying to achieve here?"   Usually I'll realize there is some higher level of abstraction that lets me solve the same problem a very different way.   For example, sometimes I realize I'm trying to use NFS files as a poor-man's RPC or lock mechanism.  There are much better ways to do either of those things.
NFS is good for a single client accessing a particular directory.  Outside of that if you have a problem and are going to fix it with NFS, then you have two problems.
I love NFS but it is limited in where I try to use it.
Answer: It is by no means certain to be your problem, but I've sometimes found coherency issues with NFS where the clients and server do not all have rigidly synced clocks.  Are all systems concerned sync'ed to NTP?

File modifications lagging behind


jquery mobile populate select box from another but clear selection first

Question: I have two select boxes and second gets updated with GET ajax on first select box selection.
It works cool, but...
If a person made a selection in the fist field (c) it updates field (s) correctly.
When a person made a selection in field (s) but then decide to select another option in (c), field (s) gets updated, but the previous selection of (s) do not clear, even that when it actually does update the options of (s)
What is missing in my code to clear the (s) field when new options are loaded?
    <form data-parsley-validate method="post" action="#post1" data-ajax="false"  data-theme="<?=$theme?>">

Select Category:
<select name="c" id="c" data-mini="true" placeholder="Select Category" required>
<option value="">Select Category</option>
<?
$ssql="SELECT * FROM cat";
$sresult=mysql_query($ssql);
while($srows = mysql_fetch_array($sresult)){
$cat_id = $srows['id'];
$cat_name = $srows['name'];
echo '<option value="'.$cat_id.'">'.$cat_name.'</option>';
}
?>
</select>

Select Subcategory:
<select name="s" id="s" data-mini="true" placeholder="Select Subcategory" required>
<option value="">Select Subcategory</option>
</select>

<input type="submit" value="Send" name="submit" data-theme="butt">
</form>

Answer: First off, you should not be using .ready() in jQuery Mobile, instead use "Page Events" to add listeners.
To clear selection of an existing selectmenu, you should mark first option as selected and then refresh it.
$(document).on("pagecreate", "#pageID", function () {
  $("#c").selectmenu("refresh");
  $("#c").on("change", function () {
    $("#s").load("getsub.php?c="+ $("#c").val(), function () {
      $("#s option:eq(0)").prop("selected", true); /* select first option */
      $("#s").selectmenu("refresh"); /* refresh */
    });
  });
});


Add commas between allowed file extensions

Question: I need to restrict the user to upload only the file extensions that i mentioned in the Allowed file extensions filed. It works properly. But in front-end, i need to add a comma separator for this format.
Actual - Allowed file types: ai aif avi bmp csv doc docx eps fla flv gif img.
Expected - Allowed file types: ai, aif, avi, bmp, csv, doc, docx, eps, fla, flv, gif, img
Answer: You are definitely allowed to separate file types by comma in the admin are

For user area, find the ID of the form (you can find this in the firebug) then
function YourTheme_form_alter(&$form, &$form_state, $form_id){
      if($form_id == "TheFormID"){
            kpr($form);
      }
}

In this way you can find the form elements. get the Key of image field in the form array, if it is image then write
function YourTheme_form_alter(&$form, &$form_state, $form_id){
      if($form_id == "TheFormID"){
            $form['image']['#description'] = t('File types are comma separated')
      }
}

This is the general idea but a few modification might be needed
Comment: This is back-end (admin area). I need to add comma in front-end (where the user uploads their files).
Comment: I need to add comma separation here.  [1]: http://i.stack.imgur.com/B41dP.png
Comment: My code does this, it finds the ID of the form and alters the `description`
Answer: I used theme_file_upload_help hook.
 function bartik_file_upload_help($variables) {

 $description = $variables ['description'];

 $upload_validators = $variables ['upload_validators'];

 $descriptions = array();

  if (strlen($description)) {

     $descriptions [] = $description;
    }

  if (isset($upload_validators ['file_validate_size'])) {

     $descriptions [] = t('Files must be less than !size.', array('!size' => '' . format_size($upload_validators ['file_validate_size'][0]) . ''));

 }

 $a = explode(" ", $upload_validators ['file_validate_extensions'][0] );
  if (isset($upload_validators ['file_validate_extensions'])) {

$descriptions [] = t('Allowed file types: !extensions.', array('!extensions' => '' . check_plain(implode(", ", $a)) . ''));

 }

return implode('
', $descriptions);

}


Has a horse ever crossed the finish line first without a jockey?

Question: At the 2019 Preakness Stakes, jockey John Velazquez fell off his horse Bodexpress at the start of the race, but Bodexpress continued to finish the race.  Officially Bodexpress received a "DNF" (Did Not Finish) because the rule is that a horse cannot finish without a jockey.
But has there ever been a race in which a horse without a jockey was the first to cross the finish line?
Answer: Luigi Bruschelli has won 13 palios since 1996, one of which his horse finished first without him.


Twython 403 forbidden error

Question: I am trying to do two things:
1) stream data from twitter
2) update my status based on that data
Streaming works fine but every time I try to update my status, I get the a 403 error, forbidden.
I get the same response if I use:
twitter.verify_credentials()

This is the error message I receive:
twython.exceptions.TwythonError: Twitter API returned a 403 (Forbidden), Your credentials do not     allow access to this resource.

Answer: You need to show more of your code so I can see what could be wrong. But my guess is that you maybe making the same error I did. To update your status you do not need to do this:
twitter.verify_credentials()

You can use the Access Key and secret from your twitter application to update your own account. But more code will help figure this out.

Twython 403 forbidden error


Confusion with Euler-Lagrange Derivation

Question: This is mostly a re-hash of this thread, but it did not receive an adequate answer. In the derivation that I am reading, there is one step that is not justified. Perhaps obvious, but it is not clear to me. Here is the build-up:
Suppose we have the following (one-dimensional) integral that we want to minimize by finding a suitable function $Y(x)$: $\int_{x_1}^{x_2} f(Y(x),Y'(x),x) dx$. Assume we already know the function that minimizes this to be $y(x)$. Define $Y(x,\alpha) = y(x)+\alpha \eta(x)$ and require $\eta(x)$ to be a differentiable function with $\eta(x_1) = \eta(x_2) = 0$ so that this is a suitable path between the points of interests, and assume $\alpha$ is real. From this, define
$$S(\alpha) = \int_{x_1}^{x_2} f(Y(x,\alpha),Y'(x,\alpha),x) dx$$
It is clear that the minimum path is simply $Y(x,0)$, which implies that $\frac{\partial S}{\partial \alpha}\Big|_{\alpha = 0} = 0$. Now, here is where my confusion is:
With no dependence on $\alpha$ for the bounds, we know that
$$\frac{\partial S}{\partial \alpha}\Big|_{\alpha = 0} = \Big[\int_{x_1}^{x_2} \frac{\partial f}{\partial \alpha} dx \Big]_{\alpha = 0} = \Big[\int_{x_1}^{x_2} \Big(\eta(x)\frac{\partial f}{\partial Y}+\eta'(x)\frac{\partial f}{\partial Y'}\Big) dx\Big]_{\alpha = 0} = 0$$
and my author, without justification, immediately makes the jump to
$$\Big[\int_{x_1}^{x_2} \Big(\eta(x)\frac{\partial f}{\partial Y}+\eta'(x)\frac{\partial f}{\partial Y'}\Big) dx\Big]_{\alpha = 0} = \int_{x_1}^{x_2} \Big(\eta(x)\frac{\partial f}{\partial y}+\eta'(x)\frac{\partial f}{\partial y'}\Big) dx$$
Perhaps in clearer form, the following step is taken:
$$ \frac{\partial f(y+\alpha \eta,y'+\alpha \eta',x)}{\partial \alpha} = \eta \frac{\partial f}{\partial y} + \eta'\frac{\partial f}{\partial y'}$$
Can someone provide justification for this step? My intuition tells me that
$$\frac{\partial f}{\partial Y(x,\alpha)}\Big|_{\alpha = 0} = \frac{\partial f}{\partial Y(x,0)} = \frac{\partial f}{\partial y(x)} $$
but some essential part of that just feels wrong. It reminds me of the silly mistake that a careless calculus student can make, saying that
$$\frac{\partial f(x,y)}{\partial x}\Big|_{(x,y) = (x_o,y_o)} = \frac{\partial f(x_o,y_o)}{\partial x}$$
Which is clearly incorrect by noting that $f(x_o,y_o)$ is simply some constant. In the above case I think the issue is much subtler.
Comment: That is the chain rule.
Comment: Your problem is the part, where the $\partial Y$ and $\partial Y'$ are replaced by $\partial y$ and $\partial y'$? The line above can be taken as known?
Comment: Indeed, take the line above as known, as you can work out by bringing in the partial derivative and applying the chain rule.
Comment: the intuitive step is wrong because $f$ is a function of both $Y$ and $Y'$ which are technically independent variables here. Moreover, both depend on $\alpha$ so ignoring the $Y'$ does matter.
Answer: $$ \frac{\partial f(y+\alpha \eta,y'+\alpha \eta',x)}{\partial \alpha} = \eta \frac{\partial f}{\partial y} + \eta'\frac{\partial f}{\partial y'}$$
Why? Well, if $f$ is a function with arguments $f(Y,Y',x)$ and $Y,Y',x$ are thought of as functions of $\alpha$ then the chain-rule is:
$$ \frac{\partial}{\partial \alpha}f(Y,Y',x) =
\frac{\partial f}{\partial Y}\frac{\partial Y}{\partial \alpha}+
\frac{\partial f}{\partial Y'}\frac{\partial Y'}{\partial \alpha}+
\frac{\partial f}{\partial x}\frac{\partial x}{\partial \alpha}.$$
But, for you variation about the optimal path $y$,
$$ \frac{\partial Y}{\partial \alpha} = \eta \qquad \& \qquad \frac{\partial Y'}{\partial \alpha} = \eta'$$
whereas $\frac{\partial x}{\partial \alpha}=0$. Finally, setting $\alpha =0$ changes the evaluation of those partials from arbitrary $Y$ to $y$ and writing the partial derivative w.r.t. $y$ merely expresses that evaluation at the optimal path.
Comment: Thanks for the response. Yes, I understand how the expression with $Y$ is generated with the chain rule. It's the switch from $\partial Y$ to $\partial y$ in the denominator that I wanted justification for (your very last comment). Sure, it may seem obvious that along the optimal path that $\partial Y$ = $\partial y$ since we are holding $\alpha$ constant at zero. For me, though, this lacks rigor, and kind of masks the somewhat clear calculus that I imagine goes into it's justification.
Comment: This is a standard notation, we write $df/du$ to denote $f'(u)$. To be explicit, we could write $(df/dx)(u)$, but $df/du$ compactly expresses the derivative evaluated at $u$. It is the same with $Y$ verses $y$.


slurm AllocMem always 0

Question: When running jobs with --mem or --mem-per-cpu the AllocMem stays 0 when the job is running.
# scontrol -V
slurm 2.6.5
# sbatch --mem=2500 tmp.sh
Submitted batch job 25
# squeue
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
                25      main   tmp.sh     root  R       0:05      1 w1
# scontrol -o show node w1 | awk '{ print $1, $13, $14}'
NodeName=w1 RealMemory=3764 AllocMem=0

Why is that the case?
I would expect there to be a number corresponding to my mem requirement.
Answer: You have to specify that you want to use Memory as a Consumable Resource.
Check in the Slurm config file that you have SelectTypeParameters=CR_CPU_Memory or something stating Memory and that the select plugin that you are using is the SelectType=select/cons_res.

slurm AllocMem always 0


Probability of at least N events occuring

Question: I have a series of N events, each with its own probability of occurring. How would I calculate the probability that at least M of the N events actually do occur?
I think this is conditional, in that getting at least M occurrences depends on getting at least M-1 occurrences. Past that I'm getting stuck.
Comment: Unfortunately although you can calculate it there won't be an easy formula.  You just have to multiply the probabilities of every possible combination of more than $M$ events and add them up.
Comment: What do you know about your events? If they're independent you can calculate the probability exactly. If not you can only estimate it.
Comment: @Tim: I guess we have a silent assumption here about independence, sort of like an extension of Binomial distribution
Answer: For any future visitors, the expression given for the probability of at least $m$ out of $n$ events $(A_1, \ldots ,A_n)$ in An Introduction to Probability by William Feller is this:
$$P_m = S_m - \binom{m}{1}S_{m+1}+\binom{m+1}{2}S_{m+2}-\ldots\pm\binom{n-1}{m-1}S_n$$
where $$\displaystyle S_k = \sum_{1\leq i_1< i_2\ldots< i_k\leq n}P(A_{i_1}\cap A_{i_2}\cap\ldots \cap A_{i_k})$$
It's certainly not pretty, but it is general.
It is proved by finding the expression for exactly $m$ events and then adding the expressions from $m$ to all $n$ using induction.
Comment: The summation notation means to add all terms with particular values of $i_1, i_2, ..., i_k$ which satisfy the inequalities at the bottom. So if $n=3$ and $k=2$,
$$S_2 = P(A_1\cap A_2) + P(A_2\cap A_3) + P(A_1\cap A_3),$$
because the only values for $(i_1, i_2)$ that satisfy the inequalities are $(1,2)$, $(2,3)$ and $(1,3)$.
Comment: Hi, how to you expand the summation? $$\displaystyle S_k = \sum_{1\leq i_1< i_2\ldots< i_k\leq n}P(A_{i_1}\cap A_{i_2}\cap\ldots \cap A_{i_k})$$ Can you do a simple example expansion of where m=2 and n=3. Or provide me with some key words to find another thread. I have just never seen a summation with all those greater than signs and no value at the top.
Answer: Let us consider that we have four independent events A,B,C,D each with probability Pa,Pb,Pc,Pd associated with it.
Now we have to find the probability of at least two events occurring.
First we find the probability of exactly two events occurring.
These two events can be AB, AC, AD, BC, BD, CD.
event space = {AB, AC, AD, BC, BD, CD, }
Since all these events are mutually exclusive.
Probability of any one them occurring is Sum of all probabilities in the events space.
P(AB) = Pa * Pb * (1-Pc) * (1-Pd)
P(AC) = Pa * (1-Pb) * Pc * (1-Pd)
P(AD) = Pa * (1-Pb) * (1-Pc) * Pd
P(BC) = (1-Pa) * Pb * Pc * (1-Pd)
P(BD) = (1-Pa) * Pb * (1-Pc) * Pd
P(CD) = (1-Pa) * (1-Pb) * Pc * Pd
After that we can sum all these to get the probability of Two events occurring. In a similar way we can calculate probability of Three and Four events occurring.
Final probability = Prob(Two_events) + Prob(Three_events) + Prob(Four_events)
However I am unable to generalize for at least M events out of N.
I hope it will shed more light on your problem.
Comment: @Alex that is why this is so hard to generalize
Comment: @Corv: if you solved it, you can write your solution as an answer to your own question, so we all can know it.
Comment: That's where the fun starts, because you don't care which $k$ events happen, so if say $k=2$ you need to account for $\binom{n}{2}$ ways of doing so
Comment: That's precisely the description I needed and exactly the horrifying complexity I hoped it wouldn't lead to. Here's hoping I can code up a decent calculator!
Comment: Nasty as all hell, but I got my numbers crunched. Thanks, everyone.
Answer: Per Alex's suggestion, here's the Lua script I used to solve my problem. It does take a long while, and don't ignore the optimization suggestion if it's relevant!
<code>--[[
    Lua script to calculate the odds that N of M events, each with its own probability of occuring, actually occur.

    numEvents: M, the total number of events in the chain
    sucessChances: the probabilities of each event, in order
    targetNumSuccesses: N, the minimum number of successes we'd like to see

    If targetNumSuccesses is less than half of numEvents, this could be optimized by finding event chains with exactly numEvent successes, rather than failures.
    The optimization would probably save you several lengthy iterations, especially if targetNumSuccesses is very small.
    This is left as an exercise for the reader.
--]]

local numEvents = 30
local targetNumSuccesses = 16
local successChances = {}
for i=1, numEvents do
    -- Insert code to calculate your event probabilities here!
    local someValue

    successChances[i] = someValue
end

-- Establish a failure table of all events failing to occur
local failureTable = {}
local function resetFailureTable()
    for i=1, numEvents do
        failureTable[i] = false
    end
end

-- This is a helper function to work through the possibile permutations of events.
local function reverseSubList(list, front, back)
    if front > back then
        front, back = back, front
    end

    local swaps = (back - front)/2
    for i = 0, swaps do
        list[front+i], list[back-i] = list[back-i], list[front+i]
    end
end

local numEventProbability = 0
local totalProbability = 0
local exhausted = false

--[[
    Start iterating through the possible series of events.

    The outer loop creates an event chain with exactly numEvent *failures*.
    The inner loop:
        calculates the probability of that event chain
        adds it to a running total
        finds the next event chain with the same number of failures
    Once all chains with exactly numEvent failures have been calculated:
        numEvent is "exhausted"
        the total probability of all chains with exactly numEvent failures is printed
        that value is added to the running total probability
--]]
for numEvents = 1,(numEvents - targetNumSuccesses) do
    -- Fill the failureTable with an appropriate number of hits.
    resetFailureTable()
    for i = 1, numEvents do
        failureTable[i] = true
    end

    -- This is already the base permutation of the failureTable, so:
    while not exhausted do
        local eventChainProbability = 1
        for subEvent = 1, numEvents do
            if failureTable[subEvent] then
                eventChainProbability = eventChainProbability*(1-successChances[subEvent])
            else
                eventChainProbability = eventChainProbability*(successChances[subEvent])
            end
        end
        numEventProbability = numEventProbability + eventChainProbability

        -- Get the next permutation of the failureTable
        for i = 30,1,-1 do
            if failureTable[i] == true and failureTable[i+1] == false then
                local rightmost
                for j=30,1,-1 do if failureTable[j] == false then rightmost = j break end end
                failureTable[i] = false
                failureTable[rightmost] = true
                reverseSubList(failureTable, i+1, 30)
                break
            elseif i == 1 then
                exhausted = true
            end
        end
    end
    exhausted = false
    print(string.format("Odds of being exactly %d failures: %.2f", numEvents, numEventProbability))
    totalProbability = totalProbability + numEventProbability
    numEventProbability = 0
end

print("Odds of at least %d successes: %f", targetNumSuccesses, 1-totalProbability)
local a = io.read()
</code>
Answer: For anyone who finds this question - like I did - and wants to learn more about the problem:
If the $N$ events are independent and non-identical (as suggested by "each with its own probability of occurring" from the question), then the probability of getting $M$ or more successes follows a Poisson-binomial distribution. The answer given by zer0cube is correct and Milind Hegde posted the general case. A closed-form variation of the Probability Mass Function (PMF) - the probability that exactly $M$ trials occur - was found by Fernandez & Williams (2010) (and can be found on the linked wiki page). To find the probability that $M$ or more events occur, then you would sum the PMF for $k$ successes from $k = M, M+1, \dots N$.