Criar tabela Editável

Question: Gostava de criar uma tabela em que as suas células possam ser editadas mas que cumpram uns certos requisitos. O que eu tenho é isto:
HTML

        Cód.
        Designação
        Qnt.
        Uni.
        Preço
        Desconto
        IVA
        Sub-Total

JQUERY
$(function () {
        $("td").dblclick(function () {
            var conteudoOriginal = $(this).text();
            $(this).addClass("celulaEmEdicao");
            $(this).html("<input type='text' value='" + conteudoOriginal + "' />");
            $(this).children().first().focus();
            console.log($(this).attr('id'));
            $(this).children().first().keypress(function (e) {
                if (e.which == 13) {
                    var novoConteudo = $(this).val();
                    $(this).parent().text(novoConteudo);
                    $(this).parent().removeClass("celulaEmEdicao");
                }
            });
            $(this).children().first().blur(function () {
                $(this).parent().text(conteudoOriginal);
                $(this).parent().removeClass("celulaEmEdicao");
            });

        });
    });

Tenho também um botão para adicionar novas linhas:
//ADICIONAR LINHA
    $("#btnAdd").click(function () {
        $('#vendaTabela').append('

');
    });

Quanto aos requisitos será melhor ir por fases:
1º- Gostava de arranjar uma forma de as células serem identificadas. Sei que posso colocar id's nos td's do HTML mas quando adiciono nova linha, como é que elas são identificadas?
2º- Como posso colocar função por colunas? Isto é, quero que a coluna 'Sub-Total' seja a multiplicação  da coluna 'Qnt.' com a coluna 'Preço'. Consigo fazer a função para a primeira linha já criada no HTML, mas, como fazer para quando adiciono outra linha ?
3º- Como posso colocar um autocomplete na coluna que quiser, por exemplo na coluna da 'Designação' ?
Comment: você pode adicionar um plugin ao projeto? neste caso eu aconselho utilizar o jsView para auxiliar.
Comment: Poder posso, mas não conheço esse plugin, tenho de pesquisar sobre o seu uso.
Comment: depois posto um exemplo sobre ele, vai ajudar no momento de adicionar uma nova linha já com alguns dados.
Comment: As linhas que adiciono serão sempre iniciadas vazias. Obrigado :)
Comment: postei uma maneira alternativa de fazer esta tabela utilizando jsViews, você pode dá uma olhada se tiver interrese.
Answer: Como eu disse mais acima, você pode fazer isto utilizando o jsViews, como no exemplo abaixo:
Para isto você precisará de uma Entidade que servira de modelo, no caso a seguinte:
{
    Codigo: "",
    Designacao: "",
    Quantidade: "",
    Unidade: "",
    Preco: "",
    Desconto: "",
    IVA: "",
    SubTotal: ""
}

Você terá os templates para cada estado da Row (Detalhes, Inserir e Criar):

Lembrando que você pode usar operações aritimeticas no template, então {{:Quantidade * Preco}} é valido e pode ser utilizado no SubTotal.
Note que utilizei as propriedades do Modelo nos templates, desta forma sabemos como preencher o template.
por fim teremos o HTML da tabela em si:

            Cód.
            Designação
            Qnt.
            Uni.
            Preço
            Desconto
            IVA
            Sub-Total

            <input name="btCreate" type="button" value="Criar" />

O Script abaixo está montando a tabela utilizando um Source, que no caso pode ser uma requisição AJAX, porém estou utilizando uma lista com 10 Entidades.
var documento = $(document);
var vendaTabela = $("#vendaTabela");
var tabelaBody = $("tbody", vendaTabela);

var tmplDetails = $.templates("#tmplDetails");
var tmplEdit = $.templates("#tmplEdit");
var tmplCreate = $.templates("#tmplCreate");

var entidades = [];
for (var i = 1; i <= 10; i++) {
    entidades.push({
        Codigo: "Codigo" + i,
        Designacao: "Designacao" + i,
        Quantidade: "Quantidade" + i,
        Unidade: "Unidade" + i,
        Preco: "Preco" + i,
        Desconto: "Desconto" + i,
        IVA: "IVA" + i,
        SubTotal: "SubTotal" + i
    });
}

tabelaBody.empty();
$.each(entidades, function (indice, entidade) {
    var novaLinha = $(tmplDetails.render(entidade));
    novaLinha.data("Entidade", entidade);

    tabelaBody.append(novaLinha);
});

documento.on("click", "#vendaTabela input[name='btCreate']", function () {
    var novaEntidade = {};
    var novaLinha = $(tmplCreate.render(novaEntidade));
    novaLinha.data("Entidade", novaEntidade);

    tabelaBody.append(novaLinha);
});

documento.on("click", "#vendaTabela input[name='btEditar']", function () {
    var linha = $(this).closest("[data-type]");
    var entidade = linha.data("Entidade");
    var novaLinha = $(tmplCreate.render(entidade));
    novaLinha.data("Entidade", entidade);

    linha.replaceWith(novaLinha);
});

documento.on("click", "#vendaTabela input[name='btSalvar']", function () {
    var linha = $(this).closest("[data-type]");
    var txtCodigo = $("[name='txtCodigo']", linha);
    var txtDesignacao = $("[name='txtDesignacao']", linha);
    var txtQuantidade = $("[name='txtQuantidade']", linha);
    var txtUnidade = $("[name='txtUnidade']", linha);
    var txtPreco = $("[name='txtPreco']", linha);
    var txtDesconto = $("[name='txtDesconto']", linha);
    var txtIVA = $("[name='txtIVA']", linha);
    var txtSubTotal = $("[name='txtSubTotal']", linha);

    var entidade = linha.data("Entidade");
    entidade.Codigo = txtCodigo.val();
    entidade.Designacao = txtDesignacao.val();
    entidade.Quantidade = txtQuantidade.val();
    entidade.Unidade = txtUnidade.val();
    entidade.Preco = txtPreco.val();
    entidade.Desconto = txtDesconto.val();
    entidade.IVA = txtIVA.val();
    entidade.SubTotal = txtSubTotal.val();

    var novaLinha = $(tmplDetails.render(entidade));
    novaLinha.data("Entidade", entidade);

    linha.replaceWith(novaLinha);

});

note que eu fiz o bind dos eventos utilizando o on direto no document, fiz isto para que estes eventos estejam disponiveis para elementos criados através do template.
pro fim o codigo na integra:

var documento = $(document);
var vendaTabela = $("#vendaTabela");
var tabelaBody = $("tbody", vendaTabela);

var tmplDetails = $.templates("#tmplDetails");
var tmplEdit = $.templates("#tmplEdit");
var tmplCreate = $.templates("#tmplCreate");

var entidades = [];
for (var i = 1; i <= 10; i++) {
    entidades.push({
        Codigo: "Codigo" + i,
        Designacao: "Designacao" + i,
        Quantidade: "Quantidade" + i,
        Unidade: "Unidade" + i,
        Preco: "Preco" + i,
        Desconto: "Desconto" + i,
        IVA: "IVA" + i,
        SubTotal: "SubTotal" + i
    });
}

tabelaBody.empty();
$.each(entidades, function (indice, entidade) {
    var novaLinha = $(tmplDetails.render(entidade));
    novaLinha.data("Entidade", entidade);

    tabelaBody.append(novaLinha);
});

documento.on("click", "#vendaTabela input[name='btCreate']", function () {
    var novaEntidade = {};
    var novaLinha = $(tmplCreate.render(novaEntidade));
    novaLinha.data("Entidade", novaEntidade);

    tabelaBody.append(novaLinha);
});

documento.on("click", "#vendaTabela input[name='btEditar']", function () {
    var linha = $(this).closest("[data-type]");
    var entidade = linha.data("Entidade");
    var novaLinha = $(tmplEdit.render(entidade));
    novaLinha.data("Entidade", entidade);

    linha.replaceWith(novaLinha);
});

documento.on("click", "#vendaTabela input[name='btSalvar']", function () {
    var linha = $(this).closest("[data-type]");
    var txtCodigo = $("[name='txtCodigo']", linha);
    var txtDesignacao = $("[name='txtDesignacao']", linha);
    var txtQuantidade = $("[name='txtQuantidade']", linha);
    var txtUnidade = $("[name='txtUnidade']", linha);
    var txtPreco = $("[name='txtPreco']", linha);
    var txtDesconto = $("[name='txtDesconto']", linha);
    var txtIVA = $("[name='txtIVA']", linha);
    var txtSubTotal = $("[name='txtSubTotal']", linha);

    var entidade = linha.data("Entidade");
    entidade.Codigo = txtCodigo.val();
    entidade.Designacao = txtDesignacao.val();
    entidade.Quantidade = txtQuantidade.val();
    entidade.Unidade = txtUnidade.val();
    entidade.Preco = txtPreco.val();
    entidade.Desconto = txtDesconto.val();
    entidade.IVA = txtIVA.val();
    entidade.SubTotal = txtSubTotal.val();

    var novaLinha = $(tmplDetails.render(entidade));
    novaLinha.data("Entidade", entidade);

    linha.replaceWith(novaLinha);

});

<link href="http://cdn.foundation5.zurb.com/foundation.css" rel="stylesheet"/>

            Cód.
            Designação
            Qnt.
            Uni.
            Preço
            Desconto
            IVA
            Sub-Total

            <input name="btCreate" type="button" value="Criar" />

Comment: Agradeço-lhe desde já a colaboração e ajuda. Testei e está bastante completo, mas para o que estou a fazer, e para o nível exigido é um bocado complexo :D De qualquer forma Obrigado e merece o meu voto
Comment: Acredito que uma vez que você entenda o `jsRender`, o script em si fica facil de entender, até pq a unica coisa que ele faz é substituir um template pelo outro.
Answer: 1. Você pode resolver utilizando classes nas colunas e id's nas linhas para identificá-las. Por exemplo:

    123
    Teste

    123
    Teste

var $numero = $("#linha1 .numero"); // coluna número na linha 1

2. Seguindo o exemplo anterior, você poderia fazer algo assim:
var subTotal = 0;
var $linhas = $("#minhaTabela tbody > tr");
// aconselho a usar o tbody na seleção para evitar selecionar o header e o footer

// percorre todas as linhas
$linhas.each(function() {
    // o this refere-se a linha
    var qtd = $(".quantidade", this).html();
    var vlr = $(".valor", this).html();

    // tenta converter para float e retorna 0 caso esteja em branco
    qtd = parseFloat(qtd) || 0;
    vlr = parseFloat(vlr) || 0;

    subTotal += qtd * vlr;
});

$("#subTotal").html(subTotal);

Para atualizar o subtotal ao adicionar uma linha é simples: coloque o código acima em uma função e chame novamente sempre que necessário.

