How to convert a maze to a Graph?

Question: I am trying to convert a maze data structure into a graph. The maze is like a grid and some walls between cells.
maze[8][8][4] is how the maze is represented.
If maze[i][j][0] = 1 it means you can't go up from (i,j)
if maze[i][j][1] = 1 it means you can't go down from (i,j)
// and so on

I want to convert this maze to a graph how can I do it?
Answer: You can do it in two ways:
1.Create an adjacency matrix from your initial matrix. The adjacency matrix is of the form:

h[i][j] = 0, if there is no direct link from i to j
(i and j are not neighbors in the maze)
h[i][j] = 1, if there is a direct link from i to j
(i and j are neighbors in the maze)

2.Create the neighbor list for each node: j is in the neighbor list of i if there is a direct link between i and j.
Comment: Keep in mind that if h[i][j] == h[j][i], you only need to store/calculate half of the matrix.
Comment: See [this](http://stackoverflow.com/questions/2218322/what-is-better-adjacency-lists-or-adjacency-matrix-for-graph-problems-in-c/5419933#5419933) for some info on what to choose when considering memory efficiency
Answer: Think of the input data as a Adjacency matrix. Think of the maze as a path, where each connecting segment creates vertices. And each corner is a node (including start and finish) If the connection exist, there is a value in the matrix. If it doesn't you can use INF or -1 to tell that there is no route. Anyway, you can solve this using Dijkstra's shortest math algorithm. There is a lot of info on the net about it.
http://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/
Answer: for every neighbor cells, if there is no wall between them, make them connected in the graph.
Answer: game = [[1, 1, 1, 1, 1, 1, 1],
        [1, 'A', 1, 1, 0, 0, 1],
        [1, 0, 0, 0, 0, 1, 1],
        [1, 0, 0, 1, 1, 1, 1],
        [1, 1, 0, 0, 0, 'B', 1],
        [1, 1, 1, 1, 1, 1, 1]]
rows = len(game)
cols = len(game[0])
graph = {}
for i in range(1, rows - 1):
    for j in range(1, rows - 1):
        if game[i][j] != 1:
            adj = []
            for ele in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
                if game[ele[0]][ele[1]] == 0 or game[ele[0]][ele[1]] == 'B':
                    adj.append((ele[0], ele[1]))
            graph[(i, j)] = adj
print(graph)

    {(1, 1): [(2, 1)],
    (1, 4): [(2, 4), (1, 5)],
    (1, 5): [(1, 4)],
    (2, 1): [(3, 1), (2, 2)],
    (2, 2): [(3, 2), (2, 1), (2, 3)],
    (2, 3): [(2, 2), (2, 4)],
    (2, 4): [(1, 4), (2, 3)],
    (3, 1): [(2, 1), (3, 2)],
    (3, 2): [(2, 2), (4, 2), (3, 1)],
    (4, 2): [(3, 2), (4, 3)],
    (4, 3): [(4, 2), (4, 4)],
    (4, 4): [(4, 3), (4, 5)],
    (4, 5): [(4, 4)]}

I have added padding of size 1 to make the code more simle , actual size of maze will be (row-1,cols-1),

How to convert a maze to a Graph?


Survfit function in R to score a new dataset

Question: I have built a cox proportional hazards model in the R survival package.  I want to score new data set using this model.  I thought the survfit function would doing this using survfit(original model, newdata), but it only seems to predict only the outcomes for the original data, not the original model using the new data.
Any insight is appreciated!
Comment: If you're after predictions of survival probability, the function `survest` [`survival`-package] (or predictSurvProb [`pec`-package]) is very useful for this.
Answer: Use the predict function.  According to the survival package documentation there is a predict.coxph function, so when that package is loaded and you pass predict a coxph object it will use it for prediction with your new data.  See ?predict.coxph for the additional arguments.


Diluting a $0.5M$ solution to $0.1M$

Question:
Using the $0.5 M$ solution how will you prepare a solution of $0.1 M$? Glassware provided is $3$ volumetric flasks of size $500, 250$ and $100 ml$. All bulb pipettes are provided and the sizes are $10, 20, 25$ and $50ml$.

I'm thinking of the formula $C_1V_1 = C_2V_2$ and I have a little confuse of how to select my $V_1$(volume 1) and $V_2$(volume 2).
Answer: You need to mix one unit of concentrated (.5M) solution with four units of solvent.
You want to start with $c_1V_1 = c_2V_2$.
Let's say, you want to make 500 ml of 0.1M solution, than your $V_2$ is 500 ml and $V_1 = V_2\frac{c_2}{c_1}$: $V_1$ is now 100 ml. You have to pour 100 ml of concentrated solution into 500 ml flask and fill it to 500 with solvent.

Diluting a $0.5M$ solution to $0.1M$


How to add Javascript Library for use in CouchDB views

Question: Disclaimer: I am reviewing CouchDB for a new project, so I am not too familiar with its inner workings yet.
How can I introduce a Javascript Library into CouchDB so that I can use it in my MapReduce views?
Comment: possible duplicate of [How do I DRY up my CouchDB views?](http://stackoverflow.com/questions/1197449/how-do-i-dry-up-my-couchdb-views)
Answer: take a look at the answers (all of them) of this question they describe a range of possible solutions:
How do I DRY up my CouchDB views?


How to get MAC address using sys/unix in Go

Question: I am writing a sample program to get a MAC address using sys/unix package on Ubuntu22.04 docker container.
What I am doing is to call unix.IoctlIfreq(soc, unix.SIOCGIFHWADDR, ifr) and get the bytes in ifr. But Ifreq struct has only Uint32() and Uint16() as exported methods and MAC Address has 6 bytes length.
What is a good way to get a MAC address?
(I can find many examples of getting a MAC address with syscall package but cannot find the way with sys/unix.)
Comment: What's wrong with using `net.Interfaces()` from the standard library?
Comment: Thanks for the answer! There is nothing wrong with net library :) I just wanted to use sys library for learning purposes.
Answer: I will suggest to use the more generic net package
Use Interfaces to get list of network interfaces.

func Interfaces() ([]Interface, error)

type Interface struct {
    Index        int          // positive integer that starts at one, zero is never used
    MTU          int          // maximum transmission unit
    Name         string       // e.g., "en0", "lo0", "eth0.100"
    HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
    Flags        Flags        // e.g., FlagUp, FlagLoopback, FlagMulticast
}

Then use HardwareAddr to get physical hardware address.

func (a HardwareAddr) String() string


Incorrect structured XML document after deleting element

Question: When I try to load a XML file after deleting an Element from the file, it shows the following error: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 9, position 10. What is wrong with my code?
This is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<data>
   <booze>booze1</booze>
   <booze>booze2</booze>
   <booze>booze3</booze>
   <booze>booze4</booze>
</data>

And my code:
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("favorites.xml", FileMode.Open, file))
   {
       XDocument xDoc = XDocument.Load(stream, LoadOptions.None);
       // delete node
       xDoc.Descendants("data").Elements("booze").Where(x => x.Value == favorite).DescendantsAndSelf().Remove();
       xDoc.Save(stream);
    }
}

Comment: What exactly is at Line 9, position 10 ? There's nothing else in your xml file is there? (Because there's only 7 lines in what you are showing us) You don't have some comments before the <?xml do you?
Comment: I think the "DescendantsAndSelf()" extension causes the  tag to be removed, hence the resultant XML has no root.
Comment: @PaulZahra The weird thing is that mentions that line and position no matter how many elements I have in my file. Even if there is only 1 element left after deleting a 2nd..
Comment: @bit It doesn't remove the `` tag because if I add a breakpoint to the line with `xDoc.save(stream);` and inspect the `xDoc`, it shows a correct document in my eyes..
Comment: possible duplicate of [Silverlight XML Save does not overwrite the entire file](http://stackoverflow.com/questions/11032253/silverlight-xml-save-does-not-overwrite-the-entire-file)
Comment: @PaulZahra Possibly not an exact duplicate since he does not want to clear the file on open, he wants to clear it on write. I think that the solution provided on that question will not work for him as is.
Answer: My guess is that when you run the code for the first time, the delete succeeds and your XDoc contains :
  <?xml version="1.0" encoding="utf-8"?>
    <data>
      <booze>booze1</booze>
      <booze>booze2</booze>
      <booze>booze4</booze>
    </data>
, but when calling XDoc.Save you simply append this to the favorites.xml file. After that, the favorites.xml file contains something like this:
<?xml version="1.0" encoding="UTF-8"?>
<data>
   <booze>booze1</booze>
   <booze>booze2</booze>
   <booze>booze3</booze>
   <booze>booze4</booze>
</data><?xml version="1.0" encoding="utf-8"?>
<data>
  <booze>booze1</booze>
  <booze>booze2</booze>
  <booze>booze4</booze>
</data>

That is why all the subsequent file loads throw the error. What you should do is overwrite the file, and not append to it. The first way of doing that that comes to mind is to close the stream, open it with Mode.Truncate and then save the XDoc. Or you can delete and recreate the file. I am not that familiar with IsolatedStorageFiles so this is a wild guess.
Comment: Ok, already thought about something like that. But how can I solve that? Clearing the `stream`? And how can I do that?
Comment: Because of the 'using' statement and the 'new' I doubt that this happens.
Comment: @PaulZahra I'd recommend for JelleKerkstra to try and create a new clear file and see if the append happens on XDoc.Save().
Comment: @Solver After looking into it a bit more I think your are correct, a quick test I think is to change FileMode.Open to FileMode.Create so that it overwrites the file instead of appending, look at this possible duplicate http://stackoverflow.com/questions/11032253/silverlight-xml-save-does-not-overwrite-the-entire-file
Comment: @PaulZahra I believe that FileMode.Create will overwrite the file on load, and you won't be able to read the XML. A possible option could be to use two streams. One stream to load the file (FileMode.Open) and one stream to write to the file (FileMode.Create). That would effectively read the file and then recreate it after the modifications have been made.
Comment: Can't get any combinations with FileMode.Create or Open and FileAcces.Write. So I'll just do a workaround by creating an empty xml document and insert the remaining elements. not the most elegant solution but it will work I hope
Comment: @JelleKerkstra Have you tried opening the file with FileMode.Open, loading it in your document with XDoc.Load, closing the filestream, modifying the XDoc, opening a new fileStream with FileMode.Create, and saving with the second filestream? In that order? :) I see no reason why that should fail...
Comment: @Solver Yay, followed you last comment and it works now!! Thanks a lot!!


Capturar query string no WordPress

Question: Estou tentando capturar a query string paged na página principal para chamar um componente e carregar os dados. Porém, o retorno dela é sempre 0.
Quando utilizo este componente em outras páginas, funciona normalmente.
Estrutura da URL com a Query String

http://minha_url.com.br/estilo/page/2/

Componente capturando a query string
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;

Página principal chamando o componente

Posts recentes

    <?php get_template_part('components/content', 'posts'); ?>

Retorno do get_query_var('paged') dentro da página principal

0

