Should I test for all the OLS assumptions for a pooled OLS from panel data?

Question: As the title says, I need to perform a Pooled OLS, a Fixed effects and a Random effects analysis.  In the case of a normal OLS, one should test for normality, collinearity, homoscedasticity, linearity, etc.
I have been following the steps described here, but I am not so sure if I should do that in my case.  My model is like the one described here.

Should I follow the same tests as in a normal OLS regression? If so, should I include the time dummy in the tests?
Comment: No by assumption if the individual specific effects exist, OLS estimators appear bias and inconsistent. Therefore, introduce diferential dummies to eliminate these effects or demean the model
Answer: It's nice to test for normality, homoskedasticity, etc. but there are two more pressing issues with pooled OLS. The first is to test whether pooled OLS is consistent, i.e. your $x_{it}$ are uncorrelated with the fixed effects $c_{i}$. Of course they should also be uncorrelated with $u_{it}$ but that you cannot test so it must hold by assumption. Concerning the fixed effects, you can compare pooled OLS and FE regressions via the Hausman test as we discussed in another question earlier (remember that FE is consistent if $x_{it}$ and $c_{i}$ are correlated but pooled OLS is not). Also when you run the FE model it will display a test at the bottom of the regression table, F test that all u_i=0. If this is rejected you should favor FE over pooled OLS.
The second issue concerns the standard errors. As I understand, you have panel data on several banks. In pooled OLS the errors are likely to be serially correlated but you can (and should, according to Cameron and Trivedi (2009)) control for this by clustering the errors on the bank id variable. In Stata you can do this via
reg y x, cluster(id)

Where x includes your independent variables and the time dummies. If you don't use this correction, your standard errors will be very small and you are more likely to draw wrong inference. Since you have a large time component serial correlation will almost surely be there. But if you need a formal test for this you can use the xtserial command which can be downloaded via net install st0039 in Stata.
The steps described in the website you linked are nice to do but given the set-up of your regression analysis you already know that homoskedasticity and time independence in the errors are unlikely to happen. These ideal conditions rarely hold in applied work anyway but it's okay because we can correct this with the clustered standard errors. The same holds for normality. You should check for highly collinear variables as they might impact on the consistency of the estimator.
As a side-note I think a very good book for you would be Cameron and Trivedi (2010) "Microeconometrics Using Stata". It covers all of the methods you are working with, it discusses potential problems, how to diagnose and to solve them, etc. It's a bit pricy but perhaps you can find a copy in your university library. If you intend to do empirical work in the future it's definitely a good buy though.
Comment: Andy, I truly don't know how to thank you for this and all your other answers.  I was so puzzled by the whole OL thing! Please allow me 2 more questions.  When I do FE, clustered, the F(x,x) and F>p values disappear, is this normal? Also, I am not seeing this: F test that all u_i=0.  Look, this is what I get: http://imgur.com/AzV70Ve.  Is that last test that "test whether pooled OLS is consistent, i.e. your xit are uncorrelated with the fixed effects ci"? // I'm getting the book now, I have several other, but some are too technical and I get lost, I hope this one is easier to understand :)
Comment: Ah yes, I forgot that this F-test for u_i=0 only holds under homoskedasticity (try without the cluster option but either way the proper comparison test should be the Hausman test - with xtoverid you can do this also with cluster robust errors). The Hausman test is to see whether pooled OLS is consistent. The missing F value for the model is okay. In Stata the F(x,x) should be in light blue color - you can click on it and an explanation will appear as to why the F statistic is missing. I'm glad you are finding my answers useful :)
Comment: Thanks again! Isn't the Hausman test to compare FE with RE? The F(x,x) is in blue color, and I read the help, but I didnt understand wheter it was ok that it was missing or not, until now.
Comment: Hausman is a very general test, that's why it became so famous. You just have to have a consistent estimator (FE in this case) and you must be able to rank the variances (Var(FE)>Var(OLS)) to compare two models with Hausman. Concerning the missing model F, the help says: "Your estimation results show an F or chi2 model statistic reported to be missing. Stata has done that so as to not be misleading, not because there is something necessarily wrong with your model."
Comment: OK, nice.  I didn't know I could use it to compare with OLS too, everywhere I've read it always is mentioned for comparing FE and RE.  Regarding the Stata help, I read that, but since I didn't know it was dependent on whether the data was homoscedastic or not, I thought that anyway was there something wrong with my model.

Thank you again Andy, I'll compile all your answers now and try again.  Hopefully this time I'll be able to get this done!  Have a great Sunday!


Memory leak in tomcat

Question: My web application works in tomcat server, but after som time memory usage start to grow and grow until server goes down.
This happens on live only and it did not happened when I try to reproduce this situation locally. I tried to profile remotely with JProfiler, and found a lot of byte[] before server went down. The server goes down 2-3minutes after it starts to take more and more memory so I can't see what items use byte[] in code because Heap walk take more time (web app is really big).
I will be grateful for any help or suggestion.
Comment: Add `-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="/tmp"` to your `java` arguments, wait for the dump and import in JProfiler
Answer: suggestion: put a few memory usage dumps in your classes where are the caching storage fields with HashMap, Vector ( or unsynchronized versions of those collections) usually those keep references. Than I would put a plus url, which will dump my variable, like:
Runtime.getRuntime().callMemoryFunctionForgotName(): used memory is  270mb... for eg.
-here you should see WHEN is growing, over the time there are actions and one of those has leak.

when you have detected the link43 with action9 will produce memory leak than is easy :)
just add a few hashMap.count() or size()  (forgot this too , lol) to logg, than you will see which one of 20 collection is increasing and not decreasing. Hopefully you have acces to live server logs.

So I would do in 2 steps.

Memory leak in tomcat


Cakephp Localization, Cannot Change language when DEFAULT_LANGUAGE is set

Question: I am confused :)
I'm using the p18n component in cakephp found here:
http://www.palivoda.eu/2008/04/i18n-in-cakephp-12-database-content-translation-part-2/
This component requires me to set in core.php the following constant:

define("DEFAULT_LANGUAGE", 'eng')

However when this is set I cannot change the language using:

Configure::write('Config.language', 'eng');

At the moment, into my knowledge, the only way to change the locale of my static content is the use of the Configure::write. But for the dynamic content to change through the use of the p28n component I must have the DEFINE_LANGUAGE constant set to a value.
This is all very confusing. Any help will be much appreciated.
Answer: I'm not familiar with particular component, but I've done this "manually" by setting the same constant in my app/config/bootstrap.php file and then setting the "actual" language to be used in my AppController (copied from the core code to app/app_controller.php). The appropriate snippets of that controller look like this:
uses ( 'L10n' );

class AppController extends Controller {
  public function beforeFilter() {
    $this->_setLanguage();
    /**
     * Set the default "domain" for translations. The domain is the
     * same as the po file name in a given locale directory. e.g.
     * __d ( 'homepage', 'message_id' ) would look for the
     * message_id key in homepage.po. Using the __() convenience
     * function will always look in default.po.
     */
    $this->set ( 'domain', 'default' );
  }

  private function _setLanguage() {
    $this->L10n = new L10n();

    # Auto-detect the request language settings
    $this->L10n->get();
  }
}

