The probability that the number of successful of Bernoulli experiments are always close to their mean

Question: Given an infinite series of Bernoulli experiments, with probability p for the success of each, I wish to show that for every $p'<p$, the probability that at any point in the sequence more than $p'$ of the tests were successful is positive.
I am not entirely certain this theorem holds, but it feels intuitively true to me, yet I haven't found a way to prove it.
Comment: The title does not describe the question.
Answer: At each time $n$, add $1-p'$ if the $n$th test is succesful, and $-p'$ otherwise. Call $X_n$ the position after $n$ steps. Since each test is succesful with probability $p$, the process $(X_n)$ performs a random walk with drift (the mean displacement) $(1-p')p+(-p')(1-p)=p-p'$. This drift is positive hence the probability that $X_n\gt0$ for every $n\geqslant1$ is positive.

Example: Assume that $p'=\frac13\lt p$, then $Y_n=3X_n$ is a $+2/-1$ random walk. Let $t_i$ denote the probability that $(Y_n)$ hits $0$ starting from $i$, then the probability that $X_n\gt0$ for every $n\geqslant1$ is $z=p\cdot(1-t_2)$.
Furthermore, the Markov property after one step of the random walk shows that $(t_i)$ solves the system $t_0=1$, $t_i=pt_{i+2}+qt_{i-1}$ for every $i\geqslant1$, where $q=1-p$.
Thus $t_i=ar^i+bs^i+ct^i$ for some $(a,b,c)$, where $(r,s,t)$ solve the characteristic equation $x=px^3+q$. Thus, $t=1$ and $(r,s)$ solve $px^2+px=q$, that is, $s=-\frac12-\frac12\sqrt{1+4q/p}$ and $r=-\frac12+\frac12\sqrt{1+4q/p}$, hence $s\lt-1$ and $0\lt r\lt1$.
One knows that $t_i\to0$ when $i\to\infty$ hence $b=c=0$ and $t_i=r^i$ for every $i\geqslant0$. In particular, $z=p(1-r^2)=\frac32p-1+\frac12p\sqrt{1+4q/p}$.
Sanity checks: $z\to1$ when $p\to1$ (why?), $z\to0$ when $p\to\frac13$ (why?), and $z$ is a nondecreasing function of $p$ (why?).
Comment: Your random walk argument is excellent. But I was thinking can we prove it just by calculating the probability explicitly and showing that it is positive?
Comment: @SamratMukhopadhyay When $p'=a/b$ is rational the probability $u$ to stay positive for ever solves a polynomial equation of degree $b$ hence what you suggest is possible in principle--except that $1$ always solves said polynomial equation hence, to decide that $u$ is another root one must know that $u\ne1$ by another argument. If $p'$ is irrational, this approach fails even before that since the random walk lives on a dense subset of the real line.
Comment: The probability $u$ is going to be $\sum_{k=\lceil{np'}\rceil}^{n}p^k(1-p)^{n-k}$ right? Kindly correct me if I have got the whole thing wrong.
Comment: @SamratMukhopadhyay Why do you think so? I see no reason why and this contradicts the solvable cases.
Comment: That's why I was a little confused about this question, I thought the question asks to show that the probability I have written is positive for all $n$. Maybe I understood it wrong. Please tell me the correct one.
Comment: @SamratMukhopadhyay Please compare your beliefs with the case fully solved at the end of my post.
Comment: let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/9843/discussion-between-samrat-mukhopadhyay-and-did)
Comment: Thanks. I really liked the proof.


std::map::erase(const key_type&) stop working after comparison customized

Question: I customize the comparison in std::map as below.
class my_cmp {
public:
  bool operator()(const string &a, const string &b) {
  return (a.length() >= b.length());
  }
};

map<string, int, tmp_cmp> tmp; works well with all key sorted per string length. But tmp.erase("a string"); no longer works. Is there a solution to make the std::map::erase(key_type) still work after the comparison customized?
Comment: My map keys can have the same length. that's what I expect. But even I change to
    `class my_cmp {
      public:
        bool operator()(const string &a, const string &b) {
          if (a.length() == b.length())
            return (a  b.length());
        }
    };`
Answer: Custom comparator in std::map must have the same behavior as "less" operator. So, you have to change your code to something like return (a.length() < b.length());
Comment: yes. Actually { if (a.length() == b.length()) return (a  b.length()); } works. Thanks.
Answer: @Chad's comment aside, I think your problem is that you include = in your implementation.
map's definition of equality is !less(a,b) && !less(b,a). This does not work for your definition of less.
Here is what Standard says in "23.2.4 Associative containers"

3 The phrase "equivalence of keys" means the equivalence relation
  imposed by the comparison and not the operator== on keys. That is, two
  keys k1 and k2 are considered to be equivalent if for the comparison
  object comp, comp(k1, k2) == false && comp(k2, k1) == false. For any
  two keys k1 and k2 in the same container, calling comp(k1, k2) shall
  always return the same value.
Comment: btw, where can I find map's definition of equality?
Comment: Edited the answer to include standard reference.
Answer: The comparator for a map must impose a strict weak ordering. In part that means that comp(a,b) and comp(b,a) cannot both be true. So comparisons should act more like < and > and not like <= or >=.


Autosize grid columns as per value in grid value in extjs6

Question: I want to autosize the grid columns to max size of the content. How do i do this. i see only autoSize function on grid column, not sure how to use it.Say column header width is more then the column data width, that should be used.
I have used autosize function, It works, but i have few issues, I have to define the align property i.e text fields will be left and number fields will be on right, then heading text gets cut. I have locked columns in all reports, locked section gives some empty spaces.
https://fiddle.sencha.com/#fiddle/1cr6

See column selling code. Its not showing full because of align: Right property. If you remove it will work.
2.After DOW column empty space is coming.
Without minWidth Property columns wont scale properly.
Answer: Take a look at this piece of documentation: Ext.grid.column.Column - Setting sizes. I don't think you can make columns scale to content. You can, however, set fixed sizes or make them flex.
I hope this helps.
Comment: Yeah i have given them fixed sizes.


How do I remotely connect over TCP to a SQL Server 2008 instance (firewall is off, TCP is enabled)?

Question: I am attempting to connect to a SQL Server 2008 R2 instance from another machine using the TCP protocol over port 1433.
On the server side:

SQL Server is running.
SQL Server has had its "Allow remote connections to this server"
checkbox checked.
SQL Server has the TCP protocol enabled.
At least one of the IP addresses in the list of IP addresses that SQL
Server claims to be listening on is the correct one.  None of the
items in this list are Enabled, but that is not anything that I
have touched.  All are Active.  All have their port set to 1433
(I never touched it).  I have also set all of them to Enabled at various points; this made no difference.  I have not been able to track down any documentation on why both an active status and an enabled status need to be maintained.
TCP dynamic ports is set to blank (I never touched it) for all IP
addresses.  As I understand it, this is what you want to do for connections that should specify the TCP port explicitly in their connection information (which is what I want).
TCP port is set to 1433 for all IP addresses, including IPAll.
The firewall is off.
SQL Server authentication is set to SQL Server Authentication
(followed by Windows Authentication).

I am able to ping the box remotely.
The symptoms of not being able to connect are basically my client (a JDBC client) hangs.  The URL is analogous to this: jdbc:sqlserver://my.host.here:1433;databaseName=frob and I specify a valid user and password (and in any event do not get a login/security error).
The URL is valid and well-formed.
I can telnet to port 1433 on the box without a connection refusal error so the port is open.
Thanks for any help you can provide.
Answer: And the answer is: it's a regression in the JDK, version 1.6.0 update 29 (you know, the version that is current on the Mac).  See this: https://bugs.java.com/bugdatabase/view_bug?bug_id=7105007


How to increase the accuracy of my logistic regression model?

Question: I am dealing with a tricky, unbalanced data set and trying to run a logistic regression model. One class is present with a 10:1 ratio.
My objective here is to boost my predictive accuracy - minimize the incorrect predictions and maximize the correct ones.
I've tried undersampling (which doesn't work very well) and I have tried logistic regression,case-weighted logistic, and Firth logistic regression.
None of them are very successful.
While I can sometimes yield good true negative rates depending on the dataset, in the end the prediction is merely representative of its underlying class distribution. The case-weighted logistic does about as well as the Firth when I test it - which is to say, it does horribly.
So...

Is there anything else I can do to meet my objective? From what I
can tell, exact logistic regression is an option but only for very
small datasets, which is not the case here.
Do I need to go back and explore variable selection?
Why is it that the penalized model (Firth) is not a substantial
improvement over the case-weighted logistic?
Are there any options I am not considering?
Should I change tactics and look to something like an anomaly
detection model instead?

--
Please excuse me if this question is lacking in details -this is my first time asking a question here. I would be happy to add in anything.
Comment: Logistic regression outputs probabilities. What's the resampled AUC of these probabilities?
Answer: Just think about the fact that you have a 10:1 class ratio and yet your interest is in zero-one loss, that is, maximizing the proportion of cases correctly identified. The accuracy of a trivial model that just guesses the modal class for every case would be 10/(10 + 1) = 91%, which is pretty high. In order to substantially beat 91%, as with 95% accuracy, you need one or more highly predictive features. If you don't have any, as is often the case in real problems, the best you can hope for is quite small improvements on 91%. In a nutshell, this problem is too easy for statistics or machine learning to be able to get you much more predictive power.
Possibly I can give you more specific advice if you add details.
Answer: It might make sense for you to try different models. You might have some success with something like decision trees (which can also help you find highly predictive values as Kodiologist suggested), KNN, SVMs, or some other model that can handle non-linear relationships.
Answer: Generally, in case your model is biased you need to come up with a more complex model -- one way to achieve this is to add more (hopefully good) features as been pointed out previously. But this can be a no easy matter and probably depends on your domain expertise. What you, however, could try out of the box is features interaction. Essentially, you are not limited in choosing the order of interactions/number of interacting variables and then could compare different models produced in such a way. Hope this makes sence
Answer: As some others have suggested, moving to a more complex algorithm may provide an increase in accuracy. If you were to stick with logistic regression, perhaps oversampling your smaller cohort may help to enrich your predictive performance. This could be achieved using a weighted logistic regression weighted logistic regression. Thankfully social scientists have implemented many of these algorithms in R and SAS.
Another strategy could be to use either forward or backward feature selection.
Ultimately, my money would be to move to more complex models, and would give particular attention to random forests and boosting.


Expressão regular para validar um campo que aceita CPF ou CNPJ

Question: Tenho um <input> no formulário que deve aceitar CPF ou CNPJ.
Utilizarei no back-end em PHP o valor para guardar ou no campo "cpf" ou no campo "cnpj" de uma tabela no banco de dados.
Estou usando AngularJS e preciso de uma expressão regular para colocar em ng-pattern, para aceitar apenas dados utilizáveis.
Não me interessa neste momento validar os dígitos de verificação ao final.
Quero ser um pouco flexível, permitindo ao usuário que use pontos, traço e barra se quiser, ou só números.
Answer: Solução:
Esta solução valida estes formatos: 00000000000, 00000000000000, 000.000.000-00, 00.000.000/0000-00 e até 000000000-00 ou 00000000/0000-00, por exemplo. Os pontos e traços são opcionais em cada uma das posições.
O que não é aceito, por exemplo: 000-000-000-00 (mas pode ser alterado conforme nota abaixo)
Regex:
([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})

