AlertDialog: Why can't I show Message and List together?

Question: Does anybody know why the AlertDialog doesn't show the list of items when I add a Message with .setMessage()?
The negative and positive buttons will be shown, but not the list.
When I delete the line with .setMessage() everything works.
This is my code:
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
myAlertDialog.setTitle("Options");
myAlertDialog.setMessage("Choose a color.");

CharSequence[] items = {"RED", "BLUE", "GREEN" };

myAlertDialog.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // do stuff
    }
});

myAlertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
       // do stuff
    }
});

myAlertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
       // do stuff
    }
});

myAlertDialog.create();
myAlertDialog.show();

Comment: Where you have added the list items ?
Comment: Why don't you put only "myAlertDialog.setTitle("Choose a color");" ?
Comment: @Alex Feh Check out my answer.
Comment: @GrIsHu The List is in the var "items" as an CharSequence[] as you can see in line 5 of the code. The items are added in the first parameter of .setSingleChouceItems() in line 7.
Comment: @JaAd This code only shows an example, in my code it would be great if i could use both.
Comment: @JaAd yes, i get your point here. But i have an layout that requires this look, i know stupid. But if it doesn't work i have to live with it.
The most thing that bothered me with this problem was that i wasted a lot of time not knowing that the .setMessage() was the problem why the list wasn't shown. Is there an convention or something for that?
Comment: @AlexFeh the only think you can do is a custom Dialog with a title, a text (in your case "Choose a color.") and a list below the text. By the way, giacomino's answer is good and if you don't want to loose more time, replace "Options" with "Choose a color." ;-) .
Answer: From the docs,

Because the list appears in the dialog's content area, the dialog cannot show both a message and a list and you should set a title for the dialog with setTitle().

The setMessage() and setSingleChoiceItems() are therefore mutually exclusive.
Answer: AlertDialog creates its content dynamically by checking if mMessage that you set via setMessage() is null. If mMessage is NOT null (that means you have called setMessage() method), then it setups its content for ONLY TextView to show message and skips listview setup. If you haven't called setMessage(), then it removes TextView and then setups for listview.
Solution
So, to show both message and list in dialog, I suggest you to use below solution which setups custom view for AlertDialog.
        final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Dialog title");

    final List<String> items = new ArrayList<>();

    items.add("Item 1");
    items.add("Item 2");
    items.add("Item 3");

    final ArrayAdapter<String> arrayAdapterItems = new ArrayAdapter<String>(
            getContext(), android.R.layout.simple_list_item_single_choice, items);

    View content = getLayoutInflater().inflate(R.layout.list_msg_dialog, null);

    //this is the TextView that displays your message
    TextView tvMessage = content.findViewById(R.id.tv_msg);

    tvMessage.setText("Dialog message!!!");

    //this is the ListView that lists your items
    final ListView lvItems = content.findViewById(R.id.lv_items);
    lvItems.setAdapter(arrayAdapterItems);
    lvItems.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    builder.setView(content);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //do something
            Toast.makeText(MainActivity.this,
                    items.get(lvItems.getCheckedItemPosition()),
                    Toast.LENGTH_SHORT).show();
        }
    });
    final AlertDialog dialog = builder.create();

    lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //when you need to act on itemClick
        }
    });

    dialog.show();

And this is list_msg_dialog.xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">

<TextView
    android:id="@+id/tv_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingTop="15dp"
    android:text="some text"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:textColor="@android:color/black"
    android:textSize="18sp" />

<ListView
    android:id="@+id/lv_items"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@null" /></LinearLayout>

Answer: It's a little tricky, but you can use myAlertDialog.setCustomTitle(myTextView) and custom yout textView as title and text with HTML format.
TextView textView = new TextView(context);
textView.setText("YourTitle\n\n Your body");
myAlertDialog.setCustomTitle(textView);

Comment: This was the answer I was looking for!
Comment: thank you, but html  not working here.
Answer: Try to set the items in your alertdialog as below:
  AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
  myAlertDialog.setTitle("Options");
  myAlertDialog.setMessage("Choose a color.");
  myAlertDialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
 CharSequence[] items = {"RED","BLUE","GREEN"};

 myAlertDialog.setItems(items, new DialogInterface.OnClickListener() {

 public void onClick(DialogInterface d, int choice) {

   }
  });
     ............
   myAlertDialog.create();
 myAlertDialog.show();

Answer: You can make your alert dialog with list using the following method:
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
myAlertDialog.setTitle("Options");
myAlertDialog.setMessage("Choose a color.");

List<String> items = new ArrayList<String>();
items.add("RED");
items.add("BLUE");
items.add("GREEN");

final ArrayAdapter<String> arrayAdapterItems = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_expandable_list_item_1, items);

myAlertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       // do stuff
    }
});
myAlertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       // do stuff
    }
});

myAlertDialog.setAdapter(arrayAdapterItems,
new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        //do what you want
    }
});
myAlertDialog.show();

Hope it helps!
Comment: This doesn't solve the problem, as above the message and items are mutually exclusive.
Comment: Yes, now the code works.
But unfortunately i have to remove .setMessage() to see the List.
But thank you, i like the way with the adapter!
Comment: At the line myAlertDialog.setAdapter() i get an error that the var items has to be an ListAdapter and not a List.
Did i do something wrong? I just copied your code.
Comment: No, sry it was my mistake. You were getting error because it was trying to set the List instead the ListAdapter, that I forgot to create. I editted my answer, now it should works!


Separability of $x^p-t$ over field of rational functions $\mathbb{F}_p(t)$

Question: First of all, $\mathbb{F}_p(t)$, the field of rational functions, has characteristic $p$, since it contains $\mathbb{F}_p$, right?
With that in mind, we can see that $x^p -t$ is not separable over this field, because its formal derivative is $px^{p-1} - 0 = 0$. And it holds for any $p$.
On the other hand, by fixing $p=3$, we can see that the following three values are roots of $x^3 -t$:
$\alpha_0 = t^{1/3}$, because $\alpha_0^3 = t$.
$\alpha_1 = -\sqrt[3]{-1}t^{1/3}$, because $\alpha_1^3 = (-1)^3(\sqrt[3]{-1})^3 (t^{1/3})^3 = -1 \cdot -1 \cdot t = t$.
$\alpha_2 = (-1)^{2/3}t^{1/p}$, because $\alpha_2^3 = ((-1)^{2/3})^3(t^{1/3})^3 = (-1)^2 \cdot t = t$.
Since $x^3 - t$ has three distinct roots, it is separable, isn't? And thus, we have a contradiction.
So, what is the point I'm missing?
Comment: Yes $\mathbb{F}_p$ is the subfield of $K= \mathbb{F}_p(t)$ generated by $1$, so that $\underbrace{1+\ldots+1}_p = p = 0$, thus $\forall a \in K, \underbrace{a+\ldots+a}_p = p a = 0$ i.e. $K$ has characteristic $p$
Answer: By writing "$\sqrt[3]{-1}$" you're assuming that the polynomial $x^3+1$ has a root other than $-1$, but $x^3+1=(x+1)^3$, so the only root is $-1$ and your three roots are all equal.
Comment: Yes, sure, I just have to take $\alpha = \sqrt[3]{t}$. Ok. I understand now. I can't just take new elements (like $\sqrt[3]{-1}$) and put them into the field to construct a extension without checking that those new elements are really new (in this case, it is like $\sqrt[3]{-1}$ was just $1$).
Comment: So, should I be able to write $x^3 - t$ as $(x - \alpha)^3$ or $(x - \alpha)(x - \beta)^2$?
Comment: Yes, it does hold that $x^3-t=(x-\alpha)^3$ in $\mathbb{F}_p(t)(\alpha)$. This follows from the fact that taking the third power is a homomorphism in characteristic 3.


Find $\lim_{x \rightarrow 0} \frac{e^{\sin x} - e^x}{\sin^3 2x}$

Question: I have to find $\displaystyle\lim_{x \rightarrow 0} \frac{e^{\sin x} - e^x}{\sin^3 2x}$ using Taylor polynomials.
Here's what I've done so far:

$e^x = 1 + x + \frac{1}{2} x^2 + \frac{1}{6} x^3 + o(x^3)$
$\sin x = x - \frac{1}{6} x^3 + o(x^4)$
$e^{\sin x} = 1 + x - \frac{1}{6} x^3 + o(x^4)$
$\sin 2x = 2x + o(x^2)$
$\sin^3 2x = 8x^3 + o(x^6)$

Therefore I can rewrite my limit as: $\displaystyle\lim_{x \rightarrow 0} \frac{-\frac{1}{3} x^3 - \frac{1}{2} x^2 + o(x^3)}{8x^3 + o(x^6)}$
In this form, the limit appers to be $-\frac{1}{24}$, but the correct result is $-\frac{1}{48}$.
Could you tell me what I'm doing wrong?
Comment: The first equation is wrong. correct one is $\mathbb{e}^x = 1 + x + \frac{1}{2} x^2 + \frac{1}{\color{red}{6}} x^3 + o(x^3)$
Comment: Argh, what a silly mistake! Fixed that and an another copy&paste error.
Comment: You forgot to add $\frac{1}{2}\sin^2 x=\frac{1}{2}x^2+o(x^3)$ and $\frac{1}{6}\sin^3 x=\frac{1}{6}x^3+o(x^3)$...
Answer: $\mathbb{e}^x = 1 + x + \frac{1}{2} x^2 + \frac{1}{6} x^3 + o(x^3)$ and $\sin x=x-\frac{1}{6}x^3+o(x^3)$
$\Rightarrow \mathbb{e}^{\sin x}=1+x-\frac{1}{6}x^3+\frac{1}{2}x^2+\frac{1}{6}x^3+o(x^3)=1+x+\frac{1}{2}x^2+o(x^3)$
$\Rightarrow \mathbb{e}^{\sin x}-\mathbb{e}^x=-\frac{1}{6}x^3$.
Answer: Let's rewrite the limit as
$$\displaystyle\lim_{x \rightarrow 0} e^x\times\lim_{x \rightarrow 0}\frac{e^{\sin x-x} - 1}{\sin x-x}\times\lim_{x \rightarrow 0}\left(\frac{2x}{\sin 2x}\right)^3\times \lim_{x \rightarrow 0}\frac{\sin x-x}{8x^3}=\lim_{x \rightarrow 0}\frac{x-x^3/6+O(x^5)-x}{8x^3}=-\frac{1}{48}$$
Q.E.D.


Is there a way to set block visibility programmatically without using the PHP Filter module?

Question: By enabling the core PHP Filter module, I can use PHP code to set the visibility of a block in the block configure page.
But I don't want to enable the PHP filter module for security measure. So is there a way to do it in custom module? I can't find a hook doing so. And a close hook hook_block_view_alter can only change the block content.
Answer: Yes. Setting the blocks content to false will hide it from the display and will hide the region if there are no other blocks present. I'd imagine you would need a setup similar to the following:
$block['content'] = _my_module_custom_block();

...

function _my_module_custom_block(){
    if( HIDE_CONDITION )
        return false;
}

Comment: wouldn't it still show a block with empty content?
Comment: works, great. thx.
Comment: @gilzero , no it would not.
Answer: The Rules block visibility module is another alternative available ... which doesn't require the PHP filter to be enabled. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.
The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless.
Need to show a block only for users registered more than a month ago?
Perhaps you have a block that must be shown only between 8am-5pm on weekdays?
What about displaying or hiding a block based on current weather conditions?
All of this can be done by using Rules block visibility.

Moreover, as per the "if you can do it with Rules, you can use it to control block visibility" above, you have the power of Rules available to implement all sorts of custom logic (cfr. the "programmatically / custom module" part of your question).
For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?".


How to find meta tag source?

Question: I have a site where the conditional,

How to find meta tag source?


how to call a function on load of an element?