3. Não entendi sua dificuldade aqui, você não sabe adicionar um autocomplete? Por você já utilizar o jQuery, pode utilizar o jQueryUI. Veja a página de exemplo.
Comment: Desde já agradeço a sua ajuda. Olhando ao ponto 1 da sua resposta, a minha dúvida é a seguinte: Eu no meu html só tenho um 'tr' onde defino o id que quero. Se atentar à minha função de adicionar linha, o que ponho nesse 'tr' para ir encrementando o id quando adicionar a linha nova?
Comment: Se possível utilize um contador. Caso contrário, uma solução bem dinâmica é utilizar o número de linhas atuais, por exemplo: `var id = 'linha' + $("#minhaTabela tbody > tr").length;`.
Comment: Obrigado pela ajuda. Não estou a entender esta parte: var $numero = $("#linha1 .numero"); // coluna número na linha 1
Fiz o console.log($numero) e o retorno é um objeto, pode explicar-me melhor para que serve ou o que faz essa linha?
Comment: Ele irá retornar um objeto jQuery. Tente, por exemplo, executar `console.log($numero.html())`. Você está utilizando este objeto jQuery no seu código, mesmo sem perceber.
Comment: retorna-me undefined :s Tenho isto:  Cód.    ... var $codigo = $("#tr0 .codigo");
Comment: Você está confundindo um pouco os seletores css. O seletor `#tr0 .codigo` irá procurar pelo elemento com classe `.codigo` **dentro** de um elemento com id `#tr0`. Faça o teste novamente com um html assim: `123`.
Comment: Dessa forma funciona :D Mas eu não posso ter o Header na minha tabela? isto é os  ?
Comment: Pode sim, mas de uma olhada novamente no código html que você tentou antes: ` Cód.  `. A coluna `.codigo` não está dentro da linha `#tr0`.
Comment: Já pus da forma que queria mas tou com um problema agora. Eu com o codigo quero ir buscar a designaçao. Para tal, estou a usar o var id = $(".codigo",this).html(); dentro do each das linhas. Se eu fizer o console log, ele faz o print de tudo. No caso do subtotal, está a funcionar direito porque ele pega o valor certo, mas para ir buscar a designação em que tenho de passar o id, tenho o metodo getDesignacaobyId(id) dentro do each das linhas e como percorre todas as linhas ele pega sempre na primeira designação. Sabe como posso dar a volta a isto ?
Comment: Segue o mesmo exemplo de como obter o valor. Adiciona uma classe para a coluna desejada e para obter basta executar `$(".designacao", this).html()` dentro do loop.
Comment: Eu segui exatamente da mesma maneira, mas o problema reside aqui: eu tenho dois each's: $linhas.each(function (){...e depois para ir buscar a designacao por ajax tenho ...  if (data.length > 0) {
                        $.each(data, function (key, value) {... logo dentro deste ultimo each, o this ($(".designacao", this).html()) não funciona.
Comment: Acredito que seja melhor você criar uma nova pergunta para este problema.
Comment: ok assim o farei então

Criar tabela Editável


$M \lambda=\mu \implies \lambda \geq \mu$, where $\lambda, \mu$ are partitions of $n$, and $M$ is a $n \times n$ double stochastic matrix

Question: I have $\lambda, \mu$ as 2 partitions of $n \ (=|\lambda|=|\mu|)$, and  I have been given that $M \lambda=\mu$, where M is a $n \times n$ double stochastic matrix i.e. $$\sum_im_{ij}=\sum_jm_{ij}=1$$
I have to show that $$\lambda \geq \mu  \ \ \ \ \ \ \ \ (\text{i.e.} \sum_{k=1}^i\lambda_k \geq \sum_{k=1}^i\mu_k \ , \ \forall i \geq 1)$$
Now, we know that $$\sum_jm_{ij}\lambda_j=\mu_i \ \ \ \ \ \ \ \ \ i\geq1$$
For $i=1$
$$\mu_1 = \sum_jm_{1j}\lambda_j \leq \lambda_1 \Big(\sum_jm_{1j} \Big)=\lambda_1$$
But I'm unable to proceed with proving the general inequality, any help will be appreciated. This is an example from Macdonald's Symmetric Functions and Hall Polynomials
Comment: First: what is a partition? a positive vector with the sum of elements equal $n$ and sorted in decreasing order?
Second: $\lambda \ge \mu$ means that the partial sum of $\lambda$ are greater than the ones of $\mu$? reformulate the definition, please
Comment: Hint: Lemma 1 in http://artofproblemsolving.com/community/c6h19097p140145 (the "symmetric majorization criterion") shows that you only need to check that every real $x$ satisfies $\sum_{k=1}^n \left|\lambda_k - x\right| \geq \sum_{k=1}^n \left|\mu_k - x\right|$. But the triangle inequality shows that each $i \in \left\{1,2,\ldots,n\right\}$ satisfies $\sum_{k=1}^n m_{i,k} \left|\lambda_k - x\right| \geq \left|\mu_i - x\right|$ (because if you omit the absolute values, then this becomes an equality). Sum this up over all $i$, and you get the inequality you need.
Comment: Yes, precisely what you said. Fixed
Answer: $$
\sum_{k=1}^i \mu_k = \sum_{k=1}^i (M\lambda)_k = \sum_{k=1}^i \sum_j m_{kj}\lambda_j = \sum_j \lambda_j \cdot \sum_{k=1}^i m_{kj}
$$
Define $p_j = \frac 1i\sum_{k=1}^i m_{kj}$. From the hypotesis you get $p_j\le 1/i$ and $\sum_j p_j= 1$.
$$
\sum_{k=1}^i \frac 1i\mu_k =  \sum_j \lambda_j p_j = \sum_{k=1}^i \frac 1i\lambda_k  - \sum_{k=1}^i \lambda_k \left(\frac 1i -p_k\right) +\sum_{k=i+1}^n \lambda_k p_k
$$
$$
\le \sum_{k=1}^i \frac 1i\lambda_k  - \lambda_i\sum_{k=1}^i \left(\frac 1i -p_k\right) +\lambda_{i+1}\sum_{k=i+1}^n  p_k
$$
but $\sum_{k=1}^i \left(\frac 1i -p_k\right) = 1 - \sum_{k=1}^i p_k = \sum_{k=i+1}^n  p_k $, so
$$= \sum_{k=1}^i \frac 1i\lambda_k  -(\lambda_{i}-\lambda_{i+1})\sum_{k=i+1}^n  p_k
\le \sum_{k=1}^i \frac 1i\lambda_k
$$


Normality is not transitive

Question:
Let $G=S_3\times S_3$ where $S_3$ is the symmetric group. Let $p=
        \begin{pmatrix}
        1 & 2 & 3 \\
        2 & 3 & 1 \\
        \end{pmatrix}
$, let $L=(p)$, $K=L\times L$ and $H=\{(I_3,I_3),(p,p),(p^2,p^2)\}$. Show that $K\triangleleft G$, $H\triangleleft K$ but $H$ no is a normal subgroup of $G$.

I wonder if there a quick way to do this exercise, without having to develop each of the products.
Answer: $K \trianglelefteq G$ is easy to be seen because each $L \trianglelefteq S_3$ as $L$ has index 2 in $S_3$. For $H \trianglelefteq K$, we can use the fact:
If $H$ has a prime index $p$ in $G$ and there is no prime divisor of $|G|$ less than $p$, then $H \trianglelefteq G$.
$|K:H| = 3$ and there is no smaller prime dividing $|K|=9$, so we see that $H \trianglelefteq K$.
Now $H$ not being normal in $G$ should be easy done by one computation $((1,2),e)*(p,p)*((1,2),e)  = ((1,3,2),e)\notin H$
Answer: Using generators, there won't be much computation needed.
$K$ is generated by $(1,p)$ and $(p,1)$. If $H$ is invariant under conjugation by the generators of $K$, then $H$ is normal in $K$.
Lagrange's theorem proves that $S_3$ is generated by $p$ and any transposition, e.g. $t=
        \begin{pmatrix}
        1 & 2 & 3 \\
        2 & 1 & 3 \\
        \end{pmatrix}$. So $G$ is generated by $(1,p),(1,t),(p,1),(t,1)$.
Conjugating these subgroups by the '$p$' generators will be easy, you just need to worry about $tpt^{-1}$.
Comment: I do not understand the next part: If H is invariant under conjugation by the generators of K, then H is normal in K.
Answer: Observe that $|L| = 3$ and $|S_3| = 6$, so the index of $L$ in $S_3$ is $2$. Therefore, $L \lhd S_3$. Consequently, $K = L \times L \lhd S_3 \times S_3 = G$.
Then, observe that $K$ is abelian, because it is a direct product of abelian groups, so all of its subgroups are normal. Therefore $H \lhd K$.
To see that $H$ is not normal in $G$, define
$$\tau = \begin{pmatrix}
1 & 2 & 3 \\
1 & 3 & 2 \\
\end{pmatrix} \quad \text{and} \quad
\mu = \begin{pmatrix}
1 & 2 & 3 \\
1 & 2 & 3 \\
\end{pmatrix}$$
You can confirm that $\tau p \tau^{-1} = p^2$ and $\mu p \mu^{-1} = p$, and therefore
$$(\tau, \mu)(p, p)(\tau^{-1}, \mu^{-1}) = (p^2, p) \not\in H$$
Comment: If a group is the Cartesian product of two sets of finite order, the order is equal to the product of the orders?
Comment: @MGarcía Yes, that's correct. The direct product $A \times B$ is the set ordered pairs $(a,b)$ where $a\in A$ and $b \in B$, so if $|A|$ and $|B|$ are finite, then $|A \times B| = |A||B|$.

Normality is not transitive


how to add trailing stop loss?

Question: i have made a stratergy for buy and long. I want to add trailing stoploss with pips. i want like 10 pips of trailing stoploss, and want to exit based of stoploss not because of trend change. After the stoploss is hit, only enter on next trend change signal weather buy or sell. no reenter once the stop loss is hit. I tried my best but could not figure out how to add trailing stoploss and no reenter condition. your help will be really appreciated.
Thank you
<code>//@version=5
strategy('Supertrend MTF Heikin Ashi', overlay=true)

Mult = input.float(defval=0.5, title='ATR Factor',  maxval=100, step=0.1)
Period = input.int(defval=5, title='ATR Period',  maxval=100)
//Heikin Ashi high, low, close
h = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
high)
l = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low)
c = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
close)
//HeikinAshi atr
Atr = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
ta.atr(Period))
Up = (h + l) / 2 - Mult * Atr
Dn = (h + l) / 2 + Mult * Atr
float TUp = na
float TDown = na
Trend = 0
TUp := c[1] > TUp[1] ? math.max(Up, TUp[1]) : Up
TDown := c[1] < TDown[1] ? math.min(Dn, TDown[1]) : Dn
Trend := c > TDown[1] ? 1 : c < TUp[1] ? -1 : nz(Trend[1], 1)
Trailingsl = Trend == 1 ? TUp : TDown
linecolor = Trend == 1 and nz(Trend[1]) == 1 ? color.lime : Trend == -1 and
nz(Trend[1]) == -1 ? color.red : na

uptrend = Trend ==1
downtrend = Trend == -1

plot(Trailingsl, color=linecolor, linewidth=2, title='SuperTrend')

bgcolor(uptrend == 1 ? color.new(color.teal, transp = 85) : color.new(color.red,
transp = 85))
timeallowed = input.session("0945-1100", title = "allowed time")
iswithintime = time(timeframe.period, timeallowed)

if uptrend and iswithintime
    strategy.entry("long", strategy.long)

if downtrend
    strategy.close("long", qty_percent = 100)

if downtrend and iswithintime
    strategy.entry("short", strategy.short)

if uptrend
    strategy.close("short", qty_percent = 100)
