Não consigo escrever uma contagem de números específicos dentro de uma lista

Question: Olá, boa tarde a todos. Sou estudante na linguagem Python e estou com dificuldades em um exercício.
Desenvolvi o código que me retorna tranquilamente uma lista com meus números inseridos na lista, mas não consigo por exemplo, contabilizar quantas vezes o número 5 foi digitado. Alguem por favor, poderia me ajudar?
numeros = []
cont = 0
while True:
    num = input('Digite um número: ')
    while not num.isnumeric():
        print('Opção inválida')
        num = input('Digite um número: ')
    else:
        numeros += num
        cont += 1
    esc = input('Deseja continuar?[S/N]: ')
    while not esc.isalpha():
        print('Opção inválida.')
        esc = input('Deseja continuar?[S/N]: ')
    else:
        if esc in 'Ss':
            print('', end='')
        elif esc in 'Nn':
            break
        else:
            print('Por favor, use apenas S ou s, N ou n, tente novamente.')
            esc = input('Deseja continuar?[S/N]: ')
print(numeros)
print(numeros.count(5))

Se o código aparecer meio bagunçado aqui por conta da formatação do site que ainda não domino, me perdoem.
Answer: Para simplificar a explicação vou gerar o mesmo problema em um código menor.
#MESMO PROBLEMA EM UM CÓDIGO MENOR
numeros = list()
for i in range(5):
    num = input('Digite um número: ')
    numeros.append(num)
print(numeros)
print(numeros.count(5))

O que esse código faz é ler 5 entrada de dados, no caso a gente pede 5 números, mas pra facilitar a explicação e ficar mais fácil de ler eu não vou fiz testes com a entrada pra ver se é realmente um número.
O que acontece é o seguinte, a função input(), sempre vai retorna uma string. Ou seja, quando você adicionar o número na lista ele vai ser adicionado como string a não ser que você converta o número usando a função int() ou float(). Como no exemplo abaixo:
numeros = list()
for i in range(5):
    num = input('Digite um número: ')
    numeros.append(int(num)) # int(num) -> transforma a `string` em inteiro.
print(numeros)
print(numeros.count(5))

Answer: Percebi que sua lista é formada por strings. Desta forma, você deve contar a string de 5, ou seja contar quantas vezes aparece a string "5". Portanto, a única coisa que você tem que mexer é na última linha do seu código. Neste caso você deve substituir 5 por "5".
Então o código ficaria:
numeros = []
cont = 0
while True:
    num = input('Digite um número: ')
    while not num.isnumeric():
        print('Opção inválida')
        num = input('Digite um número: ')
    else:
        numeros += num
        cont += 1
    esc = input('Deseja continuar?[S/N]: ')
    while not esc.isalpha():
        print('Opção inválida.')
        esc = input('Deseja continuar?[S/N]: ')
    else:
        if esc in 'Ss':
            print('', end='')
        elif esc in 'Nn':
            break
        else:
            print('Por favor, use apenas S ou s, N ou n, tente novamente.')
            esc = input('Deseja continuar?[S/N]: ')
print(numeros)
print(numeros.count('5'))

Agora, se sua intenção for montar uma lista de números e contar um valor numérico específico, você pode utilizar o seguinte código.

cont = 0
numeros = []
while True:
    cont += 1
    while True:
        try:
            n = int(input(f'Digite o {cont}º número: '))
            break
        except ValueError:
            print('Digite apenas número inteiros!')
    numeros.append(n)

    resp = input('Desejas continuar? [S/N] ').upper()
    while (len(resp) != 1) or (resp not in 'NS'):
        print('Valor INVÁLIDO! Digite apenas ')
        resp = input('Desejas continuar? [S/N] ').upper()
    if resp == 'N':
        print(f'A lista gerada foi: {numeros}')
        v = int(input('Desejas contar qual valor da lista? '))
        print(f'O valor "{v}" aparece {numeros.count(v)} vezes na lista!')
        break

Observe, que este último código monta uma lista de números e conta um valor numérico especificado pelo usuário.
Comment: Fico muito feliz pela resposta de cada um até agora, é de grande valia o que fazem nesse site. Agora posso continuar meus estudos sem precisar pular esse exercício e compreendendo melhor a lógica da promamação em python.


Links in sidebar to link to search results

Question: I have a view in sidebar showing a taxonomy Category. There is a view on /search-page that displays search using Search API view and exposed filters. I want the sidebar Categories to link to the search page with filtered category (/search-page?filter_category[]=13)
What would be a proper way of doing this?
I tried to do rewrite on the Name field in sidebar view.

"Rewrite the output of this field"
rewrite Text to [name]
checked the "Output this field as a link"
set Link Path to search-page?field_[machine_name]%5B%5D=[tid]
the links in sidebar get double escaped (it's a bug https://www.drupal.org/node/1342124)

In addition to the bug, I don't particularly like this way, because the path is hardcoded.
Is there a way to kind of link to the search view with some params instead so that Drupal handles the URL internally?
Answer: Actually it seems like the double escaping did work OK after all. A rewrote the URL to search-page?field_[machine_name][]=[tid] and it worked :)


Shell script to compare remote directories

Question: I have a shell script that I am using to compare directory contents. The script has to ssh to different servers to get a directory listing. When I run the script below, I am getting the contents of the server that I am logged into's /tmp directory listing and not that of the servers I am trying to ssh to. Could you please tell me what I am doing wrong?
The config file used in the script is as follows (called config.txt):
server1,server2,/tmp

The script is as follows
#!/bin/sh
CONFIGFILE="config.txt"
IFS=","
while read a b c
do
    SERVER1=$a
    SERVER2=$b
    COMPDIR=$c
    `ssh user@$SERVER1 'ls -l $COMPDIR'`| sed -n '1!p' >> server1.txt
    `ssh user@$SERVER2 'ls -l $COMPDIR'`| sed -n '1!p' >> server2.txt
done < $CONFIGFILE

When I look at the outputs of server1.txt and server2.txt, they are both exactly the same - having the contents of /tmp of the server the script is running on (not server1 or 2). Doing the ssh +dir listing on command line works just fine. I am also getting the error "Pseudo-terminal will not be allocated because stdin is not a terminal". Adding the -t -t to the ssh command isnt helping either
Thank you
Comment: Why do you have backticks around the ssh command?
Comment: use something like `dirdiff` or if you'd rather, there's a very nice `DirDiff` plugin for ViM.
Comment: @AlexejMagura, I do not/ cannot install any plugins. My issue is with the ssh command. It is doing a listing of /tmp of the host that is running the script and not the remote host I am intending to run the ls command on
Comment: turn on `set -vx` to debug/trace your script. You need to see that `/tmp` is inside the cmd sent to `ssh user@host`, its just a matter of getting the quoting right. Your posted question obscures your problem, boil it down to something someone could execute from their cmd line. Good luck.
Comment: @shellter, On the command line,
 ssh user@$SERVER1 'ls -l /tmp' executes just fine. Also I added the keys etc and am not prompted for a password.I have the back ticks in order to execute the command.
Comment: if you're getting the results of the local /tmp dir in your script, that definitely means that your quoting is off. You can't escape a singlequote inside single quotes, so you have to build up un-quoted single quotes with with something like `'"'"'` (instead of just `\'` as you might think). Good luck.
Comment: See: [How to diff and merge two directories?](http://vi.stackexchange.com/q/778/467) at Vim SE
Answer:
I have the back ticks in order to execute the command.

Backticks are not needed to execute a command - they are used to expand the standard output of the command into the command line. Certainly you don't want the output of your ssh commands to be interpreted as commands. Thus, it should work fine without the backticks:
    ssh user@$SERVER1 "ls -l $COMPDIR" | sed -n '1!p' >>server1.txt
    ssh user@$SERVER2 "ls -l $COMPDIR" | sed -n '1!p' >>server2.txt

(provided that double quotes to allow expansion of $COMPDIR are used).
Answer: first you need to generate keys to login to remote without keys
ssh-keygen  -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host

then try to ssh without pass
ssh remote-host

then try to invoke in your script but first make sanity check
var1=$(ssh remote-host) die "Cannot connect to remote host" unless $var1;

Comment: I can login just fine to the remote machine. It's just that the ls is being run locally rather than on the remote server. On the command line, the ssh+ls works fine.
Comment: I narrowed down the issue to the fact that the $COMPDIR is not being evaluated within ssh user@$SERVER1 'ls -l $COMPDIR'. Is there a reason for this? Would you be able to tell me how I can pass a directory name in it's place?
Comment: I added the following and now the ls executes remotely
     ssh npandit@$SERVER1<> "1.txt"
But how do i get the list of files to be written to a local file?


Commutative and additive monoids

Question: I am unfamiliar with higher levels of topology and number theory but find myself working on a project in which I need an understanding of some of those topics. Is anyone able to provide information on the difference between a commutative and additive monoid? Or are they the same type of monoid and additive is just a property?
Answer: A monoid is a set $M$ together with a binary operation $\times\colon M\times M\to M$ (usually denoted using infix notation), which is:

Assocative: for all $x,y,z\in M$, $(x\times y)\times z = x\times(y\times z)$.
Has an identity: there exists $e\in M$ such that for all $x\in M$, $e\times x = x\times e = x$.

The monoid is "commutative" if for all $x,y\in M$, we have $x\times y = y\times x$.
An example of a monoid is to take a set $X$ and let $M$ be the set of all functions $X\to X$; and letting the operation be composition of functions. The identity element is the identity function. This is generally not a commutative monoid.
An example of a commutative monoid is the positive integers under multiplication. Another example is the nonnegative integers under addition.
Often, instead of $\times$ we simply use juxtaposition, so that instead of writing "$a\times b$", we write $ab$ or $a\cdot b$. We also use "$1$" to denote the identity.
However, it is also common that when the operation is commutative, we use "additive notation", and use $+$ for the operation symbol and $0$ for the name of the identity.
It sometimes happens that additive notation is used even if the monoid is not commutative, but that is rare and the authors will tell you they are doing so explicitly and prominently.

Commutative and additive monoids


Using GSON with object to print JSON object

Question: Quick questions about GSON, after reading the documentation. This is what I have with JSON:
var json = {
     id: "person1",
     name: "person 1",
     data: {"email": "firstname.lastname@example.com"},
     children: [{
                    id: "person2",
                    name: "person 2",
                    data: {"email": "firstname.lastname@example.com"},
                    children: []
                },{
                    id: "person3",
                    name: "person 3",
                    data: {"email": "firstname.lastname@example.com"},
                    children: []
                }
                ]
}

1) Can I use GSON without using Class objects in Java? Can this be done easily using GSON and Java. Meaning I can do something like
GSON gson = new GSON();
gson.toJson("name", "person 1");