Question: I am trying to call a function onload of elements so that I can specify different changes of each elements I am calling the function.
like i have many divs and each and every div has same data-attribure, I am calling all divs with same data-attribute. Each and every div has different html text, I want to call them, when I am loading the page and get those different values. Like in jQuery we call 'each' function. Here I am submitting my html and javascript code... that will draw a clear picture of it.
   <-- html -->

        1000

        420

        2000

    var abc = document.querySelectorAll('[data-attribute]');
        for (i= 0; i < abc.length; i++) {
            abc[i].onload = function(){
            // --------- working well with click / mousemove events -----
               console.log('function called!');
            };
        }

I don't want to use any library or don't want to call the function on element.
P.S. also let me know if there is other option to do the same.
Thank you..
Comment: Your elements are static so they must load before you can execute your javascript otherwise javascript will not be able to find the element to check the load state... For this I think you will need to create the elements once the page has loaded, then you should be able to use `onload` or a `load` event listener depending on what sort of elements you're loading. I'm finding it hard to understand your question so this is the best I can offer at this moment in time.
Comment: As @NewToJS hints, you have a "chicken and egg" problem. You cant assign an event handler until an element has loaded. What are you actually trying to achieve inside this `onload` handler? It may be that you can perform an action when the *whole page* has loaded, and vary the behaviour depending on some other attribute of the elements.
Comment: @Jamiec Yes , I  want to perform an action when the whole page has loaded, and vary the behaviour depending on some other attribute of the elements.
Answer: You could wait for the entire DOM to load, and perform any actions you require.
window.onload = function(){
    var abc = document.querySelectorAll('[data-attribute]');
    for (i= 0; i < abc.length; i++) {
         var elem = abc[0];
         if(elem.getAttribute("data-attribute") == "one"){
             // do something here
         }
    }
}

You can expand this idea for as many different attributes/elements as you need.
Answer: Ready and Load are executed upon document object model. So, you cannot call them on a specific element. In this case, you can use set-timeout function after the DOM is ready to change on multiple elements.
Comment: Actually, this answer is right. I've tried all of 'DOMContentLoaded', 'load' and 'ready', and doesn't work. I found this answer, and tried with `setTimeout()` function and it works well.


My VM instances on Google Cloud Platform do not work after increasing SSD size

Question: Could you please help fix my emergent Google Cloud Platform problem? The configuration information as follow:

The VM instances name is "cchatty"
Machine type: n1-standard-1 (1 vCPU, 3.75 GB memory)
CPU platform: Intel Broadwell
Zone: us-west1-c
Service account: firstname.lastname@example.com

Today, I increased my SSD size of VM instances, and restarted it, after that I can not open this instances, and my website on this instance has also shut the service, the site is www.cchatty.com. So I want to log in the shell via SSH, but it is also failed and told me the port 22 issues "Connection Failed. We are unable to connect to the VM on port 22. Learn more about possible causes of this issue."
Now our website cannot be open and also we can not log in the Linux shell. That's very serious for us, our website service is shut down for customers.
By the way, we have another instance group, it works well, we don't increase the SSD size. We think it must some problem during increase the SSD size.
Comment: I log in with gcloud, I found all working files deleted, it looks reset after increasing SSD size.
Comment: that's fixed, it is the CentOS 6 issue. thanks all.
Answer: There is a known issue on  Centos 6 when the kernel is updated to 2.6.32-754.el6 version the VM instance will lose their default gateway. Which is already fixed and CentOS 6 has updated packages available now. However if you are not using the updated package and experiencing the same issue, you can follow the following workaroud:
you can add a simple bash script to the start-up script with the following:
"#!/bin/bash
route add default gw [default_gateway_ip] eth0"
Where "[default_gateway_ip]" is the default gateway IP address of the VM instances region. You can find this IP address by doing the following:

In the Console go to VPC Network>VPC Networks
Use the Gateway IP address that is used for the Region where your VM instance is being hosted from.

After you add this to the Startup script on the VM instance and start it back up, you should now have network connectivity again.
Also in order to implement a Startup script on a existing VM instance, do the following:

Stop the VM instance
Follow the steps in the attached article starting at Step 3 where it says to expand Management, disk, networking, SSH keys section.
Save the changes
Start the VM instance


C# LINQ Join com condição OR?

Question: Estou precisando de escrever esse SQL em Linq, porém na condição desse JOIN é o que me pega.
SELECT * FROM
Boleto_Retorno AS BR
LEFT JOIN Recebimentos AS R ON SUBSTRING(BR.CH_NossoNumero,1,11) =
CASE WHEN LEN(BR.CH_NossoNumero) = 12
THEN SUBSTRING(R.CH_NossoNumero,1,11)
ELSE NULL END
OR BR.CH_NossoNumero =
CASE WHEN LEN(BR.CH_NossoNumero) > 12
THEN R.CH_NossoNumero
ELSE NULL END
LEFT JOIN Clientes AS C ON C.CD_Cliente = R.CD_CodigoCliente

WHERE (BR.VF_Processado = 'true')
AND (BR.DT_DataRecebimento BETWEEN @DataInicial AND @DataFinal)

Já tentei fazer assim
from Boleto_Retorno in db.Boleto_Retorno.AsNoTracking()
    where Boleto_Retorno.VF_Processado == true
    && Boleto_Retorno.DT_DataRecebimento >= dataInicial
    && Boleto_Retorno.DT_DataRecebimento <= dataFinal
from Recebimentos in db.Recebimentos
where Boleto_Retorno.CH_NossoNumero.Substring(1, 11) ==
    (
        Boleto_Retorno.CH_NossoNumero.Length == 12 ? Recebimentos.CH_NossoNumero.Substring(1, 11) : null)
        || Boleto_Retorno.CH_NossoNumero == (Boleto_Retorno.CH_NossoNumero.Length > 12 ? Recebimentos.CH_NossoNumero : null
    )

select new ModelBoletoRetorno{ //campos da tabela}).ToList();

mas não retornar igual a instrução SQL o retorna umas 9 linhas e a expressão Linq retorna umas 420.000 linhas.
Observação: ignorem o select *
Comment: Essa SQL não é traduzido corretamente para Linq, o ideal, faça uma `View` e chame pela camada de persistência.
Comment: esse segundo `WHERE` que você adicionou deveria ser na verdade um `ON`, porém, não sei se será possível montar essa expressão, então sugiro que você crie uma `procedure` no banco que execute a consulta original em SQL à partir de um filtro de data de recebimento que você informará. A ideia da `View` sugerida acima também é uma boa opção, porém, como vejo que existe filtro na consulta (principalmente de data), talvez a procedure seja mais performática.
Comment: esse sql já é uma procedure, e estamos tirando elas e "convertendo" em linq. Eu montei de outro jeito, um foreach da lista do Boleto_Retorno, e um metodo que faz uma busca (firstOrDefault) no recebimentos com as condições feitas da procedure. Embora tive que ser obrigado a ir por esse caminho ja que nao é possivel via linq. Mas deu certo.
Answer: Falta o join
Nesse caso a estrutura deveria ficar próxima a isso
var query = (from ta in ctx.TableA
             join tb in ctx.TableB
               on ta.Id equals tb.Id into TJoin
             from Tables tbs in TJoin.DefaultIfEmpty()
             where tbs.Id != 2
             select new TablesJoinViewMode
             {
                 Id1 = ta.ID,
                 Id2 - tb.Id
                 /* set other properties*/
             });

C# LINQ Join com condição OR?


UWP Desktop e banco de dados

Question: Estou desenvolvendo uma aplicação para fins de gestão de negócios relacionados a locações. O trivial CRUD, relatórios, controles etc, comumente desenvolvidas em windows forms. Porém, estou fazendo uma aplicação UWP.
O dilema começa no fato de que aplicações UWP não podem acessar as classes ADO.NET, sendo assim a única maneira (que eu sabia) de apps UWP trabalharem com banco de dados é via WebService.
O problema é que estou tendo certa dificuldade para tentar encaixar toda a regra relacionada a persistência de dados dentro do WebService.
Busquei por informações de qual seria a melhor forma de fazer isso e encontrei sugestões de criar um serviço para cada entidade a ser persistida na base de dados, mas não acho uma solução muito prática e "elegante".
Também pensei em criar métodos de CRUD genéricos que pudessem persistir qualquer tipo de entidade, ou seja, um unico insert(AnyType anyData) para inserir qualquer conjunto de dados na sua tabela apropriada. Mas não consegui avançar no raciocínio, pois cada entidade é muito particular. Umas tem mais propriedades que outras, outras tem propriedades do tipo coleções (List, Dictionary), que serão persistidas em tabelas separadas (Exemplo: LOCAÇÃO e ITENS_DA_LOCAÇÃO), outras não tem, umas são extremamente simples outras mais complexas.
Em fim, criar um métodos de CRUD genéricos não é uma tarefa simples.
Quais outras solução eu poderia tentar nesse caso?
Comment: Não sei dizer se não pode acessar o banco de dados, pode até ser que não possa usar o ADO.NET, mas é restrição específica. Precisa ver se precisa fazer UWP, pode ser que não seja a opção mais adequada. Mesmo que faça e que **queira* acessar o banco de dados de outra fonte, existem diversas outras formas de fazer isso sem que seja um webservice. Pode ser que o UWP tenha alguma restrição que eu não sei, mas se isso for verdade pra mim ele ñ serve pra praticamente nada, até criar uma aplicação no iOS pode mais.
Comment: Quase tudo que tem na internet são soluções complexas para problemas que deveriam ser complexos, só que a maioria dos problemas são simples.
Comment: Resolvi fazer via UWP para tirar proveito dos recursos visuais do UWP. Existe o `WPF` mas UWP introduz muita coisa nova e torna mais fácil a criação de certos componentes que em `WPF` seria doloroso de fazer.
Para se criar um `HamburgerMenu` no `WPF` é um parto, já no `UWP` existe o componente pronto.
Você disse que haveria outras formas para trabalhar com `UWP` e banco de dados. Quais seriam?
Comment: Nunca usei UWP e não conheço bem, mas além ser ser claro que tem que poder, tem algumas coisas por aí: https://code.msdn.microsoft.com/How-to-access-data-from-5f2602ec, https://docs.microsoft.com/en-us/windows/uwp/data-access/, https://blogs.windows.com/buildingapps/2017/02/06/using-sqlite-databases-uwp-apps/#9u5tL2h4WuwOhpGr.97. POde acessar o banco de dados através de um serviço, mas não precisa ser webservice, talvez por TCP/IP, tem muitas formas
Comment: Caso seja uma aplicação monousuário poderia optar por `SQLite` (armazenamento local). Outras situações no momento não vejo uma solução que não seja algo relacionado a serviço. Não se trata exatamente como você informa na pergunta `'um serviço para cada entidade'`, um serviço atende sim todas entidades. Também estou começando a aprender essa parte de `webservice` dotnetcore já tem muito material sobre o assunto, junto com `efcore` já dá para começar a criar serviços. Trata-se de tecnologias recentes mas a tendência e evoluir.
Comment: A função do `WebService` não seria exatamente trabalhar com manipulação de dados e sim receber e responder as solicitações dos clientes. O trabalho de manipulação dos dados ficaria a cargo por exemplo de uma   `dll (Projeto c#)`. Recomendo (caso ainda não conheça) aprender sobre efcore (entity framework core) https://docs.microsoft.com/en-us/ef/core/index
Answer: Nesse seu caso, recomendo o estudo do Entity Framework Core, que poderá usar bancos de dados SQLite e cujo mapeamento é totalmente automático em cenários mais simples, em que cada tabela "filho" tem apenas uma tabela "pai".
Caso seja necessário que outras máquinas modifiquem apenas um banco de dados "central" poderá ser feito o uso de sockets, recomendo usar o Restup, que basicamente transformará seu app em um servidor REST, perfeito para esse tipo de caso, qualquer dúvida é só falar.
Comment: Olá! No meu caso a aplicação UWP deverá persistir os dados em um banco de dados na nuvem, onde outros usuário também irão persistir. Resumidamente será uma instancia SQL server no azure onde as aplicações UWP devem persistir.
Nesse caso você me recomendaria Sockets?
Comment: Sim, isso é o mais recomendado.

UWP Desktop e banco de dados


Handle "possible SYN flooding on port 443. Sending cookies."

Question: My website constantly has several users online all the time.
The server uses Apache/PHP, database and Memcached. On normal use, the application works well and fast.
However, the server appears to sometimes have "SYN flooding" attacks. I really believe/suspect these are intentional and not caused by our legit users. (as they appear to happen when there are some individuals that register new accounts and try to cause trouble)

Jun 27 22:12:21 xxxx kernel: [xxxx.xxxx] possible SYN flooding on port
  443. Sending cookies. Jun 27 22:13:22 xxxx kernel: [xxxx.xxxx] possible SYN flooding on port 443. Sending cookies. Jun 27 22:14:25
  xxxx kernel: [xxxx.xxxx] possible SYN flooding on port 443. Sending
  cookies.

Unfortunately, when that happens, my whole traffic is affected:

[Mon Jun 27 22:15:28.842067 2016] [mpm_event:error] [pid 12022:tid
  132875292207712] AH00485: scoreboard is full, not at MaxRequestWorkers

My MaxRequestWorkers is 600. I have increased it a few times in the past.
Recently I also have increased net.ipv4.tcp_max_syn_backlog and ListenBackLog to 5000.
My server has 16GB RAM and 1 Gbps bandwidth.
I am not really happy that there appears to be someone that can easily control whether my website is alive or not.
What can one do to stop this?
Also, netstat seems to give me the IPs connected to the server right now.
Is it possible to get the top IPs at a given time in the past?
Answer:
Syn flood is kind of attack that is near to impossible to protect on
single host.

Check SynCookies
cat /proc/sys/net/ipv4/tcp_syncookies

and enable (set to 1) it if disabled.
It help legit users keep working.
Also You can try set lover /proc/sys/net/ipv4/tcp_synack_retries
https://stackoverflow.com/a/26674591/205355

About IP

Synflood usually use spoofed random source IPs, so it can't be filtered based on source IP.

As long as Your service is public, anyone can easily check it liveliness
You can buy professional DDOS protection service. It use huge regional desributed cluster, geolocation aware DNS, corelation, ISP cooperation and more. It redirect clean user traffic to Your service on IP unknown to public (and hackers). But it can cost a lot, and can depends on power needed to survive and mitigate attack.
Comment: Just idea, make log of Syn in firewall and parse that log by awk or by script in preferred language, to count syn from same IP.

Because of reason 3, more interesting to analyze web server log and try to detect IP of who check liviness of your site. It must be repeating one simple query. But it can be distributed on many IPs.

If You found that IPs (its real, not a fake) You can try imitate attack success for that IPs.

But this is challenge of shield and sword. Not a ultimate solution.
Comment: From other side, You can try guess who benefits from Your site down? Possible it unfair competition?


why don't DHCP discover/ARP messages amplify and reverberate in WANs?

Question: I don't understand how the ISP can assign Public IPs to routers that newly join their network, without having DHCP or ARP messages amplified millionfold.
As far as I know, for a L3 router to join a network at all, the joining entity has to talk with the DHCP server to get an IP address. DHCP discover messages are broadcast with MAC FF:FF:FF:FF:FF:FF, and to the whole subnet. And so, if the router is newly connected to a WAN with thousands, if not ~100000s of other routers, I would imagine the result to be a DHCP discover message that reverberates and amplifies until its TTL expires - which is certain to either a) fail to reach target or b) cause millions, not if billions, of other messages.
And, facing the same direction, I can apply the same argument to ARPs. ARP messages are broadcast as well around the network, just like the DHCP discover, and so the same set of problems would arise.
I can probably apply the same argument to messages used by the Network layer to coordinate its routers with distance-vector algorithm, unless the routers are somehow organized in a tree or graph-like manner, but I digress.
Where have I gone wrong?
Comment: why do you think the broadcast frame would "reverberate and amplify" when it's sent to the whole subnet (or rather, L2 broadcast domain)?
Answer: You make a lot of assumptions that simply aren't true.
Topologies vary, but an ISP's network is not one flat broadcast domain, so broadcasts aren't amplified a million-fold.  Routers block broadcasts, so they are limited to one subnet.
ISPs can filter broadcasts further, and do many other "tricks" to limit DHCP and ARP messages to a small set of devices.
One more thing:  distance vector routing algorithms (like RIP) are essentially obsolete.  ISPs generally use OSPF or IS-IS internally, and BGP to connect with their customers and peers.
Comment: It sounds weird to you because there are lots of networking features that you're unfamiliar with.  Routers can forward DHCP broadcasts (as unicast) to one central DHCP server.
Comment: OK, I get that they would try to filter stuff out, but this sounds even weirder to me. If routers block broadcasts, how are the ISP's DHCP server(s) even reached? Surely the DHCP server can't be in every subnet in the network...?
Comment: Ah, that makes sense. Thank you very much.
Answer:
As far as I know, for a L3 router to join a network at all, the joining entity has to talk with the DHCP server to get an IP address. DHCP discover messages are broadcast with MAC FF:FF:FF:FF:FF:FF, and to the whole subnet.

