Determining sample size for uniform distribution

Question: May you help me to decide what is the minimal sample size for a uniform distributed sample.
Assume that I've find the sample average, standard deviation and the $\alpha$.
Comment: The minimum sample size *for what purpose* ?
Comment: This question appears so closely related to the one at http://stats.stackexchange.com/q/3121 that perhaps the answers and comments there will be helpful here, too.
Answer: After discussion in the comments, I rewrite most of this answer...
It seems that the question can be interpreted as "how to find a confidence interval for the mean".
1. Without any assumption on the distribution
You sample independent $X_1, \dots, X_n$ from an unknown distribution
with finite mean and variance $\mu$ and $\sigma^2$. Using central
limit theorem, for $n$ big enough, you can approximate the
distribution of the sample mean $\overline X = {1\over n}
\sum_{i=1}^n X_i$ by a $\mathcal N \left( \mu, {1\over
n}\sigma^2\right)$.
Approximating $\sigma^2$ by
$\widehat{\sigma^2} = {n \over n-1} \left( {1\over n}
\sum_i X_i^2 - \left(\overline X\right)^2\right)$,
an asymptotic confidence interval of level $1-\alpha$ on $\mu$ is
given by
$$ \left[ \overline X - z_{1-\alpha/2} \sqrt{\widehat{\sigma^2} \over n} ;
\overline X + z_{1-\alpha/2} \sqrt{\widehat{\sigma^2} \over n} \right],$$
where $z_{1-\alpha/2}$ is a quantile of the standard normal distribution (eg for
$\alpha = 0.05$, $z_{0.975} = 1.96$).
This can be useful to compute a rough estimation of
the number of supplementary samples you may need to collect (just use
the current estimate of $\widehat{\sigma^2}$ to compute $n$).
2. Assuming that the distribution is uniform
Note that this assumption shouldn't be made without serious reasons.
So you sample independent $X_1, \dots, X_n$ ($n > 1$) from a uniform
distribution $\mathcal U (a,b)$, the bounds of the interval $[a,b]$
being unknown paramaters. The esperance is ${1\over 2}(a+b)$.
The maximum likelihood estimators of $a$ and $b$ are
$m = \min_i X_i$ and $M = \max_i X_i$. These are not independant so
we consider the density of $(m,M$) which is given by
$$\phi(u,v) = \left\{ \begin{array}{ll}
{n(n-1) \over (b-a)^n} (v-u)^{n-2} & \mbox{if}\ 0 \le u \le v \le 1 \\
0 & \mbox{else}
\end{array}\right.$$
From this it easy to get the density of ${1\over 2}(m+M)$ which is
very concentrated on ${1\over 2}(a+b)$ :
$$f(t) = \left\{ \begin{array}{ll}
 {n 2^{n-1} \over (b-a)^n} (t-a)^{n-1} & a\le t \le {1\over2}(a+b) \\
 {n 2^{n-1} \over (b-a)^n} (b-t)^{n-1} & {1\over2}(a+b)\le t \le b \\
\end{array}\right.$$
To get a confidence interval of the form
$$\left[ {1\over2}(m+M) - {\gamma\over 2}(M-m) ;{1\over2}(m+M) +
{\gamma\over 2}(M-m) \right],$$
we compute
$$\mathbb P \left( {1\over2}(m+M) - {\gamma\over 2}(M-m) \le {1\over2}(a+b) \le {1\over2}(m+M) + {\gamma\over 2}(M-m) \right)$$
which is simply equal to $1 -
{1\over (1+\gamma)^{n-1}}$,
so to get an CI of level $1-\alpha$ you just put $\gamma =
e^{-{1\over n-1}\log \alpha} - 1$. This procedure gives a suprisingly small
CI (or more precisely, its size decreases surprisingly fast as $n$ increases; for big $n$ it is $\sim -{1\over n}\log \alpha$).
3. Short illustration with R and $n = 50$, $n=1000$
It is plain that the CI obtained by the second method is much
shorter. Just an illustration of this:
<code>> n <- 50
> x <- runif(n)
> gamma <- exp(-log(0.05)/(n-1)) - 1
> m <- min(x); M <- max(x)
>  0.5*( (m+M) + c(-1,1)*gamma*(M-m) )
[1] 0.4799359 0.5404375
> mean(x) + c(-1,1)*1.96*sd(x)/sqrt(n)
[1] 0.4290694 0.5892294
</code>
And with $n=1000$
<code>> n <- 1000
...
>  0.5*( (m+M) + c(-1,1)*gamma*(M-m) )
[1] 0.4984559 0.5014571
> mean(x) + c(-1,1)*1.96*sd(x)/sqrt(n)
[1] 0.4805048 0.5161008
</code>
4. To understand the discussion in the comments, some elements from the
original answer
In the original answer, I foolhardishly proposed to use the CI
procedure based on central limit theorem, but using as an estimator
of the variance, which is ${1 \over 12} (b-a)^2$, the quantity
${1\over 12} (M-m)^2$. This was a curious mixture, I thank everybody for this stimulating discussion.
Comment: @whuber I think min Xi and max Xi are sufficient statistics for the uniform distribution. Wouldn't that mean that using other order statistics can't improve things?
Comment: Good point--I had overlooked that well-known fact.  I think what's nagging at me, though, is how unusual it is to *know* that the parent distribution truly is uniform.  I would question that assumption in this particular case (other questioners on SE sites have [mistakenly used "uniform" to refer to decidedly non-uniform distributions](http://stats.stackexchange.com/q/7542)).  In general, one should favor methods that are robust to failures of distributional assumptions.  The main worry about using the extremes of the sample is that they are as non-robust as one can get.
Comment: Ignoring for a moment the important caveats that @whuber mentions, it is curious that you use $\bar{X}$ for the mean estimate but a variance estimate using the order statistics. Using the order statistics as an estimate of the mean will yield convergence with variance $O(n^{-2})$ instead of the $O(n^{-1})$ from using the sample mean. Note also that $\hat{\sigma}_n^2 \uparrow \sigma^2 = (b-a)^2/12$, providing an easy proof of (a) almost sure convergence and (b) negative, but negligible, bias of the variance estimate given. It is straightforward (though perhaps inadvisable) to correct the bias.
Comment: This estimator of the standard deviation is unusual.  Is it superior (in any way) to the usual estimator based on the sample SD?
Comment: @whuber Hum, I was focalised on the keyword "uniform distribution"... I don't have time now (and won't have time in the next days) to dig this, but quickly: this estimator is biased, which is bad but it could be debiased; the great quality of this estimator is that its standard error is proportionnal to 1/n, whereas the classical estimator as a standard error proportionnal to $1/\sqrt n$!
Comment: @whuber If you want to see the full computation, I could do that – I never did it, I am confident it is tractable, and curious myself to see it. However I'll have to ask you to wait until 2012, I'll be off... Remind me that later in a way or an other.
Comment: The $1/n$ behavior derives from the assumption of uniformity.  I suspect similarly good asymptotic behavior may attach to the usual estimator when this assumption is made.  (Bias is not the dominant issue here, because it's a $O(1/n)$ error.)  I have also been wondering whether a linear combination of order statistics might do better than the usual SD.  It certainly cannot do any worse than your proposal, which is already in that form, and seems likely to do much better--but I haven't the time to do the calculations.
Comment: I generated 100,000 samples of size 100 from a $U(0,1)$ distribution.  The sample standard deviation had a standard deviation of 0.013; the MLE estimate had a standard deviation of 0.004.  The RMSE of the sample standard deviation was 0.013, the RMSE of the MLE estimate was 0.007 (up from the sd of 0.004 due to the bias.)  Hmmm, should I run into this problem, I'll estimate the standard deviation differently now!
Comment: I generated 50 000 samples from size 10 to ~ 1000 by steps of 50, and ploted (size, sd) to verify my assertions! The standard error of the usal estimator for σ is really in $1/\sqrt n$. The uniform distribution is the classical example where MLE for the bounds converge exceptionnaly fast. I hadn't this in mind when I used this estimator, I just wanted to use plainly the information that the distribution is uniform. I don't have time either, but this is a nice problem (the last proposition of whuber is very interesting).
Comment: @whuber I agree...
Comment: @cardinal you are totally right, I had the same thoughts. :)
Comment: @cardinal I hope you'll like it this way!
Comment: @whuber I think you might like to read the improved answer. This answer is far from being complete (I skipped most gory details, I didn't give the variances, or the CI you can get on the parameters a and b), but I think that for this question it's enough. However if some of you want to see more of it... I could write more.
Answer: The minimum sample size for estimating the sample average is 1. The minimum sample size for estimating the standard deviation is 2. I don't know about the minimum sample size for estimating $\alpha$, as I don't know what you intend $\alpha$ to signify.
Comment: Technically, the minimum sample size for estimating any parameter is *zero* ;-)
Comment: I want to find the connection between the sample size and the siginification of the sample average. (then I'll be able to decide what sample size is sufficent)
Comment: @nba What do you mean by *signification of the sample average*? Your question is far from being clear. First of all, is that a continuous or a discrete uniform distribution? On which set? Is it for example that you are sampling a uniform distribution on $[0,\theta]$, and you want to estimate $\theta$? Tell us what you want to do.
Comment: I was measure a data for a system. the number of measurment was N, I want to know if I need to Measure more. The purpose is to achieve a good estimation for the mean (the data best fited to a continious uniform distribution, by a calulation on the data).
Comment: @nba "Good" is relative, which is why you are receiving such cryptic responses.  The uncertainty in your estimate should shrink as sample size increases, and if you can quantify "good estimation" a bit then folks here can help you more.


(Socket) Conexão Cliente e Servidor - como obter retorno do Cmd-> Cliente (Python)

Question: Eu quero executar um comando no Cmd do cliente e retorna os valores pro servidor.
Já Consegui Executar o comando no Cmd do cliente... Agora como faço para retorna os valores pro servidor?
O Servidor:
import socket
import os

Host = "127.0.0.1" #str(input("Host: "))
Door = 8291        #int(input("Door: "))

conexao = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

conexao.bind((Host,Door))
conexao.listen(2)

c, e = conexao.accept()

while True:

    os.system('cls')

    print("Connected to : ", e)

    print("\n\n")

    print("|----------------|-------|")
    print("|  Instructions  | Press |")
    print("|----------------|-------|")
    print("|  Send Message  |   1   |")
    print("|----------------|-------|")
    print("|  Send CodeDos  |   2   |")
    print("|----------------|-------|")

    print("\n\n")

    Choice = int(input("I Pick: "))

    os.system("cls")

    if Choice == 1:

        Msg = input("Message:")
        c.send("1".encode('ascii'))
        c.send(Msg.encode('ascii'))

    elif Choice == 2:

        Cod = input("CodeDos:")
        c.send("2".encode('ascii'))
        c.send(Cod.encode('ascii'))

    else:

        print("Faill")
        os.system('pause')

O cliente:
import socket
import os

Host = "127.0.0.1" #str(input("Host: "))
Door = 8291        #int(input("Door: "))

conexao = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

conexao.connect((Host,Door))

while True:

    info = conexao.recv(1024)

    if info.decode('ascii') == "1":   # If Choice == 1 (Message) Do:

        info = conexao.recv(1024)

        info = info.decode('ascii')

        print(info)

    elif info.decode('ascii') == "2": # If Choice == 2 (CodeDos) Do:

        info = conexao.recv(1024)

        info = info.decode('ascii')

        os.system(info)

Comment: No seu 'cliente' você usou apenas o recebimendo com "recv", mas, não enviou novamente para dar um retorno! Pode-se implementar o envio no final do "while" que irá retornar!
Answer: Perfeito, já está fazendo a conversa entre servidor e cliente, utilize no cliente a biblioteca subprocess
*Nota: Estou utilizando o Windows11, por isso utilizei o encoding cp850, pois é como o CMD do windows retorna.
import subprocess
command_output = subprocess.run("ipconfig", capture_output = True, encoding='cp850').stdout
print(command_output)

No seu caso agora amigo, basta ajustar sua estrutura, abaixo só a parte que eu modifiquei.
Cliente
import subprocess

...

elif info.decode('ascii') == "2": # If Choice == 2 (CodeDos) Do:
    info = conexao.recv(1024)
    info = info.decode('ascii')
    command_output = subprocess.run(info, capture_output = True, encoding='cp850').stdout
    conexao.send(command_output.encode('ascii'))

Servidor
elif Choice == 2:

    Cod = input("CodeDos:")
    c.send("2".encode('ascii'))
    c.send(Cod.encode('ascii'))
    command_return = c.recv(1024).decode('ascii')
    print(command_return)


How to determine if api service is blocking requests from VPS

Question: This works from my development machine (MacOS):
import requests
r=requests.get('http://api.usno.navy.mil/rstt/oneday?date=today&loc=Sacramento,CA')

But not from my VPS (Ubuntu LTS). From the VPS I get:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='api.usno.navy.mil', port=80): Max retries exceeded with url: /rstt/oneday?date=today&loc=Sacramento,CA (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 110] Connection timed out',))
Same version of Python (3.6) and requests (2.20.1) on both systems.
"Max retries exceeded with url:" is clearly not the problem, since I can access it from the MacOS machine.
How can I get requests to provide more information, such as determine if the IP of my VPS is being blocked from the API provider?
I am able to access http://www.airnowapi.org and https://api.weather.gov from both systems and receive a 200 status.
Comment: `Max retries exceeded with url: ...` is the libraries way of saying "I've tried to create a connection to the server a couple of times, but never succeeded". So you can't say it's "clearly not the problem". Could be a lot of reasons, maybe a firewall interfering with outgoing connections, network problem, ...
Comment: Under the hood `requests` tries multiple times. That's why you are getting the wording `Max retries exceeded with url`. Try to use `requests` to access other URLs from other services. If you are getting the same error, then it might be a firewall issue or similar. If not then the timeout error is your client telling you that you are blocked. To go around this you need something like a proxy service, or get a new IP, or the like.
Comment: Added: "I am able to access http://www.airnowapi.org and https://api.weather.gov from both systems and receive a 200 status"
Comment: Perfect. Then you're good to go. When you get `requests.exceptions.ConnectionError` you are blocked by `navy.mil` or your ISP for that particular service. Use a proxy service. I recommend [crawlera](https://scrapinghub.com/crawlera). No affiliation, just like them. One IP and it rotates proxies for you.
Answer: Be aware that on November 20, 2018 they have updated their api to version 2.2 which now uses HTTPS. Might want to update your URL to reflect that change.
r=requests.get('https://api.usno.navy.mil/rstt/oneday?date=today&loc=Sacramento,CA')


Vetor Inicializado mas Nulo

Question: Estou a dar em doidos, com um pedaço de código simples.
A minha aplicação, serve para dar informação a utilizar sobre um percurso.
Eu adiciono várias cidades, e depois tento adicionar ao vetor, mas no final o vetor encontra-se sempre nulo.
Eis o código:
 Ponto_Turistico Santo_Tirso = new Ponto_Turistico("Santo Tirso");
 Ponto_Turistico Felgueiras = new Ponto_Turistico("Felgueiras");

adPontoTuristico((T) Santo_Tirso);
adPontoTuristico((T) Felgueiras);

Método addPontoTuristico:
ERRO na Linha:

if (this.cont_cid == this.pontosTuristicos_roteiro.length) {

public void adPontoTuristico(T pontoT) {

    Ponto_Turistico c = (Ponto_Turistico) pontoT;

        if (this.cont_cid == this.pontosTuristicos_roteiro.length) {
            expandCapacity_PT();
        }
        this.pontosTuristicos_roteiro[this.cont_cid] = c;
        this.cont_cid++;

}

EDIT CLASSE ROTEIROS
public class Roteiros<T> {

    private final int DEFAULT_SIZE = 20;
    private Ponto_Turistico[] pontosTuristicos_roteiro;
    private int cont_cid;

    /**
     *
     */
    public void Roteiros() {

        this.pontosTuristicos_roteiro = new Ponto_Turistico[DEFAULT_SIZE];
        this.cont_cid = 0;

    }

    /**
     * Adiciona um ponto turistico
     *
     * @throws IOException
     */
    public void AdicionarPonto_Turistico() throws IOException {

        System.out.println("**Adicionar um Ponto_Turistico**");
        System.out.println("Nome do novo Ponto Turistico:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String opc = br.readLine();

        if (procurarPontoTuris((T) opc) != null) { // se o ponto turistico que o jogador adicionar  for diferente  de null, ou seja se ainda nao existir

            Ponto_Turistico nova_city = new Ponto_Turistico(opc);
            adPontoTuristico((T) nova_city);
        } else {
            System.out.println("Já existe um ponto turistico com o nome escolhido!");
        }
    }

    /**
     * Adiciona um Ponto Turistico
     *
     * @param pontoT a adicionar
     * @return
     */
    public void adPontoTuristico(T pontoT) {

        Ponto_Turistico c = (Ponto_Turistico) pontoT;

            if (this.cont_cid == this.pontosTuristicos_roteiro.length) {
                expandCapacity_PT();
            }
            this.pontosTuristicos_roteiro[this.cont_cid] = c;
            this.cont_cid++;

    }

    public void removerPontoTuristico() throws IOException {

        System.out.println("\n **Remover Ponto Turistico**");
        System.out.println("\n Introduza o ponto turistico a ser removido:");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String opc2 = br.readLine();

        Ponto_Turistico pro = procurarPontoTuris((T) opc2);
        if (pro != null) {
            for (int i = 0; i < this.cont_cid; i++) {
                if (this.pontosTuristicos_roteiro[i].getNome().equals(opc2)) {

                    this.pontosTuristicos_roteiro[i] = null;
                    this.cont_cid--;
                } else {
                    this.pontosTuristicos_roteiro[i] = this.pontosTuristicos_roteiro[i + 1];
                    this.cont_cid--;
                }
            }
        } else {
            System.out.println("Ponto Turistico: " + opc2 + " nao encontrada!");
        }

    }

    /**
     * Iniciar um jogo
     *
     * @throws IOException
     * @throws EmptyCollectionException
     */
    public void iniciar() throws IOException, EmptyCollectionException {
        String a;

        do {
            System.out.println("Agência de Viagens");
            System.out.println("1- Percursos Existentes");
            System.out.println("2- Administracao");
            System.out.println("3- Sair");

            System.out.print("A sua escolha: ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String opc = br.readLine();
            a = opc;
            System.out.println(" ");

            switch (opc) {
                case "1":
                    menuJogar();
                    break;
                case "2":
                    admin();
                    break;
            }

        } while (!a.equals("3"));

    }

    /**
     * Menu administrador
     *
     * @throws IOException
     */
    public void admin() throws IOException {
        String o;
        do {
            System.out.println("\n\nMenu Administrador");
            System.out.println("1- Adicionar Ponto Turistico");
            System.out.println("2- Remover Ponto_Turistico");
            System.out.println("3- Alterar Trajetos entre Pontos");
            System.out.println("4- Adicionar Trajetos Entre Pontos");
            System.out.println("5- Listar Pontos Turisticos");
            System.out.println("6- Listar Trajetos");
            System.out.println("7- Sair");
            System.out.print("A sua escolha: ");

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String opc = br.readLine();
            o = opc;

            switch (opc) {
                case "1":
                    AdicionarPonto_Turistico(); // adiciona um novo ponto turistico
                    break;
                case "2":
                    removerPontoTuristico(); // remove um ponto turistico já existente
                    break;
                case "3":
                    System.out.println("\nAlterar Trajetos entre Pontos ");
                    System.out.print("Nome do Ponto Turistico a ser alterado:");
                    String nome = br.readLine();
                    Ponto_Turistico proc = procurarPontoTuris((T) nome);
                    if (proc != null) {
                        int pos = 0;
                        for (int i = 0; i < this.cont_cid; i++) {
                            if (this.pontosTuristicos_roteiro[i].getNome().equals(nome)) {
                                pos = i;
                            }
                        }

                        System.out.println("\nINTRODUZA OS VALORES ANTIGOS!");
                        System.out.print("Distancia Total: ");
                        double old_dist = 0;
                        String ond_d = br.readLine();
                        old_dist = Double.parseDouble(ond_d);
                        System.out.print("\nDuracao Total: ");
                        double old_durac = 0;
                        String ond_dura = br.readLine();
                        old_durac = Double.parseDouble(ond_dura);
                        System.out.print("\nCusto Total: ");
                        double old_custo = 0;
                        String ond_cust = br.readLine();
                        old_custo = Double.parseDouble(ond_cust);
                        System.out.print("\nPonto Turistico Final: ");
                        String ond_city = br.readLine();

                        Trajeto[] old_traj = this.pontosTuristicos_roteiro[pos].getAdjacencias();

                        for (int j = 0; j < (this.pontosTuristicos_roteiro[pos].getNum_cidades() * 2); j++) {
                            if ((old_traj[j].getCidade_final().getNome().equals(ond_city)) && (old_traj[j].getDistancia() == old_dist) && (old_traj[j].getDuracao() == old_durac) && (old_traj[j].getCusto() == old_custo)) {
                                System.out.println("\nINTRODUZA NOVOS VALORES!");
                                System.out.print("Distancia: ");
                                double new_dist = 0;
                                String new_d = br.readLine();
                                new_dist = Double.parseDouble(new_d);
                                System.out.print("\nDuracao: ");
                                double new_durac = 0;
                                String new_dura = br.readLine();
                                new_durac = Double.parseDouble(new_dura);
                                System.out.print("\nCusto: ");
                                double new_custo = 0;
                                String new_cust = br.readLine();
                                new_custo = Double.parseDouble(new_cust);
                                System.out.print("\nCidade Destino: ");
                                String new_city = br.readLine();
                                if (procurarPontoTuris((T) new_city) != null) {
                                    Trajeto traje = new Trajeto(procurarPontoTuris((T) new_city), new_dist, new_durac, new_custo);
                                    this.pontosTuristicos_roteiro[pos].alterarTrajeto(pos, traje);
                                } else {
                                    System.out.println("Não foi encontrado nenhum ponto turistico com o nome " + new_city + "\n");
                                }
                            }
                        }
                    } else {
                        System.out.println("Ponto Turistico  não encontrado!\n");
                    }

                    break;
                case "4":
                    System.out.println("\n**Adicionar Trajetos ao Ponto Turistico**");
                    System.out.print("Indique a cidade à qual quer adicionar: ");
                    String city = br.readLine();
                    Ponto_Turistico proc2 = procurarPontoTuris((T) city);

                    if (proc2 != null) {
                        int pos1 = 0;
                        for (int i = 0; i < this.cont_cid; i++) {
                            if (this.pontosTuristicos_roteiro[i].getNome().equals(city)) {
                                pos1 = i;
                            }
                        }
                        System.out.println("\nINTRODUZA NOVOS VALORES!");
                        System.out.print("Distancia Total: ");
                        double new_dist = 0;
                        String new_d = br.readLine();
                        new_dist = Double.parseDouble(new_d);
                        System.out.print("\nDuracao Total: ");
                        double new_durac = 0;
                        String new_dura = br.readLine();
                        new_durac = Double.parseDouble(new_dura);
                        System.out.print("\nCusto Total: ");
                        double new_custo = 0;
                        String new_cust = br.readLine();
                        new_custo = Double.parseDouble(new_cust);
                        System.out.print("\nPonto Turistico de Destino: ");
                        String ncity = br.readLine();
                        Ponto_Turistico g = procurarPontoTuris((T) ncity);
                        if (g != null) {
                            this.pontosTuristicos_roteiro[pos1].adicionar_trajeto(g, new_dist, new_durac, new_custo);
                        } else {
                            System.out.println("Ponto Turistico de  destino nao encontrado!");
                        }

                    } else {
                        System.out.println("Ponto Turistico " + city + " não encontrado!");
                    }
                    break;

                case "6":
                    System.out.println("Pontos Turisticos Existentes");
                    for (int k = 0; k < this.cont_cid; k++) {
                        System.out.println(k + 1 + "- " + this.pontosTuristicos_roteiro[k].getNome());
                    }
                    break;
                case "7":
                    System.out.println("** Trajetos Existentes a partir da cidade **");
                    System.out.println("Introduza o Ponto Turistico a ver: ");
                    String l_ci = br.readLine();
                    Ponto_Turistico v = procurarPontoTuris((T) l_ci);
                    Trajeto[] t = v.getAdjacencias();

                    System.out.println("\n\n\nTrajetos:");
                    for (int ta = 0; ta < v.getNum_cidades(); ta++) {
                        System.out.println((ta + 1) + "- " + t[ta].getCidade_final() + " dista: " + t[ta].getDistancia() + " Custo: " + t[ta].getCusto() + " Duracao: " + t[ta].getDuracao());
                    }
                    break;
            }
        } while (!o.equals("8"));

    }

    /**
     * Menu Secundario
     *
     * @throws IOException
     * @throws EmptyCollectionException
     */
    public void menuJogar() throws IOException, EmptyCollectionException {
        String a;
        do {
            System.out.println("Menu de Viagens");
            System.out.println("1- Nova Viagem");
            System.out.println("2- Sair");

            System.out.print("A sua escolha: ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String opc = br.readLine();
            a = opc;
            System.out.println(" ");

            switch (opc) {
                case "1":
                    menuNovaViagem();
                    break;

            }
        } while (!a.equals("3"));
    }

    /**
     * Procurar pontos turisticos
     *
     * @param nome nome do ponto turistico
     * @return ponto turistico
     */
    public Ponto_Turistico procurarPontoTuris(T nome) {

        String nome_pontoTuris = (String) nome;

        for (int i = 0; i < this.cont_cid; i++) {
            if (this.pontosTuristicos_roteiro[i].getNome().equals(nome_pontoTuris)) {
                return this.pontosTuristicos_roteiro[i];
            }
        }

        return null;
    }

    /**
     * Menu novaViagem
     *
     *
     * @throws IOException
     * @throws EmptyCollectionException
     */
    public void menuNovaViagem() throws IOException, EmptyCollectionException {
        Percursos_SelfService percursoSelf = new Percursos_SelfService();
        String a;
        do {
            System.out.println("\n\n");
            System.out.println("O que deseja fazer:");
            System.out.println("1- Self Service Percurso");
            System.out.println("2- Ver Pontos Turisticos mais Atraentes");
            System.out.println("3- Procurar Percurso de uma Cidade");
            System.out.println("4- Sair");

            System.out.print("A sua escolha: ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String opc = br.readLine();
            a = opc;
            System.out.println(" ");

            switch (opc) {
                case "1":
                    percursoSelf.verPercursosMaisEconomicos();

                    /*  String opc2 = br.readLine();
                    System.out.println(" ");
                    Cidade target = procurarCidade((T) opc2);
                    if (target != null && !j.containsCidade(target)) {
                        j.conquistarCidade(target);
                    } else if (j.containsCidade(target)) {
                        System.out.println("Cidade já conquistada!");
                    } else {
                        System.out.println("Cidade nao encontrada!"); */
                    break;

                case "2":
                    System.out.println("Os nossos pontos de turismo");
                    for (int i = 0; i < this.cont_cid; i++) {
                        System.out.println(this.pontosTuristicos_roteiro[i].getNome());

                    }
                    break;
                case "3":
                    /*
                    System.out.print("Alterar para a cidade: ");
                    String b = br.readLine();
                    Cidade cti = procurarCidade((T) b);
                    if (j.containsCidade(cti)) {
                        j.setCidade_atual(cti);
                    } else {
                        System.out.println("Ainda nao conquistou " + cti.getNome());
                    }
                     */
                    break;

            }
        } while (!a.equals("4"));
    }

    /**
     * Carrega informação Esta função é uma alternativa pouco eficiente, na
     * tentativa de superar a falha nos ficheiros
     */
    public void carregarinfo() {

        Ponto_Turistico Santo_Tirso = new Ponto_Turistico("Santo Tirso");
        Ponto_Turistico Felgueiras = new Ponto_Turistico("Felgueiras");
        Ponto_Turistico Amarante = new Ponto_Turistico("Amarante");
        Ponto_Turistico Celorico_de_Basto = new Ponto_Turistico("Celorico de Basto");
        Ponto_Turistico Fafe = new Ponto_Turistico("Fafe");
        Ponto_Turistico Guimaraes = new Ponto_Turistico("Guimarães");
        Ponto_Turistico Mondim_Basto = new Ponto_Turistico("Mondim de Basto");
        Ponto_Turistico Famalicao = new Ponto_Turistico("VN Famalicão");

        Santo_Tirso.adicionar_trajeto(Felgueiras, 29.0, 41.0, 2.50);
        Santo_Tirso.adicionar_trajeto(Mondim_Basto, 63, 48, 2.2);
        Santo_Tirso.adicionar_trajeto(Famalicao, 11, 20, 3.5);
        Amarante.adicionar_trajeto(Famalicao, 12, 25, 4.5);

        adPontoTuristico((T) Santo_Tirso);
        adPontoTuristico((T) Felgueiras);

    }

    /**
     * Expande a capacidade de pontos turisticos
     *
     * @return
     */
    private Ponto_Turistico[] expandCapacity_PT() {

        Ponto_Turistico[] novo = new Ponto_Turistico[this.pontosTuristicos_roteiro.length + this.DEFAULT_SIZE];
        System.arraycopy(novo, this.cont_cid, this.pontosTuristicos_roteiro, 0, this.cont_cid);
        return novo;
    }

}

EDIT - CLASSE PONTO_TURISTICO
public class Ponto_Turistico<T> implements Comparable<Ponto_Turistico> {

    private final int DEFAULT_CAPACITY = 7;
    private final String nome;
    private Trajeto[] ligacoes;

    private double minDistance = Double.POSITIVE_INFINITY;
    private double minCusto = Double.POSITIVE_INFINITY;
    private double minDuracao = Double.POSITIVE_INFINITY;
    private int num_cidades;
    private Ponto_Turistico previous;

    /**
     * Construtor de Cidade
     *
     * @param nome Nome da Cidade
     *
     */
    public Ponto_Turistico(String nome) {
        this.nome = nome;
        this.ligacoes = new Trajeto[DEFAULT_CAPACITY];
        this.num_cidades = 0;
    }

    /**
     * Método para comparar Pontos Turisticos
     *
     * @param other outra cidade
     * @return -1 se menor 0 igual 1 superior
     */
    public int compareTo(Ponto_Turistico other) {
        return Double.compare(minDistance, other.minDistance);
    }

    /**
     * Obter nome
     *
     * @return nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * Obter adjacencias
     *
     * @return array de adjacencias
     */
    public Trajeto[] getAdjacencias() {
        return ligacoes;
    }

    /**
     * obter minima distancia
     *
     * @return
     */
    public double getMinDistance() {
        return minDistance;
    }

    /**
     * Obter anterior
     *
     * @return
     */
    public Ponto_Turistico getPrevious() {
        return previous;
    }

    public void setAdjacencies(Trajeto[] adjacencies) {
        this.ligacoes = adjacencies;
    }

    public void setMinDistance(double minDistance) {
        this.minDistance = minDistance;
    }

    public void setPrevious(Ponto_Turistico previous) {
        this.previous = previous;
    }

    public int getDEFAULT_CAPACITY() {
        return DEFAULT_CAPACITY;
    }

    public int getNum_cidades() {
        return num_cidades;
    }

    public void setNum_cidades(int num_cidades) {
        this.num_cidades = num_cidades;
    }

    public boolean isEmpty() {
        return this.num_cidades == 0;
    }

    /**
     * Expandir capacidadade de adjacencias
     *
     * @return
     */
    private Trajeto[] expandCapacity() {
        Trajeto[] novo = new Trajeto[this.ligacoes.length + DEFAULT_CAPACITY];
        System.arraycopy(novo, this.num_cidades, this.ligacoes, 0, this.num_cidades);
        return novo;
    }

    /**
     * Adiciona uma adjacencia
     *
     * @param cidadefinal cidade destino
     * @param distancia diastancia entre cidades
     * @param duracao duracao
     * @param custo custo por km
     */
    public void adicionar_trajeto(Ponto_Turistico cidadefinal, double distancia, double duracao, double custo) {
        if (isEmpty()) {
            this.ligacoes[0] = new Trajeto(cidadefinal, distancia, duracao, custo);
            this.num_cidades++;
        } else if (this.num_cidades == this.ligacoes.length) {
            this.ligacoes = expandCapacity();
            this.ligacoes[this.num_cidades] = new Trajeto(cidadefinal, distancia, duracao, custo);
            this.num_cidades++;
        } else {
            this.ligacoes[this.num_cidades] = new Trajeto(cidadefinal, distancia, duracao, custo);
            this.num_cidades++;
        }
    }

    public double getMinCusto() {
        return minCusto;
    }

    public void setMinCusto(double minCusto) {
        this.minCusto = minCusto;
    }

    public double getMinDuracao() {
        return minDuracao;
    }

    public void setMinDuracao(double minDuracao) {
        this.minDuracao = minDuracao;
    }

    /**
     * Funçao exponencial
     *
     * @param h o valor
     * @param d exponencial
     * @return resultado
     */
    private int exp(double h, double d) {
        int res = 1;
        for (double i = 0; i <= d; i++) {
            res *= h;
        }
        return res;
    }

    /**
     * Alterar adjacencia
     *
     * @param pos posição
     * @param trajeto trajeto
     */
    public void alterarTrajeto(T pos, T trajeto) {
        this.ligacoes[(Integer) pos] = (Trajeto) trajeto;
    }

}

EDIT ** MAIN
public static void main(String[] args) throws EmptyCollectionException, IOException {
        // mark all the vertices

        Roteiros play = new Roteiros();
        play.carregarinfo();
        play.iniciar();

    }

**EDIT com NOVO TESTE **
public class testeh{
private final int DEFAULT_SIZE = 20;
private int[] pontosTuristicos_roteiro;
private int cont_cid;

public void Roteiros() {

    this.pontosTuristicos_roteiro = new int[DEFAULT_SIZE];
    this.cont_cid = 0;

}

 public void adPontoTuristico(String pontoT) {

        if (this.cont_cid == this.pontosTuristicos_roteiro.length) {

        }
        this.pontosTuristicos_roteiro[this.cont_cid] = 0;
        this.cont_cid++;

}

**CLASSE MAIN **
 testeh dc = new testeh();
 dc.adPontoTuristico("ola");

Continua a dar erro, NullPointerExeption...
Answer: Desculpe a sinceridade, mas o seu código está muito cheio de problemas e bastante gambiarrado.
A causa do seu NullPointerException é isso:
public void Roteiros() {
    // ...
}

Não coloque void na assinatura dos construtores! Isso não é um construtor na verdade, é apenas um método com o mesmo nome da classe fingindo ser um construtor!
Além disso, seu código tem mais uma miríade de problemas. Por exemplo, isso:
    private Ponto_Turistico[] expandCapacity_PT() {

        Ponto_Turistico[] novo = new Ponto_Turistico[this.pontosTuristicos_roteiro.length + this.DEFAULT_SIZE];
        System.arraycopy(novo, this.cont_cid, this.pontosTuristicos_roteiro, 0, this.cont_cid);
        return novo;
    }

Ele cria um novo array mas não o coloca em lugar nenhum. Todos os lugares onde este método é chamado não fazem nada com o retorno. Se ao invés de usar arrays, você usasse listas, não precisaria ficar fazendo tanta gambiarra para saber se o array está cheio ou não, ou onde inserir elementos nele.
Também tem isso:
        if (procurarPontoTuris((T) opc) != null) { // se o ponto turistico que o jogador adicionar  for diferente  de null, ou seja se ainda nao existir

            Ponto_Turistico nova_city = new Ponto_Turistico(opc);
            adPontoTuristico((T) nova_city);
        } else {
            System.out.println("Já existe um ponto turistico com o nome escolhido!");
        }

Ou seja, se achou o ponto turístico, então adiciona um novo. Se não achou, diz que já existe. Você deveria ter usado == ao invés de !=.
Também, você usa os tipos genéricos para absolutamente nada útil. Tire os tipos genéricos deste código que eles não servem para nada.
Também digo para você usar apenas um Scanner e por favor, siga as regras de nomenclatura da linguagem. Há ainda mais um monte de coisas que eu poderia sugerir, mas vou ficar por aqui por enquanto.
Comment: Amigo, desculpe o trabalho, por um erro de porcaria!! Obrigado pelas sugestões de melhoria. Essa classe foi copiada de outra,  e muitas coisas ainda estão incompletas, mas obrigado pela correcção!
Comment: @ZéLuísDias Tudo bem.

Vetor Inicializado mas Nulo


Ordenar pelo maior ID e agrupar os semelhantes

Question: Como posso listar os registros, ordenando pelo último, mas quando tiver o campo related preenchido, trazer os outros registros com esse mesmo valor?
A ideia geral é fazer um order by id desc e agrupar os relacionados.
Minha tabela está assim:
ID  TITLE       RELATED
1   tenis       null
2   camiseta    123456
3   calca       null
4   moletom     123456
5   casaco      null

O correto seria listar assim: listando pelo maior ID, mas quando tem o campo related, da prioridade para os outros registros que possuem o mesmo valor que ele.
ID  TITLE       RELATED
5   casaco      null
4   moletom     123456
2   camiseta    123456
3   calca       null
1   tenis       null

Comment: Não me parece que isso se consiga fazer com uma _query_ "simples". Terá de haver aí alguma lógica intermédia.
Answer: SELECT id,
       title,
       related
  FROM produtos
 ORDER BY
  CASE WHEN related IS NULL
        THEN 1
        ELSE 0
   END, related;

Resultado:

id         title           related

2           camiseta        123456
4           moletom         123456
1           tenis           NULL
3           calca           NULL
5           casaco          NULL

(5 linha(s) afetadas)
Não é possível classificar desse jeito que você quer, pois a classificação segue
uma hierarquia o SQL classifica o  COD e dentro do cod classifica related
mas se você o principal objetivo for o agrupamento do related e em orderm descendente
talvez te ajude.
ou isso ajuda
SELECT title,
       related,
       cod
  FROM produtos
 ORDER BY
  CASE WHEN related IS NULL
        THEN 1
        ELSE 0
   END, related, cod DESC;

title           related     cod

moletom         123456      4
camiseta        123456      2
casaco          NULL        5
calca           NULL        3
tenis           NULL        1

(5 linha(s) afetadas)
Answer: Ficam aqui duas alternativas:
A primeira consiste em usar uma junção (com a própria tabela). Esta é uma solução portável, visto que apenas usa elementos ou funcionalidades standard da linguagem SQL, e que estão disponíveis em (quase) todos os sistemas de gestão de base de dados.
SELECT P1.*
  FROM produtos P1
  LEFT JOIN
  (
     SELECT RELATED, MAX(ID) MAX_ID
       FROM produtos
      WHERE RELATED IS NOT NULL
      GROUP BY RELATED
  ) AS P2
    ON P2.related = P1.RELATED
 ORDER BY CASE WHEN P2.RELATED IS NULL THEN P1.ID ELSE P2.MAX_ID END DESC, P1.ID DESC

A ideia passa por identificar os produtos para os quais existe informação na coluna RELATED, e para cada um dos códigos RELATED identificar qual o ID máximo correspondente. Esta informação será usada como critério de ordenação da seguinte forma:
ORDER BY CASE WHEN P2.RELATED IS NULL THEN P1.ID ELSE P2.MAX_ID END DESC
O resultado, será aquele que indicou na sua pergunta:
| ID  | TITLE    | RELATED |
| --- | -------- | ------- |
| 5   | casaco   |         |
| 4   | moletom  | 123456  |
| 2   | camiseta | 123456  |
| 3   | calca    |         |
| 1   | tenis    |         |

A segunda alternativa, faz uso das funções de janela disponiveis nas versões mais recentes do MySQL.
SELECT ID,
       TITLE,
       RELATED,
       MAX(ID) OVER (PARTITION BY CASE WHEN RELATED IS NOT NULL THEN RELATED ELSE ID END) AS SortingOrder
  FROM produtos
ORDER BY 4 DESC, ID DESC
;

O resultado será o mesmo da versão anterior.
| ID  | TITLE    | RELATED | SortingOrder |
| --- | -------- | ------- | ------------ |
| 5   | casaco   |         | 5            |
| 4   | moletom  | 123456  | 4            |
| 2   | camiseta | 123456  | 4            |
| 3   | calca    |         | 3            |
| 1   | tenis    |         | 1            |

Comment: Perfeito! Só uma observação, na primeira alternativa só foi preciso remover a parte "AS SORTING_ORDER". Pode ser pq estou usando a versão 5.7.
Comment: Tem razão, foi uma erro de copy-paste. Já alterei a resposta.


Are the accidentals counted as a single note or double notes?

Question: I am a newbie to music . I have a doubt about accidentals .
My doubt is whether a basic note and an accidental of that note is counted as a single note or double.
That is , if a tune X contains following notes C,C# , G,Gb only . My question is "Do that contains 4 notes or two notes that is C and C# or B and Bb is considered as a single note or double note ?"
Comment: Unless you can give an example, and make the question easier to understand, it will be disallowed. Please re-phrase it.
Comment: @Tim , The question had the example already. here the example..

That is , if a tune X contains following notes C,C# , G,Gb only . My question is "Do that contains 4 notes or two notes that is C and C# or B and Bb is considered as a single note or double note ?"
Comment: As an example, I hoped for a sample. It's not crystal clear.If it comes from an actual tune, please quote. For example, are those notes in the same bar? And when you ask about a double note do you mean two notes played simultaneously? And are you aware exactly what an accidental is?
Answer: The simple answer is that the altered version of a note is considered a different note than the unaltered version.  In other words, yes, a C and C# are considered two different notes.

To be clear though, the term note actually has three distinct, albeit related meanings in music.

First, the term note can be synonymous with pitch, which appears to be how you are using the word.  For instance, if I sing an A and you sing an A then we are singing the same note.
Note can also be used to mean a certain isolated occurance of a pitch, rather than the pitch itself.  In this sense, you could say that the first three notes of the theme from the first movement of Beethoven's fifth symphony are the same pitch (G).
Lastly, the term can be used to describe the written notation of a pitch and duration, the actual notes on a page of sheet music.

Assuming you are asking with the first definition in mind, there is actually one more layer of ambiguity.  With the examples you mentioned, it is clear that C and C# are two different pitches (they sound at different frequencies).  However, most musicians will tell you that C# and Db are two different notes as well, despite the fact that they represent the same pitch (in equal temperment) are are played with the same key on the piano.  And then there's also the question of whether or not you are going to say two pitches separated by an octave (two C#s for instance) are the same or different "notes".
The point is that the term note is not a particularly well-defined musical term, which makes your seemingly simple question at lot more complicated than you would think.  That said, there is no common definition for which C and C# would ever be considered the same note.
Comment: Well, you asked for it..  There's a thoroughly complicated answer to you question.  Hope that helps!
Comment: Thanks.. This is what I need. Will you able to give the theoretical explanation for this (or give a link for the theory behind this) @Casey Rule
Answer: The 'default' is a single letter name, A-G. If there are #s or bs in the KEY SIGNATURE then all he affected notes are played as such. As in the key of G, every F will be played as an F#. If a note somewhere in the tune needs to be different, as in a sharp, flat or natural, it will be shown just before that note, and will last for the rest of the bar.If your tune has a C in it, and later a C#, there will indeed be different notes, but at least one will have an ACCIDENTAL before it. The same with B and Bb.
Answer: In your example (C, C#, G, Gb) you have simply four distinct notes because in both theory and practice they are really different notes and pitches (sounds).
There is another more tricky example where you have two distinct names for notes but, basically, the same pitch: B#, C or E, Fb (there are more, of course). Well, B#, C share the same sound, but they are two distinct notes.
Answer: The notes C and C# are two different notes just as G and Gb are two different notes. So (going with your example), if you have a tune that contains C, C#, G, and Gb, you would have four different notes.


Error in installing a software named chatterbot

Question: I am getting the following error when I try to install ChatterBot 0.6.0
Error is:

Could not find a version that satisfies the requirement chatterBot (from versions: )
  No matching distribution found for chatterBot

The command I used for running is:
<code>pip install chatterBot
</code>
I am running it in Windows 8 64 bit system.
Answer: The package does not work for your python version. If you are using 3.6, lower it to 3.5; if you are on python 2, 2.7 should be fine.
Comment: Once you tried, please let me know whether that works. Cheers!


Area of a shaded region

Question:
What is the area of shaded region??
The radius of a circle is 4.

What I have tried :
if radius : r= 4
then length of square : length=8
and half of it is width of rectangle :width=4 So

The Area of a shaded region = area of rectangle(CDGF)-Areaof triangle(DGE)
=>legth*width-1/2(legth*width)
=>8*4-1/2(8*4)
=>24-12
=>12

the area of shaded region is 12

but answer does not have this option !! it does carry the option "None of these" but I am confused with my answer !!!
Comment: But the side of the square is not $8$. The diagonal is.
Comment: then how to do ??@Ari if i consider BCD triangle than if square side is not 8 so the same Problem :(
Answer: "None of these". Write $a=EF=EG=GD$, then you know $ED=4$. By Pythagoras, $EG^2+GD^2=ED^2$, i.e. $2a^2=16$; so $a^2=8$. The shaded area is $EF\cdot FC+\frac12 GD\cdot GE$, that is $\frac{3}{2}a^2=12$.
Answer: You know it has to be "None of the above" because it is the only answer that doesn't include "$\pi$". The question tells you that half the diagonal of the square has length $4$, so its side length is $4\sqrt{2}$. The circle needn't be there at all.
Comment: I guess this is an IQ-type of question. It needs analysis like yours.
Comment: Yes u right @Edward Jiang this the practice question for my test !!

Area of a shaded region


Logistic regression- is it okay to build a model that maximizes recall and use the coefficients for inference

Question: I'm a novice in the field of ML and stats.
So I have a dataset where the target feature (dependent variable) is binary (True, False), I'm trying to make some inferences and find features in the dataset that explain the target feature, I'm not trying to create a predictive model, I'm just trying to get the coefficients and the constant for further interpretations, my current method is to StratifiedShuffleSplit the dataset into a training and a testing sets, then use sklearn RFECV (Recursive feature elimination with cross-validation) with logistic regression (no regularization) and try to maximize the recall
I'm just wondering whether this is a possible way to do inference, or should I just stick with statsmodel logit and use maximum likelihood estimation
if you have other suggestions, please let me know, thanks!
Answer: Maximum likelihood estimation exists for a reason.  The gold standard objective function--the one that results in the best coefficients--is the log likelihood plus the penalty function.  The penalty can be a variety of functions.  The best penalty is the log prior, resulting in Bayesian estimates.  No penalty means you get ordinary maximum likelihood estimates, which are fine if your effective sample size (roughly the minimum of the number of outcome events or the number of non-events) is large in comparison with the number of predictor parameters.
Note that split-sample validation is not a good idea unless n>20,000 give-or-take.  Details about all this are in RMS.


solving second order nonhomogeneous Linear differential equation with undetermined coefficients

Question: The problem is to find the general solution of the following equation:

$$y''+ \alpha\ y'^2 =g$$
  where (alpha) and (g) are consents.

I cant find the particular form of y.can we use Perturbation to solve it?
Comment: **Hint:** Let $y' = p$.
Answer: Hint
As Moo commented, the key hint is to start defining $p=y'$. Doing so, the differential equation becomes $$p'+\alpha p^2=g$$ which is separable since you can write it as $$\frac{dx}{dp}=\frac 1 {g-\alpha p^2}$$ which easy to integrate $$\int dx=\int \frac {dp} {g-\alpha p^2}\implies x+C=\frac{\tanh ^{-1}\left(\frac{\sqrt{\alpha } }{\sqrt{g}}p\right)}{\sqrt{\alpha }
   \sqrt{g}}$$ Extract $p$ from it and integrate again.
I am sure that you can take it from here.


Multiple Regression in SPSS: Insignificant coefficients, significant F-statistic, no multicollinearity

Question: To give you some background, my thesis is looking at the relationship between 3 basic psychological needs (competence, autonomy, and relatedness) to 3 emotion regulation (ER) strategies (concealing, adjusting and tolerating). I have concluded with my supervisors that 3 multiple regression tests would be appropriate, where for each one there would be a different ER strategy as a predictor, and the psychological needs as regressors.
I have conducted the multiple regressions for each predictor, however, am confused by the SPSS outputs.
For my first test, for Concealing, my F-statistic is significant, (p = 0.000), however, 2 of the 3 predictors' T-tests are quite insignificant (p = 0.384, p = 0.618).
How can this be possible, if the F test shows that the model is powerful in predicting the outcomes, but the T-tests say otherwise? I thought that this could be due to multicollinearity, however, the VIF's are below 10.

When I do the test for the second ER strategy, none of the predictors are significant however the F-statistic still is, and VIFs are below 10. For the third ER strategy, tolerating, both the F-statistic and predictors are insignificant.

Attached are screenshots of what I am speaking about. Does this all simply mean my data doesn't explain anything? Or have I misinterpreted something?
I hope this makes sense and would really appreciate any guidance! Stats is not really my thing :)
Answer: Two things to keep in mind with MR analyses: First, the VIF cut-off of 10 is to flag extreme variance inflation. You have VIF values ≈2.5, which would result in $R^2 ≈ 0.6$...this means 60% of the variance is shared amongst those variables. Thus, there is some amount of multicollinearity at play here. Second, the p-values reported by SPSS for each variable in an MR model are to assess the significance of if adding that one variable to the model with all but that variable produces a statistically significant change in the amount of variance of the predicted variable explained. (That is a very long sentence, I realize.) In other words,
• calculate $R^2$ for the entire model;
• drop the variable on the current row;
• recalculate the MR and get the smaller $R^2$;
• assess the statistical significance of $H_0: \Delta R^2=0$; and
• repeat for all of the variables.
What may be more useful to you is to use a Type-I $\Delta R^2$ calculation for the model (this requires entering the variables in the model in a fixed and specific order).
Happy to share more if it will help.


Book Recommendation Please! [Casella Berger] Statistical Inference

Question: I would appreciate your 2 cent on book recommendation.
I have basic exposure to probability theory back in college (e.g. calc, stats, probability undergrad level) but haven't dealt with them for a while.
I am seeking that perfect book which could aid my reading on Casella Berger's Statistical Inference. An analogous example in Linear Algebra is, before delving into graduate level linear algebra, I read Axler's Linear Algebra Done Right, and this helped me tremendously.
A seasoned person, please help!
Thank you.
Answer: You could look at Hogg, Mckean and Craig's Introduction to Mathematical Statistics.
But Casella and Berger's Statistical Inference is suitable as a first text for both undergrads and graduate students with basic mathematical maturity.
Comment: Thank you for the suggestion. I will take a look into it!
Comment: I should add that Casella and Berger is probably too easy if you are already familiar with measure theory, despite its reputation as a "graduate level" textbook -- it is only really suitable for people without measure-theoretic knowledge.
Comment: It is definitely one of those graduate level books which is suitable for people who aren't graduate students. But even for people who know measure theory, it may be useful as a supplement to something like Shao's Mathematical Statistics (which I think is my favorite intro text).


End of url and nginx rewrite rule

Question: I'm using following nginx rewrite rule and I don't understand why next results.
server {
    server_name old.example.com;
    listen 80;
    listen [::]:80;
    rewrite ^/.*-f(\d+)/.*-t(\d+)\.html$ https://new.example.com/view.php?f=$1&t=$2 permanent;
}

The following result is ok (the url ends with '.html').
$ curl --head "http://old.example.com/abcd-f15/efgh-t125.html"
HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.6
Date: Sat, 02 May 2020 12:48:53 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://new.example.com/view.php?f=15&t=125

The following result is also ok (the url doesn't ends with '.html').
$ curl --head "http://old.example.com/abcd-f15/efgh-t125.htmlx"
HTTP/1.1 404 Not Found
Server: nginx/1.17.6
Date: Sat, 02 May 2020 12:50:26 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

But I don't understand the following result (the url doesn't ends with '.html' but rewrite rule is matched).
$ curl --head "http://old.example.com/abcd-f15/efgh-t125.html#x"
HTTP/1.1 301 Moved Permanently
Server: nginx/1.17.6
Date: Sat, 02 May 2020 12:51:53 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://new.example.com/view.php?f=15&t=125

Comment: The `#x` is the [fragment](https://en.wikipedia.org/wiki/URL#Syntax) and is not seen by the server.
Comment: OK I understand. So this fragment can't be redirected ? Something like `http://old.example.com/view.php#1 -> http://new.example.com/view.php#1` isn't possible ? We must only have `http://old.example.com/view.php#1 -> http://new.example.com/view.php` ?
Comment: In my answer, I've explained how the *fragment* is preserved by the client and you don't have to worry about it on your server-side redirection if it isn't supposed to change.
Answer: Let's take a look at the RFC 3986, 3 regarding the URL syntax:

     foo://example.com:8042/over/there?name=ferret#nose
     \_/   \______________/\_________/ \_________/ \__/
      |           |            |            |        |
   scheme     authority       path        query   fragment
      |   _____________________|__
     / \ /                        \
     urn:example:animal:ferret:nose

We are interested in the fragment, and it's defined in the section 3.5:

Fragment identifiers have a special role in information retrieval
systems as the primary form of client-side indirect referencing,
allowing an author to specifically identify aspects of an existing
resource that are only indirectly provided by the resource owner. As
such, the fragment identifier is not used in the scheme-specific
processing of a URI; instead, the fragment identifier is separated
from the rest of the URI prior to a dereference, and thus the
identifying information within the fragment itself is dereferenced
solely by the user agent, regardless of the URI scheme.

TL;DR: The processing of the HTTP URL fragment #x happens on the client side alone.
Let's add some verbosity to your curl with a -v and we can see this more clearly:
$ curl --head "http://old.example.com/abcd-f15/efgh-t125.html#x"

> HEAD /abcd-f15/efgh-t125.html HTTP/1.1
> Host: old.example.com
> User-Agent: curl/7.64.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.17.6
< Date: Sat, 02 May 2020 12:51:53 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
< Location: https://new.example.com/view.php?f=15&t=125

I believe from this you can already deduce why this is the expected behaviour for your rewrite.

What happens to the fragment after the redirect? Unless your Location: header exclusively specifies a new fragment, the client (web browser) preserves it per RFC 7231, 7.1.2:

If the Location value provided in a 3xx (Redirection) response does
not have a fragment component, a user agent MUST process the
redirection as if the value inherits the fragment component of the
URI reference used to generate the request target (i.e., the
redirection inherits the original reference's fragment, if any).

In short, for URL http://old.example.com/abcd-f15/efgh-t125.html#x:

If the Location doesn't specify a new fragment (the current situation):
  > GET /abcd-f15/efgh-t125.html
  > Host: old.example.com
  < Location: https://new.example.com/view.php?f=15&t=125

-> https://new.example.com/view.php?f=15&t=125#x

If you added a fragment y to your rewrite rule:
  > GET /abcd-f15/efgh-t125.html
  > Host: old.example.com
  < Location: https://new.example.com/view.php?f=15&t=125#y

-> https://new.example.com/view.php?f=15&t=125#y

The new HTTP request doesn't contain the fragment, either, so it's same for both:
  > GET /view.php?f=15&t=125
  > Host: new.example.com

End of url and nginx rewrite rule


What is the start code for i frame in H 264 codec?

Question: I want to know what is the start code and how to find the i frame in the H 264 codec. Basically I'm trying to encrypt the video files using Java; how can I parse the video file to get the i frame using Java?
Comment: possible duplicate of [How to decode H.264 video frame in Java environment](http://stackoverflow.com/questions/2165593/how-to-decode-h-264-video-frame-in-java-environment)
Answer: Based on the comments above here's a simple code snippet you can use:
ByteBuffer bb = ... // Byte buffer with your frame or h.264 NAL stream

int marker = 0xffffffff;
while (bb.hasRemaining()) {
    int b = bb.get() & 0xff;
    if (marker == 1) {
        if ((b & 0x1f) == 5)
            System.out.println("IDR slice!! " + (bb.position() - 1));
    }
    marker = (marker << 8) | b;
}

If you simply want to know whether a given frame is an IDR slice ( key frame ) or not you can use H264Utils.idrSlice from JCodec ( http://jcodec.org ):
ByteBuffer bb = ... // Byte buffer with your frame or h.264 NAL stream
if(H264Utils.idrSlice(bb))
    System.out.println("IDR slice!!");

The reason you would want to use library is because IDR NAL unit slice is not the only slice carried in an encoded frame, so you will have to get through the sequence of SPS,PPS and SEI to finally get to your IDR slice.
Answer: the previous answer is referring to the nal_unit_type field in the nal_unit().  Detailed syntax for nal_unit is in section 7.3.1() of ISO 14496-10 .
One thing that is different between H.264 and MPEG 2 is that H.264 distinguishes between an I slice and an IDR.  The I slice can itself be decoded into a complete picture, but it is possible for slices decoded afterward to refer to slices before the I.  Section 3.62 states "After the decoding of an IDR picture all following coded pictures in decoding order can be decoded without inter predition from any picture decoded prior to the IDR picture".  It's like an MPEG 2 closed GOP.
Returning to nal_unit_type, see table 7-1 which indicates that an IDR has nal_unit_type 5, but if you're looking for I slices, you can look at the primary_pic_type (table 7-5) in the access_unit_delimiter() (section 18.104.22.168), or you could check the slice_type (table 7-6) of the slice_header() (section 7.3.3)
Answer: If the stream is in Annex-B format, you may look for the "start code prefix" which equals to 0x000001, following by the NAL Unit. The NALU type information is provided in the first byte.


Obter url da página seguinte com javascript

Question: Estou tentanto comparar a página seguinte usando o history.go(), ou seja comparando a página seguinte com uma determinada url para obter um resultado positivo.
VEJA:

function a() {
    if (history.go(2) == "http://www.google.com.br") {
        alert("ok");
    }
}
</script>
<button onclick="a()">verificar</button>

Mas não acontece nada!
Uma outra coisa interessante seria se conseguisse exibir a url seguinte, com:
alert(history.go(2))

ou
alert(history.forward())

Mas o resultado que se obtem é indefinido.
Ou então verificar se uma URL está no history:
if (window.history.go("http://www.google.com.br")== true){
    alert("ok");
}

Ou até mesmo verificar se existe uma pagina seguinte no history, como:
if (history.forward() == true){
    alert("ok")
}

Se alguem puder me ajudar, agradeço desde já.
Comment: Eu **suponho** que isso não é possível pois de certa forma compromete a privacidade do usuário.
Answer: Os métodos history.go, history.back e history.forward não têm valor de retorno. O propósito destes métodos é navegar no histórico de páginas, e não saber qual é tal histórico.
Se fosse possível ler o histórico das páginas como você pretende, isso constituir-se-ia em uma falha de segurança, de forma que um código malicioso poderia aproveitar-se disso para saber quais são as páginas que o usuário acessou e então enviar essa informação para algum site malicioso, e portanto violando a privacidade do usuário.
Recomendo que você faça do lado do servidor o rastreio das páginas que o usuário acessou no seu site. Assim, você terá a parte do histórico relevante para o seu site e poderá redirecioná-lo conforme o necessário utilizando history.go(url).


How do I dynamically (AJAX) show radios in form API?

Question: A form like the following is expected to show additional fields when user selected "Yes" in the "Show more" radio button. When shown, the more section has a "Name" text field, and a "Gender" radios set.
But it never works as expected. The "Gender" label will be there, but the radio buttons are never rendered. I know changing "Gender" into radios would work, but for some reason, I need to use multiple radio buttons. Is there any way I can make this work?
<?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;

class DonationPrepareForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(
    array $form,
    FormStateInterface $form_state
  ) {
    $form['show_more'] = [
      '#type' => 'radios',
      '#title' => 'Show more',
      '#options' => [
        'Y' => 'Yes',
        'N' => 'No',
      ],
      '#ajax' => [
        'callback' => '::ajaxBuildForm',
        'wrapper' => 'edit-more-wrapper',
        'progress' => [
          'type' => 'throbber',
          'message' => $this->t('Loading...'),
        ],
      ],
    ];
    $form['more'] = [
      '#type' => 'container',
      '#prefix' => '
',
      '#suffix' => '
',
    ];

    return $form;
  }

  public function ajaxBuildForm(array &$form, FormStateInterface $form_state)
  {
    if ($form_state->getValue('show_more') === 'Y') {
      $form['more']['name'] = [
        '#type' => 'textfield',
        '#title' => 'Name',
        '#required' => TRUE,
      ];
      $form['more']['gender'] = [
        '#type' => 'radios',
        '#title' => 'Gender',
        '#options' => [
          'M' => 'Male',
          'F' => 'Female',
        ],
        '#required' => TRUE,
      ];
    }
    return $form['more'];
  }
}

Same question extends to other composite form elements (e.g. Checkboxes). Is there any way to make AJAX work for them?
Answer: Actually it is significantly easier to use form element #states property to make an element visible , invisible , enabled , disabled , required or ... rather than using ajax
A code like this will do the job:
<?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;

class DonationPrepareForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(
    array $form,
    FormStateInterface $form_state
  ) {
    $form['show_more'] = [
      '#type' => 'radios',
      '#title' => 'Show more',
      '#options' => [
        'Y' => 'Yes',
        'N' => 'No',
      ],
    ];

    $form['more']['name'] = [
      '#type' => 'textfield',
      '#title' => 'Name',
      '#states' => array(
        'visible' => array(
          ':input[name="show_more"]' => array('value' => 'Y'),
        ),
        'required' => array(
          ':input[name="show_more"]' => array('value' => 'Y'),
        )
      ),
    ];
    $form['more']['gender'] = [
      '#type' => 'radios',
      '#title' => 'Gender',
      '#options' => [
        'M' => 'Male',
        'F' => 'Female',
      ],
      '#states' => array(
        'visible' => array(
          ':input[name="show_more"]' => array('value' => 'Y'),
        ),
        'required' => array(
          ':input[name="show_more"]' => array('value' => 'Y'),
        )
      ),
    ];

    return $form;
  }

}
Comment: This does not work for my case because I'd want the appeared fields to be required fields.
Comment: What does this not work exactly? The required state is also input-dependent in the answer?
Comment: States don't work for radio buttons: https://www.drupal.org/project/drupal/issues/3267246
Answer: Move form building to buildForm(). You can't build form elements in an Ajax callback (I've also renamed the callback method to make this clear):
<?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;

class DonationPrepareForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
     $form['show_more'] = [
      '#type' => 'radios',
      '#title' => 'Show more',
      '#options' => [
        'Y' => 'Yes',
        'N' => 'No',
      ],
      '#ajax' => [
        'callback' => '::ajaxCallback',
        'wrapper' => 'edit-more-wrapper',
        'progress' => [
          'type' => 'throbber',
          'message' => $this->t('Loading...'),
        ],
      ],
    ];
    $form['more'] = [
      '#type' => 'container',
      '#prefix' => '
',
      '#suffix' => '
',
    ];

    if ($form_state->getValue('show_more') === 'Y') {
      $form['more']['name'] = [
        '#type' => 'textfield',
        '#title' => 'Name',
        '#required' => TRUE,
      ];
      $form['more']['gender'] = [
        '#type' => 'radios',
        '#title' => 'Gender',
        '#options' => [
          'M' => 'Male',
          'F' => 'Female',
        ],
        '#required' => TRUE,
      ];
    }

    return $form;
  }

  public function ajaxCallback(array &$form, FormStateInterface $form_state) {
    return $form['more'];
  }
}

Comment: Thanks a lot. It works!


Lazy Column is blinking when navigating with compose navigation

Question: I set up navigation, pagination and use flow to connect ui with model. If simplify, my screen code looks like this:
@Composable
MainScreen() {
    val listState = rememberLazyListState()
    val lazyItems = Pager(PagingConfig(...)) { ... }
        .flow
        .cachedIn(viewModelScope)
        .collectAsLazyPagingItems()

    LazyColumn(state = listState) {
        items(lazyItems, key = { it.id }) { ... }
    }
}

And here is my NavHost code:
NavHost(navController, startDestination = "mainScreen") {
    composable("mainScreen") {
        MainScreen()
    }
}

But when i navigate back to MainScreen from another screen or just opening the drawer, data is loaded from DataSource again and i see noticeable blink of LazyColumn.
How to avoid reloading data?
Answer: Your code gives me the following error for cachedIn:

Flow operator functions should not be invoked within composition

You shouldn't ignore such warnings.
During transition Compose Navigation recomposes both disappearing and appearing views many times. This is the expected behavior.
And your code creates a new Pager with a new flow on each recomposition, which is causing the problem.
The easiest way to solve it is using remember: it'll cache the pager flow between recompositions:
val lazyItems = remember {
    Pager(PagingConfig(/* ... */)) { /* ... */ }
        .flow
        .cachedIn(viewModelScope)
        .collectAsLazyPagingItems()
}

But it'll still be reset during configuration change, e.g. device rotation. The best way to prevent this is moving this logic into a view model:
class MainScreenViewModel : ViewModel() {
    val pagingFlow = Pager(PagingConfig(/* ... */)) { /* ... */ }
        .flow
        .cachedIn(viewModelScope)
}

@Composable
fun MainScreen(
    viewModel = viewModel<MainScreenViewModel>()
) {
    val lazyItems = viewModel.pagingFlow.collectAsLazyPagingItems()
}

Comment: It didn't helped in my case, after navigating back `lazyItems.itemCount` shows 0 for about 100 milliseconds and it cause flickering. Paging version is `3.1.1`
Comment: @AkbolatSSS Check out [this answer](https://stackoverflow.com/a/69491725/3585796), if it's not the case - ask a separate question with more details and [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example).
Comment: not my case, not pure Compose for now. But I've found that `collectAsLazyPagingItems#collectPagingData` triggers with a delay, which cause zero items at first


Best practice to make cleanup after Scala Future is complete

Question: I want to make some cleanup (like close db connection) after a Future is complete.
Currently I achieve it this way:
Future { ... } onComplete {
  case Success(v) =>
    // ...
    conn.close()
  case Failure(ex) =>
    // ...
    conn.close()
}

There's duplicate code and it's also tedious.
Is there any best practice to this?
Answer: Since the same action conn.close() is performed on both success and failure, consider executing it as a side-effect using andThen like so
Future { ... } andThen { _ => conn.close() }

Similarly, using onComplete we could do
Future { ... } onComplete { _ => conn.close() }

The difference between andThen and onComplete is that latter will return Unit, that is, discard the returned value of the Future.
Answer: The sad truth is that Scala Futures don't have this basic feature built in. I would therefore strongly suggest using a modern effect system like ZIO or cats-effect, both of which solve this problem and a myriad of others that Futures have. The easiest way to do what you want is to use the bracket method:
https://zio.dev/docs/overview/overview_handling_resources
Now bracket works great, but there's a way that usually works even better: the Managed type. It's virtually impossible to write code that leaks resources if you consistently use Managed when acquiring resources:
https://zio.dev/docs/datatypes/datatypes_managed
That said,if you absolutely must use Futures, you'll have to write your own try-finally equivalent. Or you can use mine:
  def tryFinally[A](tryy: => Future[A])(finallyy: => Future[Any])(
  implicit ec: ExecutionContext): Future[A] =
    Future.fromTry(Try(tryy)).flatten.transformWith { t =>
      finallyy.flatMap((_: Any) => Future.fromTry(t))
    }

Answer: I like to use map, you can do something like this:
val mapped: Future[String] = future.map(_ => "OK").recover{case _ => "KO"}

Comment: Using two calls when one would do is not good practice. Use a single call like `onComplete`, `andThen` or `transform`


How can I tune a bagpipe practice chanter?

Question: I find that the high G is sometimes as much as a semitone sharp. One solution would be to try to use less air pressure when playing the note, but that's tricky. Currently I am using electrical tape to partially cover the G hole, but that has the problem of inconvenience and also crushes the sound of the G grace note. Is there a better solution?
Comment: That's pretty normal for woodwind instruments. I play saxophone, and there's absolutely no way an instrument's going to be in tune all the way, whether it's professional or not. Most have a "quiet" 2nd octave D and D#, a sharper 2nd octave overall and a messed up low-end. Depends on the material used as well. Wood will shrink and crack if you don't play it regularly.
Answer: There are some excellent suggestions at
https://appsreeds.com/pages/dealing-with-a-sharp-high-g
(Some of which you've tried already).
The other thing to say is that practice chanters aren't always made to the most exacting of standards, so it may be something that can't be fixed like an incorrect hole placement, a resonating chamber that has not been precision engineered, or a poor quality reed (although the reed can be swapped out of course).
If it's a new chanter sold to you from a reputable instrument maker you should contact them and discuss the issue direct - if it's by an established maker they may even be prepared to help if it's one of theirs but you bought it second hand. If it's a no-name practice chanter bought off ebay or from a shop where the chanters were displayed next to the tartan scarves and the shortbread, it's probably not going to be of that quality.
Answer: If this is helpful, the tape should only be used to change the pitch of certain notes, while making it easier to fit your fingers. I would suggest getting a new chanter and starting afresh. Maybe visit the manufacturer and question about this as it might not be your fault. That's all I really know in how to fix the problem. I have only had experience with  bagpipes practice chanter, but it has never failed me...


Where can I find a time range widget in Javascript/jQuery?

Question: I'm looking for some sort of TimeRange widget in Javascript/CSS/jQuery.  I'm not looking for a time/date picker, which are widely available.
I need it for a website to allow businesses to select their openinghours by clicking and hovering over the hours they're open.
<code>+-----------------------------+
| 0h 0h15m 0h30m    ... 23:45 |
+-----------------------------+
</code>
Anybody has seen such a nice looking customizable timerange selector widget?
Cheers
Answer: Google Calendar has nice one (you can see it when you click "Check guest and resource availability" link on the event details form). But I can imagine it would be hard to clone.

(source: rajchel.pl)
Comment: Thanks for the screenshot!  That's exactly what I'm looking for, in a more compact form.   I can't image it doesn't already exist, eg. as a pluggable jQuery widget.
Answer: I'd look for a slider widget.. then set the times you need as the intervals.
The jQuery UI has one:  jQuery UI Slider.
Update: based on the comment below about (single vs. double slider)...
1.) Theres a post already (just found) about making a 2 handled slider using the jQuery UI slider here.
Or if you have 2 sliders... one for opening time and one for closing... where each is broken down into 15min segments, but only for half a day each, would this work?
e.g. (ignore the ASCII-graphic uglyness)
<code> Open Time (AM): 12   1    2    3    4    5    6    7    8 |  9    10   11   12
                                                           ^ 8:15am

Close Time (PM): 12   1    2    3    4    5    6  |  7    8    9    10   11   12
                                                  ^6:30pm
</code>
Furthermore, if this is for "typical" businesses... you could likely chop from 11pm <-> 5am from the sliders.
Or,
I'm not a big fan of scriptaculous, but they seem to have a double slider:
Comment: I thought about that as well, but that wouldn't really work as we don't know how many regions there are from the start.  Eg. A business could be open from 8am-10am, 12-14am and 18-20am.  Moreover, as I understand from the documentation, there's a max of  one range per slider it seems.

This is an example I found with ticks: http://jsbin.com/iwime
Comment: Thanks scunliffe, I'm resorting to this not-so-flexible solution, until a widget emerges!
Answer: http://trentrichardson.com/examples/timepicker/
go to " Show only the time picker without date picker" and check if that is what you need.
Answer: http://ghusse.github.io/jQRangeSlider/index.html
Demos here jQRangeSlider and here Technical demo

jQRangeSlider is open source published under dual license GPL and MIT.
  You can use it in both open source and commercial software.

<code>Prerequisites:
    jQuery
    jQuery UI Core
    jQuery UI Widget
    jQuery mousewheel plugin (optional)
</code>


Spring JPA Versioned Entity hibernate errors

Question: Is there a better way to update a versioned entity other than to catch exception and retrying ?
Below is the code
<code>@Entity
@AllArgsConstructor
@Builder
@NoArgsConstructor
@Data
@IdClass(com.test.domain.EmployeeKey.class)
public class Employee {
    @Id
    private Integer employeeId;
    private String name;
    private Double commission;
    @Id
    private LocalDate inputDate;
    @Version
    private Integer version;
}

public class EmployeeKey  implements Serializable {
    private Integer employeeId;
    private java.time.LocalDate inputDate;
}

@SpringBootApplication
@Slf4j
public class SampleApplication {

    public static void main(final String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

    @Component
    public class AppStartupRunner implements ApplicationRunner {
        @Autowired
        EmployeeRepo employeeRepo;
        @Override
        public void run(ApplicationArguments args) throws Exception {
            log.info("Your application started with option names : {}", args.getOptionNames());
            List<Employee> empList = new ArrayList();
            Employee employeeComm1 = Employee.builder().employeeId(1).name("Sam").commission(100.45).inputDate(LocalDate.of(2019,10,01)).build();
            Employee employeeComm2 = Employee.builder().employeeId(1).name("Sam").commission(87.54).inputDate(LocalDate.of(2019,10,01)).build();
            Employee employeeComm3 = Employee.builder().employeeId(2).name("John").commission(56.78).inputDate(LocalDate.of(2019,10,01)).build();
            Employee employeeComm4 = Employee.builder().employeeId(3).name("Katie").commission(65.23).inputDate(LocalDate.of(2019,10,01)).build();

            empList.add(employeeComm1);
            empList.add(employeeComm3);
            empList.add(employeeComm4);
            empList.add(employeeComm2);
//            employeeRepo.saveAll(empList);
            for (Employee emp: empList) {
                try {
                        employeeRepo.save(emp);
                    }
                 catch(Exception e) {
                     if (employeeRepo.findByEmployeeIdAndInputDate(emp.getEmployeeId(), emp.getInputDate()).isPresent()) {
                         Employee empFromDb = employeeRepo.findByEmployeeIdAndInputDate(emp.getEmployeeId(), emp.getInputDate()).get();
                         empFromDb.setCommission(emp.getCommission());
                         empFromDb.setName(emp.getName());
                         employeeRepo.save(empFromDb);
                     }

                }
            }

        }
    }
}
</code>
Output shown below
Hibernate: insert into employee (commission, name, version, employee_id, input_date) values (?, ?, ?, ?, ?)
Hibernate: insert into employee (commission, name, version, employee_id, input_date) values (?, ?, ?, ?, ?)
Hibernate: insert into employee (commission, name, version, employee_id, input_date) values (?, ?, ?, ?, ?)
Hibernate: insert into employee (commission, name, version, employee_id, input_date) values (?, ?, ?, ?, ?)
2019-10-15 16:43:10.372  WARN 17216 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1, SQLState: 23000
2019-10-15 16:43:10.372 ERROR 17216 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ORA-00001: unique constraint (I706446.SYS_C008493) violated
Hibernate: select employee0_.employee_id as employee_id1_0_, employee0_.input_date as input_date2_0_, employee0_.commission as commission3_0_, employee0_.name as name4_0_, employee0_.version as version5_0_ from employee employee0_ where employee0_.employee_id=? and employee0_.input_date=?
Hibernate: select employee0_.employee_id as employee_id1_0_, employee0_.input_date as input_date2_0_, employee0_.commission as commission3_0_, employee0_.name as name4_0_, employee0_.version as version5_0_ from employee employee0_ where employee0_.employee_id=? and employee0_.input_date=?
Hibernate: select employee0_.employee_id as employee_id1_0_0_, employee0_.input_date as input_date2_0_0_, employee0_.commission as commission3_0_0_, employee0_.name as name4_0_0_, employee0_.version as version5_0_0_ from employee employee0_ where employee0_.employee_id=? and employee0_.input_date=?
Hibernate: update employee set commission=?, name=?, version=? where employee_id=? and input_date=? and version=?
Comment: If the employe exists are you updating the existed one ?
Comment: yes, if the combination of employeeId+inputDate exists - I expect an hibernate to issue an update with Version column incrementing in DB table.
Comment: My approach would be to check if the entity exists in the database first. If so, I would just update it. Else I would create it.
Answer:
I expect an hibernate to issue an update with Version column
  incrementing

Your expectation is incorrect. You use version not as it is intended to be used. The purpose of versioning is to provide optimistic locking and not simply indicate how many times has the entity changed. The intended usage scenario is following: before updating an entity you should read it from the database, then modify, then save. Hibernate checks if the version you are trying to save is the same as in the database.
A) If the same, it will automatically increment version and save your entity, i.e. it will update the existing record in the database.
B) If the version in the entity you are trying to save is less than the version in the database, it means you are trying to save an outdated version. It means you are not aware of changes that were done in the meanwhile. In such case Hibernate will throw an exception.


Content block doesn't show when query is added to the URL

Question: I have a Drupal 7 page where some of my content blocks don't show up, if there is a query added to the URL.
Say my block is main_content, and it is shown all all pages except the listed ones, where no page is listed; the block appears correctly on on www.mydomain.com, but not on www.mydomain.com?gclid=xxx.
This is an issue for me because Google AdWords banners are linking to my page with a query attribute, and then the page does not display correctly.
What could be the issue here?
Comment: Does this happen for every page or only the frontpage? Do you use context or panels?
Comment: I managed to fix this issue. I'm very new to Drupal and I'm administrating a site that someone else built. The problem was in template.php in a function called 'views_views_pre_execute'. Some code was checking for drupal path alias and some custom queries was built.

I changed: '$path = substr(drupal_get_path_alias(request_uri()),1);' into 'drupal_get_path_alias($_GET['q']);' and now it works.
Answer: I managed to fix this issue. I'm very new to Drupal and I'm administrating a site that someone else built. The problem was in template.php in a function called 'views_views_pre_execute'. Some code was checking for drupal path alias and some custom queries was built. I changed: '$path = substr(drupal_get_path_alias(request_uri()),1);' into 'drupal_get_path_alias($_GET['q']);' and now it works.


Initial value in Cauchy's problem properties?

Question: i have this simple question :
let's have this Cauchy's problem , (with $f$ given and smooth):
$y'(t)=f(y(t))$ and $y(0)=y_0$ ,t>0,
is it true that when we have $y_0=0$ then the solution $y$ verify $y(t)=0$ $\forall t>0$  if yes how can we prove it .
(and if not true can someone give a counter-example)
thanks ....
Comment: What are your thoughts? What have you tried?
Comment: i think it is true , well i found this example , $y'(t)=y^2(t)$ , and $y(0)=0$ then the book says that $y$ is zero everywhere so i wanted to know if it is true in general
Answer: No. What is true is that if $f(y_0)=0$, then $y(t)=y_0$ is a constant solution.
Try the modified example $y'=y^2-1$ which has constant solutions at $y_0=\pm 1$ but not at zero.
Comment: you said "" What is true is that if $f(y_0)=0$ then $y(t)=y_0$ is a constant solution."" this is exactly what i was searching !! can you tell me how to prove it??
Comment: Well, $y'(t)=0=f(y(t))$, so the differential equation is satisfied.
Comment: i don't see, what you want to tell me , here in your case there is only $f(y_0)=f(y(0))=y'(0)=0 $ that all what we have , doesn't it ? how did you get  $y'(t)=0$ for all t
Comment: in general case i mean not in this example , above
Comment: The derivative of a constant is constant zero.
Comment: yes i know , but here you proved it only for t=0, how about t>0
Comment: No, this is valid everywhere since it is constant, nothing changes.


Can I include a correlated dummy in a Fixed Effects Model?

Question: I am researching whether an increased number of women in a board of directors has influence on the company's performance.
I have conducted a hausman test and am using a fixed effects model.
My model includes several uncorrelated independent variables / controls like R&D-Budget, Board size, usw.
I have a diversity index to measure the amount of women in percent, the values range from 0-100, and I have a dummy that just stands for the absolute value of 3 or more women per board. (0= less then three / 1= three or more)
Can I bring both these values in one regression, or would they skew my results? (Which I suspect they do)
Answer: If the variables measure the same "thing"/phenomenon, your model could suffer from multicolinearity, which would inflate your standard errors (e.g. the precision of your statistical model).
Suppose you start running a model without the dummy, and obtain a precisely estimated (low standar errors) effect. Now, if you run the model with both variables included and see that coefficients on both have a low precision (i.e. high standard errors), you should investigate if they are highly correlated (i.e. measure the same phenomenon). Standard errors are proportional to the correlation between any two sets of variables (see https://en.wikipedia.org/wiki/Variance_inflation_factor).
If they are highly correlated, it possibly indicates that your model cannot differentiate between the effects (if there indeed are two seperate ones) as they measure the same thing. It could also be the case that they do not have any bearing on your dependent variable, but then you would expect that the single variable regression (+ controls) turned out the have an insignificant coefficent.
What your model's effectively saying is (as I interpret your model) that there is some threshold at three women in which there is an extra effect, and at the same time there is some effect from adding more women to the board. You might also consider if this logic, and the results you obtain, are consistent.
Comment: You got my model exactly right, I want to find out whether more women equal more performance and if at 3+ women, this performance behaves differently (positive/negative or stronger/weaker).

So from what I read from your comment, I can run my model with both variables included?

Or should I first run both seperately, then joined and see if the standard errors change?
Comment: If so, I would suggest you use an interaction variable between 3+-dummy and female proportion, like: beta_2*D(>=3)*prop + *beta_1*prop. As of now, you test if there is a single "bump" when there are more than three women, and not a shift in the effect of proportion (correct?). Moreover, I suggest you start with the most parsimonious model. In your case, this might be there are no extra effect at 3+, corresponding to running a model with only proportions, and testing this against a model with 3+-interaction. Be advised that I am unfamiliar with your research's specific, so consider carefully!
Comment: Yes, that totally makes sense. I didn't even know this possibility existed, thank you!

This page also explained it seemingly well:

https://biologyforfun.wordpress.com/2014/04/08/interpreting-interaction-coefficient-in-r-part1-lm/

Edit: Just read your edit. Yes, your assumption is correct. I want to see whether a higher female share increases performance, but expect a expecially high effect at 3+.

In case I run two sparse models, one with only the dummy and one with only the proportion, depending on my significance and regression I can decide whther it's worth joing them, correct?
Comment: I think I got it wrong the first time, once with only the proportions and once with the proportions AND 3+, correct?
Comment: Yes, with an interaction between 3+ and proportion, please read: https://en.wikipedia.org/wiki/Interaction_(statistics), where x1=prop, and x2 = 3+. The first model under "Introdction" section is what you proposed (I think) and latter is what I suggested. This differs, as in intepret it, from your first suggestion in the quesiton.


manupulate title using pipe in html-Angular2

Question: I'm building rows and columns using "ngx-datatable". for one of the row item I need to use a pipe to capitalize the item. I know how to do it in .js. But I must need to do it in html for some reason. I don't have any idea how to do that in html. Can any one help?
Thanks in advance..

  private getRows() {
    this.rows = [];
    for (const item of this.list) {

      const rowObject: any = {
        date: item.date,
        method:item.method,                    status:item.status),
        detail: item.paymentId
      };

      this.rows.push(rowObject);
    }

  }
 <ngx-datatable
    [rows]="rows"
    [columns]="columns"
    *ngIf="rows.length">
  </ngx-datatable>
Comment: If you don't want to do it in `js` the other option is `css`: `text-transform: capitalize;`.
Comment: Not exactly to "capitalize". I must need to use a pipe for one of the row item
Comment: You can pass your pipe in the row template mentioned [here](https://swimlane.gitbooks.io/ngx-datatable/content/api/detail/inputs.html)
Answer: You can control the output of your row template.
<ngx-datatable [rows]="rows" [columns]="columns" *ngIf="rows.length">

  <ngx-datatable-column name="Date">

    <ng-template let-value="value" ngx-datatable-cell-template>

      {{value | date}}
    </ng-template>

  </ngx-datatable-column>

</ngx-datatable>

Comment: condition means what kind of condition?
Comment: Whatever condition you want to apply.  ex. `*ngIf="row.$$index == 2"` - if row index is 2 etc


Por que preciso chamar Schema::table, no Laravel, mais de uma vez?

Question: No momento, estou em um curso da Udemy sobre Laravel 7. Em um ponto dele, preciso renomear a coluna de uma tabela chamada site_contacts. Quando tento simplificar o código e colocar todas as instruções em apenas um callback ocorrerá o erro There is no column with name 'contact_reason_id' on table 'site_contacts' ao chegar na instrução para alterar o tipo da coluna. Porém, se eu colocar separadamente, como está no código, a migration é executada sem problemas.
Gostaria apenas de entender porque precisa ser feita desta forma e se tem algum jeito melhor. A princípio, acho que a classe precisa atualizar seus atributos internos para prosseguir com as instruções. É isso mesmo o que ocorre?
public function up()
{
    Schema::table('site_contacts', function (Blueprint $table) {
        $table->renameColumn('contact_reason', 'contact_reason_id');
    });

    Schema::table('site_contacts', function (Blueprint $table) {
        $table->unsignedBigInteger('contact_reason_id')->change();
    });

    Schema::table('site_contacts', function (Blueprint $table) {
        $table->foreign('contact_reason_id')->references('id')->on('contact_reasons');
    });
}

public function down()
{
    Schema::table('site_contacts', function (Blueprint $table) {
        $table->dropForeign('site_contacts_contact_reason_id_foreign');
        $table->dropIndex('site_contacts_contact_reason_id_foreign');
    });

    Schema::table('site_contacts', function (Blueprint $table) {
        $table->integer('contact_reason_id')->change();
    });

    Schema::table('site_contacts', function (Blueprint $table) {
        $table->renameColumn('contact_reason_id', 'contact_reason');
    });
}

Answer: Já passei por isso, ao que parece, o "rename" da coluna: 'contact_reason' para 'contact_reason_id' só ocorre quando toda a migração é executada.
Ocorre o erro que está enfrentando porque quando ele chega no trecho:
 Schema::table('site_contacts', function (Blueprint $table) {
    $table->unsignedBigInteger('contact_reason_id')->change();
});

a coluna 'contact_reason_id' ainda não existe.
Acho interessante querer saber o porque, mas o ideal é você fazer separadamente mesmo.


How to handle exceptions in a batch process

Question: I have some special configured content entities in my system which are protected in a pre-delete hook against entity deletion.
There is also a batch process (VBO - entity delete) where they can be selected before hand.
I want to handle gracefully the exception in a batch process which wasn't written by me (and cannot be touched). I tried to subscribe to the exception event and look for xmlHttpRequest which was all fine, but then I dunno what response to set back to stop the process, or log the exception within the batch process with the proper message. What I always get back is this, where the error message is what I put into the AjaxResponse body:

Any help appreciated find a more sophisticated solution to redirect from the batch processing in case of an exception.
Comment: You need to return an ajax response. But solving the problem how you can pick up the batch once you've thrown an exception seems to be far more complicated than a simple try/catch statement around the delete in the batch process, so that you can pick up the deletion process right away in the same batch request without bothering the user.
Comment: But this is what am currently doing: returning an ajax response and I also attached the image above what the result is. This is not working as you can see.
Comment: And I also wrote: it's a 3rd party code, I don't even have the chance to put try catch block anywhere. Please read carefully again.
Comment: You would need to return an Ajax response which pickups the batch process, not a 500 status code. I've read the question. I didn't try to answer. My comment was saying, look you could add a simple try/catch to the batch or you have the very difficult task how to pickup the batch again once you are thrown out.
Comment: I find this unbelievable that the batch API cannot take care of simple exceptions.
Comment: Is it this action you are using https://git.drupalcode.org/project/views_bulk_operations/blob/8.x-3.x/src/Plugin/Action/EntityDeleteAction.php? Why not write your own action wrapping a try/catch around $entity->delete?
Comment: This what I did in the end: create my own plugin, extend from VBO plugin, and then just pust entity->delete in try catch.
Thank you for your support!
Comment: Great, do you mind posting the plugin code as an answer?
Answer: So in the end I took and overrode the plugin provided by the VBO.
This will put the exception message into the summary displayed with a drupal_set_message at the end of the batch process (VBO 3.0.0-alpha2).

namespace Drupal\my_module\Plugin\Action;

use Drupal\views_bulk_operations\Plugin\Action\EntityDeleteAction;

/**
 * Delete entity action with default confirmation form.
 *
 * @Action(
 *   id = "my_module_delete_entity",
 *   label = @Translation("MyModule Delete selected entities"),
 *   type = "",
 *   confirm = TRUE,
 * )
 */
class MyModuleEntityDeleteAction extends EntityDeleteAction {

  public function execute($entity = NULL) {
    try {
      $entity->delete();
      return $this->t('Delete entities');
    }
    catch (\Exception $e) {
      return $e->getMessage();
    }
  }

}