Clique aqui e faça seus testes em realtime no regexpal.
Explicação:

[0-9]{2} Faixa de caracteres: 0 a 9, quantidade: 2 caracteres;
[0-9]{3} Faixa de caracteres: 0 a 9, quantidade: 3 caracteres;
[0-9]{4} Faixa de caracteres: 0 a 9, quantidade: 4 caracteres;
[\.]?Um ponto, opcional. Foi usado \ no ponto, pois ele sozinho é caractere especial;
[-]? Um traço, opcional (se acrescentar outros caracteres, comece pelo - sempre);
[\/]? Uma barra, opcional. Tambem "escapada" com \ pra agradar o PCRE;
(grupo1)|(grupo2) Se um dos grupos validar, a expressão é válida.

Caso queira aceitar outros separadores, basta acrescentar entre os [ ].
Exemplo: [-\.\/]? vai aceitar tanto - quanto . e / naquela posição (? = ou nada).

Para adaptar para outros contextos de regexp, algumas variações possíveis seriam tirar o escape da barra (\/ => /) e opcionalmente colocar um ^ no começo e um $ no fim da linha.
Nota: propositalmente usamos [0-9] em vez de \d para que não sejam aceitos outros sets considerados dígitos (como ¹²³ e outras "surpresas" que, por exemplo, o Unicode pode trazer no futuro).
Comment: Esta minha pergunta foi feita somente porque, ao pesquisar no Google, os resultados apresentados eram medonhos. Eu pretendia postar a minha resposta logo em seguida, mas você foi hiper-veloz em responder - e forneceu uma resposta boa e correta (e sem ser *copy/paste* de pesquisa Google). Hoje, enfim, postei minha resposta, apenas relatando o que já estava implementado no momento da pergunta. Aceito sua resposta como a correta por merecimento, mas fica aqui o comentário para o público que a minha resposta, abaixo, está igualmente OK. Beleza? Obrigado.
Comment: E viva o Stack Overflow... que alcance o topo dos resultados no Google e as pessoas possam acessar boas respostas! :-)
Comment: @J.Bruni quando for assim deixe um recado no comment da pergunta, assim a gente tem como saber que já tem resposta engatilhada. (depois deleto esse comment aqui). Grato pela consideração. De fato, eu montei o regex na hora mesmo, nem me aventurei a procurar, por ser uma coisa bem específica.
Comment: mais simples: `CNPJ_REGEXP=/^(\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2})$/`
Comment: @FernandoFabreti procurei fazer ela mais didática e legivel, mas é bom o pessoal saber da versão com \d. (geralmente o parser transforma as duas versoes na mesma coisa).  Só não sei como vai ficar fica o \d com dígitos não arábicos à medida que o suporte de unicode se ampliar, e podem passar outras coisas "esquisitas" pra finalidade.
Comment: Ahh, entendi. Blz.
Comment: sem falar que tem a opção de responder a própria pergunta assim que posta
Comment: @Bacco Sugiro também acrescentar o ^ e $ para evitar o match quando o usuário digita, por exemplo, 12, 13 ou 15 dígitos. Aliás, acrescentei uma resposta com look behind e look ahead, para o caso do CPF ou CNPJ poder aparecer no meio do texto.
Comment: @PauloMerson eu já havia mencionado no fim da resposta. Depois, se for o caso elaboro melhor pra evidenciar o ponto. Grato pela dica, de qq forma.
Answer: Expressão regular para validar um campo que aceita CPF ou CNPJ (sem cálculo dos dígitos verificadores):
/^([0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}|[0-9]{2}\.?[0-9]{3}\.?[0-9]{3}\/?[0-9]{4}\-?[0-9]{2})$/

Ela pode ser entendida assim (onde "cpf" é a expressão para validar CPF e "cnpj" é a expressão para validar CNPJ):
/^(cpf|cnpj)$/

As barras inicial e final (/) não fazem parte da expressão propriamente dita - são apenas delimitadores. O caracter ^ no começo e o caracter $ no final exigem que o conteúdo completo do string a ser validado corresponda à expressão entre os mesmos. Os parênteses contendo a barra vertical (a|b) criam uma "opção" alternativa entre "a" e "b". Satisfazendo qualquer uma das duas expressões, o resultado será positivo. No lugar de "a" e "b", temos então as expressões específicas para CPF e CNPJ, separadamente.
Para CPF:
[0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}

A interrogação (?) faz com que a especificação de caractere precedente seja opcional. Portanto os pontos e o tracinho são opcionais. A classe de caracteres [0-9] representa qualquer caracter de 0 a 9 (poderíamos usar \d, mas eu prefiro [0-9] por ser mais legível). Finalmente, o número entre colchetes ({3}) determina uma quantidade específica de vezes que a especificação de caractere precedente deve se repetir. Assim sendo, são requeridos no total 11 caracteres numéricos (3 + 3 + 3 + 2).
Para CNPJ, a estrutura é semelhante:
[0-9]{2}\.?[0-9]{3}\.?[0-9]{3}\/?[0-9]{4}\-?[0-9]{2}

Aqui são requeridos no total 14 caracteres numéricos (2 + 3 + 3 + 4 + 2).
Lembrando que a barra invertida (\) antes do ponto (.) e outros  caracteres especiais é um caracter de "escape", que serve para desconsiderar a interpretação especial do caractere seguinte e considerá-lo literalmente. (O ponto, sem "escape", significa "qualquer caractere". Com "escape", significa meramente o caractere "ponto" mesmo.)

Para saber se é CPF ou CNPJ
No lado do servidor, em PHP, é feita a seleção entre CPF ou CNPJ considerando o número de dígitos presentes no campo:
$numeros = preg_replace('/[^0-9]/', '', $valor);

if (strlen($numeros) == 11)
{
    $cliente->cpf = $valor;
}
elseif (strlen($numbers) == 14)
{
    $cliente->cnpj = $valor;
}

Observação: isso não substitui a validação feita pela expressão regular que vimos acima, a qual também é realizada no lado do servidor (no meu caso as regras estão embutidas no modelo, com as mesmas expressões regulares de validação que vimos acima para CPF e CNPJ, só que separadas - cada qual em seu respectivo campo).
Answer: Para um input simples (permite digitar apenas CPF ou CNPJ):
^(\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}|\d{3}\.?\d{3}\.?\d{3}-?\d{2})$

Para um input ou text box em que o CPF ou CNPJ pode aparecer no início, meio ou fim de um texto:
(?<=\D|^)(\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}|\d{3}\.?\d{3}\.?\d{3}-?\d{2})(?=\D|$)

A diferença principal é o uso de "look behind" e "look ahead" para garantir que o dígitos do CPF ou CNPJ são precedidos/sucedidos por caracteres não numéricos. Sem isso, haveria o match de uma sequência de, por exemplo, 20 dígitos!
Answer: E-mail: "^([-a-zA-Z0-9_-]*@(gmail|yahoo|ymail|rocketmail|bol|hotmail|live|msn|ig|globomail|oi|pop|inteligweb|r7|folha|zipmail).(com|info|gov|net|org|tv)(.[-a-z]{2})?)*$"
CPF: "^((\d{3}).(\d{3}).(\d{3})-(\d{2}))*$"
CNPJ: "^((\d{2}).(\d{3}).(\d{3})/(\d{4})-(\d{2}))*$"
CPF/CNPF:"^(((\d{3}).(\d{3}).(\d{3})-(\d{2}))?((\d{2}).(\d{3}).(\d{3})/(\d{4})-(\d{2}))?)*$"
Ex.: (VB.NET)
Dim hRegex As Regex = New Regex("^(((\d{3}).(\d{3}).(\d{3})-(\d{2}))?((\d{2}).(\d{3}).(\d{3})/(\d{4})-(\d{2}))?)*$", RegexOptions.None)

MsgBox(hRegex.IsMatch($"000.000.000-00")) 'Saída {True|False}