these messages are broadcasted in a single layer 2 domain. broadcast does not cross layer 2 boundaries.
also, DHCP is not a common case for routers. Many times, routers need to be pre-configured with IP addresses, and automatic address assignment is not happening at all. I would assume that in ISP WANs this is actually the case, not the DHCP part.
If you are talking about a home router that connects to ISP network, then DHCP is processed on the other side of the link, and is not broadcasted further.

And so, if the router is newly connected to a WAN with thousands,

WAN with that number of routers is not a single layer 2 domain. Also router is layer 3 device. The broadcast cannot go past the layer 2 boundary, that is it stops at the next router.
I would assume that this router shold know where to route DHCP messages or is capable of answering them himself. I however do not know.

And, facing the same direction, I can apply the same argument to ARPs. ARP messages are broadcast as well around the network, just like the DHCP discover, and so the same set of problems would arise.

ARP is also broadcasted in the same layer 2 domain. ARP actually does not make sence across layer 2 boundary, since layer 2 communication is only used between two neighbor devices attached to the same layer 2 domain.
How to scale layer 2 is also a valid question, but it is a separate question. For your scenario it is safe to assume that the number of devices in a single layer 2 domain is small.

I can probably apply the same argument to messages used by the Network layer to coordinate its routers with distance-vector algorithm, unless the routers are somehow organized in a tree or graph-like manner, but I digress.

It kinda does. Routing protocols have mechanisms that are used to keep the number of network layer messages small enough. These mechanisms have their limitations, which renders routing protocols not applicable in some scenarios. Since the functionality and mechanisms are different, this requires discussing each protocol separately.
Organizing routers in a tree is pretty bad for the redundancy standpoint. If a link fails, there is no place in topology to reroute. (Here, tree refers to the actual physical topology. (R)STP reduces topology to a tree, but if links fail, new tree can be calculated.)
And every network can be modelled as a graph, since graphs can have any number of nodes, and any number of links, and be arbitrary connected.


Guitar Pro voicings limitation. Need Guitar Pro expert!

Question: In a nutshell, I'm trying to move a rest, because Guitar Pro (perhaps rightly) sees a minor 3rd and won't allow them to be separate voicings. In the picture below you'll see the quarter rest should be ABOVE the whole note and then the measure would look fine! I've tried toying with all 4 voicings and "multi-voice" mode, but cant seem to find a solution ( I need autonomy, which is one area I'm finding Guitar Pro really falls short!)
As you can see, I'm trying to avoid having to tie the F note throughout the measure which is tie-overkill in my opinion - messy looking and harder to read.  Let me know if you have any recommendations. Of course, one fix is just to make the F a quarter note and leave it up to the player, but I'm transcribing a piece and I hate to sacrifice precision if I don't have to.
Comment: Sorry I don't understand what you are trying to do. I can confirm that you can't choose the location of the rest (I agree the automatic position is wrong, it should be above), but anyone who can properly read a sheet music will see what it means. What I see on your screenshot is that the notes are on their own voicings, right ? It's just the rest that is not correctly positioned ? You may try to hide the rest (I think you can do that in the Stylesheet - F7 shortcut)
Comment: As you wish, but then you change the music ;) (Guitar Pro won't let the note ring, and the reader would probably not understand it either).
Comment: Yea, but it still looks unprofessional even if the reader can figure it out. Thanks anyway. I think it's more practical just to make it a quarter note and move along.
Comment: It would be impossible for the f note to last a whole beat. I think it would be a half beat if anything.
Comment: Good observation, that is certainly true. I just made it a quarter for simplicity. Hopefully the player will have the sense of taste to hold it a beat.
Comment: I do not know if I understand correctly, and I think it happens just like other users. Why not post what you want to do, the original score (even if it is handwritten)? In any case sure that in Finale you do not have this problem.
Comment: Still no solution in Guitar Pro 7.5. A workaround would be to export the piece to MusicXML, and import the file in another software and maybe edit the position of the "rest" from there.
Answer: Guitar Pro has a Let ring feature, which will essentially act as if you have held the note and is common in guitar tab. The limitations of this are the same as the limitations of writing a longer note - it will be interrupted by rests or by other notes being played on the same string, and the note's volume will still eventually decay similar to holding down a note.
To use it, highlight the note/notes you wish to ring out and either find it in the UI (it's in a different place in every iteration of Guitar Pro I've used), find it in the Tools menu, or use the keyboard shortcut i.


some custom UIView lost after my iPad app received memory warning

Question: I'm developing an app to browse online photos, and sometimes received memory warning(level 1), after that, if I go back to last UIViewController(in a NavigationControll), I found that some custom UIView lost, I cannot get them back, but I can create new such UIView
does anyone know the problem?
Answer: The memory warning prompts the os to dump any unneeded views. If you create them in IB, or in the viewDidLoad or loadView method in your view controller they'll be re-created when your view re-loads. You could also have your view controller retain them specifically, but that'll have a larger memory footprint than just re-creating them whenever they're needed.
I should say, loadView is only called if you don't use a nib for creating your view, so viewDidLoad is probably where you want to put them.
Answer: It's expected behavior. You can override didReceiveMemoryWarning (i.e. do nothing and don't call on super); but what you really want is to handle those situations correctly, i.e. set the views up again if necessary.
Comment: What do you mean by "don't call on super"? I think you must call, doesn't it?
Comment: You don't have to call [super didReceiveMemoryWarning]. But I'd say it's a hack, and don't really recommend that.


p-value as a distance?

Question: Can p-values between multiple pair-wise tests be considered as a similarity/distance measure and multidimensional scaling be applied over a pair-wise matrix of p-values to reduce the dimensionality? This is a soft-question, but what would be the biggest issue here, and how could that be best overcome? (ex: triangular inequality?)
Comment: My short answer to your first question  about "similarity distance" would be No , you may refer to similar question on a different matter here: http://stats.stackexchange.com/questions/32890/can-p-values-be-used-to-show-impact-of-treatment/32897#32897 . So this posts would suggest other "divergence" or distance measures. I guess it's better to first adress your first question.
Answer: If all the "true distances" are 0, then the p-values will follow a uniform distribution and would just be random, incorrect distances.
If the true distances are not 0 then you still have scaling issues where a test statisic may be more meaningful.  P-values of 0.9 and 0.6 are not very different in interpretation while p-values of 0.06 and 0.01 are quite different in interpretation, but the mds algorithms would put more distance between the former than the later.
You should also consider power, you may have 2 groups that have a very small distance between them, but large sample sizes so you get a small p-value; then another pair with a large difference between them, but due to small sample size (low power) you get a larger p-value.
Answer: A specific case, where the p-values are generated from $\chi ^2$ tests over frequency tables were used as similarities and multidimensional scaling was applied in this paper:
http://www.biomedcentral.com/content/pdf/1748-7188-1-10.pdf
Answer: I believe the answer is yes.
One could think of the "similarity" between two variables to be measured by (say) correlation.  And for the p-value to be the significance of the correlation being different than 0.  In such a case, a small p-value (nearing zero) is one that reflects a large distance ("difference") between the variables.
You could turn the p-values into z scores (where the "distance" of them will have to "usual" direction), and see if the methods you mentioned will make sense on that...
Answer: I am not sure what you mean by "p-values between multiple pair-wise tests ".  The p-value is a measure of how likely / unlikely for a particular test it would be to see a value as extreme or more extreme than what was actually observed if the null hypothesis is true.  When doing pairwise testing there is no particular connection between one p-value and another.  I do not see how any p-values could be looked at as a similarity measure between pairwise tests.