</code>
Comment: About backtest in [heikin ashi] (https://www.tradingview.com/script/q9laJNG9-Backtesting-on-Non-Standard-Charts-Caution-PineCoders-FAQ/).
Answer: To trailing, use strategy.exit
<code>strategy.exit(
  'Trailing Stop',
  'long',
  trail_points   = 10,
  trail_offset   = 10)
</code>
Comment: this one works but, I don't want to enter once stop loss is hit
Comment: it is working but I don't want to enter once stop loss is hit. any idea how to fix it?

how to add trailing stop loss?


Jquery in Customer Portal in Salesforce

Question: I am trying to use a Visualforce page with Jquery UI and Salesforce Customer Portal. But Jquery UI is overriding all Standard Salesforce stylesheet. How Can i Prevent that.? Any help would be appreciated.
Answer: Can you ask this question on salesforce.stackexchange.com? Community is dedicated to salesforce.

Jquery in Customer Portal in Salesforce


Quick Integration by Parts Question

Question: So, came across the following question while I was studying for a test:
"Let $g$ be a differentiable function such that $\int g(x)e^{\frac{x}{4}}dx=4g(x)e^{\frac{x}{4}}-\int 8x^2e^{\frac{x}{4}}dx$ What's a possible expression for $g(x)$?"
We're talking about integration by parts, and I assumed that I should apply that formula ($\int udv = uv-\int vdu$) here, but it seems like after using that that there's no formula given for $g(x)$? I'm a bit lost and any help would be appreciated!
Comment: Yes, the integral of u' is u because integration is the reverse of differentiation. See calculus's answer.
Comment: $ \frac{2}{3} x^3 $?
Comment: Ok, I'm a bit confused on how you got there though.
Comment: I would differentiate both sides and compare.
Comment: Ok, so I'm still a little confused, if I take the derivative of an integral that cancels it out right? So it'd basically be the same thing as just taking the integral signs away and going from there?
Comment: IBP is: $ \int u v' dx = u v - \int u' v dx $. Here, my v' is $e^{\frac{\pi}{4}}$ and then I figured out that $u$ is $g(x)$ and must equal $\frac{2}{3} x^3$.
Comment: The formula in the question is compact but probably not as helpful for this question as my version of the formula.
Comment: Ok, I'm starting to see where you're coming from. So could you essentially just take the integral of $u'$ and get $u$ from that?
Comment: I'm just not seeing how you got $u$ from that formula.
Answer: I write down the two terms so that you can compare them.
$$\int g(x)\cdot e^{\frac{x}{4}}\, dx=g(x)\cdot 4e^{\frac{x}{4}}-\int 8x^2e^{\frac{x}{4}}\, dx$$
$\begin{array}{}  \qquad \qquad \qquad \qquad \qquad \quad \Huge\Updownarrow  \large ➀ &  \qquad \Huge\Updownarrow \large ➁ &   \quad   \ \Huge\Updownarrow \large ➂ \end{array}$
$$\int g(x)^{}\cdot h^{'}(x)\, dx=g(x)\cdot h^{}(x)-\int g(x)^{'}\cdot h^{}(x)\, dx$$
$\large ➀$ Identify $h^{'}(x)$
$\large ➁$ Check if $h^{}(x)=4e^{\frac{x}4}$
$\large ➂$ Extract $h(x)$ so that the remaining part is $g^{'}(x)$. Then integrate to obtain $g^{}(x)$.

Quick Integration by Parts Question


logistic regression for modelling

Question:
I have these data plotted above. The explanatory variable represents intensity levels of ground shaking at different locations in an earthquake, and the response variable represents amounts of compensation awarded to damaged buildings. I want to fit a model to these data so that how much compensation is likely to be awarded can be estimated given an intensity level. Can I use logistic regression here? How should I go about building the model for these data? Thanks in advance.
Comment: Logistic regression doesn't apply because your outcome variable isn't categorical. Do you have continuous data on `Intensity`, or only the grouped continuous data in bins of .04? Does the `Intensity` distribution really max out at .48?
Comment: I can see that, but earthquake intensity is continuous, so I'm hoping you have access to continuous data that hasn't been polychotomized/binned like this – it's a waste of information if the original information was more precise. Anyway, I've been trying `rpois(1000,6)*.04+.12` for `Intensity`, but it's not peaked or negatively skewed enough...and [I'm still thinking `Awarded` is gamma-distributed](http://stats.stackexchange.com/q/91687/32036), but you have some other good possibilities to consider there.
Comment: Nick - I like the use of the word polychotomized.
Comment: It might be appropriate to [create a sample data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (I know it's an SO link but it's sometimes hard to describe quantitative ideas qualitatively!)
Answer: Since your response variable (dependent variable) - compensation - is continuous, you wouldn't use logistic regression for this. Logistic regression is normally used to model the underlying probabilities in binomial proportions.
The fact that your predictor (independent variable) has been categorized has no impact on this choice, which is driven by the response variable.
A typical model for data like these might be a GLM. Note that both the mean and the variation change as the predictor changes; the fact that the spread seems to roughly increase with the mean would suggest trying a gamma GLM as a first attempt.
However, the response isn't linear in intensity; you might try a small-order polynomial (such as a quadratic in the log-mean), or you might try splines or some other form of smooth model.
If you have more than a few zeros in the response, you might use a zero-inflated model - essentially to model the probability of any compensation in terms of intensity, and then model the amount of compensation conditional on there being a non-zero amount. (If there are only a few zeros you might simply add a small constant to those few values to allow the gamma fit)
Answer: I think quantile regression is the way to go here. First, it makes no assumptions about the distribution of residuals. Second, it allows you to model any quantile of the dependent variable.

logistic regression for modelling


Change edit link URL in the content administration page

Question: I been trying to recreate something in the admin section of Drupal from a previous.
In the content, when you roll over the "edit" link. It shows the link like this: https://www.example.com/dolor-melior/edit?destination=admin/content
But I needs it to be: https://www.example.com/node/1/edit?destination=admin/content
I have Pathauto, Redirect and Global Redirect modules installed.
Comment: you have installed pathauto? or you set the url alias manually?
Comment: I have pathauto, redirect and global redirect installed.
Answer: Go to YOUR_SITE/admin/config/search/path/delete_bulk check All aliases and click in Delete aliases now!
Comment: This worked.  I tried to hit the arrow button, but I don't have enough reputation.
Comment: @blackbull77 a pleasure help you, and good luck in your projects ;-)
Comment: Spoke to soon. After I deleted it and try to update the URL alias in the page the title came back again.  :(
Comment: @blackbull77 You must uninstall the pathauto module
Comment: I think got it.  There must be a difference with Acquia Dev Desktop 2 and MAMP because they both render the link differently. Thanks


What is the name?

Question: How should I call this in c programming:
char *fileNames[3] = {"string1", "string2", "string3"};

Pointer array, array pointing to strings, or what?
Not really laziness to look for the answer somewhere else, it is that I didn't find it.
Update 1: I've called the above (whatever it is) fileNames because I'm actually using it to store file names, but it could have been anything in my question.
and Thank you for the huge debate, but there are so many answers that is even difficult to decide which is the right one for me.
Comment: Read [Difference between `char* str[]` and `char str[][]` and how both stores in memory?](http://stackoverflow.com/questions/17564608/what-does-the-array-name-mean-in-case-of-array-of-char-pointers/17661444#17661444) I explained with diagrams, also compare it with 2-D strings.
Comment: "Array of pointers to char", or simply "array of strings" or just "string array"
Comment: Try the ["Clockwise-Spiral Rule"](http://c-faq.com/decl/spiral.anderson.html).
Comment: Well, technically it's an array of pointers (of length 3), so that's what I'd call it?
Comment: `fileNames` would be called: `array of pointers to char`
Comment: I would name it ptrFileNames
Answer: You generally can read simple C / C++ declarations from right-to-left, but you're supposed to follow the Clockwise/Spiral Rule.
It's an array of pointers to characters.
Comment: I think the OP understands what it *is*, (s)he's just asking what to *call* it. In that context, "array of pointers to characters" is a poor name, since it describes its *type* with no information about its *meaning*. (In particular: it ignores the fact that the pointed-to characters are the initial characters of C-style strings.)
Comment: Nonsense. C types are read in a clockwise spiral. See http://c-faq.com/decl/spiral.anderson.html.
Comment: "Pointer array, array pointing to strings, or what?"  I don't think you're correct.
Comment: I'm not correct, I'm just asking and I see it has generated a debate so wasn't as straightforward as I first thought.
Comment: and I have used it in the past but neither understand it very well.
Comment: Pamplones, I was responding to @ruakh, when I meant, "I don't believe you're correct, Pamplones is trying to figure out what to call the type of this, not come up with a name for the variable."
Comment: @Pamplones, so am I right?  Are you trying to understand what to call the type of your fileNames variable, or are you trying to rename your fileNames variable to a more appropriate name?
Comment: @MattCruikshank: I didn't mean that (s)he was asking for a variable-name, only that (s)he *wasn't* asking for a rote and meaningless characterization of the type. It's true that in C, a "string" and a "pointer to a character" have the same type, but you shouldn't refer to the former as the latter.
Comment: I'm trying to understand the type of filenames and also why not use filenames[3] instead?
Answer: The name fileNames  is an array of 3 char* (char pointers) that do point to 3 string literals in your declaration. it is be called: 'array of pointers to char' or 'array of strings' because each index points to a string. You should make them constant:
const char *fileNames[3] = {"string1", "string2", "string3"};

Comment: @Wiz Thanks! before you I also notice same so I added some more text. and made `pointer` as *`pointer`*
Comment: This is wrong. It's not an array of 3 'char*' pointers. That would be char** []. It's an array of three pointers to a char.
Comment: @GrijeshChauhan He wrote "3 char* pointers". That's wrong. That's three pointers to a pointer to a char.
Comment: @Wiz yes but I parenthesis Is it correct now? suggest if you can
Comment: *The name fileNames is an array of 3 char* (char pointers) that do point to 3 string literals* OR more specifically they contain the address of string literals because string literals represent a pointer to themselves .
Answer: For this kind of question, there's cdecl. In English, your type is
declare fileNames as array 3 of pointer to char.

Answer: You can call it as an

array of character pointers
Comment: But why not `Pointer array` **?** or `pointing to strings` **?** its not an answer.-- Not-down-voting. Should improve answer else comment it.

What is the name?


How to understand complex social relations in huge populational data?

Question: I have 20 years of data from observing dolphins. When a group is seen, it gets an unique number identifying it, and all identified (marked) dolphins were also registered. So I had a table like
Group Dolphin
1     1
1     10
1     14
2     10
2     23

I got about 20,000 groups and 500 dolphins. I made a matrix with groups in rows and dolphins in columns, with 1 where a dolphin is in a group, 0 where it's not. So I've ran a multivariate analysis (prcomp in R), and it gave me two very distinct blocks.

I know that the vertical axis (PC2) is related with the size of the group, the small groups are below, in the bottom of the triangles. But the two blocks are separated in the horizontal axis (PC1), and I didn't figured yet what it's about. I suppose the dolphin population might be separated into two "clans" or something like that, but couldn't discover yet. I colored the points according to group size, water level, place of sight, but all patterns appear equally on both sides. The block on the right has about 2400 points.
My questions are: such a separation must mean something, right? What other tools can I use to understand what is going on?
Thanks very much!
Answer: I recommend looking at prcomp$rotation[,1], which tells you how much each dolphin contributes to the first principal component. (I believe these numbers are called scores or loadings.) This might help you to interpret PC1 in your plot.
Here is an example where there are ten dolphins and 100 groups, and the dolphins are divided into two clans, with groups tending to be made up mostly of dolphins from one clan.
set.seed(100)
M <- matrix(0, 100, 10)
for (i in 1:50) M[i,] <- c(sample(0:1, 5, replace=T, prob=runif(2)),
sample(0:1, 5, replace=T, prob=c(0.95,0.05)) )
for (i in 51:100) M[i,] <- rev(c(sample(0:1, 5, replace=T, prob=runif(2)),
c(sample(0:1, 5, replace=T, prob=c(0.95,0.05)) ))
)

image(M)

biplot(prcomp(M))

prcomp(M)$rotation[,1:2]

             PC1       PC2
 [1,] -0.2088296 0.1042746
 [2,] -0.3581626 0.1974796
 [3,] -0.3615619 0.2843145
 [4,] -0.3754459 0.3319092
 [5,] -0.2892279 0.5214507
 [6,]  0.3206343 0.4149111
 [7,]  0.2782524 0.2736374
 [8,]  0.3384193 0.3517811
 [9,]  0.2716513 0.2545807
[10,]  0.3228274 0.2272208

Here, PC2 is related to the size of the group since all the dolphins contribute to it. But PC1 distinguishes between dolphins 1-5 and dolphins 6-10. You can also see this on the biplot (although in your case, a biplot might not be very helpful because there will be so many arrows.)
However, it's also possible that the separation might be an artefact and doesn't actually mean anything; this can happen with PCA. See Shalizi's PCA tutorials lecture 10 for an example.
Another thing you could consider is social network analysis. People have worked on dolphin social networks before, so you might get some ideas from there. There is a data set called Dolphin in the UCI network data repository which might have links to interesting work.
[Edit: I also wonder whether PC1 might be related to time, since your data are spread over 20 years?]
Comment: Thank you very much, Flounderer! Digging the data a little more I found out that only 3 individuals were causing that separation. They were seen alone (or as the only recognized individual in the group) more than 1000 times each. Removing them from the analysis, it all became a single, pretty cloud of points. But your insights will surely be useful. Thanks for the links!


Theoretical Basics of Lambda Functions

Question: I was just wondering where I could find some language-agnostic tutorials for what lambda functions are, what their use is, and rough equivalents in languages that don't support them.
I would especially love any information on common notations.
Thanks! Please let me know if I can elaborate on this at all.
EDIT: Oh hey I forgot something.
Just in terms of terminology, what's the difference between a lambda expression or function, an anonymous/delegate function, and a closure?
Comment: "practical" and "CS" in the same sentence! LOL!
Comment: How deep do you want to get into the mathematics? How "CS" should the answer be?
Comment: Mathematics? *bemused face!* By all means feel free to mention those, though I admit I'm more interested in their practical CS applications.
Comment: Ah, my apologies, I misunderstood you. I thought when you said "CS" you meant, more practical than pure mathematics. I keep forgetting that many people see the CS as only the theoretical side. I am more interested in the "software development" side... Is that better? But any answers will be welcomed.
Answer: Asking for a language-agnostic treatment is a very severe restriction.  I can recommend a couple of tutorials on the lambda calculus:

A tutorial by R. Rojas focuses on getting quickly to the point of programming with lambda calculus.
A tutorial by Prakash Panangden goes into a little more technical depth and ends by showing how you can simulate recursion using a fixed-point combinator.

If you want to understand the use of lambda and higher-order functions in programming, I think you are better off abandoning your agnosticism.  A great paper for the ML/Hope/Miranda/Haskell family of languages is

Why Functional Programming Matters by John Hughes.  Hughes will show you some interesting use cases of higher-order functions and in general will do a good job helping you understand why people are interested in all this lambda stuff in the first place.
Comment: I apologize if language-agnostic is too restrictive... As I said in the comments to the question, really any answers will be welcome. I'm just trying to gain a general understanding, though, and I had figured (perhaps incorrectly) that pure notation would be the way to go. On the other hand, it might be possible that a specific language example might be a better place to start, and that I can try to inductively infer the general theory from there... I'm only asking the question here; I trust you all to help me by answering it how you like.
Answer:
Just in terms of terminology, what's the difference between a lambda expression or function, an anonymous/delegate function, and a closure?

Regarding terminology:

A lambda expression is the original way of writing an anonymous function invented by Alonzo Church for the lambda calculus.  In the lambda calculus, all functions are pure: there are never any side effects.
The lambda expression was adopted by McCarthy for LISP as (lambda (arguments ...) body), which is spelled "lambda".  Sussman and Steele later made the semantics more faithful to Church's original.  The expression was also adopted by Haskell as \ arguments ... -> body, and the backslash intended to look like a lambda, but the Haskell committee used a right arrow instead of a dot because they wanted the dot for function composition.  The expression was adopted by Robin Milner for ML but is spelled even more strangely: fn args => body.  All these expressions are properly called lambda expressions, and all represent anonymous functions.  Only in Haskell are the functions pure; Lisp, ML, and Scheme all permit body to have side effects.
A closure is what you get when you evaluate a lambda expression or other kind of nested function.  The closure contains not just the compiled code for the lambda expression but also information about the free variables.  (In Lisp and Scheme, the closure stores the locations of the free variables; in Haskell and ML, the closure stores the values of the free variables.)
An anonymous function is a more general concept and need not be restricted to a simple binding and body.  For example, in Smalltalk a "block" represents an anonymous function, but the body of a block is a sequence of statements, so it is definitely not the same as a lambda expression.

I'm afraid I can't help you with "delegate function;" in my world, that term is used to describe something else entirely.
Comment: So to make sure I've got it understood, lambda expressions are a specific notation for representing a subset of anonymous functions... specifically, those that are just expressions with no side-effects, or else single statements, or...? (As you can see, I'm trying to figure out what the subset actually is; care to give me a nudge?)
Comment: @Platinum Azure: I've edited my answer to help clarify.  In the lambda world the body of a function is always an expression, but depending on the language, the evaluation of an expression may or may not have side  effects.  And in all the languages I mention (including Smalltalk) there's no syntactic distinction between statements and expressions.
Answer: You probably don't want to be referred to Alonzo Church's lambda calculus.
By far the most common notation for lambda functions is LISP.
Abelson & Sussman "Structure and Interpretation of Computer Programs" is probably the most accessible text that covers the subject, along with applications.
If you want to see where it started, dig out McCarthy's "Recursive Functions of Symbolic Expressions and Their Computation By Machine, Part I".
Comment: SICP should be required reading for anyone doing software development. Lisp/Scheme may not end up being the language you use later on in life but what you learn from it is highly valuable.
Answer: Learn a dialect of Lisp (Scheme is currently quite popular). In fact I afree with John's recommendation to read "Structure and Interpretation of Computer Programs" and learn Lisp along the way.
Lisp started out not as a programming language but a mathematical notation to describe computing. It was designed as something better to teach students than Turing machines (and if you've ever worked on a Turing machine you'd know how much they suck). The fact that Lisp code can be compiled and executed was an accident. The fact that Lisp is very close to lambda calculus (which was a different branch of mathematics) was also an accident (it was proven only much later after the language was designed).
Most of Lisp's abilities were not really "designed" into the language, not the same way as other language copying Lisp. They emerge out of the few simple rules Lisp was based on - just like theorems in math emerge out of the rules numbers are based on.
Comment: I remember having to write a Turing machine simulator once. That was not fun, although I was proud of how I had managed to create a partial brain**** interpreter as an example for verifying that the simulator worked. I think it had over a hundred different states.
Answer: You won't get too far into Lambda calculus without running into the related SKI combinator calculus, so you might as well dip your toe into that and get it over with.  The delightful and strange To Disect a Mockingbird is, if not the best place to start, possibly the oddest.  Your brain will feel like it's being rewired by an alien.  Really.
Some crazy person implemented a language for the SKI calc.  Here's an Unlambda program to print the Fibonacci numbers:
```s``s``sii`ki
  `k.*``s``s`ks
 ``s`k`s`ks``s``s`ks``s`k`s`kr``s`k`sikk
  `k``s`ksk

Hey, don't blame me.  I didn't invent it.
Things got even stranger when it was shown that a Perl regular expression in a loop could implement the SKI calculus, and therefore, that Perl regular expression in a loop is Turing complete.
Comment: ::wince:: I've read some of those... my head hurts. I'll have to come back to this. Interesting stuff so far, though, thanks! :-)
Answer: Have you looked at the Wikipedia page on Lambda calculus yet?  It's heavier on math theory but eventually does get to more practical issues once you hit first class functions.
Comment: Thanks for the links. I'd skimmed that page in the past, but armed with some of the other information this question has generated, I was able to understand it better this time around.

Theoretical Basics of Lambda Functions


Force no cache for unregistered users

Question: Is there a way to force a fresh page to be served to unregistered users every time instead of the local cached version?
Answer: You can turn off caching by going to Configuration > Performance, and uncheck "Cache pages for anonymous users."  That will turn off page caching for the whole site.
If you only want to keep specific pages from being cached you can use Cache Exclude.
You can also (but it is not recommended; see the comments) enter this PHP snippet into the page's template, for example node--123.tpl.php.
<code>// Keep the page from being cached
$GLOBALS['conf']['cache'] = FALSE;
</code>
Comment: Checkout http://drupal.org/project/cacheexclude if you just want a module to do this for you.
Comment: I would give +1 for the first part of the answer, but -1 for the suggestion to exclude pages from cache in the theme layer. The theme layer is for presentation, not behavior. Once you start putting stuff like this in your theme, you cannot change themes anymore without breaking things.
Answer: The expression local cache suggests that op is referring to the browser cache. If this is the case then navigate to Administration » Configuration » Development » Performance and set Expiration of cached pages to none.
Before testing the new configuration reset your browser cache.

Force no cache for unregistered users


How would I change this query to fit into Laravel's Query Builder?

Question: I've tried creating this query in Laravel and still can't get it to work with a query builder. It's giving me syntax errors yet I can run it in the tables and it run smooth.
Query trying to run:
<code>select A.*, sum(RawAmt) as AmountOwed from (select Login, PatID,
if(length(ApptID) > 0, ApptID, VisitID) as VisitID,
ServiceDate,
TotalCharge,
InsurancePaid,
PrevPaid,
WriteOff,
Refund,
MiscDebit,
AmountOwed as RawAmt,
ApptTime,
ApptDate,
Physician,
isCopay,
HLocation from MDPay_AcctHist where Login='demo') A
group by PatID, VisitID
</code>
It's giving me a syntax issue when trying to do this with DB::raw statements in DB::select and DB::where;
Any help on trying to write this to meet laravels specs would be helpful.
Answer: <code>$subquery = DB::selectRaw('
        Login, PatID,
        if(length(ApptID) > 0, ApptID, VisitID) as VisitID,
        ServiceDate,
        TotalCharge,
        InsurancePaid,
        PrevPaid,
        WriteOff,
        Refund,
        MiscDebit,
        AmountOwed as RawAmt,
        ApptTime,
        ApptDate,
        Physician,
        isCopay,
        HLocation')
    ->from('MDPay_AcctHist')
    ->where('Login', '=', 'demo')
    ->toSql();

$result = DB::selectRaw('A.*, sum(RawAmt) as AmountOwed')
    ->from(DB::raw($subquery . ' as A'))
    ->groupBy('PatID', 'VisitID')
    ->get();
</code>
Comment: Ok, so I dropped this code in and got this: syntax error, unexpected '}'. I tried ending the where('Login', '=', 'demo') with a ; and I got this: call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'selectRaw'. how do I need to fix this to make it work correctly?
Comment: Yeah, you added the semicolon exactly where I did, look above in my comment and I'm now getting the:  call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'selectRaw'.
Comment: yuo, we should replace `selectRaw()` with `select(DB::raw())` here, see updated answer.
Comment: I actually did this and now I'm running into this issue: Base table or view not found: 1051 Unknown table 'A' (SQL: select A.*, sum(RawAmt) as AmountOwed)

The reason this is happening is because of this end statement not being in the query you created: ... where Login='demo') A
group by PatID, VisitID

How would I get the ) A group by PatID, VisitID in there? That's defining A.* at top.
Comment: good question, I don't know the answer, so I separated it to subquestion: http://stackoverflow.com/q/29970481/1331425
Comment: Thanks for all the help, I really do appreciate it. Sometimes corporate queries are a little harder to create in Laravel, and you helping has at least wrapped my mind around how this can work. I'll follow your question and see where it ends up.
Comment: You're welcome! I got the answer on our subquestion and amended this answer, take a look.


C++ When does class object file get compiled?

Question: I'm new to C++, and having difficulty understanding the steps of class object files getting created and compiled.
Let's say I create 3 files: 1. class header file 2. class cpp file (member function definitions present) 3. main cpp file
/*
When I run the main cpp file which includes the class header file (say as "#include class.h"), when does class cpp file get compiled, object file created, and linked?
The reason why I'm having difficulty is that from the compiler point of view, when it sees the main cpp file, there is only the header definition, no member function definition. However, even in the class header file, there is no class cpp file included. How would compiler know to run the class cpp file when it is not referred to in either class header file and the main cpp file?
*/
Let me clarify my question.
// Maybe I've said things I don't even understand lol.
So, basically I'm trying to run a main function in a say 'main.cpp' file. This 'main.cpp' file includes the 'class.h' header (include "class.h"). How would compiler execute the functions defined in header file when member function is not declared in 'class.h?' All my member function declaration is in 'class.cpp,' which is not included in 'class.h' or 'main.cpp.'
Comment: There is no such a thing like "class object file" AFAIK in c++, at least not specifically. Also, you don't "run cpp files". And to understand your issue, how do you compile a project with two "cpp" files? Because it is all the same, it doesn't matter if there are classes, you can have a `cpp` with a definition of only one function.
Comment: @luk32 Example: `Rectangle.h` is included in `Rectangle.cpp`, `Rectangle.cpp` is compiled and `Rectangle.obj` is created. I think that's the "class object file" OP is referring to.
Comment: @GrinNare Update (Edit) your question adding that comment, and remove that comment.
Answer: You need to compile every .cpp file. Headers; .h files, are just for declarations, means that to let the compiler determine if you use the functions correctly. Each .cpp contains code and should be translated to machine code (.o files). After all these compilations, you need to link them to build the executable so that every function used is contained in the same file. The following commands can help you (using g++ compiler):
$ g++ -c myclass.cpp            // produces the myclass.o file
$ g++ -c main.cpp               // produces the main.o file
$ g++ -o myapp main.o myclass.o // produces the myapp executable

This is oversimplified for the sake of understanding.
Comment: Thanks for your answer. Now I understand the process. However, it leads me to another question.
Is there different kinds of C++ compilers from different manufacturer or is it built in for every computers?
Based on  your comment 'g++ compiler,' I guess there are not just one compiler but many.
Comment: Yes there is many. The two common ones are the `gcc` suite and `Visual` from Microsoft, both in different versions. But there exists many others. `xcode` uses `llvm/gcc`.
Answer: I suspect you're using an IDE, since you mention running cpp files - compilers don't know how to run anything.
Your IDE manages these dependencies for you.
When you press "Run", the IDE will decide which files in your project need compiling, and when the compilation is done it will link all the object files together.
If compilation and linking succeeded, the IDE then launches the executable program.
If you want a better understanding of the concepts, step away from your IDE and do all your compilation and linking on the command line for a while.
(It's not complicated, only tedious.)
Comment: Thanks for your answer. As I said I'm new to C++ and a lot of things are new.
I guess IDE you are mentioning is the 'Xcode' that I'm using.
So this program will deliver the files to compiler, link the object file, and launch executable program for me, correct?


Can a sum score of binary items be used for correlational analysis in the same way as likert scales?

Question: I'm wondering if you can use parametric analysis with scores made from binary responses in the same way that you can for likert scales.
Just to illustrate with an example: Say I created a survey where the respondents where asked 10 simple yes/no (1/0) questions about their experience using different drugs, with the same question for 10 different drugs. For instance; "Have you used cocain during the last 3 months?"
If I made a sumscore called "Number of different drugs used in the last 3 months" based on the amount of "yes" answers given by each respondent. Could I then use that score in the same way as a likert scale and run parametric tests? And if not, why not? Is there a better way to analyse this type of data re: correlational analysis.
And I apologise if my English is not very good, please ask me if anything is confusing.
Thank you very much for any help or resources.
Comment: Using the sum implies that you consider use of each of the 10 drugs to be equally important. I don't have your list of drugs to form my own opinion on this, but I'd be surprised if you would want to argue that the drugs are equally important for your purposes. Also you don't say what hypotheses you would want to test using these sums. // If your subjects are randomly chosen from a population of interest, then perhaps it is worthwhile using a chi-squared test to see if drugs are about equally 'popular', or if some drugs stand out as most used.
Answer: Following the idea in the second part of my Comment, suppose you
have $n=100$ subjects and a $10$-vector contains usage counts
for each of the ten drugs, as in the hypothetical data sampled below
using R.  The simulation assumes drugs have probabilities p of use from
$0.1$ to $0.3.$
set.seed(2020)
p = c(10,10,10,10,10, 15,15,25,25,30)/100;  p
[1] 0.10 0.10 0.10 0.10 0.10 0.15 0.15 0.25 0.25 0.30
use=rbinom(1000, 1, p)   # 1000 subject responses
MAT=matrix(use, byrow=T, nrow=100)
dim(MAT)
[1] 100  10  # 100 by 10 matrix, each row for a subject
drug.f = colSums(MAT);  drug.f
[1]  9  7 11 12  9 15 18 26 17 26  # freq of use for drugs

chisq.test(drug.f)

        Chi-squared test for given probabilities

data:  drug.f
X-squared = 27.733, df = 9, p-value = 0.001057

The null hypothesis is that the ten drugs are used with equal
frequency. [This is assumed by chisq.test unless another
null hypothesis is stated.]
The null hypothesis is rejected. Ad hoc tests can show which
drugs are used significantly more often than others. These ad hoc
tests should use P-values below 5% according to the Bonferroni
method of avoiding false discovery.
For example, below we compare reported frequencies of use for Drug 1 and Drug 10. (The null hypothesis that the two drugs are equally likely choices for these subjects is strongly rejected.)
prop.test(c(9,26), c(100,100))

        2-sample test for equality of proportions
        with continuity correction

data:  c(9, 26) out of c(100, 100)
X-squared = 8.8658, df = 1, p-value = 0.002906
alternative hypothesis: two.sided
95 percent confidence interval:
  -0.28265048 -0.05734952
sample estimates:
prop 1 prop 2
  0.09   0.26

Note: If you want to test something using sums of number of drugs used for each subject, here is a summary of the 100 subject sums
for my simulated data.
subj.sum = rowSums(MAT)
table(subj.sum)
subj.sum
 0  1  2  3  4
16 39 29 11  5

For what it's worth, you could test the null hypothesis that sum is centered at 2 drugs per subject or above, using a one-sample Wilcoxon (signed rank) test. (Rejected with a P-value near $0.)$
wilcox.test(subj.sum, mu = 2, alte="less")

        Wilcoxon signed rank test
        with continuity correction

data:  subj.sum
V = 585.5, p-value = 1.587e-05
alternative hypothesis: true location is less than 2


Why the solution does not converge in this optimization problem?

Question: I want to use the "projected Gradient decent algorithm" to solve this optimization problem but I do not know why it does not converge. I appreciate if anybody can help me to find the mistake.
Given $$n, C, r_i, p_i, \quad∀ i={1,2,...,n} $$ I want to solve this optimization problem:
$$maximize \quad f(x_1,x_2,...,x_n)=\prod_{i=1}^n {{((x_i-c_i)/r_i)}^{p_i}} $$
$$s.t \quad\quad\quad\quad {x_i≤r_i},\quad {c_i<x_i}, \quad {\sum_{i=1}^n x_i}=C$$
where $$0<p_i≤1 \quad\quad,   \sum_{i=0}^n p_i =1\quad \quad\quad, ∀x_i,r_i∈R,\quad x_i,r_i>0,\quad C>0 $$
After logarithmic transformation and Lagrange relaxation the problem is transformed to :
$$minimize \quad -f(x_1,x_2,...,x_n)=-\sum_{i=1}^n {{p_i}log{((x_i-c_i)/r_i)+\beta {((\sum_{i=1}^n x_i)}-C})} $$
$$s.t \quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad {x_i≤r_i},\quad {c_i<x_i},\quad \beta>0$$
Now I use projected gradient algorithm as what follows:
\begin{equation} \frac{\partial f}{\partial x_i} = -\frac{p_i}{x_i-c_i} +\beta
,\quad\quad\quad\quad \frac{\partial f}{\partial \beta} = {(\sum_{i=1}^n x_i)-C},\end{equation} Projected gradient algorithm: $$\alpha=0.0001; \quad \epsilon=0.000001;\quad \beta_{(k)}=0.1; $$ $$x_{i(k)}= c_i+(p_i/\beta_{(k)});\quad **//i=1,2,...,n$$
$$repeat $$
$$\beta_{(k+1)}=max(\beta_{(k)}-\alpha{(\sum_{i=1}^n x_{i(k)})-C),\epsilon)}
\\x_{i(k+1)}=x_{i(k)}-\alpha(-\frac{p_i}{x_i-c_i} +\beta);\quad **//i=1,2...,n$$
$$x_{i(k+1)}=max\{{c_i+ \epsilon, \quad min\{{x_{i(k+1)},r_i}\}\}} \quad **//Projection$$
$$if \quad \quad \mid{x_{i(k+1)}-x_{i(k)}} \mid<\epsilon \quad and \quad  \mid x_{i(k+1)}-x_{i(k)} \mid<\epsilon \quad then \quad stop \quad **//i=1,2,...,n $$
$$else\\ \quad \beta_{(k)}=\beta_{(k+1)}; \quad x_{i(k)}=x_{i(k+1)}; . \quad **//i=1,2,...,n $$
Comment: Are you sure the problem is convex?
Comment: yes I am. It is convex.
Answer: I think it is strange that  is also a variable.
Obviously the minimum will happen at =0, and you lose constraint on the sum of .
Although  will stop at =, that is still not right, it means very weak constraint on the sum of .


Issues in estimation and plot

Question: I am learning adaptive filters and testing the performance of using Least Squares and Kalman filter for parameter estimation for $y = X + \text{noise}$. The model is autoregressive AR(2) model
 $$y(t) = ay_{t-1} + by_{t-2}$$
and a Moving Average MA(2) model. I have obtained the parameters $(\hat{a},\hat{b})$ for AR and MA.  I want to show the plot of the desired signal vs the actual signal so that it shows how close the estimated signal is from the actual. How do I recover the signal and show the plot?
Answer: Assuming you are not looking to forecast anything, given a signal $Y$ and the following code will give you a plot of the inferred versus the actual series' residuals from an AR(2) model:
<code>mdl = arima(2,0,0); % Simple ARIMA(2,0,0)
rng(123)            % Set the random seed

Y = sin(linspace(0,20,52))' + randn(52,1);    % Some junk data'

EstMdl = estimate(mdl,Y);                     % Estimate your model
[E,V] = infer(EstMdl,Y(3:end),'Y0',Y(1:2));   % Infer model residuals using
                                              % the first two values as presample
plot(Y(3:end)); hold on;
plot(E,'r'); hold off
legend('Original Stuff', 'My stuff')
</code>
Comment: Thank you for your reply. Will this method apply when I apply other estimation techniques like Maximum likelihood or Kalman filter for estimating the state and parameters?The thing is once I estimated the parameters & state, I wanted to get the same plot as you have done.But I am unsure if estimate() and infer() command will be applicable. My concern is how do I pass the estimated coefficients into infer() since I am not using estimate() function.
Comment: Shouldn't deconvolution give the desired signal?
Comment: You can omit the call `estimate` if you specify your model's coefficients directly. You will probably need syntax like: `mdl = arima('AR',{ param1, param2}, ...`. Then you can pass the object `mdl` to `infer` once more.
Comment: @ quick questions (1) In many cases I have seen models with the functional form y(t)=1 + 0.2y(t-1) + 0.6y(t-3), is the 1 an intercept? How do I specify the "1" or "0.5" intercept in mdl = arima() (2) When calculating Means Square error between the actual (h) coefficients and the desired(H), do we consider the estimated  intercept in the formula for MSE?Ex in the Eq in (1), will mse = sum((H-h).^2)/4 OR mse = sum((H-h).^2)/3
Comment: (1) I believe you want to use `Constant`. (2) I haven't seen MSE being used to compare estimated and true coefficients, only estimated and true sample values; maybe I am wrong... In any case if you have 3 coefficient that you estimate why would you divide by 4? :D Finally check the functionality of `lagmatrix` in conjunction with `fitlm`, it will allow you to get OLS estimates for what you describe.

Issues in estimation and plot


HTML tags showing in page title

Question: I am setting page titles dynamically, based on the on a property in the page's model. Sometimes that property contains html tags, such as Article Title. When the property contains HTML tags, they are showing in the page title. I tried surrounding the property with @Html.Raw, but it didn't help. How can I make sure the tags don't show?
Code:

    <title>@Html.Raw(Model.Title)</title>

Working code based on @Santiago's answer:

@{
    string titleRaw = Model.Title;
    string htmlTag = "<[^>]*>";
    Regex rgx = new Regex(htmlTag);
    string titleToShow = rgx.Replace(titleRaw, blank);
}
    <title>@titleToShow</title>

Comment: Can you show your code?
Comment: @Html.Raw isn't going to eliminate the markup from your property.  You will need to scrub that out yourself.  In the past I have used separate properties for titles that shouldn't have markup in them.
Comment: If you combine regex and `Html.Raw`, you'll get the best of both. Clean and safe text.
Answer: Could you try to do it with regular expressions?
<[^>]*>

Took that from:
Regular expression to remove HTML tags from a string
String target = someString.replaceAll("<[^>]*>", "");

Assuming your non-html does not contain any < or > and that your input
  string is correctly structured.

A more complete explanation is available in above SO link

HTML tags showing in page title


Por que deveriamos utilizar funções que começam por mb_?

Question: Algumas vezes, surgem problemas no PHP em relação a algumas funções de string, por causa da condificação das mesmas.
Um exemplo, é o strlen.

$a = strlen('str');

$b = strlen('stré');

var_dump($a, $b); // Imprime 3 e 5

Veja no IDEONE
Como vemos, no caso de $b, foi impresso que o mesmo possui 5 caracteres, e não 4.
Sei por experiência que para resolver isso deveriamos utilizar mb_strlen, que são funções multbyte do PHP.
Exemplo:
var_dump(mb_strlen('stré', 'utf-8')); // Imprime 4

O que quer dizer exatamente esse multibyte?
Como é muito comum o uso de UTF-8 aqui no Brasil, sempre deveriamos utilizarmos as funções do tipo mb_ ao invés das funções comuns para trabalhar com strings?
Por que isso não é tratado (em relação as funções comuns para trabalhar com string) simplesmente alterando o default_charset no php.ini?
Comment: pq prefixo são legais :D haha. Pergunta importante +1.
Comment: Importante e ninguém respondeu ainda :\
Comment: Um site pode ter varias linguagens logo o tratamento e diferente, uft8 pode servir para 1 linguagem mas nao para outra, por exemplo o Iso e o que aguenta com mais letras codificadas, por isso e que o tal default_charset no php.ini nao funciona.
Answer: As funções PHP cuja nomenclatura inicia com "mb_" pertencem as funções MBString
MB significa "Multibyte", ou seja, são funções para manipular strings multibyte.
Encodes como UTF8 são do tipo multibyte (múltiplos bytes). Na documentação oficial, consulte a lista de encodes suportados: http://php.net/manual/en/mbstring.supported-encodings.php
Exemplo prático
<?php
date_default_timezone_set('Asia/Tokyo');

ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
ini_set('log_errors',TRUE);
ini_set('html_errors',FALSE);
ini_set('display_errors',TRUE);

define( 'CHARSET',   'UTF-8' );

ini_set( 'default_charset', CHARSET );

if( PHP_VERSION < 5.6 ){
    ini_set( 'mbstring.http_output', CHARSET );
    ini_set( 'mbstring.internal_encoding', CHARSET );
}

header( 'Content-Type: text/html; charset=' . CHARSET );

/*
Retorna 6
Cada caracter "coração" está ocupando 3 bytes.
Caso queira contar a quantidade de bytes, strlen() é o mais indicado.
*/
echo strlen('I♥NY') . PHP_EOL . '
';

/*
Retorna 4
Caso queira contar a quantidade de caracteres, utilize a função equivalente em MBString
*/
echo mb_strlen('I♥NY');

/*
Note que mesmo os caracteres latinos são multibyte
*/
echo strlen('ação') . PHP_EOL . '
';
echo mb_strlen('ação');
?>

Outro termo pouco utilizado para referir-se a caracteres multibyte é "variable-width encoding" (codificação de largura variável).
https://en.wikipedia.org/wiki/Variable-width_encoding
Nota adicional
Nem sempre se faz necessário usar funções mbstring. Um exemplo de caso, é quando sabe-se que uma determinada string não possui caracteres multibyte.
Exemplo:
echo strlen('123') . PHP_EOL . '
';
echo mb_strlen('123');

Como mostra o exemplo, nesse caso é desnecessário, no entanto, podemos nos aprofundar mais com outro exemplo numérico.
echo strlen('123') . PHP_EOL . '
';
echo mb_strlen('123');

Nesse exemplo, são números, porém, multibyte.
Há muitos sistemas bem desenvolvidos que "pensam" ser internacionalizados, porém a vasta maioria não faz nenhum teste com o mundo real, como se o termo global se resumisse ao continente americano e europeu.
Mais de 60% do planeta (árabes, gregos, russos, indianos, asiáticos) utiliza caracteres multibyte e cada idioma possui peculiaridades como esse exemplo dos números multibyte da tabela do idioma japonês.
Por isso, recomenda-se o uso das funções MBString caso queira construir um sistema que ofereça maior compatibilidade possível com os diversos encodes existentes.
Outra nota importante: UTF8 não é um encode compatível com todos os idiomas. E as funções MBString não limitam-se a UTF8.
Exemplo, os caracteres chineses são melhor suportados pelo encode Big5.
Há também o uso de UTF16 ou UTF32.
Todavia, mesmo para caracteres chineses, o UTF8 também é utilizado com certa segurança, pois é "raro" que os próprios chineses utilizem todos os ideogramas. São mais de 60 mil.
Comment: me dê uma moral aceitando, aí ganho facinho os 50 pontos. whahaha
Comment: está descrito na resposta, independente da "nossa região" . O que vc não conseguiu entender na resposta?
Comment: Para te dar uma moral no conteúdo, coloque aí: _Como é muito comum o uso de UTF-8 aqui no Brasil, sempre deveriamos utilizarmos as funções do tipo mb_ ao invés das funções comuns para trabalhar com strings?_
Comment: preferi não comentar sobre uma região específica pois esse assunto é global
Comment: Mas a nossa região usa `UTF-8`. E eu queria saber se e importante usar `mb_` em tudo que for criar (validação de nomes e etc)


Repeated measures - sum of biomassproduction

Question: I am student in biology. For my examination I conducted a greenhouse experiment with gras.
Actually I am not sure how to develop the correct model and unfortunately I haven't found an answer in the web during the last weeks.
Experiment:
4 different species of gras were planted in pots with 4 different types of fertilizers (different N content).
After the first harvest fertilizers were changed until second harvest.
The experiment was completely randomized and consisted of six repetitions.
Question:
I am looking for an appropriate model and a post-hoc test. I guess modeling each harvest by its own using typical anova is no problem. For analyzing sum of biomass I favor repeated measures models.
<code>Model:
Columns:
Pot_ID: ID number of the pot

Species: Name of the species
Fertilizer: A, B, C, D

Time: 1 for first harvest, 2 for total biomass including second harvest

Yield: Biomassproduction

model=lme(yield~species*fertilizer,random=~1|pot_ID,dataset,correlation=corAR1(form=~time|pot_ID))
</code>
Is the model appropriate? What does anova(model) show, both times or only time=2? How to implement a post-hoc?
Answer: I think modeling the first harvest using ANOVA is not a problem; it gets quite complicated for the second harvest because prior effects will be very different depending on the residual effects of the previous N content (left in the soil and accumulated by the plant), which is unlikely to be a random effect or be completely accounted for by the first harvest biomass. A complete design accounting for that would have 24 (4! or factorial of all N trt) treatment combinations (each prior fertilizer matched to each type of fertilizer in the second round). You could try to rephrase the question, looking at the rate of biomass increase depending on the sign of change in the fertilizer (increase or decrease).
This has a nice detailed discussion of post-hoc comparisons How to obtain the results of a Tukey HSD post-hoc test in a table showing grouped pairs?
Comment: Thank you very much.
I agree with you. Reprasing might be a good idea. I can seperate this experiment into two experiments. First one until the first harvest. Second one starting with plants that were grown under different nutrient conditions (until first cut). So I avoid questions of repeated measures.


Proposed transition matrix for MCMC in two-state Markov Chain

Question: Suppose we would like to model the weather (either sunny $S$ or cloudy $C$) using a two-state Markov Chain, given a set of data collected from 10000 days:
$$CCCSSSSSSCCCSSSSSSCCCC...$$
We can use the metropolis-hastings algorithm to get a target transition matrix $P$ whose invariant distribution is given by
$$\pi = \left(\frac{\text{# of $S$'s from the data}}{10000}, \frac{\text{# of $C$'s from the data}}{10000}\right)$$
from the data.
Now how do we choose the proposed transition matrix $Q$? Naturally, we don't want to choose $Q$ to be $$Q = \begin{pmatrix}
1/2 & 1/2 \\
1/2 & 1/2 \\
\end{pmatrix}$$
because then a typical sample from this $P$ would look like
$$CCSCSSCSCSCCSCSCCCSCCS...$$
(which is unrealistic for modeling the weather) instead of our data
$$CCCSSSSSSCCCSSSSSSCCCC...$$
Is there a systematic way of picking the $Q$ given the data set like above? I know the intuition would be say let
$$Q = \begin{pmatrix}
0.9 & 0.1 \\
0.1 & 0.9 \\
\end{pmatrix}$$
but the $0.9$ and $0.1$ are still guesses.
Comment: You don't pick the matrix: you estimate it from the observed frequencies.  The states you need correspond to *sequences* of `C` and `S`, not additional types of weather!  These sequences are called "n-grams" ($n$ is the length).
Comment: You could use a state space of $(n_s, n_c)$, where the indices refer to the number of consecutive observations in each state; obviously one of the two will always equal zero.   Then your transition matrix is of size at least equal to the sum of the two maximum sequence lengths.
Comment: You can't accomplish this with just two states.  Use more.  See https://stats.stackexchange.com/questions/115883 and https://stats.stackexchange.com/questions/32531 for details.
Comment: @whuber In case we have say 3 states ( S, C, and Rain), given a set of data, would we be able to pick the proposal matrix $Q$ in some "optimal" way? Edit: just saw the link, I will take a look, thanks!
Comment: @jbowman That's a nice idea.  In effect, it amounts to representing the sequence as a run-length encoding and modeling *those* transitions.  One might want to go a step further and study transitions of ordered pairs of such codes, thereby introducing some extra "memory" into the system. This would be needed if, say, long periods of drought were occasionally punctuated by rainy days. It would also provide a mechanism to limit the number of states: *e.g.,* a sequence 20 C's could be considered a sequence of 10C's followed by 10C's.
Comment: @whuber I guess we would have to use state space like $\{SSSS, SSSC, SSCC, ...\}$, right?
Answer: There seems to be a major confusion behind the question: MCMC techniques are simulation tools that aim at reproducing generations from a known distribution, e.g., a posterior distribution in a Bayesian setting. They are not inference methods.
In the current setting, if the transition matrix $P$ is the quantity of interest and is inferred from the data at hand, an MCMC method such as Metropolis-Hastings would operate on the space of such transitions matrices, provided a prior distribution on the $P$'s and a sampling distribution on the data, indexed by $P$, are given .


É possível voltar a likebox antiga do Facebook?

Question: É possível voltar a likebox antiga do Facebook?
Eu preciso voltar pois preciso aumentar o número de 'friends faces'. Caso não seja possível, pode me dizer se é possível aumentar esse número na nova lightbox?
Answer: Receio que isto não seja possível, pois a antiga Like Box foi completamente desabilitada com o lançamento do Graph API v2.3 e substituída pelo Page Plugin.
O Page Plugin basicamente é uma nova versão aprimorada do Like Box e com um nome diferente.

Podes acessar este link para ver o que mudou com esta nova versão do
  Page Plugin

Antigamente para aumentar o número de caras de pessoas a ser apresentado, bastava apenas aumentar a altura do Like Box que automaticamente o número da quantidade de caras seria aumentado consoante a sua altura. Infelizmente (pelo menos por agora) esta solução não se aplica a esta nova versão.
Contudo, podes sempre fazer essa sugestão no grupo Facebook Developer Community.
Comment: Muito bem descrito +1
Answer: Pois é, isso mudou. Para exibir o rosto caso 2 ou mais amigos tenham curtido a página você pode setar o atributo "data-show-faces" para true, na tag html do Facebook.
Exe.:

Já aquele box que a gente estava acostumado pelo que vi agora só existe caso o link seja de uma fanpage, ou seja, para curtir a fanpage. Para isso usa-se o Page Plugin.
Exe.:

Esse também possui outras opções de configuração. Dá uma olhada lá em https://developers.facebook.com/docs/plugins/page-plugin .


PostgreSQL mysticism with timestamp field

Question: I have PostgreSQL database table with timestamp with time zone
field called published_date.
I use psql -U postgres dbname command for access my database.
When I try to update published_date field value by command:
UPDATE mytable SET published_date='PUBLISHED_DATE' WHERE id=3;

I get following results for corresponding PUBLISHED_DATEs:
SOURCE              -> RESULT

2013-01-16 20:00:00 -> 2013-01-16 20:00:00+00 (WELL)
2013-02-16 20:00:00 -> 2013-02-16 20:00:00+00 (WELL)
2013-03-16 20:00:00 -> 2013-03-16 20:00:00+00 (WELL)
2013-04-16 20:00:00 -> 2013-04-16 20:00:00+01 (STRANGE!)
2013-05-16 20:00:00 -> 2013-05-16 20:00:00+01 (STRANGE!)
2013-06-16 20:00:00 -> 2013-06-16 20:00:00+01 (STRANGE!)
2013-07-16 20:00:00 -> 2013-07-16 20:00:00+01 (STRANGE!)
2013-08-16 20:00:00 -> 2013-08-16 20:00:00+01 (STRANGE!)
2013-09-16 20:00:00 -> 2013-09-16 20:00:00+01 (STRANGE!)
2013-10-16 20:00:00 -> 2013-10-16 20:00:00+01 (STRANGE!)
2013-11-16 20:00:00 -> 2013-11-16 20:00:00+00 (WELL)
2013-12-16 20:00:00 -> 2013-12-16 20:00:00+00 (WELL)

There is UTC timezone in database:
=> SHOW timezone;
 TimeZone
----------
 UTC
(1 row)

and system:
$ date
Tue Dec 17 00:14:12 GMT 2013

PostgreSQL version is 9.2.4
Same version of postgres compiled with same flags installed on another machine working pretty! Very strange.
Answer: That's the normal behaviour, PostgreSQL tries to accomodate the user when outputting a timestamp with timezone (which internally is indeed stored as UTC).  I assume you're in the UK, what you see is it changing between summer and winter time.  From the manual, emphasis added:

For timestamp with time zone, the internally stored value is always in
  UTC (Universal Coordinated Time, traditionally known as Greenwich Mean
  Time, GMT). An input value that has an explicit time zone specified is
  converted to UTC using the appropriate offset for that time zone. If
  no time zone is stated in the input string, then it is assumed to be
  in the time zone indicated by the system's timezone parameter, and is
  converted to UTC using the offset for the timezone zone.
When a timestamp with time zone value is output, it is always
  converted from UTC to the current timezone zone, and displayed as
  local time in that zone. To see the time in another time zone, either
  change timezone or use the AT TIME ZONE construct (see Section 9.9.3).


Discrete Math: Multiplication Principle Word Problem

Question: Assuming that there will never be more than 500 million people in the United States before the year 2050 AD, how many alphanumeric-upper digits are necessary to provide every person with a personal zip code?  (Notice that businesses and institutions are being neglected in this count.)
If the answer is 9 digits, how can I write this in the form ${n \choose x}$?
Comment: This is a [Multiplication Principle](https://en.wikipedia.org/wiki/Rule_of_product) problem, not a combinations problem.
Comment: If I have understood the question correctly, then I have to ask: do you understand **logarithms** enough to solve for the smallest value of the positive integer $n$ such that $(36)^n \geq 500000000$?  This question assumes 36 available symbols, and that the left-most character can be zero.
Comment: @user2661923 Well I see how you got this inequality, and I used a logarithm calculator online and got 5.58951, but I'm a bit rusty on logs. How would the log equation look like to solve for n?
Comment: See my answer.  If questions, please leave comment directly following my answer and I will automatically be flagged.
Answer: Let $r ~=~ 500000000$.
You want the smallest positive integer $n$ such that
$$36^n \geq r.$$
Taking logarithms of both sides:
$$n \times \log ~36 \geq \log ~r \implies n \geq \frac{\log ~r}{\log ~36}.$$
By the way, this can actually be approximately solved without a calculator, by using logarithms, base $10$, rather than base $e$, if you happen to know that
$$\log_{10} ~2 ~\text{approximately} ~=~ 0.301
~~\text{and} ~~ \log_{10} ~3 ~\text{approximately} ~=~ 0.477.$$
Then, you have that
$$\log_{10} ~36 ~\text{approximately} ~=~
2 \times \left(0.301 + 0.477\right)
$$
and
$$\log_{10} r ~\text{approximately} ~=~
8.699.
$$
Therefore, $n$ is the smallest positive integer such that
$$n \geq \frac{8.699}{2 \times \left(0.301 + 0.477\right)}.$$


Running a cron job on hPanel on Hostinger

Question: So basically I recently went from cpanel hosting to hostinger. Now I had a cron job that ran easily on cpanel. * * * * * wget --spider -O - https://social.yoursite.in/api_provider/cron/order >/dev/null 2>&1, setting this cron was easy just paste and it ran. But now on hpanel I am unable to use the same command to setup cron jobs. It throws an error "Some characters are not allowed for cron job command". On contacting hostinger support, they say I can add these command lines to a Bash script or php script and add the script to cron.
I do not know anything on running scripts but searching a lot on internet and a little help from support team I made a file cron.sh and included these inside the file #!/bin/sh/usr/bin/php/home/u375788432/youbloom.in/public_html/social/ wget --spider -O - https://social.yoursite.in/api_provider/cron/order cron:run >/dev/null 2>&1  still on checking this doesn't actually execute and the functionality I wish to achieve doesn't work.
also if we can put this into a php script that would work too.
Comment: use `curl`

    * * * * * curl --request GET 'your url'
Comment: if not solved then ask this question in hostinger support or forum
Comment: I need to put this where exactly ? inside a php script ? or bash ?
Comment: i think PHP  and learn more here :- https://www.hostinger.in/tutorials/how-to-run-and-configure-cron-jobs-on-hpanel/   about using cronjob in hPanel
Comment: @RDX I have gone through that it just says you can use php this way. But what I wasnt is how to put these command lines into the php file. The syntax
Answer: in order to use special characters, you need to put your script in a bash files.
Cron jobs with special symbols in the syntax will require you to make a bash (.sh) file which would support any form of cron job needed.
A cron job that would execute a bash file looks like this:
*/5 * * * * /bin/sh /path/to/shell/script.sh

And the bash.sh files contents should look like this:
#!/bin/sh
/usr/bin/php /home/u123456789/public_html/script/scheduled.php cron:run > /dev/null 2>&1

The first part #!/bin/sh indicates this is a Bash file that will be opened by the cron.
The second part loads the php libraries: /usr/bin/php
The third part is the cron job that otherwise did not work because of the special characters. cron:run executes the cron job inside the file every time the file is opened
So after you have created your bash file, you can set up your cron in hPanel with the path of your bash file in file manager.
Comment: Hi, I've formatted your code, please double check that I did not remove too many `*`s. Nit: here it does not matter, but don't assume `sh` is `bash`. On Debian it is `dash` by default, and even when it's `bash` it may activate some compatibility mode when called as `sh`.


Django created_at field not added when creating an object

Question: I'm trying to get Django automatically add a field to an object where the date and time is saved in the field as a timestamp.
When I create the object in my view and I go check in admin page, every other field is created but not this one.
views.py :
newconf = ConfigUser.objects.create(
        ref=''.join(random.SystemRandom().choice(
            string.ascii_lowercase + string.ascii_uppercase + string.digits
        ) for _ in range(20)),
        name='',
        user=request.user,
        # Here I don't add created_at because I want it to be automatic (see models.py)
        cpu=cpu[0],
        gpu=gpu[0],
        ram=ram[0],
        ssd_m2=ssd_m2[0],
    )

models.py :
class ConfigUser(models.Model):

    ref = models.CharField(max_length=20, unique=True, default='')
    name = models.CharField(max_length=128, default='')
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    cpu = models.ForeignKey(CPU, on_delete=models.CASCADE)
    gpu = models.ForeignKey(GPU, on_delete=models.CASCADE)
    ram = models.ForeignKey(RAM, on_delete=models.CASCADE)
    ssd_m2 = models.ForeignKey(SSD_M2, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

Answer: If a field inside model is created with auto_now attribute set, it will also include the property editable=False and therefore it will not show up in the admin panel.
To overcome this, you need to override your save method:
class ConfigUser(models.Model):
    # some code
    created_at = models.DateTimeField(editable=False)

    def save(self, *args, **kwargs):
        if not self.id:
            self.created_at = timezone.now()
        return super(User, self).save(*args, **kwargs)

Comment: Much simpler is just to add that field into read_only fields as this is already answered should be closed as duplicate
Answer: First of all, make sure to check if all model migrations have been done.
Secondly, as an alternative, you could try to create the "newconf" object from the class directly, and not via the objects.create() method, like so:
# Create the record as object of the model class
newconf = ConfigUser(ref=''.join(random.SystemRandom().choice(
            string.ascii_lowercase + string.ascii_uppercase + string.digits
            ) for _ in range(20)),
            name='',
            user=request.user,
            cpu=cpu[0],
            gpu=gpu[0],
            ram=ram[0],
            ssd_m2=ssd_m2[0])

newconf.save()

To have this method working you should only add the def__init__ method in the model ConfigUser, passing all the initialization parameters in.


How to use staleWhileRevalidate for workboxSW precache items?

Question: workboxSW.precache is cacheFirst by default, is there a way to change its strategy?
workboxSW.precache(myPrecacheItems);

Or is there a way to define the cache name on precache items, so precache items can be overwrite by specifying route?
Answer: precache is basically meant for your static assets like css/js etc. To these assets even if you don't append a hash, workbox will append a hash while precaching.
So if you change these, a new sw will come into play and new bundles will be served.
Whereas staleWhileRevalidate is a runtime strategy(basically used for dynamic data e.g. APIs). you can add runtime  handler in workbox for these and expect them to work.
If you still think that you need precache with a runtime strategy, please elaborate your use case.
Answer: While I don't know if it's possible to change the workbox.precache strategy, I believe what you want to do is fill your routing cache with some pages on service worker install.
To do this first create your workbox route with staleWhileRevalidate strategy with a defined cache name:
workbox.routing.registerRoute(
    /.*\.html/,
    workbox.strategies.staleWhileRevalidate({
        cacheName: 'MY-CACHE',
    })
);

To precache some urls for your defined route on SW install.
Add this code to your Service Worker:
self.addEventListener('install', (event) => {
    const urls = ['https://url1.html','https://url2.html','etc'];
    event.waitUntil(caches.open('MY-CACHE').then((cache) => cache.addAll(urls)));
});

Where MY-CACHE is the name of the cache set in your staleWhileRevalidate strategy.


Access, Permissions, and Security for 3rd party developers

Question: Our Marketing department wants to start hiring an outside company to assist in the development and maintenance of our marketing website. The website right now is on FreeBSD (using apache), but with plans within the next couple of months to migrate everything to CentOS. How it's set up right now is we have an internal Subversion server housing all of the code, developers check code in and out, code is then pushed to an external staging server to make sure everything looks good, and then pushed to our live server.
Like I mentioned, they're going to be hiring an outsourced company to work with them on the code. My task is to be able to work with the code, but at the same time keep them out of network.
Here are some ideas I've had:

Give them VPN access through our SonicWall (our main gateway) and only allow them access on port 443 to the Subversion server and block off everything else (I'm not even sure if the Sonicwall will do this or if it will only allow me to grant access on a subnet level.)
Set up a subversion server in a DMZ and only allow inbound traffic from their corporate IP address. This would effectively block access into our network, but still allow people inside and outside to access subversion.
Give them an account on the staging server and just let them work from there (laziest, least liked solution...)

Option 2 is the one I'm leaning towards the most. Does anyone have any input on these solutions or have a completely different solution that would work better? Our end goal is to allow them to work on the code without compromising our network.
Comment: Have someone use git-svn, do a checkout of the code you want to collaborate on. Push a copy of the git repository to some external location that both of you can access?  Someone inside would need to be responsible for pulling in changes and merging it back to svn repository?
Answer: Assuming that moving to a decent decentralised revision control system like Git isn't an option (and where "outsourced developers" are involved, I wouldn't be confident that they won't make a balls-up of SVN, let alone Git)...
If the SonicWall can do it, punching a hole in the firewall to give them access to SVN internally is an option, but I don't like it.  Whilst SVN doesn't have a glorious history of security holes, it only takes one for you to be popped by some untrustworthy idiot at the outsourced company (or anyone who has access to the outsourced company's network... "how good is the security of the systems at the other end of the VPN" is a question not nearly enough people ask).
I'd be putting the SVN server in a mutually untrusted location and locking it down tight as a drum.  I certainly wouldn't be giving them access to staging, that's just a recipe for unmaintainable nightmare scenarios (the chances of you being able to do a clean deploy are epsilon) and potential security problems that'll haunt your dreams forever.
Comment: +1 For the insight on VPN security. The more I think about it, the more I agree with you. The best way to keep our server secure is to not even allow them into it. Looks like I'll be setting up an external SVN server. Thanks for the suggestion.


Integrating $\int_{0}^{\infty}\prod_{k=1}^{n}(kx-1)e^{-ax} {d}x,$ where $a$ and $n$ are parameters.

Question: I want to integrate:

$\int_{0}^{\infty}\prod_{k=1}^{n}(kx-1)e^{-ax} {d}x, \quad $where $a$ and $n$ are parameters. Can I have a simple expansion of $\prod_{k=1}^{n}(kx-1)$?

Or can I use other techniques to figure it out? I just want the result to be simple.
Comment: the product has the form of a pochhammer
 symbol (times monomial)which is a quotient of gamma functions. therefore the resulting integral will equal the laplace transform of a quotient of a gamma functions times a monomial, which very likely has no closed form
Answer: I will assume that $a \in \mathbb{R}$ with $a>0$. Start by computing the product of the integrand. Following (13) of this source,
$$
\prod_{k=1}^n (kx-1) = (-1)^n\sum_{k=0}^n S(n+1, k+1) x^{n-k}\tag{1}\label{eq:prod},
$$
where $S(n,k)$ denotes the signed Stirling number of the first kind. Plugging \eqref{eq:prod} into the integral, see that
$$
\begin{aligned}
\int_0^\infty \prod_{k=1}^n (kx-1)e^{-ax} \mathrm{d}x &= (-1)^n\sum_{k=0}^nS(n+1,k+1)\int^\infty_0 x^{n-k}e^{-ax} \mathrm{d}x \\
&= (-1)^n\sum_{k=0}^nS(n+1,k+1) \left[-\frac{\Gamma(n-k+1, ax)}{a^{n-k+1}}\right]^\infty_{x=0} \\
&= \left.(-1)^{n+1}\sum_{k=0}^nS(n+1, k+1)\frac{(n-k)!}{a^{n-k+1}}e^{-ax}\sum_{l=0}^{n-k}\frac{(ax)^l}{l!}\right|^\infty_{x=0},
\end{aligned}
$$
where $\Gamma(m, x)$ is the upper incomplete gamma function (see (2) for its expression with $m$ as an integer). Simplifying the last equality,
$$
\boxed{\int_0^\infty \prod_{k=1}^n (kx-1)e^{-ax} \mathrm{d}x = (-1)^n \sum_{k=0}^n S(n+1, k+1) \frac{(n-k)!}{a^{n-k+1}}.}
$$
Comment: Thanks for your kind words. For the first step, just change $x \rightarrow -x$, so $\prod^n_{k=1}(1+kx) \rightarrow \prod^n_{k=1}(1-kx) = (-1)^n \prod^n_{k=1} (kx-1)$.
Comment: +1 for introducing some good ideas.  I haven't checked all the steps, but I'll try to find time to check for some specific values of $a,n$.  I notice the first step refers to a formula for the product of $(kx+1)$ not $(kx-1)$.


How do you create a "nested" bar graph?

Question: The last image in this blog post.
I have tried searching "nested bar graph" and "hierarchical bar graph", but they may not be the word for it.
Answer: Use ggplot and create separate layers:
library(ggplot2)

set.seed(1)
stupid <- data.frame(
    group= LETTERS[1:5],
    men = sample(1:10, 5),
    women = sample(1:10, 5)
)

# Melt the data and calculate totals
mstupid <- melt(stupid, id.vars="group")
stupidTotal <- ddply(mstupid, .(group), summarize, value=sum(value))

ggplot() +
    geom_bar(data=stupidTotal, aes(x=group, y=value), fill="grey50") +
    geom_bar(data=mstupid, aes(x=group, y=value, fill=variable),
             stat="identity", position="dodge") +
    theme_bw()

Answer: Look for 'barNest' in package plotrix
Answer: Use this:
ggplot() +
    geom_bar(data=stupidTotal, aes(x=group, y=value, fill="grey50"), stat="identity") +
    geom_bar(data=mstupid, aes(x=group, y=value, fill=variable),
             stat="identity", position="dodge") +
    theme_bw()

How do you create a "nested" bar graph?


find $\sum_{k=1}^{13}\sin^{2n}\left({\frac{k\pi}{13}}\right)$?

Question: Prove that
$\sum_{k=1}^{13}\sin^{10}\left({\frac{k\pi}{13}}\right)=\frac{819}{256}$.
In general, how to find $\sum_{k=1}^{13}\sin^{2n}\left({\frac{k\pi}{13}}\right)$?
Thanks in advance.
Answer: Suppose we seek to evaluate
$$S = \sum_{k=1}^{m-1} \sin^{2q}(k\pi/m)
= \sum_{k=0}^{m-1} \sin^{2q}(2\pi k/2/m).$$
Introducing $\zeta_k = \exp(2\pi i k/2/m)$ (root of unity)
we get
$$S = \sum_{k=0}^{m-1} \frac{1}{(2i)^{2q}}
(\zeta_k - 1/\zeta_k)^{2q}.$$
We also have
$$\sum_{k=m}^{2m-1} \frac{1}{(2i)^{2q}}
(\zeta_k - 1/\zeta_k)^{2q}
\\ = \sum_{k=0}^{m-1} \frac{1}{(2i)^{2q}}
(\zeta_k \exp(2\pi i m/2/m)
- 1/\zeta_k/\exp(2\pi i m/2/m))^{2q}
\\ = \sum_{k=0}^{m-1} \frac{1}{(2i)^{2q}}
(-\zeta_k + 1/\zeta_k)^{2q}
\\ = \sum_{k=0}^{m-1} \frac{1}{(2i)^{2q}}
(\zeta_k - 1/\zeta_k)^{2q} = S.$$
We conclude that
$$S = \frac{1}{2} \sum_{k=0}^{2m-1} \frac{1}{(2i)^{2q}}
(\zeta_k - 1/\zeta_k)^{2q}.$$
Introducing
$$f(z) = \frac{(-1)^q}{2^{2q+1}}
\left(z-\frac{1}{z}\right)^{2q} \frac{2mz^{2m-1}}{z^{2m}-1}
\\ = \frac{(-1)^q}{2^{2q+1}}
\frac{(z^2-1)^{2q}}{z^{2q}} \frac{2mz^{2m-1}}{z^{2m}-1}$$
we then have
$$S = \sum_{k=0}^{2m-1} \mathrm{Res}_{z=\zeta_k} f(z).$$
Observe  that the  term $(z^2-1)^{2q}$  cancels the  poles at  $\pm 1$
produced by  $z^{2m}-1$ which however is perfectly  acceptable as they
correspond   to   $\zeta_0   =   1$   and   $\zeta_m   =   -1$   where
$\zeta_k-1/\zeta_k$ is zero as well.
Residues sum to zero so we obtain
$$S + \mathrm{Res}_{z=0} f(z) + \mathrm{Res}_{z=\infty} f(z) = 0.$$
Now for the  residue at zero we see that when  $2q-1\lt 2m-1$ or $q\lt
m$ the residue is zero. Otherwise we get
$$\frac{(-1)^q}{2^{2q+1}} [z^{2q-2m}]
(z^2-1)^{2q} \frac{2m}{z^{2m}-1}
\\ = \frac{(-1)^q}{2^{2q+1}} [z^{2q}]
(z^2-1)^{2q} \frac{2m z^{2m}}{z^{2m}-1}
\\ = -2m \frac{(-1)^q}{2^{2q+1}}
\sum_{p=0}^q {2q\choose p} (-1)^{2q-p}
[z^{2q-2p}] \frac{z^{2m}}{1-z^{2m}}.$$
We must have $p=q-lm$ where $l\ge 1.$ This yields
$$-2m \frac{1}{2^{2q+1}}
\sum_{l=1}^{\lfloor q/m\rfloor}  {2q\choose q-lm} (-1)^{lm}.$$
This is correct even when $q\lt m.$
Continuing with the residue at infinity we find
$$\mathrm{Res}_{z=\infty} f(z) =
- \mathrm{Res}_{z=0} \frac{1}{z^2} f(1/z)
\\ = - \mathrm{Res}_{z=0} \frac{1}{z^2}
\frac{(-1)^q}{2^{2q+1}}
\frac{(1/z^2-1)^{2q}}{1/z^{2q}} \frac{2m/z^{2m-1}}{1/z^{2m}-1}
\\ = - \mathrm{Res}_{z=0} \frac{1}{z^2}
\frac{(-1)^q}{2^{2q+1}}
\frac{(1-z^2)^{2q}}{z^{2q}} \frac{2mz}{1-z^{2m}}
\\ = - \mathrm{Res}_{z=0}
\frac{(-1)^q}{2^{2q+1}}
\frac{(z^2-1)^{2q}}{z^{2q+1}} \frac{2m}{1-z^{2m}}.$$
This is the same as the first residue at zero except now $l$ starts at
$l=0$ and we obtain
$$-2m \frac{1}{2^{2q+1}}
\sum_{l=0}^{\lfloor q/m\rfloor}  {2q\choose q-lm} (-1)^{lm}.$$
Joining the two pieces we finally have
$$\bbox[5px,border:2px solid #00A000]{
m \frac{1}{2^{2q}} {2q\choose q}
+ m \frac{1}{2^{2q-1}}
\sum_{l=1}^{\lfloor q/m\rfloor}  {2q\choose q-lm} (-1)^{lm}.}$$


What is Bayes estimator of $\theta$ when loss function is $L(\theta,a)=I(|\theta -a|>\delta)$?

Question: Suppose $X$ given $\theta$ has pdf $f(x\mid \theta)=e^{-(x-\theta)}I(x>\theta)$ and there is a standard Cauchy prior on $\theta$. As part of an exercise, I am trying to find a Bayes estimator of $\theta$ when the loss function is $L(\theta,a)=I(|\theta -a|>\delta)$ for some specified $\delta>0$.
So posterior density of $\theta$ given the data $x$ is
$$\pi(\theta\mid x) \propto \frac{e^{\theta}}{1+\theta^2}I(\theta<x)$$
Now a Bayes estimator $d=d(x)$ is such that the following is minimized:
$$\int I(|\theta -d|>\delta)\pi(\theta\mid x)\,d\theta=1-\int_{d-\delta}^{d+\delta}\pi(\theta\mid x)\,d\theta$$
This is same as maximizing $\int_{d-\delta}^{d+\delta}\pi(\theta\mid x)\,d\theta$ with respect to $d$. I think $d$ must satisfy $\pi(d+\delta\mid x)=\pi(d - \delta \mid x)$. But is there a general solution of $d$ from here?
The loss function is increasing in $|\theta-a|$, for which I know that Bayes estimator is posterior median as long as the posterior is symmetric and unimodal. But I don't think the posterior here is symmetric. Any hint would be great.
Comment: Basically, the question is equivalent to finding an interval $[ \theta - d , \theta + d ]$ that maximizes the area under the curve of $ \pi ( x | \theta ) $, I doubt a nice general solution can be found for all potential density function.
Comment: It seems to me, intuitively, that if the posterior is unimodal with a central mode (as opposed to a mode at an endpoint), even if not symmetric, there would be only one $d$ such that $\pi(d+\delta|x) = \pi(d-\delta|x)$, and if the posterior doesn't have a nice enough closed form you'd probably just have to search for it - a one-dimensional root finding routine would do it - lacking elegance, I admit.
Answer: The posterior density
$$\pi(\theta|x)\propto \frac{e^\theta}{1+\theta^2}\mathbb I_{\theta<x}$$
is an increasing function (in $\theta$):
$$
\frac{\text d}{\text d\theta} \{\theta-\log(1+\theta^2)\}=1-\frac{2\theta}{1+\theta^2}=\frac{(1-\theta)^2}{1+\theta^2}\ge 0$$
Therefore, maximising the surface
$$\int_{d-\delta}^{\min(d+\delta,x)} \frac{e^\theta}{1+\theta^2}\,\text d\theta$$
should prove straightforward. If not, note that the surface decreases when $d+\delta$ overshoots $x$ (hence $d+\delta\le x$) and, on the opposite, that it increases with $d$ when $d+\delta\le x$. Hence
$$d^\star(x) = x-\delta$$is the optimal Bayesian decision.
Note: This exercise is somewhat related to Example 4.2 in my book, which points out the surprising phenomenon that, under a double exponential prior on $\theta$ and a Cauchy observation $x$ with median $\theta$, the MAP is always zero.
Comment: Got it. Thanks.
Answer: I think, as per my comment, it is worth noting that a maximum interval must exist and please someone critiques my proof.
WLOG,by extreme value theorem. Since integral must be continuous for a well-defined density, there exist an interval of width $2 \delta$ for each of set of intervals $ \{[-\delta , \delta],[-2 \delta,2 \delta],...\} $  that maximize the area under the curve of each interval
Since density function converges to zero as $ \theta \rightarrow \pm \infty $ , so does  $ \int_{d - \delta }^{d + \delta} \pi (\theta | x ) d\theta $ as $ d \rightarrow \pm \infty $
This means there exists a K such that for all $d \in  R \backslash [-K\delta,K\delta] $ , $ \int_{d - \delta }^{d + \delta} \pi (\theta | x ) d\theta <  \int_{ - \delta }^{ + \delta} \pi (\theta | x ) d\theta$
Or in other words, one of the intervals contains the maximum $2\delta$ interval that maximizes the area.
Since there are a maximum interval, the method you suggest should work, maybe need some intuitive minor tweaking to handle the cases where the density is not a continuous function
Have to say I suddenly realized the answer before sleep and so not sure if there are error in my proof
Edit: The assumption that density function must tend to zero is flawed.
However, its conclusion that integral must converge to zero is still valid using Monotone convergence theorem.