Comment: Fábio, [edite](https://pt.stackoverflow.com/posts/277479/edit) sua resposta e para formatar o código aperte `CTRL+K`.


R floating point number precision being lost on coversion from character

Question: I have a large floating point number as a character like so
  x<-"5374761693.91823";

On doing
 as.numeric(x);

I get the following output
   5374761694

I would like to preserve the floating point nature of the number while casting.
Answer: use digits argument in print to see the actual number:
> print(as.numeric(x), digits=15)
[1] 5374761693.91823

options is another alternative:
> options(digits=16)
> as.numeric(x)
[1] 5374761693.91823

> # assignments
> options(digits=16)
> y <- as.numeric(x)
> y
[1] 5374761693.91823

z <- print(as.numeric(x), digits=15)
z

Comment: what about assignment ?


Is it possible extend filesystem on a non-LVM?

Question: I have an Amazon Line AMI with two drives, one for root and one for data,
8G     /
1024G  /data

the data partition almost full  93% /data, and I want to extend the data partition without wiping the disk.
Is it possible to change an existing filesystem on a non-LVM partition and turned it into an LVM logical volume without having to wipe the disk ?
Answer: No it is not possible to change an existing non-LVM partition into to LVM without pvcrating on it, destroying the data on there. You'll have to do something like:

Get a new 1024G disk, and vgcrate new_vg /dev/newdisk (replace new_vg and newdisk as appropriate)
lvcreate -L+100%FREE -n new_lv (replace new_lv as appropriate)
Format and mount it somewhere
move the files (probably requires a service window)
when old disk is copied, you can umount it vgextend new_vg /dev/datadisk (the disk currently mounted on /data)
and lvextend -L+100%FREE new_vg/new_lv , then grow the fs: resize2fs /dev/new_vg/new_lv

Either that or make a backup of the files, umount the disk and repeat 1-4 (without the getting of new disk)
Answer: Take a snapshot of the disk, then create a new bigger volume from that snapshot. If your AMI uses cloud-init then it will automatically grow to fill it. If it does then edit the partition table in fdisk (make the partition as big as you want it to be) then reboot and use resize2fs to do an online resize.
Comment: but the maximum volume in Amazon is 1024G.
Answer: If you don't want to take the potentially time-consuming LVM conversion path, here's another approach that may or may not work for you, depending what kind of directory layout you have under /data.
Use mount points! In essence:

Get a new 1024 GB disk
Create a filesystem (an LVM partition, if you so will) on it
Mount your new filesystem somewhere temporary place, like /data/tmp if that does not exist already
Copy some big dir of your /data, such as  /data/bigdata to /data/tmp
Rename the original /data/bigdata to other name, such as /data/bigdata.old
Create a new dir /data/bigdata
Unmount /data/tmp (your new disk)
Mount your new disk on top of /data/bigdata

OK, I admit this is far from ideal, but something like this had to be done before the volume managers came in existence. Petter H has a better idea in his answer and you should probably follow that anyway.


Any way to determine how files were deleted on Linux/Apache server?

Question: PROBLEM:
I have several missing directories on an offsite Linux/Apache server. I've tracked down about when they went missing from the httpd error logs, and I have a list of SSL logins to the machine (complete with network addresses.)
There are several individuals who have access to my account on the machine, so I am trying to confirm whether the files were removed accidentally or intentionally. If I can determine that the session came from an external network address, then I can be sure it was intentional. Otherwise I will need additional information.
QUESTION:
Is there any way to determine what user account (mine vs. root), network session, or process removed the files? I'm not worried about restoring them as backups are recent.
RELATED QUESTION:
I've tried looking at the bash history for my own account (the only one other than root with permission) and it only seems to extend back to the beginning of my SSL session. Does this imply the bash history was tampered with, or is this normal behavior?
Answer: By default, this isn't logged (imagine how big the logs would get). You can add logging with auditctl or inotify/iwatch (interesting options, check them out), or your control panel logging or ftp logs (usually all in /var/log somewhere) or bash history might help you track it down a little. Check your HTTP access logs in case some GET request managed to do this (unlikely, but possible)
If your history isn't there, it has been cleared. This is either done manually, or a command in your .bash_logout or .logout files.
Also, it's SSH not SSL :)
Comment: It doesn't operate over SSL, it uses The Secure Shell Transport Layer Protocol: http://tools.ietf.org/html/rfc4253
Comment: ! ... wow, I was 99% percent positive SSH operated over SSL; I have no idea where such a strong misconception came from. Thanks for the heads up.
Answer: Cleanup is always a hard.  Resources you can use:
To determine who was logged in, and from where:

last / lastlog
/var/log/secure (shows successful and failed logins most of the time)

To determine what was going on at the time:

ausearch (assuming you are auditing; if you aren't, you might consider starting)
sar (to figure out if the system was idle or busy at the time)

If there hasn't been much churn since the incident, the mtime on the directory might have useful info (probably not, but I generally like to collect that kind of data either way)
For the future, get every single person with access to the machine an account of their very own.  What you have now is very silly for the reason you have just discovered;  You can't know who did what.
Comment: Unfortunately, my boss was who decided to give out my account info to the rest of the IT team... until yesterday, I wasn't even aware. I will have to discuss this with him.
Answer: The bash history only gets written when you close the shell (you can deactivate that for the current session). In a running bash session, you can't see the commands you typed in that session in the bash history file - close the session, start a new one and you should be able to see it.
Comment: The history command will show .bash_history plus any history from the current session, but you're right in that it won't appear in .bash_history
Answer: If you are running process accounting, you may be able to find the call(s) to rm in the processs accounting log.
Shell history may have been removed, or may be configured for session private history.  Concurrent sessions can also cause problems with shell history.
Answer: The only reliable way to do this is to run system accounting (like sar or similar.) Other things might give you the answer, but are not guaranteed:

bash history is not guarateed because (a) there is a history limit; (b) by default it is not written instantly; (c) you have to search through all bash history of all user accounts; (d) history options affect the behavior; (e) A user can clear their own bash history (prevent it from ever being written to file.)
sftp by default does not log transactions, but this can be enabled on the sftp server if needed. files added or removed via sftp or ftp are not logged into a shell history file or any other place.

In your particular case, if you are not already running accounting, then your only recourse is to search through the shell command histories for all the user accounts, in the hopes that it is preserved.


jQuery: selector question (dash)

Question: i'm trying to select objects which are having a dash in their classname - like <code>$("div.nav-next");</code>
it doesnt work - seems like the dash is the problems - any ideas?
thanks
Comment: I am pretty sure it works. I have done it for years in both jQuery and CSS. What does your HTML look like?
Comment: sorry i didn't realize that the elements in questions were generated by a plugin (so not existing in the dom ...) shame on me :(
Comment: I will second Bolt's comment. This works as shown: http://jsfiddle.net/VW8WP/ It's possible there is some other markup we're not seeing that is causing issues with your selector.
Comment: I believe it does work...type this into the firebug console to test $(".user-action-time") it is the blue box just above here and you'll see that it returns an object.
Answer: Try this.  Get rid of the "div" portion.
<code>$('.nav-next')
</code>

jQuery: selector question (dash)


How to find the covariance matrix of a polygon?

Question: Imagine you have a polygon defined by a set of coordinates $(x_1,y_1)...(x_n,y_n)$ and its centre of mass is at $(0,0)$. You can treat the polygon as a uniform distribution with a polygonal boundary.

I'm after a method that will find the covariance matrix of a polygon.
I suspect that the covariance matrix of a polygon is closely related to the second moment of area, but whether they are equivalent I'm not sure. The formulas found in the wikipedia article I linked seem (a guess here, it's not especially clear to me from the article) to refer to the rotational inertia around the x, y and z axes rather than the principal axes of the polygon.
(Incidentally, if anyone can point me to how to calculate the principal axes of a polygon, that would also be useful to me)
It is tempting to just perform PCA on the coordinates, but doing so runs into the issue that the coordinates are not necessarily evenly spread around the polygon, and are therefore not representative of the density of the polygon. An extreme example is the outline of North Dakota, whose polygon is defined by a large number of points following the Red river, plus only two more points defining the western edge of the state.
Comment: It works the other way: compute the inertial tensor and find its principal axes from that. The technique in your case involves Green's Theorem, which shows that the requisite integrals $$\mu_{k,l}(\mathcal{P})=\iint_{\mathcal{P}}x^ky^l\mathrm{d}x\mathrm{d}y$$ can be computed as contour integrals around $\partial\mathcal{P}$ of the one-form $\omega$ where $\mathrm{d}\omega=x^ky^l\mathrm{d}x\mathrm{d}y.$ Such forms are easy to find because any suitable linear combination of $x^ky^{l+1}\mathrm{d}x$ and $x^{k+1}y^l\mathrm{d}y$ will work. The contour integral is a sum of integrals over the edges.
Comment: You are correct that the usual term for "covariance matrix" is [moment of inertia](https://en.wikipedia.org/wiki/Moment_of_inertia) or *second* moment.  The principal axes are oriented in its eigendirections.  Running PCA on the coordinates is incorrect: it is tantamount to assuming all the mass is located at the vertices.  The most direct methods of computation of the barycenter--the *first* moment--are discussed in my post at https://gis.stackexchange.com/a/22744/664.  The second moments are computed in the same way with minor modifications. Special considerations are needed on the sphere.
Comment: @StephanKolassa I mean treating the polygon as a uniform bivariate probability density with polygonal boundary. Sure, you can sample points and the limit would be the same thing, but i'm looking for an a-priori method. The picture is just an illustration from paint that I used. The real world data I intend to use are the outlines of states and regions.
Comment: By "find", I assume simply sampling from the polygon, then calculating the covariance of the samples, is not what you have in mind?
Comment: Also, can you edit your post to include coordinates for your polygon, so people can play around with it?
Comment: @whuber Ok this is useful. So far the methods for finding the second moment of inertia assume that the object is already laid out on its principal axes. The objects I'm looking at don't have that. So I guess I'm looking for a way of computing the principal axes for a polygon before I find the second moment of inertia.
Answer: Let's do some analysis first.
Suppose within the polygon $\mathcal{P}$ its probability density is proportional function $p(x,y).$  Then the constant of proportionality is the inverse of the integral of $p$ over the polygon,
$$\mu_{0,0}(\mathcal{P})=\iint_{\mathcal P} p(x,y) \mathrm{d}x\,\mathrm{d}y.$$
The barycenter of the polygon is the point of average coordinates, computed as their first moments.  The first one is
$$\mu_{1,0}(\mathcal{P})=\frac{1}{\mu_{0,0}(\mathcal{P})} \iint_{\mathcal P} x\,p(x,y)\mathrm{d}x\,\mathrm{d}y.$$
The inertial tensor can be represented as the symmetric array of second moments computed after translating the polygon to put its barycenter at the origin: that is, the matrix of central second moments
$$\mu^\prime_{k,l}(\mathcal{P}) = \frac{1}{\mu_{0,0}(\mathcal{P})} \iint_{\mathcal P} \left(x - \mu_{1,0}(\mathcal{P})\right)^k\,\left(y - \mu_{0,1}(\mathcal{P})\right)^l\,p(x,y)\mathrm{d}x\,\mathrm{d}y$$
where $(k,l)$ range from $(2,0)$ to $(1,1)$ to $(0,2).$  The tensor itself--aka covariance matrix--is
$$I(\mathcal{P}) = \pmatrix{\mu^\prime_{2,0}(\mathcal{P})  & \mu^\prime_{1,1}(\mathcal{P}) \\ \mu^\prime_{1,1}(\mathcal{P})  &  \mu^\prime_{0,2}(\mathcal{P})}.$$
A PCA of $I(\mathcal{P})$ yields the principal axes of $\mathcal{P}:$ these are the unit eigenvectors scaled by their eigenvalues.

Next, let's work out how to do the calculations.  Because the polygon is presented as a sequence of vertices describing its oriented boundary $\partial\mathcal P,$ it is natural to invoke

Green's Theorem: $$\iint_{\mathcal{P}} \mathrm{d}\omega = \oint_{\partial\mathcal{P}}\omega$$ where $\omega = M(x,y)\mathrm{d}x + N(x,y)\mathrm{d}y$ is a one-form defined in a neighborhood of $\mathcal{P}$ and $$\mathrm{d}\omega = \left(\frac{\partial}{\partial x}N(x,y) - \frac{\partial}{\partial y}M(x,y)\right)\mathrm{d}x\,\mathrm{d}y.$$

For instance, with $\mathrm{d}\omega = x^k y^l \mathrm{d}x\mathrm{d}y$ and constant (i.e., uniform) density $p,$ we may (by inspection) select one of the many solutions, such as $$\omega(x,y) = \frac{-1}{l+1}x^k y^{l+1}\mathrm{d}x.$$
The point of this is that the contour integral follows the line segments determined by the sequence of vertices.  Any line segment from vertex $\mathbf{u}$ to vertex $\mathbf{v}$ can be parameterized by a real variable $t$ in the form
$$t \to \mathbf{u} + t\mathbf{w}$$
where $\mathbf{w} \propto \mathbf{v}-\mathbf{u}$ is the unit normal direction from $\mathbf{u}$ to $\mathbf{v}.$ The values of $t$ therefore range from $0$ to $|\mathbf{v}-\mathbf{u}|.$  Under this parameterization $x$ and $y$ are linear functions of $t$ and $\mathrm{d}x$ and $\mathrm{d}y$ are linear functions of $\mathrm{d}t.$  Thus the integrand of the contour integral over each edge becomes a polynomial function of $t,$ which is easily evaluated for small $k$ and $l.$

Implementing this analysis is as straightforward as coding its components.  At the lowest level we will need a function to integrate a polynomial one-form over a line segment.  Higher level functions will aggregate these to compute the raw and central moments to obtain the barycenter and inertial tensor, and finally we can operate on that tensor to find the principal axes (which are its scaled eigenvectors).  The R code below performs this work.  It makes no pretensions of efficiency: it is intended only to illustrate the practical application of the foregoing analysis.  Each function is straightforward and the naming conventions parallel those of the analysis.
Included in the code is a procedure to generate valid closed, simply connected, non-self-intersecting polygons (by randomly deforming points along a circle and including the starting vertex as its final point in order to create a closed loop).  Following this are a few statements to plot the polygon, display its vertices, adjoin the barycenter, and plot the principal axes in red (largest) and blue (smallest), creating a polygon-centric positively-oriented coordinate system.

#
# Integrate a monomial one-form x^k*y^l*dx along the line segment given as an
# origin, unit direction vector, and distance.
#
lintegrate <- function(k, l, origin, normal, distance) {
  # Binomial theorem expansion of (u + tw)^k
  expand <- function(k, u, w) {
    i <- seq_len(k+1)-1
    u^i * w^rev(i) * choose(k,i)
  }
  # Construction of the product of two polynomials times a constant.
  omega <- normal[1] * convolve(rev(expand(k, origin[1], normal[1])),
                                expand(l, origin[2], normal[2]),
                                type="open")
  # Integrate the resulting polynomial from 0 to `distance`.
  sum(omega * distance^seq_along(omega) / seq_along(omega))
}
#
# Integrate monomials along a piecewise linear path given as a sequence of
# (x,y) vertices.
#
cintegrate <- function(xy, k, l) {
  n <- dim(xy)[1]-1 # Number of edges
  sum(sapply(1:n, function(i) {
    dv <- xy[i+1,] - xy[i,]               # The direction vector
    lambda <- sum(dv * dv)
    if (isTRUE(all.equal(lambda, 0.0))) {
      0.0
    } else {
      lambda <- sqrt(lambda)              # Length of the direction vector
      -lintegrate(k, l+1, xy[i,], dv/lambda, lambda) / (l+1)
    }
  }))
}
#
# Compute moments of inertia.
#
inertia <- function(xy) {
  mass <- cintegrate(xy, 0, 0)
  barycenter = c(cintegrate(xy, 1, 0), cintegrate(xy, 0, 1)) / mass
  uv <- t(t(xy) - barycenter)   # Recenter the polygon to obtain central moments
  i <- matrix(0.0, 2, 2)
  i[1,1] <- cintegrate(uv, 2, 0)
  i[1,2] <- i[2,1] <- cintegrate(uv, 1, 1)
  i[2,2] <- cintegrate(uv, 0, 2)
  list(Mass=mass,
       Barycenter=barycenter,
       Inertia=i / mass)
}
#
# Find principal axes of an inertial tensor.
#
principal.axes <- function(i.xy) {
  obj <- eigen(i.xy)
  t(t(obj$vectors) * obj$values)
}
#
# Construct a polygon.
#
circle <- t(sapply(seq(0, 2*pi, length.out=11), function(a) c(cos(a), sin(a))))
set.seed(17)
radii <- (1 + rgamma(dim(circle)[1]-1, 3, 3))
radii <- c(radii, radii[1])  # Closes the loop
xy <- circle * radii
#
# Compute principal axes.
#
i.xy <- inertia(xy)
axes <- principal.axes(i.xy$Inertia)
sign <- sign(det(axes))
#
# Plot barycenter and principal axes.
#
plot(xy, bty="n", xaxt="n", yaxt="n", asp=1, xlab="x", ylab="y",
     main="A random polygon\nand its principal axes", cex.main=0.75)
polygon(xy, col="#e0e0e080")
arrows(rep(i.xy$Barycenter[1], 2),
       rep(i.xy$Barycenter[2], 2),
       -axes[1,] + i.xy$Barycenter[1],     # The -signs make the first axis ..
       -axes[2,]*sign + i.xy$Barycenter[2],# .. point to the right or down.
       length=0.1, angle=15, col=c("#e02020", "#4040c0"), lwd=2)
points(matrix(i.xy$Barycenter, 1, 2), pch=21, bg="#404040")

Comment: +1 Wow, this is a great answer!
Answer: Edit: Didn't notice that whuber had already answered. I'll leave this up as an example of another (perhaps less elegant) approach to the problem.
The covariance matrix
Let $(X,Y)$ be a random point from the uniform distribution on a polygon $P$ with area $A$. The covariance matrix is:
$$C = \begin{bmatrix} C_{XX} & C_{XY} \\ C_{XY} & C_{YY} \end{bmatrix}$$
where $C_{XX} = E[X^2]$ is the variance of $X$, $C_{YY} = E[Y^2]$ is the variance of $Y$, and $C_{XY} = E[XY]$ is the covariance between $X$ and $Y$. This assumes zero mean, since the polygon's center of mass is located at the origin. The uniform distribution assigns constant probability density $\frac{1}{A}$ to every point in $P$, so:
$$C_{XX} = \frac{1}{A} \underset{P}{\iint} x^2 dV \quad
C_{YY} = \frac{1}{A} \underset{P}{\iint} y^2 dV \quad
C_{XY} = \frac{1}{A} \underset{P}{\iint} x y dV
\tag{1}$$
Triangulation
Instead of trying to directly integrate over a complicated region like $P$, we can simplify the problem by partitioning $P$ into $n$ triangular subregions:
$$P = T_1 \cup \cdots \cup T_n$$
In your example, one possible partitioning looks like this:

There are various ways to produce a triangulation (see here). For example, you could compute the Delaunay triangulation of the vertices, then discard edges that fall outside $P$ (since it may be nonconvex as in the example).
Integrals over $P$ can then be split into sums of integrals over the triangles:
$$C_{XX} = \frac{1}{A} \sum_{i=1}^n \underset{T_i}{\iint} x^2 dV \quad
C_{YY} = \frac{1}{A} \sum_{i=1}^n \underset{T_i}{\iint} y^2 dV \quad
C_{XY} = \frac{1}{A} \sum_{i=1}^n \underset{T_i}{\iint} x y dV
\tag{2}$$
A triangle has nice, simple boundaries so these integrals are easier to evaluate.
Integrating over triangles
There are various ways to integrate over triangles. In this case, I used a trick that involves mapping a triangle to the unit square. Transforming to barycentric coordintes might be a better option.
Here are solutions for the integrals above, for an arbitrary triangle $T$ defined by vertices $(x_1,y_1), (x_2,y_2), (x_3,y_3)$. Let:
$$v_x = \left[ \begin{smallmatrix} x_1 \\ x_2 \\ x_3 \end{smallmatrix} \right]
\quad
v_y = \left[ \begin{smallmatrix} y_1 \\ y_2 \\ y_3 \end{smallmatrix} \right]
\quad
\vec{1} = \left[ \begin{smallmatrix} 1 \\ 1 \\ 1 \end{smallmatrix} \right]
\quad
L = \left[ \begin{smallmatrix} 1 & 0 & 0 \\ 1 & 1 & 0 \\ 1 & 1 & 1 \end{smallmatrix} \right]$$
Then:
$$\underset{T}{\iint} x^2 dV =
\frac{A}{6} \text{Tr}(v_x v_x^T L) \quad
\underset{T}{\iint} y^2 dV =
\frac{A}{6} \text{Tr}(v_y v_y^T L) \quad
\underset{T}{\iint} x y dV =
\frac{A}{12} (\vec{1}^T v_x v_y^T \vec{1} + v_x^T v_y)
\tag{3}$$
Putting everything together
Let $v_x^i$ and $v_y^i$ contain the x/y coordinates of the vertices for each triangle $T_i$, as above. Plug $(3)$ into $(2)$ for each triangle, noting that the area terms cancel out. This gives the solution:
$$C_{XX} =
\frac{1}{6} \sum_{i=1}^n \text{Tr} \big( v_x^i (v_x^i)^T L \big) \quad
C_{YY} =
\frac{1}{6} \sum_{i=1}^n \text{Tr} \big( v_y^i (v_y^i)^T L \big) \quad
C_{XY} =
\frac{1}{12} \sum_{i=1}^n \big( \vec{1}^T v_x^i (v_y^i)^T \vec{1} + (v_x^i)^T v_y^i \big)
\tag{4}$$
Principal axes
The principal axes are given by the eigenvectors of the covariance matrix $C$, just as in PCA. Unlike PCA, we have an analytic expression for $C$, rather than having to estimate it from sampled data points. Note that the vertices themselves are not a representative sample from the uniform distribution on $P$, so one can't simply take the sample covariance matrix of the vertices. But, $C$ *is* a relatively simple function of the vertices, as seen in $(4)$.
Comment: +1 This can be simplified by allowing *oriented* triangles, thereby eliminating the need for a proper triangulation.  Instead, you can just establish an arbitrary center $O$ and sum the (signed) values over the triangles $OP_iP_{i+1}:$ this is how it's often done because it's much less fussy.  It's easy to see that such a summation is essentially the same thing as applying Green's Theorem, because each term in the summation ultimately is a function of the edge $P_iP_{i+1}.$ This approach is illustrated in the "Area" section at http://www.quantdec.com/SYSEN597/GTKAV/section2/chapter_11.htm.
Comment: @whuber Interesting, thanks for pointing this out
Comment: Both of these answers are good, albeit a bit over my education level. Once I'm sure I fully understand them I'll try to figure out who gets the bounty.
Answer: This technical report provides various closed-form formulas for sets of 2D triangles and other primitives in 2D and 3D: https://inria.hal.science/inria-00327027/document
see also https://doc.cgal.org/latest/Principal_component_analysis/index.html
Pierre
Comment: Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer).


entityfieldquery, return several results

Question: I want get several entries of database. Im a entityfieldquery novice and I misunderstand how I do for get several results who contain certain fields (here, the fields that interest me are : delta, field_top_value and field_top_revision).

By example, I try this code :
$query = new EntityFieldQuery();
$query
    ->entityCondition('entity_id',$nid_device)
    ->entityCondition('entity_type','node');
$result = $query->execute();
dpm($result); //show just one only node...but I would like get the five entries, with field "delta, field_top_value and field_top_revision_id"

I "tried" to read the documentation, but my english reading isn't very good, and I don't find example for my case..
Thanks for help !
Comment: Is there any chance you can get someone who speaks English a bit better to help you re-phrase your question? It's really hard to understand what you're asking, but I'm almost positive there's an easy answer to this. Just need to know what you're asking exactly :)
Comment: sorry sorry x) with the entityfieldquery, I want get several results and no one only result (with my actually code)
And I don't understand how I can select some field in my entityFieldQuery (I would like select "delta", "field_top_value" and "field_top_revision" in my case)