p-value as a distance?


A triangle has side lengths 4,6,8. A tangent is drawn to incircle parallel to side 4 cutting .....

Question: Problem :
A triangle has side lengths $4,6,8$. A tangent is drawn to incircle parallel to side $4$ cutting other two sides at M and N, than length of MN is
(a) $\frac{10}{9}$
(b) $\frac{20}{ 9}$
(c) $\frac{5}{3}$
(d) $\frac{4}{3}$
I have no clue how to proceed such problem however, please help on this I will be greatful to you thanks...
Answer: Let the triangle be $ABC$, such that $M$ is on $AC$, and $N$ is on $AB$ and $x=MN$.
As $ABC$ and $AMN$ are similar we want to derive the scaling factor $k$ by calculating their areas. Let the games begin!
Let $F_1$ be the area of $ABC$, $F_2$ the area of $AMN$ and $F_3$ the area of $NMBC$. From Heron we know $F_1=3\sqrt{15}$ and from $F_1=s\cdot r$ (where $s=(a+b+c)/2$ and $r$ is the radius of the inscribed circle) we get $r=\sqrt{15}/3$.  Now $F_3$ is the area of a trapezoid, so $F_3=\dfrac{x+4}{2}\cdot2r=(x+4)\dfrac{\sqrt{15}}{3}$.  A short computation -- recalling that $F_2=F_1-F_3$ -- reveals that
$$k^2=\frac{F_2}{F_1}=\frac{5-x}{9}\quad\text{hence}\quad k=\frac{\sqrt{5-x}}{3}.$$
Grand finale: From $x=4k$ we'll happily derive $x=20/9$.
Answer: I was waiting for a more elegant solution to come up, but has not, so I will go ahead. Let the triangle be $ABC$, such that $M$ is on $AC$, and $N$ is on $AB$. Let $AM = x$, $AN = y$, and $MN = z$, and $\angle BAC = \theta$. Since, triangles $ABC$ and $ANM$ are similar due to AA criterion, we have the following relations: $y = \frac{4x}{3}$ and $z= \frac{2x}{3}$.   $(1)$
Now, the area of the triangle $AMN$ can be given by $\frac{1}{2}xy\cdot \sin\theta$. Since $BNMC$ is a tangential quadrilateral, its area is given by the formula $rs_1$, where $r$ is the inradius and $s$ is the semiperimeter. Summing both of them gives us the area of triangle $ABC$, which can be given by the formula, $rs_2$, as well, but note that their semiperimeters would be different though the inradius would be the same. We can find that $s_2=9$, which gives us the equation:
$$rs_1 + \frac{1}{2}xy\cdot \sin\theta = rs_2 = 9r$$
So, our first task is to eliminate $\sin\theta$. In order to that, we have to eliminate $s_1$ as well:
$$s_1=\frac{4 + (6 -x)+z+(8-y)}{2} = 9 - \frac{x+y}{2}+\frac{z}{2} =9 - \frac{x+y}{2}+\frac{x}{3}$$
This gives:
$$ \sin\theta = \frac{2r}{xy}(\frac{x+y}{2} - \frac{x}{3})$$
Notice that the area of $ABC$ can be given by $\frac{1}{2}\cdot6\cdot8\cdot \sin\theta =24\cdot \sin\theta$. So,
$$24\cdot \sin\theta = \frac{48r}{xy}(\frac{x+y}{2} - \frac{x}{3}) =9r $$
Cancelling the $r$ [and a bit of manipulation] and substituting for $y$ from $(1)$ gives us:
$$\frac{1}{2}(x+\frac{5x}{3})= \frac{x^2}{4}$$
Solving the [actually linear] equation gives us , $x = \frac{10}{3}$. Now we have reached our final destination, to get the final answer, substitute back from (1), which gives:
$$MN = z = \frac{2x}{3} = \frac{2\cdot\frac{10}{3}}{3}= \frac{20}{9}$$
BTW: Although the length of explanation suggests it took 10 - 20 minutes to solve the question, it took me only 2 - 3. Its surely a long method, but fast and effective nonetheless. When trying to solve questions like this, and not looking to come up with especially elegant solutions, equating [and summing if required] some property of the geometric figures from different aspects will always help, and area is always a good try.
Comment: Whew! How come I forget the Herons formula, which could have made it a bit easier, as Michael Hoppe used. Although the concept is very much the same.
Answer: The height of ∆ABC = (3√15)/4.2=(3.√15)/3 . Therefore, the height of ∆ CMN = (3.√15)/2-2r = (3.√15)/2-2√15/3 = (5.√15)/6.
So, MN/4=((5√15)/6)/((3.√15)/2)  = 5/(9 ) ,i.e., MN = 20/9.
Comment: Please, format your calculations using MathJax so that they are easier to read.


Let $A_{j,k} = \langle x_j, x_k\rangle$. Show $A$ is invertible if and only if $x_1, \ldots, x_n$ are linearly independent.

Question: Let $V$ be a vector space over $\mathbb C$ with inner product $\langle, \rangle$ and let $x_1, \ldots, x_n$ be vectors in $V$.
Consider the $n \times n$-matrix $A$ with entries $A_{j,k} = \langle x_j, x_k\rangle$.
I want to show that $A$ is invertible if and only if $x_1, \ldots, x_n$ are linearly independent.
I know that orthogonal vectors are linearly independent, so if $x_1, \ldots, x_n$ where orthogonal the result is easily proven. However, the general case I'm stuck at.
Comment: you are right..
Answer: In order to simplify the notation, set $A_{jk}=\langle x_k,x_j\rangle$ instead. Since $A$ is Hermitian, it does not really matter. If you insist on using "your" $A$, just replace $A$ with $\bar{A}$ everywhere.
Let $y:=[\eta_1,\ldots,\eta_n]^T$ denote a vector in $\mathbb{C}^n$.
Let $x_1,\ldots,x_n$ be linearly dependent. Then for some $y\in\mathbb{C}^n\setminus\{0\}$, $\eta_1x_1+\cdots+\eta_nx_n=0$. Making products with $x_j$ gives
$$
\eta_1\langle x_1,x_j\rangle+\cdots+\eta_n\langle x_n,x_j\rangle=0,
\quad j=1,\ldots,n,
$$
which is equivalent to $Ay=0$. Since $y\neq 0$, it follows that $A$ is singular.
Note that
$$\tag{$*$}\langle\eta_1x_1+\cdots+\eta_nx_n,\eta_1x_1+\cdots+\eta_nx_n\rangle
=\sum_{i,j=1}^n\eta_i\bar{\eta_j}\langle x_i,x_j\rangle=y^*Ay$$
is zero if and only if $\eta_1x_1+\cdots+\eta_nx_n=0$ (because the left-hand side of ($*$) is the square of the norm induced by the inner product).
If $A$ is singular, again, there is a nonzero $y$ such that $Ay=0$. Therefore, $y^*Ay=0$ and using ($*$) it follows that $x_1,\ldots,x_n$ are linearly dependent.
Comment: What exactly is $y^{*}$ ?
Comment: @user111854 conjugate transpose of $y$: $y^*=[\bar{\eta}_1,\ldots,\bar{\eta}_n]$. Note that $y$ is a column vector and $y^*$ is a row vector.
Comment: Thank you it is a beautiful proof.
Comment: @user111854 You are welcome!
Answer: Let
$$
B=\begin{bmatrix}
x_{1,1}&x_{2,1}&x_{3,1}&\cdots&x_{n,1}\\
x_{1,2}&x_{2,2}&x_{3,2}&\cdots&x_{n,2}\\
x_{1,3}&x_{2,3}&x_{3,3}&\cdots&x_{n,3}\\
\vdots&\vdots&\vdots&\ddots&\vdots\\
x_{1,n}&x_{2,n}&x_{3,n}&\cdots&x_{n,n}\\
\end{bmatrix}
$$
where the $k^{\text{th}}$ column is $x_k$. Then
$$
A=B^T\overline{B}
$$
We have
$$
A\overline{x}=0\implies B^T\overline{Bx}=0\implies x^TB^T\overline{Bx}=0\implies Bx=0
$$
and
$$
Bx=0\implies B^T\overline{Bx}=0\implies A\overline{x}=0
$$
Thus, $A\overline{x}=0\iff Bx=0$; that is, $A$ is singular if and only if the columns of $B$ are dependent.
Comment: @lhf I was assuming that $\langle x,y\rangle$ was the standard inner product on $\mathbb{C}$ since nothing extra was said in the question, but your comment does remind me that I was going to make sure I got the proper inner product. I have corrected that. To use a non-standard inner product, we would need to insert a symmetric, positive definite $M^T\overline{M}$ between $B^T$ and $\overline{B}$, but I believe the rest of the proof works similarly.
Comment: Since the inner product is not necessarily the standard one, $x_{ij}$ need to be coordinates with respect to an orthonormal basis for that inner product.
Comment: That'll work, and was my first thought, but using the correct coordinates works as well and is simpler.
Comment: All $M$ is, is a change of coordinates, so essentially the same thing.
Comment: Of course it is.


Conditional Multi-variate Normal Distribution

Question: I would like to find a computationally quick and sufficiently accurate way (i.e., either analytically or numerical approximation) to compute a conditional probability for a specific multi-variate normal distribution, which is defined as follows:
Let $X_1, \dots, X_n$ be n independent (univariate) normal random variables. And let $A_1, \dots, A_k$ be defined as sums of subsets of those random variables, i.e.,:
$\forall i: A_i = \sum_{j \in S} X_j$, where $S \subseteq \{1,\dots, n\}$.
I am looking for the following conditional probabilities of $A_i$:
Pr[ $A_i \leq x$ | $A_j \leq t$ $\forall j \in C$ and $j \neq i$ ], where $x$ is any number smaller than $t$, and $C \subseteq \left\{ 1,...,k \right\}$ is a subset of all possible variable indices on which the probability is conditioned. So the condition is on several $A_j$'s.
To the best to my knowledge, the conditional density of a multi-variate normal density is generally not a (uni-variate) normal distribution. I tried via Monte-Carlo simulation, but approximation is not sufficiently accurate when using 500,000 samples, 2000 bins (to classify the results) for a "problem" with 50 random variables, i.e. n=50.
Analytically that doesn't seem to be trivial. I have asked quite a few Statisticians around me and so far we haven't found a solution.
Answer: Let $X \sim \mathcal N(\mu \mathbf 1, \sigma^2 I)$ be your $X_i$ stacked up into a vector. Then $A_i = w_i X$, where $w_i = \left[ \mathbb 1(x_j \in S_i) \right]_j$ is the binary membership vector corresponding to the $i$th sum.
The joint distribution of $A_i$ and $A_j$ is then, using the distribution of an affine transformation of a multivariate normal:
\begin{align}
\begin{bmatrix}A_i \\ A_j\end{bmatrix}
&= \begin{bmatrix} w_i^T \\ w_j^T \end{bmatrix} X
\\&\sim \mathcal N\left( \begin{bmatrix} w_i^T \\ w_j^T \end{bmatrix} \mu \mathbf 1, \begin{bmatrix} w_i^T \\ w_j^T \end{bmatrix} \sigma^2 I \begin{bmatrix} w_i & w_j \end{bmatrix}\right)
\\&= \mathcal N\left( \mu \begin{bmatrix} w_i^T \mathbf 1 \\ w_j^T \mathbf 1 \end{bmatrix}, \sigma^2 \begin{bmatrix} \lVert w_i \rVert^2 & w_i^T w_j \\ w_j^T w_i & \lVert w_j \rVert^2 \end{bmatrix} \right)
\\&= \mathcal N\left( \mu \begin{bmatrix} n_i \\ n_j \end{bmatrix}, \sigma^2 \begin{bmatrix} n_i^2 & n_{ij} \\ n_{ij} & n_j^2 \end{bmatrix} \right)
\end{align}
where $n_i = \lvert S_i \rvert$ and $n_{ij} = \lvert S_i \cap S_j \rvert$.
Your desired entries are then computable from the CDF of a bivariate normal, available in various standard programming environments:
$$\Pr(A_i \le s \mid A_j \le t) = \frac{\Pr(A_i \le s, A_j \le t)}{\Pr(A_j \le t)}.$$