Não costumo utilizar WordPress mas creio que não seja um erro complexo.
Answer: Creio que 0 seja o valor real de paged mesmo. Você pode printar a global $wp_query->query_vars que o valor na página inicial deverá ser de fato 0.
Fiz isso com meu WordPress local na página inicial e o retorno foi o seguinte:
Array
(
    [error] =>
    [m] =>
    [p] => 0
    [post_parent] =>
    [subpost] =>
    [subpost_id] =>
    [attachment] =>
    [attachment_id] => 0
    [name] =>
    [static] =>
    [pagename] =>
    [page_id] => 0
    [second] =>
    [minute] =>
    [hour] =>
    [day] => 0
    [monthnum] => 0
    [year] => 0
    [w] => 0
    [category_name] =>
    [tag] =>
    [cat] =>
    [tag_id] =>
    [author] =>
    [author_name] =>
    [feed] =>
    [tb] =>
    [paged] => 0
    [meta_key] =>
    [meta_value] =>
    [preview] =>
    [s] =>
    [sentence] =>
    [title] =>
    [fields] =>
    [menu_order] =>
    [embed] =>
    [category__in] => Array
        (
        )

    [category__not_in] => Array
        (
        )

    [category__and] => Array
        (
        )

    [post__in] => Array
        (
        )

    [post__not_in] => Array
        (
        )

    [post_name__in] => Array
        (
        )

    [tag__in] => Array
        (
        )

    [tag__not_in] => Array
        (
        )

    [tag__and] => Array
        (
        )

    [tag_slug__in] => Array
        (
        )

    [tag_slug__and] => Array
        (
        )

    [post_parent__in] => Array
        (
        )

    [post_parent__not_in] => Array
        (
        )

    [author__in] => Array
        (
        )

    [author__not_in] => Array
        (
        )

    [ignore_sticky_posts] =>
    [suppress_filters] =>
    [cache_results] => 1
    [update_post_term_cache] => 1
    [lazy_load_term_meta] => 1
    [update_post_meta_cache] => 1
    [post_type] =>
    [posts_per_page] => 10
    [nopaging] =>
    [comments_per_page] => 50
    [no_found_rows] =>
    [order] => DESC
)

O código que utilizei foi este:
global $wp_query;
print_r($wp_query->query_vars);

Comment: Mas no meu caso teria que retornar o valor que está na URL, não é?
Comment: Então, o valor antes de estar na URL está na query. Mas dependendo de como você montou sua query, a paginação pode se comportar como numa página estática. Nesse caso teria que pegar o valor de 'page' ao invés de 'paged'. Exemplo:

`$page = (get_query_var('page')) ? get_query_var('page') : 1;`

Capturar query string no WordPress


Calculating the Expectation and Variance of having pairs after selecting N people

Question:
n pairs of parents attended a parent's meeting ( 2n people in total).
In the meeting, a delegation of 2k people was randomly chosen $2n \ge 2k \ge 4$ out of the 2n people. Calculate the expectation and variance of
the number of pairs of parents that were chosen for the delegation.

I'm trying to solve this question and I'm not confident at all about my attempt.. it feels like I'm shooting in the dark and doing things I don't really understand fully, plus I'm having a hard time with the algebra of calculating the variance so I thought I'll put it on hold until I get the expectation right
My attempt:

Let $X,X_{i}$ such that
$X = $Numer of pairs of parents that were chosen for the deligation
$X_{i} =  \left\{  \begin{array}{lcl} 1 \ \ \ if \ person \ i's \
> partner \ is \ selected\\ 0 \ \ \ else\\ \end{array} \right.$
Note that $X\ =\frac{1}{2}\ \sum_{i=1}^{2k}X_{i}$, also note that
$X_{i\ }\sim HG\left(2n,2,2k\right)$, as we are choosing 2k times and
are looking for 2 specials (a pair) out of a population of 2n.
$E\left(X\right)=E\left(\frac{1}{2}\sum_{i=1}^{2k}X_{i}\right)=\frac{1}{2}\sum_{i=1}^{2k}E\left(X_{i}\right)=\frac{1}{2}\sum_{i=1}^{2k}2k\cdot\frac{2}{2n}=\frac{2}{4n}\cdot\left(2k\right)^{2}$

first of all, I'm not sure that $X=X_{i}$, the whole notion of indicators is very confusing to me..
I'm also not confident that $X_{i}$ has an HG distribution.
I was wondering if I could get tips on how to approach these type of questions, as I feel quiet lost
Answer: I think an approach based on induction would work here. The trick is that even though the size of the delegation is specified to be even in the question, the delegation could in principle have any size from $0$ to $2n$. I'll let $d$ stand for the size of the delegation, so $d=2k$ for the original question.
Let $X$ be the number of pairs of parents in the delegation. For arbitrary $n, d$, we'll define the mean of $X$ to be $M(n,d)$. I'll stick with computing the mean here, getting the variance should be similar, but harder.
We have some base cases from observing that a delegation of 1 contains no pairs, and a delegation of $2n-1$ must contain all pairs but one, i.e. $n-1$ pairs. In equations:
$$
\begin{matrix}
M(n,0) = 0 \\
M(n,1) = 0 \\
M(n,2n-1) = n-1 \\
M(n,2n) = n
\end{matrix}
$$
Those are the base cases.
Suppose $1 < d < n-1$. Then consider one random pair of parents. There are 3 cases:
Case 1: Both parents are in the delegation. This has probability $p=\frac{d}{2n}\frac{d-1}{2n-1}$
Case 2: Both parents are not in the delegation. This has probability $p=\frac{2n-d}{2n}\frac{2n-d-1}{2n-1}$
Case 3: One parent is in the delegation, and one is not in the delegation. This has probability $p=\frac{d}{2n}\frac{2n-d}{2n-1} + \frac{2n-d}{2n}\frac{d}{2n-1} = 2\frac{d}{2n}\frac{2n-d}{2n-1}$
This gives a recurrent relation for $M(n,d)$. We just weight the 3 cases by their probabilities, and use the result previously computed for $n-1$ pairs of parents. In Case 1, we add 1 to the previous result, since the current pair of parents is in the delegation.
$$
M(n,d) = \frac{1}{2n(2n-1)}
\begin{pmatrix}
d(d-1)(1+M(n-1, d-2)) \\
+ 2d(2n-d)M(n-1, d-1) \\
+ (2n-d)(2n-d-1)M(n-1,d)
\end{pmatrix}
$$
This allows us to build up a table for $M$ starting from $n=0,d=0$. in the top left, and with $n$ increasing down the table and $d$ increasing across.
$$
\begin{matrix}
0 \\
0 & 0 & 1  \\
0 & 0 & & 1 & 2 \\
\end{matrix}
$$
Here we must find the first nontrivial value of the table, $M(2,2)$.
$$
M(2,2) = \frac{1}{4\times 3}(2\times1(1+M(1,0)) + 2\times2\times 2 M(1,1) + 2\times1M(1,2)) = \frac{1}{12}(2+0+2) = \frac{1}{3}
$$
Resulting table:
$$
\begin{matrix}
0 \\
0 & 0 & 1  \\
0 & 0 & \frac{1}{3} & 1 & 2 \\
\end{matrix}
$$
Through the magic of python, we can generate a much larger table and look for patterns:
$$
\begin{matrix}
0 \\
0 & 0 & 1 \\
0 & 0 & \frac{1}{3} & 1 & 2 \\
0 & 0 & \frac{1}{5} & \frac{3}{5} & \frac{6}{5} & 2 & 3 \\
0 & 0 & \frac{1}{7} & \frac{3}{7} & \frac{6}{7} & \frac{10}{7} & \frac{15}{7} & 3 & 4 \\
0 & 0 & \frac{1}{9} & \frac{1}{3} & \frac{2}{3} & \frac{10}{9} & \frac{5}{3} & \frac{7}{3} & \frac{28}{9} & 4 & 5 \\
\end{matrix}
$$
The formula here is fortunately easy to guess: it's just triangular numbers divided by odd numbers:
$$
M(n,d) = \frac{d(d-1)}{2(2n-1)}
$$
Going back and proving that this satisfies the recurrence relation is left as an exercise.
Answer: The following is a derivation of the expected value of the number of pairs, using the indicator method.
Number the pairs from $1$ to $n$, and define
$$X_i = \begin{cases}
1 \qquad \text{if pair i is included in the delegation} \\
0 \qquad \text{otherwise}
\end{cases}$$
for $1 \le i \le n$.  Then
$$P(X_i = 1) = \frac{(2k)(2k-1)}{(2n)(2n-1)}$$
so applying linearity of expectation, the expected number of pairs in the delegation is
$$E \left( \sum_{n=1}^n X_i \right) = \sum_{n=1}^n E(X_i) = n \cdot \frac{(2k)(2k-1)}{(2n)(2n-1)} = \frac{k(2k-1)}{2n-1}$$
Just a hint on finding the variance by the indicator method: the first step is to find $E(\sum_{i<j} X_i X_j)$.


Perl create a formatted table without using an object

Question: I want to simply output some results in a table without there being any offset problems. Do not worry about the foreach and the output of values that is just pseudocode for clarity.
print "\n  ______________________________________________________";
print "\n |                                                      |";
print "\n |                        Title                         |";
print "\n +______________________________________________________+";
print "\n |                          |                           |";
print "\n |          City            |          Size             |";
print "\n |__________________________|___________________________|";

#Sort by highest scores
################################
foreach (city, size)
{
print "\n | (city(value)";
print "| (size(value)";
}

Any Ideas?
Comment: In which Scalar are your Cities/Sizes hold ? Arrays ? Hashes ?
Comment: I like to use the Text::FormatTable module for this sort of thing. A good place to start: http://search.cpan.org/~trey/Text-FormatTable-1.03/lib/Text/FormatTable.pm
Answer: It's rarely used anymore, but Perl has the built in ability to create these type of forms.
Basically, you use a specification to state how you want these tables formatted, and where information in these tables will be placed using the format statement. Then, you use the Perl write statement to write to that format. You can specify headers and footers of your tables too.
Answer: I suggest you use
substr
to overwrite the correct portion of a template line.
use strict;
use warnings;

my %data = (
  Birmingham  => 1_000_000,
  Bristol     => 430_000,
  Manchester   => 110_000,
);

print "  ______________________________________________________\n";
print " |                                                      |\n";
print " |                        Title                         |\n";
print " +______________________________________________________+\n";

my $template =
      " |                          |                           |\n";
print $template;

while (my ($city, $size) = each %data) {
  my $line = $template;
  substr $line, 12, length $city, $city;
  substr $line, 39, length $size, $size;
  print $line;
}

print " |__________________________|___________________________|\n";

output
  ______________________________________________________
 |                                                      |
 |                        Title                         |
 +______________________________________________________+
 |                          |                           |
 |          Bristol         |          430000           |
 |          Manchester      |          110000           |
 |          Birmingham      |          1000000          |
 |__________________________|___________________________|


Colored Tabs missing in VS2022 17.3 (not preview)

Question: Here it is stated that...
<code>Coloured tabs is supported in Visual Studio 2022. From release 17.1 Preview 2 we can now explicitly set the colour per project…
</code>
I should look like this:

I am using VS2022 Version 17.2.3, and I don't see this.
I see this instead:

I wonder what is happening here.
Is Version 17.2.3 perhaps older than the said 17.1 Preview 2?
Is it possible that 17.1 Preview 2 is used for active development and therefore newer than official newer versions? I just don't understand it.
Thanks.
Answer: I got it.
It's there, but it needs to be activated first:

And here it is German... I just couldn't find it in German language, so I had to switch language to English first in order to find it LOL.
Here it is in German:


The meaning of two sided (double tailed) p-values

Question: A p-value is "the probability for a given statistical model that, when the null hypothesis is true, the statistical summary would be the same as or of greater magnitude than the actual observed results." Source
Thus, the p-value is defined as $\mathbb P_r(X ≥ x|H )$ for a one sided (right tailed) test.
For a two sided (double tailed) test, this probability is doubled, meaning $ p = 2 \mathbb P_r(X ≥ x|H ) $ and then compared to $\alpha$ (e.g .05). However, how can this two sided p-value be interpreted as the probability as defined above? The real probability of getting a stat. summary of greater magnitude than the observed result (e.g. +2) is $ \mathbb P_r(X≥2|H)$ and not $2 \mathbb Pr(X≥2|H)$. Yet, statisticians double the right-hand p-value. There is, of course, an error probability on the left side too ($\alpha/2$), but this should be of no concern for us, as our observed value is positive and thus creates a probability only on its right side to $ +\infty $ below the curve.
The question, therefore, is: Would it not be more mathematically accurate to compare $ \mathbb P_r(X≥2|H)$ to $\alpha/2$ $(=.025)$ instead of $2 \mathbb P_r(X≥2|H)$ to $\alpha$ $(=.05)$ in a two sided test? The results would of course be the same in symmetric distributions, but not in asymmetric ones!
Furthermore, if we say that p for double tailed tests is indeed $2 \mathbb P_r(X≥x|H)$, then this $p$ is not the probability of $X$ being of greater magnitude than $x$, but rather this probability times $2$.
Comment: One of my intentions in writing https://stats.stackexchange.com/a/130772/919 was to help people understand two-tailed tests (and even more complicated situations), so please take a look.  Your quotation, although from a reliable source, is correct only in certain textbook situations.  It is not correct as a characterization of p-values generally.
Answer: I would prefer to consider extreme rather than magnitude
If you are expecting the value $0$ for something thought to have a symmetric distribution but in fact see the value $2$
then other values that you might have seen which would be as extreme as $2$ or more extreme than $2$ would include all values greater than or equal to $2$ and all values less than or equal to $-2$
so the probability of seeing $2$ or a more extreme value can be expressed as $\mathbb P(|X| \ge 2) = \mathbb P(X \ge 2)  + \mathbb P(X \le -2) = 2\mathbb P(X \ge 2)$ by symmetry
In asymmetric distributions, you need to decide what extreme means in the context of your experiment
If you had a lightbulb which had an exponentially distributed lifetime L expected to be $1000$ hours, you might decide that extreme means that seeing a lifetime of $4000$ hours or more would be as extreme as seeing a lifetime of about $18.5$ hours or less, or you might take a different view. You could still say $\mathbb P(L \ge 4000) + \mathbb P(L \le 18.5) \approx 2\mathbb P(L \ge 4000) \approx 0.0366$
You should decide what you mean by as extreme or more extreme before you see the actual observation, and this is especially important with a distribution believed to be asymmetric


Mini electric guitar tuning

Question: To make it short, I am in the military and I try to take a guitar every time I am deployed. The thing is I can't take much with me so I need a very (very very) small guitar. I have a LAG guitalele (basically a uke with 6 strings), normally tuned in A, but a use Cordoba Mini E strings, specially made to have short instruments tuned in E. Recently I found this very small electric guitar, the Fazley Niño, with an integrated amp that works on battery. It's between an electric guitalele, a kid electric guitar and a toy, but it will do the trick when I am somewhere in the desert and I wanna play something. But it is also tuned in A and I would also like to tune it in E... I can't find special strings like the ones on my guitalele, the mini E strings being only for acoustic instruments (nylon strings). I can go down to F sharp without it being sloppy. Anyone has an idea or a trick so I can tune it down to E? (extra tension strings maybe?)
Thanks!
Comment: Knowing the length of open strings would help us answer.
Comment: Right. 500mm /19,68 inches
Answer: The short answer is that you need heavier-gauge strings.
The physics behind this is that, for a given length and tension, heavier strings vibrate more slowly because they have more mass per unit length.This article, "Ultimate guide to guitar scale length", has more details.

Mini electric guitar tuning


How to approach a GraphQL query that returns a boolean value?

Question: Need to check whether an email is available or taken during the user sign-up process. The goal is to quickly query, using GraphQL, the API server and have it tell us if the email is available or taken.
What is the general best practice on a simple boolean-ish type of situation using GraphQL?
Below is what I have come up with but I am unsure if this is a good practice or not and want to hear feedback on a better practice on queries like this.
Request:
query {
  emailExists(email:"email@example.com") {
    is
  }
}

Response:
{
  "data": {
    "emailExists": {
      "is": true
    }
  }
}

Answer: A "query" is just a field on what happens to be the Query type. A field can return any output type, including scalars -- it doesn't need to return an object. So it's sufficient to have a schema like:
type Query {
  emailExists(email: String!): Boolean!
}

The only reason to prefer an object type would be if you anticipated wanting to add additional fields in the future (i.e. something other than your current is field).
Comment: Just for the records, with this solution the request would be this (no field selection is needed):
```graphql
query {
  emailExists(email:"firstname.lastname@example.com")
}
```
Comment: @Marco eslint crashes if i use a query without selection
Comment: Exactly what I am looking for!! Thanks so much.


How to perform an HTTP redirect (302) using Rack?

Question: I am messing around with my first Rack application (this is just for experimentation).
When a call comes in I do something like this:
class Application
    def call(env)
      # Totally ignore favicons for the time being
      if env['PATH_INFO'] == '/favicon.ico'
        return [ 404, {'Content-Type' => 'text/html'}, [] ]
      elsif env['PATH_INFO'] == '/'
        return [ 302, {'http-equiv' => "refresh", 'content' => "2;url=http://google.com"}, [] ]
      end
      ...

I know this is horrible... but, again, this isn't a serious project.
I'm trying to figure out how to do the redirect. What I have does not work. Basically, when you hit / on my site I want to redirect that require to google.com.
Comment: There are two `end` missing in your program.  It does not compile without those
Comment: It was not my intention to have it be compilable (you can't test it in isolation anyway). My intension was to signify that there is elided code with the use of ellipses.
Answer: Here is working app with re-direct to Google
require 'rack'
require 'rack/server'

class HelloWorld
  def response
    [ 302, {'Location' =>"http://google.com"}, [] ]
  end
end

class HelloWorldApp
  def self.call(env)
    HelloWorld.new.response
  end
end

Rack::Server.start :app => HelloWorldApp


Calculating the total amount of change given

Question: Hello I'm currently following the Computing with C# and the .NET Framework book and I'm having difficulty on one of the exercises which is

Write a C# program to make change. Enter the cost of an item that is less than one dollar. Output
  the coins given as change, using quarters, dimes, nickels, and pennies. Use the fewest coins
  possible. For example, if the item cost 17 cents, the change would be three quarters, one nickel,
  and three pennies

Since I'm still trying to grasp c# programming the best method I came up with is using the while loop.
while(costOfItem >= 0.50)
            {
                costOfItem -= 0.50;
                fiftyPence++;
            }

I have these for each of the pences 20,10,5 etc..
I'm checking if the amount is greater than or equal to 50 pence, if so, i reduce 50 pence from the amount given by the user and add 1 to the fiftypence variable.
then it moves onto the next while loop, which I have for each pences. The problem is, somewhere along the line one of the loops takes away, lets say 20 pence, and the costOfItem becomes something like "0.1999999999999" then it never drops down to 0, which it should to get the correct amount of change.
Any help is appreciated, please don't suggest over complex procedures that I have yet covered.
Comment: What type of unit are you using for the money given?
Comment: what datatype are you using for `costOfItem`? Sounds like its `float`, if that's true, you should try `decimal` instead.
Comment: You need to use `decimal` to represent money.  Floating point types like `double` and `float` are not appropriate and will result in these sort of issues. See [this related question](http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when).
Answer: Never use double or float for money operations. Use Decimal.
For all other problems of calculation accuracy you have to use "Double Epsilon Comparison" like Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to
Answer: Sounds to me you are using float or double as datatype for costOfItem.
float and double store their values in binary. But not all decimal values have an exact representation in binary. Therefore, a very close approximation is stored. This is why you get values like "0.1999999999999".
For money calculations, you should always use decimal, since they avoid those small inaccuracies.
You can read more about the difference in Jon Skeets awesome answer to Difference between Decimal, Float and Double in .NET?
Answer: If you do the calculation in cents, you can use integers and then you don't get into floating point rounding problems
Comment: this is called Mill Representation and is based on the Mill Currency. All operations are based on integer. 1000 Mill = 1 Dollar.
Answer: Thank you all for the fast replies. The issue was that i used double instead of decimal the links provided have people explaining why it's wrong to do so in some cases. Using double should be avoided in arithmetic since apparently some float numbers do not have exact a binary representation, so instead of .23 it gives 0.2299999999999. Changing the variable to decimal fixed my issue


Tkinter: if statements not working when clicking a button

Question: I'm trying to make a game of rock, paper and scissors using Tkinter, but the buttons I created neither update who the winner was nor the score.
from tkinter import *
from random import randint

root = Tk()
score = 0

scorel = Label(root, text= "Your score is: " + str(score) + "!")
scorel.grid(row=0, column=1)

move = "none"
moves = ["Rock", "Paper", "Scissors"]

def rock():
    move = "Rock"
    return move

def paper():
    move = "Paper"
    return move

def scissors():
    move = "Scissors"
    return move

cmove = "none"
resultw = Label(root, text="No winners")
resultw.grid(row=2, column=1)

def game():
    global score
    global clabel
    resultw.destroy()
    cmove = moves[randint(0, 2)]
    if move == cmove:
        resultw.config(text="It's a tie!")
    if move == "Rock":
        if cmove == "Scissors":
            resultw.config(text="You win!")
            score += 1
        else:
            resultw.config(text="You lose!")
            score -= 1
    if move == "Paper":
        if cmove == "Rock":
            resultw.config(text="You win!")
            score += 1
        else:
            resultw.config(text="You lose!")
            score -= 1
    if move == "Scissors":
        if cmove == "Paper":
            resultw.config(text="You win!")
            score += 1
        else:
            resultw.config(text="You lose!")

rockb = Button(root, text="Rock", command=lambda: [rock(), game()])
rockb.grid(row=3, column=0, ipadx=30, ipady=20)

paperb = Button(root, text="Paper", command=lambda: [paper(), game()])
paperb.grid(row=3, column=1, ipadx=30, ipady=20)

scissorsb = Button(root, text="Scissors", command=lambda: [scissors(), game()])
scissorsb.grid(row=3, column=2, ipadx=30, ipady=20)

root.mainloop()

I tried calling move and cmove as global, defining them in the rock/paper/scissors function but none of it made any differcence, while
    clabel = Label(root, text="Enemy move is: " + cmove + "!")
    clabel.grid(row=1, column=2)

that I have inside my original code in game function (before the if statements) does work correctly.
Comment: You have to _define_ `move` as `global` in those `rock`, `scissors` etc. functions. Or, much simpler, just pass the move as a parameter to `game`, e.g. `command=lambda: game("Rock")`
Answer: I believe we can do this with much simpler code, and without making move a global nor using a lambda:
from tkinter import *
from random import choice

beats = {"Rock": "Scissors", "Paper": "Rock", "Scissors": "Paper"}

def rock():
    game("Rock")

def paper():
    game("Paper")

def scissors():
    game("Scissors")

score = 0

def game(move):
    global score

    cmove = choice(list(beats))

    if move == cmove:
        resultw.config(text="I also chose " + cmove + ", it's a tie!")
    elif beats[move] == cmove:
        resultw.config(text="I chose " + cmove + ", you win!")
        score += 1
    else:
        resultw.config(text="I chose " + cmove + ", you lose!")
        score -= 1

    scorel.config(text="Your score is: " + str(score))

root = Tk()

scorel = Label(root, text="Your score is: " + str(score))
scorel.grid(row=0, column=1)

resultw = Label(root, text="")
resultw.grid(row=2, column=1)

Button(root, text="Rock", command=rock).grid(row=3, column=0, ipadx=30, ipady=20)
Button(root, text="Paper", command=paper).grid(row=3, column=1, ipadx=30, ipady=20)
Button(root, text="Scissors", command=scissors).grid(row=3, column=2, ipadx=30, ipady=20)

root.mainloop()


Predict the next number in a descending sequence of numbers?

Question: I know this is a small sample but it's all I got.
<code>6.9, 7.2, 7.2, 7.6, 8.1, 8.2, 8.4, 8.8, 9.0, 10.1, 10.2, 10.6, 12.9, 22.5
</code>

Context: the numbers are waiting periods (in years) for people that are looking for housing. The person who waited the longest waited for more than 22 years.
In this case, housing is offered to the people who have the longest waiting period first. This is why it's a descending sequence of numbers. As a limited supply of housing is offered to people the minimum required waiting period for each person decreases.
I'm want to be able to predict the shortest (lowest) waiting period in this sequence. I know the length of the sequence since I know the number of objects available for each housing project. I have data to cross validate against, like how many people have waited X years in total. And waiting periods for similar housing from past years in similar areas. But I'm not sure how I would go about bringing these together to create a good predictor.
Basically how would I go about doing forecasting for something like this? I tried regression analysis but it didn't yield anything good.
Comment: 1 What do you mean by "descending number" there? $\:$ 2. Are you assuming the waiting times you have to be independent draws from the distribution of times that the next value will be drawn from? Assuming no censoring (e.g. that people don't give up on waiting and do something else ... -- like die first), it's a simple question. Otherwise it depends on what else you assume about the situation.
Comment: In this case housing is given to the people who have the longest waiting period first. This is why it's a descending sequence of numbers. As a limited supply of housing is offered to people the minimum required waiting period for each person decreases.
Comment: This is vital information; my previous thoughts on the question don't really apply.  Please edit your question to include the information.
Comment: @Glen_b done. I also rephrased my question because lower than 6.6 is just a specific case, something I can determine if I can predict the sequence with a reasonable margin of error.
Answer: You could treat this as a survival analysis assuming you know both how long people who have been successful in obtaining housing have had to wait as well as how long people who are still waiting for housing have been queued. In survival analysis, this latter group are called "censored" observations meaning that information as to their status wrt obtaining housing is not available as of the last period in the time series. Survival analysis is a class of models developed specifically for those special cases in predictive modeling where data are censored.
One excellently written and intuitive introduction to this class of models is Paul Allison's Survival Analysis Using SAS: A Practical Guide, Second Edition. This book covers the waterfront from the simplest Kaplan-Meier survival curves through parametric failure models to Cox proportional hazards models. Forget about the fact that it is focused on SAS software, Allison's development and overview of the methods is software agnostic in most important respects. In other words, while it would be nice to know and use SAS software in the context of his book, it remains a highly useful resource without it.
What is problematic about your specific question is that it asks for a likelihood of a value less than anything in your observed data. While this is theoretically possible, the usual caveats apply when extrapolating to data outside the observed range of any reference information.
Comment: My method may very well be flawed. All I really want to know is the probability of being offered housing in an area given the waiting periods. In this case 6.6 just refers to a specific cutoff in my case. I thought asking for the likelihood for it being lower was the right thing to do.


Como executar o ProcessList periodicamente na linha de comando?

Question: Existe alguma maneira de ver o comando SHOW PROCESSLIST de X em X segundos, pela linha de comando?
Eu estou usando o Sistema Operacional Ubuntu 16.
Comment: Se usar o `watch SHOW PROCESSLIST ` pode ver de x em x segundos (inclusive pode especificar o tempo), se eu entendi sua pergunta corretamente :p
Comment: @Tuxpilgrim não funcionou.
Answer: Você pode usar o comando mysqladmin -u root -p -i 1 processlist, onde:

-u usuário
-p será perguntada a senha
-i 1 intervalo em segundos

O resultado será algo parecido com isso:
+----+------+-----------+----+---------+------+----------+------------------+
| Id | User | Host      | db | Command | Time | State    | Info             |
+----+------+-----------+----+---------+------+----------+------------------+
| 76 | root | localhost |    | Query   | 0    | starting | show processlist |
+----+------+-----------+----+---------+------+----------+------------------+

referência: MySQL: Show processlist every second


Use generated JWT from Keycloak to authenticate user using REST API

Question: We are working on developing a mobile app and users should be able to log in to the app and in our backend Drupal site authenticated from Keycloak. Our main goal is that the user would be able to login from the app to update/create content using REST api.
We will be using a JWT token generated from Keycloak to authenticate user in the backend site.
We are using the following Drupal core and module versions:

Drupal: 8.5.3
OAuth2 JWT SSO: 8.x-1.0-rc1
OpenID Connect: 8.x-1.0-beta5
RESTful Web Services: 8.5.3
Simple OAuth: 8.x-3.0
OpenID Connect REST API: 8.x-1.0-rc1

I will first try with REST API to see if the implementation will work before integrating it to our mobile app.
Below are the steps of REST request executed in POSTMAN tool
Pre-setup
From http://192.168.254.107:8080/admin/config/services/openid-connect
We enable generic and fill in the following fields
GENERIC
Redirect URL: https://192.168.254.107/openid-connect/rest/generic
Client ID: opensocial
Client secret: XXXXX-XXXXX-XXXXX
Authorization endpoint
http://192.168.254.107:8083/auth/realms/master/protocol/openid-connect/auth
Token endpoint
http://192.168.254.107:8083/auth/realms/master/protocol/openid-connect/token
UserInfo endpoint
http://192.168.254.107:8083/auth/realms/master/protocol/openid-connect/userinfo

And I have client created in Keycloak,
http://192.168.254.107:8083/auth/admin/master/console/#/realms/master/clients/XXXXX-XXXXX-XXXXX
Client ID: opensocial
Client Protocol: openid-connect
Access Type: Confidential
* Valid Redirect URIs : http://192.168.254.107:8080/*
Base URL: http://192.168.254.107:8080
Web Origins: http://192.168.254.107:8080/*

Steps

Generate JWT from Keycloak
REQUEST: POST
ENDPOINT: http://192.168.254.107:8083/auth/realms/master/protocol/openid-connect/token
HEADER: Content-Type: application/x-www-form-urlencoded
BODY: client_id=opensocial&client_secret=f3e8f92d-c9ff-4139-b715-33e3aaa7194d&username=[username_from_keycloak]&password=[password_from_keycloak]&grant_type=password

RESPONSE:

    {
        "access_token": "XXXXX.XXXXX.XXXXX",
        "expires_in": 3600,
        "refresh_expires_in": 1800,
        "refresh_token": "XXXXX.XXXXX.XXXXX",
        "token_type": "bearer",
        "not-before-policy": 0,
        "session_state": "7f739b98-421c-4aaf-a85c-6e38424d9492",
        "scope": "email profile"
    }

Validated in jwt.io website and/or by fetching userinfo from Keycloak
REQUEST: GET
ENDPOINT: http://192.168.254.107:8083/auth/realms/master/protocol/openid-connect/userinfo
HEADER: Authorization: Bearer [access_token]
RESPONSE:

{
    "sub": "331e758e-b7cd-44b0-93ca-97aa44310335",
    "email_verified": false,
    "preferred_username": "admin"
}

Create content using token
REQUEST: POST
ENDPOINT: http://192.168.254.107:8080/node (this is the backend site)
HEADERS:
Content-Type: application/json
Authorization: Bearer [access_token]
RESPONSE:
Status: 500 500 Service unavailable (with message)
The website encountered an unexpected error. Please try again later.

In Drupal logs I find the following.

Type  php
Date    Thursday, May 9, 2019 - 06:52
  User    Anonymous (not verified)
  Location    http://192.168.254.107:8080/node
  Referrer
  Message Error: Call to a member function validateAuthenticatedRequest() on null in Drupal\simple_oauth\Server\ResourceServer->validateAuthenticatedRequest() (line 63 of /opt/app-root/src/html/modules/simple_oauth/src/Server/ResourceServer.php) #0 /opt/app-root/src/html/modules/simple_oauth/src/Authentication/Provider/SimpleOauthAuthenticationProvider.php(63): Drupal\simple_oauth\Server\ResourceServer->validateAuthenticatedRequest(Object(Symfony\Component\HttpFoundation\Request)) #1 /opt/app-root/src/html/core/lib/Drupal/Core/Authentication/AuthenticationManager.php(52): Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider->authenticate(Object(Symfony\Component\HttpFoundation\Request)) #2 /opt/app-root/src/html/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php(78): Drupal\Core\Authentication\AuthenticationManager->authenticate(Object(Symfony\Component\HttpFoundation\Request)) #3 [internal function]: Drupal\Core\EventSubscriber\AuthenticationSubscriber->onKernelRequestAuthenticate(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) #4 /opt/app-root/src/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) #5 /opt/app-root/src/vendor/symfony/http-kernel/HttpKernel.php(127): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.request', Object(Symfony\Component\HttpKernel\Event\GetResponseEvent)) #6 /opt/app-root/src/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #7 /opt/app-root/src/html/modules/simple_oauth/src/HttpMiddleware/BasicAuthSwap.php(67): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #8 /opt/app-root/src/html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #9 /opt/app-root/src/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #10 /opt/app-root/src/html/modules/jsonapi/src/StackMiddleware/FormatSetter.php(40): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #11 /opt/app-root/src/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #12 /opt/app-root/src/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #13 /opt/app-root/src/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #14 /opt/app-root/src/html/core/lib/Drupal/Core/DrupalKernel.php(664): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #15 /opt/app-root/src/html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #16 {main}.
  Severity    Error
  Hostname    192.168.254.107

Why does using a valid JWT token generated from Keycloak resulted into user not being authenticated in Drupal using REST API request? Is the method I used not possible? If not, then is there any other processes I can follow to achieve the goal?
Answer: JWT token generated from keycloak is rejected because Drupal api accepts token generated by its own internal authorization server.
Drupal's api is protected by OAuth2 protocol. Drupal use its own OAuth2 authorization server to protect its api. So, to gain access to drupal api, you need to provide an access token issued by drupal authorization server.
you can check the "iss" field in the access_token: value must be related to drupal's authorization server.
About the keycloak configuration you gave: it is related to the drupal account creation / login via OpenIdConnect. It allow to automatically create an account / log into with user data and credentials provided by keycloak. But it is not related to drupal api access. These are 2 distinct things:

OpenIdConnect: to create an account and log into system using an external identity provider, like keycloak here
OAuth2: to restrict / secure api access.

I'm also looking for a way to configure drupal to use an external OAuth2 authorization server like Keycloak
Comment: Hello @Thomas LIMIN, I'm also looking for the same solution. Any chance?


Cohomology group of an algebra

Question: I tried to guess what is a cohomology group of an algebra. I would like to find the correct definition of this. I know what is a cohomology group of a group, but I don't know how connect the second cohomology group to the central extension of a Lie-algebra.
Could someone help me to tell the definition or explain to me this?
Thanks!
Comment: When you say "algebra" do you mean "Lie algebra"?
Comment: Yes, I mean Lie-algebra.
Comment: http://en.wikipedia.org/wiki/Lie_algebra_cohomology
Comment: The cohomology theory for Lie algebras is presented in pretty much every textbook on homological algebra, from the venerable book by Cartan and Eilenberg to the one by Hilton and Stammbach to the one by Weibel, and so on. Weibel, in particular, discusses the connection with central extensions with lots of detail..
Answer: The notion that you probably want is that of Hochschild (co)homology.  This is a way of getting (co)homology groups for associative algebras, but it actually specializes to group cohomology and Lie algebra cohomology.  The reason this is possible is because modules over a finite group $G$ are the same thing as modules over the group algebra $k[G]$, and similarly, modules over a Lie algebra $\mathfrak g$ are the same thing as modules over the universal enveloping algebra $\mathcal U\mathfrak g$.  It turns out that (co)homology groups really only care about the category of modules, and so the framework of associative algebras subsumes that of finite groups and modules or lie algebras and their modules.
Of course, while this general framework gives you a unifying picture for everything, don't forget that you might have intepretations of cohomology groups in special cases that don't generalize.
Comment: In fact, no. If the OP is interested in cohomology of Lie algebras Hochschild cohomology is probably not what he is after but Lie algebra cohomology (which is certainly related to Hochschild cohomology but not quite the same)
Comment: Apologies, I hadn't read the comment where OP clarified algebra to mean Lie algebra.  I still think that anybody dealing with algebraic cohomology theories should learn about Hochschild cohomology, though, so I will leave my answer.
Comment: (Hochschild cohomology can be computed of a group algebra and of a Lie algebra, but in neither case the result coincides with the group cohomology of the group or the Lie algebra cohomology of the Lie algbra.)
Comment: That's not true.  If I remember the results correctly, you have an isomorphism if, for example, you view a group representation as a bi-module with the induced structrue on the left and the trivial structure on the right.
Comment: Yes, for specific choices you can do that. But Hochschild cohomology of both group algebras and of enveloping algebras of Lie algebras is distinctly different from group cohomology and Li algebra cohomology as a theory. Trust me, I find Hochschild cohomology very much pleasing (it is the subject I work on, in fact) but to the OP going through the route of Hochschild cohomology would be a detour too long to make much sense.
Comment: For one thing, the concrete interpretations of low degree groups —like the second one in terms of central extensions of Lie algebras and such— are going to be rather painful in terms of Hochschild cohomology.
Comment: I agree that at a superficial level, the theories are different (they are computed using different standard complexes), and so the initial interpretations of low dimensional groups will be different, but all of this is just Ext and Tor anyway, and I personally find it comforting when two similar looking theories can both be subsumed into a larger unifying theory.
Comment: It's all Exts and Tors, sure —that is the point of the book of Cartan-Eilenberg— and you could frame the whole thing in terms of cohomology of operads, if you want unification, or even more elaborate frameworks. Almost none of all that, though, will likely help the person who asked the question you are answering.
Comment: Notice that it is not that the difference lies in that the theories use different standard complexes (indeed, the other point of Cartan and Eilenberg is that there is no need to worry about standard complexes at all...) but that they are Exts and Tors in different categories.
Comment: Thank you for your answers, but I didn't deal with cohomology theories before, I just wanted to know specially what is the connection the Witt-algebra and the Virasoro-algebra through the second chomology group.
Comment: @HajnalkaKorka, then do go read Weibel: he does not do the example of the Witt algebra to construct Virasoro, but the one used to construct affine Lie algebras, which is exactly analogous.

Cohomology group of an algebra


Metatag migration from html

Question: Drupal 8.8
Modules: Metatags & Migration plus.
I am trying to convert a tag in an XML file to an open graph image metatag.
File:
modules/custom/my_module/dataxml.html
This is the url that has to be converted to the image metatag.
<ImageUrl>
  <![CDATA[https://via.placeholder.com/300.jpg/09f/fff]]>
</ImageUrl>

File: modules/custom/my_module/config/install/migrate_plus.migration.content.yml
These are the source field for the required metatag.
source:
  fields:
     - name: og_image_url
       label: Image
       selector: ImageUrl

Process for the required metatag
process:
  field_meta_tags:
    source: og_image_url
    plugin: prepare_meta

File: modules/custom/my_module/src/Plugin/migrate/process/PrepareMeta.php
The plugin links the metatag with the required value and serializes the mapped result
<?php

namespace Drupal\my_module\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
 * Plugin to prepare the meta tags.
 *
 * @MigrateProcessPlugin(
 *   id = "prepare_meta",
 * )
 */
class PrepareMeta extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $metas = [];

    // This should likely work with an array later on, but for now, it'll do.
    if (!empty($value)) {
      $metas[$this->configuration['source']] = $value;
    }

    return $metas ? serialize($metas) : [];
  }
}

When I run xDebug $metas has the following value:
$metas = [
og_image_url => "https://via.placeholder.com/300.jpg/09f/fff"
];
The node is created and the other field are migrated perfectly. However the  metatag field og:image:url remains empty and doesn't trigger an error. I don't really know what I'm doing wrong here. If someone could point me in the right direction, that would be much appreciated.
Answer: Turns out I used the wrong field name. My field is called field_meta_tag instead of field_meta_tags as used in the question above.

Metatag migration from html


How to calculate the variance of a partition of variables

Question: I'm running an experiment where I'm gathering (independent) samples in parallel, I compute the variance of each group of samples and now I want to combine then all to find the total variance of all the samples.
I'm having a hard time finding a derivation for this as I'm not sure of terminology. I think of it as a partition of one RV.
So I want to find $Var(X)$ from $Var(X_1)$, $Var(X_2)$, ..., and $Var(X_n)$, where $X$ = $[X_1, X_2, \dots, X_n]$.
EDIT: The partitions are not the same size/cardinality, but the sum of the partition sizes equal the number of samples in the overall sample set.
EDIT 2: There is a formula for a parallel computation here, but it only covers the case of a partition into two sets, not $n$ sets.
Comment: Is this the same as my question here:
http://mathoverflow.net/questions/64120/how-to-estimate-sample-mean-and-variance-from-derived-data
Comment: What does that last bracket mean?  And what do you mean by "total variance"?  Is it anything other than the variance of the combined dataset?
Comment: @whuber which last bracket? "total variance" means the variance of the total dataset.
Comment: The expression $[X_1, X_2, \dots, X_n]$ could mean many things (although conventionally it would be a vector): I was looking for a clarification.
Answer: The formula is fairly straightforward if all the sub-sample have the same sample size. If you had $g$ sub-samples of size $k$ (for a total of $gk$ samples), then the variance of the combined sample depends on the mean $E_j$ and variance $V_j$ of each sub-sample:
$$ Var(X_1,\ldots,X_{gk}) = \frac{k-1}{gk-1}(\sum_{j=1}^g V_j + \frac{k(g-1)}{k-1} Var(E_j)),$$ where by $Var(E_j)$ means the variance of the sample means.
A demonstration in R:
> x <- rnorm(100)
> g <- gl(10,10)
> mns <- tapply(x, g, mean)
> vs <- tapply(x, g, var)
> 9/99*(sum(vs) + 10*var(mns))
[1] 1.033749
> var(x)
[1] 1.033749

If the sample sizes are not equal, the formula is not so nice.
EDIT: formula for unequal sample sizes
If there are $g$ sub-samples, each with $k_j, j=1,\ldots,g$ elements for a total of $n=\sum{k_j}$ values, then
$$ Var(X_1,\ldots,X_{n}) = \frac{1}{n-1}\left(\sum_{j=1}^g (k_j-1) V_j + \sum_{j=1}^g k_j (\bar{X}_j - \bar{X})^2\right), $$
where $\bar{X} = (\sum_{j=1}^gk_j\bar{X}_j)/n$ is the weighted average of all the means (and equals to the mean of all values).
Again, a demonstration:
> k <- rpois(10, lambda=10)
> n <- sum(k)
> g <- factor(rep(1:10, k))
> x <- rnorm(n)
> mns <- tapply(x, g, mean)
> vs <- tapply(x, g, var)
> 1/(n-1)*(sum((k-1)*vs) + sum(k*(mns-weighted.mean(mns,k))^2))
[1] 1.108966
> var(x)
[1] 1.108966

By the way, these formulas are easy to derive by writing the desired variance as the scaled sum of $(X_{ji}-\bar{X})^2$, then introducing $\bar{X}_j$:  $[(X_{ji}-\bar{X}_j)-(\bar{X}_j-\bar{X})]^2$, using the square of difference formula, and simplifying.
Comment: I can't +1 this enough, super helpful formula for parallel computation in a data warehouse environment
Comment: thanks. Unfortunately, I can't guarantee that my partitions are all the same size. I'm running a massively parallel process where I need to calculate the variances of each partition in parallel then combine in the end, but the results/samples from each parallel process are not equal (it's a Monte Carlo simulation of received photons).
Answer: This is simply an add-on to the answer of aniko with a rough sketch of the derivation and some python code, so all credits go to aniko.
derivation
Let $X_j \in X = \{X_1, X_2, \ldots, X_g\}$ be one of $g$ parts of the data where the number of elements in each part is $k_j = |X_j|$. We define the mean and the variance of each part to be
$$\begin{align*}
  E_j & = \mathrm{E}\left[X_j\right] = \frac{1}{k_j} \sum_{i=1}^{k_j} X_{ji}\\
  V_j & = \mathrm{Var}\left[X_j\right] = \frac{1}{k_j-1} \sum_{i=1}^{k_j} (X_{ji} - E_j)^2
\end{align*}$$
respectively. If we set $n = \sum_{j=1}^g k_j$, the variance of the total dataset is given by:
$$\begin{align*}
  \mathrm{Var}\left[X\right] & = \frac{1}{n-1} \sum_{j=1}^{g} \sum_{i=1}^{k_j} (X_{ji} - \mathrm{E}\left[X\right])^2 \\
  & = \frac{1}{n-1} \sum_{j=1}^{g} \sum_{i=1}^{k_j} \big((X_{ji} - E_j) - (\mathrm{E}\left[X\right] - E_j)\big)^2 \\
  & = \frac{1}{n-1} \sum_{j=1}^{g} \sum_{i=1}^{k_j} (X_{ji} - E_j)^2 - 2(X_{ji} - E_j)(\mathrm{E}\left[X\right] - E_j)
 + (\mathrm{E}\left[X\right] - E_j)^2 \\
  & = \frac{1}{n-1} \sum_{j=1}^{g} (k_j - 1) V_j + k_j (\mathrm{E}\left[X\right] - E_j)^2.
\end{align*}$$
If we have the same size $k$ for each part, i.e. $\forall j: k_j = k$, above formula simplifies to
$$\begin{align*}
  \mathrm{Var}\left[X\right] & = \frac{1}{n-1} \sum_{j=1}^g (k-1) V_j + k(g-1) \mathrm{Var}\left[E_j\right] \\
  & = \frac{k-1}{n-1} \sum_{j=1}^g V_j + \frac{k(g-1)}{k-1} \mathrm{Var}\left[E_j\right]
\end{align*}$$
python code
The following python function works for arrays that have been splitted along the first dimension and implements the "more complex" formula for differently sized parts.
import numpy as np

def combine(averages, variances, counts, size=None):
    """
    Combine averages and variances to one single average and variance.

    # Arguments
        averages: List of averages for each part.
        variances: List of variances for each part.
        counts: List of number of elements in each part.
        size: Total number of elements in all of the parts.
    # Returns
        average: Average over all parts.
        variance: Variance over all parts.
    """
    average = np.average(averages, weights=counts)

    # necessary for correct variance in case of multidimensional arrays
    if size is not None:
        counts = counts * size // np.sum(counts, dtype='int')

    squares = (counts - 1) * variances + counts * (averages - average)**2
    return average, np.sum(squares) / (size - 1)

It can be used as follows:
# sizes k_j and n
ks = np.random.poisson(10, 10)
n = np.sum(ks)

# create data
x = np.random.randn(n, 20)
parts = np.split(x, np.cumsum(ks[:-1]))

# compute statistics on parts
ms = [np.mean(p) for p in parts]
vs = [np.var(p, ddof=1) for p in parts]

# combine and compare
combined = combine(ms, vs, ks, x.size)
numpied = np.mean(x), np.var(x, ddof=1)
distance = np.abs(np.array(combined) - np.array(numpied))
print('combined --- mean:{: .9f} - var:{: .9f}'.format(*combined))
print('numpied  --- mean:{: .9f} - var:{: .9f}'.format(*numpied))
print('distance --- mean:{: .5e} - var:{: .5e}'.format(*distance))

Comment: I believe you need a pair of brackets in your final equation (same size k) after you extract a factor of $k-1$.
Comment: @DzamoNorton I am afraid I do not understand where you want additional brackets
Comment: I've illustrated it here http://mathb.in/45578.  You can also see said brackets in the same-size-k formula in the other answer to this question.
Comment: But I also see now I'd lost track of the scope of the summand.  Now that I see the summand must run to the end of the line, the brackets are indeed unnecessary.  Apologies!
Answer: Many thanks to those who answered and commented!  Your answers helped me with a problem where I wanted long-term retention of descriptive statistics, but was not able to retain the original data.
My solution was to create rollup records that would have one row for each combination of desired dimensions of the data (one of which might be time). Depending on the cardinality of the dimensions, this can be a huge space savings.  In this example, we have 100 rollup records for 1 million measurements, since the cardinality of each dimension is 10.
I thought I'd demonstrate this method in SQL (PostgreSQL), and perhaps others might find this useful. This is my first answer, so do be kind. I'm not sampling the data, so I use the population formulas that use the divisor of N.
First, we create some measurement records, then roll them up into summary rollup records.  All we store is sum, count, and population variance for each combination.
stats=> \timing
Timing is on.
stats=> SELECT setseed(.5);
 setseed
---------

(1 row)

Time: 0.580 ms
stats=> drop table if exists measurements ;
NOTICE:  table "measurements" does not exist, skipping
DROP TABLE
Time: 0.450 ms
stats=> SELECT setseed(.5);
 setseed
---------

(1 row)

Time: 0.497 ms
stats=> create table measurements as
 SELECT generate_series(1,1000000) AS id,
  floor(random() *10) AS dimension1,
  floor(random() *10) AS dimension2,
 random() * 10 AS value;
SELECT 1000000
Time: 776.785 ms
stats=> drop table if exists rollups;
NOTICE:  table "rollups" does not exist, skipping
DROP TABLE
Time: 0.839 ms
stats=> create table rollups as
select dimension1, dimension2, count(1) as measurements,
 sum(value) as value_sum, var_pop(value) as value_var_pop
   from measurements
    group by dimension1, dimension2;
SELECT 100
Time: 880.328 ms

Then we calculate the mean and population standard deviation, first from the rollup records, then the complete dataset. This uses a "Subquery" to calculate the population mean used in the calculation. Note that the query times from summary records are considerably faster and should be acceptable for real-time display.
stats=> SELECT
  SUM (value_sum) / SUM (measurements) mean,
  SQRT (
    (
      SUM (
        ru.value_var_pop * ru.measurements
      ) + SUM (
        ru.measurements * (
          (ru.value_sum / ru.measurements) /* row avg */
          - (                              /* grand avg subquery */
            SELECT
              SUM (value_sum) / SUM (measurements) grand_avg
            FROM
              rollups ga
          )
        ) ^ 2
      )
    ) / SUM (ru.measurements)
  ) as stddev_pop
FROM
  rollups ru;
       mean       |    stddev_pop
------------------+------------------
 4.99954057566761 | 2.88618225050068
(1 row)

Time: 1.255 ms

stats=> select avg(value), stddev_pop(value) from measurements;
       avg        |    stddev_pop
------------------+------------------
 4.99954057566761 | 2.88618225050069
(1 row)

Time: 71.719 ms

As a bonus, we calculate mean and standard deviations by "dimension1," first from the rollup records, then the complete dataset.  This uses a "Correlated Subquery" to calculate the means for each dimension1 value.
stats=> SELECT dimension1,
  SUM (value_sum) / SUM (measurements) mean,
  SQRT (
    (
      SUM (
        ru.value_var_pop * ru.measurements
      ) + SUM (
        ru.measurements * (
          (ru.value_sum / ru.measurements) /* row avg */
          - (                              /* grand avg subquery */
            SELECT
              SUM (value_sum) / SUM (measurements) grand_avg
            FROM
              rollups ga
            WHERE ga.dimension1 = ru.dimension1   /* same (correlated) group as outer query  */
          )
        ) ^ 2
      )
    ) / SUM (ru.measurements)
  ) as stddev_pop
FROM
  rollups ru
GROUP BY
dimension1
order by 1 ;
 dimension1 |       mean       |    stddev_pop
------------+------------------+------------------
          0 | 5.00667407841466 | 2.89025062220051
          1 | 4.98845700180817 | 2.88379096643363
          2 | 4.99899669999969 | 2.88825163824503
          3 | 4.98553153843827 | 2.88365611510306
          4 | 4.99844263698516 |  2.8860230080168
          5 |  5.0179497778027 | 2.88749365173117
          6 | 5.00517112927829 | 2.88346174527641
          7 | 5.00532821601654 | 2.89266358278946
          8 | 4.98688468436205 | 2.88383465545076
          9 | 5.00219626955073 | 2.88222690226276
(10 rows)

Time: 3.828 ms
stats=> select dimension1, avg(value), stddev_pop(value) from measurements group by dimension1 order by 1;
 dimension1 |       avg        |    stddev_pop
------------+------------------+------------------
          0 | 5.00667407841466 | 2.89025062220053
          1 | 4.98845700180817 | 2.88379096643364
          2 | 4.99899669999969 | 2.88825163824505
          3 | 4.98553153843827 | 2.88365611510306
          4 | 4.99844263698516 | 2.88602300801678
          5 |  5.0179497778027 | 2.88749365173116
          6 | 5.00517112927829 |  2.8834617452764
          7 | 5.00532821601654 | 2.89266358278946
          8 | 4.98688468436205 | 2.88383465545078
          9 | 5.00219626955073 | 2.88222690226277
(10 rows)

Time: 113.342 ms


Dedekind's "different": sources, definition, original name

Question: I am interested in getting the original information regarding Dedekind's idea of the "different" (regarding ideals). Particularly, I am interested in:
1- Knowing the original German name he used for it. I think it was "diskriminant", but I am not sure.
2-Getting his original definition (It would be great if the page could be scanned and bibliographic reference could be given).
Thanks."
Answer: See :

Nicolas Bourbaki, Elements of the History of Mathematics, (French ed, 1984), page 102 :

in 1882, Dedekind completes the theory [of ideals] by introducing the different, which gives him a new definition of the discriminant and allows him to specify the exponents of the prime ideal factors in the decomposition of the latter.

In the German text of : Richard Dedekind, Über die Discriminanten endlicher Körper, 1882, Abhandlungen der Königlichen Gesellschaft der Wissenschaften zu Göttingen 29 (2): 1–56, see page 1 for : "Grundzahl oder Discriminante" and page 38 for : "Grundideal des Körpers".
Comment: No, in this 1882 paper, Dedekind calls the discriminant "Discriminante" or "Grundzahl". What we would now call the different (or different ideal) he calls "Grundideal", see p.38. He does not seem to give a name to what we would now call the different of an algebraic element $\theta$, but simply denotes it by $\theta^*$ (defined on p.4). Dedekind would never have used "Discriminante" for the different, because discrimants were well established at that point and are different from 'differents'.
Comment: It is still not clear to me whether the original term fuer the different was discriminant from the very beginning......
Comment: My question would then be: Which term does Dedekind use in his 1882 paper to mention what has ben translated as the different (regarding ideals)?
Comment: So das Wort war dis discriminante? Wikipedia refers rather to the 1882 paper Cf. Richard Dedekind,  (1882), "Über die Discriminanten endlicher Körper", Abhandlungen der Königlichen Gesellschaft der Wissenschaften zu Göttingen,  29, 2, 1882, p.  1–56.  Is that a confusion??
Comment: The term "Differente" was used by Hilbert in his Zahlbericht (Section 12), and he refers in a footnote to Dedekind having used the term Grundideal, so I suspect this means the modern term was introduced by Hilbert.  A link to the relevant page in the Zahlbericht is http://www.digizeitschriften.de/dms/img/?PID=PPN37721857X_0004|log73&physid=phys226#navi (see also the previous page).


To prove $a+b+c \ge ab+bc+ca$ when $abc=1$

Question: The actual question is not in the heading, it's this-

Let $a, b, c$ be positive real numbers such that $abc = 1$. Prove that
$$ {a \over \sqrt{7+b+c}} + {b \over \sqrt{7+c+a}} + {c \over \sqrt{7+a+b}} \geq 1$$

To complete a proof of this inequality after a deft application of Hölder's Inequality, it's enough to prove that
$$\tag{1} (a+b+c)^3\geq7(a+b+c)+2(ab+bc+ca)$$
and it turns to
$$ \tag{2} (a+b+c)^3\geq7(a+b+c)+{2\over 3}(a+b+c)^2 $$ as $ a+b+c\geq3 $.
According to my book, this solution is provided.
But I am not able to understand how ($1$) $\Rightarrow$ ($2$). Can someone explain me?
For this to happen, $a+b+c\geq ab+bc+ca$.
Thanks!
Comment: The statement in your title is not true: although $2 \times 2 \times 1/4 = 1$, we have $2 + 2 + 1/4 \neq 4 + 1/2 + 1/2$.
Comment: @MeesdeVries I've edited it. See now.
Comment: The statement in your title is still not true, because the $\neq$ in my previous comment is actually a $<$.
Comment: As $a^2+b^2+c^2 \ge ab+bc+ca$, (1) to (2) is fine.
Comment: @Book Of Flames There are another ways to prove your inequality.
Answer: We need to prove
$$\tag{1} (a+b+c)^3\geq7(a+b+c)+2(ab+bc+ca).$$
But from know inequality $3(ab+bc+ca) \leqslant (a+b+c)^2.$ Therefore the proof is completed if
$$\tag{2} (a+b+c)^3\geq7(a+b+c)+\frac23(a+b+c)^2.$$
Let $x = a+b+c \geqslant 3\sqrt[3]{abc} = 3,$ ienquality $(2)$ become
$$x^3 \geqslant 7x+\frac{2x^2}{3},$$
or
$$\frac{x(3x+7)(x-3)}{3} \geqslant 0.$$
Which is true.
Note. Must be $(2) \Rightarrow (1) $


Prove that if G is a graph with no even cycles, then every cycle in G is an induced subgraph.

Question: I tried using the contrapositive to prove the original statement:
If no cycle in G is an induced subgraph, then G is a graph with no odd cycles.
To prove this, I assumed that G did have an odd cycle that was not an induced subgraph. However, if the odd cycle is not an induced subgraph, then there is an edge connecting two vertices in the "cycle" that shortens the cycle, in a sense, by bypassing some vertices and edges, which creates a new cycle. If this cycle is even, then there is a contradiction, since G was assumed to have no even cycles. If this cycle is odd, then there is a contradiction, since it is an induced subgraph of G.
Am I thinking about this in the right way?
Comment: The negation of "no even cycle" is NOT "no odd cycles".
Comment: The negation of "every cycle is odd" is "some cycles are even".
Comment: But doesn't "no even cycles mean" that "every cycle is odd", so negating that gives "every cycle is even" which is equivalent to saying "no cycles are odd"? @N.S.
Comment: The negation of "there is no even cycle" is "there is *a* even cycle", and it does not assert the existence of odd cycle.
Comment: @user227157, the problem is that you're assuming that the graph has cycles in the first place.
Comment: The negation of "every cycle in G is an induced subgraph" is not "no cycle in G is an induced subgraph".
Answer: To add to the comment above, your contrapositive hypothesis is also not correct. The correct contrapositive statement is:
"If there exists a cycle in $G$ that does not yield an induced subgraph, then $G$ has an even cycle."
Now the contrapositive approach in itself seems fine. Consider such a cycle $C$; if the cycle has an even number of vertices, we're done. So suppose it has an odd number of vertices. Then the fact that it does not yield an induced subgraph means that there exists an additional edge between two of the vertices in $C$. Can you see why the addition of such an edge must mandate the creation of an even cycle?
Comment: That's really helpful! Yeah I think I understand, since that edge that connects two of the vertices in C will always "chop" the cycle in 2 parts, which add up to give an odd number of vertices, so one of the parts must be even. Is that right?
Comment: That's correct :)


Analyze Thread Dump fow web application

Question: Hello i have taken the thread dump for my web application which is giving out of memory again and again using the JSTACK but i am little bit confused that how to find the correct culprit thread can some one give the tips that how to analyze the dump file.
Comment: A heap dump would make more sense for out of memory errors.
Comment: how to take this heap dump in production environment for windows server
Comment: http://stackoverflow.com/questions/407612/how-to-get-a-thread-and-heap-dump-of-a-java-process-on-windows-thats-not-runnin
Answer: Use VisualVM which is included with the JDK.
Answer: When you have a OutOfMemoryError, the first step is to read the associated message. It explains the cause of the error : heap, perm, thread,...
Depending on the cause, you have to check the configuration of the space : -Xms and -Xmx for the heap, -XX:PermSize and -XX:MaxPermSize for the perm (Java 7-), -XX:MaxMetaspaceSize for the metaspace (Java 8+),... The config may be too low for your need.
After that, use tools to understand how the memory is consumed. VisualVM is great, it provides metrics on the memory, helps you to make heap dumps or to profile the memory (not in production). You may add the -XX:+HeapDumpOnOutOfMemoryError option in your startup script so that the heap dump will be automatically generated when you run an OutOfMemoryError.
If you have memory leaks, I suggest to use more advanced (and not free) profiling tools such as JProfiler or YourKit.
Comment: If i am getting the OutOfMemory because of the thread i.e. i am getting the message unable to create the native thread then can you suggest me how to move forward.
Comment: I guess that you're on a 32bits OS. The size of a process has a limite between 2 and 3 or 4 GB, depending on the OS. Each thread uses a amount of memory, set by -Xss. When the app starts a new thread and the limit is reached, a OutOfMemoryError is thrown. you seem to be in this case. The solutions : 1) move to a 64bits OS; 2) decrease the Xss value (try -Xss256k or -Xss128k); 3) decrease the -Xms and/or -XX:MaxPermSize values.
Comment: thanks for the answer but i am using 64bit not 32 bit OS actually i moved my application to a complete new server from the old server after that the problem started might be the problem related to the window job scheduler.
Comment: by the way it is a standalone program using quartz to schedule the jobs.
Comment: OK. You should then check the number of threads in your VM. You may have reach a system limit on this side.
Comment: sir is this is limiting from the OS side if so how can i check that configuration because from my application side i am not giving the number of thread limit by default it will be 200 only in server.xml.
Comment: In server.xml, you setup the number of threads associated to the Tomcat connectors, not the overall number. Quartz has its own threads.

Analyze Thread Dump fow web application


touchstart vs touchbegin events

Question: What is the right choice for targeting the touch events on mobile devices?
There's no a single word about touchbegin at https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Touch_events, while I've seen many plugins that use touchbegin event instead of touchstart.
What is the difference between them?
Answer: touchstart:
When a touchstart event occurs, indicating that a new touch on the surface has occurred.

This is (touchstart, touchend, etc.) are available at the front end and you can control it via javascript/jQuery.

touchbegin:
Where i got it is an event in the objective-c and you can't track/controll it on frontend.
Answer: There is no touchbegin event native in JavaScript. If plugins are using touchbegin they must be defining it themselves.
touchstart on the other hand is native to JavaScript, and if you're wanting to track touch events this is what you'd use.

touchstart vs touchbegin events


Proof: $U$ is an ultrafilter of a Boolean algebra $B$ if and only if for all $x$ in $U$ exactly one of $x$,$x^*$ belongs to $U$.

Question: I have been stuck with this problem for a while now. I have a proof that letting $U$ be an ultrafilter, exactly one of $x,x^*$ belongs to $U$ for all $x$ in $B$, I did this by showing that both belong to $U$ implies that $U=B$ which cannot be the case, and if neither long to $U$ then a contradiction can be derived by de-Morgan's laws. However, I'm stuck with the reverse implication, I need to prove that:
"If $U$ is a subset of a Boolean algebra $B$ such that $\forall x \in B$, exactly one of $x \in U$ and $x^* \in U$ is true, U is an ultrafilter.".
Is this even the case? Any help would be greatly appreciated, thanks in advance.
Comment: In general, such a subst will not even be a filter ...
Comment: What do you denote by $x^*$? $\neg x$?
Comment: Indeed, my mistake.
Comment: For a given ultrafilter $u\subset B$, define the set $u^\ast = \{ 1-a: a \in u\}$ and consider the two questions: "Does this set meet the required hypothesis?" and "Is u^\ast a filter?"
Answer: A correct statement would be

If $U\subseteq B$ is a filter and for all $x\in B$ exactly one of $x\in U$ or $x^*\in U$ is true, then $U$ is an ultrafilter.

The converse holds as well, and this is sometimes taken as the definition of an ultrafilter. I presume the one you've been given is that an ultrafilter is a maximal (proper) filter.
To see that the statement holds, note first that the fact that both $x$ and $x^*$ aren't in $U$ implies $U$ is a proper filter. Assume that $F$ is a filter extending $U$ and let $x\in F\setminus U.$ Then $x^*\in U,$ so $x^*\in F$ and since both $x$ and $x^*$ are in $F,$ $F$ is improper.
Without the provision that $U$ is a filter, the statement becomes very badly false. Just randomly choose elements of a Boolean algebra, randomly label with zero or one (indicators as to whether the element is inside or outside $U$), and assign the complement the opposite label. Odds are you hit a counterexample. (To guarantee you do so, just make sure you start out labeling the $0$ element with a one and the $1$ element with a zero).
Comment: Although it's easy to get counterexamples, it's not quite as easy as you made it sound. You need to arrange for each complementary pair $x$ and $\neg x$ to get opposite values. So randomly pick one element from each complementary pair, label the chosen elements $1$ and the unchosen ones  $0$. As you said, you have a good chance of getting a counterexample, and you can  guarantee a counterexample by choosing $0$ from the pair $\{0,1\}$. (Also, if you have bad luck and get an ultrafilter, just take its complement to get a counterexample.)
Comment: @AndreasBlass Yes, I put that incorrectly, thank you for the correction!
Comment: Thank you very much! This was very helpful


Aparecer campo de input dependendo da seleção do botão radio

Question: Em um formulário, vai ter a opção sim ou não, se sim o usuário vai descrever
$(document).ready(function() {
  $('#descreva').hide();
  $('#def').change(function() {
    if ($('#sim') checked) {
        if( $("#sim").is(":checked") == true)
        $('#descreva').show();
    } else {
      $('#descreva').hide();
    }
  });
});

            Possui treinamento:

            <radio id="def">

              <input type="radio" name="devweb" id="sim" value="sim">
              SIM

              <input type="radio" name="devweb" id="nao" value="nao">
              NÃO

            <input type="text" name="descreva" id="descreva">

Comment: `if ($('#sim') checked) {` esse if não faz sentido, falta um operador lógico.. de qualquer forma, um radio é um elemento que tem multiplos valores, por isso pode validar o valor direto no lugar de "checked", assim: `if ($('input:radio[name="devweb"]').val() == "sim")`
Answer: Algo simples de fazer isso é usando javascript puro mesmo:
window.onload = () => {
   var input = document.getElementById('descreva');
   var radio_yes = document.getElementById('sim');
   var radio_no = document.getElementById('nao');
    radio_yes.addEventListener('click', (e) => {
        var radio = e.currentTarget;
        if(radio.checked == true) {
          input.disabled=false;
          input.focus();
        } else {
         input.disabled=true;
          input.value='';
        }
    });

     radio_no.addEventListener('click', (e) => {
        var radio = e.currentTarget;
        if(radio.checked == true) {
          input.value='';
          input.disabled=true;
           input.focus();
        } else {
         input.disabled=false;
        }
    });
}

Segue modelo em fiddle
Answer: Precisa estudar mais o html e javascript, pois tem muitos problemas simples no código:

<radio id="def"> não existe um elemento "radio", fora que, do jeito que foi definido ainda ia faltar o fechamento dessa tag
if ($('#sim') checked)  esse if não sentido, o $('#sim') vai obter o elemento com id="sim", a partir o máximo que poderia fazer era por exemplo fazer == null ou != null, etc, porque é um seletor para o elemento, para validar se está "checked", deveria fazer por exemplo $('#sim').is(':checked').
um radio buttom tem multiplos valores, dai a ultilidade dele em poder "escolher um entre vários valores", e isso acontece porque as opinções tem o mesmo "name", por isso uma forma mais fácil de selecionar é usando: $('input:radio[name="devweb"]').
Com isso, pode assiciar o evento change a todos os radio com o mesmo name, e ainda pode usar pera pegar qual valor foi selecionado, por exemplo $('input:radio[name="devweb"]').val().

Veja abaixo um exemplo de como usar isso:

$(document).ready(function() {
  $('#descreva').hide();
  // aqui um seletor por name, para pegar todos os radio buttons "devweb"
  $('input:radio[name="devweb"]').change(function() {
    // aqui, this é o radio quem foi clicado, então basta comparar o valor com val()
    if ($(this).val() == "sim") {
        $('#descreva').show();
    } else {
      $('#descreva').hide();
    }
  });
});

    Possui treinamento:

      <input type="radio" name="devweb" id="sim" value="sim">
      SIM

      <input type="radio" name="devweb" id="nao" value="nao">
      NÃO

    <input type="text" name="descreva" id="descreva">


If f plus the integral of f has a finite limit, show that f tends to zero

Question: This is a problem from Pugh's Real Mathematical Analysis.

Let $f$ be a continuous real-valued function on $[0, \infty)$ such that $$\lim_{x \to \infty} \left( f(x) + \int_0^x f(t) \, dt \right)$$ exists (and is finite). Prove that $\lim_{x \to \infty} f(x) = 0$.

I found one solution to this problem by defining
\begin{gather}
g(x) = f(x) + \int_0^x f(t) \, dt \\
F(x) = \int_0^x f(t) \, dt
\end{gather}
and writing out the explicit solution to the ODE $F' + F = g$, then showing that $f = F' \to 0$ if $g \to L$, for some limit $L$. This solution is messy and involves showing that several terms that collectively bound $|f|$ can all be made arbitrarily small.
My gut feeling is that a nicer solution exists. Am I right?
Comment: http://math.stackexchange.com/questions/698546/does-this-condition-on-the-sum-of-a-function-and-its-integral-imply-that-the-fun/698649#698649 see if this helps
Answer: Let $ L = \lim_{x\to\infty} \left( f(x) + \int_{0}^{x} f(t) \, dt \right) $ denote the limit. We easily check that
$$ f(x) + \int_{0}^{x} f(t) \, dt = \frac{\frac{d}{dx} \left( e^{x} \int_{0}^{x} f(t) \, dt \right) }{\frac{d}{dx} e^{x}}. $$
Since $e^{x} \to \infty$ as $x \to \infty$, it satisfies the condition of L'hospital's rule and hence
$$ \lim_{x\to\infty} \int_{0}^{x} f(t) \, dt = \lim_{x\to\infty} \frac{e^{x} \int_{0}^{x} f(t) \, dt}{e^{x}} = L$$
Therefore
$$\lim_{x\to\infty} f(x) = \lim_{x\to\infty} \left( f(x) + \int_{0}^{x} f(t) \, dt - \int_{0}^{x} f(t) \, dt \right) = L - L = 0. $$
Comment: @BrianBi, That is quite often included when stating L'hospital's rule, but it is actually redundant if you see the proof. Saying it differently, the condition $f(x) \to \pm \infty$ is sort of automatic if you have $g(x) \to \infty$ and $f'(x)/g'(x) \to L (\neq 0)$.
Comment: For l'Hopital's rule don't we need the condition that $e^x \int_0^x f(t) \, dt \to \infty$?
Answer: Let $L = \lim_{x \to \infty} f(x)+\int_0^x f(t)dt$.
Let $\phi(x) = e^x \int_0^x f(t)dt$, note that $\phi$ is differentiable, $\phi(0) = 0$ and $\lim_{x \to \infty} e^{-x}\phi'(x) = L$.
Let $\epsilon>0$, then for some $x_0$ if $x \ge x_0$ we have $|e^{-x}\phi'(x)-L| < \epsilon$, and so $|\phi'(x)-e^{x}L| < e^x \epsilon$. Integrating over $[x_0,x]$ gives
$| \phi(x)-\phi(x_0)-(e^x-e^{x_0})L| < (e^x-e^{x_0}) \epsilon$, and multiplying by $e^{-x}$ gives $| e^{-x}\phi(x)-e^{-x}\phi(x_0)-(1-e^{x_0-x})L| < (1-e^{x_0-x}) \epsilon$. Letting $x \to \infty$ shows that $\limsup_{x \to \infty} | e^{-x}\phi(x) -L| \le \epsilon$. Since $\epsilon>0$ was arbitrary, we have $\lim_{x \to \infty} e^{-x}\phi(x) = L$.
Consequently we see that $\int_0^x f(t)dt \to L$, from which we conclude that $f(x) \to 0$.


Expectation of Multivariate sample variance

Question: Given $\{X_i\}$ a sample i.i.d. observations of a $J$-dimensional random variable with $E(X_i)=\mu\in\mathbb{R}^J,V(X_i)=\Sigma\in\mathbb{R}^{J\times J}$.
Is it true that:
$$
E\left[(X_i-\bar{X}_n)(X_i-\bar{X}_n)'\right]=\Sigma\equiv E\left[(X_i-\mu)(X_i-\mu)'\right]
$$
Is there a compact proof for this in the case it's true?
Thanks for your help! :D
Comment: I think you still need  the correction($\frac{1}{n-1}$) as in univariate case  to get the unbiased estimation.
Comment: @DeepNorth is correct, because this *is* the univariate case--repeated $J\times J$ times.
Answer: No.
As a very simple counter example, consider the univariate case with a single sample. Then the left side of your equation reduces down to
$E[(x - x)^2] = 0$
The right side of your equation would then reduce down to
$E[(x - \mu)^2] = \sigma^2$
Comment: @whuber: to help clarify, I added the note of a single sample
Comment: I believe you mean "one-sample" rather than univariate.
Comment: @whuber: actually, I do mean univariate, as in $J = 1$. But how the OP stated the problem implies one sample, as I understand it.
Comment: I think most people would understand the presence of an index on "$X$", as written in the question, as indicating a sample of more than one.


Is there a name for functions that are their own inverses

Question: There are terms for various kinds of functions (or operators) such as...
associative - (a # b) # c  =  a # (b # c)
commutative - a # b  =  b # a
idempotent  - f (f (x)) = f (x)

Is there a similar name for functions that are their own inverse?
f (f (x)) = x

Answer: It is called as an involution.
Comment: Some examples of involutions are shown [here](http://eqworld.ipmnet.ru/en/education/wiener.pdf)
Comment: Thanks - have to wait a few minutes before I can accept, but this is the answer.
Comment: The most trivial involution is f(x) = x.
Answer: Such a function is called an involution.


extract lines when 4th column has a value within a number range

Question: I would like to extract all the lines in a file that contain in the 4th column a value within a range (I have to do it several times, with different ranges). I'm trying the following command with awk, but it's not working:
awk '$4 ~ /[250898-251953]/ {print $0}' myfile.txt > myfile2.txt

Comment: Could you please post sample of input and expected output in code tags and let us know then.
Answer: A regex character class accepts a range of characters, not numbers. [1-35] matches a single character which is between 1 and 3 (so, 1 or 2 or 3) or 5.
You want:
awk '$4 >= 250898 && $4 <= 251953' myfile.txt >myfile2.txt

{ print $0 } is the default action so you don't need to spell that out explicitly.
Incidentally, a single awk script can handle multiple ranges in one go.
awk '$4 >= 12 && $4 <= 34 { print >"range1" }
    $4 >= 56 && $4 <= 78 { print >"range2" }
    # etc etc' myfile.txt

This breaks down if the number of ranges is bigger than the number of file handles your OS allows you to keep open concurrently. You can write a more complex awk script, or if it's a one-off or at least a rare task, just break the task into a number of small awk scripts which process some 20 ranges each (which is a typical limit for how many open file handles you can have; but consult your local OS documentation to verify the precise limit on your platform).
Answer: awk '$4 >=250898 && $4 <= 251953{print $0}' file

or
awk '$4 >=250898 && $4 <= 251953' file

Note that [0-9] means digits from 0 to 9, whereas [0-19] means digits from 0 to 1 and also 9. (Not 19)


Bound for $fg\in L^{1}$ in terms of $f,g \in L^{1} $

Question: Let's assume that $fg\in L^{1}$ with $f\in L^{1}$ and $g\in L^{1}$. Is there a common way to bound $\Arrowvert fg \Arrowvert_{1}$ in terms of the norms  $\Arrowvert f \Arrowvert_{1}$  and $\Arrowvert g \Arrowvert_{1}$? Are there any examples where such bound doesn't exist?
Thank you.
Answer: Consider the unit interval $[0,1]$ and $$f_\epsilon(x) = \frac 1 {\sqrt x} \chi_E = g_{\epsilon}(x)$$
where $E = [\epsilon, 1]$ and $\chi_E$ is a characteristic function. Then $f_{\epsilon}g_{\epsilon} \in L^1$, and we have
$$\|f_{\epsilon}\|_1 = \int_{\epsilon}^1 \frac{1}{\sqrt x} dm(x) \le \int_0^1 \frac{1}{\sqrt x} dm(x) = 1$$
and likewise for $g_{\epsilon}$. However,
$$\|f_{\epsilon}g_{\epsilon}\|_1 = \int_{\epsilon}^1 \frac 1 x dm(x) = -\ln \epsilon \to \infty$$
as $\epsilon \to 0$. So no uniform bound can exist.
Comment: Thank you. There are a couple of things I don't understand from your answer. 1.- What is dm(x)? Is it the measure? 2.- In your example $fg\notin L^{1}$, that is negating the hypothesis. What am I missing?
Comment: @yess Sorry, I misunderstood the question and have fixed it now. $dm(x)$ stands for Lebesgue measure and is the same as $dx$.


Interpretation of quantitative variable in regressions with and without dummy variables

Question: I was provided with results from two regressions:
(1)
log(annual_salary) = B1*yrs_experience + B2*PhD + B3*Masters + B4*Bachelors + e
(2)
log(annual_salary) = B1*yrs_experience + e
where 'annual_salary' and 'yrs_of experience' are quantitative variables, and 'Phd', 'Masters' and 'Bachelors' are qualitative variables taking on values 1 or 0 when appropriate.
Should/will the coefficient on 'yrs_experience' be the same in both equations? And, what is the interpretation of the coefficient in both equations?
Note that the intercept is omitted in both cases. In (1) because the researchers are interested in all groups alone, not in their relation to a base group. This seems fine. In (2), however, I am not convinced that the researchers are doing the right thing in removing the intercept based on the discussion here.
Comment: Second one mean people with 0 yrs_experience will get the annual_salary $1. If this is acceptable, the model is acceptable. if not, need to add intercept in model.
Answer: The first regression model is in effect a collection of 3 sub-models, with each sub-model being applicable to a different type of degree holder.
Sub-model (1a): PhD degree holders
log(annual_salary) = B2 + B1*yrs_experience + e

Sub-model (1b): Masters degree holders
log(annual_salary) = B3 + B1*yrs_experience + e

Sub-model (1c): Bachelors degree holders
log(annual_salary) = B4 + B1*yrs_experience + e

The sub-models assume that each type of degree holder starts out with a different baseline log(annual_salary) but after that the log(annual_salary) increases at the same rate for each additional year of experience.
The coefficient B1 in each sub-model represents the amount by which the log(annual_salary) changes, on average, for each year of additional experience among people holding the same degree. (The change will most likely be an increase.)
The second model looks at the relationship between log(annual_salary) and yrs_experience, regardless of (or ignoring) the degree type.
The coefficient B1 in the second model represents the amount by which the log(annual_salary) changes, on average, for each year of additional experience regardless of (or ignoring) the type of degree.
As pointed out by a_statistician, the second model implies that if someone - regardless of their degree - has zero years of experience (i.e., no experience), then their predicted log(annual_salary) would be equal to 0, which is to say that their predicted annual_salary would be equal to 1 (the units of 1 would be the same as the units of the data). This implication may be either too strong or nonsensical altogeher, so I concur with you that the second model desperately needs an intercept.
There could be a third model considered, which allows the rate of change in the log(annual_salary) as a function of yrs_experience to be different for different degree holders.


Expressing these sentences as conditional probability

Question: This question is a linguistic question a bit. I actually understand Bayes theorem from the mathematical point but I am always a bit confused when I encounter these kinds of sentences:
Person P is correct 70% of time. Person P says it will rain.

Now if I decide that:
R ... it will rain.
A ... P says it will rain.

It is hard for me to make some intuitive reasoning whether to say:
P(A|R) = 0.7

or
P(R|A) = 0.7

It just seems to me that both statements express the situation. Can you give me some intuitive explanation why the second statement is wrong?
Answer: The first one says
"The probability P says it will rain GIVEN that it will rain is 0.7"
If it will rain, there's a 70% chance P will predict it. What's the chance that P predicts rain when it does rain? If it was 1, P would almost never incorrectly fail to predict rain that does occur. If it was 0, P would almost never predict the rain when it occurs.
The second one says
"The probability that it will rain GIVEN that P says it will rain is 0.7"
If P predicts it will rain, there's a 70% chance it will rain. If this one is near 1, if P says it will rain, it almost certainly will. If this is near 0, when P predicts rain, it almost certainly will not rain.
They are not quite the same, because the first one shows how unlikely it is for P to fail to make the prediction when he/she should, while the second one shows how unlikely it is for P to make a bad prediction.
In terms of the initial question, it could maybe use some more detail, but I think it means that 70% of P's affirmative predictions are correct (ignoring the failure to predict what does happen). Interpreted this way, I suspect we want P(R|A) = 0.7 -- if P predicts rain, 70% of the time it does rain -- 70% of P's predictions are correct.
Comment: I actually understand the meaning of the conditional probabilities written. I am just not so sure which one to pick when the sentence is as vague as "Person P is correct 70% of time" because they both seem quite reasonable. Might be that it is just ambiguous?
Comment: I see, see the last paragraph in the edited version - I suspect this is the intention of the question, but I see why it may seem ambiguous.

What it's doing is ignoring Type II errors (false negatives): http://en.wikipedia.org/wiki/Type_I_and_type_II_errors#False_negative_error