As you can see (the screenshoot), Ill get five results.
Answer: The table in your screenshot is a field table. It stores field data for entities, for both multiple and single cardinality fields.
When you use an EntityFieldQuery, you're querying for entities, not fields. It wouldn't make sense for you to receive data for anything else.
The connection you need to encourage your brain to make is that the fields are attached to the entities. So, if you want to know about multiple field values for a node, you just need to go and get them:
// Keep track of the delta, you mentioned you wanted it...
$delta = 0;
foreach (field_get_items('node', $node, 'field_foo') as $item) {
  $field_value = $item['value'];

  $delta++;
}

If that isn't what you're looking for, then EntityFieldQuery probably isn't the tool for the job. If that's the case, and you're using MySQL as the storage backend for fields, I'd recommend manually querying the field_data_field_foo table to get what you're after directly.
Comment: @Clive I'm with you; there's a touch of ambiguity to _UI_ in a question that features a screenshot of phpMyAdmin and talks about editing entities!
Comment: I don't think its a good solution because the delta maybe modify by differents ways (another module by example..)
So, I think the delta don't start all time at 0 =)

But maybe im wrong !
Comment: Yep, you're wrong ;) the delta always starts at zero, assuming the field API had been used correctly
Comment: the delta isn't the weight ? if I set a weight equal to -1 via the ui, the delta in database will equal to -1 no ?
it's just curiosity =)
Comment: Sure, if you change database values manually, but why would you be doing that? That's what the API is for, so you don't end up unintentionally introducing inconsistencies in the data...before they're saved, field elements are run through `_field_filter_items()` to filter out empty items. That function returns them with `array_values()`, effectively resetting the array keys to zero-based again, so you can count on the delta always starting at zero :)
Comment: @user2137454 I think Clive may have slightly misunderstood that question (unless it's me!). If you edit an entity with a multi-value field, click _Show weights_, and set a weight to -1, that value's delta _won't_ be -1 when saved to the DB. (Clive describes how that's done in his comment.)
Comment: @Andy Yeah I was trying to make that last statement cover both scenarios (editing through phpmyadmin and editing through the ui). If done through the UI the delta will always be zero-basd
Answer: Your code is looking for a specific node with a specific nid. nid or entity_id is a unique key. Use different properties to query your entities.
Answer: Whenever you load data by entityfieldquery(), then it will return you everything in term of node.