2) When I use this example:
            Gson gson = new Gson();
            Person p = new Contact(rs.getString("name"));
            gson.toJson(p);
            String json = gson.toString();
            System.out.println(json);

My Json output is not what I expected. That Person instance is a public class instance with just one property - name (for testing purposes). Why would I see essentially Factory, serializeNulls, etc in the output?
Thanks
Answer: Replace this:
gson.toJson(p);
String json = gson.toString();

with this:
String json = gson.toJson(p);

Answer: Try this :
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonStr = gson.toJson(YourObject);


Moving Exposed Views Filter into another region of the page

Question: Is there any way to move the exposed filters of views into another region of the site? i.e. I want to have a Search functionality attached to the a view by exposing the "Search: Search Terms", but I want the search to reside to the right in the "Right Side Bar" region and not directly above or below the actual Views field display.
Or put another way: Is there a relatively straightforward method of replicating the Exposed Views Filter functionality of one View through another Block that resides on the same page? This way, the Block which would act as the filter "controller" would reside separately from the View on a different region of the page.
Answer: There is a module called 'Views Better expose filters' which allows you to create blocks from view exposed filters.
Project can be found at: http://drupal.org/project/better_exposed_filters
or
drush dl better_exposed_filters; drush en better_exposed_filters
There is also some excellent documentation at http://drupal.org/node/766974
Comment: Is there a way to do this grammatically?
Comment: Once the better_exposed_filters module is installed, edit you view. In the "advanced" section there is a "exposed form" section, here click on "Expose form in a block" and select "yes", then save the view. Now you can go to the block configutation page and add the new block where you want (there should be a new inactive block "exposed form: name of the view").
Comment: I have Views Better expose filters but I can't see how to do this - any ideas? Thanks
Answer: You don't need any module. Edit the view > Advanced > Exposed form in block. Put: Yes.
You can now add the filter in Structure as a block.


What analyses do I use?

Question: If I have two categorical IVs and one continuous IV and I want to see if the two categorical IVs predict a continuous DV over and above the effect of the continuous IV, what analysis should I use?
Answer: You can use partial regression plots.
If the plot of residuals of the following regressions show a pattern, then yes, the categorical IVs are indeed adding further value.

Regress Y on the categorical IVs
Regress the categorical IVs against the continuous IVs

Use the residual plots of 1 vs 2 to see if the categorical IVs are adding any additional value.

What analyses do I use?


Open XML table cell font, width

Question: How can I do to insert the table (see code below) in a specific position of a word file (the location is the text of a paragraph).
Then you can set the font and width of the table cells?
Dim table As New Table()
Dim tblBorders As New TableBorders()
tblBorders.TopBorder = New TopBorder()
tblBorders.TopBorder.Val = New EnumValue(Of BorderValues)(BorderValues.[Single])
tblBorders.BottomBorder = New BottomBorder()
tblBorders.BottomBorder.Val = New EnumValue(Of BorderValues)(BorderValues.[Single])
tblBorders.LeftBorder = New LeftBorder()
tblBorders.LeftBorder.Val = New EnumValue(Of BorderValues)(BorderValues.[Single])
tblBorders.RightBorder = New RightBorder()
tblBorders.RightBorder.Val = New EnumValue(Of BorderValues)(BorderValues.[Single])
tblBorders.InsideHorizontalBorder = New InsideHorizontalBorder()
tblBorders.InsideHorizontalBorder.Val = BorderValues.[Single]
tblBorders.InsideVerticalBorder = New InsideVerticalBorder()
tblBorders.InsideVerticalBorder.Val = BorderValues.[Single]
tblPr.Append(tblBorders)
table.Append(tblPr)
trnew = New TableRow()
tc = New TableCell(New Paragraph(New Run(New Text("A"))))
trnew.Append(tc)
tc = New TableCell(New Paragraph(New Run(New Text("B"))))
trnew.Append(tc)
tc = New TableCell(New Paragraph(New Run(New Text("C"))))
Dim tcp As New TableCellProperties()
Dim gridSpan As New GridSpan()
gridSpan.Val = 4
tcp.Append(gridSpan)
tc.Append(tcp)
trnew.Append(tc)
tc = New TableCell(New Paragraph(New Run(New Text("D"))))
trnew.Append(tc)
table.Append(trnew)

thanks!
Comment: I think I have solved your problem completely, except that it is in C#. i am not sure of VB syntaxes
Answer: Access the body of the document, parse down the body and insert the table
C# code below
        //Body is a class in DocumentFormat.OpenXml.Wordprocessing.Body
        // and document part in DocumentFormat.OpenXml.Packaging.MainDocumentPart
        Body body = doc.MainDocumentPart.Document.Body;

        //Traverse the complete document body paragraph by paragraph
        foreach (Paragraph para in body.Descendants<Paragraph>())
        {
           if(para.InnerText.Contains("text the identifies this para")
           {
               para.InserAfterSelf(Table)
           }
        }

If you don't understand the docx format well, use the openxml productivity tool
for open xml version 2.5  http://download.microsoft.com/download/5/5/3/553C731E-9333-40FB-ADE3-E02DC9643B31/OpenXMLSDKToolV25.msi
for version 2.0  http://download.microsoft.com/download/2/7/F/27FF6744-D970-4FFB-90B8-5226B2B82E0A/OpenXMLSDKTool.msi
I havenot worked with the tables in openxml my self, however, it seems to me that you have to create the runs as in tc = New TableCell(New Paragraph(New Run(New Text("A")))) and then add the properties to run, then add the runs into the paragraphs and then into cells- see the link
Format Font Inside Table OpenXML C#
Comment: tnks! How can I set the font and size of cells?
Comment: create run seperately and format the runs see the updated answer above, please vote up the answer :)

Open XML table cell font, width


Passing object which implements a child interface as its parent interface parameter

Question: I have a parent interface (IParent), a child interface (IChild) and an object which implements the child interface.
I'm trying to call a function which accepts an array of IParent parameter by passing an array of objects implementing the child interface.
On compiling I get the following error:

[dcc32 Error] Unit1.pas(46): E2010 Incompatible types: 'IParent' and
  'TForm1'

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  IParent = interface
    procedure DoSomething();
  end;

  IChild = interface(IParent)
    procedure DoSomethingElse();
  end;

  TForm1 = class(TForm, IChild)
    procedure FormCreate(Sender: TObject);
  public
    procedure DoSomething();
    procedure DoSomethingElse();
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure CallAllDoSomething(AArray : array of IParent);
var
  i : integer;
begin
  i := 0;
  while(i < Length(AArray)) do
  begin
    AArray[i].DoSomething();
    Inc(i);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Unit1.CallAllDoSomething([Self]);
end;

procedure TForm1.DoSomething();
begin
  ShowMessage('Something');
end;

procedure TForm1.DoSomethingElse();
begin
  ShowMessage('Something else');
end;

end.

Answer: You have to add IParent to the declaration of TForm1, too:
 TForm1 = class(TForm, IParent, IChild)

Comment: Indeed. Without that listing of _IParent_ the _TForm1.DoSomething_ is in fact the implementation of _IChild.DoSomething_ and not _IParent.Dosomething_.
Answer: In order to assign a TForm1 object directly to an IParent, you have to include IParent in TForm1's declaration:
TForm1 = class(TForm, IParent, IChild)

This behavior is documented in Embarcadero's DocWiki:
Implementing Interface References

An interface-type expression cannot reference an object whose class implements a descendent interface, unless the class (or one that it inherits from) explicitly implements the ancestor interface as well.
For example:
type
  IAncestor = interface
  end;
  IDescendant = interface(IAncestor)
    procedure P1;
  end;
  TSomething = class(TInterfacedObject, IDescendant)
    procedure P1;
    procedure P2;
  end;
     // ...
var
  D: IDescendant;
  A: IAncestor;
begin
  D := TSomething.Create;  // works!
  A := TSomething.Create;  // error
  D.P1;  // works!
  D.P2;  // error
end;

In this example, A is declared as a variable of type IAncestor. Because TSomething does not list IAncestor among the interfaces it implements, a TSomething instance cannot be assigned to A. But if you changed TSomething's declaration to:
TSomething = class(TInterfacedObject, IAncestor, IDescendant)
// ...

the first error would become a valid assignment. D is declared as a variable of type IDescendant. While D references an instance of TSomething, you cannot use it to access TSomething's P2 method, since P2 is not a method of IDescendant. But if you changed D's declaration to:
D: TSomething;

the second error would become a valid method call.

Alternatively, since IChild derives from IParent, you can explicitly cast the TForm1 object to IChild first, and then let the compiler convert the IChild into an IParent for you:
Unit1.CallAllDoSomething([IChild(Self)]);

This behavior is also documented:
Interface Assignment Compatibility

Variables of a given class type are assignment-compatible with any interface type implemented by the class. Variables of an interface type are assignment-compatible with any ancestor interface type. The value nil can be assigned to any interface-type variable.


Melhor prática de persistência de dados

Question: Quando se trata de persistir os dados com o Core Data. Qual é a melhor maneira de persistir os dados?
Por exemplo:

Possuo um objeto Pessoa (subclasse de NSObject). E no momento da
  persistencia eu "converto" o PessoaMO (subclasse de
  NSManagedObject).

