Continuar "do while" do último ponto

Question: Existe um programa de cadastros que devo realizar, coloco as opções de cadastro e etc. Todo o código é salvo nos vetores e ficam conforme o esperado, mas quando retorno ao main(), ele volta ao numero 1, pois precisei inicializar o mesmo nas primeiras linhas do case.
Quero saber algum caminhos onde posso continuar a partir do ultimo ponto, por exemplo, encerrei 10 cadastros, retorno ao menu para realizar outras ações, mas quando voltar a opção de cadastro de clientes, quero que ele continue a partir do ultimo (11 e etc). Bloco do Case 1.
int main(void) {

//Variaveis

int opcao;
clientes cadastro[100];
int i;
char sair_cad;

//Codigo

printf("\n\n1 - Cadastrar Novo Cliente\n");
printf("2 - Cliente\n");
printf("3 - Alterar Cliente\n");
printf("4 - Excluir Cliente\n");
printf("5 - Cadastrar Automovel\n");
printf("6 - Automovel\n");
printf("7 - Alterar Automovel\n");
printf("8 - Excluir Automovel\n");
printf("9 - Locacao\n");
printf("\n  Selecione uma opcao por favor: ");
scanf("%d", &opcao);
getchar();

switch (opcao)
{
case 1: // Codigo do Cadastro de Clientes

    i = 1;

    do
    {
        system("cls");
        cadastro[i].codigo_cliente = i;
        printf("\n\nCodigo do cliente:  %d", i);

        printf("\nNome:  ");
        scanf("%[^\n]s", &cadastro[i].nome);
        setbuf(stdin, NULL);
        printf("RG:  ");
        scanf("%s", &cadastro[i].rg);
        setbuf(stdin, NULL);
        printf("CPF:  ");
        scanf("%s", &cadastro[i].cpf);
        setbuf(stdin, NULL);
        printf("Endereco:  ");
        scanf("%[^\n]s", &cadastro[i].endereco);
        setbuf(stdin, NULL);
        printf("Data de Nascimento:  ");
        scanf("%s", &cadastro[i].data_nasc);
        setbuf(stdin, NULL);
        printf("Registro Habilitacao:  ");
        scanf("%s", &cadastro[i].registo_habilitacao);
        setbuf(stdin, NULL);
        getchar();
        system("cls");
        printf("\n\n\t\t\t\tCadastro Salvo com Sucesso!");
        printf("\n\n Deseja realizar outro cadastro: S/N ?:   ");
        scanf("%c", &sair_cad);
        setbuf(stdin, NULL);

        i = i + 1;

    } while (sair_cad != 'n');
    system("cls");

    return main();

Comment: A resposta resolveu sua dúvida? Acha que pode aceitá-la? Veja o [tour] se não souber como faz. Isso ajudaria muito indicar que a solução foi útil para você. Também pode votar em qualquer pergunta ou resposta que achar útil no site todo (quando tiver 15 pontos).
Comment: `return main()`? É recursivo infinito seu código?
Answer: A primeira coisa que precisa mudar é eliminar a chama recursiva que vai acabar trazendo problemas. Pode não causar nada no exercício, mas vai aprender errado. Isto estourará a pilha em uma execução prolongada. Resolva isto com um laço. Aí é só manter a inicialização do contador fora do laço que manterá o número que estava.
Na verdade muita coisa neste código não é uma boa, mas não vou tentar mexer nisto.
int main(void) {
    int opcao;
    clientes cadastro[100];
    int i = 1;
    do {
        printf("\n\n1 - Cadastrar Novo Cliente\n");
        printf("2 - Cliente\n");
        printf("3 - Alterar Cliente\n");
        printf("4 - Excluir Cliente\n");
        printf("5 - Cadastrar Automovel\n");
        printf("6 - Automovel\n");
        printf("7 - Alterar Automovel\n");
        printf("8 - Excluir Automovel\n");
        printf("9 - Locacao\n");
        printf("\n  Selecione uma opcao por favor: ");
        scanf("%d", &opcao);
        getchar();
        switch (opcao) {
        case 1: // Codigo do Cadastro de Clientes
            do  {
                char sair_cad;
                system("cls");
                cadastro[i].codigo_cliente = i;
                printf("\n\nCodigo do cliente:  %d", i);
                printf("\nNome:  ");
                scanf("%[^\n]s", &cadastro[i].nome);
                setbuf(stdin, NULL);
                printf("RG:  ");
                scanf("%s", &cadastro[i].rg);
                setbuf(stdin, NULL);
                printf("CPF:  ");
                scanf("%s", &cadastro[i].cpf);
                setbuf(stdin, NULL);
                printf("Endereco:  ");
                scanf("%[^\n]s", &cadastro[i].endereco);
                setbuf(stdin, NULL);
                printf("Data de Nascimento:  ");
                scanf("%s", &cadastro[i].data_nasc);
                setbuf(stdin, NULL);
                printf("Registro Habilitacao:  ");
                scanf("%s", &cadastro[i].registo_habilitacao);
                setbuf(stdin, NULL);
                getchar();
                system("cls");
                printf("\n\n\t\t\t\tCadastro Salvo com Sucesso!");
                printf("\n\n Deseja realizar outro cadastro: S/N ?:   ");
                scanf("%c", &sair_cad);
                setbuf(stdin, NULL);
                i++;
            } while (sair_cad != 'n');
        case 2:
        } while (opcao != 0);
        system("cls");
    }
}

Coloquei no GitHub para referência futura.
Answer: A solução apresentada por Maniero é válida.
Seu inimigo aqui é a chamada return main();, pode parecer interessante a ideia de economizar linhas de código reutilizando a função, mas no seu caso ela acaba sendo ruim, afinal estais tentando resolver todo o problema somente em uma função (a principal).
O que acontece com seu programa:

Inicia em Main
Declara variáveis.
Inicializa I em 1 (vetores iniciam em zero, não se preocupe, é um erro bem comum no início).
Faz o cadastro até usuário querer voltar ao menu.
Retorna main()
Declara Variáveis (já que não há uma referência, elas voltam a iniciar em estado zero)
....

Continuar "do while" do último ponto


Can a negative binomial distribution be used to model a continuous distribution?

Question: I have a data set which is a set of continuous distances from some origin. I originally modeled this as a negative binomial distribution by rounding the data and using it as an input in the Matlab function, nbinfit:
<code>nbReg = nbinfit(round(data));
</code>
Using the output from nbinfit, I created the following CDF:

Here are the corresponding QQ Plots

Questions

Is it valid to use a negative binomial distribution to model a continuous distribution? Is there any documentation in the literature where negative binomial was used to model continuous data?
It is possible to have a similarly distributed continuous distribution
(you can remove the x == round(x) condition in nbinpdf to get a continuous distribution)?
The CDF fits very well, but is there a "better" distribution you would recommend?**
Comment: Those don't look like good fits.  Plotting a CDF against an EDF is not very revealing: use probability plots instead.
Comment: @Whuber, added QQ plots to question
Answer: The fit of your approximation is not a great tool for evaluating the choice, since you have thrown out some of the information in the data.  The more you do this, the easier it is to find a distribution that fits the data.  For example, you could perfectly match the data to a bernoulli distribution if you "rounded" your data to a binary indicator above and below a threshold.  Obviously your approximation is not so bad as this, but there is no reason to be rounding at all, as there are many other distributions you could be using, and no obvious connection between distances from an origin and the negative binomial.
For example, if you are looking for a continuous distribution that only has support for positive distances, you could use a lognormal or a gamma (or many others, but those are obvious choices).  If you have data with exact zeros (it is hard to tell from the plot and the motivation), you could use a compound poisson-gamma (aka Tweedie with power between 1 and 2).
Without more information about the application, it is hard to give a more detailed answer, since the choice of distribution should reflect the data generating process (at least to the extent possible).
Comment: +1 A discretized gamma will closely approximate the negative binomial.  To find a gamma corresponding to a negative binomial you can match moments, after first adjusting the mean of the gamma by half the bin width and its variance by 1/12 the squared bin width.


Hibernate @NotEmpty is deprecated

Question: I am using 5.2.13.Final version of hibernate-core and
5.2.13.Final version of hibernate-validator.
@NotEmpty
@Column(name = "SSO_ID", unique = true, nullable = false)
private String ssoId;

@NotEmpty comes in picture when I need the following validation message from themessage.properties file when ssoId is empty:
NotEmpty.user.ssoId=SSO ID can not be blank.
But @NotEmpty is deprecated in the latest version. What is its equivalent?
Comment: Try to use `javax.validation.constraints.NotEmpty` instead (cf. https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/constraints/NotEmpty.java) .
Answer: Generally, as something is marked as deprecated in a javadoc, the javadoc explains also what is the alternative to.
The org.hibernate.validator.constraints.NotEmpty annotation javadoc states (emphasis is mine) :

Deprecated. use the standard javax.validation.constraints.NotEmpty constraint instead

Note that since Hibernate 5, Hibernate deprecated several of its specific validation constraint classes in favor to standard JSR 380-bean validation classes.
Note also that the Hibernate Validator 6 library is actually the reference implementation of this JSR.
Here is an updated list of deprecated API for Hibernate validator.
Comment: that means we have to use both kind of annotations, Hibernate and javaannotations together?
Comment: Not "have to" but you may if you use specific Hibernate validation annotations that don't have counterparts in the JSR.  If you use Hibernate Validator 6, it should not be a problem as it is the default implementation of the JSR. So you would have only `javax.validation` annotations.
Answer: Use
import javax.validation.constraints.NotBlank;
instead of
import org.hibernate.validator.constraints.NotBlank;
Comment: Can you please elaborate with a working answer? Where does "NotBlank" go? Is it also an attribute (@NotBlank) ?

Hibernate @NotEmpty is deprecated


Triple integral, how to bound the region

Question: Given a region, which is bounded below by z=0, and enclosed by x^2+y^2+4z^2=a^2, where a is positive.
I want to know how to bound the region. Which coordinates should I use, cylindrical or spherical? Is there some technics to do this situation?
Comment: Usually when you have three variables power to square, the most common variable change is using spherical coordinates, you can draw the surface to understand better the region and can put the integration limits, but if you are more expert can deduce it form the equation of the surface.
Comment: But what is the method of finding the bound? I mean, in this question for instance, if I simply write x,y, and z in their spherical form, I don't know how to deduce r θ and φ
Answer: If the surface was $x^2+y^2+z^2 = a^2$, you'd want to use spherical coordinates. Since the surface is $x^2+y^2+(2z)^2 = a^2$ instead, a slight modification yields bounds which are constants. Set $$x = \rho \cos\theta \sin \phi, \quad y = \rho \sin\theta \sin\phi, \quad z = \dfrac{1}{2}\rho \cos\phi.$$
The bounds are $\rho \in [0,a]$, $\theta \in [0,2\pi]$, and $\phi \in [0,\tfrac{\pi}{2}]$ (since $z \ge 0$), and the Jacobian of the transformation (if you need it) is $\tfrac{1}{2}\rho^2\sin\phi$.
Comment: Shouldn't it be theta from 0 to pi/2 and phi from 0 to 2pi?
Comment: It depends what notation you use. In the above, I used $\theta$ as the polar angle and $\phi$ as the azimuthal angle. If you used the opposite notation, you can just switch all the $\theta$'s and $\phi$'s above.


Como fazer uma busca com Query Builder no Laravel?

Question: Eu necessito exibir o nome do médico que solicitou um laudo de paciente, porem, com Laravel eu tentei mostrar esta informação na View assim:
{{laudo->paciente->medico->nome}}

Então eu recebia a mensagem de erro:

Trying to get property of non-object

Como eu faço a consulta utilizando o Query Builder segundo o principio desta query:
select m.nome from medicos m, pacientes p, laudos l where m.id=p.medico_id and p.id=l.paciente_id and l.id=5;

Classe Medico

class Medico extends Model{
   //
    protected $fillable = [
        "nome", "crm", "email", "datanascimento", "senha"
    ];

    protected $table = 'medicos';

    public function pacientes(){
        return $this->hasMany('App\Paciente');
    }
}

Classe Paciente

class Paciente extends Model{
//
    protected $fillable = [
        "medico_id", "nome", "rg", "email", "datanascimento", "senha"
    ];

    protected $table = 'pacientes';

    public function medico(){
        return $this->belongsTo('App\Medico', 'medico_id');
    }
    public function laudos(){
        return $this->hasMany('App\Laudo');
    }
}

Classe Laudo

class Laudo extends Model{
//
    protected $fillable = [
        "codigo", "paciente_id", "nome_arquivo", "data_emicao"
    ];

    protected $table = 'laudos';

    public function paciente(){
        return $this->belongsTo('App\Paciente', 'id');
    }
}

Answer: Quando for carregar os laudos, utilize Eager Loading, o que irá carregar também registros de outras tabelas relacionadas:
$laudos = Laudo::with('paciente.medico')->get();

Fonte: https://laravel.com/docs/5.5/eloquent-relationships#eager-loading
Comment: Está retornando: "ErrorException: Trying to get property of non-object"
Comment: No exemplo estou carregando uma collection de laudos. Caso queira retornar apenas um laudo, pode filtrar pelo id.

Laudo::find($id)->with('paciente.medico');
ou
Laudo::where('id', $id)->with('paciente.medico')->first();
Answer: Para exibir o nome do médico na view será necessário acessar o dado da segunte maneira:
{{$laudo->paciente["medico"]["nome"]}}

Os dados ficam armazenados em um array.


Can't change sender address/domain in sendmail

Question: I have a server with sendmail and I need to send email througt PHP. I have an internal SMTP server on the same subnet, so I configured sendmail with FEATURE(nullclient', mail.server.local')dnl where mail.server.local is solved with an internal IP in /etc/hosts
There is no way that I can change the from address and domain. It's always firstname.lastname@example.com.
Mar 20 16:18:48 nameoftheserver sm-mta[16402]: v2KFImVi016402: from=<email@example.com>, size=406, class=0, nrcpts=1, msgid=<email@example.com>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]

The output of  /etc/mail/sendmail.mc:
      (short domain name) $w = nameoftheserver
  (canonical domain name) $j = nameoftheserver.localdomain.local
         (subdomain name) $m = localdomain.local
              (node name) $k = nameoftheserver

How I can change this values?
Comment: Where do you see `firstname.lastname@example.com`? Your next log line instead shows `firstname.lastname@example.com`
Comment: root@ it's in the log if I send from the cli using php, www-data is from the browser. I need to change that. I modified the question
Answer: So, you are trying to change the envelope sender (as PHP's mail() function already handles the "From:" address) with Sendmail. That can be achieved by building a genericstable database to map the input sender address to the address desired.

Create a text file /etc/mail/genericstable containing the mappings,
www-data       firstname.lastname@example.com
root           email@example.com

where the first value is the original username and the second value is the address desired. Or, if this doesn't work with your node server, the original sender address can also be in format
email@example.com  firstname.lastname@example.com

Create another file /etc/mail/generics-domains containing the domains, separated by newline.
(Notice, that the file MUST include your server's canonical domain name.)
Add the corresponding statements to /etc/mail/sendmail.mc:
FEATURE(`genericstable',`hash -o /etc/mail/genericstable.db')dnl
GENERICS_DOMAIN_FILE(`/etc/mail/generics-domains')dnl

Do the normal procedures after re-configuring Sendmail, e.g.
cd /etc/mail
/usr/bin/make
/usr/bin/newaliases
systemctl restart sendmail.service

Comment: You're welcome! I didn't care to reply earlier because the comment didn't add any new information (i.e. error messages or logs) and the solution actually should have worked.
Comment: didn't workI did it but did not work
Comment: Update: forget about my previous message, your solution resolve the problem! Many thanks!


Porting cshell code to bourne shell

Question: cat /tmp/SYSLOG | tee >( egrep -i "session limit" > sess.txt) | egrep "dst_port=3389 " > dst.txt

Works n C-shell but not Bourne.  I escaped the brackets and it no longer complains but it does not work. HELP!
Comment: what error do you get?
Comment: Are you sure that is C shell? The [`tcsh`](http://www.tcsh.org/tcsh.html/top.html) manual doesn't recognize `>( ... )` notation AFAICS.  It is a `bash` notation.
Answer: That looks like Process substitution in bash.
If you're looking for genuine Bourne shell (rather than bash), you will have your work cut out simulating that.  In fact, your best bet would be to obtain a program that explicitly runs other programs (pee is one such; ISTR there being others — see Is it possible to distribute stdin over parallel processes? where GNU parallel is mentioned).
If you can't do that either, then you'll have to revisit the code.  The simplest fix looks like:
egrep -i "session limit"  /tmp/SYSLOG > sess.txt &
egrep    "dst_port=3389 " /tmp/SYSLOG > dst.txt  &
wait

This runs the commands in parallel. The second & is unnecessary but present for symmetry. In fact, this will work in any shell, so it is the portable solution. Unless the cat /tmp/SYSLOG operation is actually a complex program, this works at least as neatly as the original code.
Answer: Bourne doesn't do process substitution, i.e. >(...). Use bash instead.
Answer: You can call out to awk in your bourne shell script:
awk '
    tolower($0) ~ /session limit/ {print > "sess.txt"}
    /dst_port=3389 /              {print > "dst.txt"}
' /tmp/SYSLOG

If you happen to have GNU awk:
gawk -v IGNORECASE=1 '
    /session limit/  {print > "sess.txt"}
    /dst_port=3389 / {print > "dst.txt"}
' /tmp/SYSLOG

Porting cshell code to bourne shell


Filter by postal code as location within user defined radius

Question: I have to make a filter in which user will add postal code and select a radius.
Suppose, user entered postal code = 12345 (which results into city 'XYZ') and the radius of 200 miles. So all nodes containing the location within the radius of 200 miles of the city 'XYZ' will be the output.
Can anyone suggest some solutions?
Answer: geofield should have what you're after.  It includes proximity handling and an exposable views filter.
Answer: Views exposed filter with Proximity/Distance worked..... :)
Comment: Can you please elaborate your answer so that it would be helpful for future readers :)


cartesian distance (2-norm) between paired sampled datasets

Question: I have uniformly sampled datasets from two acquisition circuits: an old model which has become difficult (and expensive) to procure parts to continue manufacturing, and a new version using modern microelectronics.  Paired datasets have been produced by measuring the same phenomenon using the same transducers and swapping the acquisition circuits.  (Each experiment produces a dataset: I have pairs of datasets, not datasets of pointwise paired experiments)
The paired datasets ought to be be the same scale and alignment along the independent variable, but in practice, tolerances are causing non-negligible mismatch.  I'm looking for a metric to measure agreement between the old and new systems.

The classic is to assume alignment and find mean absolute value or rms of the difference in dependent variable, evaluated at the same index.
$$ \left ( \sum \left| x(f) - y(f) \right|^p \right)^{1/p} $$
But that breaks down when the independent variable is misaligned.
For regression, one can use "orthogonal least-squares", and, based on some distance metric on points, calculate a norm of the distances between samples and nearest approach of the regression curve.
But here I don't have a parametric curve to compare against, I have another sampled dataset.
Also, if the scaling were identical and only the position had a small shift, one could use apply a translation optimized by maximizing auto-correlation, and then measure distance in only the dependent variable.  But I suspect small deviations also in scaling along the independent variable.
Is there a common metric used for distance between one sampled dataset and another dataset, also sampled?
I'm considering a "distance to nearest neighbor" approach, requiring all points from both datasets to be used, to avoid an undesirable asymmetry.  Any expected problems with that approach?
Comment: [Dynamic time warping](https://en.wikipedia.org/wiki/Dynamic_time_warping) may be of interest.
Comment: @StephanKolassa: That looks very applicable, thanks.  It looks very like Levenshtein distance, with insertion and deletion operations, only with soft- instead of hard- decision matching.  I'll explore some of the references from there as well.  Thanks for the pointer!
Answer: Dynamic time warping (DTW), as suggested by @StephanKolassa, is an excellent idea with lots of available tools. Perhaps a more modern, foundational approach would be to apply functional data analysis (FDA). One such approach, called elastic FDA, provides tools which can be viewed as a continuous extension of  DTW. These slides provide a nice starting place, if you're interested in learning more.
Note: I  am not an FDA person myself, but I at least partially buy into their advantages over classical discrete approaches like DTW. The argument for FDA usually goes: develop the theory on function spaces, not vectors, and discretize only at the last step: implementation. This is summed up in the short quote by the father of FDA, "Discretize as late as possible" -Ulf Grenander.

Broadly, the strategy is

Try to align the functions

(A) Measure amplitude distance on aligned functions

(B) Measure phase distance on aligned functions based on how much "work" was needed to align the functions.

Let's walk through a simple example with the fdasrvf package (in R).
# Generate data
N <- 100
t <- sort(runif(N))
f1 <- sin(4*pi*t) * t
f2 <- sin(4*pi*(t-1/20)) * t^1.1

plot(t, f1, type='l',
     main="Original misaligned data")
lines(t, f2, col='orange')

Next, I will use the elastic.distance function to get some measures of the distance between these two functions
fdasrvf::elastic.distance(f1, f2, t)

which returns
$Dy
[1] 2.712711

$Dx
[1] 0.3922354

The $D_y$ term is called the amplitude distance and $D_x$ is called the phase distance. Essentially, $D_y$ is what you want if the functions are aligned and $D_x$ tells you how hard we had to work to align the functions.
To break this down a little farther, let's see what the elastic FDA is doing. First, it attempts to align the functions, yielding something like:
ff <- cbind(f1, f2)
fda <- fdasrvf::align_fPCA(ff, t, lambda=0)

plot(t, fda$fn[,1], type='l', main="Aligned Data")
lines(t, fda$fn[,2], col='orange')

Now that the functions have been "phase-aligned", the usual amplitude distance is well defined and meaningful. In order to get these aligned functions, we apply a warping function, so that $f_\text{align}(t) = \gamma(f(t))$. These warping functions look like:
plot(t, fda$gam[,1], type='l', main="Warping functions")
lines(t, fda$gam[,2], col='orange')
abline(0, 1, lwd=2, col='red')

How far these functions differ from the identity line tells us how hard we had to work to align them. This is directly related to $D_x$, the phase distance.


How to show that the Hessian matrix of $G$ is positive definite?

Question: Let $\{g_i:X\subset\Bbb R\to\Bbb R;\;i=1,\ldots,m\}$ be a linearly independent set of real functions.
Given $n$ points $(x_1,y_1),\ldots,(x_n,y_n)\in X,$ consider the following function
$$G(\beta_1,\ldots,\beta_n)=\sum_{k=1}^n\left(\sum_{l=1}^m\left[ \beta_lg_l(x_k)-y_k\right]\right)^2$$
I need to prove that $G$ attains a local minimum. For this, I need to show that the Hessian matrix of $G$ is positive definite, but I'm not able to do. I'm trying to show it, but I'm able to prove that it's just positive semidefinite (for this, I'm doing things like this and this). Can someone help me show that the Hessian matrix of $G$ is positive definite?
Notice that
$$\frac{\partial G}{\partial \beta_i}=\sum_{k=1}^n\left(2g_i(x_k)\sum_{l=1}^m\left[ \beta_lg_l(x_k)-y_k\right]\right)$$
and
$$\frac{\partial^2 G}{\partial \beta_j\beta_i}=2\sum_{k=1}^ng_i(x_k)g_j(x_k)$$
Hence, the Hessian matrix of $G$ is
$$H=2
\begin{bmatrix}
\sum_{k=1}^ng_1(x_k)^2 & \sum_{k=1}^ng_1(x_k)g_2(x_k) & \cdots & \sum_{k=1}^ng_1(x_k)g_m(x_k)\\
\sum_{k=1}^ng_2(x_k)g_1(x_k) & \sum_{k=1}^ng_2(x_k)^2 & \cdots & \sum_{k=1}^ng_2(x_k)g_m(x_k)\\
 &  & \vdots & \\
\sum_{k=1}^ng_m(x_k)g_1(x_k) & \sum_{k=1}^ng_m(x_k)g_2(x_k) & \cdots & \sum_{k=1}^ng_m(x_k)^2
\end{bmatrix}$$
Thanks.
Answer: If you take the matrix $A$ with entries $ A_{kj} = g_j(x_k) $ then $A$ should be invertible from linear independence of $g_i$s and you have entries of Hessian assuming your calculation as
$$ H_{ij} = \frac{\partial^2 G}{\partial \beta_i\partial \beta_j} = 2 \sum_{k=1}^n A^T_{ik} A_{kj} $$
Hence $ H = 2A^T A $ which is always positive definite for invertible $A$.
Comment: A is not necessarily square. And if A is square, some cases it's not invertible (its columns are not linearly independent. See here: http://math.stackexchange.com/questions/422015/show-that-this-set-is-linearly-independent).
Answer: For the sake of readability, I'm going to rename some of your variables.
Suppose the given set of linearly independent functions is $\{\varphi_i:\Bbb R\to\Bbb R\mid i=1,\ldots,m\}$  and the given ponts are $(x_1,f_1),\ldots,(x_n,f_n).$
Now, if we change $G$ into $S$  and $(\beta_1,\ldots,\beta_m)$ into $(a_1,\ldots,a_m),$ we can approach your problem as follows:
\begin{aligned}S(a_0,\ldots,a_m):&=\sum_{k=1}^n\left(f_k-\sum_{i=1}^ma_i\varphi_i(x_k)\right)^2\\\text{searching for candidates}&\text{ for the minimum}\\0=\frac{\partial S}{\partial a_j}(a_0,\ldots,a_m)&=-2\sum_{k=1}^n\varphi_j(x_k)\left(f_k-\sum_{i=1}^ma_i\varphi_i(x_k)\right)\\\Rightarrow\sum_{i=1}^m\sum_{k=1}^n\varphi_j(x_k)\varphi_i(x_k)a_i&=\sum_{k=1}^n\varphi_j(x_k)f_k,j=1,\ldots,m\\A:&=\begin{bmatrix}\varphi_1(x_1)&\varphi_2(x_1)&\ldots&\varphi_m(x_1)\\\vdots&\vdots&\ddots&\vdots\\\varphi_1(x_n)&\varphi_2(x_n)&\ldots&\varphi_m(x_n)\end{bmatrix}\\\Rightarrow\underbrace{\begin{bmatrix}\varphi_1(x_1)&\ldots&\varphi_1(x_n)\\\varphi_2(x_1)&\ldots&\varphi_2(x_n)\\\vdots&\ddots&\vdots\\\varphi_m(x_1)&\ldots&\varphi_m(x_n)\end{bmatrix}}_{A^T}\cdot\underbrace{\begin{bmatrix}\varphi_1(x_1)&\varphi_2(x_1)&\ldots&\varphi_m(x_1)\\\vdots&\vdots&\ddots&\vdots\\\varphi_1(x_n)&\varphi_2(x_n)&\ldots&\varphi_m(x_n)\end{bmatrix}}_{A}\cdot\begin{bmatrix}a_1\\a_2\\\vdots\\a_m\end{bmatrix}&=\begin{bmatrix}\sum_{k=1}^n\varphi_1(x_k)f_k\\\sum_{k=1}^n\varphi_2(x_k)f_k\\\vdots\\\sum_{k=1}^n\varphi_m(x_k)f_k\end{bmatrix}\\\langle B\mathbf a,\mathbf a\rangle&=\langle A^TA\mathbf a,\mathbf a\rangle\\&=\langle A\mathbf a,A\mathbf a\rangle\\&=\|A\mathbf a\|_2^2\\&=\left\|\sum_{i=1}^ma_i\begin{bmatrix}\varphi_i(x_1)\\\varphi_i(x_2)\\\vdots\\\varphi_i(x_n)\end{bmatrix}\right\|>0\end{aligned} since $\varphi_i$'s are linearly independent, $$\begin{aligned}\sum_{i=1}^ma_i\varphi_i&=0\\\Leftrightarrow\sum_{i=1}^ma_i\varphi_i(x)&=0,\forall x\in\Bbb R\\\Leftrightarrow a_i&=0,\forall i\in\{1,\ldots,m\}.\end{aligned}$$ Your solution $\mathbf a$ must then be unique.


Can't get validation errors back from an AJAX form

Question: I have a Drupal form that should submit via AJAX. I want to validate the input is correct. A few things are happening:

The validateForm method is being hit
I'm not able to see the errors in the form with getErrors
I want to validate the input, and if an error occurs, flash a message in the form that it is invalid
On success, I want to replace the form with a success message (but its not getting that far)

Here is my form code:
<?php

namespace Drupal\harlib_newsletter_signup\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class NewsletterSignup.
 */
class NewsletterSignup extends FormBase {

  public $uniqueIdentifier = 'no_tagline';

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'newsletter_signup_' . $this->uniqueIdentifier;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    if ($this->uniqueIdentifier == "with_tagline") {
      $show_tagline = TRUE;
    }
    else {
      $show_tagline = FALSE;
    }

    $wrapper = 'ajax-wrapper-' . $this->uniqueIdentifier;

    $form['#prefix'] = '

';
    $form['#suffix'] = '

';

    $form['#attributes']['class'][] = 'hl__single-input-form__form';
    $form['#attributes']['novalidate'] = 'novalidate';

    $form['title'] = [
      '#type' => 'label',
      '#for' => 'edit-email-address',
      '#title' => 'Stay in the know',
      '#attributes' => [
        'class' => [
          'hl__label',
          'hl__label--inline',
        ],
      ],
    ];

    if ($show_tagline) {
      $form['tagline'] = [
        '#markup' => '
Sign up for email updates from MySite
',
      ];
    }

    $errors = $form_state->getErrors();

    if ($errors = $form_state->getErrors()) {
      $form['errors'] = [
        '#markup' => $errors['email_address'],
      ];
    }

    $form['email_address'] = [
      '#type' => 'email',
      '#size' => NULL,
      '#required' => TRUE,
      '#attributes' => [
        'placeholder' => $this->t('Email'),
        'class' => [
          'hl__input',
          'hl__input--inline',
          'js-is-required',
        ],
        'data-twig-suggestion' => 'newsletter_signup_email',
      ],
      '#prefix' => '
',
    ];

    $form['submit'] = [
      '#type' => 'button',
      '#value' => $this->t('Sign up'),
      '#submit' => ['::submitForm'],
      '#attributes' => [
        'class' => [
          'hl__button',
          'hl__button--small',
          'hl__button--inline',
        ],
        'data-twig-suggestion' => 'newsletter_signup_submit',
      ],
      '#suffix' => '
',
      '#ajax' => [
        'wrapper' => $wrapper,
        'method' => 'replace',
        'callback' => '::ajaxRebuildForm',
        'progress' => [
          '#type' => 'none',
        ],
      ],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $email_value = $form_state->getValue('email_address');

    if (empty($email_value) || !\Drupal::service('email.validator')->isValid($email_value)) {
      $form_state->setError($form['email_address'], $this->t('Please enter a valid email address.'));
    }

    if (mailchimp_is_subscribed('XXXXXXX', $email_value, TRUE)) {
      $form_state->setError($form['email_address'], $this->t('You are already subscribed to our newsletter.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $result = mailchimp_subscribe('XXXXXXX', $form_state->getValue('email_address'));

    if (empty($result)) {
      $form_state->setError($form['email_address'], $this->t('You are already subscribed to our newsletter.'));
    }
    else {
      drupal_set_message($this->t('Thank you for subscribing to our newsletter!'));
    }
  }

  public function ajaxRebuildForm(array $form, FormStateInterface $formState) {
    return $form;
  }

}

Is ajaxRebuildForm necessary? How can I validate this? Is setError still workable in an ajax submit?
EDIT: I've updated the validateForm method to use AjaxResponse. Nothing is happening on my screen.
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $email_value = $form_state->getValue('email_address');

    if (empty($email_value) || !\Drupal::service('email.validator')->isValid($email_value)) {
      //$form_state->setError($form['email_address'], $this->t('Please enter a valid email address.'));
      //$form_state->setRebuild();
      $response->addCommand(new CssCommand('.hl__single-input-form__input-group', ['border' => '3px solid red']));
      $response->addCommand(new AlertCommand('Heyoooo'));
      return $response;
    }

    if (mailchimp_is_subscribed('XXXXXXX', $email_value, TRUE)) {
      $form_state->setError($form['email_address'], $this->t('You are already subscribed to our newsletter.'));
    }
  }

Comment: Just a note, if you had used a randomized wrapper ID like `'ajax-wrapper-' . uniqid()`, it wouldn't work as subsequent builds/submits would have different wrapper IDs.
Answer: This actually turned out to be a combination of a few things:

I was using this form more than once on the same page
It is an AJAX form, so the submit needed to be unique.

Adding:
$form['submit_' . $this->uniqueIdentifier] instead of $form['submit'] resolved the issue. I now get AJAX errors on both, and both submit correctly.
Answer: It's probably mailchimp_is_subscribed that is breaking it. I tried the code myself with a slight change:
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $email_value = $form_state->getValue('email_address');

    if (empty($email_value) || !\Drupal::service('email.validator')->isValid($email_value)) {
      $form_state->setError($form['email_address'], $this->t('Please enter a valid email address.'));
    }

    else {
      if (mailchimp_is_subscribed('XXXXXXX', $email_value, TRUE)) {
        $form_state->setError($form['email_address'], $this->t('You are already subscribed to our newsletter.'));
      }
    }
  }

If this does not change the behavior on your system, change the default theme and test it.
Your theme may be not printing the errors. If it does, then you might be missing the messages highlighted region from your template. Search for {{ page.highlighted }} in your theme or it's parent and make sure it's printed.


Why can I take $y=g(x,z)$ as a constant when doing partial differentiation?

Question: I was trying to solve this question:

Given $F(x,y,z) = 0$, and $x = f(y, z)$, $y = g(x, z)$, $z = h(x, y)$, show $\frac{\partial{x}}{\partial{y}}\frac{\partial{y}}{\partial{z}}\frac{\partial{z}}{\partial{x}} = -1$

I searched the solution, and one of the steps states to take the $\frac{\partial{F}}{\partial{x}}$ while holding $y$ constant. However, isn't $y$ defined in terms of $x$ and $z$? Therefore, when I execute this step and take $\frac{\partial{F}}{\partial{x}} = F'_{x}\frac{\partial{x}}{\partial{x}} + F'_{y}\frac{\partial{y}}{\partial{x}} + F'_{z}\frac{\partial{z}}{\partial{x}}$, then since $y$ is a function of $x$ and $z$, the $\frac{\partial{y}}{\partial{x}}$ term shouldn't be $0$ right? I know I'm wrong, in that the term should be $0$, but I just don't understand why since $y$ is defined in terms of $x$ and $z$
Comment: The partial derivatives of $F$ don't "see" that $x$ and $y$ are related.  This means, for instance, that $$\frac{\partial}{\partial x} F(x,y,z) \neq \frac{\partial}{\partial x} F(x,g(x,z),z)$$ which bothers some people, but it's intended.
Comment: You are confused about notation. Sometimes it is clearer to specify the point where ou are taking the derivative. You are writing $(x, y, z)$ to mean three things in the same equation: the arguments of $F,$ the points where the partial derivatives of $F$ are being taken and the functions $f,$ $g$ and $h.$

By definition,
$$
\left. \dfrac{\partial}{\partial x} \right|_{(a,b,c)} f = \lim_{h \to 0} \frac{f(a + h, b, c) - f(a, b, c)}{h},
$$
which is just the usual derivative of the function $\varphi(x) = f(x, b, c)$ at the point $x = a.$
Answer: You need to specify exactly which function you are dealing with.  Different formulas can look the same.  $F(x,y) \neq F(y,z) \neq F(x,z)$.  If
$$F(x,y,z) = 0, F(f(y,z), y, z) = 0$$
then
$$\frac{\partial F}{\partial y} = \frac{\partial F}{\partial x}\frac{\partial f}{\partial y} + \frac{\partial F}{\partial y}$$
Note that the same looking term "$\frac{\partial F}{\partial y}$" is on both sides but does not mean the same thing!  It is admittedly a pretty awful notation.  On the left we have $F(y,z)$ and on the right $F(x,y,z)$ and while people try to cope by adding subscripts explaining what is held constant, it's still confusing.
One solution is to simply make sure to give every function a different name.
Let $F(x,y,z) = F(f,y,z)$ and $f(y,z) = x$ and $G(y,z) = F(f(y,z), y, z)$ so $G$ has two inputs but all the same values as $F$ with three inputs.  Then we can say $$\frac{\partial G}{\partial y} = \frac{\partial F}{\partial x}\frac{\partial f}{\partial y} + \frac{\partial F}{\partial y}$$


What algorithms need feature scaling, beside from SVM?

Question: I am working with many algorithms: RandomForest, DecisionTrees, NaiveBayes, SVM (kernel=linear and rbf), KNN, LDA and XGBoost. All of them were pretty fast except for SVM. That is when I got to know that it needs feature scaling to work faster. Then I started wondering if I should do the same for the other algorithms.
Comment: Related: [How and why do normalization and feature scaling work?](http://stats.stackexchange.com/q/41704/12359)
Comment: Also: [Variables are often adjusted (e.g. standardised) before making a model - when is this a good idea, and when is it a bad one?](http://stats.stackexchange.com/q/19216/12359)
Answer: In general, algorithms that exploit distances or similarities (e.g. in the form of scalar product) between data samples, such as k-NN and SVM, are sensitive to feature transformations.
Graphical-model based classifiers, such as Fisher LDA or Naive Bayes, as well as Decision trees and Tree-based ensemble methods (RF, XGB) are invariant to feature scaling, but still, it might be a good idea to rescale/standardize your data.
Comment: Could you elaborate more about rescaling/standarizing data for RF and XGB? I don't see how it can influence the quality of model.
Comment: +1. Just note that XGBoost actually implements a second algorithm too, based on linear boosting. Scaling will make a difference there.
Answer: Here is a list I found on http://www.dataschool.io/comparing-supervised-learning-algorithms/ indicating which classifier needs feature scaling:

Full table:

In k-means clustering you also need to normalize your input.
In addition to considering whether the classifier exploits distances or similarities as Yell Bond mentioned, Stochastic Gradient Descent is also sensitive to feature scaling (since the learning rate in the update equation of Stochastic Gradient Descent is the same for every parameter {1}):

References:

{1} Elkan, Charles. "Log-linear models and conditional random fields." Tutorial notes at CIKM 8 (2008). https://scholar.google.com/scholar?cluster=5802800304608191219&hl=en&as_sdt=0,22 ; https://pdfs.semanticscholar.org/b971/0868004ec688c4ca87aa1fec7ffb7a2d01d8.pdf
Comment: @kjetilbhalvorsen well I explained for k-means and SGD, but there are many other algorithms and  models. There is a 30k-char limit on Stack Exchange :)
Comment: What lacks from this answer is some explication of why!!  See my answer for that.
Comment: Somewhat related:  https://stats.stackexchange.com/questions/231285/dropping-one-of-the-columns-when-using-one-hot-encoding/329281#329281
Comment: @FranckDernoncourt Can I ask you a question building on this? I have a dataset of both categorical and continuous data, for which I'm building an SVM. The continuous data is highly skewed (long tail). For transformation on the continuous should I do a `log transformation / Box-Cox` and *then* also `normalise the resultant data to get limits between 0 and 1`? So i'll be normalising the log values. Then calculate the SVM on the continuous and categorical (0-1) data together? Cheers for any help you can provide.
Answer: Adding to the excellent (but too short) answer by Yell Bond.  Look at what happens with a linear regression model, we write it with only two predictors but the issue do not depend on that.
$$
   Y_i = \beta_0 + \beta_1 x_i + \beta_2 z_i + \epsilon_i
$$
$i=1, \dots, n$. Now if you, say, center and scale the predictors to get
$$
   x_i^* = (x_i - \bar{x})/\text{sd}(x) \\
   z_i^* = (z_i - \bar{z})/\text{sd}(z)
$$
and instead fit the model (using ordinary least squares)
$$
   Y_i = \beta_0^* + \beta_1^* x_i^* + \beta_2^* z_i^* + \epsilon_i
$$
Then the fitted parameters (betas) will change, but they change in a way that you can calculate by simple algebra from the transformation applied.  So if we call the estimated betas from the model using transformed predictors for $\beta_{1,2}^*$ and the denote the betas from the untransformed model with $\hat{\beta}_{1,2}$, we can calculate one set of betas from the other one, knowing the means and standard deviations of the predictors.  The realtionship between the transformed and untransformed parameters is the same as between their estimates, when based on OLS. Some algebra will give the relationship as
$$
\beta_0=\beta_0^* - \frac{\beta_1^* \bar{x}}{\text{sd(x)}} -\frac{\beta_2^*\bar{z}}{\text{sd(z)}},\quad \beta_1 =\frac{\beta_1^*}{\text{sd(x)}},\quad \beta_2=\frac{\beta_2^*}{\text{sd(z)}}
$$
So standardization is not a necessary part of modelling. (It might still be done for other reasons, which we do not cover here).  This answer depends also upon us using ordinary least squares.  For some other fitting methods, such as ridge or lasso, standardization is important, because we loose this invariance we have with least squares.  This is easy to see: both lasso and ridge do regularization based on the size of the betas, so any transformation which change the relative sizes of the betas will change the result!
And this discussion for the case of linear regression tells you what you should look after in other cases: Is there invariance, or is it not? Generally, methods which depends on distance measures among the predictors will not show invariance, so standardization is important.  Another example will be clustering.
Comment: Can you explicitly show how one calculates one set of betas from the other in this particular example of scalings you have applied?
Comment: Can you please add this as a new question? with reference back here!
Comment: @kjetil Can I ask you a question building on this? I have a dataset of both categorical and continuous data, for which I'm building an SVM. The continuous data is highly skewed (long tail). For transformation on the continuous should I do a log transformation / Box-Cox and then also normalise the resultant data to get limits between 0 and 1? So i'll be normalising the log values. Then calculate the SVM on the continuous and categorical (0-1) data together? Cheers for any help you can provide


Error when I tried create new maven module in IntellijIDE 15

Question: I tried add new module to project in IntellijIDE 15 and I have error
Failed to create a Maven project
'C:/gitProjects/mayProj/pom.xml' already exists in VFS

And in my project create file moduleName.iml but in IDE not shows module folder and in project structure this module is shows.
Answer: When adding the module to an existing module that already has a POM, it is necessary to manually specify that the new module will be in a different directory.  This is counter-intuitive, but I think the "Add Module" dialog was not built with maven specifically in mind.
What this means is when you are adding a module to an existing module with a POM, for example, an aggregator project, on the second page of the dialog, you need to manually copy and paste the "Module Name" onto the end of the "Content root" and "Module file locations".
Comment: Confirm this is still an issue in 2017 and that the [issue was originally reported to JetBrains](https://youtrack.jetbrains.com/issue/IDEA-66021) over 6 years ago.
Answer: When you create the module manually in the context root in module file location, you must define the module name.
eg. C:\Users\Desktop\Desktop\release\1.2.1-SNAPSHOT\payment\xxx-xxx-xxx
Answer: The problem is Intellij will be failing to create module folder for your module,
so you need to specify your content-root path and module-file-location.
just copy and paste your module name to content-root and module-file-location
module-name = learn-example
content-root = xxxx/zubs/learn-example
module-file-location = xxxx/zubs/learn-example

Answer: Error when I tried create new maven module in IntellijIDE on MacBook


Drupal for a portfolio- Images as nodes or something else?

Question: I am creating a portfolio using Drupal. I was wondering about best practice for handling images.
Would it be best for me to have each image as a node and then create a view and display in a slideshow, or is there a different way that images can be handled - e.g. have a collection of images attached to a node of 'gallery'?
Comment: Two words: Media module
Answer: I'd go with the image-per-node option if you think you may at any point want to add additional information per image.  You can call the content type 'Project' or something like that and put an image field on the content type.  You can have an 'unlimited' checked as the number of images allowed in that field or only allow for a single sample image.  Up to you.
The point is - imagine that down the road you want to add extra information about that particular screenshot/project, such as the date you worked on it, or who your client was, or a link to the live site or something like that.  If you make a separate node for each project, you have the flexibility to add fields in the future.
If your entire portfolio is a single node with a bunch of images, you don't have the option to add extra information to explain those images later without re-doing the entire setup.
You can always just display them as a slideshow with no extra information, regardless of which option you choose, but I'd go with the one that allows for flexibility in the long term.  :-)


Why do I always get NSURLConnection timeouts when downloading images over cellular networks?

Question: My app appears to work terribly over cellular networks - the UI often has spinners instead of images, but not always. It uses NSURLConnections driven by concurrent NSOperations. It works fine on WIFI - no problems at all.
I discovered I'm getting many NSURLConnection timeouts, but have no idea why. How can I track this problem down?
Answer: To track down the problem, I first measured the maximum number of concurrent connections, and discovered that this was consistently around 55.

reduced the maximum to 10 (by setting the operation queue max value, less timeouts, but still not perfect
reduced the maximum to 4 - even less timeouts but still got some
set the maximum to 1 - this has got to work, right? Nope!

Sometimes the app would work properly with no concurrency at all (max == 1), but it still failed about half the time.
So I burned a support incident with Apple and got one of there most experienced engineers to advise. Based on suggestions from him I tried the following:

ran the app on a different carrier's cellular network (Verizon), and it worked perfectly. So the issue was not cellular per se (or iOS) but AT&T's cellular network (NY and NJ both fail)
switched from http to https, now it works flawless on AT&T with full concurrency
investigated the web endpoint to determine its capabilities, which turned out to be pretty poor (more later). I tried a different web endpoint with more capabilities, and the original problem went away using http.

What I learned from this was as follows:

there was no difference using iOS5.1 or iOS6
if you have this problem on AT&T 3G and are using http, try switching to https
if the endpoint is using HTTP1.0 and does NOT support 'Connection: keep-alive', then every http request is setting up and tearing down a TCP connection. I believe this 'thrashing' of the cellular network is why AT&T was disconnecting some of my sessions, but of course have no way to know this for sure.
using a HTTP1.1 service that supports persistent connections, the problem disappeared. In this case there is no TCP connection thrashing.
some HTTP1.1 services support 'pipelining', as does iOS (using the NSURLRequest setting, HTTPShouldUsePipelining), and if I can switch to this then my performance should greatly improve
there is a WWDC 2012 video that discusses how to improve network performance: Session 706 "Networking Best Practices"

EDIT
So this just gets more bizarre as I peel the onion! After further discussion, some web people did a test with CloudFront, and it did accept 'Connection: keep-alive'. I tried over and over to get it to work yesterday, but could not.
The web expert suggest I try it when using https and low and behold it did! For some reason, when using 'http' over AT&T 3G, that header tag is either removed or ignored. I tested my app with Wifi too. In all cases but AT&T/3G the 'Connection' was returned in the response.


Arithmetic mean and geometric mean in $L^p$ spaces

Question: Let $(X,\mathcal{M},\mu)$ a probability space. I want to show if $f_1\in L^{p_1}(\mu),$ $f_2\in L^{p_2}(\mu)$, ... , $f_n\in L^{p_n}(\mu)$, then $$(f_1f_2\cdots f_n)^{\frac{1}{1/p_1+\dots+1/p_n}}\in L^1(\mu).$$ In particular, if $\{f_1,\dots,f_n\}\subset L^1(\mu)$ then $\sqrt[n]{f_1\cdots f_n}\in L^1(\mu)$.
I think I can use the inequality of the arithmetic mean and geometric mean to conclude something of the form $$ \Big(\int_X |f_1\cdots f_n| \Big)^{\frac{1}{1/p_1+\dots+1/p_n}}\leq\prod_{i=1}^n\Big(\int_X (f_i)^{p_1}\Big)^{1/p_i}$$
Can anyone tell me if this the right way?
Greetings !
Answer: This is a consequence of the generalised Hölder's inequality: If
$$
\frac{1}{p_1}+\cdots+\frac{1}{p_n}=1,
$$
and $f_1\in L^{p_1}(X),\ldots,f_n\in L^{p_n}(X)$, then $f_1f_2\cdots f_n\in L^1(X)$, and
$$
\Big| \int_X f_1\cdots f_n\,dx\,\Big|\le \Big(\int_X \lvert\,f_1\rvert^{p_1}\Big)^{1/p_1}
\cdots \Big(\int_X \lvert\,f_n\rvert^{p_n}\Big)^{1/p_n}\!.
$$
If $\frac{1}{p_1}+\cdots+\frac{1}{p_n}=\frac{1}{a}\ne 1$, then
$\frac{a}{p_1}+\cdots+\frac{a}{p_n}= 1$
and $\,\lvert\, f_i\rvert^{1/a}\in L^{p_i/a}$, and using the above we obtain that
$f_1f_2\cdots f_n\in L^a(X)$, and
$$
\Big| \int_X \lvert \,f_1\cdots f_n\rvert^a\,dx\,\Big|\le \Big(\int_X \lvert\,f_1\rvert^{p_1}\Big)^{a/p_1}
\cdots \Big(\int_X \lvert\,f_n\rvert^{p_n}\Big)^{a/p_n}\!.
$$
Note that
$$
a=\frac{1}{\dfrac{1}{p_1}+\cdots+\dfrac{1}{p_n}}.
$$
Comment: but, what happen if i can't asume that $\frac{1}{p_1}+\dots+\frac{1}{p_n}=1$?
Comment: See my edited answer.


Showing the existence of an infinite (strong) antichain

Question: Suppose $P$ is a poset such that there exist (strong) antichains of size $n$ for all $n \in {\bf N}$; i.e. there exist sets $S_n$ of size $n$ in $P$ such that no pair of elements of $S_n$ has a common lower bound.
Must $P$ have an infinite antichain?
Comment: While the question indeed lacks context, it is clear and sounds interesting to me. I have seen many questions like this on this website and probably have been guilty myself of asking some. I have voted to reopen it.
Comment: Posts that merely state a question are discouraged. You can improve your post with additional context. Where did you encounter the problem? Why is it of interest? What background do you have as you approach the problem? At the moment, there is nothing in this post to distinguish it from a homework problem directly copied to this site. You can edit the post to improve it.
Answer: If there is an infinite set of minimal elements (or rather elements that below them the order is linear), we're done.
So let's work under the assumption there are no minimal elements (or rather, every element has two incompatible smaller elements), as there are only finitely many of them, and they have to be in every maximal antichain.
Now proceed by induction: pick an element $a$, and two incompatible elements smaller than $a$, call them $a_0$ and $b_0$. Now $a'$ has two incompatible elements below it, so we can choose one to be $a_1$ and $b_1$. Proceed by induction splitting $b_n$ to $a_{n+1}$ and $b_{n+1}$. Then $\{a_n\mid n\in\Bbb N\}$ is the antichain you seek.
Choice is necessary, since without choice it is consistent there are counterexamples. For example, if $S$ is a set which is a countable union of pairs that no infinite set of pairs admits a choice function, then the tree of choice functions from finitely many pairs will satisfy this.
Comment: I think your assumption only makes sense for weak antichains, defined as pairwise incomparable. OP asked about strong antichains, defined as pairwise incompatible.
Comment: Nope, sorry. This is about antichains in the sense asked by the OP. At each step we are taking ***incompatible*** elements to form the antichain. But it's good that you pay attention before voting.
Comment: Your assumption that you only have to worry about elements where the order below them is linear is invalid. You need to take care of elements that are above a pairwise-compatible set, not pairwise-comparable.
Comment: Good reading comprehension skills. Read that single line again.
Comment: I'm confused about what you mean: you said "elements that below them the order is linear". That's pairwise-comparable, not pairwise-compatible.
Comment: Confused? No, you've made up your mind at least 8 minutes ago. Have a nice day.
Answer: This is shown to be true for $\mathbb P=\tau\setminus\{\emptyset\}$ in
Lemma 2.10 of [0]. It's likely known
for general $\mathbb P$, but I can't find a citation, so I'll roll
my own proof here. Assume $\mathbb P$ has (strong) antichains of size $n$
for all $n<\omega$. For compatible $p,q\in\mathbb P$,
write $p\not\perp q$ and let $p\wedge q$ satisfy $p\wedge q\leq p,q$.
If $p,q$ are incompatible, write $p\perp q$.
Let $p^\downarrow=\{q\in\mathbb P:q\leq p\}$.
Say $p\in\mathbb P$ is bad if there exists $r_p\leq p$ such that
$r_p^\downarrow$ is pairwise compatible. Let
$\mathbb P_{bad}$ collect all bad points in $\mathbb P$, and say
$p\sim q$ for $p,q\in\mathbb P_{bad}$ if $r_p\not\perp r_q$.
This is obviously symmetric and reflexive, and if we assume
$p\sim q,q\sim t$, then let $s_p=r_p\wedge r_q$ and
$s_r=r_q\wedge r_t$. Since $r_q^\downarrow\in\mathbb P_{bad}$,
$s_p\not\perp s_r$, so $r_p\not\perp r_t$ and thus
$p\sim t$. Thus $\sim$ is an equivalence relation.
If $\mathbb P_{bad}/\sim$ is infinite, we may choose $p_i\in\mathbb P_{bad}$
such that $p_i\not\sim p_j$ for $i<j<\omega$. Thus
$r_{p_i}\perp r_{p_j}$ for $i<j<\omega$, giving us
an infinite antichain $\{r_{p_i}:i<\omega\}$.
Otherwise $|\mathbb P_{bad}/\sim|=n<\omega$, and choose an antichain
$\{p_i:i\leq n\}$ in $\mathbb P$. If $\{p_i:i<n\}\subseteq\mathbb P_{bad}$,
$p_i\perp p_j$ implies $r_{p_i}\perp r_{p_j}$
and $p_i\not\sim p_j$ for all
$i\leq n$. Thus $\mathbb P_{bad}=\bigcup_{i<n}\overset{\sim}{p_i}$,
and $p_n\not\in\mathbb P_{bad}$.
So we've found $b_0\in\mathbb P\setminus\mathbb P_{bad}$.
Given $b_n\in\mathbb P\setminus\mathbb P_{bad}$, we may choose
$a_n,b_{n+1}\leq b_n$ such that $a_n\perp b_{n+1}$.
Thus by construction, $a_n\perp a_{m+1}$ for all
$n\leq m<\omega$. Therefore
$\{a_n:n<\omega\}$ is an antichain.
[0]: W.  W.  Comfort  and  S.  Negrepontis. Chain  Conditions  in  Topology.   Cambridge  Tracts  in Mathematics. Cambridge University Press, 1982.


Sequent calculus, how to prove double negation introduction and conjunction

Question: I want to prove double negation introduction in sequent calculus using the most basic rule set.
That is what I want to prove: from the sequent $$\Gamma \rightarrow\Phi,$$
the sequent
$$\Gamma \rightarrow\neg\neg\Phi$$
is derivable.
How is this accomplished? It's seems much more difficult than in natural deduction. I think I may be able to prove the conjunction myself once I have this result.
Comment: Do you have the negation sign as primitive or it is defined in terms of $\supset$ and $\bot$ ?
Answer: With $\lnot$ as primitive, we have to use $\text {L-} \lnot$ followed by : $\text {R-} \lnot$:
\begin{align}
{\cfrac{{\cfrac{\Gamma \to \Delta, A }{\lnot A, \Gamma \to \Delta} \text {L-} \lnot}}{\Gamma \to \Delta, \lnot \lnot A }\text {R-} \lnot}
\end{align}
With $\lnot A$ defined as $A \supset \bot$, we have to use $\text {L-} \supset$ followed by : $\text {R-} \supset$:
\begin{align}
{\cfrac{\cfrac{{\cfrac{\Gamma \to \Delta, A \quad \quad \bot, \Gamma \to \Delta }{A \supset \bot, \Gamma \to \Delta} \text {L-} \supset}}{A \supset \bot, \Gamma \to \Delta, \bot}\text {R-W}}{\Gamma \to \Delta, (A \supset \bot) \supset \bot }\text {R-} \supset}
\end{align}
Note : the top-right sequent is:
\begin{align}
{\cfrac{ }{\bot, \Gamma \to \Delta} \text {L-} \bot}
\end{align}


List of likelihood-based classification techniques

Question: This is a basic statistical pattern recognition question.
I'm aware of LDA classification, Naive Bayes Classification techniques which give output as a likelihood (of data belonging to a certain class).
What are other likelihood-based supervised classification techniques?
Can all of the supervised classification techniques be expressed as likelihood or likelihood ratio in order to classify given data?
Can anyone point me to articles of notes which can help me out?
Comment: This should possibly become a community wiki.
Answer: It think this has potential to become an interesting list. Maybe we should all either explain or reference how the methods use a likelihood approach.
A list of additional likelihood classification procedures:

GLMs for binomial or multinomial data (Logistic Regression, Probit Regression, other Link Functions) McCullagh & Nelder and their regularized extensions Hastie et al.
The equivalent GAMs Hastie & Tibshirani
Certain classification and model trees can also be seen as likelihood-based models (Su et al, Loh et al's work , Zeileis et al , Rusch & Zeileis, etc.)
Certain types of Boosting may also be seen as likelihood-based Lebanon & Lafferty, Friedman et al.
From what I know, Bayesian approaches nearly always use the likelihood as part of the model, hence they are always likelihood based.

If a negative (log)-likelihood for a classification problem is used as the loss function that is minimized to train the model and then predictions from this model are made, I suppose any classification approach following this counts as likelihood-based.
Comment: Regression is used to explain how a response is related to a set of covariates It puts a smooth function through the data to separate signal from noise.  It also allows interpolation between data points (possibly also extrapolation, although extrapolation far outside the range of the data is dangerous).  Generally regression is different from classification.  However in the case of probit regression and logistic regression prediction may be viewed as classification.  So including them in the list may be okay.
Answer: LDA and QDA are likelihood based approaches based on respectively both class-conditional densities are multivariate normal with the same covariance matrix and both class-conditional densities are multivariate normal with different covariance matrices.  In a broad sense kernel discrimination can be considered likelihood based. because if you assumed that the estimated densities were the actual class conditional densities the decision boundary could be considered based on likelihood ratios.  If you put a prior distribution on the class associated with a given feature vector you can get a Bayes rule by any of these approaches under the respective model assumptions.  I like the 1972 book "Pattern Classification and Scene Analysis" by Duda and Hart for a clear description of this.  There is a new addition of this book that came out a few years ago with a third author (Stork, I think).


Is modulo slow in Java?

Question: I've been looking at the implementation of ThreadLocal in the JDK, out of curiosity, and I found this :
/**
 * Increment i modulo len.
 */
 private static int nextIndex(int i, int len) {
     return ((i + 1 < len) ? i + 1 : 0);
 }

It looks fairly obvious that this could be implemented with a simple return (i + 1) % len, but I think these guys know their stuff. Any idea why they did this ?
This code is highly oriented towards performance, with a custom map for holding thread-local mappings, weak references to help the GC being clever and so on, so I guess this is a matter of performance. Is modulo slow in Java ?
Comment: I am not sure how you think that both codes are equal. One will return 0 if you exceed len, the other will return a number between 0-len-1 no matter what number you provide.
Comment: @Dici sorry, I've removed my comment, there was another one explaining that java does not generate the same code as assembly.
Comment: my feeling is that for % there may be some "worst case scenarios" where the interpreter can't choose a reasonable way to do the calculation (see http://blog.teamleadnet.com/2012/07/faster-division-and-modulo-operation.html)
Comment: @Leo looks very plausible, a comment does say that the length MUST be a power of two, and there is indeed a dynamically resized array
Comment: @Leo I just did, I hadn't had the chance to read it yet. Thanks to you too
Comment: great question. one more place that modulo is replaced is when computing a bucket for `HashMap` internally; but there the main problem is that it's done against `hashCode`, that might be negative; and doing mod against a negative value will give you a negative value, and a negative index bucket...
Comment: Well, this code will work even if `len` is `Integer.MAX_VALUE`. Maybe this is why they did it like this?
Comment: Well not really because `i + 1` could overflow. I don't think it is any safer than a modulo although I don't know how the modulo behaves with overflows
Comment: Yes, precisely. Which means it will be less than `len`!
Comment: Ok I take your point, but this `len` is the number of buckets. They would run out of memory way before reaching the overflow
Comment: You are begging the question: was this done for performance? There's no evidence of that.
Comment: It is [this code](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/ThreadLocal.java#312) you are referring to, right?
Comment: @Leo I have scanned this question a bit to quickly, let me take another look
Comment: @fge I'm looking at the Oracle JDK sources in Idea, but I think that yes, it is the same code as the Open JDK. I can see no obvious difference in the code
Comment: @Leo no problem, I did not even see that answer so I beat you at reading too fast
Comment: @erickson no evidence, but this is the likliest reason isn't it ?
Comment: anyway, upvoted, since it would be great to have an authoritative answer for this :-) (anyway -- http://stackoverflow.com/questions/15754323/how-does-the-modulus-operator-in-java-function)
Comment: @Leo I'd be happy to accept your answer if you compile your discoveries. I actually feel bad about having jumped on SO without Googling more than 1 minute.  It's just that I like asking "unusual" questions here, answers are always interesting
Comment: Yes it's slow, there is a lot on Google about this.
Comment: Is it guaranteed that i comes always less then len ? (since for >= values the two expressions will return different results)
Comment: @Bax I did not read all the code but I assume it is, given the comment. Same answer to you, Ata.
Comment: @Bax sounds like `i` is always a valid index in the array containing the buckets (array of size `len`).
Comment: it looks like @apangin provided a reasonable answer. You can accept his/hers :-)
Answer: % is avoided for performance reasons in this example.
div/rem operations are slower even on CPU architecture level; not only in Java. For example, minimum latency of idiv instruction on Haswell is about 10 cycles, but only 1 cycle for add.
Let's benchmark using JMH.
import org.openjdk.jmh.annotations.*;

@State(Scope.Benchmark)
public class Modulo {
    @Param("16")
    int len;

    int i;

    @Benchmark
    public int baseline() {
        return i;
    }

    @Benchmark
    public int conditional() {
        return i = (i + 1 < len) ? i + 1 : 0;
    }

    @Benchmark
    public int mask() {
        return i = (i + 1) & (len - 1);
    }

    @Benchmark
    public int mod() {
        return i = (i + 1) % len;
    }
}

Results:
Benchmark           (len)  Mode  Cnt  Score   Error  Units
Modulo.baseline        16  avgt   10  2,951 ± 0,038  ns/op
Modulo.conditional     16  avgt   10  3,517 ± 0,051  ns/op
Modulo.mask            16  avgt   10  3,765 ± 0,016  ns/op
Modulo.mod             16  avgt   10  9,125 ± 0,023  ns/op

As you can see, using % is ~2.6x slower than a conditional expression. JIT cannot optimize this automatically in the discussed ThreadLocal code, because the divisor (table.length) is variable.
Comment: I'll look at your answer in details tomorrow, I need some sleep right now :D Thanks !
Answer: mod is not that slow in Java. It's implemented as the byte code instructions irem and frem for Integers and Floats respectively. The JIT does a good job of optimizing this.
In my benchmarks (see article), irem calls in JDK 1.8 take about 1 nanosecond. That's pretty quick. frem calls are about 3x slower, so use integers where possible.
If you're using Natural Integers (e.g. array indexing) and a power of 2 Divisor (e.g. 8 thread locals), then you can use a bit twiddling trick to get a 20% performance gain.

Is modulo slow in Java?


Create nodes if optional match returns null

Question: All the nodes I'll be using have unique constraints associated to them.
I tried to create new nodes like this (I thought about MERGE so it would not create the node if it already exists):
MERGE (n:url{url_addr:'test.com'})-[:FROM_DEVICE]->(o:device{mode:'iphone'})

And the node already exists it returns this error:
Node(21) already exists with label `url` and property `url_addr` = 'test.com'

It was obvious, since I didn't match them beforehand, so I did it in order to just pass the results properties:
MATCH (n:url{url_addr:'test.com'}), (o:device{mode:'iphone'})
MERGE (n)-[:FROM_DEVICE]->(o)

But in case any of this records do not exist new nodes will not be created.
So I thought about using OPTIONAL MATCH, since it would replace any non-existent value with null, but this means it would also replace the properties I passed with null values.
OPTIONAL MATCH (n:url{url_addr:'test.com'}) RETURN n
MERGE n-[:FROM_DEVICE]->(o:device{mode:'iphone'})

It returned this error:
Failed to create relationship `  REL87(d65d30eb-65f5-4460-b166-15996622cf1b)`, node `n` is missing. If you prefer to simply ignore rows where a relationship node is missing, set 'cypher.lenient_create_relationship = true' in neo4j.conf

So I thought about using CASE statements in order to first verify if the match would return null and then create the node to define the relationship, but things didn't work well:
OPTIONAL MATCH (n:url{url_addr:'test.com'})
WITH n as test
RETURN CASE
    WHEN test IS NULL THEN CREATE (:url{url_addr:'test.com'})
END

This happens:
Invalid input '{': expected
  "!="
  "%"
  ")"
  "*"
  "+"
  ","
  "-"
  "."
  "/"
  ":"
  "<"
  "<="
  "<>"
  "="
  "=~"
  ">"
  ">="
  "AND"
  "CONTAINS"
  "ENDS"
  "IN"
  "IS"
  "OR"
  "STARTS"
  "XOR"
  "["
  "^" (line 4, column 51 (offset: 135))
"        WHEN CASE IS NULL THEN CREATE (a:cookie_id{url:'test.com'})"
                                                   ^

Removing the properties of the node does not help.
Answer: MERGE always matches or creates the whole pattern. So, you might want to split up the MERGE clause, so that it matches or creates each node and the relationship:
MERGE (n:url {url_addr:'test.com'}) // match or create url node
MERGE (o:device {mode:'iphone'})    // match or create device node
MERGE (n)-[:FROM_DEVICE]->(o)       // match or create relationship

You might also want to have a look at conditional cypher execution with APOC procedures:
https://neo4j.com/labs/apoc/4.2/cypher-execution/conditionals/


Statistics: How to use Poisson to determine if event would not occur?

Question: This is a follow up to my previous question
disk manufacturer averages 0.2 missing pulses per disk. let X denote # of missing pulses
I am using poisson distribution
I'm having trouble determining the probability that a disk would have no missing pulses
So I did P(X=0)$e^{-0.2}(0.2)^0 /0!$ = $0.81873$
So I think that is the probability of getting missing pulses so I did complement
$1-0.81873 = 0.18127$
another question asks me to determine the probability that next 2 disks will contain no missing pulses.
So I did 0.18127 * 0.18127 = 0.03285
Can anyone confirm if this is right?
Answer: Let $X$ be the number of missing pulses. As you wrote, $\Pr(X=0)=e^{-0.2}$. That is the answer, no further manipulation needed.
If $Y$ is the number of missing pulses on $2$ disks, then $Y$ has Poisson distribution with parameter $0.4$. So the probability of no missing pulses is $e^{-0.4}$.
For this particular problem, we could instead square the first answer, but it is important to get practice with the Poisson, to be able to handle more general problems.


How to AUTO-EMPTY fields in a form AFTER submission

Question: I'm using this ( http://www.j-download.com/demo/demo-j-dcontact.html ) form on my website. It works great but the only issue is that AFTER successfull submission ALL the fields are STILL FILLED.
Can somebody please help me with this, I would like so the fields are auto-emptied after successfull submission..
Thanks.
Comment: Use the reset() function; something like this after the form submits: `$('#myForm').reset();`. You could also loop through all the given form elements with `$('#myForm').find(':input').each();`.
Comment: Thanks everybody. I've done it now by adding

    `document.getElementById("form").reset();`

to the template .php file of the form AFTER form submission
Answer: can be acheived with jquery or raw javascript after the post and it is explained here
This script is from that site (in case the link dies)
function clear_form_elements(ele) {
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}

you could also reset() the form
Comment: Thanks everybody. I've done it now by adding

    `document.getElementById("form").reset();`

to the template .php file of the form AFTER form submission


Concave envelope: equivalent definitions

Question: Let $f: X\to \mathbb{R}$ be bounded. Consider the following definitions:
Definition 1: $f^{*}(x)=\inf\{h(x): h\in \mathbb{R}^{X} \;\;\text{affine and}\;\; h\ge f\}$.
Definition 2: $f^{**}(x)=\inf\{h(x): h\in \mathbb{R}^{X} \;\;\text{affine, continuous and}\;\; h\ge f\}$.
Definition 3: $f^{***}(x)=\min\{h(x): h\in \mathbb{R}^{X} \;\;\text{concave and}\;\; h\ge f\}$.
All were extracted from different textbooks or articles. They refer to the concave envelope of $f$.
Are they all equivalent?
I can see how they are related, but, for instance, for $g$ strictly concave with $g(x)\ge f^{***}(x)$, $f^{***}(x)=g(x)$ but what about $f^{*}(x)$? Why is continuity required in definition 2?
Comment: In finite dimensions, def 2 and def 1 are equivalent because all affine functions are continuous there.
Comment: Another thing to note is that the pointwise infimum of affine functions is again concave and $f^*$ is a function which is concave and for which $f^*\geq f$. Or, more clearly, there is the inclusion $\{h(x):h\in\mathbb{R}^X\mbox{ affine and }h\geq f\}\subset \{h(x):h\in\mathbb{R}^X\mbox{ concave and }h\geq f\}$.
Comment: Right! Concave functions are continuous as well, so implicitly in def 1 and def 3 there might be the hypothesis of X finite.
Comment: May I know where those definitions come from?
Answer: Posed as it is posed, for general $X$ (hence possibly infinite dimensional) it is clear that there may exist linear unbounded functions, making the two definitions nonequivalent.
Note also that for general $X$ we may not even be sure the $\min$ exists in definition $3$.
Knowing what is $X$ (a generic set, a set endowed with a preference relation and maybe an order topology, a finite dimensional space,...) is fundamental for the question, since in total generality it is false.
Comment: I think you can consider X to be a subset of $\mathbb{R}^{n}$, with $n$ finite. More generally, $X$ is a vector is a real topological vector space.


Closed Form Summation Example

Question:
$$ \sum_{i=1}^n (ai +b) $$
Let $n \geq 1$ be an integer, and let $a,b > 0$ be positive real numbers. Find a closed form for the following expression. In other words you are to eliminate the summation and write the expression as a function of a b and n. Explain how you computed your answer

I have tried to complete the exercise, but am not receiving the proper answer. Can anyone please provide English based understanding? I am trying to parse it out as much as possible for better understanding

first term = 1
Last term = an + b
constant difference = b
$$ i = (n(1 + (an + b))b) /2  $$
Therefore the above summation =
$$ a * ((n(1+(an+b))b)/2) + b  =$$
$$ a(n(1+an+b)b)/2+b = $$
$$ a((n+an^2 +bn)b)/2+b = $$
$$ a(bn +abn^2 +b^2n)/2 + b  $$
This appears to be failing.
when a = 1 b=1 and n =2, summation should = 3, the formula above gives 5
What am i missing here? everything seems right I feel like I am missing something simple.
Also, can someone provide help on proof by induction, in as simple terms as possible?
Thanks in advance
Answer: The constant difference between each term is not $b$, and the first term is not $1$ (but rather $a+b$).
For clarity, I would recommend doing the following. Split the sum into two parts:
$$\sum_{i=1}^n ai + \sum_{i=1}^n b$$
This is equivalent to:
$$[a + 2a + 3a + \dots + (n-1)a + na] + [b + b + b + \dots + b +b]$$
The first sum is an arithmetic progression (A.P) where the difference between each term is $a$, and we have $n$ terms. You seem to know how to do this? The second sum is just the summing of $n$ terms of $b$, which is $n \cdot b$.
Comment: Hmm, wouldn't it be clearer to write $ \displaystyle a \cdot (\sum_{i=1}^n i) $ and $\displaystyle b \cdot (\sum_{i=1}^n 1)$ ? The closed-forms for the sum-expressions should even easier jump out from the memory...
Comment: s the following correct now? Using the split summations above for clarity if we factor out a, first term = 1, last term = n, difference = 1..
$$a((n(1+n)1)/2)+nb $$
$$a((n+n^2)/2)+nb$$

Closed Form Summation Example


Understanding Proposition 2.2 in Jun Shao's Mathematical Statistics

Question: I am reading Jun Shao's "Mathematical Statistics" second edition https://link.springer.com/book/10.1007/b97553.
I need help understanding the statement and the proof of proposition 2.2 on page 117:

In the statement, $\mathbb{A}$ is the space of actions for our statistical decision and $A$ is a measurable subset of $\mathbb{A}$. The detailed setting can be found on the first paragraph of page 113 section 2.3.1. The definition of (randomized) decision rule can be found on the two paragraphs right above equation (2.22).
Let us write $(\Omega, M, \mu)$ for the probability space where $X$ is defined.
I have the following questions:

How exactly is $\delta_1$ defined? What is the right way of reading (2.23)? I find myself always having trouble with statements like "random variable given $T=t$". By the definition of decision rule, $\delta_0(\cdot, A)$ is a Borel function when $A \subseteq \mathbb{A}$ is a given measurable set. Since $X$ is a random variable defined $(\Omega, M, \mu)$ , we can view $Y=\delta_0(X, A)$ also as a random variable on $(\Omega, M, \mu)$. Does $E[\delta_0(X, A) \mid T=t]$ mean the conditional expectation of random variable $Y$ given the $\sigma$-algebra generated by the set $\{ T(X)=t \}$?
If what I claim in 1 is correct, then how do I understand the first sentence in the proof? By definition of decision rule, we have to show that $\delta_1(\cdot,A)$ is a Borel function for every given $A$ and $\delta_1(t, \cdot)$ is a probability measure on $\mathbb{A}$. I can prove the latter but I am not sure about the first. It is not clear to me why this is related to sufficiency of $T$ either.
If what I claim in 1 is correct, then how do I show the second equality? Let $C \in M$ be a $T(X)$ measurable set. It seems that I need to show:

\begin{equation*}
\int_C \int_{\mathbb{A}} L(P,a) d\delta_1(x,a)d\mu(x) = \int_C \int_{\mathbb{A}} L(P,a) d\delta_0(x,a) d\mu(x)
\end{equation*}
It is not clear to me why this holds.

What is the importance of suddenly assuming $\mathbb{A}\subseteq \mathbb{R}^k$? Where is it used during the proof?
Answer:
$\delta_1(t, A)$ is defined to be any function which is measurable in $t$ for each $A$, and satisfies $\delta_1(T, A) = \mathbb E[\delta_0(X, A) \mid T]$ almost surely for each $A$. (We know such a function exists by the Doob-Dynkin lemma.)

The point about measurability we've covered in 1. The point about sufficiency is saying that you can compute the conditional expectation in $\delta_1$ without using the unknown $P$; the conditional expectation depends only on the distribution of $X \mid T$, which is known as $T$ is sufficient.

When the loss function $L(P, a) = 1_A(a)$ for some measurable set $A \in \mathcal B^k$, your statement is equivalent to
$$\mathbb E[1_C\delta_1(T, A)] = \mathbb E[1_C\delta_0(X, A)],$$
which follows from the definition of conditional expectation. This extends to losses which are $\mathcal B^k$-simple for fixed $P$ by linearity, and then to losses which are integrable for fixed $P$ by a standard limiting argument.

I don't think we need $\mathbb A \subseteq \mathbb R^k$ here;  maybe the author assumed it to simplify the proof in 3?
Comment: Thank you. This clarifies all.
Comment: I believe the reason we take $\mathbb A \subset \mathbb R^k$ is for regularity, see for example, https://en.wikipedia.org/wiki/Regular_conditional_probability.


Buscar por palavra levando em consideração relação entre 3 tabelas

Question: Estou montando um sistema de busca para um guia empresarial e na hora de buscar só estou conseguindo buscar uma palavra especifica em uma tabela, por exemmplo: pizza. Mas o ideal seria ver se existe uma empresa chamada "pizza" ou se alguma que não tenha "pizza" no nome faz parte da categoria "pizzaria", por exemplo, e trazer ela.
Essa é a estrutura que estou usando para relacionar uma empresa com as categorias:
| empresa    |
|------------|
| id_empresa |
| nome       |

| categoria    |
|--------------|
| id_categoria |
| nome         |

| categoria_empresa    |
|----------------------|
| id_categoria_empresa |
| id_empresa           |
| id_categoria         |

Essa é a busca que estou fazendo:
SELECT * FROM empresa WHERE nome LIKE %"pizza"%

Comment: Dê uma olhada no [tour] você pode votar em tudo no site, além de aceitar uma resposta em perguntas sias.
Answer: Você pode colocar as condições todas no WHERE:
SELECT
   empresa.nome, categoria.nome
FROM
   categoria_empresa
   INNER JOIN categoria ON categoria.id_categoria = categoria_empresa.id_categoria
   INNER JOIN empresa   ON empresa.id_empresa     = categoria_empresa.id_empresa
WHERE
   categoria.nome LIKE "%pizza%" OR empresa.nome LIKE "%pizza%"

Ps: o importante aqui é o WHERE conter todas as condições válidas. O JOIN mais adequado só depende de como vai lidar com as duplicidades.
Retornando apenas uma linha por empresa:
Se for para retornar apenas uma empresa para várias categorias, pode fazer desta forma:
SELECT
   empresa.nome, GROUP_CONCAT( categoria.nome ) AS categorias
FROM
   empresa
   LEFT JOIN categoria_empresa ON empresa.id_empresa     = categoria_empresa.id_empresa
   LEFT JOIN categoria         ON categoria.id_categoria = categoria_empresa.id_categoria
WHERE
   categoria.nome LIKE "%pizza%" OR empresa.nome LIKE "%pizza%"
GROUP BY
   empresa.id_empresa

Neste caso, pode remover o GROUP_CONCAT( categoria.nome ) AS categorias. Está aí apenas para complementar os resultados e facilitar o debug.
E para contar os registros, para paginação:
SELECT
   COUNT( DISTINCT empresa.id_empresa )
FROM
   empresa
   LEFT JOIN categoria_empresa ON empresa.id_empresa     = categoria_empresa.id_empresa
   LEFT JOIN categoria         ON categoria.id_categoria = categoria_empresa.id_categoria
WHERE
   categoria.nome LIKE "%pizza%" OR empresa.nome LIKE "%pizza%"

Comment: Funcionou perfeitamente muito obrigado pela paciência!


Scanning, Checking, Converting, Copying values ... How to ? -- C --

Question: Its been a while now and im still trying to get a certain code to work. I asked some question about different commands etc. before, but now I hope this is the final one (combining all questions in one code).
I basically want to :
*Scan an input (should be character ? )
*Check if its a number
*If not, return error
*Convert that character into a float number
*Copy the value to another variable ( I called it imp here)
Here is what I came up with :
EDITED CODE*
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
main(){
  int digits;
  float imp=0;
  char* alpha;
  do{

    printf("Enter input\n\n");
    scanf("\n%c",alpha);
    digits=isdigit(alpha);
      if(digits==0){
         printf("error\n\n");
      }
     imp=atof(alpha);
  }while(digits==0);

}

The problem is this code does not work at all ... It gives me that atof must be of a const char and whenever I try changing it around, it just keeps failing. I am frustrated and forced to ask here, because I believe I have tried alot and I keep failing, but I wont be relieved until I get it to work xD So I really need your help guys.
Please tell me why isnt this code working, what am I doing wrong ? I am still learning C and really appreciate your help :)
EDIT
Error given atm is :
Argument no 1 of 'isdigit' must be of type 'const int', not '<ptr>char'
EDIT
This code compiles fine, but crashes when an input is entered.
  #include<stdio.h>
  #include<stdlib.h>
  #include<ctype.h>
  main(){
  int digits;
  float imp=0;
  char* alpha=0;
  do{

   printf("Enter input\n\n");
   scanf("\n%s",alpha);
   digits=(isdigit(alpha[0]));
   imp=atof(alpha);
  }while(digits==0);
 }

Comment: Thanks for making it look better sbi, I dont have the editing tools...Not sure why
Answer: You probably need to use %s instead of %c and to put it in char array (char*). You also probably get an error that you need to use const char* and not const char.
You don't want to read just one character - you want to read an entire string...
EDIT:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
main(){
  int digits,i;
  float imp=0;
  char* alpha = malloc(100); /* 100 is for example */
  do{
    printf("Enter input\n\n");
    scanf("\n%s",&alpha);
    for (i = 0; i != 100; ++i)
    {
      if (alpha[i] == '\0')
        break;
      if (!isdigit(alpha[i]))
      {
        printf("error\n\n");
        return ...;
      }
    }
     imp=atof(alpha);
  }while(true);
}

Comment: Thanks for replying, I change %s to %c, got less errors. Now I have a new error about digits `Argument no 1 of 'isdigit' must be of type 'const int', not 'char'`
Comment: I've added an example for something closer to what you probably want.
Comment: Thank you very much for that structure .. I have made mine to compile with no errors .. but it crashes once I input anything, can you please check the edited code ? (see question)
Comment: You need to allocate memory for alpha and give α (address of alpha) instead of alpha when using scanf().
Comment: Thanks for replying .. It says `An object of type 'void' cannot be assigned to an object of type 'char'` ...
Answer: Why not have scanf do the atof conversion for you?
 #include <stdio.h>
 int main()
 {
    float imp=0;
    while (1)
    {
       printf("Enter input\n\n");
       if (scanf("%f", &imp) < 1) break;
    }
    return 0;
}

Your most recent example is failing because alpha is a NULL pointer.  Declare it as char alpha[40]; to allocate space for it.  You'll probably want to use %40s in your format string to prevent scanf from overflowing alpha.
Also, use strtod instead of atof and you'll know whether the conversion was successful (better than your method of using isdigit which will fail on a negative integer).
Comment: Ahhh thank you very much !!! Your explanation got my code working !!  I had to remove the NULL array as you mentioned, and prevent scanf from overflowing it !!! Legend, thank you:) :)


Building Lydian chords

Question: I am learning about chords and their relationships to major scale modes in jazz. As I understand it, each mode allows a different chord to be built by choosing the 1st, 3rd, 5th, 7th of the scale. I wanted to do this with the Lydian mode in C (4th mode of G major) and I got C E G B for the chord.
However, I looked here and it also includes the F# and calls the chord Cmaj7#11. What is the difference between my chord and this Cmaj7#11 (except for the obviously added F#)? What sort of chord have I created and is it really a Lydian chord?
Comment: Jazz chords are not always built `1 3 5 7`. This chord you are asking about is usually has `1 3 4 7`. Fun fact - the tune Lost Woods from Zelda The Ocarina of Time is entirely built on the Lydian mode. Cool stuff
Answer: whether a chord is part of a mode depends on the surrounding chords. In other words, part of what determines a chord quality is not the chord itself, but the context. If you play just a Cmaj7 all by itself, there's no way to know whether it is in the key of G or the key of C, so therefore you don't know which mode it is. But if you play other chords in the key of G, before and afterwards, then you know it is part of that key and therefore a Lydian sound.
Likewise, if you include more context by including the #11 in the chord itself, you're giving it a Lydian sound.
So, the bottom line is, it all depends on context and how much "information" you provide with the chord.
Comment: Since Gmaj.  contains the same notes as C Lydian, the chords used will basically be the same. No clues there then.The OP is asking about a C Lydian chord, which is different from a chord out of the C Lydian mode.
Comment: @Tim. Clarify your comment. When you say a chord "out of the C lydian mode" give examples because it's not clear what you're referring to.
Comment: -chords that can be made out of the Lydian mode are going to be the same as chords from its parent major key. E.g. C Lydian chords, as triads ,will be C,D, Em, Fo, G, Am and Bm, as they all emanate from G maj., the parent key which produced C Lydian. Tetrachords are noted in my answer below, but the Lydian chord itself is a 5 note chord which is spelled 1-3-5-7-#11.Hope this helps.
Comment: @MichaelMartinez *User Tim meant "F♯°", not "F°".*
Answer: The tetrachords made from the C Lydian mode (using the 7 notes of Gmaj.) are going to be the same 7 chords as found in Gmaj. itself - Cmaj.7, D7., Em7., F#m7b5, Gmaj.7, Am7., Bm7. Since the mode uses the same notes as the Gmaj. scale, the chords will follow.
The Lydian chord of C (C Lydian chord !) is a bigger chord in that it contains 5 notes: C-E-G-B and F#. This is Cmaj.7#11.It may be co-incidental that it uses the 'Lydian' name, but it's confusing.  After doing some homework, it would appear there is no Dorian chord, no Mixolydian chord, etc.
Answer: Jazz Chords are built using the characteristic notes of each mode of every scale. The chord types can be quite complex (like dom7alt) but the basic alterations for the major scale, in relation to the tonic, are as follows:
I:(Ionian or major) no alterations, since all alterations are based of the tonic major chord with its regular 1st, 3rd, 5th, and 7th
ii:(dorian) the same as the natural minor mode (b3,b6,b7) except for the sixth, which is not flatted
iii: (phrygian) the same as the natural minor mode except for its for b2
IV: (Lydian) contains a #4
V: (Mixolydian)
vi:(natural minor mode, or aeolian) b3,b6,b7
vii: (diminished mode, or locrian) same as phrygian, with the addition of the b5, or diminished fifth
One possible cause of your misunderstanding might be the '#11'. a sharped 11th is the same as a sharped fourth, only it is up an octave. Chord notation, when describing alterations, uses the original degree within the seven-note scale. When a specially labeled note, like the sharped fourth in your case, is one or more octaves higher then the bass note, the number is increased by seven to indicate the higher octave. So basically, a sharped fourth is the same as a sharped 11th, except the sharped 11th is an octave higher.

Building Lydian chords


Prevent nginx to execute images as PHP script

Question: My website has been compromized by an user who uploaded an image with PHP code in it. This code allows to upload files, and he uploaded a malicious PHP script.
He was able to call his "image-php" with a GET on this URL :
http://mypwnedwebsite.com/image.jpg/.php

How can I configure nginx to prevent this behaviour? I mean, with a simple /.php, it acts as everything was PHP, which is wrong from my point of view.
I actually have a "classical" nginx 1.6.2 configuration, and I think this part is the most relevant :
location ~ \.php$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 120;
        include /etc/nginx/fastcgi_params;
    }

Comment: See [this article](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#passing-uncontrolled-requests-to-php). You are missing a `try_files $uri =404;` statement.
Answer: This should work.
location ~ \.php$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 120;
        include /etc/nginx/fastcgi_params;
        try_files $uri $uri/ /404.html;
    }

It tries to find $uri, if not a folder, if not 404 error is thrown.
Comment: Thank you, it actually works. Do I have to put `try_files $uri $uri/ /404.html` in every `location` block on every website, or is there a way to do this globally?
Comment: https://www.nginx.com/blog/creating-nginx-rewrite-rules/
The try_files directive

Like the return and rewrite directives, the try_files directive is placed in a server or location block. As parameters, it takes a list of one or more files and directories and a final URI:

try_files file … uri;


specific examples of algebraic closure on finite field

Question: I want to confirm my understanding of algebraic closure for finite fields.
What sorts of elements do the algebraic closures $\overline {\mathbb{F}_2}$, $\overline {\mathbb{F}_3}$, $\overline {\mathbb{F}_7}$, and $\overline {\mathbb{F}_{25}}$ contain?
This answer seems most illuminating, specifically:

We can form a nested chain of extensions $$ E_1\subset
 E_2\subset\cdots \subset E_i\subset E_{i+1}\subset\cdots $$ of finite
  fields $E_i$ for all positive integers $i$ such that $E_1=\Bbb{F}_p$

Taking $p=3$, for example, such that $E_1 = \mathbb{F}_3 = \lbrace0, 1, 2\rbrace$
Then $E_2 =\Bbb{F}_{3^{2}} =\Bbb{F}_9 =\lbrace0, 1, 2, 3, 4, 5, 6, 7, 8\rbrace$
Indeed, $E_1\subset E_2$, but following this train of thought implies that the algebraic closure of $\Bbb{F}_p$ is just the positive integers, but if that were the case, surely one of the references I've read would have said so by now!
The only other way I've been able to interpret this is that $\overline {\Bbb{F}_3} = {0, 1, 2, 3, 9, 27, 81, ...}$ or similar.
Comment: @WillR $\alpha^2+\alpha+1=0$ won't give you $\Bbb{F}_9$ because
$$x^2+x+1=x^2-2x+1=(x-1)^2$$ is reducible. You could use $\alpha^2+1=0$ though. Viewed differently, there are no primitive roots of order $p$ in characteristic $p$ fields.
Comment: It is **not** true that $\mathbb{F}_{9}=\{0,1,\ldots,8\}.$ $\mathbb{F}_{9}$ is a field with $9$ elements, and the only way I know to describe it is as $$\{0,1,2,\alpha,1+\alpha,2+\alpha,2\alpha,1+2\alpha,2+2\alpha\},$$ where $3=0$ and $\alpha$ is an entirely new element that satisfies $\alpha^{2}+\alpha+1=0.$ I think you should review finite fields before studying algebraic closures of finite fields.
Comment: You can not identify any of the finite fields $\mathbb{F}_{p^n}$ with a subset of the integers $\mathbb{Z}$ in a fruithful way. I suppose that the origin of the confusion is that $\mathbb{F_p}$ can be regarded as a quotient of $\mathbb{Z}$ and by abuse of notation we call its elements $0,1,\ldots, p-1$. But the situation with $\mathbb{F}_{p^n}$, with $n>1$, is different because it is not a quotient of the integers and, in particular $\mathbb{F}_{p^n}\neq \mathbb{Z}_{p^n}=\mathbb{Z}/p^n\mathbb{Z}$.
Comment: @DustyPhillips: No. The elements of $\mathbb{F}_{27}$ are polynomials in a new element $\beta$ which satisfies $\beta^{3}+2\beta^{2}+1=0.$ The key attribute which these elements $\alpha\in\mathbb{F}_{9}$ and $\beta\in\mathbb{F}_{27}$ have in common is that they are roots of polynomials ($t^{2}+t+1$ and $t^{3}+2t^{2}+1,$ respectively) which are **irreducible** over $\mathbb{F}_{3}.$ Since $\mathbb{F}_{3}$ is a field, $\mathbb{F}_{3}[t]$ is a PID and therefore, if $f(t)\in\mathbb{F}_{3}$ is irreducible, then $\mathbb{F}_{3}/\langle f(t)\rangle$ is a field.
Comment: @DanteGrevino Ah, that's exactly my confusion, I thought the notations were interchangeable. Thanks.
Comment: If I combine WillR's notation, with dan_fuala's description, I reason as follows, is this correct?:

 *  ${F}_{3^{1}} = {0, 1, 2}$  with 3 elements
 *  ${F}_{3^{2}} = {0, 1, 2, a, a+1, a+2, 2a, 2a+1, 2a+2}$  with 9 elements
 *  ${F}_{3^{3}} = {0, 1, 2 .... 2a+1, 2a+2, b, b+1, b+2, b+a, b+a+1, ... b+2a+2, 2b, 2b+1, 2b+2, 2b+a, 2b+a+1,...,2b + 2a+2}$ with 81 elements
${F}_{3^{4}}$ with $81^2$ elements including a new element c?
 *  and so on, infinitely?
Comment: But this discussion is quickly getting off topic, so we shouldn't continue it further here. From the sounds of it you aren't familiar with ring and field theory in the depth required to be considering algebraic closures just yet. I recommend that you learn about finite fields from a suitable source. Perhaps look at Chapter 6 of [Paul Garrett's Abstract Algebra notes](http://www-users.math.umn.edu/~garrett/m/algebra/notes/Whole.pdf).
Comment: Oops! Small typo, I obviously meant $f(t)\in\mathbb{F}_{3}[t]$ above. Unfortunately it is too late for me to edit that now.
Comment: @JyrkiLahtonen I don't know why I thought $1$ was not a root, but of course it is. Thank you for correcting me.
Comment: Not much harm done, @WillR. Your main points are spot on.
Comment: @WillR $\mathbb{F}_9$ can also be viewed as $\{0, 1, -1, i, i + 1, i - 1, -i, -i + 1, -i - 1\}$
Comment: @Smiley1000 But that set is not closed under addition (or multiplication).
Comment: @WillR of course you have to consider it to wrap around modulo 3 (meaning $1 + 1 = -1$, therefore $i + i = -i$ and so on)
Answer: This is a possible way to have a picture of the algebraic closure of $\Bbb F_p$. Consider all natural numbers $1,2,3,4,5,6,7,8,9,10,11,12,13,\dots$ and arrange them in a tree with levels $0,1,2,3,\dots$ as follows.

In level $0$ we have the one and only $1$ .
In level $1$ we have all the prime numbers, so $2,3,5,7,11,13,\dots$ .
In level $2$ we have all natural numbers which are a product of two primes, so $4,6,9,10,\dots$ .
In level $3$ we have all natural numbers which are a product of three primes, so $8,12,\dots$ .

And so on. These are the vertices of the tree.
Now draw an edge / a connection (as arrow) $a\to b$ from one level to the next one if $a$ divides $b$.
It is hard to draw, but i will try.
<code>:::::::::::::::::::::::::::::::::::::::
         |/ |/
level 3: 8 12 ...
         |/|
level 2: 4 6 9 10 ...
         |/|/ /
level 1: 2 3 5 7 11 13 ...
         |/ / / / /
level 0: 1
</code>
Now imagine at each place $a$ placed the field
$$
\Bbb F_{p^a}
$$
with $p^a$ elements. Then for each edge $a\to b$ in the tree we have a field inclusion of the corresponding fields $\Bbb F_{p^a}\to \Bbb F_{p^b}$.
Consider now the union of all fields in all levels.
This is
$$
\bar {\Bbb F}_p\ .
$$
The way to construct it shows, that all statements "in it", that depend only on elements, and only on finitely many elements can be checked in a (sufficiently big) finite field.
Comment: @Jyrki Lahtonen i was trying to address only the question about "elements". There was no claim about a commutative diagram. The OP had problems (was my understanding) to figure how elements in $\Bbb F_3$, $\Bbb F_9$ sit in the algebraic closure $\bar{\Bbb F}_3$. And also for $\Bbb F_{25}$. So i tried to build some sentences that work in both cases. (No mention of commutative diagrams, directed sets...) But here as a comment. The tree level structure can be inductively used to get compatible inclusions $n/q\to n$, $q|n$, $q$ prime, thus a functor from the tree diagram category to finite fields.
Comment: I realize that you really want to describe a direct limit using as little machinery as possible. But you need to exercise a little bit of extra care. A potential problem is that the embeddings $\Bbb{F}_{p^a}\to\Bbb{F}_{p^b}$ (when $a\mid b$) are not unique. That itself is not a serious problem, but for the purposes of forming the direct limit (=the union) you need to worry about whether the composition of embeddings, say, $\Bbb{F}_{p^2}\to\Bbb{F}_{p^4}\to\Bbb{F}_{p^{12}}$ is equal to the composition $\Bbb{F}_{p^2}\to\Bbb{F}_{p^6}\to\Bbb{F}_{p^{12}}$.
Comment: The reason I used the nested sequence
$$\Bbb{F}_p\to\Bbb{F}_{p^{2!}}\to \Bbb{F}_{p^{3!}}\to\Bbb{F}_{p^{4!}}\to\cdots$$ in the linked thread was precisely to sidestep such issues.
Comment: Good. Agree with your main point (and understand your reasoning for cutting a few details).