Pretty vanilla stuff, but it works great. And breaking out the _setLanguage() method allows for the use of different methodologies to determine locale (e.g subdomain like fr.mydomain.com).
Comment: thanks man I'll give it a try and she how it goes.
Is there any reference to the use of L10n in cakphp? I could not find one.
Comment: There surely is. Take a look at i18n and l10n chapter of the docs (http://book.cakephp.org/view/161/Internationalization-Localization). Much of what I did is pulled straight from there. Originally, I had built my stuff to key off of the subdomain, but ended up stripping a lot of that back for simplicity.
Comment: Hey thanks for the answer. I cannot find the code you're using in the cakephp book. Actually I cannot get any definitions from search results for the 'L10n'.
Comment: Did you click the link I included in my comment? It takes you right there. No "finding" required. I don't know that the section includes any code, but the ideas I applied came from there.


Finding integer solution of congruence equation

Question: Find all integer solution $x$ which are solutions to the following congruence relation:

$243x+17=101$  (mod 725)

In this question, I can only figure out 243 and 725 are relatively prime. But I have no any idea on it.
Answer: $243x=725y+84\iff81\cdot3~x=81~(9y+1)-(4y-3)\iff4y-3=81(9y-3x+1)$, at which point it's hard not to notice that $81+3=84=4\cdot21$. Can you take it from here ? :-$)$
Answer: Hint:   $x=243^{-1} \cdot (101-17) \mod 725$.


what makes two networks different?

Question: When can we say two networks are different from each other? Is it when they have different subnet masks or when they use different policies? Will it be right to say that a router is needed to connect two 'different' networks?
Answer: Definitely. That's what a router is by definition. You can make your own study on what the difference between a router and a switch is.
As for the first part of your question, basically, networks are different if they do not connect directly. So two subnets of one net may be considered different and separate, as they will need a router to bridge them.
Answer: client 1 : 192.168.0.10
client 2 : 192.168.1.10
are those clients in the same network ?
this depends on the netmask they are using.
If they are set to use :
255.255.255.0 : then they are not on the same network
255.255.0.0   : then they are on the same network
It is however right to say "a router is needed to connect 2 different networks" , this would then be called "routing"
see the following :
A /24 network may be divided into the following subnets by increasing the subnet mask successively by one bit. This affects the total number of hosts that can be addressed in the /24 network (last column).
Prefix size     Network mask    Available
subnets     Usable hosts
per subnet  Total
usable hosts
/24     255.255.255.0   1   254     254
/25     255.255.255.128     2   126     252
/26     255.255.255.192     4   62  248
/27     255.255.255.224     8   30  240
/28     255.255.255.240     16  14  224

/29     255.255.255.248     32  6   192
/30     255.255.255.252     64  2   128
/31     255.255.255.254     128     2 256
table can be found on :
https://en.wikipedia.org/wiki/Subnetwork

what makes two networks different?


Somar o desmembramento de número

Question: Boa tarde pessoal,
Tenho uma questão que não estou conseguindo resolver. Sou iniciante em programação e começando com Javascript.
Tenho um número inteiro, preciso pegar todos os números menores e iguais a ele e fazer a soma. Ex: número = 5. A soma da questão seria essa: 5+4+3+2+1.
Escrevi da seguinte maneira:
function somaNumero(numero) {
    var numero = i;
    var numero = (i * (i - 1))/2;
}
console.log(somaNumero);

Comment: Já pensou em um loop?
Comment: Marcelinha, o somatório de Gauss para se determinar a soma de todos os naturais até determinado número é dada por `function somaNumero(n) {
    return (n * (n + 1)) /2;
}

console.log(somaNumero(5));`
Comment: Leia: [Que erro eu cometi fazendo minha pergunta?](https://pt.meta.stackoverflow.com/questions/8388/que-erro-eu-cometi-fazendo-minha-pergunta)
Comment: definiu duas vezes a variavel numero. Não definiu a variavel i. Não passou o valor na chamada da função  Faça assim, essa linha é desprezível var numero = i; portanto elimine-a. Mude os i por numero e troque o sinal - por sinal + Chame a função passando o valor console.log(somaNumero(5)); Essa solução está na abordagem matemática da minha resposta
Answer: Um loop do numero passado para a função

function somaNumero(numero) {
  var total = 0;
    for(var i = 1; i <= numero; i++){
      total += i;
    }
    return total;
}

//chama a função passando o numero
console.log(somaNumero(5));

O problema com a implementação acima é que, conforme o número aumenta, aumenta também o número de iterações.

Usar uma abordagem matemática para encontrar a soma de N números pode remover completamente o uso do loop for. Vamos tentar resolver o problema acima com uma abordagem matemática.
1 + 2 + 3 + 4 + 5 + 6 + …… .. + n

Vamos supor que a soma das séries acima seja total .
total = 1 + 2 + 3 + 4 + 5

Agora vamos inverter a série e adicionar os números de trás para frente, o que também dará o mesmo total.
total = n + (n-1) + (n-2) + (n-3) + (n-4)

Vamos somar os dois totais
total + total = (n + 1) + ((n-1) + 2) + ((n-2) + 3) + ((n-3) + 4) + ((n-4) + 5)

2total = (n+1) + (n+1)+ (n+1)+ (n+1)+ (n+1)

2total = n(n+1)

total = (n(n+1))/2

ou seja (numero * (numero + 1)) / 2
Resultado

function somaNumero(numero) {
  return (numero * (numero + 1)) / 2
}

console.log(somaNumero(5));

Para aqueles que sabem o que é uma Progressão Aritmética – PA , basta observar que a fórmula da soma dos n primeiros termos da PA (a1,a2,a3,…,an) é dada por
Sn=a1+a2+…+an=((a1+an)⋅n)/2
Como (1,2,3,4,…,n) é uma PA, de razão 1, segue que a soma dos n
primeiros termos é dada por:
   (1+n)⋅n
  _________
      2

total = ((n+1)⋅n)/2

Somar o desmembramento de número


Finding sampling distribution of normal MLE and likelihood

Question: I'm reviewing old exams in preparation for a statistics final, and I'm stuck on a particular question:
Suppose that you have n independent random variables $Y_i$, with each distributed normal with expected value $\beta x_i$ and known variance $\sigma^2$. Further suppose that the $x_i$ are known constant values, and define the likelihood as $\mathcal{L}(\beta; y) = \frac{f(y ; \beta)}{f(y ; \hat{\beta})}$, where $\hat{\beta}$ is the maximum likelihood estimate for $\beta$.

Show that $\hat{\beta} = \frac{\sum_{i=1}^n x_i y_i}{\sum_{i=1}^n x_i^2}$
Find the sampling distribution of the MLE
Show that the sampling distribution of $-2\ln(\mathcal{L}(\beta; y))$ is Chi-squared with 1 df

I had no problem with (1), but I'm unsure about (2) and (3).
For (2), I rewrote (1) as $\sum_{i=1}^n k_i y_i$ where the $k_i$ are constants and equal to $\frac{x_i}{\sum_{i=1}^n x_i^2}$. I then used linearity of expectation to say that the expected value of the sampling distribution is $\sum_{i=1}^n k_i \beta x_i$. For the variance, I believe it would be $\mathrm{Var}(\sum_{i=1}^n k_i y_i) = \sum_{i=1}^n \mathrm{Var} (k_i y_i)$ because of independence. Therefore, the variance of the sampling distribution should be $\sigma^2 \sum_{i=1}^n k_i^2$. Since the $y_i$'s are normal, their sum should be normal, so the sampling distribution is $N(\sum_{i=1}^n k_i \beta x_i, \sigma^2 \sum_{i=1}^n k_i^2)$.
For (3), I'm not sure where to start. Using the definition of $\mathcal{L}(\beta; y)$ above, I simplified to be
$$\mathcal{L}(\beta; y) = \exp \left[ \frac{1}{2\sigma^2} \left(2\beta \sum_{i=1}^n x_i y_i - \beta^2 \sum_{i=1}^nx_i^2 - \frac{(\sum_{i=1}^n x_i y_i)^2}{\sum_{i=1}^n x_i^2}  \right)  \right]$$ So $-2\ln \mathcal{L}(\beta; y)$ would be
$$ \frac{-1}{\sigma^2} \left(2\beta \sum_{i=1}^n x_i y_i - \beta^2 \sum_{i=1}^nx_i^2 - \frac{(\sum_{i=1}^n x_i y_i)^2}{\sum_{i=1}^n x_i^2}  \right)$$ but I'm not seeing how this is a chi-squared distribution with 1 degree of freedom.
I realize this is a long post, but I'd appreciate any hints or clarifications on either subproblem.
Answer: You need to simplify (2) much more.
Recall that a linear combination of independent normals is itself normal; namely if $Y_i \sim \operatorname{Normal}(\mu_i, \sigma_i^2)$, then $$\sum c_i Y_i \sim \operatorname{Normal}\left( \sum c_i \mu_i, \sum c_i \sigma_i^2 \right).$$  So if our realizations $y_i$ are drawn from $Y_i \sim \operatorname{Normal}(\beta x_i, \sigma^2)$, then the choice $c_i = x_i$, $\mu_i = \beta x_i$, $\sigma_i = \sigma$ immediately gives $$\sum x_i y_i \sim \operatorname{Normal}\left( \beta \sum x_i^2, \sigma^2 \sum x_i^2 \right).$$   Therefore $$\hat \beta = \frac{\sum x_i y_i}{\sum x_i^2} \sim \operatorname{Normal}\left( \beta, \frac{\sigma^2}{\sum x_i^2} \right).$$
To motivate part (3), write $$\mathcal L(\beta \mid \boldsymbol y) \propto \prod f(y_i \mid \beta) \propto \exp \sum -\frac{(y_i - \beta_i x_i)^2}{2\sigma^2}$$ hence $$-2 \ell(\beta \mid \boldsymbol y) \propto \sum \left(\frac{y_i - \beta_i x_i}{\sigma}\right)^2.$$  What is the distribution of this?  Since $y_i \sim \operatorname{Normal}(\beta_i x_i, \sigma^2)$, it follows that $$\frac{y_i - \beta_i x_i}{\sigma} \sim \operatorname{Normal}(0,1),$$ and the sum of IID standard normals is chi-squared, but here we have the wrong degrees of freedom because we disregarded a lot of details in the interest of getting some understanding.  Based on the above, I think you should try your hand at doing the calculation while incorporating $f(\boldsymbol y, \hat \beta)$.
Answer: For (2), re-instate the expression for the $k_i$'s and see what this gives you (there was a typo in what you wrote, now corrected).
For (3) a strategy here would be to show that
$$-2\ln \mathcal{L}(\beta; y) = -2 \ln f(y ; \beta) +2\ln f(y ; \hat{\beta})$$
is the difference of two chi-squares, one with one degree of freedom less than the other (chi-squares are closed under addition and subtraction).
We have that
$$-2\ln \mathcal{L}(\beta; y) = \sum_{i=1}^n\left(\frac {y_i -\beta x_i}{\sigma}\right)^2 - \sum_{i=1}^n\left(\frac {y_i -\hat \beta x_i}{\sigma}\right)^2$$
Given the assumptions the first sum is a sum of $n$ independent standard normals, and so a chi-square with $n$ degrees of freedom. So what you have to show is that the second sum is a chi-square with $n-1$ degrees of freedom. Intuitively, we should expect so, because what you have is again a sum of  standardized normal random variables but by their estimated mean rather than the true one (so "you lose one degree of freedom"). This can be viewed as an application of the general form of  Cochran's Theorem. Can you take it from here?


Alpinejs: x-text not updating text on conditional change

Question: I have the following code which should, as far as I can see, update the text 'Copy to Clipboard' to 'Copied!' on the click of the element. The text is being copied to the clipboard, but the element isn't changing it's inner text. Any input would be appreciated.

             {{ 'Text to be copied' }}

Answer: The problem lies in the precopy variable.    The way you use it, it results in a global variable (window.precopy) and by default the only variables watched  must be defined in the Alpine object.
The solution is to declare precopy in x-data while x-init can be removed:

         {{ 'Text to be copied' }}


How can Drupal be made to serve with root-relative paths?

Question: I'm trying to avoid serving pages that load css and js resources through URLs that specify either http or https.  (I'm trying to find a workaround for the problem described in this question, in case a real solution cannot be found.)
Because Drupal is specifying http:// for the href for all the CSS and JS files, when the server erroneously switches to https, as described in the aforementioned question, the browser seems to be ignoring all these files, and I get an unstyled page.
Instead of
<link type="text/css" rel="stylesheet" href="http://www.example.com/sites/default/files/css/css_pbm0lsQQJ7A7WCCIMgxLho6mI_kBNgznNUWmTWcnfoE.css" media="all" />

(etc.)
I'd like to serve
<link type="text/css" rel="stylesheet" href="/sites/default/files/css/css_pbm0lsQQJ7A7WCCIMgxLho6mI_kBNgznNUWmTWcnfoE.css" media="all" />

or even
<link type="text/css" rel="stylesheet" href="//www.example.com/sites/default/files/css/css_pbm0lsQQJ7A7WCCIMgxLho6mI_kBNgznNUWmTWcnfoE.css" media="all" />

Can I make Drupal serve URLs that don't specify protocol?
Answer: You can use a template process (YOUR_THEME_process_html) hook to rewrite all resource urls (js and css) using a regular expression.
Here is an example which transforms all http:// or https:// to //:
function YOUR_THEME_process_html(&$vars)
{
    foreach (array('head', 'styles', 'scripts') as $replace) {
        if (!isset($vars[$replace])) {
            continue;
        }

        $vars[$replace] = preg_replace('/(src|href|@import )(url\(|=)(")http(s?):/', '$1$2$3', $vars[$replace]);
    }
}

Comment: This worked like a charm for me. We too are behind an F5 server and are using it to make sure all requests are done through https.
Comment: This worked for me, unfortunately it doesn't appear to affect img tags (even adding img to the array) so I'm still gettting issues with this. It's odd that Drupal will load all resources over HTTP even when it's HTTPS.
Comment: `img` are not part of the `$vars` variables passed from the `YOUR_THEME_process_html`. The images you are trying the alter the source URL, are they part of the content ?
Comment: Worked like a charm! For those wondering how to apply this conversion to links in the page content, add `'page'` to the array in the `foreach` loop.
Answer: This has been fixed in Drupal 8. In Drupal 7, there is an issue about fixing it.
Answer: We had that same issue with a website we were developing and could not come to a resolution on why some of the paths would be https while others would be http.  The work around, which you may not like the answer, was to use the Pathologic module which turns everything from an absolute to relative so your forms would use a /path-to-form instead of http://www.whatever.com/path-to-form.
Comment: Since pathologic is an input filter, it AFAIK only affects content, not the URLs that Drupal generates.  So I don't see how this can help here either.


Dealing with mis-escaped characters in JSON

Question: I am reading a JSON file into Python which contains escaped single quotes (\'). This leads to all kinds of hiccups, as nicely discussed e.g. here. However, I could not find anything on how to address the issue. I just did a
newstring=originalstring.replace(r"\'", "'")

and things worked out. But this seems rather ugly. I could not really find much material on how to deal with this kind of thing (creating an exception, or something) in the json docs either.

Is there a good, clean procedure for such an issue?

Going back to the source is not possible, unfortunately.
Thanks for your help!
Comment: The `\'` character sequence is indeed invalid JSON, because there is no such escape sequence in JSON. How did you produce this output in the first place?
Comment: Do consider filing a bug report to that project in that case.
Comment: Hi Martijn, I did not produce it at all, just working with what was given to me.  Apparently it was created with "Export to JSON plugin for PHPMyAdmin".
Comment: Sure, I just thought it was probably a problem with user settings (I've never worked with PHPMyAdmin). This seems like a straightforward thing for a *json plugin* to get right? I'll look into it.
Answer: The JSON standard defines specific set of valid 2-character escape sequences: \\, \/, \", \b, \r, \n, \f and \t, and one 4-character escape sequence to define any Unicode codepoint, \uhhhh (\u plus 4 hex digits). Any other sequence of backslash plus other character is invalid JSON.
If you have a JSON source you can't fix otherwise, the only way out is to remove the invalid sequences, like you did with str.replace() even if it is a little fragile (it'll break when there is an even backslash sequence preceding the quote).
You could use a regular expression too, where you remove any backslashes not used in a valid sequence:
fixed = re.sub(r'(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})', r'', inputstring)

This won't catch out an odd-count backslash sequence like \\\ but will catch anything else:
>>> import re, json
>>> broken = r'"JSON string with escaped quote: \' and various other broken escapes: \a \& \$ and a newline!\n"'
>>> json.loads(broken)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.5/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid \escape: line 1 column 34 (char 33)
>>> json.loads(re.sub(r'(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})', r'', broken))
"JSON string with escaped quote: ' and various other broken escapes: a & $ and a newline!\n"

Answer: The right thing would be to fix whatever is creating the invalid JSON file. But if that's not possible, I guess the replace is needed. But you should use a regular expression so it doesn't replace \\' with \' -- in this case the first backslash is escaping the second backslash, they're not escaping the quote. A negative lookbehind will prevent this.
import re
newstring = re.sub(r"(?<!\\)\\'", "'", originalstring)

Answer: The solution is not bad. It seems ugly because the problem is ugly - you have corrupt data. It's certainly simple, elegant, and effective. It will only fail if the substring \\' (that's three characters, I'm not escaping anything) is present anywhere, and even then only if the number of consecutive slashes is even. So your options are:

Just do your current thing, but first check if r"\\'" in originalstring and throw an error if so. Easy, safe, probably fine.
Use a regex with a negative lookbehind for (\\\\)+ or something.
Catch errors and use the attributes of the errors to decide on a portion of the string to replace.

Check out this snippet:
import json
from json.decoder import JSONDecodeError

s = r'"\'"'
print(s)
try:
    print(json.loads(s))
except JSONDecodeError as e:
    print(vars(e))

Output:
"\'"
{'msg': 'Invalid \\escape', 'colno': 2, 'doc': '"\\\'"', 'pos': 1, 'lineno': 1}

Comment: Only in Python 3 does the exception have those attributes; in Python 2 all you'll get is a `ValueError` exception, and you are stuck with parsing the text message for this info.
Comment: You can use `json.JSONDecodeError` by the way, you don't need to import it from `json.decoder`.
Comment: Yes, Python 2 gives me a `ValueError: Invalid \escape`:


Knowing A and the discriminant, how to find B and C?

Question: On a Quadratic equation of the form $ax^2+bx+c$, I am given the discriminant ($\Delta=b^2-4ac$) and A.
To find B and C, I tried to solve it through $x1$ and $x2$...:

$x1 = \frac{-b-\sqrt{\Delta}}{a}$
$x2 = \frac{-b+\sqrt{\Delta}}{a}$

...using those properties:

$x1 + x2 = - \frac{b}{a}$
$x1 \times x2 = \frac{c}{a}$

Without any success...
So, is it possible to retrieve B and C?
Two additional properties:

the discriminant is always positive
B is always negative
Comment: Welcome to MSE. You'll get a lot more help if you show that you have made a real effort to solve the problem yourself, even if you haven't made much progress. What are your thoughts? What have you tried? How far could you get? Where are you stuck? This question will likely be closed if you don't add more context. Please respond by editing the question body. Clarifications don't belong in the comments.
Comment: Don't you think that you could recall that the quadratic equation is $Ax^2+Bx+C=0$ ?
Comment: Answer: No. Details on demand once you have shown that you have worked...
Answer: One equation
$$\Delta=B^2-4AC\tag{1}$$
with 2 unknowns $B$ and $C$: there is a fundamental indeterminacy.
A counterexample among many: If $A=1/4$ and $\Delta=16$, (1) becomes:
$$16=B^2-C$$
which is possible for $(B=-4, \ C=0)$, $(B=-5, \ C=9)$, $(B=-6, \ C=20)$, etc. (I have taken into account the constraint on $B$ to be negative).
Otherwise said,
$$\frac14x^2-4x=0, \ \ \ \frac14x^2-5x+9=0, \ \ \ \frac14x^2-6x+2\sqrt{5}=0$$
have all the same $A$ and the same $\Delta$ without having the same $B$ and the same $C$.
Comment: I computed the $\Delta$ ($b^2-4ac$) for those 3 equations and they don't share the same one
Comment: $\Delta1 = (-4)^2-4\times\frac{1}{4}\times0 = 16$   ,  $\Delta2 = (-5)^2-4\times\frac{1}{4}\times9 = 16$  , $\Delta3 = -6^2-4\times\frac{1}{4}\times2\sqrt5 = 31.52..$
Comment: Now corrected for $\Delta_3$ which is also equal to $20$, etc.
Comment: Thank you! I can see that they all share the $y$ but not for the same $x$.


In python, how do I replace a column of misspelled names with the correct names from another dataframe along with the columns of data in the 2nd one?

Question: I have two dataframes. df1 has official, clean data on a few hundred people. Data like names, addresses, gender, race, etc. And df2 is simply a list of people who attend various clubs so their names are potentially entered multiple times for various clubs. df2 was also entered manually, so there are some misspellings and nicknames. Everyone in df2 should have one entry in df1 that represents them.
I need accurate statistics regarding the club members. Fuzzywuzzy is really cool for being able to find approximate matches quickly, but it's almost certain to false positively and/or false negatively match a couple of people depending on the set cutoff point.
So, how do I replace the names in df2 with their closest match in df1 along with the columns of data in column 1 associated with them? Also, is there a simple, mathematical way I can find the right cutoff balance and how do I identify the most uncertain results after that?
My first strategy to solve this problem was to ask GPT-3.5 to do it for me (I know it's shameful, but I'm very much a coding novice. Also, GPT-4 rejects my credit card.). There were dozens of attempts made with differing results and minor adjustments made by myself. The code I'm getting usually works. But, since I'm no master coder, I'm just not confident about how to get the code to do exactly what I want. For example, it seems to be finding approximate matches, but not combining the dataframes how I want. Here's the code I used:
score_cutoff = 80

# Define a function to match rows based on similarity to all three columns
def match_rows(row, df):
    Name = row['Full Name']
    ID = row['ID Number']
    max_score = 0
    max_row = None
    for i, r in df.iterrows():
        score1 = fuzz.token_sort_ratio(Name, r['Full Name'])
        score2 = fuzz.token_sort_ratio(ID, r['ID Number'])
        avg_score = (score1 + score2) / 2
        if avg_score > max_score:
            max_score = avg_score
            max_row = r
    if max_score >= score_cutoff:
        print("df2 Row:\n", row)
        print('\n')
        print("Skyward Match:\n", max_row)
        print("Score:", max_score)
        print('\n------------------------------------------------------------\n')
        return max_row
    else:
        return None

# apply the match_rows function to each row in the inaccurate dataframe
matches = df2.apply(match_rows, axis=1, args=(df1,))

# create a new column in df2 indicating whether or not a match was found and the entry got replaced

df2['Matched']=False
for i, r in matches.iteritems():
    if r is not None:
        df2.loc[i, ['Full Name', 'ID Number']] = r[['Full Name', 'ID Number']
        df2.loc[i, 'Matched'] = True

# print the final matched dataframe
print(df2)

The code below "create a new column in df2" was added when I asked for a column that shows whether the df2 row found a match in df1 to replace it (I wanted to make sure all df2 entries got replaced with a validated name) but I'm not sure if that's working either. Any advice would be greatly appreciated and examples of code would be doubly so!
Edit: Here's a sample of what the dataframes look like per request.
Comment: Could you share how your df1 and df2 look like? No need for the full data, just a couple of rows with fake data to show the columns.
Comment: I've added the data frame examples as requested.
Comment: Does df2 contain just the name or both name and ID number?
Comment: It has both, so most people would probably say I could just look for exact matches among the IDs. But they are manually entered and I've noticed there are at least a few invalid entries that need to be corrected. Hence I'm trying to match by average similarity in both columns.
Answer: From my research, the best package to use for fuzzy matching at the moment seems to be rapidfuzz. It is actively maintained and has a complete documentation.
Code Snippet
The following is one approach to perform fuzzy match of each row in df2 to the population of df1.
from rapidfuzz import process, fuzz
import pandas as pd
from typing import Callable

df1 = pd.DataFrame.from_dict(
    {
        'Full Name': ['Patrickson, Jessica Gloria', 'Bennett, Franklin Scotty', 'Aguirre Rodriguez, Diego Andre'],
        'ID Number': ['7561863', '4863187', '6453152'],
        'Gender': ['F', 'M', 'M'],
        'Race': ['Caucasian', 'Caicasian', 'Caucasian'],
        'Hispanic': ['N', 'N', 'Y'],
        'Age': [23, 19, 29],
        'State': ['NY', 'UT', 'TX'],
        'City': ['New York City', 'Salt Lake City', 'Austin'],
    }
)

df2 = pd.DataFrame.from_dict(
    {
        'Full Name': ['Jessica Smith', 'Frank Bennett', 'Diego Rodriguez', 'Frank Bennet'],
        'ID Number': ['7561863', '4863187', '6453152', '4863187'],
    }
)

def custom_scorer(w_name: float, w_id: float, sep: str = '#') -> Callable:
    """Custom scorer for process.extractOne.

    The scoring depends on two scores: similarity for name and similarity of ID
    However, we can impart different weight on each score. For example, if we
    have more confidence on the accuracy of ID than name, we can give higher
    weight to the similarity of ID than name. In real practice, w_name and
    w_id shall be tweaked based on the data, but the two weights should sum up
    to 1.

    :param w_name: weight for name similarity
    :type w_name: float
    :param w_id: weight for ID similarity
    :type w_id: float
    :param sep: separator between name and ID in the query and choice strings,
        defaults to '#'
    :type sep: str, optional
    :return: a helper function that is passed to process.extractOne
    :rtype: Callable
    """

    def helper(query: str, choice: str, **args) -> fuzz.WRatio:
        """This helper function is the one returned by custom_scorer.

        It is also what is actually supplied to process.extractOne.

        :param query: query string, must be in the form of name + {sep} + ID,
            where the separator is supplied by custom_scorer
        :type query: str
        :param choice: choice string, must be in the same format as query
        :type choice: str
        :return: custom computed similarity score between query and choice
        :rtype: fuzz.WRatio
        """
        q1, q2 = query.split(sep)
        c1, c2 = choice.split(sep)
        r1 = fuzz.partial_token_sort_ratio(q1, c1)
        r2 = fuzz.ratio(q2, c2)
        return w_name * r1 + w_id * r2

    return helper

def match(
    row: pd.Series,
    population: pd.Series,
    df: pd.DataFrame,
    score_cutoff: int,
    w_name: float,
    w_id: float,
    sep: str,
) -> pd.Series:
    """Match the best row in population for the given row.

    :param row: a row in the dataframe to match
    :type row: pandas.Series
    :param population: population from df1 as a list of strings. Each person
        is represented by their full name and ID number concatenated together
    :type population: pandas.Series
    :param df: dataframe where the population is acquired from.
    :type df: pandas.DataFrame
    :param score_cutoff: the minimum score to consider a match
    :type score_cutoff: int
    :param w_name: weight for name similarity
    :type w_name: float
    :param w_id: weight for ID similarity
    :type w_id: float
    :param sep: separator between name and ID in the query and choice strings,
    :type sep: str
    :return: the best match row from df for the current row. If a match is not
        possible, return the current row.
    :rtype: pandas.Series
    """
    m = process.extractOne(
        row['Full Name'] + sep + row['ID Number'],
        population,
        scorer=custom_scorer(w_name, w_id, sep=sep),
        score_cutoff=score_cutoff,
    )
    if m is not None:
        return df.iloc[m[2]]  # matched row index in population
    return row  # match failed

# These are the params to tune
score_cutoff = 80
w_name = 0.4
w_id = 0.6
sep = '#'

# info of all the people to match, obtained from df1 and converted into a
# list of strings
population = df1[['Full Name', 'ID Number']].apply(
    lambda row: row['Full Name'] + sep + row['ID Number'], axis=1,
)

# Find the matching row in df1 for each row in df2, stored in df3
df3 = df2.apply(
    match, axis=1, args=(population, df1, score_cutoff, w_name, w_id, sep),
)
df3 = df3[df1.columns]  # reorder columns to match df1

print(df3)

The resulting df3 is:
                        Full Name ID Number Gender       Race Hispanic  Age  \
0      Patrickson, Jessica Gloria   7561863      F  Caucasian        N   23
1        Bennett, Franklin Scotty   4863187      M  Caicasian        N   19
2  Aguirre Rodriguez, Diego Andre   6453152      M  Caucasian        Y   29
3        Bennett, Franklin Scotty   4863187      M  Caicasian        N   19

  State            City
0    NY   New York City
1    UT  Salt Lake City
2    TX          Austin
3    UT  Salt Lake City

Explanation

The basic tool used is process.extractOne, which does all the heavy lifting of finding the best match row in df1 for the current row in df2. The majority of the code given by ChatGPT in the OP is doing process.extractOne manually, with much worse performance of course.
The logic of matching is customized (see custom_scorer). Since we have two columns to match, yet rapidfuzz only allows for direct string to string match, we first concatenate the two strings together (see the creation of population), with a separator between name and ID. And then split them up during the actual scoring process (see the helper function inside custom_scorer).
We can obtain two similarity scores, one for name and the other for ID. We can take average of them, which essentially is imparting the same weight on these scores. However, empirically, the likelihood of making mistakes in recording name and ID are different. Thus, their weights shall be different as well. Exactly what weight should be given to name similarity and ID similarity is open for discussion. Mostly, this is data-dependent. In other words, one has to use cross validation to find the best weight designation. In the example above, the weight of name similarity (w_name) is set to 0.4 and the weight of ID similarity (w_id) is set to 0.6 (i.e., we believe a match in ID can outweigh a mismatch in name).
The similarity scoring methods for name and ID are different. For name, fuzz.partial_token_sort_ratio is used, because name contains multiple tokens with possibly different ordering (as exemplified in the distinct ways first and last name are treated in df1 and df2). It makes sense to compare only the sorted unique tokens of names. For ID, fuzz.ratio is used, because ID shall contain only one token. So a direct fuzz.ratio suffices.

What's Next?

In the example above, there are three parameters that need tuning: score_cutoff, w_name, and w_id. One should perform cross validation to tune these parameters, and choose a set that fits the data the best.
If df1 or df2 is extremely large, one might want to investigate performance improvement. But most of the time, it should NOT be an issue if the population does not go into hundreds of thousands. Always keep in mind that "premature optimization is the root of all evil -- Donald Knuth".


UIBackgroundMode remote-notification doesn't work on iOS 7.1

Question: I'm having issues with the remote-notification background mode on iOS 7.1. I filed a bug on Radar (http://openradar.appspot.com/radar?id=5279828798341120), which I copied below for convenience.
Anybody else experiencing this issue? Any word on Apple's end?

Summary
Sending a push notification with the content-available tag is supposed to wake up the app in the background and call application:didReceiveRemoteNotification:fetchCompletionHandler. It works well on iOS 7.0.6, but not on iOS 7.1.1.
Steps to Reproduce

Create an empty iOS application project. Launching it should display a plain white view.
Ask for push permissions when the app starts, and print the push token in the console.
In the target settings (Capabilities -> Background Modes) enable Remote notifications.
In AppDelegate, implement application:didReceiveRemoteNotification:fetchCompletionHandler. Make it change the window's background color to red and NSLog("Application did receive remote notification").
Run the app from Xcode (Product -> Run).
Close the app from Xcode (Product -> Stop).
Double press the Home button: the multitasking screenshot of the app shows a plain white view.
With a program such as nomad-cli (http://nomad-cli.com/), send a push notification to your device with the "content-available" tag (option -n with nomad-cli): the push notification should appear on the Home screen.
Double press the Home button and check the color of the multitasking screenshot.

Expected Results
If the app was launched in the background and application:didReceiveRemoteNotification:fetchCompletionHandler was called, the multitasking screenshot should be red. Moreover, the console logs in the Xcode Organizer should display Application did receive remote notification.
Actual Results
On iOS 7.0.6, the multitasking screenshot is indeed red. Moreover, the console logs in the Xcode Organizer display the following logs:
<code>May 30 16:50:14 iPad kernel[0] <Debug>: launchd[266] Container: /private/var/mobile/Applications/47E7DA84-E83A-4B43-B550-7D203921BD49 (sandbox)
May 30 16:50:14 iPad Test[266] <Warning>: Application did receive remote notification
</code>
On iOS 7.1.1, the multitasking screenshot stays white, and the console logs in the Xcode Organizer don't display anything.
Version
iPhone 5: iOS 7.1.1 [11D201]. iPad 2: iOS 7.0.6 [11B651].
Configuration
iPhone 5 32GB AT&T, using WiFi. iPad 2 16GB, using WiFi.
Answer: This was a false alert. One of the devices I used for testing had background fetch disabled in the settings (Settings -> General -> Background App Refresh). I'm closing the issue.
Comment: Hi @Alexis. Disabling 'Background App Refresh' also disables background push notification fetch functionality?
Comment: Indeed, that was my experience.


Android MySQL Connection

Question: I am trying to connection with php-MySQL using below code
try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://127.0.0.1/mytable.php");
    // HttpPost httppost = new HttpPost("http://localhost/mytable.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
}catch(Exception e){
     Log.e("log_tag", "Error in http connection"+e.toString());
}

when control goes on httpclient.execute(httppost); it throws an exception:
org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1 refused

I also add <uses-permission android:name="android.permission.INTERNET" /> to my AndroidManifest.xml file.
When I test my php file directly from a browser, it works fine. Any suggestions?
Answer: From the emulator, il you want to access your physical computer, you must use the following IP address :
10.0.2.2

The one you used, 127.0.0.1, points to the machine inside which your code is executed -- i.e. the emulator itself.
As you don't have an HTTP server running on your Android emulator, your application cannot connect to it -- which explains the error message you get.

For more informations, you should read the following section of the manual : Network Address Space
Comment: Jinx - but your answer's better.
Comment: hi thanks . now I am facing java.net.SocketException: The operation timed out from same method  HttpResponse response = httpclient.execute(httppost); as I also set time out limit public static final int HTTP_TIMEOUT = 90 * 1000;  mHttpClient = new DefaultHttpClient();
            final HttpParams params = mHttpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
            ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
Comment: If you do not test from an emulator, but from a real device, you must not use 10.0.2.2 to access you physical computer -- but its real address on your network.
Comment: I follow 10.0.2.2 its removes Connection to http://127.0.0.1 refused exception . but now its giving exception as The operation timed out..where i am doing wrong ?
Answer: 127.0.0.1 is localhost, the current device. On your computer, it refers to your computer, but on a device or an emulator it refers to that device.
See this document on how to refer to the localhost of your computer when running within an emulator. Alternatively, you can change 127.0.0.1 to your computer's actual IP address.
http://developer.android.com/resources/faq/commontasks.html#localhostalias

Android MySQL Connection


Prove that a closed ball $B$ is closed and bounded in $(C[0,1], d^*)$

Question: Let $C[0,1]$ be the set of all continuous functions from $[0,1]$ into $\Bbb R$ and let $(C[0,1], d^*)$ be the metric space defined as $$d^*=sup\{|f(x) - g(x)|: x \in [0,1]\}$$  Let $$B = \{f: f \in C[0,1] \land d^*(f, 0) \le 1\}$$ where $0$ denotes the constant function from $[0,1]$ into $\Bbb R$ which maps every element to $0$.
Prove that the closed ball $B$ is closed and bounded in $(C[0,1], d^*)$.
I know this is bounded because $(\exists r > 0)(d^*(f_1,f_2) \le r)(\forall f_1,f_2 \in B)$ if we chose $r = 1$ as defined in $B$, but I'm not sure how I would prove this is closed.  I would assume showing that every sequence of functions converges to a function in $B$, or the compliment is open, etc.
Comment: Does $C[0,1]$ mean you are talking about continuous functions only? It changes a lot your problem...
Comment: I added that in the description.
Answer: If you know you're in a metric space $(X,d)$, any set of the form $D = \{x: d(p,x) \le r \}$, wheer $r > 0, p \in X$ is closed and bounded. Nothing special about $C([0,1])$. You can show the complement is open: if $d(q,p) > r$ then show using the triangle inequality that $B(q, d(q,p) - r)$ is disjoint from $D$.
Comment: I don't understand how I would show that they're disjoint with the triangle inequality.  If $B(q,d(p,q) - r)$ is an open ball of radius $d(p,q) - r$, then $B(q,d(p,q) - r) = \{x : x \in X \land d(q,x) < d(p,q) -r \}$.

So $d(x,q) < d(p,q) - r \land d(p,x) \le r$
Comment: Suppose $q'$ is in $B(q, d(p,q)-r) \cap D$. Then $d(q,p) \le d(q,q') + d(q',p) < d(p,q) - r + r = d(p,q)$, contradiction.


select all/none using hidden fields not quite working

Question: I'm trying to use hidden fields in a form to pass along state selections instead of checkboxes so that I can improve ui a bit and also not have to deal with the cumbersome posting of checkbox fields to classic asp server pages.
I created a test example of what I'm trying to do here: jsFiddle example of problem
I'm using the 2 character state text value of the div to update the value of the hidden fields.  When I hit the select all link, I'm check the value for '#statesTX' expecting it to be 'TX' but finding that it contains all of the text values for each div.
Is there a way to get that select all link to up date each hidden field with the text value of its individual parent div?
Answer: Yes, there is a way to update each one to its parent text:
<code>$('input[type=hidden]').each( function() {
   $(this).val($(this).parent().text());
});
</code>
Answer: Check below code and see if this is something that you want,
DEMO
<code>var $boxes = $('.boxes');

$('#lightall').click(function() {
    $boxes.each(function () {
        $(this).find('input[type=hidden]').val($(this).text());
    });
    $boxes.addClass('lights');
    alert('statesAL: ' + $('#statesAL').val());
});

$('#lightnone').click(function() {
    $('.boxes input[type=hidden]').val('');
    $boxes.removeClass('lights');
    alert('statesTX: ' + $('#statesTX').val());
});
</code>
Comment: You guys in SO land are amazing.  That worked perfectly!  I appreciate the look and answers!


How do I hide the code of my theme?

Question: Is it possible to hide the code of the theme that I have created for my client? In other words, when I open the page with firebug I dont want to see the HTML and all the CSS of my theme, also when I click with the right mouse button.
Comment: Not really a drupal issue. You can't stop someone from using firebug, and even if you did stop them from using right click, they could get around it with firebug.
Comment: Not a drupal issue.  Only way is a flash only site
Answer: No, the browser needs to download the markup and stylesheet to do its job, and you shouldn't try this kind of thing, It doesn't work and is annoying to site visitors.
Please read the answers on this similar StackOverflow question: https://stackoverflow.com/questions/6597224/how-to-hide-html-source

How do I hide the code of my theme?


Gps ou triangulação de antenas - Como saber a precisão da localização?

Question: Tenho um app que utiliza a api Location e funciona certinho na maioria dos aparelhos, porem tem um que mesmo ligado o gps não funciona com precisão adequada( passa por cima das casa e nao na rua).
Pelo teste que fiz ele usa a triangulação de antenas mesmo com o gps ligado.
Alguém sabe se existe uma maneira de validar a qual o tipo de localização o aparelho esta usando.
Se for com uma precisão baixa(triangulação de antenas) posso a partir disso traçar uma rota entre dois pontos. Se não utilizo a rota que o gps mostrou.
Comment: Mesmo o GPS não tem uma precisão 100%, depende muito do sinal da região, e do aparelho que está usando. E o GPS não é o único sistema de localização, existem outros que podem ate ser mais precisos dependendo da finalidade.
Comment: Se você estiver usando a primeira posição que o sistema te retornar, as chances são altas que seja uma triangulação de antena, que é obtida mais rápido que o GPS. Você pode validar a origem pelo método `Location.getProvider()` ou então pela accuracy como o @ramaral explicou.
Answer: Depois de obter uma Location pode usar o método getAccuracy() para obter a precisão estimada, em metros, dessa localização.
A precisão estimada é definida como o raio de 68% de confiança: se você desenhar um círculo centrado nesta latitude e longitude, com um raio igual à precisão, há uma probabilidade de 68% da verdadeira localização estar dentro do círculo.
Você pode usar esse valor para decidir se traça uma rota entre os dois pontos ou se utiliza a rota que o gps mostrou.
Quanto menor for o valor retornado pelo método getAccuracy() maior é a precisão da localização obtida.


Time based probability question

Question: A restaurant sees 70 customers in a 24 period based on historical patterns. However, 40 customers came in today between 9AM-11AM. What is the probability that in the next 22 hours that there will be atleast 30 more customers?
The assumption that the customer walk ins are uniformly distributed. Meaning for e.g. if the limit was 48 customers, we observe that there is two customers per hour uniformly across the day & night
Comment: This is one of those all too frequent problems where one is expected to make an assumtion that is patently unreasonable. Perhaps someone wrote a glowing review on Placebook.
Answer: It doesn't matter what happened from 9:00 to 11:00.  The probability of seeing at least $30$ customers in the next $22$ hours is the same as usual.  If you assume that the arrivals are Poisson-distributed, where the expected number in $22$ hours is $\lambda = 70\cdot 22/24 = 385/6 \approx 64.17$, then the probability of seeing at least $30$ customers in $22$ hours is
$$
P[X\ge 30] = \sum_{k=30}^{\infty} \frac{\lambda^k e^{-\lambda}}{k!} = 1 - \frac{\Gamma(30, \lambda)}{\Gamma(30)}\approx1-7.0825\times 10^{-7}.
$$
In other words, the busy morning almost guarantees a good day overall.
Comment: Thanks @ mjqxxxx - the answer is correct and based on Poisson distribution and can also be validated here - http://stattrek.com/online-calculator/poisson.aspx

Time based probability question


Dynamical system output - differential equations

Question: I am applying at a Faculty for Electrical Engineering, and I have an entrance exam in two days. I have few exams for exercise, from previous years and I keep getting stuck in one particular type of problem:
"A dynamicaI system is described by a differential equation of the following form:
$$y''(t) + 5y'(t)+4y(t) = u(t)$$
where t is the independent variable of time, $u(t)$ is the input signal into the system, and $y(t)$ is the system response."
I need to prove that if the system is excited with stepped amplitude signal of $1$, the output of the system in the steady state will reach a constant value of $0.25$.
Can anyone please suggest how can I solve this problem. I think I should use Laplace Transformation to solve it, but I do not know to how transform the above differential equation in some type that can be solved with Laplace. Also, i do not get where does the amplitude signal of $1$ is used.
Any help or hints are welcomed and appreciated, I need all the help I can get.
Thank you.
Comment: Maybe this would suit [ELECTRICAL ENGINEERING.SE](https://electronics.stackexchange.com) better?
Comment: Thank you for the suggestion, I did not know there is a page for Electrical Engineering. I will try there.
Comment: The question definitely belongs to math.SE. To solve this, why not use that every solution of the differential equation is $$y(t)=ae^{-t}+be^{-4t}+\frac13e^{-t}\int_0^tu(s)e^sds-\frac13e^{-4t}\int_0^tu(s)e^{4s}ds$$ for some constants $(a,b)$ depending on the initial conditions $(y(0),y'(0))$, and plug in the function $u(t)$ you are interested in, whatever it is? For instance, if $u(t)=1$ for every $t>0$, one gets a fully explicit formula for $y(t)$, from which the limit when $t\to\infty$ is direct...
Comment: An electrical engineer would probably do Laplace tranformation $$(s^2+5s+4)Y=\frac{1}{s}$$ then do the partial fractions, solve the system and ensure that the limit is as expected. Or maybe use the [final value theorem](https://en.wikipedia.org/wiki/Final_value_theorem) instead if allowed.
Comment: I started solving it like that and ended with $$Y=\frac{1}{4}-\frac{1}{3}e^{-t}+\frac{1}{12}e^{-4t}$$ , and I am stuck again.
Comment: @mchingoska Correct (except for that it is $y(t)$, not $Y(s)$). Now take the limit when $t\to +\infty$. Does it go to the steady state $1/4$?
Comment: @А.Г. Well when $t\to \infty$, then all exponential values in the above equation are equal to $0$. So I guess I am left with the limit of $\frac{1}{4}$, or when I divide $0.25$ (the result I need). Can you please just explain why do I need to find the limit when $t\to \infty$ ? Is it because it is a steady state?
Comment: @mchingoska A [steady state](https://en.wikipedia.org/wiki/Steady_state) is a state that nothing happens to the system at. An equilibrium. In this case,  $y(t)=\text{const}$. An aedonist has shown that it is $1/4$. Then you have to prove that the output does, indeed, go to it as $t\to\infty$. One way is to calculate the output and to take the limit. Anyhow, the behaviour as $t\to\infty$ is that of interest.
Comment: @A.Γ. Thank you for the answer.
Answer: To answer your question you do not actually need to solve the differential equation.
Indeed, in steady state conditions both $y´´$ and $y´$ will vanish: hence you are (asymptotically) left with the equation $$ 4 y(t) = 1$$ (as $u(t)$ is where the step input function comes into play) from which one concludes $$ y = 1/4 = 0.25$$
Comment: **Assuming** there is a steady state...
Comment: @KirylPesotski If you feed in the harmonic signal $e^{i\omega t}$ you will get your output $y_p(t)$ as a harmonic oscillation with no existing limit as $t\to\infty$. Your $y_s$ is not a steady state, but an amplitude (frequency response at $\omega$). I do not see your point.
Comment: in the harmonic case, the steady state solution does not exist!
Comment: @Did, needless to say you are right. I was just wondering  if the existence of a steady state was in some way implied by the question formulation itself, which I believe the OP adapted from the exam sheet, "I need to prove that if the system is excited with stepped amplitude signal of 1 , the output of the system in the steady state will reach a constant value of 0.25"
Comment: Yes, the steady state was given in the question formulation. I did not know that in steady state the condition y'' and y' vanish. @An aedonist, your answer helped me a lot, thank you very much.
Comment: @Did The steady state is clearly to exist after it has been calculated as $y(t)=1/4$. What I think is important here to add is that the steady state is, in fact, *stable*, so the output will asymptotically reach it as questioned and does not diverge.
Comment: @A.Γ. Right, I meant "assuming the solution converges to a steady state" (which seems to be assumed from the start, for reasons I cannot fathom since one can easily *prove* it).
Comment: @mchingoska, glad I could help, all the best for your exam
Comment: The steady state is defined as the $t\rightarrow\infty$ limit of the particular integral, the particular integral of this equation is just $1/4$ (with $u(t)=1$). So, in general, the above approach is invalid but yields the correct solution in this particular case
Comment: @Kiryl Pesotski, I am unsure I understand why the approach is invalid, could you please clarify? I would be grateful if, for example, you exhibited an example where the invalid approach yields a wrong answer.
Comment: Let $u(t)=e^{-i\omega{t}}$, the particular intgral is $y_{p}(t)=\frac{e^{-i\omega{t}}}{4-5i\omega-\omega^{2}}$, the steady state $y_{s}=\lim_{t\rightarrow\infty}y_{p}(t)=\frac{1}{4-5i\omega-\omega^{2}}$, in the particular case $\omega=0$, we recover the original equation and answer is $1/4$. Is it clear?
Comment: @Kiryl Pesotski, that is clear indeed, yet I disagree with your downvote. We were dealing specifically with the case $u(t) = H(t)$ as posed by the OP, where $H(t)$ stands for the step function, and I never claimed any more generality.
Comment: Yes, you're right, let it be $e^{-\lambda{t}}$, the particular integral $y_{p}=\frac{e^{-\lambda{t}}}{\lambda^{2}-5\lambda+4}$, thus $y_{s}=\frac{1}{\lambda^{2}-5\lambda+4}$. Sorry i've mistaken as wanted to quickly make the point...


access hadoop intermediate map output files

Question: Is it possible to access or read map intermediate output files i.e. sequential file- file.out ?
I want to read a file.out file. I used approach mentioned in this link, http://hadooptutorial.info/hadoop-sequence-files-example/   ,
but it says that file.out is not a sequential file.
Answer: If you want to read the output of the mapper use the following configuration argument while running the job
<code>-Dmapreduce.job.reduces = 0
</code>
It will set the number of reducers to 0, leading to 0 partitioners and only mappers result will be displayed in output directory.


Uniform convergence with uniform bound on second derivatives implies $C^1$ convergence.

Question: I am having some difficulties proving this fact, since I am not very familiar with this kind of exercises.
Let $f_n : \mathbb{R} \to \mathbb{R}$ a sequence of $C^\infty$ functions.
Suppose $f_n$ converge to some $f$ uniformly on compact intervals and that $f_n ''$ are uniformly bounded. Then $f_n$ converge to $f$ in $C^1$ on compact intervals.
I have to prove that $\underset{x\in [-T,T]}{\sup} |f'_n(x) -f'(x)| \to 0$ as $n\to \infty$, but I don't quite know how to proceed. Thanks in advance for any help.
Comment: Is there any textbook or reference which proves this or a similar statement?
Answer: Let $K$ be a compact subset of $\mathbb{R}$ and $\Vert \cdot \Vert_{\infty}^{K}$ defined by :
$$ \Vert f \Vert_{\infty}^{K} = \sup \limits_{x \in K} \vert f(x) \vert $$
Let $n \in \mathbb{N}$. For all $n$, $\varphi := f_{n}-f$ is twice differentiable, with $\varphi$ and $\varphi''$ bounded on $K$. An inequality due to Landau gives you :
$$ \Vert \varphi' \Vert_{\infty}^{K} \leq 2 \big( \Vert \varphi \Vert_{\infty}^{K} \Vert \varphi'' \Vert_{\infty}^{K} \big)^{\frac{1}{2}} $$
Since $(f_{n}'')_{n}$ are uniformly bounded, there exists a constant $C > 0$ such that :
$$ \Vert f_{n}'-f' \Vert_{\infty}^{K} \leq C \big( \Vert f_{n}-f \Vert_{\infty}^{K} \big)^{\frac{1}{2}} $$
Which gives you that $(f_{n}')_{n}$ converges uniformly to $f'$ on every compact. It proves that $(f_{n})$ converges to $f$ in $\mathcal{C}^{1}$ norm on compact intervals since the norm is : $\displaystyle \Vert f \Vert_{\infty ~ , ~ \mathcal{C}^{1}}^{K} = \sup \limits_{x \in K} \big( \vert f(x) \vert + \vert f'(x) \vert \big)$.
Comment: How can we guarantee that $f'$ and $f''$ exist? Without this, $\varphi$ cannot be twice differentiable. As I know, the uniform convergence $f_n \to f$ just guarantees that $f$ is continuous.
Comment: @JaeYoungLee Yes it seems we were saying the same thing. I thought you were saying it doesn't work. I didn't say anything more in my first comment since this theorem is standard.

I really do mean that a priori $f_n'$ uniformly converges to some $g$ (Not $g'$. I shouldn't have said differentiable g). Then by the theorem, $f$ is also differentiable and $f' = g$.

Also, I agree that technically the theorem in Pugh's book did not say $f' = g$, but if you see the proof, they prove that.
Comment: @PratyushSarkar all agreed!
Comment: @JaeYoungLee You can use $\varphi = f_n - f_m$ instead and work with Cauchy sequences.
Comment: @PratyushSarkar, agreed and thanks, but we also need to apply https://math.stackexchange.com/questions/3356315/uniform-convergence-of-the-derivatives-means-convergence-of-the-function to have $f_n' \to f'$ --- the above proof with $\varphi = f_n = f_m$ will show $(f_n')_n$ is uniformly Cauchy so uniformly converges, but it doesn't show the convergence towards $f'$.
Comment: I also think rather than $\mathcal{C}^\infty$, twice differentiability of each $f_n$ is enough to prove the statement.
Comment: @JaeYoungLee Fix a compact set $K$. We know $f_n$ converges uniformly on $K$ to some $f$. Now, doing what I said shows the sequence $f_n'$ converges uniformly on $K$ to some differentiable function $g$ (it seems you agree with this). Now there is a theorem (for example Theorem 9 in Chapter 4 of Real Mathematical Analysis by Pugh) that these properties imply that in fact $f$ is differentiable and moreover $f' = g$.

I agree that $C^2$ is enough.
Comment: @PratyushSarkar I think we were on the same page. You meant $f_n' \to g'$ on $K$ for some differentiable function $g$ (which I agree). Then, https://math.stackexchange.com/questions/3356315/uniform-convergence-of-the-derivatives-means-convergence-of-the-function, which is a bit famous theorem from Principles of Mathematical Analysis by Rudin, shows $f' = g$.
Comment: @PratyushSarkar I meant $f' = g'$ at the last.
Comment: @PratyushSarkar Theorem 9 in Chapter 4 of Real Mathematical Analysis by Pugh -- see https://smartmanmaths.files.wordpress.com/2017/11/real-mathematical-analysis.pdf -- shows $f$ is differentiable, but it doesn't show $f' = g'$. But anyway, thank you for the theorem.


MySQL SELECT query hangs

Question: On my server I have 6 MySQL databases. They all power Wordpress websites. For some reason, 2 of these databases are not accessible. To my knowledge they use INNODB, but I can't confirm it as even doing a:
mysql> use INFORMATION_SCHEMA;
mysql> SELECT * FROM TABLES;

hangs on me. The working databases seem fine (at least the websites are online). When I tried to access the bad database websites, it will just keep spinning.
If I do a:
mysql> use rbb;
mysql> SELECT * FROM wp_posts;

It will just hang.
My server is running CentOS 6.2 and up to now i've had no issues.
Any pointers would be much appreciated. I have no idea what to do next.
Even opening a Sequel Pro to the server hangs the application.
If I try a query and then check the process list in another connection, it shows:
+----+------+-----------+-----+---------+------+----------------+------------------------+
| Id | User | Host      | db  | Command | Time | State          | Info                   |
+----+------+-----------+-----+---------+------+----------------+------------------------+
| 10 | root | localhost | rbb | Query   | 45   | Opening tables | SELECT * FROM wp_users |

Comment: Check your MySQL logfile for any error messages.
Comment: Tried rebooting/restarting MySQL? Indeed also check MySQL error logs, usually somewhere in /var/log or /var/lib/mysql file is usually called [hostname].err. If you use Linux you can try to find it run: `find / -name *.err`
Answer: According to the mysql documentation, the 'opening tables' state shouldn't be seen unless there are locks getting in the way, or when your table_open_cache is too small.
Check mysql's errorlog to find out what happened, check the show engine innodb status output for innodb info.
Comment: And a relevant older thread ... http://stackoverflow.com/questions/10904044/need-to-clear-mysql-innodb-lock-without-restarting-database
Answer: Maybe a mutex / lock went wild ?
Probably a restart will fix. Otherwise you can check if there are any locks and release them by hand.
Try with SHOW FULL PROCESSLIST;
Comment: Tried a restart and it didn't work. Also, the FULL PROCESSLIST only shows 1 query and that is the PROCESSLIST query.

MySQL SELECT query hangs


Validação para TextArea utilizando Razor MVC 4

Question: Tenho o seguinte modal e preciso incluir validações para que o textarea seja obrigatório ao clicar no botão "Sim", ou seja, não pode ser enviado em branco, e para que o textarea tenha no mínimo 15 caracteres.
  @model TClient

              @using (Html.BeginForm())
              {

                      <button type="button" class="close" data-dismiss="modal">×Close</button>

@(Model.IsLocked ? "Desbloquear" : "Bloquear") cliente

Deseja realmente @(Model.IsLocked ? "desbloquear" : "bloquear") o cliente @Model.Name?

                                      Motivo:

                                      @this.TextArea("motivo").Class("form-control required").Rows(7).Columns(50)

                      <button class="btn" data-dismiss="modal" aria-hidden="true">
                          Não
                      </button>
                      <button type="submit" id="confirmar" class="btn btn-primary">
                          Sim
                      </button>

              }

Comment: Eu não entendi o que é isto aqui: `@this.TextArea("motivo").Class("form-control required").Rows(7).Columns(50)`. Pode editar sua pergunta com o código fonte desses métodos?
Comment: Está usando jQuery.Validate?
Comment: @CiganoMorrisonMendez Acredito que sim, pois existem várias validações na aplicação que trabalho em javascript, jquery, etc. Qual seria sua proposta para as questões? Pois não encontrei nada a respeito até o momento
Comment: @CiganoMorrisonMendez é onde eu crio o TextArea com Razor, em MVC 4
Answer: Não consigo ver esta abordagem bem:
@model TClient

...

@this.TextArea("motivo").Class("form-control required").Rows(7).Columns(50)

O correto seria tipar fortemente a View:
@model MeuSistema.Models.MeuModel

E usar o seguinte clichê:
@Html.TextAreaFor(model => model.Motivo, new { @class = "form-control required", @cols = 50, @rows = 7 })
@Html.ValidationMessageFor(model => model.Motivo)

Se você decorou o Model corretamente:
[Required]
[DataType(DataType.MultilineText)]
public String Motivo { get; set; }

E o jQuery.Validate está instalado corretamente, deve funcionar sem mais configurações. Ainda, você pode forçar a validação do formulário da seguinte forma (jQuery):
$('#botaoSubmit').on('click', function() {
    $("#form").valid();
});


Definition of derivative $f(x) = 3x - \frac{1}{x^2}$

Question: $f(x) = 3x - \frac{1}{x^2}$
I am finding this problem to be very tricky:
Answer: There is no benefit joining the $3$ into the other fraction. It just makes your algebra harder/messier.
$$\lim_{h\to0}\frac{3(x+h)-\frac{1}{(x+h)^2}-3x+\frac{1}{x^2}}{h}$$
$$=\lim_{h\to0}\frac{3h-\frac{1}{(x+h)^2}+\frac{1}{x^2}}{h}$$
$$=\lim_{h\to0}\frac{3h-\frac{x^2}{x^2(x+h)^2}+\frac{(x+h)^2}{x^2(x+h)^2}}{h}$$
$$=\lim_{h\to0}\frac{3h+\frac{(x+h)^2-x^2}{x^2(x+h)^2}}{h}$$
$$=\lim_{h\to0}\frac{3h+\frac{2xh+h^2}{x^2(x+h)^2}}{h}$$
$$=\lim_{h\to0}\frac{3h+\frac{h(2x+h)}{x^2(x+h)^2}}{h}$$
$$=\lim_{h\to0}3+\frac{(2x+h)}{x^2(x+h)^2}$$
$$=3+\frac{2x}{x^2\cdot x^2}$$
$$=3+\frac{2}{x^3}$$
Answer: Just a hint:
Use anothe version of differentiation:
$f'(a)=\lim_{x\to a}\frac{f(x)-f(a)}{x-a}= \lim_{x\to a}\frac{(3x-1/x^2)-(3a-1/a^2)}{x-a}=\cdots=\lim_{x\to a}\frac{3a^2x^2(x-a)-(x-a)(x+a)}{x^2a^2(x-a)}=??$
Answer: we have $$\frac{f(x+h)-f(x)}{h}=\frac{3(x+h)-\frac{1}{(x+h)^2}-3x+\frac{1}{x^2}}{h}$$ for the numerator of the right-hand side we obtain
$$\frac{h \left(3 h^2 x^2+6 h x^3+h+3 x^4+2 x\right)}{x^2 (h+x)^2}$$ thus we have $$\frac{f(x+h)-f(x)}{h}=\frac{3h^2x^2+6hx^3+h+3x^4+2x}{x^2(h+x)^2}$$
now you can calculate the limit


button changing its color

Question: <!DOCTYPE html>

        <meta charset="utf-8" />
        <title>moockup< /title>

        <button id="button" class="yellowBackground" id="thumb" >paina tästä< /button>

Why that's not working i want to press that button and it must change its color to yellow and red after every click, i know how to do it in javascript but i am learning now jQuery.
Comment: you have two ids in your button tag....
Comment: aha i cant do that ? ok what i have to do now ?
Answer: Here is a solution man : http://jsfiddle.net/leojavier/9psL0oa0/
<button class="yellowBackground" id="thumb" >test</button>

css
.yellowBackground {

background-color: yellow;
}

.redBackground {

background-color: red;
}

js
$("#thumb").click(function() {

   $("#thumb").toggleClass("redBackground");

});

Comment: the toggleClass method thakes the classname that you want to add and remove... you assign a class by default and the toggle class should overwrite the default class
Comment: you should have only one ID attribute per tag, and it should be unique across the whole document
Comment: thank you Leo Javier !! so if i have for example  do i have to use one id in one row ? and can i use many classes ?
Comment: you should use classes... as long as you want many elements with the same behavior... for example $(".thumb")
Answer: Hope this will answer your question.
fiddle[http://jsfiddle.net/57vss22x/3/]
jquery
$(".place").click(function () {
$(this).toggleClass("green");
});

css
.place { background-color: yellow; }
.place.green { background-color: red; }

HTML
<button id="button" class="place" id="thumb" >paina tästä</button>

Answer: You can't have multiple id attribute in an element.

$("#button").click(function() {
  changeThumb();
});
function changeThumb() {
  $("#button").toggleClass("yellowBackground redBackground");
}
.yellowBackground {
  background-color: yellow;
}
.redBackground {
  background-color: red;
}

<button id="button" class="yellowBackground">paina tästä</button>

button changing its color


How to determine whether or not a group is a free group?

Question: How do i show whether or not a group could be free.
For example the Reals.
Answer: Here is a hint: when is $F(S)$, the free group on the set $S$, abelian? Now note that $\mathbb{R}$ is abelian...

Suppose $\mathbb{R}$ is isomorphic to the free abelian group on a set $S$. Then any $a\in\mathbb{R}$ can be uniquely written as a finite sum $$a=\sum_{s\in S} n_ss,\quad n_s\in\mathbb{Z}$$
("finite sum" meaning all but finitely many of the $n_s=0$).
Let $N=\max\{|n_s|\}$. We must also be able to uniquely write $\frac{a}{N+1}$ as a finite sum
$$\frac{a}{N+1}=\sum_{s\in S}m_ss,\quad m_s\in\mathbb{Z}$$
But because $a=\sum_{s\in S}(m_s\cdot(N+1))s$, we must have that $n_s=m_s\cdot (N+1)$ for all $s\in S$ and $n_s,m_s\in\mathbb{Z}$, which is impossible (unless $a=0$) because $|n_s|<N+1$. Thus $\mathbb{R}$ cannot be a free abelian group.
Note: this argument works for any divisible group.
Comment: Maybe @Zarks meant free abelian?


Query document based on a reference to a cached object

Question: I have two types of objects User and Company whose data is stored in MongoDB collections user and company. User contains a reference to Company. I can query for user using the following code in the UserRepository:
$this
  ->createQueryBuilder()
  ->field('employer')->references($company);

Company however is a heavy used object which is cached using Redis. When no cache is present, everything works fine. But when the company instance was fetched from the cache. The Doctrine unit of work does not know about the instance. Executing the code above will therefore result in the following error:
Cannot create a DBRef for class App\Document\Company without an identifier. Have you forgotten to persist/merge the document first?

I have found out that I can use a hack to register the company with the unit of work after fetching it from Redis:
$company = $this->fetchFromCache($params);
$documentManager->getUnitOfWork()->registerManaged($company, $company->getId(), []);

However, this seems ugly. Is there a way that I can query for the users without having to let Doctrine know about the Company object, or changing my datamodel?
Comment: You might want to check in with the Doctrine-team about the status. It has been a milestone for many years, see https://github.com/doctrine/mongodb-odm/issues/653
Answer: Unfortunately if you want to use ODM for querying you need to somehow let the ODM know about your object. While your registerManaged will work as it registers the document in the UnitOfWork it has a downside that any relations you might have there will be broken and/or Doctrine will assume they're new during persist/flush. You could try something along this:
$company = $this->fetchFromCache($params);
if ($dm->getUnitOfWork()->containsId($company->getId(), Company::class)) {
    return $dm->find(Company::class, $company->getId());
}
return $dm->merge($company);

As noted before, merge will recursively merge your documents and its embedded documents/relationships are and mark them as managed by ODM, just like they'd be just fetched. Please note that merging will conform to your cascading settings for references. More info about merging can be found in the docs. Also be aware that merging documents can affect already fetched documents and update their fields with what is being merged.
Merge operation will be only executed once as just before we're checking whether ODM already manages a company identified by an id fetched from the cache. If it does, we're calling $dm->find() which will not hit database as first it'll look up in-memory map of objects and return you the instance that is already managed. This way you'll always get a document managed by the ODM avoiding query to the database.


Pass parameters to docker-compose command

Question: I have Dockerfile:
FROM python:3.8

ARG CMD_ARGS=$CMD_ARGS
ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /app
COPY . /app
WORKDIR /app

And Docker-compose.yml file:
version: '3'

services:
  selenium:
    image: selenium/standalone-chrome
    ports:
    - 4444:4444
    restart: always

  app:
    build:
      context: .
    volumes:
    - .:/app
    command: sh -c "python3 my_script.py -arg1 -arg2"
    depends_on:
      - selenium

Is there a way not to hardcode arg1,arg2 in docker-compose.yml file and instead pass it in docker-compore up command OR using environmental variables (args containing whitespace characters) somehow?
Answer: You can try Variable substitution for your case.
example from docs:
db:
  image: "postgres:${POSTGRES_VERSION}"

When you run docker-compose up with this configuration, Compose looks
for the POSTGRES_VERSION environment variable in the shell and
substitutes its value in. For this example, Compose resolves the image
to postgres:9.3 before running the configuration.
If an environment variable is not set, Compose substitutes with an
empty string. In the example above, if POSTGRES_VERSION is not set,
the value for the image option is postgres:.

or for passing variable to command You can use $$.

You can use a $$ (double-dollar sign) when your configuration needs a
literal dollar sign. This also prevents Compose from interpolating a
value, so a $$ allows you to refer to environment variables that you
don't want processed by Compose.

example:
app:
  command: sh -c "python3 my_script.py $$VAR_ARG_1"

and on cli, You need to set VAR_ARG_1 variable.


CSS classes not loading

Question: I am trying to play around with this css package: https://github.com/elrumordelaluz/csshake
I followed the directions, but for some reason the animations aren't available on my page.
here's my head:

<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/reset.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
<link type="text/css" href="css/csshake.css">

and then in the body I'm trying:

shake!

If I inspect that element with Chrome dev tools, I can see that none of the css classes are applied to the div. If I change the header to <link type="text/css" rel="stylesheet" href="css/csshake.css">, then the classes appear in dev tools, but they are listed as invalid property values.
I'm sure I'm missing something really basic, but I don't know where to look.
Answer: If you look at the CSS file, it doesn't natively support cross browsers - you'll have to add relevant browser prefixes yourself
For example, animation-name: shake-base; only works in browsers that allow un-prefixed values (like new versions of FireFox). To serve Chrome you'd have to add -webkit-animation-name: shake-base;
You can use the following tools to let your computer do most of the work for you

CSSPrefixer (manually)
AutoPrefixer (automatic)
Prefixr (manual)
PrefixFree (automatic)

There are others as well
Comment: Yes. Specifically, -webkit- is needed.
Comment: @bjb568 Thanks, I was adding that in (:

CSS classes not loading


Solve a seemingly simple limit $\lim_{n\to\infty}\left(\frac{n-2}n\right)^{n^2}$

Question: $$\lim_{n\to\infty}\left(\frac{n-2}n\right)^\left(n^2\right)$$
Why does this go to 0? Why can I not just divide each item in the fraction by n and assume it would go to 1?
Comment: Aah! lengthy answers pays!
Answer: $$\lim_{n\to\infty} \left(\frac{n-2}n\right)^{n^2} = \lim_{n\to\infty} \left(\underbrace{\left(1 - \frac 2 n\right)^n}_{\to e^{-2}}\right)^n = 0$$
Edit: Since what I'm doing does not seem kosher (see the comments), I'll explain: If the "inner" limit had been $1$, then indeed this argument fails. However, if it goes to something strictly between $0$ and $1$, then the argument is fine, and it arises from an implicit use of the sandwich rule. (After you do this sort of thing a lot, you tend to leave these details unwritten.)
Here is a more accurate solution:
First of all, we have
$$\lim_{n \to\infty} \left(1-\frac 2 n\right)^n = e^{-2}$$
Therefore, for large enough $n$, we have
$$\left(1-\frac 2 n\right)^n < c$$
where $c$ is any constant you like strictly between $e^{-2}$ and $1$. Therefore, we have (for large enough $n$),
$$0 \le \left( \frac {n-2} n \right) ^{n^2} < c^n \to 0$$
which by the sandwich rule implies the required result.
Comment: @YoniRozenshein Sorry but your method is wrong: you can't pass to limit in  just on one part of the expression and this is a counterexample: your method gives $\lim_{n\to\infty} (\underbrace{1 - \frac 1 n}_{\to 1})^n=1$ and this obviously wrong.
Comment: @SamiBenRomdhane, you cannot use this when the limit goes to 1 but you can when it goes to less than 1. I will edit my answer to explain this.
Comment: Now time to convince myself that I'm not actually stupid.
Comment: You're not stupid :)
Comment: @SamiBenRomdhane I agree I have seen questions ending up wrong when we use limit in a single part without putting value in every variable present
Answer: A subtlety not fully appreciated by many beginning calculus students worldwide is that all occurences of the variable (here $n$) are tied together. In other words they go to infinity hand-in-hand. There are many occasions, where the limit processes can be separated, and done one at a time, but there are THEOREMS governing those situations. Yes, they look trivial, their proofs are usually not very difficult, but yet they are powerful! Because they tell that we can, in a way, treat some occurences of the variable $n$ as if the others did not exist.
For the purposes of explaining this difficulty I will study a different limit, namely
$$
\lim_{n\to\infty}\left(\frac{n-2}n\right)^n.
$$
Many a beginning student wants to conclude that the fraction tends to $1$, and thus the answer should also be $1$, because the limit of $1^n$ as $n\to\infty$ is (quite correctly!) equal to $1$. But effectively these students are calculating an iterated limit
$$
\lim_{m\to\infty}\left(\lim_{n\to\infty}\left(\frac{n-2}n\right)\right)^m,
$$
because they first study what happens to fraction $(n-2)/n$, and only then consider the effect of raising the result to a high power.
So the justification is exactly the one you proffered in your OP, and it is correct for this iterated limit. Observe that here first $n$, then $m$ tends to infinity. I indicated this by using parentheses to make it clear that $n\to\infty$ first (first working inside the parens).
But we might also approach this differently, and reverse the order of the two limit process, and study the following limit
$$
\lim_{n\to\infty}\left(\lim_{m\to\infty}\left(\frac{n-2}n\right)^m\right)
$$
instead.
Here a student could (again correctly) argue that w.l.o.g. we can assume that $n>2$. Therefore the fraction $(n-2)/n$ is between $0$ and $1$. If $q\in(0,1)$, then we know (perhaps a result derived in connection with a geometric series?) that
$$
\lim_{m\to\infty}q^m=0.
$$
As this works for all $n>2$, the answer to this limit question should be $\lim_{n\to\infty}0=0$.
Again, this reasoning is correct, and applies to the limit process, where first $m$ and only then $n$ tends to infinity.
Yet we know from our studies revolving around the Napier's constant $e$ that the answer to the limit
$$
\lim_{n\to\infty}\left(\frac{n-2}n\right)^n=e^{-2},
$$
i.e. neither zero nor one but something in-between.
What went wrong? We forgot that the $n$ that we plug in when calculating the value of the fraction $(n-2)/n$ is the same as the exponent $n$. But why should we care, a student may cry? How is this any different from a calculation like
$$
\lim_{n\to\infty}\frac{n+3}{2n-7}\cdot\frac{3n+5}{n-4},
$$
where we CAN calculate it like
$$
\lim_{m\to\infty}\left(\lim_{n\to\infty}\frac{n+3}{2n-7}\cdot\frac{3m+5}{m-4}\right)=\left(\lim_{n\to\infty}\frac{n+3}{2n-7}\right)\cdot\left(\lim_{m\to\infty}\frac{3m+5}{m-4}\right)=\frac12\cdot\frac31=\frac32?
$$
Well, that's exactly because there is a theorem saying that when
$\lim_{n\to\infty}a_n=A$ and $\lim_{n\to\infty}b_n=B$, and BOTH $A$ and $B$ are real numbers, i.e. not infinities, then we also have $\lim_{n\to\infty}a_nb_n=AB$. That result is very simple. And there is a similar result for the powers: $\lim_{n\to\infty}a_n^{b_n}=A^B$ (provided that $A>0$). But this only applies to (finite) real numbers $A,B$. If you try to apply this result to the sequence
$$
\left(\frac{n-2}n\right)^n,
$$
you see that the premises do not hold, because here the exponents tend to infinity as opposed to some number $B$. Therefore we must look for a different approach.
This kind of subtleties are the reason, why the calculus textbooks devote a lot of time to the so called indeterminate (is that the correct English term?) cases such as $0/0$, $\infty/\infty$, $\infty\cdot0$, $1^\infty$, where the usual shortcuts cannot be justified by a suitable theorem (because no such theorem exists, and the answer always `depends').
Comment: Sorry about the longish rant. I'll be teaching freshman calculus to physics majors again next fall, so I'm testing how this kind of an explanation is received :-/
Comment: You're going to be a great teacher :)
Comment: (To answer your question about "indeterminate": yes, this is indeed perfectly good English.)
Comment: @JyrkiLahtonen Thanks sir. I understand now
Answer: $$\lim_{n\to\infty} \left(\frac{n-2}n\right)^{n^2}=\lim_{n\to\infty}e^{n^2\log(1-2/n)}=\lim_{n\to\infty}e^{n^2.(-2/n)}=\lim_{n\to\infty}e^{-2n}=0$$
Comment: You should write $e^{n^2\left(-\frac{2}{n}-\frac{1}{2}\left( \frac{2}{n} \right)^2-\dots\right). }$.
Answer: $$\lim_{n\to\infty}\left(\frac{n-2}n\right)^\left(n^2\right)$$
$$\lim_{n\to\infty}\left(1 -2/n\right)^{n^2}$$
$$e^{\lim_{n\to\infty}\left({\left(\frac{-2}{n}\right)}\times n^2\right)}$$
$$e^{-\infty} =0;$$


What is the recommended way to structure Sass with Zen theme?

Question: I have just started using Sass and decided to go ahead and build a theme with Zen for the first time. My very first task is to split the header region into 3 separate regions: header-left, header-main, header-right. From what I've read so far, the best practice to do this is to go in your subtheme's .info file and declare the region and then go to the page.tpl.php file and use the corresponding variables.
I also understand that I will probably have to write Sass that looks like this:
<code>.header-container {
 @include zen-grid-container;
}

.header-left {
 @include zen-grid-item(3, 1);
}

.header-main {
 @include zen-grid-item(3, 2);
}

.header-right {
 @include zen-grid-item(3, 3);
}
</code>
My questions are the following:
1) Where do I add my Sass? Defining the structure of a new region seems like a good fit for the layouts directory. Do I add Sass in the layouts/_responsive.scss file or do I create a new file like layouts/_custom.scss?
2) How do I organize my custom styling in a Zen subtheme? Is there a "best-practices" guide that describes how things should be ordered?
Comment: There is a best practices style guide [SMACSS](https://smacss.com) for structuring CSS, but how you translate it to Zen SASS folder and file structure is up to you. Also look at [Understanding SMACSS file structure for subthemes](https://www.drupal.org/node/2087061) and similar posts.
Comment: These links were really helpful for me to get a better grasp of how my stylesheets can be organized. I think this comment deserves to be incorporated in the accepted answer or at least be an answer of its own for posterity reasons.
Answer:
Do I add Sass in the layouts/_responsive.scss file or do I create a new file like layouts/_custom.scss?

Completely up to you. Modularisation is always a good thing, how far you go with implementing that as partials depends on your tastes, and how much code you actually need to write.

How do I organize my custom styling in a Zen subtheme? Is there a "best-practices" guide that describes how things should be ordered?

I wouldn't have thought so. There isn't an industry standard for structuring a Sass project, so any claims of "best practice" or "this should be done like this" that you see must be taken with a pinch of salt.
It's mostly common sense in my experience. If a file's getting too big, split it up. If natural groups start to form (views, node, users) then you might want to split those up into sub-folders. If you're not already using it, Sass globbing will take the pain out of including partials if you have a lot of them.