Here entity_id from table and nid of node is same
vid parameter in node is same as revision_id in the table
delta specifies multiple value saved w.r.t a particular field e.g. a field category with table field_data_field_category has multiple values testcategory1 ,testcategory2.

Then it will saved in the database as delta 0 for testcategory1 and 1 for testcategory2.
And when it will be returned in node all values are assembled to show in the particular field w.r.t which it belongs in the form of array.
Also you can load the node data separately by node_load() function by passing entity_id/node id to load all data w.r.t that particular node in assembled mode.


How to use actionButton when leaving submitButton in flexdashboard?

Question: I would like the renderValueBox to be calculated when I hit a button, not automatically.
I tried using actionButton but it didn't work. I was also unable to use the submitButton function in flexdashboard, but it didn't work either.
I would like to do this using only Shiny::actionButton function (recommended by documentation). My code:
---
title: "Sum"
runtime: shiny
output:
  flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
theme: yeti
---

side{.sidebar}
-------------------------------------------

```{r}
library(tibble)
library(shiny)
library(shinyWidgets)
library(flexdashboard)
library(scales)
```

**Análise**

```{r}
autonumericInput(
inputId = "a",
value = 0,
label = "Value 1",
align = "center",
currencySymbol = "R$",
currencySymbolPlacement = "p",
decimalPlaces = 2,
digitGroupSeparator = ".",
decimalCharacter = ","
)

autonumericInput(
inputId = "b",
value = 0,
label = "Value 2",
align = "center",
currencySymbol = "R$",
currencySymbolPlacement = "p",
decimalPlaces = 2,
digitGroupSeparator = ".",
decimalCharacter = ","
)

actionButton("execute", "Calcule")
```

```{r}
f_1 <- function(a, b) {
  a + b
}
```

```{r}
reac <- eventReactive(input$execute, {
  x = tibble(
    a = input$a,
    b = input$b
  )
}, ignoreNULL = FALSE)

pred <- reactive({
  temp <- reac()
  f_1(
    a = input$a,
    b = input$b
  )
})
```

abc{}
--------------------------------------

###
```{r}
renderValueBox({
  valueBox(
    value = scales::dollar(x = round(x = pred(), digits = 4), prefix = "$ ", suffix = " dollar(s)", big.mark = ",", decimal.mark = "."),
    caption = "Sum",
    color = "steelblue",
    icon = "fa-plus"
  )
})
```

Answer: ---
title: "Sum"
runtime: shiny
output:
  flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
theme: yeti
---

side{.sidebar}
-------------------------------------------

```{r}
library(tibble)
library(shiny)
library(shinyWidgets)
library(flexdashboard)
library(scales)
```

**Análise**

```{r}
autonumericInput(
inputId = "a",
value = 0,
label = "Value 1",
align = "center",
currencySymbol = "R$",
currencySymbolPlacement = "p",
decimalPlaces = 2,
digitGroupSeparator = ".",
decimalCharacter = ","
)

autonumericInput(
inputId = "b",
value = 0,
label = "Value 2",
align = "center",
currencySymbol = "R$",
currencySymbolPlacement = "p",
decimalPlaces = 2,
digitGroupSeparator = ".",
decimalCharacter = ","
)

actionButton("execute", "Calcule")
```

abc{}
--------------------------------------

###
```{r}
renderValueBox({
    req(input$execute)
  valueBox(
    value = scales::dollar(x = round(x = isolate(input$a) + isolate(input$b), digits = 4), prefix = "$ ", suffix = " dollar(s)", big.mark = ",", decimal.mark = "."),
    caption = "Sum",
    color = "steelblue",
    icon = "fa-plus"
  )
})
```

The trick here is to use isolate() wrap your inputs, so these inputs will not trigger the update of the box and add the req(input$execute) to trigger the updates.


Simple question in number-theory

Question: $a, b, c$, and $d$ are integers. From exactly one of the equations $A, B, C, D, E$, one can deduce that $14$ divides $a \cdot b$. Which one?
A) $7a+8b=14c+28d$
B) $14a+28b=7c+8d$
C) $14a+8b=7c+28d$
D) $7a+28b=14c+8d$
E) $28a+8b=14c+7d$
I tried solving for $b$ in $x_1a+x_2b=x_3c+ x_4d$, then calculating $a\cdot b$ expressed by only $a$, hoping it would yield something. Otherwise I am clueless.
How can this be solved?
Comment: See if you can deduce, just from looking at the coefficients, whether $a$ is forced to be divisible by $2$ or $7$, and the same for $b$. Analogous example: in $9w+5x=15y+12z$, you can conclude that $x$ is a multiple of $3$. Do you see why?
Answer: Hint: in $7a+8b=14c+28d$, what divisors can we say for sure that $a$ has, if any? What about $b$?

Simple question in number-theory


Event of pulling a white ball in first draw is not independent of pulling a 2nd white ball

Question: You have two urns. Urn One contains 6 white balls and 2 black balls. Urn Two contains 7 white balls and 8 black balls.
You pick a random urn; you get Urn One with probability 2/3. You
pull out, sequentially with return, two white balls.
Show that for Urn One, the event of pulling out a white ball in the first draw is not independent
from the event of pulling out a white ball in the second draw without
return.
How do I tackle this problem. This isn't a homework question, I found it in a paper and it caught my attention.
Answer: If I'm understanding you well, the experiment only involves taking two random balls from Urn One.
To check if two probabilities are independent, you can use any definition of independence - they are equivalent. In this case, would just compute probability of the second ball being white if the first one has been white, and compare it the the probability of the second one being white if the first one has been black. If both events are independent, both conditional probabilities would be the same - that is, probability of the second event doesn't depend on whether the first has taken place.
If the first ball has been white, when you are going to take the second one there are 5 white balls and 2 black balls in the urn (there were 6 white balls at the beginning, but you removed one when you took the first ball).
P(second white|first white)=5/7
But if the first ball were black, there are 6 white balls and one black ball in the urn.
P(second white|first black)=6/7
Please notice that the event "first black" would be the same as "not first white".
Then, since P(second white|first white)<>P(second white|not first white), the events "second white" and "first white" are not independent.


How do I compare a rotating StringBuilder with a normal String in Java

Question: Here is the question: Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
A shift on s consists of moving the leftmost character of s to the rightmost position.
For example, if s = "abcde", then it will be "bcdea" after one shift.

Input: s = "abcde", goal = "cdeab"
Output: true
Input: s = "abcde", goal = "abced"
Output: false

Here is the code I wrote:
public static void main(String[] args) {

    System.out.println(rotateString("abcd","cdab"));

    }

public static boolean rotateString(String s, String goal) {
        Boolean flag = false;
        StringBuilder builder = new StringBuilder(s);

            for (int i=0;i<=s.length()-1;i++){
            if (builder.toString() == goal) {
                flag= true;
                break;
            }

            char temp = builder.charAt(builder.length()-1);
            for (int j=s.length()-1;j>=0;j--){

                if (j==0) continue;
                builder.setCharAt(j, builder.charAt(j-1));

            }
            builder.setCharAt(0,temp);
            }

        return flag;
    }

I don't know how the flag is still false I also tried debugging it, it shows value of builder.toString() and goal is same.
I was expecting the flag to be true but it always stays false.
Comment: You are not using the correct method to compare two strings in Java.  See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java
Comment: Actually that answer is not the best as you don't need to reference the Object method.  You really want "if(goal.equals(builder.toString())){"
Comment: It's perhaps worth pointing out that you don't need to rotate the StringBuilder at all - or even use a StringBuilder. You can efficiently compare regions of a String using [`regionMatches`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#regionMatches-int-java.lang.String-int-int-): just compare `s.regionMatches(0, goal, i, goal.length() - i) && s.regionMatches(i, goal, 0, s.length() - i)` (or something like that).
Answer: The reason the flag remains false even when builder.toString() and goal seem to have the same value is because you are using the "==" operator to compare the two strings.
In Java, the "==" operator checks for reference equality, not the content equality of strings.
You should do this way:
public static void main(String[] args) {

    System.out.println(rotateString("abcd", "cdab"));

}

public static boolean rotateString(String s, String goal) {
    Boolean flag = false;
    StringBuilder builder = new StringBuilder(s);

    for (int i = 0; i <= s.length() - 1; i++) {

        // Use equals() to compare strings.
        if (builder.toString().equals(goal)) {
            flag = true;
            break;
        }

        char temp = builder.charAt(builder.length() - 1);
        for (int j = s.length() - 1; j >= 0; j--) {

            if (j == 0)
                continue;
            builder.setCharAt(j, builder.charAt(j - 1));

        }
        builder.setCharAt(0, temp);
    }

    return flag;
}

Check this out: What is the difference between == and equals() in Java?
Answer: Since your question already has been answered, this is just to give you some more context.
In Java primitive types (like int, char, and double) can be compared using == no problem, but you generally will want to use the object's classInstance.equals() method for comparisons. This is because == returns true if and only if the objects being compared refer to the same memory address. The StringBuilder and String s are referring to different objects so they are different in the eyes of ==.
Think about it this way:
Let's make a class called Data as follows
public class Data {
    public int value;

    public Data(int value) {
        this.value = value;
    }
}

and let's add an override to Data's inherited .equals method (which it inherits from Object's)
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null || getClass() != obj.getClass()) {
        return false;
    }
    Data otherData = (Data) obj;
    return this.value == otherData.value;
}

Now if we run the following code:
public class Main {
    public static void main(String[] args) {
        Data data1 = new Data(0);
        Data data2 = new Data(0);

        boolean usingDoubleEquals = data1 == data2;
        boolean usingEqualsMethod = data1.equals(data2);
    }
}

We will get usingDoubleEquals = false and usingEqualsMethod = true. Note how we are using == in the return statement of the .equals method, but now it's evaluating two primitives: int value of the Data instance that is calling the method and int value of the Data instance that is being passed to the method.
Now, if you want to have even more fun, look into Java's String Pool for some exceptions to this rule that are unique to String types, and look up the Knuth–Morris–Pratt algorithm for a faster solution to the algorithm!
Comment: Hi, while your answer can be useful in a different context, it is not a direct anser to OP's question. I highly recommend reading [What is an acceptable answer?](https://meta.stackexchange.com/a/118694/387003) to learn about answering to-the-point when needed.
Comment: Thanks @Rakib, will do! New to the platform, so I gotta get up to speed.
Answer:
"... I don't know how the flag is still false I also tried debugging it, it shows value of builder.toString() and goal is same. ..."

Since you're comparing String values you'll need to use the equals method.
If you change the conditional statement to the following, the flag value returns true.
if (builder.toString().equals(goal))

