Possible timing issues when using setActiveConnection to an external database under normal Drupal 8 operation

Question: I'm using Drupal 8.0.5. In my settings.php file I have defined an external database:
$databases['notificationsdb']['default'] = array (
  'database' => 'NotificationsDB',
  'username' => 'my_user_name',
  'password' => 'my_password',
  'prefix' => '',
  'host' => '127.0.0.1',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

I have then created a custom form where I need values from the NotificationsDB database to populate a form fields. In my custom form class in the __constructor I created an instance of my Notification Client class:
public function __construct() {
        $this->_notificationClient = new NotificationsClient();
}

In my NotificationsClient class I have the following code:
public function __construct() {
        // Set the active database to notificationsdb
        \Drupal\Core\Database\Database::setActiveConnection('notificationsdb');
        $this->_db = \Drupal\Core\Database\Database::getConnection();
    }
public function __destruct() {
        // Set the active database back to the default
        \Drupal\Core\Database\Database::setActiveConnection('default');
        // https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Database.php/function/Database%3A%3AsetActiveConnection/8
}

When I go to view my form I get the following error:
Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'notificationsdb.shortcut_set_users' doesn't exist: SELECT ssu.set_name AS set_name FROM {shortcut_set_users} ssu WHERE (ssu.uid = :db_condition_placeholder_0); Array ( [:db_condition_placeholder_0] => 1 ) in Drupal\shortcut\ShortcutSetStorage->getAssignedToUser() (line 103 of core/modules/shortcut/src/ShortcutSetStorage.php).
From what I can tell the Shortcuts module is trying to run some queries against the NotificationsDB. I'm guessing this is a timing issue. I'm also concerned with how Drupal handles multiple database connections.
Answer: In looking at the getConnections method closer you can pass it the $target and $key parameters directly and it will return a handle to the specified database.
$this->_db = \Drupal\Core\Database\Database::getConnection('default', 'notificationsdb');

This way you don't have to mess around with setting the active database.


Topic modeling (LDA) gives different outputs

Question: I am using Topic Modeling Tool which is based on Mallet and using latent dirichlet allocation (LDA). When I ran the tool multiple times, with the same input (a folder of 200-500 short text files), and set Topics = 10 with the default of 10 words printed per each topic, the generated 10 topics and the words in the output changes.
So am i supposed to reran it multiple times and how can I pick one?
Does it matter which tool of topic modelling I use? I found a few other tools in my search including Stanford Topic Modeling Toolbox, Natural Language Toolkit, etc.
Comment: It's worth noting that LDA is a multi-modal problem, and so one should *expect* different solutions given different starting points. That being said, I'm not sure what the recommended methods for choosing one solution over another in this problem would be.
Comment: You are probably just going to have to read the documentation here. Most likely, there is a random component as a part of the underlying analyses.
Answer: As Cliff notes in comments, the objective function for LDA is non-convex, making it a multimodal problem. That is, you can expect any given run to be locally optimal; you cannot expect that any given run would outperform some other run from different starting points.
To choose between multiple runs, consider this from another paper on variational methods by LDA creators David Blei and Michael Jordan (emphasis mine):

Practical applications of variational methods must address initialization of the variational distribution. While the algorithm yields a bound for any starting values of the variational parameters, poor choices of initialization can lead to local maxima that yield poor bounds. We initialize the variational distribution by incrementally updating the parameters according to a random permutation of the data points. (This can be viewed as a variational version of sequential importance sampling). We run the algorithm multiple times and choose the final parameter settings that give the best bound on the marginal likelihood.

They were writing about Dirichlet process mixtures, but the principle of selecting the run that best predicts the data carries. Also consider selecting based on perplexity of a held-out test set.
Answer: In the c implementation of D. Blei hyperparameters are optimized using an Expectation maximization algorithm that is initialized by n random document seeds.
This could produce slightly different results...try to set the hyperparameters fixed and see whether you get the same results...
Regarding re-running the algorithm several times (with slightly different settings of the hyperparameters):
You can measure the similarity between the topics in the different iterations in order to measure "how much" the topics differ from each others. If in 2 consecutive iterations you find 4 pairs of topics with a good similarity (~90%?) you can consider the topics consistent.
Choosing the right similarity metric is another question....
In principle you can use any similarity (or dissimilarity) function between histograms (Jeffrey divergence?), or be elengant: http://www.machinelearning.org/proceedings/icml2007/papers/105.pdf


Content Security Policy problem with Tailwind CDN on Netlify app hosted

Question: I'm having an issue about CSP rules on my netlify app.
I'm trying to use the CDN from tailwind on my website, and on development it works fine, but when I deploy to netlify, it says the cdn has been blocked by CSP policy.
I already have the CDN URL on the _headers file, but it didin't make any difference at all.
this is how i did the setup my _headers file:
/*
   Strict-Transport-Security: max-age=63072000; includeSubdomains; preload
   Content-Security-Policy: default-src https: 'self' *.netlify.app; connect-src 'self' 'unsafe-inline' https://accounts.google.com/gsi/* https://google.com/pagead/* https://google.com/ccm/* https:// bat.bing.com/* https://google.com/* https://app-cdn.clickup.com/* https://forms.clickup.com/* *.clarity.ms *.googleadservices.com *.bing.com connect.facebook.net *.facebook.net snap.licdn.com *.ads.linkedin.com *.adsymptotic.com https://cdn.tailwindcss.com/cdn.linkedin.oribi.io * .facebook.com analytics.google.com *.google-analytics.com *.analytics.google.com *.googletagmanager.com *.g.doubleclick.net *.google.com *.google.com.br gap: ws :* *.fontawesome.com *.w3.org cdnjs.cloudflare.com *.cloudfront.net *.amazonaws.com *.ampproject.org bat.bing.com; font-src 'self' fonts.gstatic.com cdnjs.cloudflare.com *.cloudflare.com; frame-src https://accounts.google.com/gsi/ *.google.com *.facebook.com *.bing.com *.youtube.com *.instagram.com; img-src 'self' data: blob: *.facebook.com *.clarity.ms *.linkedin.com *.bing.com snap.licdn.com *. ads.linkedin.com *.adsymptotic.com *.amazonaws.com *.w3.org *.cloudfront.net i.ytimg.com *.google-analytics.com *.analytics.google. with *.googletagmanager.com *.g.doubleclick.net *.google.com *.google.com.br; script-src https://accounts.google.com/gsi/client https://bat.bing.com/ https://google.com/pagead https://google.com/ccm https: 'self' ' unsafe-inline' 'unsafe-eval' *.google.com google.com *.cloudfront.net snap.licdn.com analytics.google.com *.bing.com 'unsafe-inline ' https://www.googletagmanager.com; style-src https://accounts.google.com/gsi/style https://cdn.tailwindcss.com/ https: 'self' 'unsafe-inline' frame-ancestors 'self';
   Permissions-Policy: accelerometer=(), autoplay=(), camera=(), encrypted-media=(), fullscreen=(self), geolocation=*, gyroscope=(), magnetometer=(), microphone=() , midi=(), payment=(), picture-in-picture=(), sync-xhr=(), usb=()
   X-Content-Type-Options: nosniff
   X-XSS-Protection: 1; mode=block
   Set-Cookie: HttpOnly; Secure; SameSite=Strict;

And the error is:
Refused to connect to 'https://cdn.tailwindcss.com/' because it violates the following Content Security Policy directive: "connect-src 'self' 'unsafe-inline' ...

Answer: I resolve the issue...
its there no problem with the _headers file and the CSP config. There was a issue with my service-worker, he was caching my csp , consequently, he was not updating when i edit the file.
I fix removing the caching from the service worker.
Comment: As it's currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Answer: In your connect-src directive you have listed "https://cdn.tailwindcss.com/cdn.linkedin.oribi.io" which means that cdn.tailwindcss.com is restricted to the path /cdn.linkedin.oribi.io. It should likely be "https://cdn.tailwind.css.com/ https://cdn.linkedin.oribi.io/", or just the hostnames without scheme and slash.


File is found, but name is not updated

Question: I did this code to check if a given filename is ".jpg" and if yes, change it to ".png":
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>

char newname[2000];

customRename(char *name) {
    int i = strlen(name),
    answer;

    memset(newname, '\0', sizeof(newname));
    strcpy(newname, name);

    if ((name[i-1] == 'g') && (name[i-2] == 'p') && (name[i-3] == 'j') && (name[i-4] == '.')) {
        newname[i-2] = 'n';
        newname[i-3] = 'p';
        answer = rename(name, newname);
        if (answer == -1) {
            fprintf(stderr, "%s -> %s: %s\n", name, newname, strerror(errno));
        } else {
            printf("%i: ", answer);
            puts(newname);
        }
    }
}

void listdir(const char *name, int level)
{
    DIR *dir;
    struct dirent *entry;

    struct stat s;

    if (!(dir = opendir(name)))
        return;
    if (!(entry = readdir(dir)))
        return;

    do {
        if (S_ISDIR(s.st_mode)) {
            char path[1024];
            int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
            path[len] = 0;
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
                continue;
            }
            //printf("%*s[%s]\n", level*2, "", entry->d_name);
            listdir(path, level + 1);
        }
        else {
            //printf("%*s- %s\n", level*2, "", entry->d_name);
            customRename(entry->d_name);
        }
    } while (entry = readdir(dir));
    closedir(dir);
}

int main() {

    printf("Root path:\n");

    char rootpath[2000];
    gets(rootpath);
    listdir(rootpath, 0);

    return 0;
}

But, for some unknown reason it prints:
-1: LF1603300450121ARG.png

The -1 means something was wrong... but what is wrong?
Thanks in advance.
Comment: I'm going to guess the file you are trying to rename is in a subdirectory, and since you only pass the filename you're looking for it in the working directory and not where it actually is.
Comment: As @MarcB said, `if (answer == -1) { fprintf(stderr, "%s -> %s: %s\n", name, newname, strerror(errno)); }`
Comment: `#include `
Comment: In that case please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) which shows what you have tried (including the error test suggested) that can be copy/paste and replicated. But even then, unless the file was saved with the wrong `.suffix` in the first place you'll render the file unreadable.
Comment: `s` is uninitialized.
Comment: Does `name` include the full path name? If it is `No such file or directory` your recursion may not be the "current directory"
Comment: do you have permission to rename files in that directory? check `errno` to get the actual reason for failure. and note that this does NOT turn that file from a jpg into a png. it just changes the name, and can make the picture appear "corrupt" to anything expecting an actual .png picture when loading the file.
Comment: That function fails horribly if you pass it a name shorter than 4 characters or longer than `INT_MAX`.
Comment: I tried manually renaming and it worked, then I think permission is not the cause.
Comment: I tried that code, but in Dev-C++ it shows: `[Error] 'errno' undeclared (first use in this function)`. How can I declare it? Or is it from some include library?
Comment: Strange, it shows `No such file or directory`. I will double check
Comment: @WeatherVane. I just posted the relevant part, but I updated with the full code
Comment: @melpomene. I took `listdir` from another post. And initially it was `if (entry->d_type == DT_DIR) {` ... but it didn't work for me, then I put `s` there.... anyways, how can it be a problem?
Comment: This may help you: http://stackoverflow.com/questions/1542763/what-is-the-correct-way-to-use-the-stat-function-to-test-if-a-dirent-is-a-dire/1542780#1542780
Answer: int n = strlen(name);
if ( n > 4 ) {
   if ( strcmp(&name[n-4], ".jpg") == 0 ) {
      strcpy( &name[n-3], "png";
   }
}

Comment: While this code snippet may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

File is found, but name is not updated


How many college level varsity women's rugby teams are there in the US currently?

Question: I know there are a lot of club women's rugby teams in the US at the college level but I'm very curious how many of those teams are actually considered varsity level sports at their universities currently?
Answer: Harvard. Brown. Dartmouth. Quinnipiac. West Chester. Army. Lindenwood. Bowdoin. Eastern Illinios. Central Washington. Colorado Tech. Norwich. New England College. Life. Davenport. Sacred Heart. *Penn State. *Notre Dame College.
Comment: Hi welcome to the community and glad you could join us.  Its always useful to provide your source as a link if possible when answering a question.


How to use EntityFieldQuery with a simple text field?

Question: I have a content type called foo and it has a textfield called field_bar. I have one that has a value set to baz.
I'd like to use the EntityFieldQuery object to find all foos that have field_bar with the baz value.
$nodeQuery = new EntityFieldQuery();
  $nodeQuery->entityCondition('entity_type', 'node', '=')
    ->entityCondition('bundle', 'foo')
    ->fieldCondition('field_bar', 'value', '12345', '=');

The problem is that I get no results or error back. If I remove the fieldCondition, I get all foos including the one I am looking for.
As the documentation is vague and only for experts, I googled a lot, but can't find any reason for my query not returning my node.
I also tried with
->fieldCondition('field_bar', 'value', array('12345'), '=');
to see if that matters, but its the same. I dd-ed the $nodeQuery but can't see the executed SQL.
Is there anything special with textfields?
Comment: I am in the context of a REST service resource operation, but as w/o the fieldcondition I see all nodes, I guess its not about permissions..
Comment: Are you sure your db values are actually what you expect? Does `->fieldCondition('field_bar', 'value', '%12345%', 'LIKE');` give you the right results, for example?
Comment: @Clive: thx, no result with % and like. Double checked that the match is exact anyways, no whitespace around the value.
Answer: From the canonical reference on EFQs,

->fieldCondition($field, $column = NULL, $value = NULL, $operator =
  NULL, $delta_group = NULL, $language_group = NULL)
These conditions are specific to a field implementation.
Looking at the body field in the article node type, a field condition
  would look like this:
  ->fieldCondition('body', 'value', 'A', 'STARTS_WITH')

field name. Though the field table in the database is named 'field_data_body', the actual field name is 'body'. This is in the
  field_config_instance table.
column. This is the column in the database that should be matched, with field name prefix removed. For body field, the database columns
  are: body_value, body_summary, body_format, and language. 'value',
  'summary', 'format', and 'language' are the actual arguments you would
  use. Likewise an image field would use 'fid', 'alt', and 'title' as
  column names; an entity reference field would use 'target_id' and
  'target_type' as column names.

So, your syntax is correct, assuming your field name is correct (you want the machiine name), and your column is correct.  Text and Longtext fields both use "value".  You can verify this by looking at the database itself.
If your field name is field_bar, there will be a table called field_data_field_foo.  Look at the columns.  You should see one called field_bar_value.
Answer: Ok, this is very important!
The solution was adding this to the query:
 $nodeQuery->addMetaData('account', user_load(1));

As I mentioned I was running the code in the context of a Services module resource request. Looks like that I am prevented from filtering on a particular field, however not prevented from getting everything if I don't specify filter just load all and use node_load.
Comment: EFQ automatically enforces node_access when you query against node entities.  By adding that account, you are essentially bypassing all node access permissions (ie, you are introducing a security issue into your site).  The node that you are trying to get likely isn't accessible by anonymous users, either because it is unpublished or because of another access restriction.
Comment: you can bypass this need: http://drupal.stackexchange.com/a/39548/13366
Answer: Have you tried to debug that EntityFieldQuery with dpm() like this?
dpm($nodeQuery->execute());

What is your result?
PS: you need the Devel module to run dpm() function...
Comment: I dumped the query, actually its a huge structure, I see my condition exploded there, nothing special. The raw SQL is not shown.


nslookup returns different IP than $_SERVER['SERVER_ADDR']

Question: Without knowing much about system administration, here is in short what I noticed:
running nslookup the-site.com returns some IP
but
running $_SERVER['SERVER_ADDR'] on that site returned different IP.
Could someone give a possible explanation of why is this difference?
Longer version:
Why I'm asking this? I was trying to white-list a clients IP form my security plugin (on WordPress), but even after white-listing IP I got from nslookup he still seemed blacklisted. I then tried white-listing the IP returned by $_SERVER['SERVER_ADDR'] (executed on his site) and that did the trick.
Thanks!
Comment: Hi @kasperd, that's not important for my question. I use a wordpress plugin for security and blacklisitng/whitelisting among others. Question is pretty clear, why the other site that tries to connect to my site (why and how doesn't matter) has different IPs with nslookup and $_SERVER['SERVER_ADDR'].  Tero Kilkanen, my server config has nothing to do, other site's server seems to have, and as I'm not an expert, I would like just some simple suggestions as in the accepted answer. Thanks for your interest guys!
Comment: This question is a bit unclear. A white-list isn't necessary unless you have introduced some blocking in the first place. And you haven't told us where you are blocking connections and what criteria that is based on.
Comment: What is your web server and what is its configuration? How is the server set up?
Answer: The PHP server variable <code>$_SERVER['SERVER_ADDR']</code> is set by the webserver on which your PHP code is executed.
There are many situations in which the actual webserver that runs your code will have a different ip-address from the one associated with the DNS domain name in the URL:

The site is behind a CDN like CloudFront.
The site is behind a load-balancer
The site is behind a reverse proxy server
etc.


Exercicio calculo maior numero python

Question: Boa tarde,
Estou desenvolvendo um exercício em python, vi que já existem algumas resoluções semelhantes no forum, mas desenvolvi uma própria e não estou entendendo o motivo do mal funcionamento (só é retornado o valor 0, a não ser que caia no if <2, nesse caso a resposta é "ERRO")
Segue código e proposta do exercício, agradeço a ajuda.
Escreva a função maior_primo que recebe um número inteiro maior ou igual a 2 como parâmetro e devolve o maior número primo menor ou igual ao número passado à função
Note que:

maior_primo(100) deve devolver 97
maior_primo(7) deve devolver 7

def eprimo(k):

  n = 2
  numero = 0

  if k<2:
    return "Teste: Não é possivel calcular"
  else:
    while n<=k:
      if all(n%x!=0 for x in range(2,k)):
        numero=n
        n=n+1
      else:
        n=n+1

  return numero

k=int(input("numero k:"))

print(eprimo(k))

Answer: na sua verificação de 1 a 1, você está procurando divisores até o número máximo dado(k), não até o número menor que está verificando (n):
if all(n%x!=0 for x in range(2,k)):

Troque por:
if all(n%x!=0 for x in range(2,n)):

e deve funcionar.
Depois de arrumar, fica um pouco de espaço pra você otimizar a coisa: por que começar procurando os primos a partir do número mais baixo, por exemplo? Será que teria algum ganho se você começasse de K e fosse verificando os números progressivamente menores?
Também veja se você entende direito o que essa linha que precisa de correção está fazendo - ela é código bem mais avançado do que o código que você tem em volta - se não estiver entendendo, é mais legal re-escrever de uma forma mais simples, que você entenda, do que usar uma fórmula mágica que "mais ou menos funciona".
Comment: Muito Obrigado! Fiz a correção e funcionou normalmente, entendi que caso mantivesse o range entre 2 e k ele seria bem maior, mas ainda penso que mesmo com esse range maior ele ainda deveria encontrar as divisões com resto 0 e consequentemente memorizar o número, não deveria?
Comment: o problema com o range maior é que ele vai passar por "n" também, e o resto da divisão de n por n sempre é zero, mesmo se n for primo.
Comment: Não conhecia essa diretiva `all()` com essa compreensão de lista. Python sempre me surpreende com sua elegância

Exercicio calculo maior numero python


How to get file extension from filename in PostgreSQL

Question: How to get file extension from filename in PostgreSQL 10
filename.zip
filename.tar.gz
filename.rar
filename.tar.bz2

It's like this down here
filename_202011120001.zip
filename_202011120002.tar.gz
filename_202011120003.rar
filename_202011120004.tar.bz2

Comment: For `filename.tar.gz`, do you want `.gz` or `.tar.gz`?
Comment: And what do you want as a result?
Answer: You can use regexp_replace() . . . but it is unclear what you want.  If you want to remove all characters up to and including the first period:
regexp_replace(filename, '^[^.]*[.]', '')

If you want to return the last suffix:
regexp_replace(filename, '^.*[.]([^.]+)$', '\1')

Here is a db<>fiddle.
Answer: You can use substring():
substring(filename from '\..*')

This captures everything from the first dot until the end of the string.
Demo on DB Fiddle:
select filename, substring(filename from '\..*') ext
from (values
    ('filename.zip'),
    ('filename.tar.gz'),
    ('filename.rar'),
    ('filename.tar.bz2')
) t(filename)

filename         | ext
:--------------- | :-------
filename.zip     | .zip
filename.tar.gz  | .tar.gz
filename.rar     | .rar
filename.tar.bz2 | .tar.bz2


Get authenticated user details from a remote database

Question: I have 2 domains say site1.com and example.com. I have separate databases for both these sites .
How could I perform or allow users from my site1.com to login to my second site example.com without registering. If a person is an authenticated user in site1.com, he or she could login to the example.com site without signing up, i.e by using the login credentials of site1.com. Are there any modules that would help or be suitable for my task?
Also I would like to know is I could access some table details from a remote database. Thanks in advance
Answer: I recommend separating these questions.
1) Drupal Single Sign on module: https://drupal.org/project/sso
2) Again, I recommend posting this as a separate question, but could you be more specific? which table(s)?
Comment: tanx for the reply @jdu.But i really need it for my drupal 7 website.`sso` is for drupal 6


solve $z^2+z+1$ for $z=(x,y)$ by writing $ (x,y)(x,y)+(x,y)+(1,0)=(0,0)$

Question: solve the equation $z^2+z+1=0$ for $z=(x,y)$ by writing
$$ (x,y)(x,y)+(x,y)+(1,0)=(0,0)$$
and then solving a pair of simultanious equations x and y
suggestion: use the fact that no real number x satisfies the given equation to show that $y\neq 0$
ans $z=(-\frac{1}{2},\pm \frac{\sqrt{3}}{2})$

First attempt
$$ \begin{aligned}
   &(x,y)(x,y)+(x,y)+(1,0)
  \\&=(x x-y y,y x+x y)+(x,y)+(0,1)
  \\ & =(x^2-y^2,2xy)+(x,y)+(0,1)
  \\& =(x^2-y^2+x,2xy+y+1)
\end{aligned}$$
 so  $x^2-y^2+x=2xy+y+1$ (missing steps, could use a hint) so $z=(-\frac{1}{2},\pm \frac{\sqrt{3}}{2})$
Comment: $(a,b)=(0,0)$ implies $a=0$ and $b=0$, a system of equations. While it technically implies $a=b\ (=0)$  this is not the direction that will help you.
Answer: Hint:  There is a mistake in your attempt and you should instead get $x^2-y^2+x+1=0$ and $2xy+y=0$.  Then, write $(2x+1)^2-4y^2=-3$ and $(2x+1)y=0$.


Historically, did pseudo RNGs have range [0,1] because of the ease of drawing from $f$ using $F^{-1}(p)$, or for some other reason?

Question: A simple, though not necessarily efficient, way to simulate random draws from a probability density function $f$, is to apply the inverse cumulative distribution function to a random variate distributed uniformly from 0 to 1. The pseudo random number generators (RNGs) I am (barely) familiar with produce pseudo-random values having this kind of distribution.
I do not really know much about the history of how RNGs were developed, but I wonder if RNG default behavior was expressly designed because of its suitability for simulating from arbitrary distributions (or at least from arbitrary distributions with computable inverse cumulative distribution functions)? Or are there other reasons why RNG algorithm output should range [0,1]—as opposed to [-1, 1], [0, floating point limit], or [0, long limit], etc.—and specifically with a uniform, as opposed to some other distribution?
I know that, statistical properties aside, computational algorithms have other properties, e.g., security, memory use, computational complexity, etc., which weigh on development and adoption of a RNG algorithm; any insights welcome.
Answer: In principle, any continuous distribution can serve as a starting point for a random number generator.  Nevertheless, the standard continuous uniform distribution is a natural starting place for a pseudo-random number generator (PRNG) for a few main reasons:

The method of inverse transformation sampling allows us to generate any random variable from a standard uniform random variable, and the latter is a natural starting place for this computation.  In particular, the mathematics of transforming a standard uniform random variable to a random variable with another distribution is particularly simple and intuitive.
The standard uniform distribution has particularly simple properties for the purposes of testing the accuracy of the PRNG method.  These methods are subject to a battery of tests to ensure that they have desirable properties for a random number generator.  These tests are particularly easy to frame for a PRNG that generates a standard continuous uniform random variable.  For example, standard occupancy tests are particularly easy to deploy for uniform random variables.
Computational methods that generate real numbers are subject to rounding error.  In most platforms the numbers are stored in double-precision floating point format, and this format has a fixed level of accuracy in the fractional part.  When generating a standard continuous uniform random variable the interval between values of the fractional part have fixed probability, so no intervals are larger or smaller than others.  (Contra this reasoning, note that this is also arguably a reason to prefer an exponential random variable as the starting point for analysis, since the floating point format uses an exponent.)
Answer: Vetting a PRNG. First, the standard uniform distribution $\mathsf{Unif}(0,1)$ is mathematically simple. So given pseudorandom numbers that are purported to be indistiguishable, for practical purposes, from a random sample from a standard uniform population, it is easy to test whether the claim is true.
For example, close to $1/10$th of the observations should lie
in each of the intervals $(0.1k, 0.1k+.1),$ for $k = 0, \dots, 9,$ and we can do a chi-squared goodness of fit test to see if that is true.
Today in R, the procedure runif(10^4) generates $10\,000$ observations supposedly from  $\mathsf{Unif}(0,1).$
set.seed(510)
u = runif(10^4)
hist(u, br=seq(0,1,by=.1), ylim=c(0,1200), label=T)

x = hist(u, br=seq(0,1,by=.1), plot=F)$counts; x
[1]  959  960 1044 1048  966 1001 1044 1001  990  987

So we see that the $10\,000$ observations are consistent
with a sample from $\mathsf{Unif}(0,1).$ [In chisq.test equal probabilities for groups are assumed if no other probabilities
are supplied.]
chisq.test(x)

        Chi-squared test for given probabilities

data:  x
X-squared = 10.884, df = 9, p-value = 0.2837

    Chi-squared test for given probabilities

data:  x
X-squared = 10.884, df = 9, p-value = 0.2837

And so on, through many more tests to vet the random number generator
as useful.
Quantile method for continuous distributions. Second, as you say, it is possible to use the quantile (inverse CDF) transform to get samples from a vast variety of other distributions. So that the
transform below should give us a pseudo-random sample from $\mathsf{Exp}(1).$
w = qexp(runif(10^5), 1)
hist(w, prob=T, br=50, col="skyblue2")
 curve(dexp(x,1), add=T, col="red", n=10001)

In the figure above, a standard uniform density curve seems a good fit to a density histogram of the data. Also, a Kolmogorov-Smirnov test does not reject the null hypothesis
that the first 5000 values in w are a sample from a standard uniform distribution. [The test does not allow samples larger than 5000.]
ks.test(w[1:5000], pexp, 1)

        One-sample Kolmogorov-Smirnov test

data:  w[1:5000]
D = 0.0054447, p-value = 0.9984
alternative hypothesis: two-sided

The test statistic $D$ of the K-S test is the maximum vertical
discrepancy between the target CDF and the empirical CDF
of the sample (a stairstep function of sample values that
approximates the CDF). We illustrate with a sample of size 100.
ks.test(w[1:100], pexp, 1)

        One-sample Kolmogorov-Smirnov test

data:  w[1:100]
D = 0.076693, p-value = 0.5988
alternative hypothesis: two-sided

plot(ecdf(w[1:100]))
curve(pexp(x,1), add=T, col="red", lwd=2)

Quantile method for discrete distributions. The quantile transform method also works for discrete random variables
(provided that the quantile function is carefully programmed, as it is in R). So let's simulate a sample from $\mathsf{Binom}(10, .5):$
[The R procedure ks.test does not apply to discrete distriubtions.]
v = qbinom(runif(5000), 10, .5)
hist(v, prob=T, br = (-1:10)+.5, col="skyblue2")
  vv = 0:10;  pdf = dbinom(vv, 10, .5)
  points(vv, pdf, col="red")

Notes: (1) In R, the quantile method us used to generate normal random samples—even though
the normal CDF cannot be expressed in closed form and thus cannot be inverted
analytically. R uses Michael Wichura's (piecewise) rational approximation to the
standard normal CDF, and his inversion of it. Results are accurate up to
double-precision arithmetic.
set.seed(2020);  rnorm(1)
[1] 0.3769721
set.seed(2020);  qnorm(runif(1))
[1] 0.3769721

Earlier methods of simulating standard normal variates were
(a) to use $\sum_{i=1}^2 U_i - 6,$ where $U_i \stackrel{iid}{\sim}
\mathsf{Unif}(0,1),$ which relies on the quick convergence of the CLT for uniform random variables and requires only simple arithmetic operations, and (b) to use the Box-Muller transformation, which is
somewhat more accurate and requires computing logarithmic and trigonometric functions.
(2) Undoubtely, there are many other reasons: some of them lost history from
the mid-1950s, and some possibly yet to come here in additional answers
or comments.


Recuperando dados no Firebase Android com classe

Question: Tenho esta estrutura de banco que é esta

Na minha classe tenho um função chamada getLocal (), onde ela pegaria o nome Iguatemi. desta estrutura. Porem ao realizar a query ele esta retornando como null.
Segue minha clase com as funções abaixo:
public class Mesa {

private String ID;
private String Local;
private DatabaseReference reference;
private String NFC;

public Mesa() {
}

public void Salvar(){
    DatabaseReference reference = ConfiguracaoFirebase.getReferencia();
    reference.child("Mesas").child(getID()).setValue(this);
}

@Exclude
public String getID() {
    return ID;
}

public void setID() {
    reference = ConfiguracaoFirebase.getReferencia().child("Mesas");
    this.ID = reference.push().getKey();
}

public String getLocal(String Txt) {
    DatabaseReference reference = ConfiguracaoFirebase.getReferencia();
    reference.child("Mesas").child(Txt).child("local");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String texto = (String) dataSnapshot.getKey();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("TAG", databaseError.getMessage());
        }
    });

        return Local;
}

public void setLocal(String local) {
    Local = local;
}

}
Por favor me ajudem com o que eu estou errando.
Para ficar melhor a pergunta vou colocar como estou fazendo na Activity.
package meals.com.meals.activity.Activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import meals.com.meals.R;
import meals.com.meals.activity.modelo.Mesa;
import static meals.com.meals.R.array.Restaurante_KFC_Pratos;
import static meals.com.meals.R.array.Restaurante_McDonalds_Pratos;
public class FazerPedido extends AppCompatActivity {
private TextView InfoMesa;
private Spinner SpinnerRestaurante;
private Spinner SpinnerPratos;
private Mesa Mesas;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fazer_pedido);

    InfoMesa = (TextView) findViewById(R.id.textViewInfoMesa);
    SpinnerRestaurante = (Spinner)findViewById(R.id.spinnerRestaurante);
    SpinnerPratos = (Spinner) findViewById(R.id.spinner_pratos);

    Intent intent = getIntent();

    Bundle bundle = intent.getExtras();

    String NFC = bundle.getString("TagNFC");

    Mesa Mesas = new Mesa();
    Mesas.RecuperarMesa(NFC);

    InfoMesa.setText("Você esta em " + Mesas.getLocal() + " " + "na mesa \n" + NFC);
   // InfoMesa.setText("Você esta na na mesa \n" + NFC);

}

}
Answer: Chame dataSnapshot.getValue()para acessar os dados.
E é boas práticas verificar sedataSnapshop.exists() antes de chamar.
Se não existir, é porque você está acessando uma child vazia.
reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshop.exists()){
            Mesa mesa = dataSnapshot.getValue(Mesa.class);

            //Faça o que quiser com o objeto.
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e("TAG", databaseError.getMessage());
    }
});

Sempre que for salvar e retornar dados pelo Firebase, crie um Getter simples pra todas as variáveis que for recuperar. Assim como fez com a ID, mas com o Local também.
Assim como você tem um método public void Salvar(), crie um public Mesa Recuperar() que vai baixar a Mesa assim como você fez. Retorne essa Mesa.
public Mesa Recuperar(){
    Mesa mMesa = new Mesa();
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshop.exists()){
                mMesa = dataSnapshot.getValue(Mesa.class);

                //Faça o que quiser com o objeto.
            }
        }
    return mMesa;
}

E tendo o getter simples para o local da classe Mesa.
public String getLocal() {
    return local;
}

Assim, se quiser acessar o local, é só chamar mMesa.getLocal() e fazer o que quiser, como salvar numa variável global ou retornar.
Comment: O método "getLocal()" deveria ser um método getter comum. E aí você poderia chamar o mesa.getLocal() para ter acesso a String retornada por ele. E recuperar os dados do banco de dados através de um método separado, que retornaria uma Mesa.
Comment: se eu quiser pegar o Nome do local em String e retornar, como ficaria?
Pois como estou pegando A classe Mesa.class eu iria pegar tudo né?
Comment: acho que sou muito burro, não sei se pode passar um email ou algo do tipo para vc me ajudar mais..... segue o meu ai 19 997749701 pode me chamar
Comment: Dentro do Mesa recuperar eu coloquei esta parte do codigo a mais pois tenho que procurar através do ID

`public Mesa RecuperarMesa (String Txt){`
 `Mesa mesa = new Mesa();`
`DatabaseReference reference =ConfiguracaoFirebase.getReferencia();`
        `reference.child("Mesas").equalTo(Txt);`
Comment: Pelo que seu código mostra, você salvou a mesa usando o ID. Então para procurar usando o ID é só dar um reference.child("Mesas").child(ID). Com isso você já tem a referência da mesa com o ID que você quer.


How to prevent Drupal from automatically truncating blog entries?

Question: When you enable the Drupal 7 Blog module, the blog page (http://mydrupal.tld/blog) displays truncated blog entries.  It looks like Drupal truncates after the end of the first paragraph (
).  In order to see the entire blog entry, you have to click on its title or on the Read more link.
I'd like to save that click to my visitors by displaying full length blog entries directly on the blog page.
It seems that print render($content); performs the truncation, but I couldn't find anything in the method's API about that.
Any idea ?
Comment: Is it necessary to use the built in blog module? A View could easily accomplish what you need.
Comment: Yes I have to use this module because I'm migrating from a 6.x version with an existing database.
Comment: I strongly disagree that this is a reason enough to keep to the blog module. You should also be aware that while the module itself may live on, it has been dropped from Core in D8. I would move away from it as soon as possible. (http://drupal.org/node/233301)
Comment: Guy: I'm starting with Drupal and my management doesn't leave me enough time to get into that kind of consideration.  PLUS Drupal 7 IS SHIPPED with that module, so yeah, I'm using it, that seems pretty logical to me.
Comment: Using a blog module that ships with Drupal for blogging, yeah, that definitely is logical. I'm just saying that despite this, you shouldn't. The module is being dropped becuase it's just not a good module anymore, as is well documented in the issue I linked to. If this came across as a negative comment towards _you_, I apologize, I only meant to help. :)
Comment: Ok, thanks.  Sorry for my reaction: this whole Drupal integration is slowly driving me completely mad...  Thanks for the heads up, I'll look into it if the guys above let me ;)
Answer: The length of the body text for a node teaser is set in the "Manage Display" section for  the content type's fields.
Just go to http://mysite.com/admin/structure/types/manage/blog/display/teaser, and change the format/format settings (i.e. "Trim length") until the teasers look exactly like you want.
Comment: I've tried it already, but nor the "Plain text" option, nor rising the "Trim length" did solve my problem.  It keeps displaying in the exact same way (only the first § gets shown).
Comment: Finally, unthicking "Teaser" in the "Custom display settings" section solved it (even if I don't really understand why).  Thanks.
Comment: I am having the same problem. After the first  ends the teaser doesn't display anything else, even though it is well below the trim length I set in the teaser display.

Can you explain what you mean by unthicking Teaser and where you did this? (I am assuming you mean content type->manage display->teaser, but not sure)


Handling errors and flash messages when using ajax form in devise

Question: I am using devise, submitting all devise forms like sign up, login, forgot password forms in ajax way by adding remote: true.
How I can handle displaying error messages and flash messages.
Answer: Does this help you out?
Devise - Sign In with Ajax
Basically, you would send a 401 or similar status back with the response, then handle the error inside your ajax error function.  From there you could append to a DOM element and style accordingly.
Comment: For all the actions like registrations, sessions, forgot password, resend confirmation do i need to do the same?
Comment: I'd think you would approach it the same way. You just have to handle errors manually based on the response http status, as opposed to letting devise do it for you.


How to get to the Messaging app to text someone?

Question: I'm making an app and I want to be able to go from my app to the messaging app straight to the "Create new" page. Is there a way I can navigate to that page straight from my app?
Answer: As Andrew M mentioned, the SmsComposeTask is the correct control to use. Here is some sample code for you:
SmsComposeTask smsTask = new SmsComposeTask();
smsTask.To = "0123456789"; // the number you would like to send the sms to
smsTask.Body = "Some prefilled text...";  // if you would like to fill some text
smsTask.Show();

When Show() is called, the app will navigate to the Messaging application and display an SMS filled in with the defined parameters.
Simply use the above code in an event handler (i.e., the event for when a button is clicked), and the user will be navigated accordingly.
Answer: Use the SMSComposeTask:
http://www.nickharris.net/2010/09/how-to-sms-using-the-smscomposetask-for-windows-phone-7/


Sample and Population Mean and Median

Question: The equations for calculating sample and population standard deviations are slightly different:
$ s = \sqrt{ \frac{1}{n-1} \sum_{i=1}^{n} (x_i - \overline{x})^2 } $
$\sigma = \sqrt{ \frac{1}{n} \sum_{i=1}^{n} (x_i - \mu)^2 }$
So why are calculating the sample and population mean, and sample and population median, the same?
Comment: This question comes up often (with slightly different phrasing). Here is an example: http://stats.stackexchange.com/questions/11707/why-is-sample-standard-deviation-a-biased-estimator-of-sigma
Answer: Loosely speaking the standard deviation uses 1 estimate ( the mean ) requiring 1 degree of freedom subtracted from the sample size.
The sample mean and median don't use any estimates for their respective calculation, so no requirement for a degree of freedom
Answer: Yes, the formula for sample and population standard deviations are different. The formula for calculating the population and sample mean are roughly the same in the sense that in both the cases we are diving the sum of observations by the number of observations. The only difference is the number of observations in both the cases are denoted by different terms. In case of population mean, we denote it by $N$ and in case of sample mean we denote it by $n$. Similarly, median or the $0.5^{th}$ quantile for both population and sample can be calculated using the same method or formula. The only difference will be in the results.

Sample and Population Mean and Median


Issue with predict function in R for one biostack and not another

Question: I am doing species distribution modeling (ecological niche modeling) where I project a model to either current or future climate rasters (Bioclim variables).
When I predict to the current rasters (object=bio_stack), everything works, using this code:
#predict species current distribution using bio_stack
current_dist<-predict(object=bio_stack, model=mod_gam,filename="whitebark_current_dist_17April2014",
           na.rm=TRUE, type="response", format="GTiff", overwrite=TRUE,
           progress="text")

However, when I predict to the future rasters (object=future_bio_stack), this code does not produce the results:
#predict species future distribution using future_bio_stack
future_dist<-predict(object = future_bio_stack, model=mod_gam,filename="whitebark_future_dist_17April2014",
           fun=predict, na.rm=TRUE, type="response", format="GTiff", overwrite=TRUE,
           progress="text")

Instead, I get this warning message:
In addition: Warning message:
In predict.gam(model, blockvals, ...) :
  not all required variables have been supplied in  newdata!

The future biostack layers to which I am projecting look ok in an image, with normal min and max values (I can not post the image without a reputation score).
However, the min and max values of the future_bio_stack look strange in the summary, especially compared to the bio_stack.  Here is a summary of each of the stacks, with the min and max values provided:
> future_bio_stack

class       : RasterStack
dimensions  : 4800, 5880, 28224000, 5  (nrow, ncol, ncell, nlayers)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -152, -103, 30, 70  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84 +no_defs
names       : WC05future_clipped, WC06future_clipped, WC08future_clipped,     WC13future_clipped, WC15future_clipped
min values  :             -32768,             -32768,                 -32768,             -32768,             -32768
max values  :              32767,              32767,                  32767,              32767,              32767

> bio_stack
class       : RasterStack
dimensions  : 4800, 5880, 28224000, 5  (nrow, ncol, ncell, nlayers)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -152, -103, 30, 70  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
names       : WC05_clipped, WC06_clipped, WC08_clipped, WC13_clipped, WC15_clipped
min values  :          -56,         -429,         -145,            7,            5
max values  :          457,          100,          332,          767,          129

Any ideas why bio_stack is working with the predict function but future_bio_stack is not?
Answer: I am answering my own question here, as I just figured this out.
In order to project the trained model (which used bio_stack) to another set of rasters (to eg. future_bio_stack), the column names of each of the stacks have to be the same.
I renamed the files AND re-stacked the rasters of the future_bio_stack,  making the names the same as those in bio_stack:
> bio5future<-raster("WC05_clipped.tif")
> bio6future<-raster("WC06_clipped.tif")
> bio8future<-raster("WC08_clipped.tif")
> bio13future<-raster("WC13_clipped.tif")
> bio15future<-raster("WC15_clipped.tif")
>
> future_bio_stack<-stack(bio5future, bio6future, bio8future,
> bio13future, bio15future)

I then re-ran the predict function, using future_bio_stack as my object, and it worked.


Objective-C Multiple Cast

Question: Is it possible to cast an object in Objective-C so as to tell the compiler that its type could be one of many?
For example, in my answer to iOS: Two Gestures, One Target-Action, I know an object will either be a UITapGestureRecognizer or a UILongPressGestureRecognizer but am not sure which one. And, both of those classes respond to numberOfTapsRequired but not through a common protocol. They just both implement the same property.
So, to get around compiler errors, I just cast the object as UILongPressGestureRecognizer. This works now, but if a future version of the iOS SDK changes the name of the UITapGestureRecognizer numberOfTapsRequired property (and left that of UILongPressGestureRecognizer unchanged), then my code would compile but crash with an unrecognized selector exception on a double-tap.
So, if there were a way I could tell the compiler, "Hey, I know this object is either one of two types," then that would allow me to make an accurate cast.
If you can't do this in Objective-C, do any programming languages allow this? I hear C# pretty much lets you do anything.
Comment: Keep things simple and just use two Target-Actions
Answer: I don't think that a "multiple cast" exists in Objective-C, but you could use something like this to catch this issue at compile-time.
if([gestureRecognizer isKindofClass: [UITapGestureRecognizer class]]) {

     (UITapGestureRecognizer*)gestureRecognizer.numberOfTapsRequired;
}

else if([gestureRecognizer isKindofClass: [UILongPressGestureRecognizer class]]) {

     (UILongPressGestureRecognizer*)gestureRecognizer.numberOfTapsRequired;
}

Objective-C Multiple Cast


How I should deserialized this object correctly?

Question: I use an API that return a collection of company symbols with some properties but I don't know how should I deserialize it
{
    "A": {
        "advanced-stats": {
            "prop1": 0.198791,
            "prop2": 16.59,
            "prop3": 12.44,
        }
    },
    "AA": {
        "advanced-stats": {
            "prop1": 0.198791,
            "prop2": 16.59,
            "prop3": 12.44,
        }
    },
    "AAAU": {
        "advanced-stats": {
            "prop1": 0.198791,
            "prop2": 16.59,
            "prop3": 12.44,
        }
    }
}

Comment: Dictionary (keys = A, AA etc) of Objects containing a property (advanced-stats) of type List/Array of "some pair type" would be a start.. and transform in whatever local model is appropriate.
Comment: This might help you to get a starting point: http://json2csharp.com/
Comment: I already try this method, also on other sites, but nothing match
Comment: @Stefan You probably don't want to use external web applications as you might store sensitive data somewhere else. You should refrain from using those and OP is asking for a specific C# solution when looking at the tags.
Comment: @Barrosy: well, I meant just to get started with the C# modeling... anyhow, OP seems to already have tried that.
Comment: What solutions have you tried so far? Please check [How-to-ask](https://stackoverflow.com/help/how-to-ask) when asking questions. You might want to check out [this](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) question. See  the following method `System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(json);` as reference.
Comment: @user2864740 It seams that this is the way. I also try this method but in a wrong way. Thank you very much !
Answer: You can model the JSON using the following classes:
public class AdvancedStats
{
    public double Prop1 { get; set; }
    public double Prop2 { get; set; }
    public double Prop3 { get; set; }
}

public class AdvancedRoot
{
    [JsonProperty("advanced-stats")]
    public AdvancedStats AdvancedStats { get; set; }
}

Since the JSON keys have different names, you can model this as Dictionary<string, AdvancedRoot>. Then to deserialize (using Newtonsoft.Json):
var results = JsonConvert.DeserializeObject<Dictionary<string, AdvancedRoot>>(json);

Try it online


Behaviour of using drupal_add_js

Question: When using the drupal_add_js function, should I see the script I am loading appear in the list of scripts being loading in the header of the generated html page? I thought I should, but I don't - so perhaps my understanding of this function is wrong. Can someone clarify what is suppose to happen.
Just as note I have verified that the path to the script is correct so I know that isn't the issue.
Thanks.
Comment: Could you post the code you're using?
Comment: if you have turned on javascript aggregation/optimization, then no, you won't see the individual file, but its content will be included in the file drupal builds.  it's always a good idea to clear these caches when new files are added.  of course, if you aren't doing this aggregation/optimization, then yes, they should show up.
Comment: I don't have aggregation turned on and do clear the cache every time I make a change. But my understanding is correct that it should be visible. Now I just need to figure out why it isn't working.
Comment: @user5013 please post your code
Answer: With drupal_add_js() you can set the scope to either header or footer, specify the group the script is loaded into, and give the script a weight relative to that group. Scripts are loaded by scope > group > weight, where a lower weight will be loaded before a higher weight.
If you have aggregation turned on in /admin/config/development/performance, the script will be combined with others with the same scope & group, and ordered by weight.

Behaviour of using drupal_add_js


Normalization of dummy variables

Question: My data consists of several continuous measurements and some dummy variables representing the years the measurements have been made.
Now, I want to learn a neural network with the data.
Therefore, I am zScore-normalizing all variables, including the dummy variables. However, I wonder if this is a reasonable approach, because normalizing the dummy variables alters their ranges, which I guess makes them less comparable if their distributions differ.
On the other hand, not normalizing the dummy variables might also be questionable, because without normalization their influence on the networks output might be suboptimal.
What is the best approach to deal with dummy variables, normalizing them (zScore) or just leaving them as they are?
Comment: You might find this paper by A. Gelman interesting http://www.stat.columbia.edu/~gelman/research/published/standardizing7.pdf
Answer: Normalization would be required if you are doing some form a similarity measurement.
Dummy variables by its nature acts as a binary switch. Coding it as (0,1) or (-.5,.5) should have no impact on the relationships it exhibits to a dependent variable, if what you are trying to do is some form or regression or classification.
It would matter if you are performing clustering because it would be scale dependent.
Answer: Normalizing dummy variables makes no sense. Usually, normalization is used when the variables are measured on different scales such that a proper comparison is not possible. With dummy variables, however, one puts just a binary information in the model and if it is normalized the information of the impact of e. g. one year is lost.
Comment: So, according to the question, how do we deal with loss function?

Normalization of dummy variables


Select selected and other options display differently?

Question: My code is short:

select {
    font-size:1.2em !important;
    padding:4px;
    color:#555;
    margin-left:5px;
}
<select name="loc" id="loc_login">
    <option value="1" selected="selected">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

The problem is that font in non-selected options, (2 and 3) is much smaller than in selected option. That is shown on some computers I've used for test (in Firefox only). All computers with the problem are running under Windows 7, but a few different (50+) versions of Firefox. I have tried on some more Win7 computers, with same Firefox version, but all fonts are same size.
I have even put this code on another page, where it was the only thing in the body tag and the only style defined, with the same result. Also, I've tried changing the default font in the browser, which didn't work either.
When I remove the CSS for the select, all fonts are small and same size.
The goal is that all of them should be 1.2em. Any ideas ?
Comment: Maybe this will help ► [font-size-issues-comparing-chrome-and-firefox](http://stackoverflow.com/questions/4899792/font-size-issues-comparing-chrome-and-firefox)
Comment: or this -> https://fettblog.eu/style-select-elements/
Comment: Possible duplicate of [Font-size issues comparing chrome and Firefox](http://stackoverflow.com/questions/4899792/font-size-issues-comparing-chrome-and-firefox)
Comment: Sadly, nothing helped.
Computers that shows fonts of same size still do, and those which show options with smaller fonts still do.
Comment: here's the printscreen https://s24.postimg.org/ulit0bzmt/example.png
left - as it should be, right - as it is seen on some computers.
Answer: All seems working fine on:
Win7 - Chrome
Arch Linux - Firefox (latest)
But if you are sure it is not, you can try the following CSS code:
select {
    font-size: 1.2em !important;
    padding: 4px;
    color: #555;
    margin-left: 5px;
}

Comment: i tried, there is difference on either side - on some computers fonts are still the same size, on others still different size.
Comment: @Aleks Not working even with the !important at the end of the line? If so, you can try this:
    `
    ...
   `
Comment: no, stil the same.
Only the 1st one changes size when i change it. All other options have same, smaller font size and size doesn't change for them. It is irrelevant if i declare size in em or px. It is not in collision with some other style.

This has the totaly same result

`
1
2
3
`
Comment: @Aleks try putting the `style="..."` to each ``
Comment: i've tried that, but that doesn't work on any computer.
Comment: I think this is a case of User Agent Style Sheet... (The default css)
Comment: no, i've tried that, too. Beaviour is the same no matter what is the size of default font. In "affected" computers, it changes the size of selected option only. It doesn't change the size of other options.
Comment: So then... I have just no idea what's going on. If you have the page published, can you share it, because maybe something of your other scipts or style sheets makes conflict. I recommend you to run several tests with a browser console opened, and briefly check all proccesses and what other CSSes or JavaScripts are doing. Other option is that the OS may have caused lag or something with the video card... Just I have no idea what could cause this.
Comment: It's on sites in intranet, so i don't have it published on the web.  A gentle reminder - i tried just that part of code (selector with option and one css style) and it keeps happening. So, it isn't a conflict with other css styles.
As you, i'm totally out of ideas. Tnx for your help.


Sign flipping when adding one more variable in regression and with much greater magnitude

Question: Basic setup:
regression model: $y = \text{constant} +\beta_1x_1+\beta_2x_2+\beta_3x_3+\beta_4x_4+\alpha C+\epsilon$
where C is the vector of control variables.
I'm interested in $\beta$ and expect $\beta_1$ and $\beta_2$ to be negative. However, there is multicollinearity problem in the model, the coefficient of correlation is given by,
corr($x_1$,$x_2)=$ 0.9345, corr($x_1$,$x_3)=$ 0.1765, corr($x_2$,$x_3)=$ 0.3019.
So $x_1$ and $x_2$ are highly correlated, and they should virtually provide the same information.  I run three regressions:

exclude $x_1$ variable; 2. exclude $x_2$ variable; 3. original model with both $x_1$ and $x_2$.

Results:
For regression 1 and 2, it provide the expected sign for $\beta_2$ and $\beta_1$ respectively and with similar magnitude. And $\beta_2$ and $\beta_1$ are significant in 10% level in both model after I do the HAC correction in standard error. $\beta_3$ is positive but not significant in both model.
But for 3, $\beta_1$ has the expected sign, but the sign for $\beta_2$ is positive with the magnitude twice greater than $\beta_1$ in absolute value. And both $\beta_1$ and $\beta_2$ are insignificant. Moreover, the magnitude for $\beta_3$ reduces almost in half compared to regression 1 and 2.
My question is:
Why in 3, the sign of $\beta_2$ becomes positive and much greater than $\beta_1$ in absolute value? Is there any statistical reason that $\beta_2$ can flip sign and has large magnitude?  Or is it because model 1 and 2 suffer omitted variable problem which inflated $\beta_3$ provided $x_2$ has positive effect on y? But then in regression model 1 and 2, both $\beta_2$ and $\beta_1$ should be positive instead of negative, since the total effect of $x_1$ and $x_2$ in regression model 3 is positive.
Answer: Think of this example:
Collect a dataset based on the coins in peoples pockets, the y variable/response is the total value of the coins, the variable x1 is the total number of coins and x2 is the number of coins that are not quarters (or whatever the largest value of the common coins are for the local).
It is easy to see that the regression with either x1 or x2 would give a positive slope, but when incuding both in the model the slope on x2 would go negative since increasing the number of smaller coins without increasing the total number of coins would mean replacing large coins with smaller ones and reducing the overall value (y).
The same thing can happen any time you have correlalted x variables, the signs can easily be opposite between when a term is by itself and in the presence of others.
Comment: Greg, how does this work with times series financial data? If I'm only interested in understanding the relationship between x1 and y, and x2 and y, then wouldn't I opt for the separate regression interpretation? Certainly it is true that y tends to rise as x2 rises. Only in the context of holding x1 constant does x2 have a negative relationship with y.
Answer: You have answered your own question - there is collinearity.
A bit of explanation: $x_1$ and $x_2$ are highly collinear. But when you enter both into the regression, the regression is attempting to control for the effect of the other variables. In other words, hold $x_1$ constant, what do changes in $x_2$ do to $y$.  But the fact that they are so highly related means that this question is silly, and weird things can happen.
Comment: Thanks a lot. But since multicolinearity in theory only inflatesthe variance but not affects the overall prediction power of the highly correlated variables, so I thought $\beta_1*x1+\beta_2*x2$ in model 3 should provide similar result as $\beta_2*x2$ in model 1 or $\beta_1*x1$ in model 2, since pairwise correlation of x1 x2 with x3 is not high (actually this is my confusing part). But since correlation can be really messy, and in practice, I should not expect this since my model is only an approximation of the DGP and the correlation with other variables matters.
Comment: If you want to get into the math of this, I highly recommend books by David Belsley.
Comment: Great, Thank you so much!!!  Just requested the books from the library :)
Answer:
Why in 3, the sign of β2 becomes positive and much greater than β1 in absolute value? Is there any statistical reason that β2 can flip sign and has large magnitude?

The simple answer is there is no deep reason.
The way to think about it is that when multicollineary approaches perfect, the specific values that you end up obtaining from the fitting become more and more dependent on smaller and smaller details of the data. If you were to sample the same amount of data from the same underlying distribution and then fit, you could obtain completely different fitted values.


Search a list in C# - List myList = new List();

Question: I have code that looks like this:
List<Meme> myList = new List<Meme>();
myList.Add(DogThugLife);
myList.Add(AintNobodyGotTimeForThat);
myList.Add(WashingTheDishes);

I was wondering how I can add a search feature so people can search for the items in this list. I have also made a Meme class:
class Meme
{
    public string Name { get; set; }
    public string Topic { get; set; }
    public bool Popular { get; set; }
    public string Identifier { get; set; }
}

and some info on the items in my list:
Meme DogThugLife = new Meme();
DogThugLife.Name = "Dog Thug Life";
DogThugLife.Topic = "Thug Life";
DogThugLife.Popular = false;

So basically I want users to be able to search for these properties. Thanks!
Comment: Check this one http://stackoverflow.com/questions/1175645/find-an-item-in-list-by-linq
Comment: It's not clear what you mean by "users search for these properties". Is this a web app, API, or a Winforms app? How will users be interacting with it? (If you just want to know how to search List, @starcorn's link is where to go.)
Comment: http://stackoverflow.com/questions/9854917/how-can-i-find-a-specific-element-in-a-listt
Comment: It's a Winforms app, but I only have the code part not the visual form part yet. Just checked @starcorn's link, thanks for that.
Answer: The List class in C# has a Contains method. That contains method will iterate over the collection and search for an identical object. It does so by invoking the Equals method in System.Object for each object in the collection.
Basically, the Contains method receives an object as a parameter. It will pass this object to the System.Equals method for each object in that collection until it finds an identical object or until it has iterated over all of the objects in the collection (didn't find what it was searching for).
This means that you need to override the Equals method in your Meme class,  this is an example.
A basic implementation for your Meme class can be like this:
class Meme
{
    public string Name { get; set; }
    public string Topic { get; set; }
    public bool Popular { get; set; }
    public string Identifier { get; set; }

    public bool Equals(object other)
    {
        var casted = other as Meme;
        if(null == casted)
        {
             return false;
        }

        return casted.Name == Name && Casted.Topic == Topic and Casted.Popular == Popular && Casted.Identifier == Identifier;
    }

    //IMPORTANT: The Equals and the GetHashCode methods are intertwined. Thus, when you override one of them, you should override the other.
    //The rule is: The GetHashCode method should return the same value for two objects that are equals (according to their Equals method)
    //More about the GetHashCode method: https://msdn.microsoft.com/en-us/library/system.object.gethashcode%28v=vs.110%29.aspx
    public int GetHashCode()
    {
        //Naive implementation
        return Name.GetHashCode();
    }
}

And then:
var myList = new List<Meme>();
// add items to list.
var dogThugLife = new Meme();
// initialize dogThugLife object's properties (though you should use a constructor for that)
var result = myList.Contains(dogTugLife);

It is important to note that there are better ways in terms of simplicity and performance to search for objects in a collection. If you know from advance that you will need to search for an object and you know that you won't have only a few number of items in that collection, then you should consider using a Dictionary or a HashSet instead of a list.
A simple example for using a HashSet:
var mySet = new HashSet<Meme>();
// add items to set (example: mySet.Add(someMeme)).
var dogThugLife = new Meme();
// initialize dogThugLife object's properties (though you should use a constructor for that)
var result = mySet.Contains(dogTugLife); // This will search the set via the GetHashCode value of dogTugLife

Comment: Thanks for the help :)
Answer: A sample program showing how to peek a meme object by its identifier from a generic list
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;

namespace SOFAcrobatics
{
    class Meme
    {
        public String Name
        {
            get;
            set;
        }

        public String Topic
        {
            get;
            set;
        }

        public Boolean Popular
        {
            get;
            set;
        }

        public String Identifier
        {
            get;
            set;
        }

        public Meme (String name, String topic, Boolean popular, String identifier)
        {
            this.Name = name;
            this.Topic = topic;
            this.Popular = popular;
            this.Identifier = identifier;
        }

        public override string ToString()
        {
            return String.Format("(Identifier: {0}, Topic: {1}, Popular: {2}, Name: {3})",
                this.Identifier,
                this.Topic,
                this.Popular,
                this.Name);
        }
    }

    public static class Launcher
    {
        public static void Main ()
        {
            List<Meme> memes = new List<Meme>() {
                new Meme ("name1", "topic1", false, "id1"),
                new Meme ("name3", "topic2", true, "id2"),
                new Meme ("name2", "topic3", false, "id3")
            };

            MessageBox.Show(Launcher.SearchByIdentifier(memes, "id2").ToString(),
                "Search result for Meme name3",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }

        private static Meme SearchByIdentifier (List<Meme> memes, String identifier)
        {
            return memes.Find(
                m => (m.Identifier == identifier)
                );
        }
    }
}

Comment: don't forget that lambda expressions are only available from .net 3.5 upwards


How to tell what is being loaded on a Rules Content is viewed event

Question: Is there a way I can see what exactly is being executed on a Rules Event of 'Content is viewed', followed by an action of page redirect to an external URL? Specifically if the content is actually being viewed, and then redirected.
I tried Devel viewing the query log and I didn't find anything. I also tried Devel's Display redirection page, but upon visiting the node, it just timed out.
Using rules event 'Content is viewed', then redirecting, I am still able to get a log message that says I visited that node, so I believe it is loading the node, then redirecting.
This concerns me because when I test other methods of node redirects to external URLs, I don't get a log message saying that I visited that node.
Answer: I struggled with a similar question ... The "clue" about what "Content is viewed" actually means, seems to be like so:

The event Content is viewed is what it is: the user has (already) started viewing the content.
The (system) event Drupal is initializing is what you should use instead (possible combined with other Events and/or Actions), if you want to trigger some Rules "Action(s)" BEFORE the user can start viewing (= looking at) the content.

For more details, and a working sample (in Rules 'export format'), refer to
How to specify a Rules event like "Content is 'going to be' viewed"?
Be aware: this applies to D7 ... and D6 is another story.


Concurrency involving equilateral triangles constructed outside a triangle

Question: Let $ABC$ be a triangle. On the exterior of sides $AB$ and $AC$, construct two equilateral triangles $ABE$ and $ACF$. Let $H$ be the orthocenter of $ABC$ and $P$, $Q$ be the centers of $ABE, ACF$ respectively. I found out that $AH, EQ, FP$ are concurrent, and I remembered that it was a theorem in school geometry, but I couldn't prove it or find out its origin.
I also found (by GeoGebra) that: If $M, N$ are the midpoints of $AC, AB$ respectively then both $MP, NQ, AH$ and $EM, FN, AH$ are concurrent too, but I haven't proven it. Is this a well-known result in plane geometry?

Please give me some hints or prove/disprove these results for me. Thanks
Answer: This is a very nice problem!
Lemma
Consider the following diagram, where we construct right triangles externally to an arbitrary triangle $\bigtriangleup ABC$. If $\;\measuredangle ABD=\measuredangle CBE \;$ and $\; BG \perp AC$; $$DC, BG, EA$$ are concurrent.

Proof.
Apply the law of sines on $\bigtriangleup CAD$ and $\bigtriangleup CBD$:
$$\frac{CD}{\sin(90°+\alpha+\beta)}=\frac{AD}{\sin \zeta} \tag{1}$$
$$\frac{CD}{\sin(\measuredangle ABD+\gamma+\delta)}=\frac{AD/(\sin\measuredangle ABD)}{\sin \epsilon} \tag{2}$$
Dividing $(1)$ by $(2)$:
$$\frac{\sin(\measuredangle ABD+\gamma+\delta)}{\sin(90°+\alpha+\beta)}=\frac{\sin \epsilon \cdot (\sin\measuredangle ABD)}{\sin \zeta} \tag{3}$$
Applying the law of sines on $\bigtriangleup ACE$ and $\bigtriangleup ABE$ we also get:
$$\frac{\sin(\measuredangle CBE+\gamma+\delta)}{\sin(90°+\epsilon+\zeta)}=\frac{\sin \beta \cdot (\sin\measuredangle CBE)}{\sin \alpha} \tag{4}$$
Since $\bigtriangleup ABG$ and $\bigtriangleup CBG$ are right triangles:
$$\sin\gamma=\sin(\alpha+\beta) \;\; \text{and} \; \sin\delta=\sin(\epsilon+\zeta)\tag{5}$$
Dividing $(3)$ by $(4)$ and substituting from $(5)$:
$$\frac{\sin(\alpha)}{\sin(\beta)}×\frac{\sin(\gamma)}{\sin(\delta)}×\frac{\sin(\epsilon)}{\sin(\zeta)}= 1$$
Hence, by (Trigonometric) Ceva's theorem, the lines are concurrent. We note that this concurrency is independent of the measures of the angles $\measuredangle ABD$ and $\measuredangle CBE$ as long as they are equal.

Back to the original figure,

By the reverse of Thales' Theorem $NM \parallel BC$, so $AH \perp NM$. Construct the line segments $MP, AP, NQ, AQ\;$ and we get the desired configuration for our lemma (the main triangle being $\bigtriangleup AMN$). So, $MP, NQ, AH$ are concurrent. By similar constructions and using our lemma again both $EM, FN, AH$ and $EQ, FP, AH$ are also concurrent.  $\; \blacksquare$

I suspect there is a simpler way to prove the Lemma (might be related to Jacobi's Theorem).
Comment: Once you have your Lemma, you're done. After all, the Lemma implies that *any* "similarly-situated" points $U$ and $V$ on the perpendicular bisectors of $\overline{AB}$ and $\overline{AC}$ have the concurrency property. This includes the case of centers $U=P$ and $V=Q$ and the case of midpoints $U=N$ and $V=M$ (and also the case vertices $U=E$ and $V=F$, etc, etc, etc).
Comment: @trequartista Perhaps you have a typo; $EP, FQ, AH$ are not concurrent.
Comment: @Blue You are right, of course. Since my initial observation was to use Pappus', I tried to force a solution using it. I totally neglected the fact that the Lemma trivializes all three concurrencies immediately. I edited accordingly, thank you. Any ideas on a geometric proof rather than using trigonometry to prove the Lemma?
Comment: In the Lemma you surely meant "concurrent", not "collinear".
Comment: @dodoturkoz: At the moment, I don't see a trig-less approach that's any neater than your trig approach. Besides, the trig form of Ceva's Theorem needs more exposure. :)
Comment: @user I surely did! Thank you for the correction.
Comment: @dodoturkoz Thank you very much for your nice solution. I know your lemma, but I forgot to use it. By the way, is this problem a well known theorem in elementary geometry?
Comment: @anonimo Sorry, I just noticed your response. To be honest, I have never seen this exact problem before, though concurrencies related to polygon constructions on sides of a triangle are extremely common. Here is a link to an extensive collection (BTW, I also don't know French, but you can translate it easily) by the Frech geometer Jean-Louis Ayme: http://jl.ayme.pagesperso-orange.fr/Docs/Triangles%20adjacents.pdf
Comment: @anonimo Regarding your proof: I feel a little tired now, but on the top of my head I couldn't understand how you deduced $AP\perp CD$. If there is a simple explanation to it (maybe angle chasing), then you are right. This would be a much simpler proof than mine :)
Comment: @dodoturkoz You could try to prove the lemma as follows: Let P be the intersection of the perpendicular line of AE, passing through C and BG. Then $AP\perp CD$, and F is the orthocenter of APC therefore CD, AE, BG are concurrent.
Comment: @dodoturkoz  I can see from your proof that MP, NQ, AH and  ME, NF, AH are concurrent but why EP, FQ, AH are concurrent?
Comment: @dodoturkoz Yes, I mean EQ, FP, AH. How can we deduce the proof for the concurrency of them by using your lemma? I think your lemma applies for MP, NQ and AH only.
Comment: I can only use Pappus theorem for the case $EQ, FP, AH$ and looking for the more elementary proof.
Answer: Welcome to MSE! This is vaguely related to the Fermat Point. Hope this helps.
Comment: I felt that too, but I couldn't figure out that relationship. Could you please give me more details on that?
Answer: Well, this should not be consider as an answer, but as a complement for the accepted answer of dodoturkoz, a more elementary proof for the lemma.

Let $K$ be the intersection of $BG$ and the perpendicular line of $AE$ passing through $C$.
Then, $\widehat{KCB}=\widehat{AEC}$.
Also $\widehat{KBC}=\dfrac{\pi}{2}+\widehat{ACB}=\widehat{BCE}+\widehat{ACB}=\widehat{ECA}$.
Thus the two triangles $KBC$ and $ACE$ are similar therefore: $\dfrac{KB}{AC}=\dfrac{BC}{CE}$.
On the other hand, $\Delta BCE\backsim \Delta BAD$, hence,
$\dfrac{BC}{CE}=\dfrac{AB}{AD}$.
Thus $\dfrac{KB}{AC}=\dfrac{AB}{AD}$.
Moreover: $\widehat{KBA}=\dfrac{\pi}{2}+\widehat{BAC}=\widehat{BAD}+\widehat{BAC}=\widehat{DAC}$.
Therefore $\Delta KBA\backsim \Delta CAD$, and then $\widehat{BAK}=\widehat{ADC}$. Hence $CD\perp AK$.
As so, let $F$ be the intersection of $CD$ and $AE$, $F$ should be the orthocenter of $KAC$ and therefore $F$ is on $BG$.
Proof for the lemma is completed.
Comment: (+1) With so many given right angles, a proof involving the orthocenter of a triangle is of course more natural. Also, the way you used the proportionality of sides and congruency of the included angle to establish the final similarity is very nice.


Expectation of Inverse Logit of Normal Random Variable

Question: I have a random variable $Y = \frac{e^{X}}{1 + e^{X}}$ and I know $X \sim N(\mu, \sigma^2)$.
Is there a way to compute $\mathbb{E}(Y)$? I have tried to work out the integral, but haven't made much progress. Is it even possible?
Comment: Apparently, an analytical solution is not known. A known approximation is given in this Math stackexchange link: https://math.stackexchange.com/questions/207861/expected-value-of-applying-the-sigmoid-function-to-a-normal-distribution/1994805
Comment: @Greenparker The distribution of $Y-1/2$ is symmetric around $0$ in that case, *QED.*
Comment: I did it symbolically as a one-liner with mathStatica/Mathematica ... but an easy way to see why it must be so:  ......  (i) if $X \sim N(0,\sigma^2)$, then its pdf is symmetric around 0. (ii) Consider the transformation $Z = Y-\frac12 = \frac12 \tanh(x/2)$. Then $Z$ is a symmetric S-shaped curve about $X = 0$, and E[Z] must equal 0 (by symmetry).  Since $Y = Z + \frac12$, it follows that $E[Y] = \frac12$
Comment: If $\mu = 0$, then $E[Y] = \frac12$, for any $\sigma$.
Comment: @wolfies Could you give a source/derivation of that?
Comment: Or as whuber said it in one-line :)
Answer: As mentioned already in the question comments and answer by @Martijn there doesn't appear to be an analytical solution for $E(Y)$ apart from the special case where $\mu = 0$ which gives $E(Y) = 0.5$.
Additionally by Jensen's inequality we have that $E(Y) = E(f(X)) < f(E(X))$ if $\mu > 0$ and conversely that $E(Y) = E(f(X)) > f(E(X))$ if $\mu < 0$. Since $f(x) = \frac{e^x}{1 + e^x}$ is convex when $x < 0$ and concave when $x > 0$ and most of the normal density mass will lie in those regions depending on the value of $\mu$.
There are many ways to approximate $E(Y)$, I have detailed a few I am familiar with and included some R code at the end.
Sampling
This is quite easy to understand/implement:
$$
E(Y) = \int_\infty^\infty f(x) \mathcal{N}(x|\mu, \sigma^2) dx \approx \frac{1}{n} \Sigma_{i = 1}^{n}f(x_i)
$$
where we draw samples $x_1, \ldots, x_n$ from $\mathcal{N}(\mu, \sigma^2)$.
Numerical integration
This includes many methods of approximating the integral above - in the code I used R's integrate function which uses adaptive quadrature.
Unscented transform
See for example The Unscented Kalman Filter for Nonlinear Estimation by Eric A. Wan and Rudolph van der Merwe which describes:

The unscented transformation (UT) is a method for calculating
  the statistics of a random variable which undergoes
  a nonlinear transformation

The method involves calculating a small number of "sigma points" which are then transformed by $f$ and a weighted mean is taken. This is in contrast to randomly sampling many points, transforming them with $f$ and taking the mean.
This method is much more computationally efficient than random sampling. Unfortunately I couldn't find an R implementation online so haven't included it in the code below.
Code
The following code creates data with different values of $\mu$ and fixed $\sigma$. It outputs f_mu which is $f(E(X))$, and approximations of $E(Y) = E(f(X))$ via sampling and integration.
integrate_approx <- function(mu, sigma) {
    f <- function(x) {
        plogis(x) * dnorm(x, mu, sigma)
    }
    int <- integrate(f, lower = -Inf, upper = Inf)
    int$value
}

sampling_approx <- function(mu, sigma, n = 1e6) {
    x <- rnorm(n, mu, sigma)
    mean(plogis(x))
}

mu <- seq(-2.0, 2.0, by = 0.5)

data <- data.frame(mu = mu,
                   sigma = 3.14,
                   f_mu = plogis(mu),
                   sampling = NA,
                   integration = NA)

for (i in seq_len(nrow(data))) {
    mu <- data$mu[i]
sigma <- data$sigma[i]
    data$sampling[i] <- sampling_approx(mu, sigma)
data$integration[i] <- integrate_approx(mu, sigma)
}

output:
    mu sigma      f_mu  sampling integration
1 -2.0  3.14 0.1192029 0.2891102   0.2892540
2 -1.5  3.14 0.1824255 0.3382486   0.3384099
3 -1.0  3.14 0.2689414 0.3902008   0.3905315
4 -0.5  3.14 0.3775407 0.4450018   0.4447307
5  0.0  3.14 0.5000000 0.4999657   0.5000000
6  0.5  3.14 0.6224593 0.5553955   0.5552693
7  1.0  3.14 0.7310586 0.6088106   0.6094685
8  1.5  3.14 0.8175745 0.6613919   0.6615901
9  2.0  3.14 0.8807971 0.7105594   0.7107460

EDIT
I actually found an easy to use unscented transformation in the python package filterpy (although it is actually quite quick to implement from scratch):
import filterpy.kalman as fp
import numpy as np
import pandas as pd

def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))

m = 9
n = 1
z = 1_000_000
alpha = 1e-3
beta = 2.0
kappa = 0.0
means = np.linspace(-2.0, 2.0, m)
sigma = 3.14
points = fp.MerweScaledSigmaPoints(n, alpha, beta, kappa)
ut = np.empty_like(means)
sampling = np.empty_like(means)

for i, mean in enumerate(means):
    sigmas = points.sigma_points(mean, sigma**2)
    trans_sigmas = sigmoid(sigmas)
    ut[i], _ = fp.unscented_transform(trans_sigmas, points.Wm, points.Wc)

    x = np.random.normal(mean, sigma, z)
    sampling[i] = np.mean(sigmoid(x))

print(pd.DataFrame({"mu": means,
                    "sigma": sigma,
                    "ut": ut,
                    "sampling": sampling}))

which outputs:
    mu  sigma        ut  sampling
0 -2.0   3.14  0.513402  0.288771
1 -1.5   3.14  0.649426  0.338220
2 -1.0   3.14  0.716851  0.390582
3 -0.5   3.14  0.661284  0.444856
4  0.0   3.14  0.500000  0.500382
5  0.5   3.14  0.338716  0.555246
6  1.0   3.14  0.283149  0.609282
7  1.5   3.14  0.350574  0.662106
8  2.0   3.14  0.486598  0.710284

So the unscented transform seems to perform quite poorly for these values of $\mu$ and $\sigma$. This is maybe not surprising since the unscented transform attempts to find the best normal approximation to $Y = f(X)$ and in this case it is far from normal:
import matplotlib.pyplot as plt

x = np.random.normal(means[0], sigma, z)
plt.hist(sigmoid(x), bins=50)
plt.title("mu = {}, sigma = {}".format(means[0], sigma))
plt.xlabel("f(x)")
plt.show()

For smaller values of $\sigma$ it seems to be OK.
Answer: The variable $Y$ has a logit normal or logistic normal distribution whose moments have no known analytical description. You can obtain the values computationally.
More about these distributions is described in a freely available article: Atchison, J., and Sheng M. Shen. "Logistic-normal distributions: Some properties and uses." Biometrika 67.2 (1980): 261-272.
In that text they do not give any expressions for limits, approximations or behavior of the moments (except mentioning that they exist).  But, they do continue with expressions for the expectation value for the ratio of two components in a multivariate logistic normal distributed variable.


For which orthogonal matrices does the matrix exponential converge?

Question: Part (a)
For which 2×2 orthogonal matrices A does
$\large e^A=I+\frac{A^1}{1!}+\frac{A^2}{2!}+…$
converge?
Part(b)
For what A does the series converge to an orthogonal matrix?
My work:
Let A be 2x2 and orthogonal.  Then $A^tA = AA^t = I$ and so this implies that A is normal.  Over the ground field = $C$, A is then orthogonally / unitarily diagonalizable.
We can write $A=QDQ^*$, where Q is unitary and D is diagonal with the eigenvalues of A on the diagonal.  Also, since A is assumed to be orthogonal, then the modulus of each eigenvalue is 1.
Now $$e^A=I+\frac{A^1}{1!}+\frac{A^2}{2!}+…$$
$$ e^A=I+\frac{QDQ^*}{1!}+\frac{QD^2Q^*}{2!}+…$$
$$ e^A= Q(I+\frac{D}{1!}+\frac{D^2}{2!}+…)Q^*$$
$$ e^A= Qe^DQ^*$$
Where $e^D$ is again diagonal.
What can I say from here?  I know that online sources such as Wikipedia and Wolfram just state without any proof or extended discussions that the matrix exponential is well-defined and converges for any square matrix.  If this is stated as a fact without proof, then it seems a little strange that I am working on a problem statement that asks "for which orthogonal 2x2 matrices A does $e^A$ converge".  Is there an important point that I am overlooking?  Or can I really just state that the matrix exponential converges for any square matrix A, hence it is well-defined and converges for any 2x2 orthogonal matrix A?
Any suggestions and hints for how to finish part (a) and how to start on part (b) are welcome.
Thanks,
Comment: Your calculation is sufficient for the case you're given. You don't need to appeal to a general result, even though it is nice. What does your calculation say?
Comment: That's exactly right.
Comment: Is it allowed to use property that $e^{A} \cdot e^{B} = e^{B} \cdot e^{A} = e^{A+B}  \iff \lbrack A, B \rbrack = 0$?
Comment: Yes, I thought about part (b) when I was asking this. It's easy to see that $(e^{A})^{T} = e^{A^T}$. You want $e^A$ to be orthogonal, i.e. $e^A \cdot e^{A^T} = e^{A^T} \cdot e^A = I$. From the property follows that $e^A \cdot e^{A^T} = e^{A+A^T}$. Now you can conclude what matrix should be $A + A^{T}$ if it's exponent is $I$ :)
Comment: However, take my advice with caution. I need to check is the property that I'm referring to is really true.
Comment: It seems that I've made a mistake. Most books only have statement $\lbrack A, B \rbrack = 0 \Rightarrow e^A e^B = e^B e^A = e^{A+B}$.
Comment: It is the right answer (because any orthogonal matrix is an exponent of skew-symmetric matrix), but at least we have to prove that zero matrix is **the only** real matrix which exponent gives identity matrix. As you might know from complex analysis, $e^z = e^{z + 2 \pi k i}$ so it's not always true that from $e^{z} = e^{w}$ follows $z = w$.
Comment: The biggest assumption that we have here is that from $e^{A} \cdot e^{A^T} = e^{A^T} \cdot e^A$ follows $e^{A} \cdot e^{A^T} = e^{A+A^T}$. Using Jordan decomposition it's not really hard to prove that zero matrix is the only **real** matrix which exponent equals identity matrix.
Comment: Hi @ThadJanisse, thanks for the quick response.  Well, I think that in concluding my answer, I need to address the issue of whether the $e^D$ matrix actually converges, but this is clear since it is just $diag(e^{\lambda_1}, ..., e^{\lambda_n})$.  What do you think?  Thanks,
Comment: Ok, got it - thanks so much @ThadJanisse.  On to part(b) ...
Comment: Hi @Evgeny, I think that's fine to use.  Would this be for part(b) of the question?  That would be very interesting to see.  Thanks :-)
Comment: Hi Evgeny, are you assuming that A and $A^t$ commute?  That would imply that A is normal, and I don't think we are allowed to assume that.  Though that could be one case to prove, and then to consider other cases, e.g., A not normal, A diagonalizable / not diagonalizable.  Or, did you mean something else with your solution, which possibly covers *all* cases of A?  Thanks,
Comment: Hi @Evgeny, I'm just expanding out some of your work on paper and now see that, *assuming* that $A$ and  $A^t$ commute (so A is normal), then [A+$A^t$] = [0], in order to achieve the identity matrix I.  This shows that $A^t$ = -$A$, and so we can conclude that for any *anti-symmetric* normal matrix A, the series $e^A$ will converge to an orthogonal matrix.  What do you think?  Thanks,
Comment: Although I am not saying very much ...for an answer to the question.  There are a bunch more cases to cover.  I will re-read your comments now to see what else I can derive...thanks so much @Evgeny :-)
Comment: Hi @Evgeny, I think I see what you're saying.  So, basically, *force* the answer to be anti-symmetric, normal matrices, by first assuming that A is normal.  Then to show that it is the *only* possible answer and that there are no other cases to cover, just simply show that the zero matrix is the only matrix that yields the identity matrix, when we exponentiate it.  But that is already clear, since the zero matrix is trivially diagonal, and $e^{[0]}$ is just $diag(e^0, ... , e^0)$ = [0].  What do you think?  Would you say that it is pretty much a complete answer to part(b)?  Thanks,
Comment: Oh, no...sorry, @Evgeny, I did not show that other matrices do not yield the identity matrix, when exponentiated... hmm...
Comment: Ok, got it -- thanks so much @Evgeny :-)
Answer:
I know that online sources such as Wikipedia and Wolfram just state without any proof or extended discussions that the matrix exponential is well-defined and converges for any square matrix.

$\quad$ Every matrix has an element of maximal size. $($Obviously, if anything can cause
divergence, it's that one$).~$ Let its absolute value be $M.~$ So let us construct a square
matrix S, whose every single element is $M.~$ Then $S^k=\Big(n^{k-1}M^k\Big)_{n\times n}~,~$ and each
element of $A^k$ lies between $\pm~n^{k-1}M^k.~$ But $~e^S\approx\bigg(\dfrac{e^{nM}}n\bigg)_{n\times n}~,~$ so every element
of $e^A$ is definitely bounded. However, even in this case divergence could still theoret–
ically happen, if at least one such element $($not necessarily the same$)$ were to freely
oscillate inside a given range, without actually converging to any particular value
within that interval. But this is not possible, since each new term of the infinite series
decreases at an exponential rate, being trapped between $\pm~\dfrac{n^{k-1}M^k}{k!}.$
Comment: @LebronJames: Just take a simple $2\times2$ or $3\times3$ matrix, and square it. What do you get in each cell ? $M\cdot M+M\cdot M+\ldots+M\cdot M$, *n* terms in total, meaning $n\cdot M^2$. Multiply it again by itself, and see what happens. Also, it goes on without saying, that if some terms were smaller than *M*, and positive, then the sum of products would be smaller than $n\cdot M^2$. And if they are negative, and less than *M* in absolute value, than it's even "worse", since they not only add less, but outright cancel the contribution of the other positive terms.
Comment: Hi @Lucian, can you please elaborate on how you arrive at $n^{k-1}M^k$ in your first equation?  And how does the approximation of $e^S$ show the boundedness of $e^A$?  Thanks,
Comment: Ok, got it -- thanks so much @Lucian :-)
Answer: The fact that the series for $e^A$ is easy to prove using matrix norms.  In particular, you should try to prove the inequality
$$
\left\|e^A \right\| \leq e^{\|A\|}
$$
if $\|\cdot\|$ is a norm (such as the Frobenius norm) which satisfies $\|AB\| \leq \|A\| \cdot \|B\|$ for all matrices $A,B$.

It is, in fact, a well known result that a matrix $A$ will be such that $e^{tA}$ is orthogonal for every $t \in \Bbb R$ if and only if $A$ is skew-symmetric, which is to say that $A^T = -A$.  This is a commonly used result in the context of Lie Groups and Lie Algebras.
At the very least, you should try to prove that if $A^T = -A$, then $e^A$ is orthogonal.
I am not sure whether there are any other matrices $A$ for which $e^A$ is orthogonal.

A matrix $A$ such that $e^A$ is orthogonal but $A\neq A^T$:
$$
\pmatrix{0&1\\0&2\pi i}
$$
or better yet
$$
\pmatrix{0&-1\\ 4 \pi^2 & 0}
$$
Comment: For a **real** example where $e^{A} e^{A^T} = e^{A^T} e^A$ but $e^A e^{A^T} \ne e^{A+A^T}$, try $$ A = \pmatrix{0 & 0 & 1 & 0\cr 0 & 0 & 0 & 1\cr 0 & 0 & 0 & -2\pi\cr 0 & 0 & 2\pi & 0\cr}$$
Comment: @LebronJames Orthogonal matrices do commute with their transpose, because a matrix is orthogonal iff its orthogonal is: $AA^\top=I=A^\top A$.
Comment: I think it's an interesting question. Jordan normal form doesn't help, but Schur decomposition might.
Comment: See my latest edit. Still not sure how to classify all such matrices, though
Comment: Slightly more generally, if $a \ne c$,
$$\exp\pmatrix{a & b\cr 0 & c\cr} = \pmatrix{e^a & \dfrac{b (e^c - e^a)}{c-a}\cr 0 & e^c}$$
so this gives an example of a non-normal matrix whose exponential is normal if $a \ne c$ but $e^a = e^c$, i.e. $c - a = 2 \pi i n$ for some nonzero integer $n$.
Comment: @Omnomnomnom Well, all sources that are known to me have the statement in form "$\lbrack A, B \rbrack = 0 \Rightarrow e^A e^B = e^B e^A = e^{A+B}$". I think it means that there is some counterexample to the statement $e^B e^A = e^A e^B = e^{A+B} \Rightarrow \lbrack A, B \rbrack =0$. Also, the real assumption that I suggested Lebron James to make is that from $e^{A} e^{A^T} = e^{A^T} e^{A}$ follows $e^{A} e^{A^T} = e^{A+A^T}$.
Comment: @Evgeny if you have a complex matrix that you want to make real, replace the entry $a+bi$ with the block
$$
\pmatrix{a&-b\\b&a}
$$
an oldie, but a goodie.  I've also added a real example of my own to the end of my answer.
Comment: @Omnomnomnom Wow, I forgot about this trick :) thanks!
Comment: Hi @Omnomnomnom, yes, with the help of Evgeny (see the comments in this question post), I was able to show that requiring A to be anti-symmetric yields an orthogonal matrix exponential $e^A$.  However, we needed to make a pretty big assumption, I think:  we assumed that A was normal, so that A commutes with its transpose - this allowed us to use the exponential rule $e^Ae^{A^t} = e^{A+A^T}$.  Setting this equal to the identity matrix forces A to be anti-symmetric. I don't know of any other way to provide a proof, without this assumption.  What do you think?  Thanks,
Comment: Thanks as usual for your answer :-)
Comment: Hi @anderstood, yes agreed.  But what I am asking is really this:  if $e^Ae^{A^T} = e^{A^T}e^A$, then this shows that the matrix exponentials commute -- but from this, must $A$ and $A^T$ *also* commute?  If they do, then my proof is complete.  What do you think?  Thanks,
Comment: @RobertIsrael Great, thank you very much! It also would be very interesting and educational (at least for me) if you have shared the insight that helped to find this example :)
Comment: Hi @Omnomnomnom, I read up a little bit on your last comment - turning a complex matrix into a real matrix, and going from a complex entry into a 2x2 real block.  (I computed an explicit example a couple nights ago, but very mechanically.) Apparently, this change from a complex matrix to a real matrix is an operator isomorphism.  Does this automatically mean that if the complex matrix is invertible, then the corresponding real matrix is also invertible?  And if the complex matrix is similar to another complex matrix, then the two corresponding real matrices will again be similar?
Comment: The notes don't mention this after stating that the change from complex to real is an isomorphism, so I am just wondering whether you have a quick, intuitive answer to offer.  Thanks, @Omnomnomnom
Comment: Hi @Omnomnomnom -- I'll actually post this as a new question on MSE now.  Thanks,