O que eu queria saber, é basicamente o seguinte:
A maneira correta de fazer isso, é como o exemplo citado acima ou diretamente com o NSManagedObject sem ter a classe Pessoa (subclasse de NSObject)?
Comment: http://meta.pt.stackoverflow.com/a/1911/101
Comment: Você precisa somente da classe que deriva de NSManagedObject. O que você mexer nela irá automaticamente ser salvo. A não ser que precise mesmo de uma cópia temporária do objeto para trabalhar com ele por um tempo sem salvar, o que não é algo comum.
Answer: Essa sua solução parece desrespeitar o DRY (Don't Repeat Yourself) pois além de existir uma duplicação no código as classes PessoaMO e Pessoa vão ser dependentes. (Qualquer mudança em uma quebra a outra).
Uma solução seria user diversos com NSManagedObjectContext's para fazer um melhor controle do que será persistido e o que será descartado. (Creio que seja isso que você esteja fazendo).

Melhor prática de persistência de dados


Proportion of explained variance in PCA and LDA

Question: I have some basic questions regarding PCA (principal component analysis) and LDA (linear discriminant analysis):

In PCA there is a way to calculate the proportion of variance explained. Is it also possible for LDA? If so, how?
Is the "Proportion of trace" output from the lda function (in R MASS library) equivalent to the "proportion of variance explained"?
Comment: Your first question may be a duplicate of http://stats.stackexchange.com/questions/22569, where you can find answers.  Presumably "LDA" means *Linear Discriminant Analysis* (it has other statistical meanings too, which is why we try to [expand acronyms](http://meta.stats.stackexchange.com/questions/1707/is-it-reasonable-to-put-try-to-avoid-acronyms-in-the-help-on-asking-questions)).
Comment: In a sense, a discriminant accounts for a variability as a p. component does, the eigenvalue being the amount of it. However, the "variability" in LDA is of special sort - it is the _ratio_ of between-class variabilty to the within-class variability. Each discriminant tries to account for as much as possible of that ratio. [Read further](http://stats.stackexchange.com/a/48859/3277)
Comment: Thanks for the explanation. Therefore, if in the axes of the PC components I label them as "PC (X% of explained Variance)"  what would be the correct short term when I label the LDs. Thanks again.
Comment: With LDA, the correct wording will be "LD (X% of explained between-group Variance)".
Comment: Thanks again for the great help and patience. BTW how can I access the Proportion of trace (LD1, LD2) as I wish to save them in two separate variables?
Comment: Please only one question per thread.
Answer: I will first provide a verbal explanation, and then a more technical one. My answer consists of four observations:

As @ttnphns explained in the comments above, in PCA each principal component has certain variance, that all together add up to 100% of the total variance. For each principal component, a ratio of its variance to the total variance is called the "proportion of explained variance". This is very well known.
On the other hand, in LDA each "discriminant component" has certain "discriminability" (I made these terms up!) associated with it, and they all together add up to 100% of the "total discriminability". So for each "discriminant component" one can define "proportion of discriminability explained". I guess that "proportion of trace" that you are referring to, is exactly that (see below). This is less well known, but still commonplace.
Still, one can look at the variance of each discriminant component, and compute "proportion of variance" of each of them. Turns out, they will add up to something that is less than 100%. I do not think that I have ever seen this discussed anywhere, which is the main reason I want to provide this lengthy answer.
One can also go one step further and compute the amount of variance that each LDA component "explains"; this is going to be more than just its own variance.

Let $\mathbf{T}$ be total scatter matrix of the data (i.e. covariance matrix but without normalizing by the number of data points), $\mathbf{W}$ be the within-class scatter matrix, and $\mathbf{B}$ be between-class scatter matrix. See here for definitions. Conveniently, $\mathbf{T}=\mathbf{W}+\mathbf{B}$.
PCA performs eigen-decomposition of $\mathbf{T}$, takes its unit eigenvectors as principal axes, and projections of the data on the eigenvectors as principal components. Variance of each principal component is given by the corresponding eigenvalue. All eigenvalues of $\mathbf{T}$ (which is symmetric and positive-definite) are positive and add up to the $\mathrm{tr}(\mathbf{T})$, which is known as total variance.
LDA performs eigen-decomposition of $\mathbf{W}^{-1} \mathbf{B}$, takes its non-orthogonal (!) unit eigenvectors as discriminant axes, and projections on the eigenvectors as discriminant components (a made-up term). For each discriminant component, we can compute a ratio of between-class variance $B$ and within-class variance $W$, i.e. signal-to-noise ratio $B/W$. It turns out that it will be given by the corresponding eigenvalue of $\mathbf{W}^{-1} \mathbf{B}$ (Lemma 1, see below). All eigenvalues of $\mathbf{W}^{-1} \mathbf{B}$ are positive (Lemma 2) so sum up to a positive number $\mathrm{tr}(\mathbf{W}^{-1} \mathbf{B})$ which one can call total signal-to-noise ratio. Each discriminant component has a certain proportion of it, and that is, I believe, what "proportion of trace" refers to. See this answer by @ttnphns for a similar discussion.
Interestingly, variances of all discriminant components will add up to something smaller than the total variance (even if the number $K$ of classes in the data set is larger than the number $N$ of dimensions; as there are only $K-1$ discriminant axes, they will not even form a basis in case $K-1<N$). This is a non-trivial observation (Lemma 4) that follows from the fact that all discriminant components have zero correlation (Lemma 3). Which means that we can compute the usual proportion of variance for each discriminant component, but their sum will be less than 100%.
However, I am reluctant to refer to these component variances as "explained variances" (let's call them "captured variances" instead). For each LDA component, one can compute the amount of variance it can explain in the data by regressing the data onto this component; this value will in general be larger than this component's own "captured" variance. If there is enough components, then together their explained variance must be 100%. See my answer here for how to compute such explained variance in a general case: Principal component analysis "backwards": how much variance of the data is explained by a given linear combination of the variables?
Here is an illustration using the Iris data set (only sepal measurements!):

Thin solid lines show PCA axes (they are orthogonal), thick dashed lines show LDA axes (non-orthogonal). Proportions of variance explained by the PCA axes: $79\%$ and $21\%$. Proportions of signal-to-noise ratio of the LDA axes: $96\%$ and $4\%$. Proportions of variance captured by the LDA axes: $48\%$ and $26\%$ (i.e. only $74\%$ together). Proportions of variance explained by the LDA axes: $65\%$ and $35\%$.
\begin{array}{lcccc}
& \text{LDA axis 1} & \text{LDA axis 2} & \text{PCA axis 1} & \text{PCA axis 2} \\
\text{Captured variance} & 48\% & 26\% & 79\% & 21\% \\
\text{Explained variance} & 65\% & 35\% & 79\% & 21\% \\
\text{Signal-to-noise ratio} & 96\% & 4\% & - & - \\
\end{array}

Lemma 1. Eigenvectors $\mathbf{v}$ of $\mathbf{W}^{-1} \mathbf{B}$ (or, equivalently, generalized eigenvectors of the generalized eigenvalue problem $\mathbf{B}\mathbf{v}=\lambda\mathbf{W}\mathbf{v}$) are stationary points of the Rayleigh quotient $$\frac{\mathbf{v}^\top\mathbf{B}\mathbf{v}}{\mathbf{v}^\top\mathbf{W}\mathbf{v}} = \frac{B}{W}$$ (differentiate the latter to see it), with the corresponding values of Rayleigh quotient providing the eigenvalues $\lambda$, QED.
Lemma 2. Eigenvalues of $\mathbf{W}^{-1} \mathbf{B} = \mathbf{W}^{-1/2} \mathbf{W}^{-1/2} \mathbf{B}$ are the same as eigenvalues of $\mathbf{W}^{-1/2} \mathbf{B} \mathbf{W}^{-1/2}$ (indeed, these two matrices are similar). The latter is symmetric positive-definite, so all its eigenvalues are positive.
Lemma 3. Note that covariance/correlation between discriminant components is zero. Indeed, different eigenvectors $\mathbf{v}_1$ and $\mathbf{v}_2$ of the generalized eigenvalue problem  $\mathbf{B}\mathbf{v}=\lambda\mathbf{W}\mathbf{v}$ are both $\mathbf{B}$- and $\mathbf{W}$-orthogonal (see e.g. here), and so are $\mathbf{T}$-orthogonal as well (because $\mathbf{T}=\mathbf{W}+\mathbf{B}$), which means that they have covariance zero: $\mathbf{v}_1^\top \mathbf{T} \mathbf{v}_2=0$.
Lemma 4. Discriminant axes form a non-orthogonal basis $\mathbf{V}$, in which the covariance matrix $\mathbf{V}^\top\mathbf{T}\mathbf{V}$ is diagonal. In this case one can prove that $$\mathrm{tr}(\mathbf{V}^\top\mathbf{T}\mathbf{V})<\mathrm{tr}(\mathbf{T}),$$ QED.
Comment: +1. Many things you discuss here were covered, slightly more compressed, in my [answer](http://stats.stackexchange.com/a/48859/3277). I have added a link to your present answer in the body of that my old one.
Comment: @ttnphns: I remember that answer of yours (it has my +1 from long time ago), but did not look there when writing this answer, so many things are indeed presented very similarly, perhaps too much. The main reason I wrote this answer, however, was to discuss "explained variance" (in the PCA sense) of the LDA components. I am not sure how useful it is in practice, but I was often wondering about it before, and have recently struggled for some time to prove the inequality from Lemma 4 that in the end was proved for me on Math.SE.
Comment: Note that the diagonal of $\mathbf{V}^\top\mathbf{T}\mathbf{V}$ is $\lambda+1$, the denominator to compute canonical correlations.
Comment: @ttnphns: Hmmm... I think that for each eigenvector $\mathbf{v}$, $$B/W = \frac{\mathbf{v}^\top\mathbf{B}\mathbf{v}}{\mathbf{v}^\top\mathbf{W}\mathbf{v}} = \lambda$$ and $$B/T = \frac{\mathbf{v}^\top\mathbf{B}\mathbf{v}}{\mathbf{v}^\top\mathbf{T}\mathbf{v}} = \frac{\mathbf{v}^\top\mathbf{B}\mathbf{v}}{(\mathbf{v}^\top\mathbf{B}\mathbf{v}+\mathbf{v}^\top\mathbf{W}\mathbf{v})} = \frac{\lambda}{\lambda+1},$$ as you say in your linked answer. But the value of $\mathbf{v}^\top\mathbf{T}\mathbf{v}$ (outside of any ratio) cannot really be expressed with $\lambda$ only.
Comment: It appears to me that the eigenvector of a given discriminant contains information of $B/W$ for that discriminant; when we calibrate it with $\bf T$ which keeps the covariances between the variables, we can arrive at the eigenvalue of the discriminant. Thus, the information on $B/W$'s is stored in eigenvectors, and it is "standardized" to the form corresponding to no correlations between the variables.


Regular Expression to capture entire list as one item- javascript

Question: I am working on js filter for string text and I want to use regex to capture entire list of list items, so I can count them and iterate throught them.
Please see what I am trying to achieve: https://regex101.com/r/Jd3JYW/2/
I know how to capture lines separately using this regex (^\*[^\*](.*)$)/gm but I need to capture entire list so I can differentiate between two lists, so I can make each of them start from n.1.
I expected the following code to work  ((^\*[^\*](.*)$)+)/gm I thought that this wil capture first item (as it works for separate lines) and also any following untill patern is broken. However it is not working, I am still getting separate lines captured. Do you have any idea guys, how to capture whole list? Thank you.
Comment: @WiktorStribiżew this works. Thank you!
Comment: Adam, I posted a variation of the regex that also supports the last line as a part of the list.
Comment: Thank you Wiktor. I really appreciate your help as well as explanation :)
Answer: You use $ to match line endings, but it is a zero width assertion. You should use some consuming pattern to match line endings, like [\r\n]+:
/^(?:\*[^*\n].*(?:[\r\n]+|$))+/gm

See the regex demo
Details

^ - start of a line
(?:^\*[^*\n].*(?:[\r\n]+|$))+ - 1 or more sequences of:

\*  - a * char
[^*\n] - a char other than LF and *
.* - any 0+ chars other than line break chars, as many as possible
(?:[\r\n]+|$) - either 1+ LF or CR symbols or end of a line (to match the last line).


Can a websocket client in a browser send headers?

Question: There is a debate raging in the office about a browsers ability to send handshake headers are part of establishing a websocket connection.
The websocket spec makes it clear that headers are possible. And, infact, in my non-browser client code I do set handshake headers and they are received without and issue.
The open debate is: Can a web browser using javascript insert headers are part of the handshake? The spec seems to indicate there is nothing against it but no one can find client side documentation that says how.
There is a lot of conflicting information. It's obviously possible because I am able to do it in Java without a problem. But for Javascript no one is seeing how. Is it a browser restriction?
To clarify how my question is different than others: Is the inability to do this a limitation of the default javascript libs? The web browser or the spec? It looks like it's a library limitation only
Comment: It can't be a browser restriction, can it? Everyone can write their own browser if they so wish.
Comment: Possible duplicate of [HTTP headers in Websockets client API](http://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api)
Answer: The issue is that the standard API for creating a webSocket connection in a browser does not provide any mechanism for setting custom headers.  So, it is a limitation of the webSocket browser API.
You can control the URL you send including query parameters and cookies are sent with the request so you have the ability to use the path, query parameters or cookies to send additional information with the connection request.
<code>WebSocket WebSocket(
  in DOMString url,
  in optional DOMString protocols
);
</code>
Comment: @ChristianBongiorno - Did this answer your question?
Answer: I am afraid that the WebSocket API in web browsers does not allow to set custom headers. Still, the browser sets cookies, if any.


SVI implied vol model parameterization

Question: It would be nice if anybody could help me.
I'm trying to understand the raw SVI parameterization of Gatheral.
We have: $$\omega(k; \chi_R)=a+b \left\{\rho(k-m)+ \sqrt{(k-m)^2+ \sigma^2}\right\} $$
I'm not clear about what exactly is given to calculate this and how to determine the parameters.
Can anybody explain me this, please?
Thanks for your help.
Answer: In the SVI model, you are modeling the implied volatility $\omega$ (at a fixed time) as a function of the strike $k$, with other parameters $a, b, \rho, m, \sigma^2$ fixed.
The way you compute this is, given a bunch of quotes at the same maturity, run numerical optimization to calibrate the parameters, and once they are fixed, you have implied vol as a one-dimensional function of the strike $k$.
UPDATE
Here is a practical guide to do this.
Implement the functions

BSvol2price -- straight-forward implementation of the BS formula calculations
BSprice2vol -- rootfinder inversion of the calculation above, matching the vol to produce a given price

Now you have 2 options, both using multi-dimensional numerical optimization:

fit in vol space - convert incoming price quotes to vols (using BSprice2vol), and fit the parameters to the resulting vols
fit in price space -- fit the parameters by producing prices from vols and measuring errors in price
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/86716/discussion-on-answer-by-gt6989b-svi-implied-vol-model-parameterization).

SVI implied vol model parameterization


Rails hang in Thread

Question: I am trying to make a single-threaded part multi-threaded. The initial single thread code works fine, but after I put it into a thread, the server hangs without any log information in the console.
Initial version:
data_array.each do |data|
  service = SomeService.new(data)
  data['answers'] = service.answer_data
end

Multi-threaded version:
threads = []
threads << Thread.new {
  data_array.each do |data|
    service = SomeService.new(data)
    data['answers'] = service.answer_data
  end
}
threads.each(&:join)

I tried binding.pry and get some more information:

I have a Class called AggregationReport < Report
When SomeService tried to run AggregationReport.new, it's stuck there forever.

Please help me understand what is happening here, any thoughts/help would be much appreciated!

Ruby version: Ruby27x
Rails: 5.2
Comment: If you're using activerecord - each thread will need to get access to a database connection and can block if there aren't sufficient connections in the pool. Is the service/report using the database? If so, see https://makandracards.com/makandra/45360-using-activerecord-with-threads-might-use-more-database-connections-than-you-think
Comment: Also - can you add the versions of ruby, rails and any gems that the hanging-report code uses? It's possible your report (or some gem it's using)
Answer: I'm making some assumptions about why you're threading this, but I'd expect typical threading code to follow the following basic structure:
threads = data_array.map do |data|
  Thread.new { SomeService.new(data).answer_data }
end

answers = threads.map(&:value)

For each piece of work you need done:

Create a thread that does the work and stores the result in a threadsafe way
Join each thread to ensure execution (this is handled by #value)
Collect the results

If your service needs a database connection (e.g. postgres, redis, etc) you'll need to manage this as well. For activerecord add ActiveRecord::Base.with_connection{ } inside the thread to allocate a db connection. There are similar approaches for Redis/etc. Then ensure you have sufficient connections for the # of threads you want to create (look at the pool: setting in config/database.yml).
Thread.new { ActiveRecord::Base.with_connection{ SomeService.new(data).answer_data } }

For this and other reasons, it isn't always a great idea to spawn N threads, so instead of this approach you may consider using a thread pool of X and ensure you have a db connection pool of at least X+1 connections. All of this can be complex. There are great gems that sit on top of ruby threading and make it easier to do this exact kind of thing. I'd strongly recommend checking out concurrent-ruby (bundled with Rails).
Comment: You could simplify your code by using [Thread#value](https://ruby-doc.org/core/Thread.html#method-i-value) rather than resorting to thread-locals, e.g. `answers = data_array.map { |data| Thread.new {SomeService.new(data).answer_data} }.map(&:value)`. Also, rather than using `parallel`, in a Rails environment you already have [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby) available which allows you to use thread pools and promises.
Comment: Thank you so much for your reply! Why I want to make it multi-threaded is that each section of the data takes a long time to process, and I just want it to run faster. Yes, this service needs a database connection. I updated my testing code and removed the loop just to see what will happen if there is only one thread, then I ran into the same issue, still stuck at `AggregationReport.new`. `Thread.new { ActiveRecord::Base.connection_pool,with_connection {SomeService.new(data).answer_data } }`. I check `pool:` and the number is 5, I changed it to a bigger number but still the same result.
Comment: If you stop the process when it's hanging you should get a stack-trace of where it was when the execution stops. If so - can you paste that into the question? It's possible there some other resource (similar to the database issue but something else) that it's blocked on
Comment: Or use the debugger / breakpoints to see where it's hanging
Comment: The process just stuck there without any errors. And if I stopped the process by crtl + c, it just shows interrupt. No other error. I tried breakpoints and found that the thread hang at `Class.new`. Then I added `binding.pry` right before `Class.new` and when it reaches there, I type `Class.new` in the console without type `next` or `step`, the process will hang too. So I guess the problem is I can't new a `Class` inside a thread.
Comment: Can you explain more about what the code is doing? (why are we creating a class? Is it being stored in a global constant?) It could be that the code you're trying to run is not threadsafe, especially if you're storing something in a class variable. Can this class be initialized before going into the threads?
Comment: Hey there, the Service is simply getting the data from the db and the `Class` is one of the models. The issue is probably related to development environment, after I run it in beta/prod environment the code works fine. I also saw some similar posts in StackOverflow and seems like none of them have found any solutions.
Comment: Oh, i thought you were creating a new class (with `Class.new`) rather than just an instance of a random class (e.g. `SomeClass.new`). You may try eager-loading the class definitions in dev to see if that avoids the problem.

Rails hang in Thread


Can reinforcement learning be used as path guidance?

Question: I have an online path guidance system that learns from a set of past experiences(trajectories) to provide a guidance for the user on how to cover the given space in the best way, adopting to whatever choice the user take. I have previously applied a second-order Markov model and acquired a successful rate of 73% on correctly predicting the path followed in the testing set.
Now, I would like to try another approach: Markov decision processes(Reinforcement Learning) to implement this system and use the first-order Markov transition matrix as the states transition probabilities for the model. However, from what I've understood the agent in such applications is a simulation and would follow the action given by the learned policy, but in my case the agent(user) might suddenly decides to jump from a state to another without following the given suggestion(policy). Is reinforcement learning unsuitable for my application?
Answer: You could restrict the learning algorithm to update its action values only when following the policy. For instance, SARSA uses the current reward and action values from the previous and current steps. It doesn't need to update at every step. Or maybe the user's decision to not follow the policy could be counted as a negative reward.


Cannot read property 'toLowerCase' of undefined - jQuery

Question: I've read the other examples where toLowerCase is undefined but I still don't understand. Can anyone explain why my function won't work?

var changeTitle = function() {
    var loanTitle = $(this).val();
    $(this).prev().text(loanTitle);
};

Loan One

<input type="text" onkeypress="changeTitle()" maxlength="25" />
Comment: Are you sure this is the code that's giving that error? toLowerCase isn't even called...??? Anyway, you need to null check variables that you call toLowerCase on.
Comment: @Barett Yes that's what pops up and I'm not too sure why.
Answer: In your function, the this object refers to the window and not the input field. Update your onkeypress code to the following:
<input type="text" name="loanName" onkeypress="changeTitle.call(this)" class="loanNameV1 qForms" maxlength="25" placeholder="Loan Name" />

Comment: Because the function is part of the window object, not the input element object. So, onkeypress you are calling a function that is part of the window object. The this object will refer to the window. Using call or apply, you can change the reference of this to another object. In this case, your input field. When you get a chance, research "call" and "apply". It will help you understand it a lot better.
Comment: And to add to it, onkeypress is part of the input field's object, and so the this object will refer to the input field.
Comment: Ahhh thanks @hoyen! It works now but why does "this" refer to the window and not the element exactly?
Answer: I'm not sure what you really are trying to do, but I passed your input element as a parameter to the function you defined as this and I added the JQuery library to my snippet. It works with the exception that the last key stroked isn't displayed in the title. Maybe you can use this along with your toLowerCase feature and fix it up to your liking.

var changeTitle = function(obj) {
    var loanTitle = $(obj).val();
    $(obj).prev().text(loanTitle);
};

 Loan One

<input type="text" onkeypress="changeTitle(this);" maxlength="25" placeholder="Loan Name" />


Rails ActiveRecord finding questions by tag in named scope

Question: I want the equivalent of SO search by tag, so I need an exists query but I also still need to left join on all tags. I've tried a couple of approaches and I'm out of ideas.
The Qustion - Tag relationship is through has_and_belongs_to_many both ways (i.e. I have a QuestionTags joiner table)
e.g.
Question.join(:tags).where('tag.name = ?', tag_name).includes(:tags)
I would expect this to do what I need but actually it just mashes up the includes with the join and I just end up with basically an inner join.
Question.includes(:tags)
   .where("exists (
               select 1 from questions_tags
                where question_id = questions.id
                  and tag_id = (select id
                                  from tags
                                 where tags.name = ?))", tag_name)

This fetches the correct results but a) is really ugly and b) gives a deprecation warning as again it seems to confuse the includes with the join:

DEPRECATION WARNING: It looks like you are eager loading table(s) (one
  of: questions, tags) that are referenced in a string SQL sn ippet. For
  example:
Post.includes(:comments).where("comments.title = 'foo'")

Note I'm trying to write these as named scopes.
Let me know if the question isn't clear. Thanks in advance.
Comment: To be honest I am confused why inner join is not good enough for You, can You please go more in detail why joining tags is not working for You?
Comment: @EdgarsJekabsons because I need to display a list of all questions tagged with a particular tag, but within that list still I need to display all tags for each question, avoiding an N+1 lookup. E.g. click on the `sql` tag above to see the SO page that does what I need. With only the inner join the page would only show the `sql` tag under each question.
Answer: OK, got it. I know no built in syntax to do it. I have used an alternative before, You can do like this:
Question.include(:tags).where("questions.id IN (
#{ Question.joins(:tags).where('tags.name = ?', tag_name).select('questions.id').to_sql})")

You can also join this subquery to your questions table instead of using IN. Alternatively if You are not against adding gems and You are using Postgres, use this gem.
It provides really neat syntax for advanced queries.
Comment: Thanks, this works but I again get the deprecation warning, is there any way to fix that?
Comment: Yes, it makes no difference. I actually get this warning even with unrelated include, e.g. `Question.tagged("sql").includes(:votes)`. It seems to me a bug in rails, I'm having same problem on any query that has both a `join` and an `includes`.
Comment: Sorry don't have any Rails 4 projects cloned so can't play around myself. Maybe try submitting a ticket in Rails Github repo ?
Answer: Use preload instead of includes:
Question.preload(:tags).where("exists ....


Unable to Upgrade Prometheus Blackbox Exporter to v0.24.0 via Helm using chart v8.10.1 on AWS Kubernetes - Error: resource mapping not found for name

Question: I'm trying to upgrade Prometheus Blackbox Exporter to v0.24.0 using Helm chart v8.10.1 on my AWS EKS Kubernetes cluster v1.25 but it is failing with the following error:
helm upgrade -n monitoring prometheus-blackbox-exporter prometheus-community/prometheus-blackbox-exporter --version 8.10.1
Error: UPGRADE FAILED: unable to build kubernetes objects from current release manifest: resource mapping not found for name: "prometheus-blackbox-exporter" namespace: "monitoring" from "": no matches for kind "PodSecurityPolicy" in version "policy/v1beta1"

How to solve this?
Answer: Solution:
First, you need install helm-mapkubeapis (one time action per AWS EKS Kubernetes cluster). Then you need to run helm mapkubeapis for the prometheus-blackbox-exporter Helm release and then you need to delete the prometheus-blackbox-exporter Helm release and then you need to install it again.
Below you can see all the commands for the above mentioned steps:
helm plugin install https://github.com/helm/helm-mapkubeapis
helm mapkubeapis -n monitoring prometheus-blackbox-exporter
helm del -n monitoring prometheus-blackbox-exporter
helm install -n monitoring prometheus-blackbox-exporter prometheus-community/prometheus-blackbox-exporter --version 8.10.1

Note: It is assumed that the Prometheus Helm charts repository is already added to your AWS EKS Kubernetes cluster. If not, you can run the following command to do so: helm repo add prometheus-community https://prometheus-community.github.io/helm-charts


What are the drawback to storing files in the /tmp folder when using Google App Engine

Question: I am writing a node app that posts some files to the backend, I want to upload the, and get URI's to do this, I am using  multer and I am thinking of storing the files in the tmp directory.
What are the drawbacks to this?

Will I incure more costs and how can I avoid that?
When will the files be deleted from the /tmp folder? I want them to be deleted as soon as possible after uploading and getting their uris.

This is an open ended question.
Answer: I don't think this will increase costs.
You will probably want to delete them yourself, when you are done with them.  I don't think GAE will delete them (except when deploying a new instance) and you don't want to fill the disk.
The only downside I can think of is that the files are specific to an instance so only the same instance that put them there can use them.  If you are doing this in a single HTTP request you should be ok.
Answer:
What are the drawbacks to this?

Based on this documentation on reading and writing temporary files:

All files in this directory are stored in the instance's RAM, therefore writing to /tmp takes up system memory.

Will I incure more costs and how can I avoid that?

This would depend on the size on the file size in the /tmp as this would take up system memory. Pricing would still based on the pricing calculator. Please take note that pricing is still not final and would depend on your usage and taxes that would apply on the final billing.

When will the files be deleted from the /tmp folder? I want them to be deleted as soon as possible after uploading and getting their uris.

Files written in /tmp are only available to the instance that created the files. You can either delete the instance, deleting the temporary files with it, or create a custom script that would manually delete the files once the tasks have been completed.


Como criar um módulo no npm?

Question: A ideia é conseguir baixar esse módulo pelo npm install e conseguir executar esse pacote:
npm install nome-do-modulo --save

Uma vez baixando poderia ser executado no meu projeto da forma abaixo:
nomeDoModulo = require('nome-do-modulo')

Comment: ve se [esse link do npm](https://docs.npmjs.com/getting-started/creating-node-modules) ajuda.
Answer: Os passos a dar são:
npm init

isso vai criar um package.json e vai perguntar-te uma série de coisas sobre o módulo que estás a criar.
Depois costumo fazer tag do código (dar-lhe uma versão), depois de fazer commit com:
git tag 1.0.0

e para enviar para a npm:
npm publish

Se ainda não tiveres conta podes criar no site e depois usar npm login na linha de comando, ou então fazer diretamente npm adduser e depois fazer o npm publish.
Como já tenho conta há algum tempo não me lembro das perguntas que fazer mas esses passos devem te levar lá.

Como criar um módulo no npm?


How can I listen for drupalOverlayBeforeClose in custom jQuery?

Question: I am monitoring a particular form for changes and want to warn the user before they attempt to navigate away from the form without submitting it. I've successfully gotten this working in all instances except for the overlay close button – because that element is bound to the Drupal.overlay.close() function, a user is able to click it and immediately close the overlay, even though I'm listening for all $('a') clicks.
I looked inside of modules/overlay/overlay-parent.js and see that it provides the drupalOverlayBeforeClose event so that other scripts can respond and stop the overlay from closing if needed, but I can't figure out how to implement this. Do I just listen for this event globally, through the $(document) or something? Or do I need to extend the Drupal object and attach my function to this event somehow?
Comment: Let me add that listening via `$(document). drupalOverlayBeforeClose(function() { alert('foo'); })` never fires.
Answer: You need to use the following code,
jQuery(document).bind('drupalOverlayBeforeClose', function(event) {
   // your code
   return false // This will prevent the overlay from closing.
});

click, dbclick and other methods are shortcut methods for bind('click', function(){}), bind('dbclick', function(){}) respectively. However there is no shortcut method called drupalOverlayBeforeClose, that's why the following code didn't worked,
$(document).drupalOverlayBeforeClose(function(event) {

});

So you need to bind drupalOverlayBeforeClose explicitly using bind. If you return false in your callback, then the overlay won't be closed.
Hope this clarifies the answer.
Comment: Is your .js file being called by your front-end theme, or the admin theme?
Comment: This helps a lot. I should have tried binding that sooner and not treated it like a shortcut.

I'm still experiencing an issue where the event that is passed into the function doesn't seem to be the event that I want. For example, if I do `$(document).bind('drupalOverlayBeforeClose', function(event) { event.preventDefault();` and then run `if (event.isDefaultPrevented()) { alert('foo'); }` in overlay-parent.js I see no 'foo'. Is the event being passed into function() the proper "event"?
Comment: Added the information about preventing the overlay from closing in the answer :)
Comment: `return false;` isn't doing it for me. overlay-parent.js is checking `if (event.isDefaultPrevented()) { return false; }` which is why I tried `preventDefault()`.
Comment: No need to try preventDefault(), just return false that's enough. Look at the comment before that line - **If a handler returned false, the close will be prevented.**. You're making the things more complicated :(
Comment: `return false;` does nothing – the overlay still immediately closes. I read all of the comments, but I can clearly see that the code is looking for `isDefaultPrevented()`.
Comment: Does `return false` in an event handler cause the preventDefault() state to change?
Comment: Its working perfectly fine for me. Show your code so that I can say where its going wrong.
Comment: let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/6302/discussion-between-mathankumar-and-charlie-s)
Comment: Chat was helpful. While this code does show a popup: `var confirm = window.confirm(Drupal.t("You will lose all unsaved work."));return confirm;` the overlay still immediately closes regardless of what choice the user selects. It looks like the event object is completely empty in the listener. I've even moved this into a standalone JS file (outside of my behavior attach) and it's still empty.
Comment: It's attached via ['#attached']['js'] to the node edit form in a module.
Answer: Try following code
jQuery("#overlay-container").find("iframe").contents().find("#overlay-close").click(function (event) {
    console.log('Clicked on close overlay link');

    event.preventDefault();
    event.stopPropagation();

    var r = confirm("Do you want to close overlay?");
    if (r == true) {
        Drupal.overlay.close();
    } else {
        //do something else
    }

});

When user will click on close overlay link, a confirming dialog should display.
Comment: Thanks Ajinkya. I'm trying to listen when the event is triggered (`drupalOverlayBeforeClose`) and have gotten it working thanks to you and mashemetician's code – the issue was with the scope of the event between the parent and the iframe. The issue I still have, however, is that if I press "cancel" the first time, the X never listens for subsequent clicks. Do you have that behavior when you use your code?
Comment: No that behavior does not happen with my code. I can still listen for subsequent clicks.


Does playing the Jew's harp damage the teeth

Question: When I was a kid I was gifted a Jew's harp. I never formally learned to play it (and also didn't learn to play other instruments, so my knowledge on music theory and on instruments is very limited).
I'm not sure I played it the way it's supposed to be played.
I always produced the (to my ears) best sound when the harp lay firmly connected to my teeth.
But playing, the vibration of the metal was quite strong, even numbing in a way.
Does (correctly) playing the Jew's harp damage the teeth?
Comment: Disclaimer: I have NO IDEA. But [it looks like](https://soundadventurer.com/how-to-avoid-damaging-your-teeth-while-playing-the-jaw-harp/), no, doing it right can be safe (while clearly, doing it wrong might not be). I strongly suspect that having it contact your teeth seems much more resonant *to you* because it's sending vibrations right through your skull, but I wonder whether it really matters to the sound transmitted to the listener. My takeaway from the link above is "start learning with a weak reed and take it easy."
Comment: If the reed is not at the resonant frequency of your teeths you're probably fine. The bad thing is that you don't know this frequency until you reach it and shatter your teeths. The good thing is that a shattered (smaller) teeth will have a higher frequency hence, after the first shatter you know that you're safe by playing Jew's harp work a lower pitch ;).
Comment: @Tom Is... is that how Jews-harpists usually find out? Or is that conjecture?
Answer: The Jew's harp is supposed to lie against the front of the teeth; not bitten down on.  Another recommendation is to (primarily) pluck the reed inwards (this reduces the chance of pulling the frame off of the teeth); c.f. How To Avoid Damaging Your Teeth while Playing the Jaw Harp


ANOVAs from SPSS and R report different degrees of freedom. Which are correct?

Question: I am conducting a reanalysis of some data. The dv is continuous (beta value ie neural activity) and the iv is categorical (position) with three levels (1, 2, 3). There are 126 observations. The original analysis was conducted in SPSS and the original paper reports F(2,82) = 43.058, p = 0.00.  I am conducting my analysis in R, and I am getting residuals df of 123. Here is the table:
> summary(aov(Beta ~ Position, data = cFFA_same))
                  Df Sum Sq Mean Sq F value Pr(>F)
factor(Position)   2  118.3   59.13    4.08 0.0193 *
Residuals        123 1782.7   14.49
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

I am quite poor in statistics - can anyone explain which df are correct and why/how they affect the F statistic and p value in this way?
Edit: The design is repeated measures, and I have seen that I should be using aov_car from the afex package instead of Anova/aov. However, I get the same output for both:
Anova(lm(Beta ~ Position, data = cFFA_half))

Anova Table (Type II tests)

Response: Beta
           Sum Sq  Df F value Pr(>F)
Position    50.73   2  1.4999 0.2272
Residuals 2080.08 123

aov_car(Beta ~ Position + Error(Beta), data = cFFA_half)

Contrasts set to contr.sum for the following variables: Position
Anova Table (Type 3 tests)

Response: Beta
    Effect     df   MSE    F  ges p.value
1 Position 2, 123 16.91 1.50 .024    .227
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

I was also not sure what to use as the error term. Is this the problem?
Comment: summary(aov(Beta ~ as.factor(Position), data = cFFA_same)) should give the same freedom. Position is a categorical iv, this needs to be reflected in formula.
Comment: @xiaoA position is already treated as a factor without making it explicit in the formula. You can see this in the number of degrees of freedom associated with the variable, which is 2 (as expected for a factor variable with 3 levels) and not 1 (as expected for numerical variable).
Comment: Part of the code to make the dataframe included making position a factor: `cFFA_same$Position<−factor(cFFAsame$Position, levels = c("1", "2", "3"))`
Answer: You have different degrees of freedom for the residuals 123 vs 82.
This indicates that you probably are

working with different data (the original paper does not have 126 observations)

or

you are doing several steps differently (e.g. the original paper might have used some selection of the data or has additional variables in the model).

It is not possible to see what it is exactly. In order to answer more specifically about this you should make your question more detailed. Ideally the question should be made reproducible (e.g. provide links to the data and original research).
Comment: It is definitely the same data and in its entirety. The design is repeated measures, which apparently the `Anova` function I used isn't set up for. I have tried with `aov_car` from afex which is apparently specifically for this kind of design, but I am getting the same output
Comment: @danielb a repeated measures design falls into the category 'or you are doing several steps differently' and more specifically 'additional variables in the model'. But to answer with more detail you should provide more details about the data and the analysis.
Make your question reproducible. I can not see how your data is definitely the same as the data from the original research.
Comment: In addition, it is unclear how the repeated design works for your case. It seems like you do not have a fully repeated measures design, because in that case I would expect the difference in degrees of freedom to be a multiple of two or three.


Export & Import - Single IIS WebSites using powershell

Question: Is there any way to export and import the single iis website from one server to another.
Thanks,
Sandesh
Answer: Yes you can, everything is in the IISAdministration Powershell module, using the Export-IISConfiguration cmdlet. You can visit this MS docs link  to review what cmdlets best suit your specific needs.
You can also use a couple of commands:
To Export website:
%windir%\system32\inetsrv\appcmd list site /config /xml > c:\sites.xml

To Import the website:
%windir%\system32\inetsrv\appcmd add site /in < c:\sites.xml

Hope that helps a bit!
Comment: This still works for IIS 8 and IIS 10, just tested it
Also good to know that this will import and export ALL items, including the "default website".  So you will get an error if you try to import it and its already there.  Make sure to delete them first (if you can afford to do that).
Also this will not export the App Pools, only the value of the App Pool associated with the Web Site.  You will need another process to Import/Export that.
`%windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\apppools.xml`
and
`%windir%\system32\inetsrv\appcmd add apppool /in < c:\apppools.xml`
Comment: Be careful with the `appcmd list apppool /config` command though - it exports the application pool passwords in clear text!


How to run code when you receive an email

Question: I'm making a mobile product for a social network and one of the features we would like to add is text-to-status. We are doing something different though, similar to YouTube's mobile upload. YouTube uses emails such as:
firstname.lastname@example.com

I would like to something similar with PHP, except do something like this:
Have the user text:
firstname.lastname@example.com

Then when the server receives this it will detect the phone number(for instance: email@example.com) saved on the account, sort of like how Facebook/Twitter work with their shortcodes.
I have been researching this for about 3-4 months and nothing has really come up with much of anything. If anyone can help it would be wonderful. Just a side-note, I am not very fluent in any server-side coding other than php and server-side Javascript.
Answer: Well there are two choices:

You can configure your mail server to put the mails in a mailbox and then have a PHP script run by cron or such polling it from time to time and fetching new mail
Or you can configure your mail server to execute a program once a mail is being received. Often this is done by specifying an alias to something like |/usr/bin/php /path/to/script.php which will then receive the mail from stdin.
Comment: I actually like this and makes it seem less complicated than I though, but i'm not accepting it just yet to see if anyone else has a method that may work even better. Thank you very much though.
Comment: Well the third option I can image is writing your own mail daemon which will receive the SMTP communication, but existing MTAs are good for that ;-)
Comment: You also have the option of sending the message to the app via http rather than running the script directly which can create an overhead and isn't load balanced etc. I wrote a blog post at http://steve.dynedge.co.uk/2010/09/07/incoming-email-in-rails-3-choosing-the-right-approach/ explaining some of the options. It's rails but the ideas apply to php too.
Answer: I had this question too and I wanted to get emails in real time so I worked out my own solution with google app engine. I basically made a small dedicated google app engine app to receive and POST emails to my main site. That way I could avoid having to set up an email server.
You can check out Emailization (a little weekend project I did to do it for you), or you this small GAE app that should do the trick.
I kinda explained it more on another question.
Hope that helps!


the number of $10$-digit numbers $\overline{a_1 a_2a_3...a_{10}}$ such that $a_i \in \{1,2,3,4\}$ and $\sum_{i=1} ^{i=10} a_i$ is even

Question:
Let $n$ be  the number of $10$-digit numbers $\overline{a_1 a_2a_3...a_{10}}$  such that $a_i \in \{1,2,3,4\}$ and $\sum_{i=1} ^{i=10} a_i$ is an even number. Which of following options is true? 1. $n=2^{19}$ 2. $n < 2^{19}-1$3. $n=2^{19}+1$ or $n=2^{19}-1$ 4. $n > 2^{19}+1$

We know for every  $10$-digit numbers  $\overline{a_1 a_2a_3...a_{10}}$  such that $a_i \in \{1,2,3,4\}$ we have $\sum_{i=1} ^{i=10} a_i=2k$, or $2k+1$ so the number of  $10$-digit numbers is $4^{10}=2^{20}$ and half of them are even, so answer is $n=2^{19}$.
Comment: Where is the question?
Answer: There are $4^{10}$ numbers with symbols from 1 to 4 and half of them are even. So the result is given by 1.
Comment: How we can solve this if $a_i \in \{1,2,3,4,5\}$ ?
Comment: A bit different, $5^{10}$ and $2$ out of $5$ are even.
Comment: Seems that the question is misunderstood. It is not asking whether $a_{10}$  is even. Rather it is asking how many of the $10$-digit numbers have  $\sum_{i=1}^{i=10}a_i$ is even.


How does this likelihood function log transformation work?

Question: Suppose I have the following Bayesian inference problem:
$$\underset{\{{\theta_i\},\{B^j}\}}{\operatorname{argmax}}
 P(\{\theta_i\},\{B^j_{il}\}|\{v_{jl}\}) \propto P(\{v_{jl}\}|\{\theta_i\},\{B^j_{il}\}) P(\{B^j_{il}\}) P(\{\theta_i\}) \tag1 \label{eq1}$$
and suppose that given $\{B^j\}$ we have the MAP estimate on $\{\theta_i\}$:
$$\hat{\theta_i} = \frac{\sum_{jl} B^i_{jl} v_{jl}/\sigma_j^2}{1/\sigma_0^2 + \sum_{jl}B^j_{il}/\sigma_j^2} \text{ for } i = 1, \dots, L.$$
In the paper Bayesian Nonparametric Federated Learning of Neural Networks  (2020), the authors then say: "
We can now cast [the] optimization [problem] [..] with respect to only $\{B_j\}$ [..] Taking natural logarithm we obtain:
$$-\frac{1}{2}\sum_i(\frac{\|\hat{\theta_i}\|^2}{\sigma_0^2} + D\log(2\pi\sigma_0^2) + \sum_{jl} B_{il}^j \frac{\|v_{jl} - \hat{\theta_i}\|^2}{\sigma_j^2}) + \log (P(\{B^j\}) \tag2 \label{eq2}$$
Can someone help me understand how the different terms in \eqref{eq2} are found? Is there a simple formula to take the likelihood expressed in \eqref{eq1} into \eqref{eq2}? (Knowing that the $\{v_{jl}\}$ are assumed to be normally distributed)

EDIT:
So indeed as @FabianWerner said, the second part of equation (2) $-\frac{1}{2}\ D\log(2\pi\sigma_0^2) + \sum_{jl} B_{il}^j \frac{\|v_{jl} - \hat{\theta_i}\|^2}{\sigma_j^2}$ simply comes from writing out the pdf of the normally distributed  variable $v_{jl}$ and applying common log transformations. I still haven't figured  why the first term and the sum over i $\sum_i(\frac{\|\hat{\theta_i}\|^2}{\sigma_0^2} ..)$ is here though..
Comment: I have the feeling that it is just using the natural rules for log like log(ab) = log(a) + log(b) and log(a/b) = log(a) - log(b). For example, $\log(...\cdot P(B^j)) = \log(...) + \log(P(B^j))$. That is how the last summand is derived. The rest should really just be those two rules above and filling in the definitions for the other $P$-terms...
Comment: Yes, for the sum I agree. But I don't see where the (-1/2) or the terms inside the parenthesis come from (in particular the norms?)
Comment: Humm... I don't know anything about this particular paper but let's start with the next simple term: $P(\{\Theta_i\}$. What is the definition of that term? According to what they claim it should be something like $\prod_i e^{|\Theta_i|/\sigma_0^2}$... that essentially means that the $|\Theta_i|$ are somewhat normally distributed... My guess (unfortunately, it is a very bad habit of mathematicians to mystify their work by not writing down explicitly what they are doing and by not using references appropriately) is that is goes like this:
Answer: Sorry, this is more of a comment than an answer but it didn't fit in the comment field...
In (2) they say that the $v_{jl}$ (with respect to something else I don't quite understand without getting into the details of this paper) are normally distributed. Then they say that they assume that the covariance matrices $\Sigma$ are just something like a*diag(1,1,...,1). Let's say that we have a random variable $X\sim N(\mu, \Sigma)$ then
$$p(x) = \text{const} ~~ e^{-\frac{1}{2} (x-\mu)^T \Sigma^{-1} (x-\mu)}$$
Now let's assume that $\mu = 0$ and $\Sigma = a \cdot \text{diag}(1,1,...)$ then the expression above simplifies:
$$p(x) = \text{const} \cdot e^{-\frac{1}{2a} x^Tx}$$
If $x = (x_1, ..., x_d)$ what is $x^Tx$? It is nothing else than $x_1^2 + ... + x_d^2 = |x|^2$. Hence,
$$\log(p(x)) = \log(\text{const}) + \left(-\frac{1}{2} \frac{1}{a} |x|^2 \right)$$
which looks pretty much like the expression that they have in the logarithm form of the likelihood...
So I guess that $P(\{v_{jl}\} | ...)$ becomes this term above and for some weird reason (I am by far not an expert in this MAP business) this MAP step allows them to drop the term $P(\{\theta_i\})$. Maybe after substituting this by an approximation is becomes constant and therefore is irrelevant for the optimization process or something like that...
Comment: Yep I guess it's something along those lines. I don't see why you would assume that $\mu$ = 0 in your example?
Comment: Otherwise you will end up with $|\theta - \text{something}|^2$ instead of just $|\theta|^2$...
Comment: yes I see.. I'll try to see if I can write it out given your hints, thanks (sorry not enough rep points to vote up)
Comment: @user1360448 No worries and good luck :-) Yes, please share what you figured out so that others can also benefit from it.


Criar e armazenar hash MD5 de valor existente após INSERT

Question: Possuo uma tabela X, com um campo chamado email e outro email_hash, gostaria de que toda vez que fosse inseriado um novo registro com email, O MySQL usaria o valor do email inserido e criasse um hash MD5 e inserisse no email_hash, como posso fazer isso? triggers?
Answer: Para encriptar um valor em MD5, basta utilizar a função de mesmo nome, por exemplo:
SELECT MD5('Psr');
        -> '0dd833a62f068acadd6604eec8daf236'

Com isso você pode utilizar o trigger_time BEFORE com o trigger_event INSERT. Por exemplo:
/* Cria a tabela com o campos necessários */
CREATE TABLE `user` (
    `id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `email` VARCHAR(40) NOT NULL,
    `email_hash` VARCHAR(32) NULL
);

delimiter //

/* Cria o trigger para setar, antes de inserir no DB, o valor do campo `email_hash` */
CREATE TRIGGER md5Email BEFORE INSERT ON `user`
FOR EACH ROW
BEGIN
    SET NEW.`email_hash` = MD5( NEW.`email` );
END;//

delimiter ;

/* Insere o valor */
INSERT INTO `user` (`email`) VALUES ("email@example.com");

Comment: Obrigado, é possível fazer isso usando o campo num valor DEFAULT? como para capturar a data atual com CURRENT_TIMESTAMP
Comment: @NGTHM4R3 Não é possível fazer dessa forma.
Comment: Beleza, obrigado


Argue, using symmetry, that the average distance between two points on a unit circle must exceed 1

Question: Textbook problem:

Two points are chosen at random on the circumference of a circle of radius $1$. Let $D$ be the average distance between them. Argue, using principles of symmetry, that $D$ must exceed $1$.

Textbook solution:

Suppose the first point chosen is at the north pole of the circle. I assert that the average vertical distance to the second point will be exactly $1$. Indeed, the possible second points can be paired up: a point at distance $h$ below the north pole paired up with a corresponding point at distance $h$ above the south pole, and the average vertical distance of these two points from the north pole is $1$. Since the average distance between the points must exceed the vertical distance, it must exceed $1$.

Question: Why is it valid to average the vertical distance of these two points to get the average vertical distance of any two points (the step in bold above)?  I understand the rest of the argument, but this step I do not see exactly why it holds.
To get the average vertical distance of these two points for this step I did the following:

North pole point has coordinates $(0,1)$
Second chosen point (in the first or second quadrant) on the circle has coordinates $(x,1-h)$ for some $x$
The paired-up corresponding point above the south pole has coordinates $(x,h-1)$
From the north pole, averaging the vertical distance of the two points is then $\frac{1-(1-h)+1-(h-1)}{2} = 1$ since $1-(1-h)$ is the vertical distance from the north pole to one point and $1-(h-1)$ is the vertical distance from the north pole to the other point

Note that I'm able to use the integral definition of average value of a function to find that the exact answer is $4/\pi$.
Comment: @UngarLinski If $X$ (resp. $Y$) is the vert. distance of a point from the North (resp. South) Pole, then $Y = 2-X$. So, by linearity of expectation $E(Y) = E(2-X) = E(2) - E(X) = 2 - E(X)$. By symmetry $E(Y)=E(X)$ so $E(X) = 2 - E(X)$ and thus $E(X) = 1$.
Comment: I would have expected the symmetry argument to be "the average distance from the north pole must be the same as the average distance to the south pole (proof: flip the circle over about its equator). Therefore it is 1"
Comment: @eyeballfrog That reminds me more of the concept of a median than an average because it sounds like it relies on something like half the time the point is here and half the time is there where the equator is the median or something.
Comment: @UngarLinski  Each point vertical distance from North Pole is $2 - $ (vert. list. from South Pole). So if the ave. vert. dist. from North Pole is $A$, then ave. vert. dist. from South P. is $2 - A$ (by linearity of expectation). But by symmetry these two averages are equal ...
Comment: @Ned Can you expand on this and show where or how you used linearity?
Answer: This is a valid proof because of the nature of right triangles.
Whichever point is chosen first, we can rotate the circle to make that the north pole. Since the distance to the south pole is two, the average vertical distance, as pointed out in the proof, is one. But the distance to any of the other points on the circle is the hypotenuse of a triangle with one leg equal to the vertical distance. Since the hypotenuse of the triangle is larger than any of its two legs, the average distance must be greater than the average vertical distance. Since the average vertical distance is one, the average distance must be greater than one.
EDIT: Through the discussion in the comments, Ungar's real difficulty was understanding why the average vertical distance is $1$. At his request, I paste the following comment from below into this answer.
We are trying to show that the average distance from the north pole to the south pole is $1$. Forget the circle and concentrate on the interval between $0$ and $2$. There is one point that is a distance of one away. For every other point, e.g. $\frac{1}{4}$, there is a point $\frac{7}{4}$. Their average is 1. For $\frac{1}{10}$, there is $\frac{19}{10}$ and their average is $1$. That's why the average is $1$. Every point that is not a distance of $1$ away is paired with a point whose distance is such that the average is $1$.
Comment: @UngarLinski No. Those points come in pairs. In other words, consider any vertical distance from the north pole. For any point at a fixed distance, there is a point at the same distance to the opposite side of the horizontal diameter with equal probability of being selected. Thus, the average is the distance to the horizontal diameter. It is also a median but it is the average.
Comment: @UngarLinski We are trying to show that the average distance from the north pole to the south pole is $1$. Forget the circle and concentrate on the interval between $0$ and $2$. There is one point that is a distance of one away. For every other point, e.g. $\frac{1}{4}$, there is a point $\frac{7}{4}$. Their average is $1$. For $\frac{1}{10}$, there is $\frac{19}{10}$ and their average is $1$. That's why the average is $1$. Every point that is not a distance of $1$ away is paired with a point whose distance is such that the average is $1$.
Comment: The triangle and hypotenuse part of the argument I get.  It's the part I put in boldface that I don't think I follow.  Your explanation above does not use the author's paired-up points along with the point at the north pole.  See what I mean?
Comment: @UngarLinski The part in boldface is the part where the average vertical distance is one, right? The average of a set of points is the place where you would place a fulcrum to balance them. If you have an interval $[a,b]$, the average is $\frac{a+b}{2}$. Is that your difficulty? The author is trying to prove that by saying every distance above the horizontal diameter is as equally likely as every distance below.
Comment: But then why does he use three points in making his argument?  Half the points on one side and half on the other is the median which is not always the average.  I think that's why he's using the vertical distance from the north pole to two separate points.  I hope I'm making sense.
Comment: So it seems like he's saying take the vertical distance from the north pole to two separate points, and then average these which gives you $1$.  But I am not clear if that is what he means.
Comment: That's it!  I was pairing them wrong.  That answers my question exactly.  I misinterpreted the part I boldfaced as saying to average the vertical distance from the north pole to point 1 with the vertical distance from the north pole to point 2.  This, coincidentally, also gives you $1$ but I could not wrap my head around why we would average two vertical distances like that.
Comment: Can you edit your comment into your answer so I can accept it?
Comment: @UngarLinski Done.
Comment: _"We are trying to show that the average distance from the north pole to the south pole is 1"_ - no, the average distance from one pole to the other is $2$. I believe this was a typo.
Comment: @paperskilltrees There s no typo. The distance to the south pole is $2$ so the average distance of all the points from the north pole to the south pole is $1$. I suppose you could be literal and say that there is only one point at the south pole and its distance is $2$ so, therefore, the average distance is $2$ but that would be ignoring the context of the conversation and a little pedantic.
Comment: @paperskilltrees Also, we were talking about the vertical distance. That was the part of the original proof that Ungar didn't understand.


Differences between two set of definitions of statistics

Question: What's the difference between statistics as a science and statistics as a collection of facts and figures
Comment: What are you asking? What are these definitions?
Comment: The former is about methods of compiling and analysing data, and the latter is the data produced by those methods
Answer: I think you are asking this - or asking for this answer:
Statistics, as a field of mathematics (also called mathematical statistics), is the study of random events and how to model them.  More focused yet, there is the notion of 'a statistic', which is a function of use in doing statistics.  For example the mean is a statistic, defined as the sum of points of data divided by the number of points.  But, note that 'a statistic' can be (and seem) a bit odd.  For example, if we want to make a statistical statement about the heights of people leaving an airplane, we can obviously use the mean to measure it.  But, we could also use just the height of the first person.  Surprisingly, this would be a perfectly good 'estimator', to use yet another term, in some ways.
I think your question part #2 relates to data collection.  For example, the US Bureau of labor statistics produces lots of data related to (as one example) unemployment through time.  There are endless libraries (offline and online) of statistics such as that.
Very often the tools and approaches of statistics, in the first sense, are applied to statistics in the latter sense.  And, loosely speaking, the study of how to apply those in the first sense to the data in the second sense is called the study of statistics.


Como implemetar MouseListeners numa GridLayout?

Question: Eu criei uma GUI para um Board de um jogo com uma GridLayout de 8x8. Nessa Grid preciso ter um mouse listener em cada um dos containers. Como é que consigo isso?
Já tentei de diversas formas e não estou a conseguir.
Code:
<code>    public static void createAndShowGUI() {
        GridLayout gl_Board = new GridLayout(0, 1, 5, 5);
        board = new Board();
        JPanel Board = new JPanel(gl_Board);
        Board.add(board);
        board.setLayout(new GridLayout(8, 8, 2, 2));
        Board.setBounds(10, 25, 708, 423);
        frame.getContentPane().add(Board);

</code>
Answer: Ola
Crie um botão e adicione um listener nele
Por exemplo
<code>JButton button = new JButton();
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("cliquei no botao");

            }
        });
</code>
Comment: Não se esqueça de adicionar o botao ao painel


Is there a way to do groupby in pandas such that same feature is used in both row and columns?

Question: Input table:
person    cat1    cat2
   P1        A    X
   P1        B    X
   P2        A    X
   P2        B    Y
   P3        A    X
   P3        B    Y

Required table:
             B
        X    Y    Z
     X  1    2    0
A    Y  0    0    0
     Z  0    0    0

The aggregation function can be unique persons - nunique()
Code till now:
df.groupby(['cat1','cat2']).person.nunique().unstack()
But this does not give the right table. Any help on how to do this?
Comment: why does it assumes that the count of A, X is equal to B, Y (perhaps this is why it cannot be done in an obvious way)?
Comment: P2 and P3 are essentially same rows with different person number
Comment: How is his operation called mathematically?
Answer: This is fairly ugly, but seems to do the job:
df1 = df.groupby('person').apply(lambda g:
    g.pivot_table('cat2', None, 'cat1', aggfunc=lambda x: x)
)
df2 = df1.reset_index().drop('level_1', axis=1)
df3 = df2.pivot_table(None, 'A', 'B', aggfunc=lambda x: len(x))

Now sure how efficient it is on "Big Data" (TM).
Essentially, df1 contains as many columns as the values for cat1, filled with the value in cat2:
cat1         A  B
person
p1     cat2  X  X
p2     cat2  X  Y
p3     cat2  X  Y

df2 is the same with clutter removed:
cat1 person  A  B
0        p1  X  X
1        p2  X  Y
2        p3  X  Y

and df3 contains the result:
  person
B      X  Y
A
X      1  2

Comment: not working. ```Shape of passed values is (2, 1), indices imply (1, 1)```
Comment: @Mysterious In your question, is `person` an index or a column in the dataframe?
Comment: it is a column in the dataframe
Comment: @Mysterious it works with your example though, `df = pd.DataFrame({'person': ['p1', 'p1', 'p2', 'p2', 'p3', 'p3'], 'cat1': ['A', 'B', 'A', 'B', 'A', 'B'], 'cat2': ['X', 'X', 'X','Y', 'X', 'Y']})`


One ng-options is dependent on what is selected in another - AngularJS

Question: I have a form in AngularJS where the options for one of the dropdowns depends on what is selected in the first.
<select ng-options="obj.name for obj in institutions track by obj.id" ng-model="newUser.institution">
</select>

<input type="text" name="email" ng-model="newUser.email">

<select ng-options="obj.name for obj in organizations track by obj.id" ng-model="newUser.associatedOrg">
</select>

Institutions are all loaded right off the bat from available institutions in the database. That is done in the controller like this:
function populate() {
    return Institutions.all().then(function (institutions) {
        $scope.institutions = institutions;
        return institutions;
    });
}

// at the end of the controller
populate();

However, those "organizations" in the second dropdown are based on their parent table institution, so I need to do something like $scope.organizations = institution.memberOrganizations; in the controller after an option from the first dropdown is selected.
For now, just to make sure things work, I have made a button called "Load Organizations" with an ng-click for this function:
$scope.getOrganizationsOnCampus = function(institution) {
    $scope.organizations = institution.memberOrganizations;
};

That works, however that is a really bad user experience.
So my question is: How do I update $scope.organizations every time a new institution is selected?
I want to do this without listening to the DOM - like you would in jQuery, because I know that is way against the AngularJS best practices.
P.S. For further clarity, here is a screenshot of before, and after that "Load Organizations" button is clicked, to load the child organizations of the selected institution. This is what I want to automatically do every time a different institution is selected in the previous option.
BEFORE

AFTER
Answer: Would you use ng-change like this:
<select ng-options="obj.name for obj in institutions track by obj.id" ng-model="newUser.institution" ng-change"updateOrg(newUser.institution)">
</select>

and then in your controller, you have a function like this:
$scope.updateOrg = function(institution) {
    $scope.organizations = institution.memberOrganizations;
};


How to have a static URL for serverless framework AWS deployments?

Question: When I use serverless framework to deploy with sls deploy to an AWS endpoint with lambdas and dynamodb, the host of the endpoint changes every time with a different prefix. This is a problem because if I release a client, I won't be able to deploy with serverless again.
For example, a host might look like this: 9svhw8numd.execute-api.us-east-1.amazonaws.com
That 9svhw8numd part changes each time there is a new deployment.
I've checked the serverless documentation and I can't seem to find anything that tells me how to configure it to have a static URL. How do I keep the host static for each serverless deployment?
Answer: What you're seeing is a URL for an AWS API Gateway instance. If you delete and re-create your serverless stack, a new endpoint will be generated. If you don't remove the stack, it'll stay the same throughout multiple serverless deploy commands.
If you'd like a custom domain instead of one generated by API Gateway, you'll need to configure a domain name via AWS Route 53. If you're using the Serverless Framework, here's a good guide to do that.


'Extended Secondary Dominant Chords'?

Question: Was asked about secondary dominants recently. Also about secondary extended dominants.
From a bit of research, it seems there are two views on what the latter are. Could be extended (dominant) chords - as in 9ths, 11ths, 13ths, with/out alterations. Usually in root position, with notes expanding past the octave. Or - dominant chords which move to another dominant , as in key C, the A7 that moves to D7.
I can understand extended chords as such, and also extended chord sequences, which the second definition nods towards. but the two definitions just don't appear to align.
I guess in the latter, key C, A7 going to Dm7 wouldn't qualify as an extended secondary (secondary extended?) chord. Any thoughts?
Answer: Yes, we know what a Secondary Dominant is.  There seems to be an alternative name - Extended Dominant.  Which is confusing, due to the more usual meaning of an Extended chord - 9ths and 13ths etc.   So better to stick with Secondary Dominant I think!
I've never heard of an Extended Secondary Dominant.  The references I can find to that term are queries rather than definitions.  So I guess we can't count it as a standard term.  If someone uses it, check what HE thinks it means!
An associated question.  In Dm7, G7, C is the first chord a Secondary Dominant?  Or must it be D7, G7, C to qualify?     If Bm7b5, E7, Am7, Dm7, G7, C (or any other mix of minor and major 7 chords) isn't a string of Secondary Dominants, it's something darn close! Maybe no leading note and tritone, but a definite dominant function.   So what do we call it?
Comment: In regards to your last sentence, I would just call it a circle of fifths. A "secondary" (or "applied") dominant, in my opinion, specifically suggests chromaticism that makes it a dominant-functioning chord. Thus the only true secondary dominant in your final sentence is the E7.
Comment: I would tend to agree; when I read the title, I interpreted it as chord extensions on dominants that happen to also serve the role of secondary dominant in a key center.
Answer: I think you already have the answer in your question.
In something like C: A7 D7 G7 C the secondary dominant "extend" back in the progression, but this kind of thing has a clear name: harmonic sequence.
Some people use the term backcycling.
Both just seem to be terms people have coined, because they don't know a (basic) term like harmonic sequence.
So, someone seems to be skipping over two existing terms and coining a new one.
Answer: Berklee College of Music uses the term "Extended Dominants" in their harmony curriculum.
Extended Dominants: A series of dominants without a direct key relationship, each one resolving down a fifth to the next.
source: https://college.berklee.edu/core/glossary.html
Comment: So, in key C, where D7 is present, but *doesn't* go directly to G/G7, that D7 *won't* be an 'extended dominant'?
Comment: Are you thinking of something like the A section to Take The A Train? I would not call that D7 an extended dominant or a secondary dominant, but some do call it a secondary dominant. Unless it sounds like it "wants" to resolve down a 5th or half step, I just call it "II7"

'Extended Secondary Dominant Chords'?