Additionally, you can rotate the string much simpler.
public static boolean rotateString(String s, String goal) {
    StringBuilder builder = new StringBuilder(s);
    for (int count = 0; count < s.length(); count++) {
        builder.insert(builder.length(), builder.charAt(0));
        builder.deleteCharAt(0);
        if (builder.toString().equals(goal)) return true;
    }
    return false;
}


O que é long polling?

Question: Sempre quando pesquiso sobre "atualizações periódicas" e "notificações em tempo real" para aplicações Web, me deparo com o termo long polling.
Pelo pouco que entendi, parece se referir à atualizações periódicas feitas com AJAX, por  exemplo, com um setInterval do JavaScript,
Mas eu gostaria de saber mais aprofundadamente:

O que é long polling?
Usar long polling, para fins de manter uma aplicação atualizada, gasta muito recurso do servidor?
O termo long polling se aplica, na programação, apenas no contexto Web, AJAX, JavaScript e afins?
Comment: _Comet_ não tem a ver com _long polling_? Se sim, relacionada: https://pt.stackoverflow.com/q/32563/64969
Comment: Olha, eu não sei se irá ajudar muito, mas achei um site bem informativo sobre Long Polling, caso queria dar uma olhada, acho que encontrará oque procura:
imasters.com.br/artigo/23436/javascript/veja-como-o-long-polling-pode-te-ajudar-a-desenvolver-aplicacoes-em-tempo-real/?trace=1519021197&source=single
Comment: Apenas para associar a pergunta com outra que possui uma exemplificação https://pt.stackoverflow.com/questions/64080/qual-a-diferen%C3%A7a-entre-long-polling-e-o-normal-ajax
Answer: O Long Polling é uma técnica que simula uma indisponibilidade do servidor para manter uma conexão HTTP aberta. Essa técnica foi criada a partir da necessidade de comunicação em tempo real com um servidor web.
Contexto
Com a necessidade de se obter dados em tempo real do servidor, antigamente, os desenvolvedores criavam rotinas que enviavam uma requisição de tempos em tempos (como você mesmo descreveu) para "perguntar" se havia mudanças no modelo de dados da aplicação. Caso houvesse, a resposta da requisição conteria esses dados e então os mesmos poderiam ser atualizados na aplicação.
Esse tipo de rotina consumia uma quantidade grande de recursos quando a aplicação tendia a ser escalável (crescer de forma acelerada).
Vamos simular:
Imagine uma página que envia a cada 5 segundos uma requisição AJAX para um endpoint específico a fim de descobrir se houve mudanças de notificações, por exemplo.
Isso não se torna um problema com poucos usuários simultaneamente conectados. Mas digamos que a aplicação cresça de 10 usuários para 100 usuários conectados simultaneamente.
Vamos pensar maior. Imagine 1.000 usuários conectados.
Em 5 segundos, 1.000 requisições serão enviadas para o mesmo endpoint. Após mais 5 segundos, mais 1.000 e assim por diante.
Dependendo da arquitetura da aplicação, isso começa a sobrecarregar o servidor. Especificamente aquele endpoint.
"A solução" - Long Polling
Quando um cliente faz uma requisição o servidor simula uma indisponibilidade de dados e faz com que a requisição HTTP não tenha resposta enquanto não houver mudanças no modelo dos dados da aplicação.
Isso resolve o problema? Sim e não.
Sim, por que não é necessário ficar enviando requisições de tempos e tempos para checar essas mudanças.
Não, por que isso causou outro problema de desempenho. As requisições HTTP não foram especificadas para isso. Estender uma requisição HTTP pode "parecer" resolver o problema da quantidade de requisição enviadas ao mesmo endpoint, mas na verdade ela também consome muito recurso justamente por que está sendo usada inadequada.
A verdadeira solução - WebSockets
Escrevi um post sobre WebSockets e sua implementação em Java (mas tem implementações pra quase todas as linguagens atuais).
Sugiro que dê uma olhada por que lá eu explico com detalhes sobre a especificação e também dou exemplos práticos que da pra reproduzir ai na sua casa.
Comunicação em tempo real com WebSockets
Enfim..
Foque seu estudo em implementações WebSockets e seja feliz com a verdadeira comunicação em tempo real.

O que é long polling?


How to reference resource bundle?

Question: I am creating a Java GUI and want to use a resource bundle to store all of my sql queries in a .properties file. My class for loading the resource bundle is in a package in the src/main/java folder where the .properties file is in the src/main/resources folder:

I keep getting a missing resource exception. What am i missing?
Other similar questions say to add the folder to the build path, which it is:

This is my call to get the resource bundle:
Answer: Remove the .properties file extension to form a correct basename as indicated by the official ResourceBundle guide
ResourceBundle rb = ResourceBundle.getBundle("dominiondatabase");

Comment: Genius! Glossed over a tiny detail.

How to reference resource bundle?


Integral of $a^x\cos^axdx$

Question: I was given the following integral to solve:
$$\displaystyle \int 3^x\cos^3x dx$$
Writing $\cos^3x = \dfrac{\cos 3x + 3\cos x}{4}$ and $3^x = e^{x \ln 3}$, and using the standard result $\displaystyle \int e^{ax}\cos bxdx = \dfrac{e^{ax}(a\cos bx + b\sin bx)}{a^2 + b^2} + C$, I got,
$$\displaystyle \int 3^x\cos^3x dx = \dfrac{3^x}{4}\left[\dfrac{(\ln 3)\cos 3x + 3\sin 3x}{\sqrt{9 + (\ln 3)^2}} + \dfrac{(\ln 3)\cos x + \sin x}{\sqrt{1 + (\ln 3)^2}}\right] + C$$
My first thought upon seeing the integral was to generalise it as $I(a) = \displaystyle \int a^x (\cos x)^a dx$. But I realized that this does not lead to any easy solution and hence did it in the way mentioned. But can this antiderivative be evaluated in terms of elementary functions? And how could it be done?
Comment: When $a$ is an integer, an analogous formula to yours for $\cos^3(x)$ is true and using the same method, it can be written with elementary functions.
Comment: First you might need to find a formula for $\cos^a x$.
Comment: @Kenta Yeah, I thought about using Euler's Formula and representing $\cos^a x$ as a sum of cosines of multiples of $x$. But, as you pointed out, that works only for integers, and I don't know what to do if $a$ is any real number.
Answer: Wolfram Alpha evaluates the integral as $$\int a^x\cos^a(x)dx=$$
$$\left(\frac{1}{\ln(a)-ia}\right)(1+e^{2ix})a^x\cos^a(x) \operatorname{F}_{2,1}\left(1, \frac{1}{2}(a-i\ln(a)+2);\frac{1}{2}(-a-i\ln(a)+2);-e^{2ix}\right)+C$$
Where $\operatorname{F}_{2,1}(a,b;c;x)$ is the Hypergeometric Series. It seems that we can arrive at this answer by using the binomial theorem to expand $\cos^a(x)$ into a series of complex exponentials and then integrate term by term. The answer is a little messy but to my knowledge this is the only answer for $a\in\Bbb{R}$, unless there are some Hypergeometric identities that can reduce the $\operatorname{F}_{2,1}$ term to something nicer.

Integral of $a^x\cos^axdx$


Link doesn't appear in configuration

Question: I'm a Drupal 8 newbie writing my first module. I have a functioning yet basic module to learn from. What I'm working on now is for it to get a value from a configuration page. If I go directly to the URL for the custom config page it works. What I'm having trouble with is getting a link to my custom config page to display on the main config page. I've been through every tutorial I can find on this topic and I just can't figure out why it's not working. I hope you all can help. Here's my yml files.
itemrequest.info.yml
name: Item Request
type: module
description: Allow the public to request items for the District to purchase.
package: YCFLD
core: 8.x
configure: itemrequest.settings

itemrequest.routing.yml
itemrequest.itemrequest:
  path: '/itemrequest'
  defaults:
    _controller: '\Drupal\itemrequest\Controller\ItemrequestController::itemrequestpage'
    _title: 'Request an Item'
  requirements:
    _permission: 'access content'
itemrequest.settings:
  path: '/admin/config/itemrequest/settings'
  defaults:
    _form: '\Drupal\itemrequest\Form\ItemrequestForm'
    _title: 'Item Request'
  requirements:
    _permission: 'access administration pages'
  options:
    _admin_route: TRUE

itemrequest.links.menu.yml
itemrequest.settings:
  title: 'Item Request Form'
  description: 'Settings for Item Request form"
  route_name: itemrequest.settings
  parent: system.admin_config

Do see anything wrong in the .yml files? Is there anything I can do to troubleshoot this?
Comment: A don't see a `menu_name` in your itemrequest.links.menu.yml file specifying the menu it appears in.
Comment: I got it working and I didn't need a menu_name.
Answer: system.admin_config is the top-level page at /admin/config. Normally you would not put your configuration link there - you would instead put it into one of the blocks that appear on that page. For example, the "People" block would be user.admin_link, so if you want your config under "People" use user.admin_link as your parent. Or if you want to put your configuration link under the "Development" block use system.admin_config_development as your parent.
If you want to create your own top-level block then you need to do things a little differently - see the core user module and how it defines user.admin_link in user.links.menu.yml and user.routing.yml - this shows how a new block is created on the config page. You can then put your configuration link in that new block.
Comment: Changing system.admin_config to system.admin_config_development did the trick. Thank you! Now I'm going to see if I can figure out how to create my own top-level block.

Link doesn't appear in configuration


FreeBSD equivalent for Linux configuration of OpenVPN

Question: I'm following a tutorial (https://snikt.net/blog/2016/12/01/openvpn-over-https/) regarding OpenVPN via SSL on Linux machines.
At one point it states:

We modify /etc/default/openvpn to automatically enable this tunnel
  when Openvpn starts:
AUTOSTART="ssl"

Can anyone tell me what the equivalent of this on FreeBSD 11 is?
Answer: You have to add the next lines to the /etc/rc.conf:
openvpn_enable="YES"
openvpn_if="tun"
openvpn_configfile="/usr/local/etc/openvpn/server.conf"
openvpn_dir="/usr/local/etc/openvpn"

Then you have to start the service:
service openvpn start

If you want to restart service after some edits make to the config, you have to run
service openvpn restart

Comment: This I did and everything works. I'm just wondering if my traffic now really is going over HTTPS in order to obscure the usage of OpenVPN, pretending to just use SSL via HTTPS.
Comment: Apache and nginx `httpd` do not need `openvpn` to establish https connections, they have all functionality bundled already
Comment: You can also monitor the traffic with `tcpdump -v -i ` to verify your settings.


How to access data that an offset of a register in gdb with disp?

Question: How can I access a value such as 0x14(%edx) using the display command instead of examine?
Just like the question here, except I'd like to use disp so that the displayed value is updated as I step through the program:
How to access data that an offset of a register in gdb?
Answer: Like this:
<code>(gdb) display *(int*)(0x14 + $edx)
</code>


Como incrementar chaves primárias e estrangeiras no PostgreSQL usando SERIAL

Question: Não consigo incrementar automaticamente as chaves primárias e estrangeiras das minhas tabelas usando SERIAL, pois acontece erro e sempre tenho que inserir o valor das chaves.
<code>create table cliente(
    cd_cliente serial primary key not null,
    nome varchar(50),
    endereco varchar(50),
    cidade varchar(50)
)