If you need to condition on multiple variables, you can easily extend this in exactly the same way. For example,
\begin{align}
\begin{bmatrix}A_i \\ A_j \\ A_k\end{bmatrix}
&= \mathcal N\left( \mu \begin{bmatrix} n_i \\ n_j \\ n_k \end{bmatrix}, \sigma^2 \begin{bmatrix} n_i^2 & n_{ij} & n_{ik} \\ n_{ij} & n_j^2 & n_{jk} \\ n_{ik} & n_{jk} & n_k^2 \end{bmatrix} \right)
\end{align}
and
$$\Pr(A_i \le s \mid A_j \le t, A_k \le r) = \frac{\Pr(A_i \le s, A_j \le t, A_k \le r)}{\Pr(A_j \le t, A_k \le r)}.$$
Comment: @San It's the same idea, just in higher dimensions, as shown in my edit.
Comment: @San It can be numerically approximated: for example [in Python](http://www.nhsilbert.net/source/2014/04/multivariate-normal-cdf-values-in-python/) or [Matlab](https://www.mathworks.com/help/stats/mvncdf.html).
Comment: The Python code ends up calling [this Fortran routine](http://www.math.wsu.edu/faculty/genz/software/fort77/mvndstpack.f), which  you could call from C++. I don't think an analytical solution in terms of "standard" functions exists, as noted e.g. [on the Wikipedia page](https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Cumulative_distribution_function).
Comment: Dear Dougal,

Thanks for your response. That works fine if I condition only on one variable.

Unfortunately, I need to condition on several of those composed variables, for example: $Pr[ A_i \leq s | A_{j1} \leq t \ \& \ A_{j2} \leq t ]$. Any ideas? PS.: I have modified the question to make this clearer. Sorry for the ambiguity!
Comment: I know, but I cannot compute $Pr(A_i \leq s, A_j \leq t, A_k \leq r)$ as used in your last post. That's not a bivariate anymore and hard to approximate. Or am I missing something out?
Comment: Hum, now I am back to numerical approximation. I was hoping to find any analytical solution. But, well, numerical approximation would be fine if I can do it quick, and from C++. Any chance that you know a package callable from C++? I have already looked for it.. a lot.. many thanks for your answers in either case. I appreciate.
Comment: Yes, maybe calling the Fortran routine of Alan Genz' code is the way to go. Thanks, Dougal, for your time.


stratification in cox model

Question: I am trying to understand difference in output between the following lines of code:
library(survival)

summary(coxph(Surv(futime, fustat) ~ age + strata(rx), data=ovarian))
summary(coxph(Surv(futime, fustat) ~ age, data=ovarian))

What does adding strata(rx) do?
Explanation ?strata says:
**Value**
a new factor, whose levels are all possible combinations
of the factors supplied as arguments.

What is the point to take all possible combinations of factor values, when they are mutually exclusive. What am I missing? Why example contains strata(rx) i.e. only one factor, when explanation is talking about interaction of factors?
Comment: The explanation is a general one: one factor is a special case of the interaction between factors.
Answer: In a Cox model, stratification allows for as many different hazard functions as there are strata. Beta coefficients (hazard ratios) optimized for all strata are then fitted.
In your example, the model coxph(Surv(futime, fustat) ~ age + strata(rx) will output a hazard ratio for age in the presence of two (or more) hazards intrinsic to the levels of rx. If rx violated the proportional hazards assumption, for example, stratifying may help meet the PH assumption and provide more valid estimates for age. The effect of rx is not explicitly provided as a hazard ratio. Likelihood estimates for the model can be used to assess whether stratification by rx improved the model fit.
coxph(Surv(futime, fustat) ~ age will output a hazard ratio for age only, assuming that the hazard for different levels of rx are the same. In this model, the effect of rx is not explicitly modeled.
The model coxph(Surv(futime, fustat) ~ age + rx may be useful to consider. This model would provide estimates of the HR for age and rx with both present in the model ("adjusted for one another"). This would be different from coxph(Surv(futime, fustat) ~ age + str(rx) in that the unstratified model provides estimation of effect for both age and rx using a single underlying hazard.
Comment: A reason to stratify is to (hopefully) correct violations of the PH assumption. In my practice, I also implement stratification when I don't need to explicitly measure the effect of a variable but I know its values have vastly different underlying risks (eg, magnitude or "shape"; another form of PH violation). I could not find a related post with more detailed information. If you need a detailed answer, you may want to post a new question.
Comment: In practice, how do we decide between these two approaches? stratify or adjust.
Comment: @ToddD, in the scenario where a variable isn't of interest as a covariate but might work as a stratifying variable, what criteria do you use to compare (a) `coxph(Surv(futime, fustat) ~ age)` with (b) `coxph(Surv(futime, fustat) ~ age + strata(rx))`? Do you fit both models and compare their AIC? Or does it make more sense to make that call based on context (e.g., use rx as a stratifying variable when we have biomedical reasons to expect that baseline hazard differs by rx)?
Answer: In addition to the good answer from Todd B, here's a process you can follow, and some more of the background math.
A reasonable process to follow

Fit a Cox model (fit.unstrat) with all of the covariates and no stratifying variables.

Use cox.zph(fit.unstrat) to check for violations of the proportional hazards assumption. If you see e.g. rx has p<0.05, then rx violates the proportional hazards assumption and should not be included as a covariate in the Cox model. A reasonable follow-up is to try changing it to a stratifying variable, e.g., fit.strat <- update(fit.unstrat, . ~ . - rx + strata(rx)).

Next, it's appropriate to evaluate whether multiple models improve over the stratified model. The stratified Cox model allows the two rx groups to have different baseline hazards / baseline survivals, but it still only calculates a single effect $\beta_\text{age}$ of age on survival time. You can think of this as being the mean effect of age averaged across both rx groups. This raises a question: do you get a statistically significantly better fit when you allow the two rx groups to have different covariate effects ($\beta_\text{age, rx=2}$ and $\beta_\text{age, rx=1}$)? To answer this question, you:

fit the stratified model,
fit two separate Cox models for datasets subsetted by rx, and
compare them in a likelihood ratio test, where the p-value is $P\left(\chi^2_{(k-1)(q)}\right)>\ell_\text{stratified model} - \sum \ell_\text{unstratified models}$, with
$k$ being the number of stratifying groups (just two here--rx=1 and rx=2), $q$ being the number of covariates (just 1 here--$z_\text{age}$), and $\ell$ is a model's log-likelihood.

Here's some sample code for the likelihood ratio tests. In this example (pretending rx violates the proportional hazards assumption), we wouldn't choose the multiple models, because while they improve the log likelihood, it's not a statistically significant improvement in the fit (p=0.1575):
library(survival)
fit.strat <- coxph(Surv(futime, fustat) ~ age + strata(rx), data=ovarian)
fit.grp1 <- coxph(Surv(futime, fustat) ~ age, subset=(rx==1), data=ovarian)
fit.grp2 <- coxph(Surv(futime, fustat) ~ age, subset=(rx==2), data=ovarian)

LL.strat <- fit.strat$loglik[2]
LL.unstrat <- fit.grp1$loglik[2] + fit.grp2$loglik[2]

X2 <- -2*(LL.strat - LL.unstrat)
n_groups <- length( unique(ovarian$rx) )
n_params <- length( fit.strat$coef )
p_val <- 1 - pchisq(X2, df=(n_groups-1)*n_params)

Background math
Standard Cox proportional hazards model:
$$h(t|Z) = h_0 \exp(\beta_\text{age}Z_\text{age} + \beta_\text{rx}Z_\text{rx})$$
fit.unstrat <- coxph(Surv(futime, fustat) ~ age + rx, data=ovarian)

The quantity $h_0$ is called the baseline hazard. It corresponds with the baseline survival, which is the survival of the reference group whose covariates are all equal to 0.

For a Cox model, the baseline hazard is estimated from the data non-parametrically (without assuming any distribution).

The equation above implies:

(hazard for rx==2 group)/(hazard for rx==1 group) is a constant. In other words, group 2's hazard is always higher/lower than group 1's hazard by a constant factor.

The two rx groups have the same baseline hazard and baseline survival.

Stratified Cox proportional hazards model (you're actually fitting two equations that share a single $\beta_\text{age}$):
$$h_\text{rx=2}(t|Z) = h_{0,\text{rx=2}} \exp(\beta_\text{age}Z_\text{age}) \\
h_\text{rx=1}(t|Z) = h_{0,\text{rx=1}} \exp(\beta_\text{age}Z_\text{age})$$
fit.strat <- coxph(Surv(futime, fustat) ~ age + strata(rx), data=ovarian)

These equations imply:

The two rx groups have different baseline hazards / baseline survivals.
The two rx groups share a single factor, $\exp(\beta_\text{age})$, by which the hazard is always higher/lower for a 1-unit increase in age. In other words, we're specifying that both rx groups have the same $\beta_\text{age}$.

Multiple models (this is just a term I'm making up):
$$h_\text{rx=2}(t|Z) = h_{0,\text{rx=2}} \exp(\beta_\text{age, rx=2}Z_\text{age}) \\
h_\text{rx=1}(t|Z) = h_{0,\text{rx=1}} \exp(\beta_\text{age, rx=1}Z_\text{age})$$
fit.grp1 <- coxph(Surv(futime, fustat) ~ age, subset=(rx==1), data=ovarian)
fit.grp2 <- coxph(Surv(futime, fustat) ~ age, subset=(rx==2), data=ovarian)

Short note about time-dependent covariates
In addition to fitting a stratified model, another typical way to remediate a violation of the proportional hazards assumption is to (1) convert the violating variable into a time-dependent variable, and then (2) fit a new Cox PH model and check if the time-dependent covariate satisfies the PH assumption. Time-dependent covariates are an entirely different topic, but they can get invoked for the same reason as a stratified Cox model. Picking between these two often depends on context. If you think the two rx groups truly have different baseline survivals, then a stratified model makes sense. If there's a time component to rx (e.g., the data also contain a variable indicating how much time passed before they started treatment, or we think rx might have a cumulative effect such that the treatment only provides a benefit after some amount of time has passed), then a time-dependent variable might make more sense.

stratification in cox model


What would be the best equivalent of Java annotations in C++?

Question: I'm a Java programmer and I'm learning c++ 11 which I like a lot. I really think it's great but I'm missing the notion of annotation in this language.
First, is there something close to Java style annotation in the standard ? I think there's not after my research.
What would be the closer, standard way (no compiler extensions), to "simulate" a Java style annotation in c++ ?
And please don't mention the c++ 11 override annotation. I mean user defined annotations.
Comment: What would you like to do with these user-defined annotations?
Comment: According to [this SO post](http://stackoverflow.com/a/4689981/1979005), noooopeee. Also, possible duplicate of http://stackoverflow.com/q/4689756/1979005
Comment: What I have in mind is porting one of those fancy ORM such as Hibernate to c++ :) I know there are already lots of existing solutions out there, but I really like the annotation style.
Comment: The idea in C++ **may** be that anything you would do with tooling processing annotations at runtime / as a build step you should be doing with templates instead.
Comment: Interesting comment. What would be the mind process to "translate" a java annotation to a c++ template ?
Answer: There is nothing like annotations in standard C++. For alternatives, it depends on why you want to use annotations.

one of the use of annotations in Java is to mark some methods or classes with meta-data so it can be retrieved through reflection at runtime. But reflection in standard C++ is very limited, so there is nothing like that. I haven't had a look at boost::reflect, maybe it can be useful to you.
another use of annotations is to have tools that intervenes at compile time to inject some code or to do checks. The tool to do this kind of stuff in C++ is the preprocessor.
Comment: I was more thinking of runtime stuff. But you're right there are 2 different retention types in Java annotations.


If $S$ and $T$ are nonempty subsets of a finite group $G$, then either $G=ST$ or $|G|\geq |S|+|T|$.

Question: Exercise 2.29 from Rotman's book An Introduction to the Theory of Groups.

(H.B.Mann) Let $G$ be a finite group, and let $S$ and $T$ be (not necessarily distinct) nonempty subsets. Prove that either $G=ST$ or $|G|\geq |S|+|T|.$

I would appreciate any help in order to solve this question.
P.S. I've deleted the previuos post with the same question, because it was confuse. I am sorry for the inconvenience this might cause you.
Comment: By deleting the previous post, you also deleted the comments, which should have helped you solve the problem!
Answer: Here's a proof sketch:
Assume that $|S| + |T| > |G|$. We want to show that $G = ST$.
Fix $x \in G$, and focus the attention on the sets $S$ and $U = \{ x t^{-1} : t \in T \}$. First, these sets have cardinalities $|S|$ and $|U| = |T|$ respectively. At the same time, they are both subsets of $G$, whose size is strictly smaller than the sum of their cardinalities. This implies that $S$ and $U$ have a nonempty intersection. Can you take it from here?


When is there a representer theorem?

Question: The case of regularization in a hilbert space is considered---an optimization problem with an error term and a Tikhonov-regularizer.
In the article "When is there a representer theorem" it is stated on page 2 (2508), and proofs are referred to, that the representer theorem holds when the regularizer is a nondecreasing function of the norm, i.e. square of the hilbert space norm.
Are there any other requirements on the problem, for example on the error term?
Answer: Scholkopf et al,"A Generalised Representer Theorem, suggests that the loss function can be arbitrary.  I suspect the kernel function must be positive-definite for the representer theorem to hold as well.  However, it is a very long time since I last looked at that sort of thing, so there may well have been something I have overlooked.

When is there a representer theorem?


WCF Updating Services

Question: We currently have a production application used by about 400 people. The application used WSHTTP binding with our WCF services. When we do releases on the server side code, we normally do it late at night when few people are on. It is my understanding that an update will cause IIS to recycle and it will be seamless to the end-user.

Is my understanding correct?
Is there any reason we can't update our WCF services in the middle of the day?
Are there any impacts you've seen on users currently connected?
Answer: The users are going to be impacted only if endponts being used are sessionful, i.e.
a) enabled WCF security at message level, (my 2 cents
b) reliable session
That's because, whenever any changes are made in folders being monitored by ASP.NET (there are good blogs on what directories are monitored), the application pool is unloaded and reloaded. Now, WCF stores the tokens (when using message security) in memory. If the application domain is unloaded, the token used to authenticate user is invalidated. So, if client tries to continue using it, service application doesn't find it and throws.
Solution to this could be using a stateful security context token (SCT) in a secure session, the session can withstand the service being recycled,
http://msdn.microsoft.com/en-us/library/ms731814.aspx
P.S.: It's always recommended to stop the IIS application pool at the time of deployment to avoid unexpected issues with ASP.NET Shadow copy.
HTH,
Amit
Answer: In answer to your questions

I do not agree that recycling the host process will be seamless to users. Any requests being processed will be unloaded and the consumers will receive availability-based exceptions
Yes, the same reason as 1.
Yes, their requests fail and they receive at best a soap fault which they have to then handle.

If you have a high availability requirement you ideally should route requests to a failover endpoint while you are patching the primary one. Or you could consider using queues rather than synchronous calls which would give you the ability to update during business hours.

WCF Updating Services


Prove that $\lim\limits_{x\to\infty} \frac{\Gamma(x+1,x(1+\epsilon))}{x\Gamma(x)}=0$.

Question: I conjecture that for any $\epsilon>0$, we have
$$
\lim_{x\to\infty} \frac{\Gamma(x+1,x(1+\epsilon))}{x\Gamma(x)}=0
$$
where $\Gamma(x,a) = \int_a^\infty t^{x-1}e^{-t} \mathrm{d}t$ denotes the incomplete Gamma function, and $\Gamma(x) = \Gamma(x,0)$ is the Gamma function.
I'm also happy with the weaker conjecture
$$
\lim_{x\to\infty} \frac{\int_{x(1+\epsilon)}^\infty t^{x-1}\log(1+t)e^{-t} \mathrm{d}t}{x\Gamma(x)}=0
$$
but I doubt that it is any easier. Any clues?
NB: the stronger conjecture is obtained from the weaker conjecture by upper-bounding $\log(1+t)$ by $t$
Answer: I have done an equivalent rewriting of this question in terms of random variables in another thread:
Tail of a sequence of RV
There the question was answered in more generality by user Did
Answer: Try to use the asymptotic expansion

$$\Gamma(s,z) = z^{s-1}e^{-z} \sum_{k=0}^{\infty} \frac{\Gamma(s)}{\Gamma(s-k)}z^{-k},\quad |z| \to \infty\quad \rm{and}\quad |\!\arg z| < \tfrac{3}{2} \pi. $$

combined with the Stirling approximation of the gamma function.


Create container.html.twig template for specific page

Question: I'm attempting to edit the search page.
I've edited container.html.twig to contain
{%
  set classes = [
    has_parent ? 'js-form-wrapper',
    has_parent ? 'form-wrapper',
  ]
%}

Search Results

{{ children }}

It works fine when I leave the file name as container.html.twig. But I'd like for it to only apply to the search page. I've tried naming it container--search-results.html.twig but that did not work.
How do I rename container.twig.html to only apply to my search page?
Answer: Am not sure if this contains the answer, but you should definitely read this:
Twig Debugging
Comment: It does not. Twig debugging is what I used to figure out that container.twig.html was being used at that point, but it did not show me how I could write a custom one specifically for that page.
Comment: ah okey! Wanted to make sure you know it! Further investigation is needed!


How to search for a string in a category name?

Question: I use the search result and I GET
$s = $_GET['s'];

Which gives: "mario"
Now there is a category named Mario Bianchi under a main category called Persone
When I GET $s I need to get all posts within that category, I tried the following but I get nothing
$terms = get_terms( 'category', array(
    'name__like' => $s,
    'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
    echo '
';
    foreach ( $terms as $term ) {
        echo '
name ) . '">' . esc_html( $term->name ) . '
';
    }
    echo '
';
}

Yet I need the actual post attached, not the category itself
Answer: get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
    'taxonomy'   => 'category',
    'name__like' => $s,
    'hide_empty' => true // Optional
) );

$term_ids = array();

if ( ! empty( $terms ) ) {
    foreach( $terms as $term ) {
        $term_ids[] = $term->term_id;
    }

    $args = array(
        'post_type' => 'post',
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'terms'    => $term_ids,
            ),
        ),
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        echo '
';
        while ( $query->have_posts() ) {
            $query->the_post();
            echo '
' . esc_html( get_the_title() ) . '
';
        }
        echo '
';
    }
    wp_reset_postdata();

}

Comment: ok let me try this, p.s.  `syntax error, unexpected '{' ` on `if ( ! empty( $terms ) {`
Comment: it is not looking in category, only finds a match of `$s` in a title, but let's say I search for `mario` and I have a category called `mario bianchi`, I need all posts within that category since we found a match of `$s` (mario) in a category since we have one `mario bianchi`
Comment: if I do `$term_ids = array(); var_dump($terms);` i `get array(0) {}` but we do have a category called `mario bianchi`
Comment: @rob.m It seems to be working for me on a test site: http://xanthous-lion.w5.poopy.life/?s=mario
Comment: in your link i see "NOTHING FOUND
Hello world!
Sorry, but nothing matched your search terms. Please try again with some different keywords."
Comment: `Hello world` has the "mario bianchi" category
Comment: what if "mario" is a sub category of "persone" tho?It doesn't appear to be including to search in childrens
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183674/discussion-between-rob-m-and-cameronjonesweb).
Comment: is there anyhow we could do the same but for tags?
Comment: go it, I had to use 'taxonomy' => 'post_tag'


PHP check csv column with strpos

Question: I need to check the csv column (string) if contain some words. Then add it to the array of csv. Here is my current code.
example csv
     A            B
 1   Order Number Order Items
 2   100001       foo - sbar - ybaz - z
 3   100002       foo - sbar - ybaz - z

This is my code.
$csv = array_map( "str_getcsv", file( /path/to/csv/, FILE_SKIP_EMPTY_LINES ) );
$keys = array_shift($csv);

$values = ["foo - s", "baz - z"];

foreach ($csv as $key => $row) {
    $csv[$key] = array_combine( $keys, $row )

    foreach ($values as $value) {

        if (strpos($row[0], $value) !==FALSE) {
            $csv[$key][$keys[1]] = $value;
        }

    }

}

This is what i want to achieve.
[0] =>
array(2) => {
    ["Order Number"] => "100001"
    ["Order Items"] =>
    array (2) {
        [0] => "foo - x",
        [1] => "baz - z"
    }
}
[1] =>
    array(2) => {
        ["Order Number"] => "100002"
        ["Order Items"] =>
        array (2) {
            [0] => "foo - x",
            [1] => "baz - z"
        }
    }

This is what my code output
[0] =>
array(2) => {
    ["Order Number"] => "100001"
    ["Order Items"] => "baz - z"
}
[1] =>
    array(2) => {
        ["Order Number"] => "100002"
        ["Order Items"] => "baz - z"
    }

if i add and make this $csv[$i][$keys[1]][] = $value;
This error show: Fatal error: [] operator not supported for strings
Comment: are you sure that $row is a string with column name??
Comment: Yes sorry. I added information on my code. Please take a look
Comment: `$csv[$i] = array_combine...` What is $i ?
Comment: @splash58 sorry wrong input gonna edit it to $key
Comment: And there - `$csv[$i][$keys[1]] ` ?
Comment: @splash58 oops. Ok thanks. Changing it to $key. But it is just a typo. Code is working tho.
Comment: Look there - where i am wrong - https://eval.in/907616
Comment: Sorry this is the $csv variable contain.

`$csv = array_map( "str_getcsv", file( /path/to/example/csv/, FILE_SKIP_EMPTY_LINES ) );`
Comment: `var_export($csv);`Is it not the same as in my example?
Comment: @splash58 nope. it contains the array of csv.
Comment: https://eval.in/907624 Let's reproduce your result
Comment: @splash58 https://eval.in/907623 but i dont know how to add it on $csv variable.
Comment: Look there - https://eval.in/907659
Comment: @splash58 Hi, Thanks for your effort. Could you post it as an answer so i can upvote it?
Comment: I've written it.
Answer: You need to add one more loop to scan row values
$csv = array_map( "str_getcsv", file('php://stdin', FILE_SKIP_EMPTY_LINES ) );
$keys = array_shift($csv);

$values = ["foo - s", "baz - z"];

foreach ($csv as $key => $row) {
    $csv[$key] = array_combine( $keys, $row );
    foreach($row as $k=>$i) {
       foreach ($values as $value) {
          if (strpos($i, $value) !==FALSE) {
              if (! is_array($csv[$key][$keys[1]])) $csv[$key][$keys[1]] = [];
              $csv[$key][$keys[1]][] = $value;
          }
        }
    }
}

demo

PHP check csv column with strpos


How can I calculate the standard deviation when knowing the P10, P25, P75, P90, mean and median?

Question: I'm trying to find the standard deviation from some simulated task duration results. The results provided the following:
Mean - 430
Median - 374
P10 - 371.8
P25 - 372.4
P75 - 440.5
P90 - 588.6
From my understanding, since the mean and median are different I cannot assume a normal distribution (so I can't divide the IQR by 1.349 to calculate the standard deviation). How else can I calculate it? Can I get away with dividing IQR by 1.349?
Thanks!
Comment: Why not just get the program that calculated those numbers to give you the SD as well?
Comment: The above-linked question seems to contain a lot of relevant info; if the mean is far from the median that would suggest that the standard deviation isn't a good summary measure for your data either..
Comment: How many points do you have? If you only have a few, you can't tell if the underlying distribution is normal or not.
Comment: I am downvoting because the question provides no information about how the standard deviation will be used, and because there clearly is not enough information for a good estimate. As an example of the difficulties, truncating the trails symmetrically about the mean might preserve all the given data but reduce the standard deviation significantly.
Comment: Different distributions might have the same mean, median, quartiles and extreme deciles and yet have very different standard deviations, so you can't just *calculate* a standard deviation. If you make some distributional assumption, then you may be able to calculate an estimate of a population standard deviation. Or you could give a lower bound on a sample standard deviation.
Answer: The example below shows that these values do not have to uniquely define the standard deviation.
set.seed(2024)
N <- 1000
x <- y <- sort(rexp(N, 1))
s <- 1000
y[1] <- y[1] - s
y[N] <- y[N] + s
mean(x) == mean(y)
median(x) == median(y)
quantile(x, 0.1) == quantile(y, 0.1)
quantile(x, 0.25) == quantile(y, 0.25)
quantile(x, 0.75) == quantile(y, 0.75)
quantile(x, 0.9) == quantile(y, 0.9)
sd(x) == sd(y)

Here, the means, medians, and specified quantiles are equal. However, the standard deviations are quite different.
I constructed this example by giving two data sets that differ in just their smallest and largest values, which are shifted by the same amount in different directions, preserving the specified quantities but resulting in wildly different standard deviations when that shift value s is as large as I set it.


Notification email on Content creation - admin approval?

Question: I made a custom content type to replace comments, as the current comment options are not satisfactory for me. Anonymous users can create this content type (comment).
I need to send an email to the administrator users each time a new comment is written, to allow them to publish the new comments.
I searched for notification email, but it is not what I expected; it only allows to send emails on regular basis. Maybe I could not make it work.
How can I send automatic emails to administrators to notify them a new comment has been created, and it is waiting for approval?
Answer: You should try the Rules modules, it can does exactly what you want.

The rules module allows site administrators to define conditionally executed actions based on occurring events (known as reactive or ECA rules). It's a replacement with more features for the trigger module in core and the successor of the Drupal 5 workflow-ng module.
Answer: Another option would be the Subscriptions module.

This module enables users to subscribe to be notified of changes to nodes or taxonomies, such as new comments in specific forums, or additions to some category of blog. Once enabled, all nodes will have an additional link that allows the user to change their subscriptions. Users have tab on their user screen to manage their own subscriptions. Users can also set an auto subscribe function which notifies the user if anyone comments on posts they have made. Admins can set this on by default.


Calculation of Weights in Locally Weighed Regression

Question: In Andrew Ng's lectures he introduces Locally Weighted Linear Regression (page 15) for which he describes the weight as:
$$
\begin{align}
w^{(i)} = exp\Big(-\frac{(x^{(i)} - x)^2}{2 \tau^2}\Big)
\end{align}
$$
However in the problem assignment for Locally Weighted Logistic Regression, the weight is described as:
$$
\begin{align}
w^{(i)} = exp\Big(-\frac{||x^{(i)} - x||^2}{2 \tau^2}\Big)
\end{align}
$$
where $x^{(i)}$ had dimensions $1 \times n$ and $x$ has dimensions $m \times n$.
Looking at the solution set, it seems that they first get the difference between $x$ and $x^{(i)}$ (giving an $m \times n$ matrix) followed by element-by-element squaring (using the Matlab/Octave <code>.^2</code> operator), and then sum along the rows to give a $m \times 1$ vector. In the linear algebra review notes (pages 7-8), multiple types of norms are described, but the sequence of operations performed doesn't seem to be consistent with any of the descriptions of norms. What is going on here?
Answer: First off, because I am not sure that this wasn't a part of your question, the norm they are using is defined as $|| \mathbf{x} || = \sqrt{\sum_{i=1}^n x_i^2}$, so if we take the norm of $\mathbf{x} - \mu$ (where $\mu$ is our mean or center) and square it, we get $|| \mathbf{x} - \mu ||^2 = \sum_{i=1}^n (x_i - \mu)^2$. The two expressions are equivalent.
It sounds like they are assigning a different center, $\mu_{i,j}$ for each $x_i$ and each hidden node $h_j$ for $j = 1, 2, \ldots, m$. In that case, for each input value $x_i$ and hidden node $h_j$, you would have:
$$
w_{i, j} = \exp \bigg(\frac{(x_i - \mu_{i,j})^2}{2\tau_j^2} \bigg)
$$
The input to node $h_j$ is then the sum of these values over all inputs $x_i$ for that node: $\sum_{i = 1}^n w_{i, j}$. I think what they are doing in the code is just the vectorized form of this.
Comment: Ah feel so stupid! Didn't write it out and so didn't see that the outer square was cancelling out the square root. Thank you!


Understanding $\mathrm{Proj}S$ for a graded ring $S$

Question: For a graded ring $S$, $\mathrm{Proj}S$ is constructed in Hartshorne's Algebraic Geometry. Then Proposition 2.5 on the same page says that
$$(D_+(f), \mathcal O|_{D_+(f)}) \cong \mathrm{Spec}S_{(f)}$$
for any homogeneous $f \in S_+$. The bijection between the underlying topological spaces is given by $$\varphi: \mathfrak a \mapsto (\mathfrak a S_f) \cap S_{(f)}.$$
I think I need some concrete examples to understand this, so I take $S = \mathbb Z[X_0,X_1]$ where the degree of $X_0$ and $X_1$ is $1$. Let $f=X_0$, $\mathfrak a$ be the ideal in $S$ generated by $X_1$, then it is homogeneous, prime and does not contain $f$. Through $\varphi$, it is mapped to $(\mathfrak a S_f) \cap S_{(f)}$. But what is $\mathfrak a S_f$? I think since $\frac{1}{X_1} \in S_f$ and $X_1 \in \mathfrak a$, $\mathfrak a S_f$ is $S_f$ itself. But $S_{(f)}$ is a subring of $S_f$, $S_f \cap S_{(f)} = S_{(f)}$, which is not a prime ideal of $S_{(f)}$. So where is wrong?
Thank you very much.
Comment: Well, since $\mathfrak{a}$ doesn't contain $f$, you know that $\mathfrak{a}S_f$ is prime--so not everything. In particular, why is $\frac{1}{X_1}\in S_f$? It's not.
Comment: @AlexYoucis Thank you very much for the comment. I think $S_f$ consists of $g(X_0,X_1)/h(X_0,X_1)$ for $g(X_0,X_1),h(X_0,X_1) \in \mathbb Z[X_0,X_1]$, such that $h(0,X_1) \neq 0$. If this is wrong, they would you please tell me what $S_f$ is? Thanks again.
Comment: $S_f$ is just the localization of $S$ at $\{1,f,\ldots,\}$. In other words, the only things in the denominator are powers of $f$.
Comment: I am confused. Would that be denoted $(f)^{-1}S$? For an ideal $P$, $S_P$ is making $S-P$ invertible...
Comment: We're not localizing at a prime, we're localizing at $f$. You may be more familiar with the notation $T^{-1}S$ where $T=\{1,f,\ldots\}$ or something, but here $S_f$ means localizating at $\{1,f,\ldots\}$. If you've never seen this notation before, I can understand why you may think we're localizing at a prime though.
Comment: @AlexYoucis: So for $S_P$, if $P$ is an element, then it means $\{1, P, \cdots\}^{-1}S$, but if $P$ is a prime ideal, then it means making $S-P$ invertible? Is this true? Thanks a lot.
Comment: That's correct :) Annoying notation, I know :)
Answer: The problem with what you wrote is the fact that $X_1$ is not invertible in $S_f$.
In particular, $\mathfrak{a}S_f=X_1 S_f\ne S_f$. Then,
$$\mathfrak{a}S_f\cap S_{(f)}=\frac{X_1}{X_0}S_{(f)}$$
This ideal IS prime. Namely, if you define the isomorphism $S_{(f)}\to \mathbb{Z}[T]$ in the natural way, $\displaystyle \frac{X_1}{X_0}\mapsto T$.
Comment: Now I see where I was wrong. Thank you very much.


Web.Config Transform Individual AppSettings to ConfigSource

Question: I'd like to transform the appsettings section of my local web.config file from a bunch of individual settings for my local dev work, to a configSource file path attribute for publishing to client servers.
Basically from this:
<appSettings>
    <add key="setting1" value="devVal1"></add>
    <add key="setting2" value="devVal2"></add>
    <add key="setting3" value="devVal3"></add>
</appSettings>

to this:
<appSettings configSource="clientSettings.config" />

This partial solution adds the configSource attribute.
<appSettings xdt:Transform="SetAttributes(configSource)" configSource="clientSettings.config"/>

But I'm not sure how to remove all the individual settings as well (I think the configSource will override them anyway, but I'd prefer to not have them there at all, to avoid having irrelevant dev settings in each of my client's web.config files)
Answer: A bit more perseverance lead me to the answer. Apply the "RemoveAll" transform to an add element within the appSetting.
<appSettings xdt:Transform="SetAttributes(configSource)" configSource="clientSettings.config">
    <add xdt:Transform="RemoveAll"/>
</appSetting>

Hope this is of use to someone in the future (if only as an example of why you should do 30 minutes of extra work before reaching for the 'Ask Question' button)...


How to create view in mongodb using spring data mongo db

Question: How to create view in mongodb using spring data mongodb?
Thanks
Answer: You can inject bean of type org.springframework.data.mongodb.core.MongoTemplate where you need it and use one of its methods executeCommand, for example executeCommand(String jsonCommand) in folowing manner:
<code>mongoTemplate.executeCommand("{ create: <view>, viewOn: <source>, pipeline: <pipeline> }");
</code>


passing class type as parameter and creating instance of it

Question: I want to pass a class type as a parameter to a method in java.all of which have the same constructor parameter.and create an instance of that Class in that method.is this possible?
Update after some years: do checkout Sapnesh Naik's answer to this, it seems to be the most up-to-date. I have not accepted is as an answer as I do not have a running java stack.
P.S: Give me a shout if you can verify.
Comment: Are you sure you actually "want" to do it? I wouldn't "want" to do it - ever.
Comment: What is your use case which is compelling this ?
Comment: There are valid use cases like instantiating classes from a factory / constructor based injection. Unless you're writing such a framework, you might as well use a DI to instantiate the class with constructor injection.
Answer: Using reflection to create the instance:
Object obj = clazz.newInstance();

This will use the default no-arg constructor to create the instance.
Constructor<?> constructor = clazz.getConstructor(String.class);
Object object = constructor.newInstance(new Object[] { strArgument });

To create the instance if you have some other constructor which takes arguments.
Answer: newInstance() is Deprecated.

This method propagates any exception thrown by the nullary
  constructor, including a checked exception. Use of this method
  effectively bypasses the compile-time exception checking that would
  otherwise be performed by the compiler. The Constructor.newInstance
  method avoids this problem by wrapping any exception thrown by the
  constructor in a (checked)
  java.lang.reflect.InvocationTargetException.

The deprecated call:
clazz.newInstance()

Can be replaced by:
clazz.getDeclaredConstructor().newInstance()

Example:
void MyMethod(Class type) throws InstantiationException, IllegalAccessException {
    type.getDeclaredConstructor().newInstance();
}

Comment: Yes this is the actual best answer.
Answer: You need to use reflection to do that, but yeah it is certainly possible.
void MyMethod(Class className)
{
    try {
        Object obj = className.newInstance();
        //obj is a newly created object of the passed in type
    }
    catch (Exception ex) { }
}

Answer: You're looking for Reflection.
Your method would look something like this:
public void m1(Class c) {

      try {
        Object obj = c.newInstance();
        //do something with your new instance
    } catch (InstantiationException ex) {

    } catch (IllegalAccessException ex) {

    }
}

The Oracle doc for the Reflection API is here
Comment: newInstance() has deprecated since Java 9
Answer: Yes you can
void x(Class cls) throws InstantiationException, IllegalAccessException {
    cls.newInstance();
}

Note that Class.newInstance may fail with
IllegalAccessException - if the class or its nullary constructor is not accessible.
InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
see API for more


Standard error from OR and HR

Question: I'm planning to combine several observational studies, some of which have produced odds ratios and some hazard ratios. How do I get the standard error from the odds ratio $1.57$ ($95\%$ CI: $1.08-2.27$) and the standard error from the hazard ratio $1.56$ ($95\%$ CI: $0.96-2.52$)?
Answer: You can back-calculate the standard errors from the CIs. Odds and hazard ratios are typically analyzed on the log scale. So, you will find that on the log scale, you get a symmetric confidence interval around the log of the estimate. For example, for $OR = 1.57$, you get $log(OR) = 0.451$ and the lower and upper CI bounds on the log scale are $0.077$ and $0.820$. Now these CI bounds are calculate with $log(OR) \pm 1.96 \times SE$, which implies that $SE = (UB - LB) / (2 \times 1.96)$. So, we get: $SE = (0.820 - 0.077) / (2 \times 1.96) = 0.189$. The same things works for the hazard ratio, where you will find that $SE = 0.246$. Note that these standard errors are for the log odds ratio and the log hazad ratio. But this is the scale we work on anyway when combining results from several studies with these outcome measures.

Standard error from OR and HR


Vim, installing pathogen Windows

Question: I tried to install vim plugin on my Windows OS but it don't work.
fist I'm going to show you my vim directoris architecture then to show you my _vimrc.vim file:
C:\Program Files (x86)\Vim >

17/07/2017  16:15    <DIR>          vim80
17/07/2017  17:45    <DIR>          vimfiles
17/07/2017  17:50               200 _vimrc

inside vim80 no changes added.
C:\Program Files (x86)\Vim\vimfiles

17/07/2017  17:47    <DIR>          autoload
17/07/2017  16:52    <DIR>          bundle
17/07/2017  17:05    <DIR>          config
17/07/2017  16:42    <DIR>          plugin

And this is how look like my ./vimfiles directorie
Inside I created 4 directorie first one: ./autoload for the pathogen symlink,
C:\Program Files (x86)\Vim\vimfiles\autoload

17/07/2017  17:12    <SYMLINK>      pathogen.vim [C:\Program Files (x86)\Vim\vimfiles\plugin\pathogen\autoload\pathogen.vim]

As you see pathogen plugin is inside ./plugin/pathogen/autoload/pathogen.vim
Then I creat a ./bundle directories for all the other plugins like NERDTree :
C:\Program Files (x86)\Vim\vimfiles\bundle

17/07/2017  16:52    <DIR>          nerdtree

Then this is my vimrc.vim file:
set nocompatible

" Initialisation de pathogen:
execute pathogen#infect()
execute pathogen#helptags()

runtime! $VIM/vimfiles/config/**/*.vim

I don't know why but when I try to use :NERDTree this message appear:
E492: Unknow command line :NERDTree

Please if someone can help me?
Thank you
Comment: `C:\Program Files (x86)\Vim` belongs to your system, not to you. You are supposed to customize Vim in `%userprofile%\vimfiles`.
Comment: Thanks for reading me,  however I'm not sure this is the cause of my problem.
Comment: It is the root cause of your problem. Return that directory to its pristine state and do your config where you are supposed to do it.
Comment: Hey, as you say I change the location of the vimfiles folder to C:/Users/Username/. In an other way the reason why my other architectur didn't work was I havn't remove the _vimrc.vim file from my C:/Users/Username/ folder. But I have an other mistake see under.
Answer: Move pathogen
from C:\Program Files (x86)\Vim\vimfiles\plugin\pathogen\autoload\pathogen.vim
to C:\Program Files (x86)\Vim\vimfiles\autoload\pathogen.vim
(I removed plugin/pathogen)
Because you installed pathogen as a 'normal' pluggin so that pathogen can find it but patogen must be directly copied as a vim extension: it is a package manager itself so must be loaded the old way.
Answer: I'm just resolving my mistake,
so this is my vim :
set nocompatible
set nocp

filetype off

" Initialisation de pathogen:
call pathogen#infect()
call pathogen#helptags()

filetype plugin indent on

source $HOME\vimfiles\config\settings.vim

Then dont use symlink they don't work, use only file
    18/07/2017  12:15    <SYMLINK>      pathogen [C:\Users\Usename\vimfiles\plugin\pathogen\autoload\pathogen.vim]
    18/07/2017  11:13                   pathogen.vim

You need to update pathogen manualy.
Answer: So I move all the vimfiles folder and the _vimrc file in my C:/Users/Username directorie and nothing in my architectur change (see comments of the previous post). I also change the _vimrc contents with:
set nocompatible
set nocp

" Initialisation de pathogen:
call pathogen#infect()
call pathogen#helptags()

source $HOME\vimfiles\config\settings.vim

But I have an other mistake. when I'm launching Gvim this message appear:
E117: Unknown function: pathogen#infect

I dont know why, this is the result of :scriptname :
  1: ~\_vimrc
  2: ~\vimfiles\config\settings.vim
  3: C:\Program Files (x86)\Vim\vim80\syntax\syntax.vim
  4: C:\Program Files (x86)\Vim\vim80\syntax\synload.vim
  5: C:\Program Files (x86)\Vim\vim80\syntax\syncolor.vim
  6: C:\Program Files (x86)\Vim\vim80\filetype.vim
  7: C:\Program Files (x86)\Vim\vim80\menu.vim
  8: C:\Program Files (x86)\Vim\vim80\lang\menu_fr.latin1.vim
  9: C:\Program Files (x86)\Vim\vim80\lang\menu_fr_fr.latin1.vim
 10: C:\Program Files (x86)\Vim\vim80\autoload\paste.vim
 11: C:\Program Files (x86)\Vim\vim80\ftplugin.vim
 12: C:\Program Files (x86)\Vim\vim80\indent.vim
 13: ~\vimfiles\plugin\pathogen\autoload\pathogen.vim
 14: C:\Program Files (x86)\Vim\vim80\plugin\getscriptPlugin.vim
 15: C:\Program Files (x86)\Vim\vim80\plugin\gzip.vim
 16: C:\Program Files (x86)\Vim\vim80\plugin\logiPat.vim
 17: C:\Program Files (x86)\Vim\vim80\plugin\manpager.vim
 18: C:\Program Files (x86)\Vim\vim80\plugin\matchparen.vim
 19: C:\Program Files (x86)\Vim\vim80\plugin\netrwPlugin.vim
 20: C:\Program Files (x86)\Vim\vim80\plugin\rrhelper.vim
 21: C:\Program Files (x86)\Vim\vim80\plugin\spellfile.vim
 22: C:\Program Files (x86)\Vim\vim80\plugin\tarPlugin.vim
 23: C:\Program Files (x86)\Vim\vim80\plugin\tohtml.vim
 24: C:\Program Files (x86)\Vim\vim80\plugin\vimballPlugin.vim
 25: C:\Program Files (x86)\Vim\vim80\plugin\zipPlugin.vim
 26: C:\Program Files (x86)\Vim\vim80\scripts.vim
 27: C:\Program Files (x86)\Vim\vim80\syntax\vim.vim
 28: C:\Program Files (x86)\Vim\vim80\ftplugin\vim.vim
 29: C:\Program Files (x86)\Vim\vim80\indent\vim.vim
 30: C:\Program Files (x86)\Vim\vim80\autoload\vimball.vim

I try a lot of solution from forum but no one work.

Vim, installing pathogen Windows


Finding the right-inverse of a function

Question: I've been given the following function:
$$f:P(X) \to P(X)$$ such that
$$ f(A) = X\backslash A$$
I figured out it's surjective and therefore has a right inverse, it's also injective, so it has an inverse.
I am trying to figure out what the right inverse is, I thought about $g(A) = X\backslash A$ but it just gives me an empty set.
Comment: Why is f not injective?
Comment: It doesn't give you just the empty set.
Answer: Consider $f^2$. For $A ⊆ X$,
$$f^2(A) = f(X \setminus A) = X \setminus (X \setminus A) = …$$
What happens if you take everything away from $X$ except for everything in $A$? You're left with …?

Finding the right-inverse of a function


What is $\int_0^{2 \pi} \sin^{2009}( x) ~~dx$

Question: What is
$$\displaystyle \int_0^{2 \pi} \sin^{2009}(x) ~~dx$$
If it is $0$, how so? Then shouldn't $\sin x$ be also $0$ and not -$\cos x$ since the top and bottom cancel out?
Comment: The integrand is $2\pi$-periodic so you can integrate over _any_ interval of length $2\pi$ and get the same answer.  Integrate over $[-\pi, \pi]$.  Your integrand is an odd function so you get $0$.
Answer: Because $\sin (\pi + x) = -\sin (\pi - x)$, we can make a substitution $u = x - \pi$.  Then we have the integral of an odd function (composition of two odd functions $\sin x$ and $x^{2009}$) between $-\pi$ and $\pi$ and so the area is $0$.
Comment: There is an antisymmetry of $\sin x$ about $x=\pi$ and so $u = x - \pi$ allows us to move the antisymmetry to $u=0$
Comment: What? I don't understand. Why did you add pi to the x inside of sin? You can't plug in 2pi and 0 until you integrate sin^2009 (x) dx....
Comment: If you know integration by substitution, try applying the substitution rule $u = x - \pi$ for your integral and see what happens.
Comment: I am wondering how you got x-pi in there in the first place
Answer: The integral evaluates to $0$, which can be intuited from the following argument: $\sin(x)$ is oscillatory and $2\pi$-periodic, so that there is equal area above and below the $x$-axis over the interval $[0,2\pi]$; this applies for all odd powers of $\sin(x)$. Moreover, this applies for all odd powers of $\cos(x)$. As long as you integrate over the period of these oscillating functions (their odd powers that is), the areas above and below the $x$-axis will cancel, yielding $0$.
Comment: I wouldn't strictly say *because* it's $2 \pi$ periodic.  More that it is odd about $x = \pi$.
Comment: @muzzlator : Point taken. I've modified my answer to reflect the informal nature of my argument.
Comment: @muzzlator : I'd say though that this is a nice intuitive look at what's going, useful for the OP.
Comment: I think the intuitive feel which you have and you wanted to share with the OP wasn't properly articulated.  You said that there is equal area above and below the axis for $\sin x$ **because** $\sin x$ is $2 \pi$-periodic.  But there are many $2 \pi$-periodic functions which don't have equal area below and above the axis.  So while what you said is true, the justification which you wrote for why it was true wasn't, I think, what you yourself pictured.
Answer: Use the reduction formula to study the recurrence relation involved $$\int \sin^{2n+1}x =\cfrac{\cos x \sin^{2n} x}{2n+1}+\cfrac{2n}{2n+1}\cdot\int \sin^{2n-1}x \ dx +c $$
and $\displaystyle \int_0^{2 \pi} \sin(x) dx =\left[-\cos x \right]^{2\pi}_0= 0$  and not just $- \cos x$