Qual a diferença entre SET e ENUM no MySQL?

Question: Quais as diferenças entre SET e ENUM no MySQL? E em quais situações ambas são aplicadas da melhor maneira?
Answer: SET e ENUM são usados quando os valores a serem armazenados são escolhidos de um conjunto fixo de valores. Você define colunas de ambos os tipos em termos de valores de strings, mas o MySQL os representa internamente como valores inteiros. Isto leva a um armazenamento muito eficiente, mas pode ter alguns resultados surpreendentes, a menos que você tenha em mente esta dualidade string/inteiro.
ENUM
O tipo ENUM é um tipo de enumeração. Uma definição de coluna deste tipo inclui uma lista de valores permitidos, cada um dos quais sendo chamado de "membro" da lista. Cada valor armazenado na coluna deve ser igual a um dos valores da lista.
ENUM ('Ásia', 'Europa', 'USA', África', 'Oceania', Antártida', 'America do Sul')
Os valores na definição do tipo ENUM são dados na forma de uma lista de strings entre aspas separadas por virgulas. Internamente, o MySQL armazena as strings como números inteiros, usando os valores 1 a n para uma coluna com n membros na enumeração. A declaração insert into paises(nome,continente) values('Kenya','Africa'); atribue o valor 'Africa' da enumeração à coluna do continente. O MySQL na realidade atribui o valor 4, porque 'Africa' é o quarto nome de continente listado na definição de enumeração.
O MySQL reserva o valor 0 como um membro implícito de todas as colunas do tipo ENUM. Por exemplo, se você atribuir "E.U.A' na coluna dos continentes, o MySQL irá armazenar o valor 0, em vez de algum valor de 1 a 7, porque 'E.U.A' não é um membro válido da enumeração. Se você selecionar a coluna mais tarde, o MySQL exibe valores 0 como '' (a string vazia).
SET
O tipo de dados SET, como o ENUM, é declarado usando-se uma lista de strings entre aspas separadas por virgulas que definem seus membros válidos. Contudo, diferentemente de ENUM, uma deterinada coluna do tipo SET pode receber a atribuição de um valor consistindo de qualquer combinação desses membros. A seguinte declaração contém uma lista de sintomas exibidos por pessoas que sofrem de alergia:
SET ('espirro','nariz entupido', ' cabeça constipada', olhos vermelhos')

Um paciente pode ter qualquer um ou todos (ou nenhum) destes sintomas, e os valores de sintoma, portanto, podem conter de zero a quatro membros individuais deste SET, separados por virgulas. As declarações a seguir gravam na coluna, respectivamente, uma string vazia (não há membros do SET), um único membros do SET e múltiplos membros do SET:
INSERT INTO alergia (sintoma) Values('');
INSERT INTO alergia (sintoma) Values('cabeça constipada');
INSERT INTO alergia (sintoma) Values('espirro', 'olhos vermelhos');

O MySQL representa colunas do tipo SET como um bitmap usando um bit por membro, de modo que elementos na definição sintoma têm valores internos de 1,2,4 e 8 (isto é, eles têm os valores dos bits 1 até 3 em um byte) Internamente, o MySQL armazena os valores mostrados nas declarações INSERT anteriores como 0 (nenhum bit ligado), 4 (bit 2 ligado) e 9 (bits 0 e 3 ligados, ou seja, 1 mais 8).
Uma definição de SET pode conter 64 membros. O armazenamento interno requerido para os valores do SET depende do número de elementos (1,2,3,4 ou 8 bytes para conjuntos de até 8, 16, 24, 32 ou 64 membros. Já uma definção de coluna di tipo ENUM pode listar até 65.535 membros.
Se você tentar armazenar um membro inválido em uma coluna do tipo SET, ele é ignorado, porque não corresponde a nenhum dos bits na definição da coluna. Poe exemplo, gravar um valor de sintoma com tosse, espirro, respiração ofegante resulta em um valor interno igual a 1 (espirro). Os elementos tosse e respiração ofegante são ignorados porque não estão listados na definição da coluna como membros válidos.
Conforme mencionado no primeiro paragrafo desta resposta, a conversão entre as representações em string e numérica de valores dos tipos ENUM e SET podem resultar em surpresas se você não for cuidadoso. Por exemplo, embora você normalmente fosse se referir a uma coluna do tipo enumeração usando as formas de strings de seus valores, também pode usar os valores numéricos internos. O efeito disso pode ser muito sutil se os valores em string se parecerem com números. Suponha que você defina uma tabela t da seguinte maneira
CREAT TABLE t (idade INT, irmãos ('0', '1', '2', '3', '>3'));

Neste caso, os valores da enumeração são as strings '0', '1', '2', '3', '>3', e os valores numéricos internos são 1,2,3,4 e 5, respectivamente. Agora suponha que você execute a seguinte declaração:
INSERT INTO t (idade, irmãos) VALUES (14, '3');

O valor da coluna irmãos é especificado aqui como a string '3', e este é o valor atribuído à coluna no novo registro. Entretanto, você também pode especificar o valor de irmãos como um número, como segue:
INSERT INTO t (idade, irmãos) VALUES (14, 3);

Todavia, neste caso, 3 é interpretado como o valor interno, o qual corresponde ao valor '2' da enumeração! O mesmo princípio se aplica às recuperações. Considere as duas declarações a seguir:
SELECT * FROM t WHERE  irmãos = '3';

SELECT * FROM t WHERE  irmãos = 3;

No primeiro caso, você obtém registros que possuam um valor na enumeração igual a '3'. No segundo caso, você obtém registros onde o valor interno é 3, ou seja, registros com valor na enumeração igual a '2'.
Fonte: MySQL Guia de Estudo para Certificação
Comment: Só posso dizer o seguinte: se há algum motivo para downvoto nessa resposta, a qual nenhum de nós conseguiu enxergar, e o autor do down conseguiu ver, esse moço deve ter um profundo conhecimento a respeito da matéria e por isso ser considerado um senhor da mais alta respeitabilidade por parte da comunidade. Porém ele trilhou o caminho do egoísmo e deixou-nos a ver navios.
Comment: Não presuma que é perseguição, olhe que o @Sorack também ganhou downvote, e essa historia de ninguem comenta já foi discutido no Meta, tem usuário que comenta o problema de uma resposta e ao invés do autor levar como critica construtiva prefere ficar achando que é vitima, ou seja é por experiencia ruim e má aceitação de muitos usuários contra as criticas que claramente são muitas vezes úteis que o pessoal fica sem vontade de comentar, se as pessoas passarem a receber melhor as criticas provavelmente vocês irão ver muitos comentários, mas enquanto a maior parte agir assim, se sentido atacado...
Comment: ... ou vitima, dificilmente os usuários vão comentar, então a comunidade aprender aceitar criticas é um passo enorme para uma melhor comunidade. PS: não vi nenhum problema nem na sua resposta nem na do @Sorack, então deixo um +1 para cada um.
Comment: Brincadeira né, texto retirado do Guia de Estudo para Certificação MySQL e tem gente dando downvoto, esse rapaz do down deve se achar o Deus do MySQL para poder dar um -1. Gostaria de ver a resposta dele caso tenha uma.
Comment: Exato, vou entrar com uma ação pedindo o dinheiro que paguei pelo livro de volta pois um membro desse site não achou isso certo.
Comment: @GuilhermeNascimento, concordo plenamente com você!  Eu fui um desses e hoje entendo e aceito criticas numa boa e até agradeço quem faz. Mas esse caso em particular passou dos limites do bom senso de quem deu down pois me faz entender que essa materia desse livro não é correta ou eu não deveria compartilhar.
Comment: @LeoCaracciolo Ressalto que não fui eu quem dei o -1. Creio que o -1 vai da opção de cada um, o négocio é aceitar por mais que esteja correto a resposta, deixo meu +1. Eu fiz uma pergunta no meta a um tempo atrás sobre o tema [O que fazer quando recebo downvotes sem motivos?](https://pt.meta.stackoverflow.com/questions/5846/o-que-fazer-quando-recebo-downvotes-sem-motivos)
Comment: Não tem, mas é assim mesmo, pessoal não justifica o `downvote`, fica difícil saber o que tem de "errado" na resposta
Comment: Acredito que alguem esteja me perseguindo por algum motivo que não sei. Não é a primeira vez que isso acontece. Talvez inveja... quem sabe. Esse tipo de conduta deveria ser passível de monitoração e dependendo do entendimento uma suspensão ou mesmo banimento
Comment: LeoCaracciolo não dá pra saber o motivo, é possivel que exista algum problema nas respostas que nem eu, nem você e nem o @Sorack notamos, não dá pra sair acusando gratuitamente, eu mesmo já ganhei downvote em respostas, algumas eu achava que estava totalmente corretas, mas tinham sim um ou outro problema, não estava inteiramente errada, mas existia um detalhe ou outro errado. Não estou dizendo que não existe perseguição, é possivel que exista, mas também é possivel que seja apenas impressão, como disse, não dá para afirmar.
Comment: @LeoCaracciolo na realidade existe um sistema preventivo contra "perseguição". Se você receber muitos `downvotes` da mesma pessoa o sistema identifica que existe algo errado e reverte as votações consideradas abusivas. Mas isso não é instantâneo, então fica tranquilo que se acontecer isso o sistema reverte (já aconteceu comigo e atesto que funciona)
Comment: Sabe aqueles problemas de caractere esquisito no PHP?
Comment: @sam to lembrado
Comment: Já resolvi, mas ontem tava dando um problema aqui. Estava fazendo um preg_replace numa palavra e o resultado tava vindo com aquelas interrogações dentro de um losango.
Answer: TL;DR
SET permite salvar uma coleção. ENUM permite apenas valores unitários.

Documentação
De acordo com a documentação do MySQL ENUM and SET Constraints:

ENUM and SET columns provide an efficient way to define columns that can contain only a given SET of values. See Section 11.4.4, "The ENUM Type", and Section 11.4.5, "The SET Type".
With strict mode enabled (see Section 5.1.8, "Server SQL Modes"), the definition of a ENUM or SET column acts as a constraint on values entered into the column. An error occurs for values that do not satisfy these conditions:

An ENUM value must be one of those listed in the column definition, or the internal numeric equivalent thereof. The value cannot be the error value (that is, 0 or the empty string). For a column defined as ENUM('a','b','c'), values such as '', 'd', or 'ax' are invalid and are rejected.

A SET value must be the empty string or a value consisting only of the values listed in the column definition separated by commas. For a column defined as SET('a','b','c'), values such as 'd' or 'a,b,c,d' are invalid and are rejected.

Em tradução livre:

As cláusulas ENUM e SET fornecem uma maneira eficiente de definir colunas que podem conter apenas um determinado conjunto de valores. Consulte a Seção 11.4.4, "O tipo ENUM" e a Seção 11.4.5, "Tipo SET".
Com o modo estrito habilitado (strict mode) (consulte a Seção 5.1.8, "Modos SQL do Servidor"), a definição de uma cláusula ENUM ou SET atua como uma restrição nos valores inseridos na coluna. Ocorre um erro para valores que não satisfazem estas condições:

Um valor ENUM deve ser um dos listados na definição da coluna, ou seu equivalente numérico interno. O valor não pode ser o valor do erro (ou seja, 0 ou a string vazia). Para uma coluna definida como ENUM ('a', 'b', 'c'), valores como '', 'd' ou 'ax' são inválidos e são rejeitados.

Um valor SET deve ser a string vazia ou um valor que consiste apenas nos valores listados na definição da coluna separada por vírgulas. Para uma coluna definida como SET ('a', 'b', 'c'), valores como 'd' ou 'a, b, c, d' são inválidos e são rejeitados.

Em quais situações ambas são aplicadas da melhor maneira?

A diferença básica é que quando você define a coluna como SET, é possível armazenar mais de valor da lista. Já no ENUM só é possível inserir um valor.
No caso abaixo os registros serão incluídos normalmente, já que podem ser utilizados valores isolados ou a combinação dos atributos:
CREATE TABLE teste_set (
  fruta SET('banana', 'laranja', 'abacaxi')
);

INSERT INTO teste_set (fruta) VALUES ('banana');
INSERT INTO teste_set (fruta) VALUES ('banana,laranja');

No código abaixo a segunda inserção ficará com a coluna fruta vazia já que cada valor só pode ser inserido uma vez:
CREATE TABLE teste_enum (
  fruta ENUM('banana', 'laranja', 'abacaxi')
);

INSERT INTO teste_enum (fruta) VALUES ('banana');
INSERT INTO teste_enum (fruta) VALUES ('banana,laranja');

Comment: Eu ia postar a mesma coisa pois está assim na documentação. Não entendi o porque do -1.
Comment: Obrigado pelo esclarecimento @Sorack


Applying some analysis to Amaj Cmaj Dmaj Amaj

Question: Why does Amaj Cmaj Dmaj Amaj work so well?
How can we describe the relationships between these chords?
What key am I even in? Amaj and Dmaj could be I and IV in A major, or Cmaj and Dmaj could be IV and V in G major? Feels like the former, but how do we explain the Cmaj in An A major key, borrowed parallel?
Is this modal or something?
Any suggestions as to where/how you could develop this progression further?
Answer: You could well be in A major.  Does A major feel like home?  ♭III is a frequent visitor to a major key.
Try adding another common interloper, ♭VII.   So you'll have Amaj Cmaj Dmaj Gmaj Amaj. Nice?
Non-diatonic chords don't necessarily change the basic tonality.  And chord progressions don't HAVE to fit a 'circle of 5ths'.
Comment: Interesting. I think I've accidentally done the ♭VII thing before. Another question if I may, when visiting the ♭III or ♭VII what would the melody/lead voice do harmonically? Modulation in to the host chords key seems like the obvious choice, any other thoughts?
Comment: It might centre on the note(s) that are common to both the home key and the 'outside' chord.
Answer: A C major in the middle of an A major context can work as a "modal interchange" or "borrowing" or temporary key change to parallel minor Am or something. It's a common thing in jazz and blues, flirting with and alternating between parallel major and minor tonalities.


Weird thing about $\sum_{k=2}^{\infty}(-1)^k\zeta{(k)}$

Question: Consider the sum $S=\sum_{k=2}^{\infty}(-1)^k\zeta{(k)}$. By a simple manipulation, we can show:
$$
S=\sum_{k=2}^{\infty}{(-1)^k\sum_{r=1}^{\infty}\frac{1}{r^k}}=\sum_{r=1}^{\infty}{\sum_{k=2}^{\infty}\frac{(-1)^k}{r^k}}=\sum_{r=1}^{\infty}{\frac{1}{1+\frac{1}{r}}-1+\frac{1}{r}}=\sum_{r=1}^{\infty}{\frac{1}{r}-\frac{1}{r+1}}=1
$$
But looking at the partial sums $S_n$ we can see that:
$$
\begin{array}{c|lcr}
n & S_n\\
\hline
2 & 1.644934... \\
3 & 0.442877... \\
4 & 1.525200... \\
100 & 1.500…026… \\
101 & 0.499…987...
\end{array}
$$
Which disagrees with the result. How can one be trapped so easily?
Edit:
And how to show
$$
\lim_{n\to\infty}{S_n-\frac{(-1)^n+1}{2}}=\frac{1}{2}
$$
?
Comment: Probably the series is divergent and can be summed to $1$ with some summation method...
Comment: Note too that $\dfrac {S_{2n}+S_{2n+1}}2\to 1$ : the series doesn't converge but... Let's add that $\sum_{k=2}^{\infty}(-1)^k(\zeta(k)-1)\to\dfrac 12$ while $\sum_{k=2}^{\infty}(-1)^k\to\dfrac 12$ (once 'regularized' by evaluating first $\sum_{k=2}^{\infty}(-a)^k$ with $a<1$) so that their sum...
Answer: Hint. The series is not convergent since
$$
\lim_{k \to \infty}(-1)^k\zeta(k)\neq 0.
$$ One may recall that
$$
\zeta(k)=\color{red}{1}+\frac1{2^k}+\frac1{3^k}+\cdots.
$$
Answer: What is convergent is $$\displaystyle \sum_{i \geq 2} (-1)^i( \zeta(i)-1)=\sum_{k \geq 2}\left( \sum_{i \geq 2} {1 \over (-k)^i} \right)=\sum_{k \geq 2} {1 \over k(k+1)}=\sum_{k \geq 2} {1 \over k(k+1)}=\sum_{k \geq 2} {1 \over k}-{1 \over k+1}= \lim_{n \to \infty}{1 \over 2}-{1 \over n+1}={1 \over 2}$$
Yours of course doesn't converge, because the summed terms don't even go to $0$. However, you can easily prove your limit because:
$${1 \over 2}=\sum_{i \geq 2} (-1)^i( \zeta(i)-1)= \lim_{n \to \infty} S_n- \sum_{i=2}^{n}(-1)^i=\lim_{n \to \infty} S_n - {(-1)^n+1 \over 2}$$


sum of combinations $\binom n{0} + \binom n{3} + \binom n{6} +... +\binom n{3k}$

Question: How should one prove that $\binom n{0} + \binom n{3} + \binom n{6} +... +\binom n{3k} = \dfrac{2^n-2}{3}$ for $n$ odd and $\binom n{0} + \binom n{3} + \binom n{6} +... +\binom n{3k} = \dfrac{2^n+2}{3}$ for $n$ even, where $k=\lfloor \dfrac{n}{3}\rfloor $?
Comment: Possible duplicate of [Show that $\sum_{k=0}^n\binom{3n}{3k}=\frac{8^n+2(-1)^n}{3}$](http://math.stackexchange.com/questions/213583/show-that-sum-k-0n-binom3n3k-frac8n2-1n3)
Comment: https://math.stackexchange.com/questions/3384818/how-to-calculate-the-binomial-sum-s-mathop-sum-limits-i-0-lfloor-fracn/3384901#3384901
Answer: Hint: consider
$$
(1+x)^n = \sum_{k = 0}^n \binom nk x^k
$$
for $x\in \{1,j,j^2\} \ \ \ (j^3 = 1, j\notin \Bbb R)$ and $1 + j + j^2 = 0$.


probability of a pair in cards

Question: I have a simple problem involving probability of drawing at least 1 pair of cards in a four card hand.  I am not getting the right answer but I dont understand the flaw in my logic.  Can  anyone explain to me why my approach is wrong?
The problem:
Bill has a small deck of 12 playing cards made up of only 2 suits of 6 cards each. Each of the 6 cards within a suit has a different value from 1 to 6; thus, for each value from 1 to 6, there are two cards in the deck with that value. Bill likes to play a game in which he shuffles the deck, turns over 4 cards, and looks for pairs of cards that have the same value. What is the chance that Bill finds at least one pair of cards that have the same value?
My solution:
Probability of 1 or more pair = ((6) * (10 choose 2))/(12 choose 4).  i.e. the # of hands with at least 1 pair are 6 (the number of ways to create a pair) * (10 choose 2) (the # of ways to select 2 cards from the remaining 10 cards after the pair).  This simplifies to 6/11, however, the correct answer is 17/33.
Any help understanding would be greatly appreciated.
Comment: You make some interesting implicit assumptions in your calculation. First, it focuses only on the possibility that the pair occurs among the first two within a sequence of four cards: there are other ways a pair can occur, so this underestimates the probability. Second, it ignores the sequence among the second two cards, thereby overestimating the probability. Fortunately, the two mistakes do not cancel, revealing the fact there is a problem!
Comment: Realized I was double counting. Cant simply multiply 6 * (10 choose 2). 2 pair instances will be double counted. e.g. pair of 1s in the 6 and then pair of 2s in the 10 choose 2 versus pair of 2s in the 6 and then pair of 1s in the 10 choose 2. The better way to do it is to separate the cases of exactly 1 pair, exactly 2 pair.

ways to create exactly 1 pair = 6 (ways to create pair) * ( (10*8)/2 ) + ways to create exactly 2 pair = (6*5)/2

Total # = 255

Denominator = (12 choose 4) = (12*11*10*9)/(4*3*2) = 11*5*9 = 455.

Probability = 255/455 = 51/99 = 17/33.
Comment: Another way to do this is simply to remove the double counting directly.  There are 6*(10 choose 2) = 270 ways to create 1 or 2 pairs where the instances of 2 pairs are double counted.  If you subtract the # of ways to make exactly 2 pairs (6*5)/2 then you get 270 - 15 = 255.
Answer: This one might be easier to think of from the other direction.
What is the probability of choosing 4 cards without any pairs (this is the complimentary set to seeing at least 1 pair).
To see 0 pairs there are 12 possibilities for the 1st card, but only 10 for the second card (since the 1st chosen card is no longer possible and the card that matches the 1st would form a pair) and 8 cards for the 3rd and 6 for the 4th, this multiplies to 5760.  The total number of possible hands (with order mattering) is $12 \times 11 \times 10 \times 9 = 11880$, so the complimentary number which is the number of (ordered) hands that contain at least 1 pair is $11880 - 5760 = 6120$, divide that by 11880 and it reduces to $17/33$.  We could also do this with order not mattering, but that would be more complicated and the extra pieces would all end up cancelling each other.
I think the 10 choose 2 may be throwing you off, I don't see where that comes into any version.

probability of a pair in cards


Order cols in two different rows with Flexbox

Question: I need to order cols that is placed in two different rows with Flexbox CSS, but I can only do it for one row. I need to have a mixed order between the two rows.
Right now I have to order and show the cols in row1 and the order and show the cols in row2, but I want to mix them and order them. (The order is OK in desktop view but I need this for mobile view. That's why I need a complete reorder from two rows).
HTML:

1

2

3

4

5

6

CSS:
/* CSS used here will be applied after bootstrap.css */
.mobile-order {
    display: flex;
    flex-direction: column;
}
.test1 {
    order: 1;
}
.test2 {
    order: 2;
    background: #ccc;
}
.box1 {
    order: 1;
}
.box2 {
    order: 3;
}
.box3 {
    order: 5;
}
.box4 {
    order: 6;
}
.box5 {
    order: 4;
}
.box6 {
    order: 2;
}

Bootply test page
This is how I need it for desktop and mobile:
Desktop:
Row1

1
  |
2
   |
3

Row2

4
    |
5
   |
6

Mobile:

4

1

5

2

6

3

Comment: Always post your code in your question please.
Comment: You can't mix the order of elements that do not share a parent...so it's not clear to me what you are trying to do.
Comment: It sounds like you should use media queries for this.
Comment: That is what i want to do @Paulie_D. I know i can´t but i thought someone could come up with some tricky solution anyway.
Comment: I'm confused as to what the two layouts should look like...the desktop and mobile views. Can you provide that?
Comment: I use media query to show the order Css aleady. I just need to be able to sort and mix between the two parents.
Comment: I added some info in the end of my question @Paulie_D
Comment: Is there some reason you have to use 2 rows?
Answer: The CSS Display Module Level 3 introduces display: contents:

The element itself does not generate any boxes, but its children and
  pseudo-elements still generate boxes as normal. For the purposes of
  box generation and layout, the element must be treated as if it had
  been replaced with its children and pseudo-elements in the document
  tree.

You can use it to "ignore" the rows and display the inner flex items as if they belonged the same flex container. Then you are able to mix them as you want.

.mobile-order {
  display: flex;
  flex-direction: column;
}
.row {
  display: contents;
}
.test2 > div {
  background: #ccc;
}
.box1 { order: 1; }
.box2 { order: 2; }
.box3 { order: 3; }
.box4 { order: 4; }
.box5 { order: 5; }
.box6 { order: 6; }

1 (row 1)

5 (row 1)

2 (row 1)

3 (row 2)

6 (row 2)

4 (row 2)

It's not widely supported yet, though.
Comment: Thanks but where can i find out supported browsers for this? @Oriol
Comment: @user2986959 According to [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/display#Browser_compatibility), currently it's only supported by Firefox 37+
Comment: Ok so this is the only option if i dont want to add Jquery then?
Comment: Well, I guess you could also place the elements at the desired places with absolute positioning. But the layout won't be flexible at all.
Comment: Upvoted for introducing me to a new CSS feature. I love that I'm still learning new things after all these years :)
Comment: I like this solution but there need to be support for more browsers. I guess i have to add some Jquery if anyone else knows some other magic trick =)
Answer: If using a single row is an option, basic flexbox with wrapping will take care of this with a media query.

[class*="box"] {
  border: 1px solid grey;
}
.mobile-order {
  display: flex;
  flex-wrap: wrap;
}
@media screen and (max-width: 992px) {
  .mobile-order {
    display: flex;
    flex-direction: column;
  }
  .test1 {
    order: 1;
  }
  .test2 {
    order: 2;
    background: #ccc;
  }
  .box1 {
    order: 1;
  }
  .box2 {
    order: 3;
  }
  .box3 {
    order: 5;
  }
  .box4 {
    order: 6;
  }
  .box5 {
    order: 4;
  }
  .box6 {
    order: 2;
  }
}

1

2

3

4

5

6

Codepen.io Demo
Comment: Yes thanks @Paulie_D. That was my original plan but in my Episerver solution i need to use two rows. Thats why i have this problem.


Is it possible to hide "try now" button from the store while keeping link to instant app activated?

Question: I added instant app compatibility to my Android application. It works : I have a "try now" button in the store (internal tests) and I can access my instant app with a link.
I would like to hide the "try now" button. I would like my instant app to be only accessible with the link.
Is it possible to do that ?
Answer: Sorry, but it's not possible to launch Instant Apps with links without the "Try Now" launch link in the Playstore.  If you would like this as a new feature, post here and they will look at your request:
Request new feature
Comment: hey, any news on this topic ? it seems here is still nothing changed from 2019? :(


How to share objects between activites, serialization or SingleInstance?

Question: I have certain data in the data model class and want to share this data/object to the other activity. Single Instance could be a good choice but want to finish my previous activity and hence will got null pointer. Please Suggest the best possible way to do it?
Comment: yes,possible case if data doesn't belong to the activity.If the instance of activity that holding the data got killed, how the static data will alive. can use static class to hold the data but its not required and consume enough memory also. Any correction?
Comment: just use static keyword before variable. And access it with classname.variable-name
Comment: @manish it's not a good practice, and if my activity got killed all static data will also not work. Static data will remain if object is there. Please provide correction if, i am wrong.
Comment: IN my case , all static data will alive while your application running, its not depend on activity lifecycle
Comment: So your data is coming from activity (input) and related to activity lifecycle ?
Comment: yes, can use single static instance of activity to hold the objects and can use in other activity but have to finish the activity. User piyush suggest a good way to do it, I tried and its working fine. Thanks for your precious time manish.
Comment: Thanks for discussion and clear my doubts, I think we have to vote up answer of piyush. According to your problem Intent with putExtra will be the best way to solve your problem. Thanks again.
Answer: The best practice is to use intent object for that, putting either parcel or serialized data in his extras.
Static is dangerous since you must be always prepared for the case in which your application will be killed and restored.
Answer: Best possible way to use serialization, SingleInstance will not work here because you have to finish your activity.
Intent intent = new Intent(this, B.class);
ArrayList<String> data = new ArrayList<String>();
arrays.add("testing");
intent.putExtra("arrayList", data);
startActivity(intent);

you can use your objects instead of String and other side you can retrieve using this.
ArrayList<String> arrayList =(ArrayList<String>) getIntent().getSerializableExtra("arrayList");


Distribution of $X+Y$ when $X$ and $Y$ are i.i.d with pdf $f(x)=\alpha\beta^{-\alpha}x^{\alpha-1}\mathbf1_{0<x<\beta}$

Question: I am working on the following problem:

Let $X$ and $Y$ be independent random variables with common density $f(x)=\alpha\beta^{-\alpha}x^{\alpha-1}\mathbf1_{0<x<\beta}$ where $\alpha\geqslant1$. Let $U=\min(X,Y)$ and $V=\max(X,Y)$. Find the joint density of $(U,V)$ and hence find the pdf of $U+V$.

As $U+V=X+Y$, I can simply find the pdf of $X+Y$ to see what the pdf of $U+V$ should be.
I get the pdf of $T=X+Y$ to be $$f_T(t)=\int f(t-y)f(y)\,\mathrm{d}y=\alpha^2\beta^{-2\alpha}\int_{\max(t-\beta,0)}^{\min(t,\beta)}(y(t-y))^{\alpha-1}\,\mathrm{d}y\,\mathbf1_{0<t<2\beta}\tag{1}$$
Not sure if that integral can be simplified though.
Coming back to the actual question, the joint pdf of $(U,V)$ is given by
$$f_{U,V}(u,v)=2f(u)f(v)\mathbf1_{0<u<v<\beta}=2\alpha^2\beta^{-2\alpha}(uv)^{\alpha-1}\mathbf1_{0<u<v<\beta}$$
I did a change of variables $(U,V)\to(W,Z)$ where $W=U+V$ and $Z=U$. Absolute value of jacobian is unity. Also, $0<u<v<\beta\implies 0<z<\frac{w}{2}<\beta$. So marginal pdf of $W$ is
$$f_W(w)=2\alpha^2\beta^{-2\alpha}\int_0^{w/2}(z(w-z))^{\alpha-1}\,\mathrm{dz}\,\mathbf1_{0<w<2\beta}\tag{2}$$
It is possible that I have made some error in the proper supports of the random variables. It is also possible that the integral does not have a solution in terms of elementary functions. In any case, I could not proceed with the integral. So I couldn't even verify that $W=U+V$ has the same pdf as $T=X+Y$. It appears that I am getting different distributions of $W$ and $T$. And out of curiosity, does the distribution of $X$ have a name (in which case I would have searched for the convolution of two such random variables) ?
Edit.
Proceeding with the last integral I get by hand
$$\int_0^{w/2}(z(w-z))^{\alpha-1}\,\mathrm{dz}=w^{2\alpha-1}\int_0^{1/2}t^{\alpha-1}(1-t)^{\alpha-1}\,\mathrm{dt}=w^{2\alpha-1}I_{1/2}(\alpha,\alpha)B(\alpha,\alpha)$$ where $I_{x}$ is the regularized incomplete beta function. Using the property $I_x(a,b)=1-I_{1-x}(b,a)$, we get $I_{1/2}(\alpha,\alpha)=\frac{1}{2}$. So finally we have $$\int_0^{w/2}(z(w-z))^{\alpha-1}\,\mathrm{dz}=\frac{1}{2}w^{2\alpha-1}B(\alpha,\alpha)$$
This implies that
$$f_W(w)=\alpha^2\beta^{-2\alpha}B(\alpha,\alpha)w^{2\alpha-1}\mathbf1_{0<w<2\beta}$$
That this is not a density in the given range of $w$ is easily seen. So I feel I have made a big mistake somewhere. I have checked my calculations with Mathematica and they seem to agree.
Comment: As a generalization of https://stats.stackexchange.com/questions/41467 (the case where $\alpha=1$), this question likely can be solved using one or more of the various techniques explained in that thread.
Comment: @Xi'an And the sum of independent beta variates does not have a closed form pdf perhaps?
Comment: @Xi'an So I feel there's nothing wrong if I end my answer with that integral irrespective of whether it has a closed form in terms of some special function or not?
Comment: I mistakenly said that $\alpha>1$, when in fact $\alpha>0$ suffices for $f$ to be a valid density. This is sometimes called a *power function distribution*. For $\beta=1$ it is a beta density, and for $\alpha=1$ it is a uniform density.
Answer: Since
\begin{align}
\int_{\max(t-\beta,0)}^{\min(t,\beta)}(y(t-y))^{\alpha-1}\,\mathrm{d}y\,\mathbf1_{0<t<2\beta}&=\begin{cases}
\int_{0}^{t}(y(t-y))^{\alpha-1}\,\mathrm{d}y&\text{when }0\le t\le \beta\\
\int_{t-\beta}^{\beta}(y(t-y))^{\alpha-1}\,\mathrm{d}y&\text{when }\beta\le t\le 2\beta\\
\end{cases}\\
\end{align}
we have ($t<\beta$)
$$\int_{\max(t-\beta,0)}^{\min(t,\beta)}(y(t-y))^{\alpha-1}\,\mathrm{d}y=
\int_{0}^{t/2}(y(t-y))^{\alpha-1}\,\mathrm{d}y+\int_{t/2}^{t}(y(t-y))^{\alpha-1}\,\mathrm{d}y$$
and by a change of variable $z=t-y$ in the second integral of the rhs
$$\int_{\max(t-\beta,0)}^{\min(t,\beta)}(y(t-y))^{\alpha-1}\,\mathrm{d}y=
2\int_{0}^{t/2}(y(t-y))^{\alpha-1}\,\mathrm{d}y$$
Similarly, when $t>\beta$,
\begin{align*}
\int_{t-\beta}^{\beta}(y(t-y))^{\alpha-1}\,\mathrm{d}y&=
\int_{t-\beta}^{t/2}(y(t-y))^{\alpha-1}\,\mathrm{d}y+\int_{t/2}^{\beta}(y(t-y))^{\alpha-1}\,\mathrm{d}y\\
&=2\int_{t/2}^{\beta}(y(t-y))^{\alpha-1}\,\mathrm{d}y
\end{align*}
again by a change of variable $z=t-y$ in the second integral of the rhs. I am however unable to recover the same functional expression for the density in this second case, namely$$2\int_{0}^{w/2}(z(w-z))^{\alpha-1}\,\mathrm{d}z$$
Now, as pointed out in the question,
$$2\int_{0}^{w/2}(z(w-z))^{\alpha-1}\,\mathrm{d}z \propto w^{2(\alpha-1)+1}=w^{2\alpha-1}$$by a change of scale, which would imply that the distribution of interest has the density
$$f(w)\propto w^{2\alpha-1} \mathbf1_{0<w<2\beta}$$
which turns it into a Beta ${\cal B}(2\alpha,1)$ distribution rescaled on $(0,2\beta)$, hence with density
$$f(w) = \{2\beta\}^{-2\alpha}\dfrac{\Gamma(2\alpha+1)}{\Gamma(2\alpha)}w^{2\alpha-1} \mathbf1_{0<w<2\beta}=2\alpha\{2\beta\}^{-2\alpha}w^{2\alpha-1} \mathbf1_{0<w<2\beta}$$

This comes as a contradiction when considering the unbelievably
  detailed answer from W. Huber, since Uniforms are Beta ${\cal
  B}(1,1)$. And since the sum of two Uniforms is not a Beta ${\cal
  B}(2,1)$ random variable, but instead an rv with "tent" density.

Aside: More generally a sum of Beta variates is not another Beta variate, the "explanation" being straightforward when looking at Betas as two Gammas normalised by their sum. Adding two Betas sees different sums in the denominator.
The issue is thus with the derivation of the density of $W=U+V$: since
$$(U,V) \sim 2\alpha \beta^{-2}[uv]^{\alpha-1}\,\mathbb{I}_{0<u<v<\beta}$$
a change of variables $(Z,W)=(U,U+V)$ leads to
$$(Z,W) \sim 2\alpha \beta^{-2}[z(w-z)]^{\alpha-1}\,\mathbb{I}_{0<z<w-z<\beta}$$
and the indicator constraints are
$$0<z \quad 2z<w \quad z<\beta \quad z>w-\beta \quad 0<w \quad\text{and}\quad w<2\beta$$
Therefore, in conclusion,
$$W\sim 2\alpha^2 \beta^{-2\alpha}\int_{\max\{0,w-\beta\}}^{\min\{\beta,w/2\}}[z(w-z)]^{\alpha-1}\,\text{d}z\,\mathbb{I}_{0<w<2\beta}$$
namely (1) and not the proposed expression (2).
Comment: That was what I was asking whether it agrees with (1) or not. You probably have to add the missing constants in $(Z,W)$ and $(U,V)$ as well. Thanks, no wonder I was getting all those weird results.


Existence of a BCH-code?

Question: I would like to ask about existence of a $[31,11,12]$ binary BCH-code. How to prove its existence, if it does i have to find the generator polynomial? Can i use some specific bound that could show or deny the existence of this code?
Since the BCH-codes are cyclic codes, we have dimension $11$, so the degree of the generator polynomial must be $20$.
Can anybody help me with this problem, please? Thank you in advance!
Comment: Do you know the definition of a BCH code? Do you know how to determine the conjugates of $\alpha^i$ where $\alpha$ is a primitive element of $GF(32)$ a.k.a. $\mathbb F_{2^5}$?
Comment: Hint: $\alpha^5$ and $\alpha^9$ share the minimal polynomial.
Comment: Yes, i know how to compute the conjugates.
Comment: Good. Now, what is the definition of a binary BCH code?
Answer: A restrictive but mostly adequate definition of a binary BCH code of designed distance $2t+1$ is of a cyclic code of length $n$ whose generator polynomial $g(x)$
has roots $\alpha, \alpha^2, \ldots, \alpha^{2t}$ where $\alpha$ is an
$n$-th root of unity in some extension field GF$(2^m)$ (also called $\mathbb F_{2^m}$) of GF$(2)$ or $\mathbb F_2$.  Since $g(x) \in \mathbb F_2[x]$, the conjugates of these $2t$ elements also are roots of $g(x)$.  If we proceed
systematically, beginning by making a list of the conjugates of $\alpha$
(which are $\alpha^2, \alpha^4, \cdots$), we see that some elements that
we would take up later have already been accounted for.
The generator polynomial of a binary BCH code of length $31$ with
designed distance $11$ has
roots $\alpha, \alpha^2, \alpha^3, \ldots, \alpha^{10}$ and so
we start listing the conjugates as
$$\underline{\alpha},\quad \underline{\alpha^2},\quad
\underline{\alpha^4},\quad \underline{\alpha^8},\quad \alpha^{16}\\
\underline{\alpha^3}, \quad \underline{\alpha^6},
\quad \alpha^{12}, \quad \alpha^{24},\quad\alpha^{17}\\
\underline{\alpha^5}, \quad \underline{\alpha^{10}},
\quad \alpha^{20}, \quad \underline{\alpha^{9}},\quad\alpha^{18}\\
\underline{\alpha^7}, \quad {\alpha^{14}},
\quad \alpha^{28}, \quad {\alpha^{25}},\quad\alpha^{19}$$
where the elements on the $2t$ list are underlined.
Note that we would have ended up with the same result
if we had been looking for a code with minimum distance
$9$; the fact that $\alpha^9$ is  a conjugate of $\alpha^5$
(as Jyrki noted) and so the designed distance works out
to be $11$ instead of $9$ is a serendipitous  bonus that we
were not seeking.
We conclude
that $\deg(g(x)) = 20$ and so there does exist a
$[n,k,d] = [31,11,11]$ BCH code, but not a $[31,11,12]$ code.
There is a $[31,10, 12]$ BCH code which is a subcode
(consisting of the even-weight codewords) of the $[31,11,11]$
BCH code described above. (It is a BCH code under a more general
definition of BCH code, where any $d-1$ successive powers of
$\alpha$ as roots of $g(x)$ gives a code of designed distance $d$:
just include $\alpha^0 = 1$ as a root of $g(x)$ in the list
displayed above).

Existence of a BCH-code?


Can't ping a DNS zone on windows server 2008 R2

Question: I´ve just configured a windows server 2008 r2, but got a lot of problems on DNS role.
Let me talk about the server configuration:

name: fdserver
IP address: 192.168.0.10
I have a DNS zone called "fd.local". This is my domain and it´s working ok.
I´ve created a zone called fdserver, and inside this zone a record (A) with "*" as a host. because this is a webserver, i´ve configured apache so if you enter something like "site.fdserver" it will point you to the "site" folder. This is working ok ONLY inside the server.
This server is a DNS server too... and have 3 entries: 192.168.0.10 (his own IP), 18.104.22.168 and 126.96.36.199 (google public DNS).

Now start the problems...
Most of the computers on my network, CAN join the domain without problems. But just CAN'T ping "something.fdserver".
Now comes the strange thing... If I remove the twoo secondary entries on my DNS server (22.214.171.124 and 220.127.116.11), it obvious stop accessing websites (like microsoft.com), but now the computer CAN ping "something.fdserver".

I don´t know If I explained correctly... and my English is terrible...
but inside the server is all working as it supposed to work. But in the workstation machines, it work only if I remove the secondary DNS!!
If you need any details, just ask! thanks!
Comment: Hello Mathias, you just solved my problem! I´ve decide to search about "forwarders" and then discover a lot of old DNS address that I used to have on my old server... removed they and add the google DNS address and is all working perfect!! Thank you!!
Comment: Is the google dns addresses (22.214.171.124 and .4.4) configured on the servers NIC or as forwarders in the DNS console?
Comment: Hi Mathias, the DNS address are configured in the NIC of the server AND on DHCP Server Options
Answer: If you have local DNS names (which you do), generally you'd want to set your DNS and AD server to use only itself as a DNS server.  You want all your other servers and client workstations to do the same.  It's the closest thing to you, and public DNS servers don't carry your local domains.  The standard doesn't require that a DNS client try each resolver it has if one of them returns a SERVFAIL or NEXIST, and in fact most don't.  The actual behaviour is that they either use all the DNS resolvers configured in round-robin, or only move down the list if the primary one is not reachable.
Resolving DNS names using root hints is never a bad idea; it removes one point of failure (the forwarders, which may change or go down).  If you can't, at least prune the forwarder list frequently.


Clearing the "Network" tab in IE11 developer tools

Question: How does one clear out the traced HTTP requests from the IE11 "Network" developer tool? There are some icons with little red "X" glyphs at the top but those do something other than clearing the view. It's possible of course with Firefox and Chrome tools, but nothing I've tried works with IE11.
I vaguely recall that IE9 had a "Clear" button but I could be imagining that.
Answer: When on the network tab, the option to clear the activity is called 'Clear session' and is located between 'Clear entries on navigate' and 'Content type'.  The icon is a little red x in the upper left corner, and then 3 lines.  It is the one highlighted yellow in the screenshot.


SBS Server 2003 (32 bit) - SQL + Exchange - is AWE useful?

Question: We have an SBS server running SQL Server 2005 and Exchange, that is notoriously tight on memory. (It's a battle between the sql and exchange).
It's not the time yet to replace it completely, but I wonder if stocking up to 8GB (the maximum the board supports, it currently holds 4GB) would help.
Since it's a 32 bit OS, the additional memory would be visible through AWE only, which at least SQL Server seems to support.

Can I expect improvements?
What config changes (Windows, SQL Server) are necessary so it makes use of AWE?
Can Exchange benefit from the additional memory, too?
Answer: SBS 2003 will only see 4GB of physical memory so AWE isn't going to do anything for you.  Have a look at Memory Limits for Windows Releases.
Comment: Damn, you, sales department! :D  - and thanks for the quick answer
Comment: I believe these limits are much more rational in the 2008 editions.. for now, anyway..
Answer: Small Business Servers nearly always use all the available memory, by design. Both Exchange and SQL Server (by default) will try to use all the available memory, but will release it 'on demand'. Result: your memory is always going to be full.
That's not the same thing as not having enough memory, that is just the server applications using the available resources to maximum effect. Unused memory is wasted memory.
The fact that SQL (by default) uses all available memory can cause problems, particularly the SBSMONITORING instance, which on some systems can begin to consume and unreasonable amount of memory (more then 100 Mb or so is unreasonable). If that's happening on your system then consider limiting the memory available to that instance. These steps in a command prompt will do it:
<code>> osql -E -S YOURSERVERNAME\sbsmonitoring
> sp_configure 'show advanced options',1
> reconfigure with override
> go
> sp_configure 'max server memory', 70
> reconfigure with override
> go
</code>

(source: sbslinks.com)
(image courtesy of Susan Bradley)
That limits the memory to 70 Mb for the SBSMONITORING instance (70 Mb is a value that has been suggested within the SBS MVP community, your mileage may vary). If you are running SBS Premium and have ISA Server, then you might also need to throttle the ISA logging database using the same technique. In this case, the instance name is MSFW and I have found that 70 Mb works for that, too.
Don't try to limit the Sharepoint instance, though. That should be left on its default settings.


Drupal 6 Site Duplication - Looks Like My Theme is Broken

Question: My goal is to duplicate a Drupal 6 site to a new host, but I am having some problems.
  New host - 184.108.40.206/~paul8226/ -- No name resolution setup yet.
  Old host - www.egcasting.com/

I performed a fresh Drupal 6 install on the new Host Gator account using the same database name, username/password as my existing site (clean URLs worked.) Then I imported the database from the existing site, and copied over sites/default (except settings.php) and all of sites/all. It looks like the site totally functions, except the theme is not styling anything at all.
Google Chrome developer tools is telling me I am missing the below CSS files, both of which are on my file system:
http://184.108.40.206/~paul8226/sites/default/files/css/css_89883be7e51f5fd34095c91a397aee3c.css
http://220.127.116.11/~paul8226/sites/default/files/css/css_6ca651b4b42888393291ef9ea27101fa.css
.
.
.

-rw-rw-r-- 1 paul8226 paul8226 39K May 13 15:57 css_89883be7e51f5fd34095c91a397aee3c.css
-rw-rw-r-- 1 paul8226 paul8226 549 May 13 15:57 css_6ca651b4b42888393291ef9ea27101fa.css

The CSS on the old host is working fine. So far, I have looked at the file, and directory, permissions going all the way up. I also tried turning on and off the CSS compression. Turning off CSS compression made the site styling look totally off in another way. When I look at the themes option, it looks like the custom theme I am trying to copy over is having some serious issues:

Broken Theme

Working Theme
I also looked through the theme permissions, and they look like what I think they should be.
 # pwd
 /home/paul8226/public_html/sites/all/themes
drwx--x--x 4 paul8226 paul8226 4.0K Aug 25  2009 eg_prod/
.
.
.
# cd eg_prod
# ls -laSh
total 260K
-rw-r--r-- 1 paul8226 paul8226  42K May 13  2009 theme-settings.php
-rw-r--r-- 1 paul8226 paul8226  37K Sep  9  2009 style.css
-rw-r--r-- 1 paul8226 paul8226  32K Aug 21  2009 template.php
-rw-r--r-- 1 paul8226 paul8226  15K Apr 23  2006 LICENSE.txt
-rw-r--r-- 1 paul8226 paul8226 7.8K Apr 20  2009 style-rtl.css

Thank you very much for taking the time to help me with this. Normally this 'thanks' section gets edited out, but oh well. Thanks!
Comment: copying the whole site with db wont work??
Answer:
/sites/*/files/css/ and /sites/*/files/js should be empty just after migration - do not copy their contend. Clear them.
Before making original site database snapshot, turn off css and js aggregation. If you didn't, turn it off now. If you don't, the duplicated site will be looking for the aggregated content which may, or may not be available on the new site.
On new site, make sure /sites/*/files/ directories are writeable by your php scripts, and that php can delete files too.
Search all theme files for not-relative URIs. There shouldn't be any.
Fix one problem at a time. First, make it work without aggregation (most probably it's URI problem), and only then attempt to fix aggregation issues.
At the end, if you still have problem - disable and then enable theme. This should ensure all theme-related caches are regenerated and theme settings variables refreshed.
Comment: +1 - your site / theme should work fine as soon as css aggregation creates new files in the new environment. flush cache liberally.
Comment: Thank you very, very much for your extremely detailed answer. I will try these solutions. I now understand why the CSS and JS aggregation should be turned off before copying over a site - the duplicated site will be looking for the aggregated content which may, or may not be available on the new site. Thanks!!
Comment: @Brian added your comment to my answer. A bit of explanation will not hurt. I cared to much to get things done and not enough to let everyone know why to do certain things.
Comment: @Brian - I just added your last step to my answer. Hope it'll save someone's time in future, so let's keep it visible. Glad it worked for you, after all.
Comment: Thanks. Unfortunately now I'm running into these type of issues after flushing the cache on a development copy of the site, but progressing:

http://drupal.org/node/1172560
Comment: Disabling, and then enabling, my theme after the duplication looks to have solved this issue AFTER taking all the steps you mentioned. Thanks!
Comment: Again, thanks a lot! Previously I was more familiar with Drupal 7, but am learning a TON with this project. I've been very happy with Drupal, and the support community, and will continue to learn.


infinite order of element with element in an infinite group

Question: If $G$ is a infinite group, then $G$ must have an element of infinite order.
Is this true?
I know that if $G$ is infinite cyclic, then it's isomorphic to $\mathbb Z$.
(I guess fact is irrelevant now)
Comment: You mean if $G$ is infinite *cyclic*, then it's isomorphic to $\Bbb Z$. In any case, what are you stuck on? What examples of infinite groups do you know?
Comment: @MikeMiller I know Z is infinite, R, C, Q
Comment: @MikeMiller what I'm stuck is would it be correct to say that 1 in Z is on infinite order since it generates all of Z?
Answer: Consider $(\mathbb Z/2\mathbb Z)^{\mathbb N}$ or $\mathbb Q/\mathbb Z$.
Comment: Okay, so this statement would be false
Answer: in an abelian group the elements of finite order form its torsion subgroup. a torsion group is infinite iff it is not finitely generated. $\Bbb Q / \Bbb Z$, cited by Hagen in his answer is a particularly clear example.


Math olympiad books recommandation (with a large number of high level problems on each topic)

Question: I have been preparing for mathematical olympiad since last year and I have read many books.
Which are like as follow:
1.Problem Primer for mathematical olympiad.
2.Problems in mathematics (By V.Govorov and fellows)
3.Challenge and Thrill of Pre-collage  mathematics (In association with NBHM of INDIA) and many more books, but no book amazed me. Yeah I believe that problems were tough enough to send me in coma sometimes but still I can't enjoy mathematics with them.
I want you guys to suggest me some adventurous Mathematics books which gave me a pleasure which I got by reading Young and Freedman, Resnick, Halliday and Walker, IE Irodov in physics.
This year I got chance to participate in International Olympiad Metropolis held in Russia (I thoought it is good to mention here so that you will not provide high school mathematical content in your answer). Hope you guys got it. Any suggestion is heartily welcome
Answer: These are my favorite ones and I hope you will like them:
$1$. An excursion in mathematics by Bhaskaracharya Pratishthana.
$2$. Putnam and Beyond by Titu Andresssu and Raven Gelca.
$3$. This link provide some other good books


Consulta em determinada string

Question: Bom pessoal a minha dúvida é o seguinte: Eu gostaria de saber como fazer uma consulta no banco de dados de determinada string, por exemplo eu gostaria de no campo "nome", eu gostaria de fazer uma consulta pelo nome: vinicios, e gostaria que me retornasse todos os registros que contem o nome vicios independente de ser vinicios cardoso,vinicios antonio, etc.
Comment: 2 negativaram, beleza, agora só me diz o motivo de terem negativado.
Comment: Tente utilizar o operador LIKE
Answer: <code>  select * from nome_da_tabela where nome like '%vinicius%'
</code>
O simbolo de % indica que pode haver qualquer coisa, ou seja, qualquer nome que contenha vinicius antes ou depois será selecionado.
Para buscar todos os nomes que começam com Vinicius por exemplo, você faria da seguinte forma:
<code> select * from nome_da_tabela where nome like 'vinicius%'
</code>
Comment: Obrigado :)....

Consulta em determinada string


In sklearn, it seems that `dot(x, x)` corresponds to `np.sum(X*X,axis=1)[:, np.newaxis]`, why is that?

Question: In a 2d Euclidean space,
Let point $a=(a_x, a_y)=(1,1)$;
Let point $b=(b_x, b_y)=(5,1)$;
Let point $c=(c_x, c_y)=(4,4)$;
the squared Euclidean distance between point a and point b is equal to
$(a_x-b_x)^2 + (a_y - b_y)^2$.
>>> a = np.array([1,1])
>>> b = np.array([5,1])
>>> np.linalg.norm(a-b)**2
16.0

the squared Euclidean distance between point a and point c is equal to
$(a_x-c_x)^2 + (a_y - c_y)^2$
>>> a = np.array([1,1])
>>> c = np.array([4,4])
>>> np.linalg.norm(a-c)**2
18.0

sklearn.metrics.pairwise.euclidean_distances uses this approach to compute the distances
>>> X = np.array([[1,1]])
>>> Y = np.array([[5,1],[4,4]])
>>> np.sum(X*X,axis=1)[:, np.newaxis] - 2*np.dot(X,Y.T) + np.sum(Y*Y,axis=1)[np.newaxis, :]
array([[16, 18]])

which corresponds to this formula

dist(x, y) = sqrt(dot(x, x) - 2 * dot(x, y) + dot(y, y))

it seems that dot(x, x) corresponds to np.sum(X*X,axis=1)[:, np.newaxis], why is that?
Comment: Are you questioning $=\sum x_i^2$?
Answer: dot(x, x) means the dot product of two vectors
${\displaystyle \mathbf {\color {red}a} \cdot \mathbf {\color {blue}b} =\sum _{i=1}^{n}{\color {red}a}_{i}{\color {blue}b}_{i}={\color {red}a}_{1}{\color {blue}b}_{1}+{\color {red}a}_{2}{\color {blue}b}_{2}+\cdots +{\color {red}a}_{n}{\color {blue}b}_{n}}$
plug in x
dot(x, x) = ${\displaystyle \mathbf {\color {red}x} \cdot \mathbf {\color {blue}x}} = x_1x_1+x_2x_2=x_1^2+x_2^2$
Vectorizing this computation gets np.sum(x*x) in Python
more info at https://en.wikipedia.org/wiki/Dot_product


Not able to see the print message after creating ObjectInputStream

Question: I have written the following program.As you can see I have put a print message after the creation of ObjectInputStream object ois.I have a server That is opened on Port 9090.As you can see from the above netstat message
sudo netstat -al | grep 9090
tcp6       0      0 [::]:9090               [::]:*                  LISTEN

I don't know why the print message is displayed on the screen.
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MyClient {
    public static void main(String[] args) {
        int count = 0;
        try {
            /*
             * Create a connection to the server socket on the server application
             */
             InetAddress address = InetAddress.getByName("localhost");
             Socket socket = new Socket(address, 9090);

            /*
             * Read and display the response message sent by server application
             */
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            System.out.println("Created client socket and Input Stream Reader");
            while (true) {
                if (count < 1000) {
                    String message = (String) ois.readObject();
                    System.out.println("OFMessage: " + message);
                    count++;
                } else {
                    break;
                }
            }
            ois.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Comment: "I don't know why the print message is displayed on the screen." or it must be "I don't know why the print message is not displayed on the screen."?
Answer: The constructor to ObjectInputStream reads from the stream - it reads the header information, basically.
So if the server you're talking to hasn't written any data, your program will just sit there, waiting until the header is actually written.
If you hit break in a debugger when running this code, I think you'll find the stack trace includes ObjectInputStream.readStreamHeader.
Comment: @tbodt: I didn't *know* it - I *researched* it. (It doesn't take long to open up Eclipse and look through the `ObjectInputStream` constructor.)
Comment: Only you would know this, Mr. Skeet.


Is this an instance of the base-rate fallacy?

Question: The following line of probability reasoning is supposedly fallacious, and is an instance of the base-rate fallacy.  The argument is that $(1)-(3)$ don't give us enough reason to conclude that $(C)$.
But it seems to me that this is not the case.  I can only see how (C) fails to follow from (2)-(3).  That is, admitting (1) forces (C) to be true given (2)-(3).  So am I correct, or is the following indeed fallacious reasoning?
$$
\tag{1}
Pr(Sx \mid x \in \mathcal{H}) \gg 0
$$
$$
\tag{2}
Pr(Sx \mid Tx \wedge x \in \mathcal{H}) \gg 0
$$
$$
\tag{3}
Pr(Sx \mid \neg Tx \wedge x \in \mathcal{H}) \ll 1
$$
$$
\tag{C}
Pr(Tx \mid Sx \wedge x \in \mathcal{H}) \gg 0
$$
Answer: I think you are correct, unless I have misunderstood something in your notation. I made an elementary probability calculation writing $A$ for $Sx$, $B$ for $x \in \mathcal{H}$ and $C$ for $Tx$. Then assuming all probabilities are nonzero:
\begin{align*}P(C | A \cap B) = 1 - P(\neg C | A \cap B) &= 1-P(\neg C \cap A \cap B)/P(A \cap B)\\
&= 1-\frac{P(A | \neg C \cap B) P(\neg C \cap B)}{P(A \cap B)}\\
&= 1-\frac{P(A | \neg C \cap B) (P(B) - P(C \cap B))}{P(A \cap B)}\\
&= 1-\frac{P(A | \neg C \cap B) (1 - P(C | B))}{P(A | B)}\\
&\ge 1 - \frac{P(A | \neg C \cap B)}{P(A|B)}
\end{align*}
You assume that $P(A | \neg C \cap B)$ is small and $P(A|B)$ is large, and so $P(C | A \cap B)$ must be close to $1$.
Comment: This is very elucidating, and it looks like (2) isn't being used at all.  Instead, one only needs (1) and (3) to show that (C) using my notation above.  Is this correct?
Comment: Yes, it seems that (2) follows from (1) and (3) as follows: $P(A|B \cap C) = P(A\cap B \cap C)/P(B \cap C) = (P(A\cap B) - P(A \cap \lnot C \cap B))/P(B \cap C) \approxeq P(A\cap B)/P(B \cap C) = P(A|B) /P(C|B) \ge P(A|B) \approxeq 1$ by (1).


Exim4 - Switch from monolithic to split config

Question: While making some changes in exim4, I decided to use the monolithic config style. So as not to confuse myself, I deleted /etc/exim4/conf.d/. I now regret this and wish to switch back to the split config style.
However, no amount of dpkg-recongifureing or update-exim4.confing will bring the files back. In fact, I am thoroughly confused by the configuration scheme for exim4 on ubuntu 16.04.
Could someone tell me how to restore the split configuration files and remove exim4.conf.template? (I don't mind if this reset my configuration, I can easily restore it).
Could someone please explain how exim decides which config files to load in which order? The manuals reference all sorts of default filenames.
Comment: Initial config file location/name is compiled in at the package build stage, but you can override it by `-C config.file`. That file should contain `.include ` or `.include_if_exists ` directives that are traversed recursively at startup.
Answer: The split configuration template is located in exim4-config (You can confirm this with dpkg -S /etc/exim4/conf.d/auth  You will need to reinstall that package using
apt-get -o Dpkg::Options::="--force-confmiss" install --reinstall exim4-config

to reinstall the package and restore the missing configuration files.  Once the configuration files are restored then you can use
dpkg-reconfigure exim4-config

to switch between the monolithic configuration and split configuration.  Any changes made to the monolithic configuration will need to be redone in the split configuration.
The split configuration uses separate folders to represent the sections of the configuration file.  Within each folder, update-exim4.conf will concatenate all of the files in alphanumeric order.  If creating new files, it's important to ensure that they sort after the 00_exim4-config_header section, since this file contains the begin line for that section.
Comment: Afterwards, can I delete exim4.conf.template?
Comment: I don't think it would harm anything either way.  You could rename it to something else first to see if anything breaks
Comment: What I'm still confused about, is how exim knows which file to read. The monolithic block, or the conf.d files. Where is the pointer? For example, apache has a main apache.conf with a line that explicitly  includes any other conf files/directories. I'm not sure how exim decides.
Comment: exim reads neither. When you execute the `update-exim4.conf` program, it checks `dc_use_split_config` to pick whichever files, then merges them with the rest of the configuration values in `update-exim4.conf.conf` to create `/var/lib/exim4/config.autogenerated`, and the default exim startup scripts use that file.
Comment: @JamesSwift if it doesn't automatically reconfigure when you reinstall `exim4-config` do `dpkg-reconfigure exim4-config` to get the question of whether you want the split configuration or not again.
Comment: Ah ha. Do I take it that exim (possibly systemd) executes update-exim4.conf on start? I found that variables were auto applied with a service restart, without `update-exim4.conf`
Comment: Probably. The whole thing is a Debianism not part of exim, so it would be the startup script/unit file that handles it.
Comment: Ok, tried the reinstall. It created the conf.d directory structure, but no files.
Comment: http://askubuntu.com/a/67028
Comment: Cheers. That's got it sorted. Thanks so much. I've summed that up in a new answer below. You can either vote it up, or edit your original and I'll mark it correct.
Answer: To answer my own question, based on @DerfK's answer. To regenerate the exim config files, the best way is to do the following:
sudo mv /etc/exim4 /etc/exim4-old
sudo apt-get -o Dpkg::Options::="--force-confmiss" install --reinstall exim4-config
sudo dpkg-reconfigure exim4-config

Warning, this will reset exim's configuration to default. Your old config is available in /etc/exim4-old/.


Undefined Javascript variable using Google Maps API

Question: I'm using Google Maps Javacript API to draw a polyline. I intended to create a function to remove the polyline when a button is clicked. The HTML code for the button is the following:
<button type="button" class="btn btn-danger" id="clear_button" onclick="removeLine()">Borrar ruta</button>

and the Javascript code for the drawing the polyline and removing it is the next:
    var map;
    var flightPath;
    function initMap() {

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 17,
            center: {lat: 37.176709, lng: -3.599057},
            mapTypeId: 'terrain'
        });

        google.maps.event.addDomListener(document.getElementById('route_button'), 'click', function() {
            var date_data = document.getElementById('datepicker').value;
            console.log(date_data);
            $.ajax({
                url:"data_fetcher.php",
                type:"POST",
                data: {date_value:date_data},
                dataType: 'json',
                success: function(respond){
                    var flightPlanCoordinates = respond;
                    var flightPath = new google.maps.Polyline({
                        path: flightPlanCoordinates,
                        geodesic: true,
                        strokeColor: '#FF0000',
                        strokeOpacity: 1.0,
                        strokeWeight: 2
                    });
                     flightPath.setMap(map);
                     console.log("AJAX level request was successful");
                },
                error: function(){
                    console.log("AJAX level request was a failure");
                }
            });
        });
    }

    // Eliminar polilinea
    function removeLine() {
    flightPath.setMap(null);
  }

everything works fine, but when I click the button to remove the polyline, the console throws the following error:

Uncaught TypeError: Cannot read property 'setMap' of undefined
      at removeLine (device.php:101)
      at HTMLButtonElement.onclick (device.php:196)

even though I declared flightPath var in the global scope. Every help will be appreciated.
Answer: In the success function, you're declaring a local variable with the same name, JS will use the closest variable it finds when assigning variables. To fix this simply remove the var.
success: function (respond) {
    var flightPlanCoordinates = respond;
    flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
    flightPath.setMap(map);
    console.log("AJAX level request was successful");
},

Comment: Thanks so much George. That was the issue. Can't figure how I couldn't see it.


Partial least squares regression (PLSR): regression coefficients vs correlation loadings

Question: I used the df below in PLSR to evaluate how the 20 independent variables (var1 to var20) influence the dependent variable.
In many studies (example) , they use

The regression coefficient: to show the direction of the relationship (+ve or -ve); and
The variable influence on projection (VIP): to infer the most influential variables (influential variables have VIP>1)

Below is the regression coefficients for each of 20 independent variables for the two components
> df_coef
           comp1         comp2 indep_variables
1   0.0015024714  0.0145192514            var1
2  -0.0154811588 -0.0365222808            var2
3   0.0216379897  0.0443815685            var3
4  -0.0097465460 -0.0035137829            var4
5   0.0610646791  0.0902798198            var5
6   0.0042542347 -0.0082339736            var6
7  -0.0466371356 -0.0578107853            var7
8  -0.0284714188 -0.0474403577            var8
9  -0.0101665311 -0.0041410784            var9
10  0.0158867370  0.0152252953           var10
11  0.0172372097  0.0194206975           var11
12 -0.0009615769  0.0079166002           var12
13  0.0100454612  0.0145392654           var13
14 -0.0180720488 -0.0136098047           var14
15  0.0080845545  0.0003998808           var15
16  0.0081133007  0.0022151183           var16
17  0.0356251877  0.0463201251           var17
18 -0.0227480951 -0.0256067563           var18
19  0.0058185044  0.0029210133           var19
20  0.0144262586  0.0134831768           var20

and the VIP for the 20 independent variables for the two components
> vip_df
             var1      var2      var3      var4     var5      var6     var7     var8      var9     var10     var11      var12
Comp 1 0.06471948 0.6668563 0.9320639 0.4198359 2.630382 0.1832526 2.008911 1.226416 0.4379269 0.6843267 0.7424988 0.04142026
Comp 2 0.69115508 1.0336675 1.1569448 0.6439488 2.301673 0.7875278 1.751718 1.164336 0.6501250 0.6822490 0.6735736 0.50971512
           var13     var14     var15     var16    var17     var18     var19     var20
Comp 1 0.4327117 0.7784598 0.3482450 0.3494832 1.534567 0.9798821 0.2506341 0.6214161
Comp 2 0.3758360 0.8971768 0.6536609 0.5688917 1.323625 0.8893241 0.3477479 0.6292981

Using this script (for regression coefficients)
library(pls)
library(tidyverse)
library(egg)

y_df <- as.matrix(df[,1])
x_df <- as.matrix(df[,2:21])
df_plsr <- mvr(y_df  ~ x_df , ncomp = 2, method = "oscorespls" , scale = T)

# Regression coefficients
df_coef <- as.data.frame(coef(df_plsr, ncomp =1:2))
df_coef <- df_coef %>%
  dplyr::mutate(variables = rownames(.)) %>%
  dplyr::mutate(variables = factor(variables,
                                   levels = c(
                                     "var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8",
                                     "var9", "var10", "var11", "var12", "var13", "var14", "var15", "var16", "var17",
                                     "var18", "var19", "var20")
                                      ))

colnames(df_coef) <- c("comp1", "comp2", "indep_variables")
df_coef

and for VIP
VIP <- function(object) {
  if (object$method != "oscorespls")
    stop("Only implemented for orthogonal scores algorithm.  Refit with 'method = \"oscorespls\"'")
  if (nrow(object$Yloadings) > 1)
    stop("Only implemented for single-response models")

  SS <- c(object$Yloadings)^2 * colSums(object$scores^2)
  Wnorm2 <- colSums(object$loading.weights^2)
  SSW <- sweep(object$loading.weights^2, 2, SS / Wnorm2, "*")
  sqrt(nrow(SSW) * apply(SSW, 1, cumsum) / cumsum(SS))
}

vip_df <- as.data.frame(VIP(df_plsr))

As a number of studies report only the values for the 1st component, below is the VIP and regression coefficients for the 20 independent variables for 1st component only.
Figure1

Using this script
library(pls)
library(tidyverse)
library(egg)

y_df <- as.matrix(df[,1])
x_df <- as.matrix(df[,2:21])
df_plsr <- mvr(y_df  ~ x_df , ncomp = 2, method = "oscorespls" , scale = T)

# Regression coefficients

df_coef <- as.data.frame(coef(df_plsr, ncomp =1:2))
df_coef <- df_coef %>%
  dplyr::mutate(variables = rownames(.)) %>%
  dplyr::mutate(variables = factor(variables,
                                   levels = c(
                                     "var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8",
                                     "var9", "var10", "var11", "var12", "var13", "var14", "var15", "var16", "var17",
                                     "var18", "var19", "var20")
                                      ))

colnames(df_coef)[1] <- "regression_coefficients"
df_coef  <- df_coef[, -c(2)]

# VIP

VIP <- function(object) {
  if (object$method != "oscorespls")
    stop("Only implemented for orthogonal scores algorithm.  Refit with 'method = \"oscorespls\"'")
  if (nrow(object$Yloadings) > 1)
    stop("Only implemented for single-response models")

  SS <- c(object$Yloadings)^2 * colSums(object$scores^2)
  Wnorm2 <- colSums(object$loading.weights^2)
  SSW <- sweep(object$loading.weights^2, 2, SS / Wnorm2, "*")
  sqrt(nrow(SSW) * apply(SSW, 1, cumsum) / cumsum(SS))
}

vip_df <- as.data.frame(VIP(df_plsr))
vip_df_comp1 <- vip_df[1, ]
row.names(vip_df_comp1)<-NULL

combine_vip_coef <- vip_df_comp1 %>%
  tidyr::gather("variables", "VIP") %>%
  dplyr::full_join(., df_coef, by = c("variables")) %>%
  dplyr::mutate(variables = factor(variables,
                                   levels = c(
                                     "var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8",
                                     "var9", "var10", "var11", "var12", "var13", "var14", "var15", "var16", "var17",
                                     "var18", "var19", "var20")
  ))

p1_vip <-
  ggplot(combine_vip_coef, aes(x = variables,  y = VIP, group =1))+
  geom_bar(stat="identity",  fill = "black") +
  geom_hline(yintercept = 1, size = 0.55, linetype = 3) +
  theme_bw()+
  theme(axis.text.x = element_text(angle=65,
                                   hjust=1,
                                   size = 6),
        axis.title.y = element_text(size = 10))+
  labs(x= "")

p2_coef <-
  ggplot(df_coef, aes(x = variables, y = regression_coefficients, group = 1))+  geom_bar(stat = "identity",  fill = "black")+
  theme(axis.text.x = element_text(angle=65,
                                   hjust=1,
                                   size = 8),
        axis.title.y = element_text(size = 2))+
  theme_bw()+
  theme(axis.text.x = element_blank())+
  labs(x="")

p_fin <- grid.draw(egg::ggarrange(p2_coef , p1_vip , ncol=1))

In addition, there is also, the correlation loadings,
Figure2

That I got using this script
S_plsr <- scores(df_plsr)[, comps= 1:2, drop = FALSE]
cl_plsr <- cor(model.matrix(df_plsr), S_plsr)
df_cor <- as.data.frame(cl_plsr)

df_depend_cor <- as.data.frame(cor(df[,1], S_plsr))

plot_loading_correlation  <-  rbind(df_cor ,
                                    df_depend_cor)

plot_loading_correlation1 <- setNames(plot_loading_correlation, c("comp1", "comp2"))

#Function to draw circle
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  return(data.frame(x = xx, y = yy))
}

dat_plsr <- circleFun(c(0,0),2,npoints = 100)

ggplot(data=plot_loading_correlation1 , aes(comp1, comp2))+
  ylab("")+xlab("")+ggtitle(" ")+
  theme_bw() +
  geom_hline(aes(yintercept = 0), size=.2, linetype = 3)+
  geom_vline(aes(xintercept = 0), size=.2, linetype = 3)+
  geom_text_repel(aes(label=rownames(plot_loading_correlation1),
                      colour=ifelse(rownames(plot_loading_correlation1)!='dependent', 'red','darkblue')))+
  scale_color_manual(values=c("red","darkblue"))+
  scale_x_continuous(breaks = c(-1,-0.5,0,0.5,1))+
  scale_y_continuous(breaks = c(-1,-0.5,0,0.5,1))+
  coord_fixed(ylim=c(-1, 1),xlim=c(-1, 1))+xlab("axis 1")+
  ylab("axis 2")+ theme(axis.line.x = element_line(color="darkgrey"),
                        axis.line.y = element_line(color="darkgrey"))+
  geom_path(data=dat_plsr ,
            aes(x,y), colour = "darkgrey")+
  theme(legend.title=element_blank())+
  theme(axis.ticks = element_line(colour = "black"))+
  theme(axis.title = element_text(colour = "black"))+
  theme(axis.text = element_text(color="black"))+
  theme(legend.position='none')+
  theme(panel.grid.minor = element_blank()) +
  geom_segment(data=plot_loading_correlation1, aes(x=0, y=0, xend=comp1, yend=comp2),
               arrow=arrow(length=unit(0.2,"cm")), alpha=0.75,
               colour=ifelse(rownames(plot_loading_correlation1)=='dependent', 'red','darkblue'))

I thought about a different way to present the correlation loadings for one component only instead of using biplot for two components. I thought this way could be an alternative for the VIP and the regression coefficients.
In the plot below:
 - The sign (+ve or -ve) will indicate the direction of the relationship (similar to regression coefficients (above);
 - The value will indicate the importance of the variable (similar to the VIP). I thought this correlation loadings values could be classified roughly in three classes to show how important the variable

0 - 0.33 (low correlation-not significant variable);
0.34-0.66 (medium correlation);
0.67-1 (strong correlation - influential variable).

Below is the barplot of the correlation loadings
Figure3

using this script
plot_loading_correlation_bar <-  plot_loading_correlation1 %>%
  dplyr::mutate(variables = rownames(.)) %>%
  dplyr::rename(loading_correlations = comp1)

plot_loading_correlation_bar <- plot_loading_correlation_bar[-21, ]

plot_loading_correlation_bar$variables <- factor(plot_loading_correlation_bar$variables,
                                                    levels = c(
                                                      "var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8",
                                                      "var9", "var10", "var11", "var12", "var13", "var14", "var15", "var16", "var17",
                                                      "var18", "var19", "var20")
                                                    )

ggplot(plot_loading_correlation_bar, aes(x = variables, y = loading_correlations,  group =1))+
  geom_bar(stat="identity",  fill = "black") +
  scale_y_continuous(limits=c(-1, 1), breaks=seq(-1,1,0.2))+
  theme_bw()+
  theme(axis.text.x = element_text(angle=65,
                                   hjust=1,
                                   size = 8))

My question:
Is it fine to present and interpret the results this way (barplot of correlation loading; figure3) instead of figure 1 or figure2?
What is the difference between correlation loading and regression coefficients?Why the direction of the relationship (sign +ve or -ve) is different in the regression coefficients and the loading correlation?
For example:
var2:

-ve regression coefficient; and
+ve loading correlation

Data
Check this excel file or the data below
structure(list(dependent = c(0.210720313902697, 0.556457097238138,
0.140269050558883, 0.655286941450933, 0.584554491981012, 0.726096510503198,
0.624048857090668, 0.651458377787164, 0.606566564109724, 0.394748412063493,
0.700273621652429, 0.269069210356057, 0.711827077631059, 0.403766493663418,
0.659732479680614), var1 = c(6.38465773020042, 9.78985858901589,
1.50356582433208, 7.65339989412654, 5.49752073990677, 3.90059314867797,
5.9409185818413, 17.4685189240362, 1.3046321051485, 5.8492626793834,
0, 22.6041793153936, 32.247470774266, 14.1062362886855, 0.843516522957992
), var2 = c(54.6485735221962, 47.7105460942601, 87.9747102844327,
51.8527177042445, 52.9410050371843, 59.9933071920204, 59.2002531727095,
6.30611865292691, 75.3578079057062, 47.5217195679834, 73.9175281998161,
33.0000057897949, 17.7895691750417, 50.7102968695948, 71.2739582196542
), var3 = c(38.9667687476034, 42.499595316724, 10.5217238912352,
40.3249893635017, 41.4876095819823, 35.8078469269088, 34.8588282454492,
76.2253624230368, 23.3375599891453, 46.6290177526332, 26.0824718001839,
44.3958148948115, 49.9629600506923, 35.1834668417197, 27.8825252573878
), var4 = c(6.38465773020042, 2.68799095824262, 3.83422657879301,
3.10357361725961, 5.72101253575559, 0.582584426668564, 0.310429629241765,
23.5236369297241, 0, 7.20751236564974, 0, 16.9040756921581, 11.776860021365,
1.14463243233264, 5.41431731847442), var5 = c(0, 6.00876004990636,
4.60414912318469, 21.2125301554554, 20.5553248044751, 23.1934190213699,
16.0656101595563, 8.76631382526795, 0.991353257873467, 2.82722923028608,
33.1101411654781, 1.4597129328763, 27.0133941457253, 7.18460363621184,
34.038045603972), var6 = c(29.812474631149, 16.9997855063674,
16.9484733602449, 12.2962240035803, 19.4487078537099, 13.3162195315644,
39.7673906398948, 0, 71.0194979738029, 21.4164343190374, 23.4607809727846,
5.70504485786588, 5.4888828491522, 11.8151517616597, 25.9226181415465
), var7 = c(63.8028676386506, 74.3034634854837, 74.6131509377774,
63.3876722237047, 54.2749548060594, 62.9077770203972, 43.8565695713072,
67.7100492450079, 27.9891487683237, 68.5488240850268, 43.4290778617374,
75.9311665170997, 55.7208629837575, 79.8556121697958, 34.6250189360071
), var8 = c(0, 0, 55.1128728093047, 6.51090929661756, 7.75041439460692,
7.95893058086494, 4.14643495765247, 0, 0, 2.54938917987072, 20.7580709729255,
0, 0.471612101548274, 0, 11.324878993859), var9 = c(35.3896058163273,
30.7432333308418, 5.10851905044963, 39.847735861398, 30.6826416725748,
31.7526597888325, 19.4881703291849, 32.2650320691635, 12.3518678893064,
39.099244961592, 7.05710760397929, 58.8850326871133, 50.9556529985623,
41.3555074493459, 11.2694297654175), var10 = c(9.88107227563547,
27.8058034065098, 7.06352142365074, 6.11279891418493, 13.8435635061977,
3.3341124261228, 68.1839854780714, 27.5842838556726, 77.3296388036071,
25.7336688950093, 10.8093669952665, 14.9315696591391, 19.6728001455373,
3.66489820552796, 1.35155474262874), var11 = c(0, 27.9788730214221,
22.4789874821358, 10.9120473953551, 28.3402024501966, 10.44521479672,
8.18140923509129, 0, 10.1563537553917, 26.509122106797, 13.9141292330255,
0, 15.2986691974923, 0, 66.5277988300636), var12 = c(54.7293219080373,
0, 0, 34.630486683079, 16.1537362551322, 43.2860322394116, 0,
40.1506840751639, 0, 0, 45.0619898493789, 26.1833976537477, 13.6012655568597,
54.7388963413616, 4.60393247565709), var13 = c(0, 3.62885344924762,
49.7156992962877, 22.8742965465896, 16.1037497470103, 26.4108812066677,
0, 8.76631382526795, 0, 2.28989887729153, 41.204133609971, 1.4597129328763,
24.7165698755149, 0, 22.1203660572375), var14 = c(68.3924324838086,
46.7825172630918, 40.2918029150206, 50.9952195120652, 52.4616283965265,
49.8656534812195, 29.2832265709446, 73.9920650538172, 17.3120134354451,
47.8438174044157, 35.5069030606913, 69.5515837880942, 62.4089302957283,
70.6441053507101, 61.3184871234248), var15 = c(9.34306648617202,
16.9407115250502, 0, 3.02375098621997, 7.49142545780987, 0.739987935905543,
47.9639272491623, 0, 45.1441255322013, 15.8949013302606, 2.37812587269736,
4.51166909866183, 1.22573367622539, 1.41402832539501, 0.0517684668983939
), var16 = c(0, 10.2631744532951, 3.69657384646686, 3.51661128643063,
5.38382097572785, 4.32919720682223, 17.0908901169596, 0, 26.3981021895912,
10.2162013196148, 1.64750150735275, 0, 0.3886506275644, 11.6553395492559,
0.672088258465062), var17 = c(0, 0, 1.70442568792041, 5.71976303452223,
5.0036595147534, 6.56182424061139, 4.14643495765247, 0, 2.17838680427836,
2.86071234634266, 3.2464151051141, 0, 0.471612101548274, 0, 0.724170451915613
), var18 = c(31.4915152861977, 13.0626789594875, 4.58852637794785,
8.07133532113299, 12.3784892438516, 1.41827358332991, 6.77359973391309,
28.7019160437988, 0.147389283434159, 20.7639813245479, 4.55174136965654,
40.9469057406407, 34.0553180610046, 0, 10.1596460774595), var19 = c(0,
27.9788730214221, 21.8693540906945, 5.7796242808183, 21.9582513650753,
1.38173157278083, 8.18140923509129, 0, 6.30678806617604, 25.9258663994387,
4.4796460797524, 0, 15.2986691974923, 0, 48.1840998259004), var20 = c(9.88107227563547,
15.9350047162996, 0, 1.62740092981472, 10.4709347286845, 0, 68.1839854780714,
27.5842838556726, 77.3296388036071, 20.9061094414991, 0, 14.9315696591391,
19.6728001455373, 3.66489820552796, 0)), row.names = c(NA, -15L
), class = c("tbl_df", "tbl", "data.frame"), .Names = c("dependent",
"var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8",
"var9", "var10", "var11", "var12", "var13", "var14", "var15",
"var16", "var17", "var18", "var19", "var20"))

Answer: If there is no specific reason to inspect how the variables are projected to each latent variable, why not go with the regression coefficients?
I find biplot useless when there are many latent variables(LVs) in PLS model and when their contributions to the model are somewhat close. By doing biplot you simply ignore all LVs after the 2nd LV and this is not desirable for most cases. In addition drawing, for instance, bar plot for 10 different loading doesn't sound good neither. Since regression coefficients combine all LVs, observation of them makes more sense to me.
If I understand you correctly, the loading correlation can be problematic too. Assume a case where there are 3 variables which are very correlated but also important for regression. Then you may get small loading values(regardless of their sign) for each of them and this may deceive you that they are negligible.
The question about signs is a good one. Generally not the sign but the magnitude of a variable's loading matters most. For 1 LV, a variable can negatively contribute to the dependent variable and in other LV positively. Inspecting total effect is, as I mentioned, is possible by looking at regression coefficients.
BTW, these are all my personal opinions and you may want something different from your data and you might want to inspect loadings of each LV. For example LVs obtained from a NIR spectrum may correspond to the spesific compounds in a solution etc.
Edit: According to the comments OP needs a more clear answer:
There are 2 main algorithms for PLS regression: The older and original one is NIPALS and there is the newer one called SIMPLS which is faster and provides more interpretable results. I believe your confusion lies in the differences among them and I was once quite confused until I went through the original papers.
In NIPALS, the vector $b$ is obtained for each component(latent variable) and it is the regression coefficients between X scores and Y scores. Since building a model with NIPALS involves deflation of X and Y matrices, prediction takes place by going in the reverse way and building Y block component by component.
In SIMPLS, the authors figured out that it is possible to come up with a model that is equivalent with NIPALS(at least for PLS1 case) but without deflation so the obtained matrices can be applied on the X directly. This includes the prediction of Y with a single $b$ that is the regression coefficents to be used on X to predict Y directly.
PLS1(1 refers to having a single column of Y) with SIMPLS:

m: number of observations
n: number of variables
a: number of components(LVs)

$\mathbf T_{(m \times a)} = \mathbf X_{(m \times n)} \mathbf R_{(n \times a)} $ where T is your X scores for a components, R is the weights for a components
$\mathbf {\hat Y_{(m \times 1)}} = \mathbf T_{(m \times a)} \mathbf Q'_{(1 \times a)}$ This is the prediction step.
Therefore you can define the regression coefficents:
$\mathbf B_{(n \times 1)} = \mathbf R_{(n \times a)}  \mathbf Q'_{(1 \times a)}$
So the prediction can be done with a single regression matrix B
$\mathbf {\hat Y_{(m \times 1)}} = \mathbf X_{(m \times n)} \mathbf B_{(n \times 1)} $
As you can see the size of B is independent of how many components you choose. So, to summarize, if you go with SIMPLS which I highly recommend, the regression coefficents I was talking about is the B in the above equation and can be used directly on the X matrix thus you can use their values for interpretation without dealing with loadings.
EDIT 2: I have encountered some R libraries which reports regression coefficents for multiple components. In that case, if SIMPLS algorithm is used, the regression coefficent for n components is the nth column of that matrix. You can test them by multiplying the X with that column to see whether you obtain the same prediction results (do not forget to account for mean centering).
Here are the papers for NIPALS and SIMPLS, respectively:
Geladi, Paul, and Bruce R. Kowalski. "Partial least-squares regression: a tutorial." Analytica chimica acta 185 (1986): 1-17.
De Jong, Sijmen. "SIMPLS: an alternative approach to partial least squares regression." Chemometrics and intelligent laboratory systems 18, no. 3 (1993): 251-263.
Sorry for the long answer.
Comment: Many thanks for your time and help. It is my first time to use PLSR. As you can see, I used only two components; two latent variables (ncomp = 2) and 20 independent variables. I got the regression coefficients for each of the 20 independent variables for each of the two components. I didn't understand the point that that the regression coefficient inspect the total effect.
Comment: I will highly appreciate if you could tell me the difference between regression coefficients and loading correlations.
Comment: The coefficents you obtained are PLSR loadings (or weights). Size of the regression coefficents are independent of the number of components you choose.
Comment: Check my answer on this link and let me know if it is still not clear so that I can expand my answer here accordingly: https://stats.stackexchange.com/questions/285809/what-is-the-relationship-between-pls-regression-discriminant-analysis-plsr-pl
Comment: I'm sorry. It is still not clear to me.
Comment: I will highly appreciate if you could expand the answer here.
Comment: See the updated answer. I hope it clears out your confusions.
Comment: Sorry for my late reply. Thanks for your time and help.


Grading of Cech-de Rham cohomology

Question: I am currently self-studying Bott and Tu. In chapter 2 the Cech-de Rham cohomology is introduced and I thought I had understood it well enough. However when I got to chapter 3 on spectral sequences I could not follow; some lines of reasoning used by the authors prompted to me think that maybe I have misunderstood something in the Cech-de Rham cohomology. So I would like some help on clarifying some concepts:
1) The Cech-de Rham complex $C^{p,q}$ is bi-graded, but when we take the D-cohomology (some call it the total cohomology) the groups $H^n_D$ are only 'single-graded', with index $n=p+q$ right? If yes then what does $H^{p,q}_D\{C^*(\mathfrak{U},\Omega^*)\}$ mean in P.167 (14.16.1)?
2) (If the answer to (1) is yes) Is there a natural way to grade $H^n_D$, ie. something like $H^n_D=\bigoplus_{p+q=n} H^{p,q}_D$? In particular can we talk about $H^n_D$ restricted to $K^{p,q}$? My intuition says this is problematic since the differential operators $D$ between $\bigoplus_{p+q=n}K^{p,q}$ 'blends' different $(p,q)$ together. The reason I am asking this is that in P.164 it is reasoned that 'the bidegrees $(p,q)$ of the double complex $K$ persist in the spectral sequence $E_r$', this I understand but what comes after is very puzzling for me: the filtration on $H^n(K)=F^n_0 \supset F^n_1 \supset F^n_2 \supset ...$ (I am using the notation from Theorem 14.6 for convenience here) has quotients $E^{0,n}_\infty,E^{1,n-1}_\infty,...$ repectively, that is, $F^n_0/F^n_1=E^{0,n}_\infty$ etc. I see no reason why this should hold, and have no idea how to prove it especially if the bigrading on $E_r$ does not carry over to the complex $A_\infty$.
It would be very appreciated if someone can help clarify point (1) for me, and point out how the statement (14.13) I have mentioned in P.164 holds (feel free to ignore my concerns and write a fresh proof, as I may have gone off in the wrong direction in the first place)
Answer:
You are quite right that $H_D^n$ is single-graded. However, there is a filtration$$H_D^n = F_0 \supset F_1 \supset F_2 \supset \ldots \supset F_n \supset 0$$on $H_D^n$, and $H_D^{p, n - p}$ is defined to be the quotient$$H_D^{p, n - p} = F_p/F_{p + 1}.$$Note that $H_D^{p, n - p}$ is not a subgroup of $H_D^n$. Rather, it is a subquotient of $H_D^n$, a quotient of a subgroup. Thus, there is an equality$$GH_D^n = \bigoplus_{p = 0} F_p/F_{p + 1} = \bigoplus_{p + q = n} H_D^{p, q},$$where $GH_D^n$ is the associated graded complex of the filtered complex $H_D^n$.
In general, $GH_D^n$ is not isomorphic to $H_D^n$. For example, if $H_D^n$ is $\mathbb{Z}_4$, the cyclic group of order $4$ and the filtration is$$\underbrace{\mathbb{Z}_4}_{= \,F_0} \supset \underbrace{\mathbb{Z}_2}_{= \,F_1} \supset 0,$$then the associated graded is$$G\mathbb{Z}_4 = {{F_0}\over{F_1}} \oplus F_1 = \mathbb{Z}_2 \oplus \mathbb{Z}_2,$$which is not isomorphic to $\mathbb{Z}_4$.
However, if we are working in the category of vector spaces, then since the dimension is the only invariant of a finite-dimensional vector space, $H_D^n$ is isomorphic to its associated graded complex.
Can you show that $H_D^n$ and $GH_D^n$ have the same dimension? It is because$$\dim(F_p/F_{p + 1}) = \dim F_p - \dim F_{p + 1}.$$The Čech-de Rham complex is of course a vector space and so (14.16.1) is to be interpreted as$$H_{\text{dR}}^k(M) \simeq GH_{\text{dR}}^k(M) = \bigoplus_{p + q = k} H_D^{p, q}\{C^*(\mathfrak{U}, \Omega^*)\},$$where$$H_D^{p, q}\{C^*(\mathfrak{U}, \Omega^*)\} = F_p/F_{p + 1}$$for the induced filtration $\{F_p\}$ on $H_D^k\{C^*(\mathfrak{U}, \Omega^*)\}$.
As with many texts in algebraic topology, "$=$" is often used to mean "isomorphism." This may have caused some confusion.
Although in the category of vector spaces, one can write$$H_D^n \simeq \bigoplus_{p + q = n} H_D^{p, q},$$this isomorphism is not canonical, i.e. there are no natural ways of identifying certain subspaces of $H_D^n$ as $H_D^{p, q}$, because, as I pointed out earlier, $H_D^{p, q}$ is a subquotient of $H_D^n$, not a subspace.
For a smooth complex projective variety, there is a canonical decomposition$$H^n \simeq \bigoplus_{p + q = n} H^{p, q},$$the famous Hodge decomposition, but that is an altogether different story.
As for the equality $F_0^n/F_1^1 = E_\infty^{0, n}$, that is simply the definition of $E_\infty^{0, n}$. There is nothing to prove there.

Thank you for the detailed reply, I understand point (1) completely now! However for point (2) I thought $E_\infty^{0, n}$ was defined to be the stable value of $E_1^{0, n}$, $E_2^{0, n}$, $\ldots$ , where $E_r^{0, n}$ is the $(0, n)$th entry in the $r$th page $E_r$. One definition or another, how can we show these two are same?

You are quite right that $E_\infty^{p, q}$ is the stationary value of $E_r^{p, q}$ as $r \to \infty$. Now each $E_r$ is the cohomology of the preceding page $E_{r - 1}$, and on $E_r$ there is a filtration$$E_r = F_0E_r \supset F_1E_r \supset F_2E_r \supset \ldots.$$
We define $E_r^{p, n - p}$ to be the quotient $F_pE_r^n/F_{p + 1}E_r^n$, where $E_r^n$ is the antidiagonal of degree $n$.

For a fixed $n$, as we turn the pages $E_r$, each page $E_r$ is the cohomology of the preceding page $E_{r - 1}$. After finitely many steps, the pages $E_r$ become stationary. There is an $r \in \mathbb{Z}^+$ such that$$E_r = E_{r + 1} = \ldots = E_\infty.$$The filtration $\{F_pE_r\}$ also becomes stationary as $r$ increases. Thus,$$E_\infty^{p, n - p} = E_r^{p, n - p} = {{F_pE_r^n}\over{F_{p+1}E_r^n}} \simeq {{F_pE_\infty^n}\over{F_{p + 1}E_\infty^n}}.$$
Comment: Thank you for the detailed reply, I understand point (1) completely now! However for point (2) I thought $E^{0.n}_\infty$ was defined to be the stable value of $E^{0.n}_1, E^{0.n}_2, ...$, where $E^{0.n}_r$ is the $(0,n)$-th entry in the $r$-th page $E_r$. One definition or another, how can we show these two are same?
Comment: I think I got it now, thanks a lot!

Grading of Cech-de Rham cohomology


data model problem

Question: public ActionResult Index()
    {
        testEntities6 devicesEntities = new testEntities6();
        List<DevicesModel> devicesModel = new List<DevicesModel>();
        var device_query = from d in devicesEntities.device
             join dt in devicesEntities.devicetype on d.DeviceTypeId equals dt.Id
             join l in devicesEntities.location on d.Id equals l.DeviceId
             join loc in devicesEntities.locationname on l.LocationNameId equals loc.Id
                           where l.DeviceId == d.Id
                           select new {
                               //devices
                               d.Id,
                               d.DeviceTypeId,
                               d.SerialNumber,
                               d.FirmwareRev,
                               d.ProductionDate,
                               d.ReparationDate,
                               d.DateOfLastCalibration,
                               d.DateOfLastCalibrationCheck,
                               d.CalCertificateFile,
                               d.Notes,
                               d.TestReportFile,
                               d.WarrantyFile,
                               d.CertificateOfOriginFile,
                               d.QCPermissionFile,
                               d.Reserved,
                               d.ReservedFor,
                               d.Weight,
                               d.Price,
                               d.SoftwareVersion,
                               //devicetype
                               dt.Name,
                               dt.ArticleNumber,
                               dt.Type,
                               //location
                               l.StartDate, //AS LastStartDate,
                               l.LocationNameId,
                               //locationname
                               Loc_name = loc.Name //AS CarrentLocation
                           };

        foreach (var dv in device_query) {
            devicesModel.Add(new DevicesModel(
                //device
                DeviceTypeId = dv.DeviceTypeId,
      /*Why I have this error: The name 'DeviceTypeId' does not exist in the current context*/
                ...
                ));

        }
        return View(devicesModel);
    }

Model:
public class DevicesModel
 {
//device
public int deviceTypeId;
public int id;
public int serialNumber;
public string firmwareRev;
public DateTime productionDate;
public DateTime reparationDate;
public DateTime dateOfLastCalibration;
public DateTime dateOfLastCalibrationCheck;
public int calCertificateFile;
public string notes;
public int testReportFile;
public int warrantyFile;
public int certificateOfOriginFile;
public int qCPermissionFile;
public int BReserved;
public string reservedFor;
public double weight;
public int price;
public string softwareVersion;
//devicetype
public string name;
public string qrticleNumber;
public string type;
//location
public DateTime startDate;
public int locationNameId;
//locationname
public string loc_name;

public DevicesModel(){}

//device
public int DeviceTypeId{get;set;}
public int Id { get; set; }
public int SerialNumber { get; set; }
public string FirmwareRev { get; set; }
public DateTime ProductionDate { get; set; }
public DateTime ReparationDate { get; set; }
public DateTime DateOfLastCalibration { get; set; }
public DateTime DateOfLastCalibrationCheck { get; set; }
public int CalCertificateFile { get; set; }
public string Notes { get; set; }
public int TestReportFile { get; set; }
public int WarrantyFile { get; set; }
public int CertificateOfOriginFile { get; set; }
public int QCPermissionFile { get; set; }
public int bReserved { get; set; }
public string ReservedFor { get; set; }
public double Weight { get; set; }
public int Price { get; set; }
public string SoftwareVersion { get; set; }
//devicetype
public string Name { get; set; }
public string ArticleNumber { get; set; }
public string Type { get; set; }
//location
public DateTime StartDate { get; set; }
public int LocationNameId { get; set; }
//locationname
public string Loc_name { get; set; }
}

Comment: Why do you have properties and public variables? A property like `public int Id { get; set; }` work on its own, and has **no relation** to the variable `id`.
Comment: -1: What's the problem?  This is long and hard to read.  The title is vague.
Answer: Its because you're creating an anonymous object in your linq "Select" clause. I think you want to create an instance of your DevicesModel class, e.g.
...
where l.DeviceId == d.Id
select new DevicesModel {
//devices
Id = d.Id,
...

Comment: What (in this case) I write in foreach statement ie, what put in: return View(???)
Comment: Sorry I don't quite understand what you mean?
Answer: I think you intend to use curly brackets instead of round brackets.
new DevicesModel
{
   //device
   DeviceTypeId = dv.DeviceTypeId,
   ...
}

or are you trying to use named arguments in C#4?

data model problem


WebDav uploads fail on files with certain characters on Apache

Question: Have webdav uploads working great on one our boxes but anytime there is a ; # or * (and maybe a few others) the upload fails. That is expected since they're restricted characters but I'm curious if there's a way to rewrite/rename those files on their way through. We don't care what the name is really it just has to make it up to the server. Started looking at mod_rewrite solutions but my rewrite fu is rather weak.
Answer: Sounds like a problem on the WebDAV client.  These characters should be urlescape()'d before sending to the server.  Can you try a different client and see if it has the same issues?


Como chamar uma activity a partir de uma BaseExpandableListAdapter?

Question: Eu gostaria de chamar uma activity a partir do clique de um botão, porém não dá certo porque a classe de origem não é uma activity, e sim uma BaseExpandableListAdapter, existe uma forma de fazer isso?
Código:
<code>holder.btnDetalhes.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(ExpandableAdapterMenuCompleto.this, DetalhesItemContaCliente.class);
            StartActivity(i);
        }
    });
</code>
Answer: Você precisa passar o Context para sua classe Adapter e passar ele como parâmetro do Intent.
Comment: Rafael, seria interessante e tornaria a sua resposta mais completa, se mostrasse como isso é feito.


How to get an array of objects from a normal array in javascript

Question: I am trying to find a way to get an array of objects from a normal array without using a for loop.
An example for that I want to turn that array:
[1,2,3,4,5]
To be an array of objects like that:
[{x:1, y:2}, {x:2, y:4}, {x:3, y:6}, {x:4, y:8}, {x:5, y:10}]
without using a for loop
Comment: What effort have you made? This seems to be a 'do my homework for me' question. Remember that SO is here to help you to *debug code that you have written*. It is not here to write code for you.
Comment: Kinda makes no sense as well, in the example you gave us you only have `[1,2,3,4,5]` but you want to "transform" that into an array of objects that have other elements that are not present in your initial array like `6`,`8`,`10` so what kind of rule are you even following to create your array?
Answer: You could map the array to a new array of objects:
result = [1,2,3,4,5].map(i => ({x: i, y: i*2}));