create table telefone(
    cd_telefone serial primary key not null,
    cd_cliente serial,
    numero_telefone varchar(20),
    foreign key(cd_cliente) references cliente (cd_cliente)
)

insert into cliente
values
(1, 'Pessoa1', 'Algum lugar', 'Cidade'),
(2, 'Pesso2', 'Algum lugar', 'Cidade'),
(3, 'Pessoa3', 'Algum lugar', 'Cidade')

insert into telefone
values
(1, 1, '01 1111-1111'),
(2, 2, '02 2222-2222'),
(3, 3, '03 3333-3333')
</code>
Comment: Se  você definiu o campo `cd_telefone`como `serial` então não informe um valor para este campo ou informe `DEFAULT` para que seja considerado o valor gerado pela `sequence`. Como já foi dito não tem sentido definir `telefone.cd_cliente` como `serial`já que você sempre deve informar a qual ciente está associado o telefone.
Answer: Na tabela 'cliente' deveria funcionar, na tabela 'telefone' acho que está errado classificar cd_cliente como serial, deveria ser inteiro, afinal é o código do cliente que deve constar do INSERT, não deve ser um número gerado automaticamente.


My Chords Don't Fit Any Scale, What Do I Do?

Question: I made a chord progression of Gm, F#aug, A#/F, E7b5, and then Gm, F#aug, A#, and finally C7. I thought it has a rather "spooky" sound to it but I realized it didn't fit any scales. I first I thought it was G minor, but obviously not. If I use Gm natural, no F#, and I use Gm melodic or harmonic no F. Then I thought maybe C blues. Nope. Even more "outlandish" Scales don't have the notes E F F# G, like double harmonic, overtone, and neapolitan. So when stuck in a scenario like this, which scale should I choose? I'm not looking for an opinion based answer, just some pointers on the best-course-of-action for something like this, no matter what non-related chords I'm dealing with.
Comment: It looks very much like you're pulling F major with a chromatic bass, the more so that B♭ (not A♯) is being held through the entire progression. You aren't stating F major outright, but that isn't necessary to imply a tonic. A chromatic line doesn't necessarily mean that you are slipping far from a key.
Comment: @Patrx2: right, though I don't feel this points to F as the tonic – the g-minor environment is too strong, even the C₇ has more plagal than dominant character. I'd thus label it as g-dorian (which, as a scale, of course contains the same notes as F-ionian).
Comment: @leftaroundabout, it depends to a certain extant on the rhythm, but, if you allow that the augmented chord is best described as running a chromatic passing tone between Gm and B♭, then a pair of ii-IV-V7 (or substitute) phrases is what you are getting here, and _those_ are fairly classic half cadences. The G environment isn't very strong at all compared compared to the F inference (in fact, it's really quite weak). I'll reiterate - you don't need to state the tonic explicitly to imply a key strongly, if the progression is strong and conventional within that key.
Comment: @Patrx2: it sure depends on the rhythm etc., but I hear an implied gm chord throughout pretty much that whole chord sequence: B♭ and D are in literally held in all the chords save the C₇, and the G itself is merely silent because that voice does the chromatic descent.
Comment: @leftaroundabout, it's the combination of root progression and the descent away from G that vitiate G's claims. With a different inversion of the voices, you would have a classic 6-5 appoggiatura to a B♭ chord, thus you can make at least as valid a claim that B♭ (which is present as an inverted pedal throughout, including over C) and D are implying a missing fifth, F. The dominant substitution in the first phrase reinforces this by emphasising the leading tone and undercutting G again (this time with G♯ rather than G♭) _in a different voice_. Thus G is not merely silent, it is supplanted.
Comment: @Patrx2 yeah, that G♯ in the E⁷♭₅ certainly messes up my interpretation. Actually I have my doubts if this _is_ a G♯, since it seems to resolve downwards to G. But the diminished fourth E-A♭ doesn't make a lot of sense, either. This can't be settled without knowing the actual individual voices.
Answer: There are two false assumptions you have about this progression and music in general. The first is that there has to be a scale to fit a given progression. This is not the case at all and a lot of more complex pieces of music use many different scales especially as the harmony weaves between different ideas. One example that has a similar progression is the verses of Spanish Castle Magic which the melody comes from the progression itself rather then a scale.

Second, these chords are very related and make a nice descending chromatic line with a large amount of common tones between the chords. A good chunk of the confusion is due to how you notated the chords as how they are named obscure the nature of this progression.  If you instead call the progressions a Gm, Gb+ Bb/F  E7b5 and Gm, Gb+, Bb, C7 you can see what is going on much better.
<code>X:1
L:1/4
M:
K:F
V:1 clef=treble
"Gm"[G _B d] "Gb+"[_G _B d] "Bb/F"[F _B d] "E7b5"[E ^G _B d]|"Gm"[G _B d] "Gb+"[_G _B d] "Bb"[F _B d] "C7"[c E G _B]||
%
</code>
As you can see, in the firs progression you always have a Bb and D in your chords and in your second progression. I can very much see this as being in one of the modes of F major as the only two notes  outside of it are Gb and G# and the E7b5 and C7 will pull you towards an F tonic if you let them resolve.
Comment: I've heard this called a "line cliché" (the descending pattern). And another great example (in the exact same key) is "While my Guitar Gently Weeps" (George Harrison).


como usar exatamente o sleep e wakeup no python3?(multithread)

Question: Olá estou tentando fazer em python3 o famoso problema do produtor/consumidor, transcrevendo do meu livro de S.O. ,porem venho enfrentando um problema , aparentemente o wait() so  bota pra dormir uma vez no loop ou algo do tipo, pois simplesmente depois da primeira vez os thread correm soltos.
código:
# -*- coding: utf-8 -*-
import threading
acordar_dormir_produtor = threading.Event()
acordar_dormir_consumidor = threading.Event()
bufer = 0
bufer_max = 10

def consumidor():
    global acordar_dormir_produtor
    global acordar_dormir_consumidor
    global bufer
    global bufer_max
    while True:
        if bufer == 0:
            print ("consumidor dormindo")
            acordar_dormir_consumidor.wait()
            print ("consumidor acordou")
        bufer = bufer - 1
        if bufer == (bufer_max - 1):
            print ("distracando produtor")
            acordar_dormir_produtor.set()
            acordar_dormir_produtor.clear()
        print ("consumidor",bufer)

def produtor():
    global acordar_dormir_produtor
    global acordar_dormir_consumidor
    global bufer
    global bufer_max
    while True:
        if bufer == bufer_max:
            print ("produtor dormindo")
            acordar_dormir_produtor.wait()
            print ("produtor acordou")
        bufer = bufer + 1
        if bufer == 1:
            print ("distracando consumidor")
            acordar_dormir_consumidor.set()
            acordar_dormir_produtor.clear()
        print ("produtor ",bufer)
print (bufer)
b = threading.Thread(target=produtor)
a = threading.Thread(target=consumidor)
a.start()
b.start()
#while True:
   # time.sleep(3)
   # print (bufer)

como fazer isso? tem outro objeto  já que o Event não funciona?
tentei o conditions sem sucesso(na verdade nem chegou a executar)
Answer: Usar uma  Queue como o drgarcia sugere é uma solução de mais alto nível para um problema real.
A sua situação no entanto é didática - e isso aqui é o que eu acho que está dando errado:  você chama o set e o clear  do Event em seguida um do outro. A implementação do threading do Python não é algo que usa assinatura de eventos - antes é feita por polling - ou seja, de vez em quando a execução vai passar para a thread que está esperando em um event.wait e ver se o seu estado mudou - mas essa verificação não é desencadeada automaticamente pela chamada ao Event.set - então vocẽ teria que dar muita (mas muita mesmo)  sorte para a thread ser chamada exatamente entre a sua chaamda  a set e a chamada ao clear em seguida.
Para o que você fez funcionar, ponha a chamada ao clear imediatamente antes  da chamada ao wait na thread que vai fazer o wait (e não na thread que vai mandar o  set):
...
def consumidor():
   ...
   acordar_dormir_consumidor.clear()
   acordar_dormir_consumidor.wait()

...
def produtor():
    ...
    acordar_dormir_consumidor.set()

Answer: Você pode usar a classe Thread e a classe Queue (que é thread-safe).
Para dormir você pode usar o time.sleep.
# -*- coding: utf-8 -*-
from threading import Thread
import time
import random
from queue import Queue

queue = Queue(10)

class ProducerThread(Thread):
    def run(self):
        nums = range(5)
        global queue
        while True:
            num = random.choice(nums)
            queue.put(num)
            print("Produced", num)
            time.sleep(random.random())

class ConsumerThread(Thread):
    def run(self):
        global queue
        while True:
            num = queue.get()
            queue.task_done()
            print("Consumed", num)
            time.sleep(random.random())

ProducerThread().start()
ConsumerThread().start()

O Producer alimenta o buffer no método Queue.put() enquanto que o Consumer lê o buffer através do método Queue.get() (e sinalize que a leitura do buffer foi com sucesso através do método Queue.task_done()). Após cada um fazer sua tarefa, passa a "dormir" por um tempo randômico.
O método Queue.get() espera por um valor na fila, então independente de quem começar a trabalhar primeiro, o consumidor só vai dar sequência quando tiver algo na fila para ser consumidor.
Caso o exemplo necessite ser mais didático, você pode usar a classe threading.Condition:
# -*- coding: utf-8 -*-
from threading import Thread, Condition
import time
import random

queue = []
MAX_NUM = 10
condition = Condition()

class ProducerThread(Thread):
    def run(self):
        nums = range(5)
        global queue
        while True:
            condition.acquire()
            if len(queue) == MAX_NUM:
                print("Queue full, producer is waiting")
                condition.wait()
                print("Space in queue, Consumer notified the producer")
            num = random.choice(nums)
            queue.append(num)
            print("Produced", num)
            condition.notify()
            condition.release()
            time.sleep(random.random())

class ConsumerThread(Thread):
    def run(self):
        global queue
        while True:
            condition.acquire()
            if not queue:
                print("Nothing in queue, consumer is waiting")
                condition.wait()
                print("Producer added something to queue and notified the consumer")
            num = queue.pop(0)
            print("Consumed", num)
            condition.notify()
            condition.release()
            time.sleep(random.random())

ProducerThread().start()
ConsumerThread().start()

Com o condition você pode fazer o "lock" para controlar quando consumidor e o produtor devem esperar e quando devem trabalhar.
Sendo objetivo na resposta da sua pergunta, o condition.wait() faz a thread dormir aguardando o condition.notify() que funciona como o "wakeup".
Referência: http://agiliq.com/blog/2013/10/producer-consumer-problem-in-python/


Elastic Search Sub list filters

Question: I have an elastic document, structured [simplified] like this:
{
 "Contest": {
   "Name": "Room1",
   "Participants": [
   {
    "PlayerID": "tester1",
    "Score": "30"
  },
  {
      "PlayerID": "tester2",
      "Score": "40"
    },
    {
      "PlayerID": "tester3",
      "Score": "10"
    }
  ]
}

}
How can I query this and get back a result, of the Contest, and only the Participant object with a Score of over 35?
When I query the object, and use a filter, I get back the entire list of participants, which is not what I need; I need a list only of the objects meeting the search criteria. Do I have to restructure with nested documents?
Answer: Use inner_hits  like :
{
"query": {
  "nested": {
     "path": "Participants",
     "query": {
      "range": {
         "Participants.score": {
            "from": 35
         }
      }
     },
     "inner_hits" : {}
     }
   }
 }

Fetch required results from inner_hits.
Comment: Thank you, that is what I had found, but just as a note: The output denormalize the structure, which I find to be less than ideal.
Comment: Did you find any other solution that does not denormalise the structure?

Elastic Search Sub list filters


Shopping cart views are not show product links

Question: I am using Commerce Kickstart V t-7.x-2.2
I realized the next: When I add a new product and I add to cart it, not show the link "view product" . But when I edit that product and modify something on product variation and save again. The link "view product" appears in Shopping cart.
Somebody could tell me what is happening? Is it a bug of the commerce kickstart ? or Am I doing something wrong?
Any help will be highly appreciated. Thanks.
Answer: Try to edit the shopping cart views, go to
admin/structure/views/view/commerce_cart_form/edit/default
On the Fields Setting Make sure there is Display path field, exclude from display. see the Global: Custom text, and you must inlcude the Dsiplay path field token there


vlan switch port modes

Question: I have a powerconnect 5548p switch and Linksys 4410N wireless access point.
I would like to host two SSIDs on the access point--one going to VLAN1 (default VLAN) and the other going to a separate VLAN (VLAN10).
On the switch port that the access point connects to--should it be trunk mode for VLAN1 and VLAN10, or general mode with VLAN1 untagged and VLAN10 tagged? Or something else entirely?
Answer: The port connected to the AP should be a trunkport, meaning that it should tag all packets with their respective VLAN ids.
Comment: I ended up using the traditional trunk mode. I removed all the VLANs from the trunked port, added the two I wanted, and disabled the default (untagged) VLAN option.

On the access point, I enabled VLANing, assigned the management VLAN to the same VLAN where the office DHCP server is, and created two SSIDs and assigned each to its own VLAN.
Answer: Your second option is correct... it should be a "general" port with VLAN1 untagged and VLAN10 as tagged.
Comment: What value does general mode add over a simple trunk with native vlan 1?
Comment: I'm fairly certain that "trunk" mode on a Dell switch only allows tagged traffic, whereas "general" is basically a trunk that also allows you to set the native vlan for the untagged traffic.
Comment: From what I've read in the switch's manual, "trunk" mode just passes packets along without adding or removing vlan tags. I wasn't sure about "general" mode since I hadn't encountered it before.
Comment: @SpacemanSpiff, I still believe you can configure Vlan1 as untagged on a real trunk port, since it's the native vlan... see [Zypher's answer here](http://serverfault.com/questions/98307/set-802-1q-tagged-port-on-vlan1-on-dell-powerconnect-switch#link-post-222240)
Comment: http://en.community.dell.com/support-forums/network-switches/f/866/t/19335968.aspx

vlan switch port modes


Plotly scatter plot divided by month and year

Question: I'm trying to plot a scatter plot with plotly express. I have a dataset of jobs, that has a column called ['Posting Updated']. I want to plot the year that the job was posted against the count with information given on which month is was posted in. Maybe color or size? I can't seem to set my data up in a way where I can do this.
Has anyone got any insight on how I could do this?
Many thanks in advance
Big Love
df['Posting Updated'] = pd.to_datetime(df['Posting Updated'])
years  = df.groupby(df['Posting Updated'].dt.year)['Job ID'].count()
months = df.groupby(df['Posting Updated'].dt.month)['Job ID'].count()
years_df = pd.DataFrame(years)
months_df = pd.DataFrame(months)
job_growth = px.scatter(years_df, x = years_df.index,
                    size = 'Job ID', color = months_df.index)

Answer: Since what you want to achieve is a bit vague, I understood from your question that you want to express the magnitude of the frequency as a scatter plot with the year on the x-axis and the month on the y-axis. For the data used in the graph, I created 200 pieces of year and month data using random numbers as appropriate, and then grouped them by year and month to calculate the frequency. The x- and y-axes of the graphs were set to only the necessary scales.
import pandas as pd
import plotly.express as px
import numpy as np
import random

df = pd.DataFrame({'JOb ID':['{}'.format(x) for x in np.arange(1000,1200)],
                   'Posting Updated': random.choices(pd.date_range('2016-01-01', '2021-01-01',freq='1d'), k=200),
                  'value':[1]*200})

df['year'] = df['Posting Updated'].dt.year
df['month'] = df['Posting Updated'].dt.month
dfs = df.groupby(['year','month']).size().to_frame('value')
dfs.reset_index(inplace=True)

fig = px.scatter(dfs, x='year', y='month',size='value', color='value')

fig.update_yaxes(tickvals=np.arange(1,13))
fig.update_xaxes(tickvals=dfs.year.unique())
fig.show()


Plotting residuals for non-linear models created with bbmle package

Question: i'm trying to get the residual plots for a non-linear model i built with bbmle, but have no idea how to approach this task. The bble package has some notes on residuals but no way of plotting something like a histogram. Any help would be greatly appreciated
Answer: The residuals() function seems to work.  And you can then do whatever you want - histogram, qqplot, scatterplot of residuals against predicted values (predict() also has a method).  For example:
<code>set.seed(1002)
lymax <- c(0,2)
lhalf <- 0
x <- runif(200)
g <- factor(rep(c("a","b"),each=100))
y <- rnbinom(200,mu=exp(lymax[g])/(1+x/exp(lhalf)),size=2)
dat <- data.frame(y,g,x)

fit3 <- mle2(y~dnbinom(mu=exp(lymax)/(1+x/exp(lhalf)),size=exp(logk)),
    parameters=list(lymax~g),
    start=list(lymax=0,lhalf=0,logk=0),
    data=dat)

par(mfrow=c(2,2))
hist(residuals(fit3))
qqnorm(residuals(fit3))
hist(residuals(fit3, type="response"))
qqnorm(residuals(fit3, type="response"))
</code>

Or have I missed the point?
Comment: Thanks Peter. This was exactly what I was hoping to do for my models


Interpreting variance in cross-validated classifiers

Question: Set-up
I have a chemometric dataset with a hierarchical structure: 18 patients with ~250 spectra per patient. I want to compare some binary classifiers on this dataset which has 9 patients with and 9 without a disease.
For hyper-parameter optimisation I am performing leave-two-patients-out cross-validation (one with and one without the disease). I chose this because it seemed sensible to have positive and negative cases in the validation set. I'm simply using accuracy to compare performance.
The problem
A curious thing occurs with this set-up. There is a very high variance in the accuracy of a classifier depending on which two patients are held aside to assess the accuracy. The range of accuracy on the validation sets varies from 92% to as low as 12% in one instance. Even aside these two extremes, there is considerable variation in results.
My interpretations
1.) Perhaps there is naturally a significant degree of inter-patient variation in such samples making it difficult to generalise the performance to new patients. The sample size may be too small to capture this natural variation - i.e. it's not representative of the population.
2.) Perhaps there is a labelling error (especially the case with the 12% accuracy). I will ask, but it might be some time before an expert can have a look, so I want to be sure it's worth the effort first.
3.) I have heard of model instability, but I'm not sure I understand it. Is this a manifestation of it? Model instability implies to me that instability (i.e. high variance) is caused by the model - but would it not be the case that the instability is a feature of the data rather than the models? Or at least an interaction of the models and the data?
The classifiers I want to compare are PCA-LDA, SVM and a CNN. I plan to perform an external CV to assess which performs best (so the entire process will be nested CV). The instability is present, and roughly the same, for all these models.
4.) I've done something silly.
Comment: Can you please explain why you obtain an accuracy just from teh two samples left out? In LOO, the accuracy is computed over *all* symples cyclically left out.
Comment: I obtain repeated measures of the accuracy, one for every pair of patients left out. From this I can obtain the average accuracy and its standard deviation. It is the variance of these accuracies that has me bemused.
Comment: I don't see the benefit in leave two out vs leave one out. There isn't need to ensure each fold has a positive and a negative case as each record eventually gets tested anyway in LOO
Comment: You only have k=9 CV measurements. As the variance of the mean value goes with $1/\sqrt{k}$, I would expect a large variance from such a small sample.
Comment: @astel I suppose that is true so long as I'm using accuracy - if I were using sensitivity/specificity then the lack of pos or neg cases would be problematic?
Comment: @cdalitz Is that variance a well established result? I see it makes sense to have a large variance but I guess the range of 92% to 12% was more than I was expecting.
Comment: It doesn't matter the metric you are using. In the end you are going to have 18 records that you have classified and calculate your metric on those 18 records, which test set they belonged to is irrelevant
Comment: @n-blake "I obtain repeated measures of the accuracy, ..." What do you mean by this? You should not obtain repeated measures of the accuracy, but each sample left out can only yield 1 or 0, where 1 means correct classification and 0 means incorrect classification. How do you obtain values like 92% or 12%????
Comment: @cdalitz each sample (patient) had ~250 spectra taken and it is these spectra that are being classified, so one patient, belonging to only one class, has ~250 measures of disease or no disease.
Comment: Hm, I would have thought that the 250 spectra are the features that are used as predictors for the two classes desease and no desease. If the spectra are the classes (250 different classes?), what are the features used to predict them?
Comment: Each spectra has an intensity on the y axis and (binned) wavenumber on the x axis. There are about 900 bins, which are the features. The goal is to be able to classify by spectrum. It's a somewhat similar set-up in [this paper](https://www.sciencedirect.com/science/article/pii/S1572100019301620?casa_token=gmwXh1oHkeMAAAAA:hrYI6KuNBx-yMn1CKaQ8oayPf2Kd_ZF_kwRltcVJY-up00-UQk6aC9hIIL6hO3hvaJqNpixRZ80).
Answer: Some issues to consider here:
First, the number of cases is very small, regardless of whether your sample is representative of the population. That leads to:
Second, your CV is necessarily something close to leave-one-out CV (in this case, one per group), which is known to have high variance. See Section 5.1.4 of An Introduction to Statistical Learning. With your approach, you only have a total of 81 models (9*9) among which to average.
Third, accuracy is a poor measure of model performance.
So try broader approaches for hyperparameter choices, like multiply repeated 5-fold CV (without necessarily limiting yourself to equal numbers of each class) or bootstrapping. During CV or bootstrapping, use a proper scoring rule like log-loss (equivalent to the logistic regression maximum-likelihood deviance criterion) or the Brier score (the classification equivalent of mean-square error) for evaluating performance.
You will still be limited by the small number of cases and potential un-representativeness of your sample. But those approaches probably have a better chance of working with your present data set.
Comment: The statement that LOO CV is known to have high variance is either wrong or at least not categorically correct. See https://stats.stackexchange.com/questions/61783/bias-and-variance-in-leave-one-out-vs-k-fold-cross-validation
Comment: Lot's to consider, thanks. I've never come across things like the Brier score in the medical literature. I'll look into it and get back with what I've found.
Comment: @NBlake take a look at the [resources provided by Frank Harrell](https://hbiostat.org/), a well respected expert on biostatistics. He freely provides pdfs of course notes, and has written a comprehensive text on Regression Modeling Strategies. See the links from that page. Those should give you a good start on important statistical considerations that are too often glossed over in the biomedical literature.