Understanding Divergence Test through difference of consecutive partial sums

Question: It's known that the difference between two consecutive partial sums $s_{n+1}$ and $s_n$ should give us the sequence term $a_{n+1}$, i.e. $s_{n+1} - s_n = a_{n+1}$.
If we take a limit of both sides as $n$ approaches infinity, we get:
\begin{equation}
\lim_{n \to \infty} s_{n+1} - s_n = \lim_{n \to \infty} a_{n+1}.
\end{equation}
Let's say it's also known that $\lim_{n \to \infty} a_n = 0$. That would imply that $\lim_{n \to \infty} a_{n+1} = 0$, and $\lim_{n \to \infty} s_{n+1} - s_n = 0$.
This seems to say that $s_{n+1} \approx s_n$ for some large value of $n$ and the sequence of partial sums (infinite sum of $a_n$) converges to some value. Two questions:

Why would the Divergence Test be inconclusive if it seems like the limit of partial sums converges to some value? (I understand that the harmonic series diverges although the limit of $a_n$ goes to zero, so looking for an argument along the lines of partial sums.)

Why is the same limit of $a_n$ going to zero enough to say that an alternating series converges? (i.e. then we have $\lim_{n \to \infty} s_{n+1} - s_n = \lim_{n \to \infty} (-1)^n a_{n+1}$ and the same argument applies).
Answer: As the harmonic series illustrates, a sequence's general term can go to zero without its being Cauchy.  It doesn't seem like the partial sums converge, only that the difference of successive partial sums goes to zero.
In the case of an alternating decreasing series, it turns out the series will be Cauchy.  See the (clever) proof of the Leibniz test.  I would reproduce it, but it should be easy to find.  I learned it in Best and Penner's Calculus, a fine book.


In eclipse, how can I enable auto insert of closing }}?

Question: I'm using eclipse for python/django work. One annoyance is that when in html editor, when I enter {% or {{, eclipse doesn't auto-complete the closing %}, }} since those aren't html tags.
I read about the templates and content assists. It seems like templates is the way to go, but can't seem to get mine to work.
How can I enable the this:

1) enter {{
2) eclipse auto-inserts }} so that we have {{ _ }} with insertion point automatically within the {{ }}.
Answer: You can use Aptana Studio. A special distribution of Eclipse that will autocomplete that for you.
It has better support for WebBased scripting languages such as Python (DJANGO), Ruby (Rails), PHP and many others.
You can download and install it from scratch, or just upgrade your current Eclipse installation with it.
Comment: Sorry, you're correct. My thinking came from the fact that everything python related in Aptana comes from PyDev. However, the Django templating portion in PyDev is only compatible with Aptana.
Comment: Eclipse+PyDev is functionally equivalent to Aptana Studio. AFAIK there's nothing special in Aptana Studio's implementation of PyDev.
Comment: @ChrisPratt: I see... You sure? Even DJANGO/JINJA2 template support is in PYDEV?


Splot multiple lines in gnuplot from the same datafile

Question: I have a file in the form
# Line 1
# x y z
  x11 y11 z11
  x12 y12 z12
  ....
  x1n y1n z1n
  ( blank row )

 .....

# Line N
# x y z
  xN1 y11 z11
  xN2 y12 z12
  ....
  xNk yNk zNk

If I try to splot such file, gnuplot intended it as a surface, and the result is very awful (because the endpoint of a line is close to the endpoint of next line, not to the first point). How can I plot them as different lines (as every line was in a different file)?
Answer: Each contiguous part of coordinates is called a block. Two blocks are separated by a single blank row. (Note, that two blank rows separate two data sets which can be accessed with index).
You can select a certain row for plotting using the every option:
block = 4
splot 'file.dat' every :::block::block

This selects the fifth block (the numbering starts at 0).
To iterate over all available blocks, you can estimate the number of blocks with the stats command:
stats 'file.dat'
splot for [i=0:int(STATS_blank)] 'file.dat'

Note, that some blanks at the end of the file, which do not separate blocks, are also counted, but that is no problem for the iteration.
You can of course also use the iteration variable i to select a certain linetype or linestyle.


Tenho dúvidas se essa seria a forma correta de fazer uma conexão utilizando a class PDO

Question: Estou com dúvida se essa forma é realmente correta de fazer uma conexão entre php e mysql com a class PDO. A dúvida é a seguinte: Cada vez que eu tiver uma arquivo que utilize essa conexão ele vai criar uma nova conexão
Class PDOUtil {

public static final function conectar() {
    try {
        $conexao = new PDO('mysql:host=localhost;dbname=site_local', 'root', '');
    } catch (PDOException $ex) {
        echo $ex->getMessage();

        if ($ex->getCode() == "2002") {
            echo 'Oi infelizmente seu Host nao foi encontrado, verifique isso com o web-master.';
        }
        if ($ex->getCode() == "1049") {
            echo 'Oi infelizmente seu banco nao foi encontrado, verifique isso com o web-master.';
        }
        if ($ex->getCode() == "1044") {
            echo 'Oi infelizmente seu usuario nao foi encontrado, verifique isso com o web-master.';
        }
        if ($ex->getCode() == "1045") {
            echo 'Oi infelizmente sua senha nao foi encontrada, verifique isso com o web-master.';
        }
    }
    return $conexao;
}

}
um exemplo de utilização
include '../config/PDOUtil.php';
$con = new PDOUtil();
$con->conectar();

Comment: A sua classe conectar é estática, então não há necessidade de instanciar um objeto. Esse é um padrão chamado singleton você poderia simplesmente chamar ela usando $con = PDOUtil::conectar(); A partir daí é só usar o objeto do PDO. $con->prepare("SELECT * FROM tabela"); $con->execute(); $retorno = $con->fetchAll(); E por ai vai.
Answer: Seu script está certo, porem vai ficar criando uma conexão nova a cada vez que você solicitar. Tente desta forma, utilizando o padrão Singleton;
Class PDOUtil {
private static $oInstance;

public static function getConexao() {

  if (!(self::$oInstance instanceof PDO)) {
    try {
      $conexao = new PDO('mysql:host=localhost;dbname=site_local', 'root', '');
    } catch (PDOException $ex) {
      echo $ex->getMessage();

      if ($ex->getCode() == "2002") {
        echo 'Oi infelizmente seu Host nao foi encontrado, verifique isso com o web-master.';
      }

      if ($ex->getCode() == "1049") {
        echo 'Oi infelizmente seu banco nao foi encontrado, verifique isso com o web-master.';
      }

      if ($ex->getCode() == "1044") {
        echo 'Oi infelizmente seu usuario nao foi encontrado, verifique isso com o web-master.';
      }

      if ($ex->getCode() == "1045") {
        echo 'Oi infelizmente sua senha nao foi encontrada, verifique isso com o web-master.';
      }
      exit;
    }

  }
  return self::$oInstance;
}

/* TORNA O CONSTRUTOR PRIVADO PARA PROIBIR DE INSTANCIA ESTA CLASSE */
private function __construct() {}

}

Para utilizar agora você sempre pode fazer desta forma:
$conexao = PDOUtil::getConexao();

Desta forma somente uma conexão vai existir na sua aplicação e você consegue pegar a mesma facilmente;
Answer: Em PDO você só precisa de uma conexão para toda a "existência" do script, exatamente igual faria com mysql_*.
$conn = new PDO(
    'mysql:host=localhost;dbname=example-pdo', 'andre', '123456',
    array(
        PDO::ATTR_PERSISTENT => true
    )
);

No exemplo acima, configuramos nossa conexão como persistente. Uma conexão persistente não é fechada no final do script, e sim armazenada em cache sendo reutilizada quando outro script solicitar uma conexão usando as mesmas credenciais.
A sua forma está correta!


How do errors in variables affect the R2?

Question: I've got a question about errors in variables. So, if I run a standard linear regression to estimate b in y = a + bx, but my independent variable x is measured with error, x = x' + x" where x' is the true value, then my estimate of b will be wrong:
$E\hat{b} = b (1 - \frac{var(x^")}{var(x') + var(x^")})$
I hope I have got that right.
My question is, how will the R squared of this regression be related to the true R squared, i.e. the proportion of variance in y explained by the true value x'?
Put it another way: suppose I have an R squared and a coefficient from a given regression of a variable measured with error; and I know the R squared that would be achieved if I could measure the variable perfectly; can I deduce an estimate of the true regression coefficient b?
Answer: Yes, it's possible, but it requires a dearth of simpifying assumptions, not always likely to hold in practice. Let's assume the following model
$$y=\alpha+\beta x^*+\epsilon$$
As usual, we assume $E[x^*\epsilon]=0$. Since you didn't mention measurement error for $y$, I won't include it. However, we do have measurement error on the predictor, i.e., we don't observe $x^*$ but $x$, which is related to $x^*$ by the model
$$x=x^*+\eta$$
Now we assume that $x^*$ and $\eta$ are independent, that $E[\eta]=0$ and that $E[\eta^2]=\sigma^2_{\eta}$ is known. Note that all this is quite restrictive! For most measurement instruments, the RMSE isn't constant across all the scale (i.e., for each value of the measured variable inside the non-saturated range), and even if it were, the RMSE wouldn't be known exactly. However, if the instrument has been calibrated multiple times by a certified laboratory, we may assume that $\sigma^2_{\eta}$ is reasonably well-known. Also, there are instruments such that, when taking measurements of $x^*$ sufficiently far from the ends of the scale, the RMSE stays the same, whatever the value of $x^*$ in this range. Thus, there are (few) real situations where these hypotheses are reasonable. Finally, we assume that $\epsilon$ and $\eta$ are independent. Note also that this model leads to
$$y=\alpha+\beta (x-\eta)+\epsilon=\alpha+\beta x+\epsilon-\beta\eta=\alpha+\beta x+u$$
Enough of the background: when we observe $\{(x_i,y_i)\}_{i=1,\dots,N}$, the estimate of $R^2$ is
$$\hat{R}^2=1-\frac{\sum_{i=1}^N(\hat{\alpha}+\hat{\beta} x_i-y_i)^2}{\sum_{i=1}^N(y_i-\bar{y})^2}$$
Now we're in for a bit of calculus:

$$\text{plim}\frac{N}{\sum_{i=1}^N(y_i-\bar{y})^2}=\frac{1}{\sigma^2_y}$$
$$\text{plim}\hat{\beta}=\text{plim}\frac{\sum_{i=1}^N(x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^N(x_i-\bar{x})^2}=\frac{\sigma_{xy}}{\sigma^2_x}=\frac{\text{Cov}[(x^*+\eta)(\alpha+\beta
        x^*
        +\epsilon)]}{\sigma^2_x}=0+\beta\frac{\sigma^2_{x^*}}{\sigma^2_x}+\beta\frac{\text{Cov}[\eta x^*]}{\sigma^2_x}+\beta\frac{\text{Cov}[\eta\epsilon]}{\sigma^2_x}=\beta\frac{\sigma^2_*}{\sigma^2_x}$$
    where in the last step we used the fact that $\text{Cov}[\eta
    x^*]=0$ (since $x^*$ and $\eta$ are independent and $E[\eta]=0$) and
    we used the hypothesis that  $\eta$ and $\epsilon$ are independent
    with 0 mean. We also used the short-hand notation $\sigma^2_*$ for
    $\sigma^2_{x^*}$. Finally, since
    $$\sigma^2_x=\text{Var}[(x^*+\eta)^2]=\sigma^2_*+2\text{Cov}[\eta
    x^*]+\sigma^2_{\eta}=\sigma^2_*+\sigma^2_{\eta}$$ we get the
    well-known expression
    $$\text{plim}\hat{\beta}=\beta\frac{\sigma^2_*}{\sigma^2_*+\sigma^2_{\eta}}$$
Using the fact that $\hat{\alpha}=\bar{y}-\hat{\beta}\bar{x}$, we can simplify the numerator in the expression of $\hat{R}^2$:
$$\sum_{i=1}^N(\hat{\alpha}+\hat{\beta}
    x_i-y_i)^2=\sum_{i=1}^N(\bar{y}-\hat{\beta}\bar{x}+\hat{\beta}
    x_i-y_i)^2=\sum_{i=1}^N(\hat{\beta}(x_i-\bar{x})
    -(y_i-\bar{y}))^2$$
Now, we can use 2. to compute the following probability limit:
$$\text{plim}\frac{\sum_{i=1}^N(\hat{\beta}(x_i-\bar{x})-(y_i-\bar{y}))^2}{N}=\text{plim}\frac{\sum_{i=1}^N \hat{\beta}^2(x_i-\bar{x})^2+(y_i-\bar{y})^2-2\hat{\beta}(x_i-\bar{x})(y_i-\bar{y})}{N}=\beta^2\frac{\sigma^4_*}{\sigma^4_x}\sigma^2_x+\sigma^2_y-2\beta\frac{\sigma^2_*}{\sigma^2_x}\sigma_{xy}$$

In 2. we've already seen that $\sigma_{xy}=\beta\sigma^2_*$, thus
$$\beta^2\frac{\sigma^4_*}{\sigma^4_x}\sigma^2_x+\sigma^2_y-2\beta\frac{\sigma^2_*}{\sigma^2_x}\sigma_{xy}=\beta^2\frac{\sigma^4_*}{\sigma^2_x}+\sigma^2_y-2\beta^2\frac{\sigma^4_*}{\sigma^2_x}=\sigma^2_y-\beta^2\frac{\sigma^4_*}{\sigma^2_x}$$

Putting this all together:

$$\text{plim}\hat{R}^2=1-\frac{\sigma^2_y}{\sigma^2_y}+\beta^2\frac{\sigma^4_*}{\sigma^2_x\sigma^2_y}=\beta^2\frac{\sigma^4_*\sigma^2_*}{\sigma^2_x\sigma^2_*\sigma^2_y}=R^2\frac{\sigma^2_*}{\sigma^2_x}$$
because, only for simple linear regression,
$$R^2=r^2=\beta^2\frac{\sigma^4_*}{\sigma^2_*\sigma^2_y}$$
Thus we (finally!!) conclude that
$$\text{plim}\hat{R}^2=R^2\frac{\sigma^2_x-\sigma^2_\eta}{\sigma^2_x}$$
which is the desired relationship between the R squared of our regression with errors in the predictor, and the true $R^2$. Note that, as well-known, in the classic error-in-variables model the OLS estimator of $R^2$ converges to a limit which is always smaller than the true $R^2$. Finally, if an estimate of $\sigma^2_{\eta}$ is known (by independent calibration of the instrument: you cannot get it from the same data used for the regression), then, since the data $\{(x_i,y_i)\}_{i=1,\dots,N}$ allow an estimate of ${\sigma^2_x}$ and ${\sigma^2_y}$, you see that we can get an estimate of $\beta$.
Comment: This looks really solid. Just to double check I've understood correctly: the estimated $\hat{R}^2$, and the known $R^2$, provide an estimate of $\beta^2 \sigma^2_\eta / \sigma^2_y $ . But we can't get $ \beta $ itself out without also knowing the variance of the measurement error.
Comment: @dash2 we're a bit impatient, it seems ;) here it is.
Comment: Thank you so much! The answer is so neat!
Comment: Yes, exactly. Of course, this isn't the only possible errors-in-variables model: for example, you could assume that $\sigma_{\epsilon}/\sigma_{\eta}$ is known (*Deming regression*) or that the reliability $\lambda=\sigma^2_*/(\sigma^2_*+\sigma^2_{\eta}) $ is known , where $\sigma^2_*$ is the variance of the latent predictor (it's immediate to show that this last case is equivalent to my model where $\sigma_{\eta}$ is assumed to be known). Anyway, all classical errors-in-variables models are similar, in that quite strong assumptions are made.
Comment: Oh, sh... I just now realized I used the true $\alpha$ and $\beta$ in my formulas, while of course I **must** use the estimators, which are biased in this context, so they don't converge to the "true" values in the $\text{plim}$. I was fooled by the fact that the result looked good: it's a well-known fact that error-in-variables leads to an $R^2$ which is **smaller** than the "true" value, unless you use an instrumental variable to compute it. No worries, though - I'm fixing this right away. Let me know if you like the new version.
Comment: It looks the same so far...
Comment: Thank you very much. I suppose you could simplify the last denominator to just use $ \sigma^2_y \sigma^2_x $. In any case, I will accept.
Comment: You're right, I'm not at a PC now but I will edit later.
Comment: I found another derivation which is a bit shorter and which would allow me to include all the algebra steps in the answer. If you're interested, I will include them this week. Note that the result is exactly the same, thus if you're interested only in the final result, then there would be no improvement for you.
Comment: Go ahead, I'd certainly think it would make a better answer!
Comment: There it is, hope you like it better now :)
Comment: I may end up using this in a published article. How should we thank/cite you?


What exactly does a DJ do when producing a track?

Question: I want to understand what a DJ does when producing a track. For example, let's look at the new track -
Martin Garrix feat. Usher - 'Don't Look Down'.

Who wrote the lyrics for this track?
Who wrote the music for this track?
What exactly did Martin Garrix do for this track?
Comment: related: http://music.stackexchange.com/q/2944/28
Comment: Is this not a ridiculously broad question?
Comment: @LightningRacisinObrit, may be.
Answer: Garrix, here, has followed a common transition -- from playing other people's records and getting a thorough understanding of how dance music works, to producing his own music. So, he is known as a DJ, but what he is doing when he makes music is producing.
Production covers a lot of activities, but it covers everything about turning an idea into a finished recording. Just to confuse matters, the idea might not fully exist before production starts. It's entirely possible that the recording and the songwriting happened in tandem.
It can be very hands on -- playing instruments, programming sequencers, actively setting up effects. Or it can be entirely supervisory; telling musicians and engineers what to do. And it can be collaborative -- the implication here is that Usher and Garrix worked together on the process of production.
The nearest you're going to get to knowing who wrote a song, is usually the credits. In this case, "Writers: Martijn Garritsen, Usher Raymond, James Abrahart" and "Producers: Martin Garrix, Usher Raymond"
One very likely scenario for a collaboration like this, between a DJ-producer and a singer-songwriter, is this:  Garrix could have worked on an instrumental piece at home on a laptop. With this sounding OK, he could have sent an MP3 to Usher, who would have come up with a vocal melody and some words. Then they could have met in a studio, recorded the vocals, tweaked the instrumental, done some final mixing and polishing, to come up with a finished recording.
That's only one possible scenario. There are no fixed rules.
Answer: Okay, here's my GUESS not really knowing anything but the credits.
Writers: Martijn Garritsen, Usher Raymond, James Abrahart
Producers: Martin Garrix, Usher Raymond
It's weird in electronic music because when we say "producer" we pretty much mean "person who wrote the track" while in the BIG music world, "producer" generally means "the person who oversees the recording of someone else's song's componenets and then pieces them together in the studio, making them sound good"
Usually this is when there is a band, with multiple members/parts which need to actually be recorded, then sequenced. A good producer knows how to get the track to sound "just right". They might suggest that the song do "THIS" here, or that the singer try singing like "this" here. Traditionally, they don't do much in the actual "writing" department.
Then there came DJs. DJs will often REMIX another person's music, sometimes only keeping a tiny part of the original. These are usually referenced by saying the original song's artist and name, with a (Martin Garrix Remix) or similar afterwards.
Since they were already pretty much doing it, many "DJs" decided to make their own tracks. I believe that since many did this using samples in the beginning rather than originating their own sounds, it became known as producing rather than writing. I could be wrong.
Nowadays, the line has blurred a bit, but in this case we know that Martin Garrix considers this to be a track that he himself has "written", well, co-written with Usher and James Abrahart. I'm going to guess that in this song, James Abrahart likely wrote the guitar (or whatever it is) riff in the beginning and perhaps some other things. Usher likely wrote the lyrics and Martin Garrix did the rest.
Of course, it is more likely that all three had some input in most every part.
Since they didn't designate, it's easy to assume that Usher did the lyrics, but they might very well have been the three of them collab-ing. Or they wanted to show that Usher did more than just pen the lyrics.
Essentially what this says to me is that all three of them wrote the music AND the lyrics, then Usher and Martin Garrix put it all together into a recorded, sequenced, song.
Apparently here mastering is considered part of production, since no one is even listed for mastering. Hope this helps.


Probability of exactly 1 given everything appears

Question: I'm trying to figure out the following question, and I'm stuck! It comes from an old PUMaC problem with a partial solution (there's a typo in the one provided on Stack Exchange as well):
Stack Post:
Collecting cards
Original Problem and Solution (problem 4):
https://static1.squarespace.com/static/570450471d07c094a39efaed/t/5de43032c573c502d7fe2f7d/1575236145801/2019Combo_A_Sols.pdf
If I'm not mistaken, I think what the problem is saying is that if you have cards $\{A,B,C,D,E,F\}$, drawn independently and with equal probability 1/6 (uniformly), what is (1 minus) the probability that you obtain exactly one A, given that you stop drawing when you've drawn every card? (We can choose A w.l.o.g. by law of total probability).
e.g. $(A,B,C,D,E,E,F)$, $(B,B,C,C,D,F,F,E,E,A)$ <-- these draws work.
According to their partial solution, it seems not so much of a coincidence that we see the harmonic series as would be in the case if we found E(number of draws until all 6 appear) using linearity of expectation. But, beyond that I'm not sure how they achieved this answer.
When I tried to go about it, it seemed like it would take significant case work (dependent on the number of draws and whether or not A is the last to appear).
Answer: Consider pulling cards until you pull $A$ twice. Note that the order of $A$ is equally likely to be the first, second, ..., sixth. Now, condition on the order of $A$, let it be the $n$th unique card. Let us assume that your position $n$ is not equal to $6$. Now, $A$ is again equally likely to be the last card among the remaining $(6 - n)$ cards, giving a probability of $A$ being last $1/(6-n + 1)$.
For $A$ to be drawn once in your scenario, $A$ had to be the last card in the first or the second draw (do you see why?), giving the final answer $(1/6) + (1/6) \sum_{n=1}^5 1/(6-n + 1)$, which matches the formula they gave. I believe they used a different counting argument, but since their solution is incomplete, and this one is simpler, you might as well use this.
As an important side note, I think the better way to think about this problem to understand all the symmetries and independences is by considering a Poisson splitting (that is how I solved it). It is a hard problem without Poisson processes for sure.
Comment: Thank you so much! The Poisson splitting was very helpful for visualizing the approach!
Comment: If I understand the punchline: either on the first draw you get (rest of letters,A) which happens 1/6 or on second draw, you sort of restart at A and reconsider A's occurrence among those that have yet to occur. If A's occurrence happens last after the rest of cards are drawn, you have (first set,A,rest,A). "Ignore" the middle A because that single last A is basically its marker, and you have your complete set. The probability A is last in the second draw depends on its position with all positions occurring with p=1/6.
Comment: Yup, the last A there is a marker if you drew all of your other cards in first set + rest! And yes, your last sentence is also precisely correct. Good job!


How do I view logistic regression coefficients from each bootstrapped iteration using R

Question: I am relatively new to both R and bootstrapping.
My data set consists of a large number of variables, with the aim of generating a logistic regression model to predict a binary yes/no outcome.
As an example, I have created a 7-parameter model for which I am looking to bootstrap for a means of internal validation using the code below.
model = glm(outcome ~ parameter1 + parameter2 + etc...,
            family = "binomial",
            data = DATA)

logit.bootstrap = function(data, indices){
  d = data[indices, ]
  fit = glm(formula(as.character(model$call)[2]), data = d, family = "binomial")
  roc = pROC::roc(response = fit$y,
                  predictor = fit$fitted.values)
  c = pROC::coords(roc,
                   x = "best", best.method = "youden", best.policy = "random",
                   ret = c("sensitivity", "specificity", "ppv",
                           "npv"))

bootn = 10
set.seed(123)
logit.boot = boot(data = DATA, statistic = logit.bootstrap, R = bootn,
                  sim = "ordinary", strata = DATA[,1])

Question:
I understand that for each bootstrapped iteration, the code will randomly sample individuals from my population and call the glm regression using the set parameters in the model. Each bootstrap iteration will deliver slightly different coefficients for each parameter based on the population sampled.
I would like to be able to view the coefficients generated for each bootstrapped iteration for each of the 7 chosen parameters to assess for stability. I essentially want to ensure the sign of each parameter does not change over the bootstrapping process. Obviously, I won't be checking all 1000, but is there a way to output this or at least 10-100 bootstrapped iterations?
Cheers
Comment: What does `logit.bootstrap` return? It doesn't seem like a correct `R` function.
Comment: Ok. what about my second comment?
Comment: are you asking about checking the bootstrap estimates during the bootstrap itself, i.e. online, or after the bootstrap is done?
Comment: @utobi I was hoping to check the bootstrap estimates of the coefficients after the bootstrap done. For example, if the coefficient estimate for parameter 1 is 1.34 in the stepwise regression, I would like to see what the values of this are in each bootstrapped iteration to see how much it varies when different populations are sampled. Essentially a way of checking the stability of the coefficients.
Comment: @utobi I think logit.bootstrap just contains the formula of the model and the code for ROC generation to be called for each bootstrap iteration. That said, this was a code given to me that I am still trying to fully understand.
Comment: the function misses a return and `}`.
Comment: check my answer below and let me know if you have any questions.
Answer: As per the comments above, your example is not replicable (e.g. you didn't provide the data and the statistic to be provided to boot is not correctly implemented), I'll show you a toy example using the mtcars dataset. If you are interested in checking the bootstrap estimates of the $\beta$'s, this example should be enough; e.g. the object betahat.boot$t below contains what you are looking for.
To run the bootstrap procedure in this example I find it easier to use the car package, which is just a wrapper around the boot
library(car)
m1 <- glm(am~wt + hp, data=mtcars)

betahat.boot <- Boot(m1, R=999) # same object as returned by a call to boot

# the object named t, contains the bootstrap estimates
head(betahat.boot$t)

# run a histogram of the bootstrap estimates for the intercept
hist(betahat.boot$t[,1])


Javascript: Stripe and Apple Pay Web setup

Question: I am trying to follow the guide here ( https://stripe.com/docs/stripe-js/elements/payment-request-button )  to setup Apple Pay for the web and Stripe.  The initial steps such as verification of domain and all the pre-setup is done but I am having an issue following the steps for the payment.
The Apple Pay Button is showing up in my Safari browser.  When the button is clicked, I fire an event called checkoutWithApplePay().  I get lost after step 3 and not sure what to do.  I am posting to my backend and on the backend, creating a payment intent and returning the client_secret
checkoutWithApplePay() {
    // STEP 1 FROM GUIDE
    const stripe = Stripe("_____");

    // STEP 2 FROM GUIDE
    const paymentRequest = stripe.paymentRequest({
        country: 'US',
        currency: 'usd',
        total: {
            label: 'Store Total',
            amount: cartTotal() ,
        },
        requestPayerName: true,
        requestPayerEmail: true,
    });

    // STEP 3 FROM GUIDE
    const elements = stripe.elements();
    const prButton = elements.create('paymentRequestButton', {
        paymentRequest: paymentRequest,
    });

    paymentRequest.canMakePayment().then(function(result) {
        if (result) {
            prButton.mount('#payment-request-button');
        } else {
            document.getElementById('payment-request-button').style.display = 'none';
        }
    });

    // STEP 4 FROM GUIDE -- THIS RETURNS A CLIENT SECRET
    axios.post('/api/checkout/checkout-with-apple-pay-web', {
        total: this.formattedCartTotal()
    })
    .then((resp) => {
          myClientSecrent = resp.clientSecret;
    });

    // THIS IS WHERE I GET CONFUSED AND NOT SURE HOW TO HANDLE AND BELOW IS
    JUST TEMPLATE CODE FROM THE GUIDE

    paymentRequest.on('paymentmethod', function(ev) {
        // Confirm the PaymentIntent without handling potential next actions (yet).
        stripe.confirmCardPayment(
        clientSecret,
        {payment_method: ev.paymentMethod.id},
        {handleActions: false}
        ).then(function(confirmResult) {
        if (confirmResult.error) {
            // Report to the browser that the payment failed, prompting it to
            // re-show the payment interface, or show an error message and close
            // the payment interface.
            ev.complete('fail');
        } else {
            // Report to the browser that the confirmation was successful, prompting
            // it to close the browser payment method collection interface.
            ev.complete('success');
            // Check if the PaymentIntent requires any actions and if so let Stripe.js
            // handle the flow. If using an API version older than "2019-02-11" instead
            // instead check for: `paymentIntent.status === "requires_source_action"`.
            if (confirmResult.paymentIntent.status === "requires_action") {
            // Let Stripe.js handle the rest of the payment flow.
            stripe.confirmCardPayment(clientSecret).then(function(result) {
                if (result.error) {
                    let data = {
                        msg: "An error occurred. Please try again."
                    }
                    this.handleShowFlashMsg(data);
                } else {
                    this.handleShowOrderConfirmModal();
                }
            });
            } else {
            // The payment has succeeded.
            }
        }
    });
}

Answer: You're almost there. When you get the 'paymentmethod' event you would use the PaymentIntent client secret you retrieved earlier to confirm the PaymentIntent and complete the payment:
    let clientSecret;

    axios.post('/api/checkout/checkout-with-apple-pay-web', {
        total: this.formattedCartTotal()
    }).then((resp) => {
      // Assign this previously defined variable
      clientSecret = resp.clientSecret;
    });

    paymentRequest.on('paymentmethod', function(ev) {
        // Confirm the PaymentIntent without handling potential next actions (yet).
        stripe.confirmCardPayment(
        clientSecret,
        {payment_method: ev.paymentMethod.id},
        {handleActions: false}
        ).then(function(confirmResult) {
        if (confirmResult.error) {
            // Report to the browser that the payment failed, prompting it to
            // re-show the payment interface, or show an error message and close
            // the payment interface.
            ev.complete('fail');
        } else {
            // Report to the browser that the confirmation was successful, prompting
            // it to close the browser payment method collection interface.
            ev.complete('success');
            // Check if the PaymentIntent requires any actions and if so let Stripe.js
            // handle the flow. If using an API version older than "2019-02-11" instead
            // instead check for: `paymentIntent.status === "requires_source_action"`.
            if (confirmResult.paymentIntent.status === "requires_action") {
            // Let Stripe.js handle the rest of the payment flow.
            stripe.confirmCardPayment(clientSecret).then(function(result) {
                if (result.error) {
                    let data = {
                        msg: "An error occurred. Please try again."
                    }
                    this.handleShowFlashMsg(data);
                } else {
                    this.handleShowOrderConfirmModal();
                }
            });
            } else {
            // The payment has succeeded.
            }
        }
    });

Comment: thanks @paul  .  How does that event listener getting fired though ( `paymentRequest.on('paymentmethod', ...)` ?  The only thing that comes back from the axios request is the clientSecret so what triggers that event listener?
Comment: That event is triggered when your user has completed the Payment Request modal, which itself is triggered when they click the Payment Request button. Stripe automatically adds the event listener in the `prButton.mount('#payment-request-button');`  line.


Poisson model for non-integer

Question: I have a GLM with (quasi)poisson family.
My dataset has 3 variables:
rate_data
rate_benchmark
X
So fitting the model:
model <- glm(formula = rate_data ~ offset(log(rate_benchmark)) + X - 1, family = (quasi)poisson, data = data)

model_null <- glm(formula = rate_data ~ offset(log(rate_benchmark)) - 1, family = (quasi)poisson, data = data)

When using "poisson" it gives me warnings about non-integer values, which it doesnt give me for the quasipoisson. However, when testing for my beta being zero anova(model_null, model, test = "LRT") it gives me completely different deviance (hence also different p-values).
Which model am I supposed to use? My first thought was using quasipoisson, but no warnings does not necessarily mean it is correct.
Comment: Poisson and quasi Poisson both require integer response  variables. The warning message was probably not added to the quasi Poisson code.
Comment: If you are able to convert the rate data back to counts, you can probable use Poisson regression on the counts with an offset to account for the differences in the denominators for the rates.
Comment: Perhaps beta regression or Gamma regression would be appropriate for your data ?
Comment: @SalMangiafico That was also my first thought. However, this is to comply with national legislation. I have to test in a parametric poisson model whether my rates deviate from their benchmark with the regressor X. So perhaps I should choose the normal poisson.
Comment: @Eli Do you have a way to show he warnings?
Comment: If you're required to use Poisson, I guess you have to use Poisson. ...  I don't know how the function reacts to non-integer values. I don't know if it makes sense to run a test, say, with integer values, and then these same data, divided by 10, and see if the results change in any meaningful way.
Comment: @Overkill123, is your data supposed to be integer valued?
Comment: @Eli that could be. My rate is probably some fraction. I could maybe search for it in my data and multiply with the denominator to get a non-negative integer.
Comment: I see what's happening. The Poisson distribution is only defined on integers , but R must use the gamma function to evaluate the factorial in the Poisson likelihood. Since you can evaluate the gamma functions at non-integers, the model converges but R throws a warning to make you aware of the non-integers.  Your best bet is to convert your response back to integers. If you have a few non-integers that make sense (e.g. a machine reports 9 and 10 that got averaged to 9.5), you'll probably be okay keeping it even if it's not technically correct.
Comment: If you're scaling to rates, and then trying to fit a Poisson - even if you could succeed - means your variance calculations will all be wrong, and hence, p-values. You should change back to unscaled counts (which is what the Poisson is a model for) and use log-exposure as an offset in a log-link Poisson GLM
Answer: The Poisson and quasi-Poisson models differ in their assumptions about the form of the function relating the mean and variance of each observation. The Poisson assumes the variance equals the mean; the quasi-Poisson assumes that $\sigma^2 = \theta\mu$, which reduces to the Poisson when $\theta=1$. Consequently, the deviance and p-values will, as you have observed, be different between the two models.
You can in fact run a Poisson regression on non-integer data, at least in R; you'll still get the "right" coefficient estimates etc.  The warnings are there as warnings; they don't represent an algorithm failure.  Here's an example:
z <- 1 + 2*runif(100)
x <- rgamma(100,2,sqrt(z + z*z/2))
summary(glm(x~z, family=poisson))

... blah blah blah ...

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  0.56390    0.17214   3.276  0.00105 **
z            0.43119    0.08368   5.153 2.57e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

... blah blah blah ...

There were 50 or more warnings (use warnings() to see the first 50)

Now we'll compare to a pure "quasi" model with the same link function and relationship between mean and variance; the "quasi" model makes no assumptions about integer values for the target variable:
summary(glm(x~z, family=quasi(link="log", variance="mu")))

... stuff ...

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   0.5639     0.2621   2.151  0.03392 *
z             0.4312     0.1274   3.384  0.00103 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for quasi family taken to be 2.318851)

Note that the parameter estimates are exactly the same, but the standard errors are different; this is due to the different calculations of variance, as reflected by the different dispersion parameters.
Now for the quasi-Poisson model, which will, again, give us the same parameter estimates as the Poisson model, but with different standard errors:
summary(glm(x~z, family=quasipoisson))

... more stuff ...

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   0.5639     0.2621   2.151  0.03392 *
z             0.4312     0.1274   3.384  0.00103 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for quasipoisson family taken to be 2.31885)

Since the mean-variance relationship and link functions are the same as in the "quasi" model, the model results are the same also.
Answer: The Poisson distribution deals with counts -- the actual number of objects you counted in a defined volume,  or the actual number of events you counted in a defined period of time.
If you normalized to a rate, the distribution is not Poisson.
Answer: @jbowman said that "You can in fact run a Poisson regression on non-integer data, at least in R; you'll still get the "right" coefficient estimates etc.". I have tried this out and saw problems in the "etc" part.
My dataset is mortality rates from Sweden , I have extracted the observations for females born in 1905. The resulting data frame contains 4 columns: age (in years, this is the predictor), ndead (number of people dying at that age), N (the total number of people in that age cohort) and haz = $n_{dead}/N$ which is the mortality rate.
First, let's perform an OLS with $\ln(\mathrm{haz})$ as the response variable to establish a "baseline", so to speak:
Call:
lm(formula = log(haz) ~ age, data = md)

Residuals:
      Min        1Q    Median        3Q       Max
-0.088305 -0.034501  0.000204  0.034399  0.075291

Coefficients:
              Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.091e+01  6.900e-02  -158.0   <2e-16 ***
age          1.012e-01  9.136e-04   110.8   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.04549 on 29 degrees of freedom
Multiple R-squared:  0.9976,    Adjusted R-squared:  0.9976

F-statistic: 1.228e+04 on 1 and 29 DF,  p-value: < 2.2e-16

If I run a Poisson GLM in R using log(N) as the offset I get a very similar result:
Call:
glm(formula = ndead ~ age + offset(log(N)), family = poisson,
    data = md)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-2.7945  -0.7639   0.1338   1.1044   1.8624

Coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept) -10.808320   0.052330  -206.5   <2e-16 ***
age           0.099560   0.000661   150.6   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 24739.939  on 30  degrees of freedom
Residual deviance:    47.526  on 29  degrees of freedom
AIC: 324.69

Now let's perform the Poisson GLM fit using haz as the response variable. The parameter estimates are sort of similar to the fit above. However, look at the estimates of the standard errors of the coefficients: they are much higher than in the "offset" case, the residual deviance is unrealistically low and the AIC goes to infinity:
Call:
glm(formula = haz ~ age, family = poisson, data = md)

Deviance Residuals:
       Min          1Q      Median          3Q         Max
-0.0174605  -0.0048843   0.0008723   0.0071586   0.0212824

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept) -11.0679     8.9329  -1.239    0.215
age           0.1033     0.1084   0.953    0.340

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 1.1368277  on 30  degrees of freedom
Residual deviance: 0.0024028  on 29  degrees of freedom
AIC: Inf

From this I conclude that the Poisson GLM implementation in R (I used Version 4.2.1) cannot really deal with non-integer response variables. This is not surprising, because the Poisson distribution is discrete and "knows nothing" about ratios.
However... here is a twist to the tale: the Quasi-Poisson apparently can deal with mortality rates! Witness this result:
Call:
glm(formula = haz ~ age, family = quasipoisson, data = md)

Deviance Residuals:
       Min          1Q      Median          3Q         Max
-0.0174605  -0.0048843   0.0008723   0.0071586   0.0212824

Coefficients:
              Estimate Std. Error t value Pr(>|t|)
(Intercept) -11.067922   0.081368  -136.0   <2e-16 ***
age           0.103309   0.000987   104.7   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for quasipoisson family taken to be 8.296971e-05)

    Null deviance: 1.1368277  on 30  degrees of freedom
Residual deviance: 0.0024028  on 29  degrees of freedom
AIC: NA

Note that the parameter estimates and the deviance values are the same (within numerical error) as what I got from the haz ~ age formula with the Poisson GLM, but the error estimates are much more reasonable.
Since I don't know how the Quasi-Poisson magic is implemented in R, I have no idea whether this was just a lucky coincidence or it is supposed to work like this all the time. The safe bet would be to restrict ourselves to fitting Poisson GLM-s to count data only, because that's what the Poisson distribution is for (number of independent events in a generic interval).
Comment: I think this STATA blog post might be relevant: [Use poisson rather than regress; tell a friend](https://blog.stata.com/2011/08/22/use-poisson-rather-than-regress-tell-a-friend/). The trick is to use robust estimate of the variance.

Poisson model for non-integer


How to add mulitple legends in chart created with the Forena reports module?

Question: I have created a report using the module Forena reports. It's a compound graph of 3 items.
I need to mention these 3 items in legend. But I am not able to add more than 1 legend.
I am trying to create this graph from the front end itself. I don't want to use the frx file.
Comment: Consider enhancing your question by adding the relevant part of the Forena report that you're trying to create. At least the SVG-tag part of  its FRX file that contains **frx:renderer="FrxSVGGraph"**. That may help to answer your question.
Comment: Sorry about that. I have modified my question as you suggested
Answer: Any attribute supported as PHP SVGGraph options may be included as frx attributes also. Checkout the PHP SVGGraph library documentation to fully understand the available options, and identify the actual SVG Graph option you should be using (there are plenty of them).
In your case, I think you're looking for SVG Graph legends. In your Forena FRX file, you should be able to specify the desired legends (say 1st Legend, 2nd Legend and 3rd Legend) within the SVG tag of your chart with something similar to this example:
  <svg frx:renderer="FrxSVGGraph"
    frx:legend_entries_1="1st Legend"
    frx:legend_entries_2="2nd Legend"
    frx:legend_entries_3="3rd Legend"
    ...
  />

For many more details about how to create charts with Forena, refer to the reports/help.renderers#frxsvggraph in your own site (which is part of what comes with Forena), or refer to the online equivalent of it.
Note: if you only have 1 legend (say MyLegend), the SVG tag of your chart should be similar to this example (no need for the suffixes like _1, etc):
  <svg frx:renderer="FrxSVGGraph"
    frx:legend_entries="MyLegend"
    ...
  />

Comment: Actually I got it working without opening the frx file. I fetched the values that I want for in the sql query itself and mentioned that in the "legends" field.
Comment: Thanks for the extra feedback. I bet that after you corrected your report (chart) specifications using the WYSIWYG reporting interface as in your comment above, the result of it got saved in the .FRX file using those legend_entries_1, etc as I suggested (which you should see if you browse the FRX file). FYI: the WYSIWYG reporting interface is typically used by Forena newbies (to get started with Forena). After gaining more experienced with Forena, many of its "power" users start using the approach of editing the FRX files (which is just an XHTML file).
Comment: I didn't have to use WYSIWYG reporting interface. Anyway, thank you for the help.
Comment: This was not the solution I used. As im entioned in mthe previous comment I got it working without using the frx file. Editing the frx file was causing problems for me . After saving the frx file, if you edit the graph from front end the above solution won't work.
Answer: This is how I got it working.
I fetched the values I want to show as the legends in the sql query I used itself.
Then I assigned this value as the legend.
Please find the screenshot.


R Arules Market Basket Analysis Redundant Rules Approaches

Question: Using R and arules. I see several opinions on how to remove redundant rules which give different answers.
If this is my example dataset:
    lhs                rhs                support confidence lift
[1] {r7sVi9T6D1nE}  => {hN1sUFRI}         0.0013  0.80       210.9
[2] {hN1sUFRI}      => {r7sVi9T6D1nE}     0.0013  0.33       210.9
[3] {8l0QeRHU0CWLP} => {XUFbPOzrmrKJgcmi} 0.0016  0.56         5.8
[4] {9f7J8ox1}      => {YTeK1f0yQd9hvPz2} 0.0016  0.36         2.1
[5] {NCzMaUfT}      => {eByLP-ea}         0.0022  0.47        77.7
[6] {eByLP-ea}      => {NCzMaUfT}         0.0022  0.37        77.7
[7] {sUvng3}        => {8D4AhPCsnBT}      0.0016  0.36         9.2

The most common approach I've seen is:
# source: http://www.rdatamining.com/examples/association-rules
rules <- sort(rules, by = 'lift')
subset.matrix <- is.subset(rules, rules)
subset.matrix[lower.tri(subset.matrix, diag=T)] <- NA
redundant <- colSums(subset.matrix, na.rm = TRUE) >= 1
which(redundant)
rules <- rules[!redundant]

Outputs:
    lhs                rhs                support confidence lift
[1] {r7sVi9T6D1nE}  => {hN1sUFRI}         0.0013  0.80       210.9
[2] {eByLP-ea}      => {NCzMaUfT}         0.0022  0.37        77.7
[3] {sUvng3}        => {8D4AhPCsnBT}      0.0016  0.36         9.2
[4] {8l0QeRHU0CWLP} => {XUFbPOzrmrKJgcmi} 0.0016  0.56         5.8
[5] {9f7J8ox1}      => {YTeK1f0yQd9hvPz2} 0.0016  0.36         2.1

However, arules provides is.redundant:

Provides the generic functions and the S4 method is.redundant to find
  redundant rules.

If I do in RStudio console:
> is.redundant(rules)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Rules all show FALSE indicating no rules are considered redundant.
Another source shows the following approach to remove redundant rules:
# source: http://rstatistics.net/association-mining-with-r/
redundant <- which (colSums (is.subset (rules, rules)) > 1) # get redundant rules in vector
rules <- rules[-redundant] # remove redundant rules

But this produces different outputted results than above:
    lhs                rhs                support confidence lift
[1] {8l0QeRHU0CWLP} => {XUFbPOzrmrKJgcmi} 0.0016  0.56       5.8
[2] {9f7J8ox1}      => {YTeK1f0yQd9hvPz2} 0.0016  0.36       2.1
[3] {sUvng3}        => {8D4AhPCsnBT}      0.0016  0.36       9.2

Which Approach Is Right And Correct?
Answer: I dont think the last approach is right. Looking at your output, the last approach removes the non redundant rules as well.
For me the 1st approach gave me an empty set.


personalizar input para multiupload de imagens

Question: Como posso personalizar um campo input do tipo file para multiupload de imagens?
gostaria de pegar algumas informações da imagem, como: formato, tamanho.
(sei que existem diversos plugins para tal, mas gostaria de aprender do zero como fazer)
Ex:
Comment: Cara, eu costumo fazer uma div com para estilizar e o input eu deixo com `display:none`, aí em Jquery eu faço o clique da div ser no input.
Comment: Recentemente estou usando o [jQuery FileUpload](https://github.com/blueimp/jQuery-File-Upload). Ele é bem complexo, então pode ser que você tenha um certo trabalho em implementar algo exatamente nesse estilo, com tamanho do arquivo, ação de cancelar e etc.
Comment: Talvez se estudar o código fonte destes plugins, ajude também.
Answer: Existem limitações de que tipo de informação está acessível no JavaScript. Do lado do servidor é diferente e pode obter-se mais informação. Mas ainda assim alguns parâmetros estão acessíveis.
Exemplo:

$('#ficheiros').on('change', function () {
    var label = $(this).prev();
    var tamanho = this.files[0].size;
    var nome = this.files[0].name;
    var tipo = nome.split('.').pop();
    var info = 'Nome do fichiero: ' + nome + ', Tipo de extensão: ' + tipo + ', tamanho em bytes: ' + tamanho
    label.html(info);
});

<input id="ficheiros" type="file" />

Tamanho do ficheiro
Acessível via this.files[0].size. Para converter em Kb pode usar esta função que encontrei:
function bytesToSize(bytes) {
   var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
   if (bytes == 0) return '0 Byte';
   var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
   return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};

Nome do ficheiro
Acessível via this.files[0].name;
Extensão do ficheiro
Tendo o nome do ficheiro pode fazer nome.split('.').pop() para obter só a extensão.


Let $A$ be a subset of $\Bbb R$ with more than one element. Let $a \in A$. If $A\setminus\{a\}$ is compact. Then prove that $A$ is disconnected.

Question: Let $A$ be a subset of $\Bbb R$ with more than one element. Let $a \in A$. If $A\setminus\{a\}$ is compact. Then prove that $A$ is disconnected.
My Attempt:
We know that if $A = B \cup C$ such that $B \neq \phi$,
$C \neq \phi$ with $\overline B\cap C$ = $\phi$, $B \cap \overline C$ = $\phi$ then $A$ is called disconnected. Here $A = A\setminus\{a\} \cup \{a\}$ such that $\overline A\setminus\{a\} \cap \{a\} = \phi$
, $A\setminus\{a\} \cap \overline{\{a\}}$ = $\phi$ $\implies$ $A\setminus\{a\} \cap \{a\} = \phi$, $A\setminus\{a\} \cap \{a\}$ = $\phi$ because
$A\setminus\{a\}$ and $\{a\}$ are closed sets so $\overline A\setminus\{a\} = A\setminus\{a\}$ and $\overline {\{a\}} = \{a\}$ Am I right? Please provide me alternate proof. Thanks.
Answer: As $A\backslash\{a\}$ is compact in $\mathbb{R}^n$, you know that $A\backslash \{a\}$ is closed in $\mathbb{R}^n$ and is hence closed in $A$ in the subspace topology. Also $A\backslash\{a\}$ is open in $A$ as singleton sets are closed in the subspace topology. Thus you find that $A\backslash\{a\}$ is an open and closed proper subset of $A$. Thus $A=(A\backslash\{a\})\cup \{a\}$ is a separation of $A$ into disjoint non-empty open and closed sets. Thus $A$ is disconnected.
Note: The definition of disconnectedness you gave is equivalent to the condition $A=B\cup C$ where $B,C$ are non-empty open subsets of $A$ and $B\cap C\neq \emptyset$. You can try to prove it.


JSP not displaying DB data on index page, Java Web App using Derby

Question: I am building a simple CRUD application and after the form is submitted, the data goes to the DB. I am trying to loop through the data and show it in a listing page but cannot make it work and after hours of searching,, I come to you for an answer.
This the the listing page code where data is to be displayed. The DAO and Bean class all seem to be in order but I'll gladly upload if needed. Thanks.
<%@ page import="java.util.List, models.beans.*, models.entity.*"%>
<jsp:useBean id="load" class="models.beans.LoadBean" scope="request"></jsp:useBean>

<% List<Load> loadListing = load.findAll();%>

<% for (Load lod : loadListing) {%>

      <%= lod.getApptTime()%><%= lod.getCarrier()%>

      Order #
      Vendor
      Cases
      Pallets
      Unload Start
      Unload End
      Total Cost
      Notify Driver

      <%= lod.getWorkOrder()%>
      <%= lod.getVendor()%>
      <%= lod.getCases()%>
      <%= lod.getPallets()%>

          <input type='text' class="form-control" value="<%= lod.getUnldStart()%>" readonly/>

          <input type='text' class="form-control" value="<%= lod.getUnldEnd()%>" readonly/>

          <input type='text' class="form-control" value="$234.34" readonly/>

        <button class="btn btn-md btn-primary">Send</button>

        <a class="btn btn-sm btn-warning" href="<%= request.getContextPath()%>/pages/form.jsp?id=<%= lod.getId()%>">Edit

        <a class="btn btn-sm btn-warning" onclick="return confirm('Are you sure?');" href="<%= request.getContextPath()%>/pages/delete.jsp?id=<%= lod.getId()%>">Delete

    <%}%>

Comment: This is not a good way. Do tutorial http://o7planning.org/en/10285/create-a-simple-java-web-application-using-servlet-jsp-and-jdbc#a827434
Answer: Try out this i think this will help you-
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:forEach items="${yourList}" var="var">

        <c:out value="${count = count+1 }" />
        ${var.attributeName}

      </c:forEach>


Can't figure out why block won't float

Question: I have a problem with CSS floating
Here you can see what it looks like:
http://migo.no/help/help.php
Basically, I have two divs and I need the last one to float left. It's important that the last div floats left.
HTML code:

block1

block2

CSS code:
#wrapper {
    overflow: hidden;
    background: red;
}

#block1 {
    background: yellow;
}

#block2 {
    background: green;
    float: left;
}

I don't understand why it won't float left, and I have been trying to figure this out for a while now.
Best of regards,
Alexander
Comment: Do you want to make `#block2` on the left side of `#block1` or do you want it to be exactly on the right side of `#block1`?
Answer: Did you close the wrapper div? Also you don't have block 1 floating left. from your link it appears that Block 2 is floating left. Do you have a more descriptive Idea of what you want to accomplish?
Comment: block 1 does not have float becouse i want block 2 left of block 1, and i need this becouse in my code i need the last block of two , to float left, not the first one, but the second one.
Comment: if you want one to sit next to the other then they both have to be floated, otherwise block one will assume 100% width. Regardless of what others tell you you don't need to specify width. float them both and you'll achieve your goal
Answer: block2 is clearing  block1 and floating left. If you want block2 to show up on the left side of block1 you'll need to either make block2 come before block1 in the html, or float block1 to the right. Please be sure to read through the CSS spec.
Comment: If you already knew that `block2` was floating left, why did you state in your question, "I dont understand why it wont float left, and i have been trying to figure this out for a while now."?
Answer: The div is floating left.
If you want the divs to be beside each other, you'll have to specify a width for them both and set them both to float left, like Jason suggested.
Answer: Actually you don't really need the width for the blocks (1 and 2), but you can use it if it's necessary for the crossbrowser. What you have to do is to float the block1 to the left side also.
#block1 {
float:left;
    background: yellow;
}
#block2 {
    float: left;
    background: green;
}

I just did test it on my machine (FFox, IE7, 8 and 9) and it's working fine, once you float the block1 to the left side.
Which Browser are you using for this? and are you using the Standard or the Quirksmodus?
Answer: Set "float:left" to 1st div also which should solve the issue
Answer: This way it works:

block2

block1

Answer: Floats usually need an explicit width set.
If you want both divs beside each other, then you need to give widths to each and float then both.
Also, overflow: hidden; will clear the float, so if you are expecting the div to float outside the wrapper, it won't happen.
Comment: Thanks @zzzzBov. Agreed.  That's why I wrote *Floats **usually** need an explicit width set*.  In many contexts you will need to set the width to achieve the desired result.  Thanks for the clarification, though.  In the future, I think it helps for people (those answering and those reading) to know why something is marked incorrect.
Comment: i allready tried that, now they both have width, still they dont float

Can't figure out why block won't float


Generalization of a product measure

Question: Let $(X,\mathfrak B(X))$ and $(Y,\mathfrak B(Y))$ be measurable spaces and further let $\mu$ be a measure on $\mathfrak B(X)$ and let $K$ be a kernel, i.e. for any $x\in X$ we have $K_x$ is a measure on $\mathfrak B(Y)$ and the map $x\mapsto K_x(B)$ is $\mathfrak B(X)$-measurable for any $B\in \mathfrak B(Y)$. Let us further assume that $\mu$ is finite and measures $K_x$ are uniformly bounded. Then there exists a unique measure $P$ on $\mathfrak B(X)\otimes \mathfrak B(Y)$ such that
$$
  P(A\times B) = \int_AK_x(B)\mu(\mathrm dx)
$$
for any measurable rectangle $A\times B$. Thus, $P$ can be considered as a certain product $\mu\otimes_{?}K$ and in case $K_x\equiv\nu$ we have $P = \mu\otimes \nu$ is just a product measure. We know that product measures are not enough to describe all possible measure over the product space, whereas the construction above is thanks to the disintegration theorems. I wonder what are the nice sources for the properties of the construction $\mu\otimes_{?}K$ - e.g. does it have its own name.
Comment: In this German script about stochastic processes: http://www3.math.tu-berlin.de/Vorlesungen/WS11/WT2/WT(2)Bolthausen09.pdf it is called *semidirect product measure*. The author then goes on concatenating several kernels in order to have notation for a proof of the Ionescu-Tulcea extension theorem.
Comment: @Ilya: The definition is on page 19, but the section starts on page 17 with the definition of product $\sigma$-algebras and  Markov kernels.
Comment: @Ilya One can do this with a bit of trickery. It is possible to take products of kernels. Let $\kappa$ from $X$ to $Y$ be a prob kernel, $I_X$ the identity kernel from $X$ to $X$. Then you can construct a product kernel $\kappa\otimes I_X$ from $X$ to $X\times Y$ that does this. I'm in a train right now, but I will post the details later.
Comment: [Disintegration theorem](http://en.wikipedia.org/wiki/Disintegration_theorem).
Comment: @Did as far as I understand (and as stated in OP) this theorem proves an existence of K given P and $\mu$ and hence is somehow an inverse statement to the existence of P given K and $\mu$. I'm interested in the latter, which properties does it have, and in particular whether there is a special notation or a name for such product. Perhaps, a joint measure? Maybe you can suggest anything?
Comment: The point of the disintegration theorem is that in sufficiently well-behaved spaces, this is exactly the same as simply being a finite measure on $\mathfrak{B}(X)\otimes\mathfrak{B}(Y)$. One can also treat the first marginal measure as a constant kernel. Finite measures and kernels form a category, but I only saw this category used in the context of probability-measures and -kernels, or sub-probability measures and kernels.
Comment: @Thomas: nice! this is perhaps something that I am looking for - could you please point out the page? My German is really bad and it may take me some time to find the right place (not to say I may miss it)
Comment: @Thomas: oh no, this lectures are so awesome - and they're in German! I'll check also his main references - maybe some of them contain this and are available in English. Otherwise, I'll spend my time anyway reading this notes. Please put this reference as an answer. Though he uses some symbols in an unusual way: $\wedge$ not for an $\inf$ but for the concatenation, and the fuzzy arrow for the definition of the kernel's dom and cod
Comment: @MichaelGreinecker: I was mostly looking for the source alike Thomas has provided: comprehensive introduction on the "forward" operations with conditional kernels (like their product measures) and their properties. The point is that i the Markov Chain literature I've seen, the only product between kernel introduced is
$$
  KL(x,C) = \int_Y L(y,C)K(x,\mathrm dy)
$$
where $K$ is a kernel on $Y$ given $X$ and $L$ is a kernel on $Z$ given $Y$. This is however not a product I am looking for since $KL$ is a kernel on $Z$ given $X$, not on $Y\times Z$ given $X$ as $K\otimes_{?}L$ is intended to be.
Comment: @MichaelGreinecker: thanks, that would be very nice
Answer: The following is in terms of probability kernels, which I'm most familiar with, but much of it generalizes. If $(X,\mathcal{X})$, $(Y,\mathcal{Y})$, and $(Z,\mathcal{Z})$ are measurable spaces and $\kappa_1:X\times\mathcal{Y}\to[0,1]$ and $\kappa_2:Y\times\mathcal{Z}\to[0,1]$ are probability kernels, we can define a composition $\kappa_2\circ\kappa_1:X\times\mathcal{Z}\to [0,1]$ by $$\kappa_2\circ\kappa_1(x,B)=\int\kappa_2(y,B)~d\kappa_1(x,d(y)).$$
One can show that this composition is associative. Measurable spaces and probability kernels form a category. This category was called the category of probabilistic mappings by Lawvere in unpublished notes from 1962. The same category was named the category of statistical decision functions in the book Statistical Decision Rules and Optimal Inference by Cencov (based on his dissertation). A nice overview of material on this category can be found in the introduction of a paper by Culbertson and Sturtz.
If $f:X\to Y$ is a measurable mapping, there is a corresponding kernel $\kappa_f:X\times\mathcal{Y}\to[0,1]$ given by $$\kappa_f(x,B) =
\begin{cases}
1 & \text{if } f(x)\in B\\
0 & \text{if } f(x)\notin B.
\end{cases}$$
This way, we can view measurable mappings as special kernels. If $(X_i,\mathcal{X}_i)$ and $(Y_i,\mathcal{Y}_i)$ are families of measurable spaces and $\kappa_i:X_i\times\mathcal{Y}_i\to[0,1]$ is a kernel for all $i$, then there is a product-kernel $$\kappa_\times:\prod_i X_i\times\otimes_i\mathcal{Y}_i\to [0,1]$$ such that $\kappa_\times(x,\cdot)=\otimes_i\kappa_i(x_i,\cdot)$.
We can also treat a probability measure as a constant-valued kernel. If we combine these ideas, we can take a probability measure (seen as a kernel) $\mu$ on $(X,\mathcal{X})$ and a kernel $\kappa:X\times\mathcal{Y}\to [0,1]$ to get a natural distribution on $\mathcal{X}\otimes\mathcal{Y}$. We let $p:X\to X\times X$ be given by $x\mapsto (x,x)$. Let $\kappa_p:X\to\mathcal{X}\otimes \mathcal{X}$ be the corresponding kernel. Let $\kappa_1:X\times\mathcal{X}$ be the kernel corresponding to the identity on $X$. We let $\kappa_\times$ be the product kernel of $\kappa_1$ and $\kappa$. Then $\kappa_\times\circ\kappa_p\circ\mu$ is essentially the natural distribution on $\mathcal{X}\otimes\mathcal{Y}$ such that $\mu$ is the marginal on $X$ and $\kappa$ gives the conditional probabilities on $Y$ given $x$. So the generalized product measure is really a special form of composing kernels.
Comment: Thanks a lot for this nice answer!
Comment: @Ilya Thanks for the bounty!
Comment: I've taken a brief look on your references (as some of them are pretty long), but haven't find a construction with $\kappa_\times\circ \kappa_p\circ \mu$ for the joint distribution given conditionals. Did you come up with it by yourself, or have you seen it somewhere else as well?
Comment: @Ilya I came up with it, but I doubt I'm the first one to do so.
Answer: There are lecture notes from Erwin Bolthausen for a course in stochastic processes in discrete time, albeit in German: www.math.uzh.ch/index.php?file&key1=11678
On page 17 he introduces Markov kernels and on page 19 the construction from the OP is defined and called »semidirektes Produktmass«, which could be translated as »semidirect product measure«. Shortly after that a sort of wedge product between kernels is introduced. All this notation is needed for his proof of the Ionescu-Tulcea Theorem.
Comment: For the sake of historical accuracy: The Ionescu-Tulcea theorem is not due to Alexandra Ionescu-Tulcea, as claimed in the PDF. It is due to Cassius Ionescu-Tulcea.
Comment: Wow, I feel cheated now. How did the author come to think it was her? Is that a common misattribution?
Comment: I'm not sure. But I was once curious about it. I guess the story would be more fun with Alexandra Below in the lead.

Generalization of a product measure


Java.lang.UnsupportedClassVersionError on MacOS

Question: I am trying to run an Java Applet application that ran fine on Windows XP (using Eclipse IDE) but after I imported the project on MacOS X (Using Eclipse on MacOS too), it fails to run displaying this error:
<code>
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
    at edu.isi.calo.mapping.gui.DieselUtil.(DieselUtil.java:9)
    at edu.isi.diesel.DieselEditor.processSettingsFile(DieselEditor.java:597)
    at edu.isi.diesel.DieselEditor.main(DieselEditor.java:80)
</code>
Has anybody faced such a problem? I deleted all the class files and built the whole project again but it still pops up this runtime error.
Comment: Which version of OSX are you running and what version of Java?
Answer: What that message is telling you is that your JRE does not know how to execute the class because the class has too new a version.  Two obvious questions are:

What version of the JRE do you have on Windows XP?
Do you have Apple's latest distro of the JRE, which is supposed to support Java 6?

I would guess that the version of the JRE you're using know is pre-Java 5.
Comment: -agree.  That Error happens when some class (maybe in a 3rd party Jar, not your code) was compiled with a newer JDK than what you are running.  It typically only happens for different major versions (1.4-1.5, 1.5-1.6, ...).
Comment: you can check the version by using the Java Preferences application on os X.
Comment: It helped me solve the problem! Thanks a lot :) Eclipse was using JRE 1.5 as default environment. Changing it to 1.6 solved the problem.


create subclass object with reference to existing object

Question: In node.js (Javascript) I have two classes, class mainclass and subclass, where subclass inherites from mainclass.
In an other module (other class, other .js file) i have an array of objects from class mainclass:
myArray[0] = new mainclass();
myArray[1] = new mainclass();
//etc..

On runtime, i want to create a new subclass object, and set its reference to the one in myArray[0], so that myArrayis not changed, but myArray[0] then returns the new subclass object.
And i want to do this in the mainclass, so that the array is not changed, but the reference in the array points now to an other object (the new subclass object). In fact i want to do something like
this = new subclass();

in a method in mainClass
mainClass.prototype.changeType = function(){
 this = new subclass();
}

which of course doesnt work because you cant assign value to this.
Comment: You can't assign to the `this` keyword
Comment: What does the array looks like ?
Comment: So you have an array of `mainclass` and want to change that to an array of `subclass`?
Comment: You can't replace a "new" with another "new". Javascript is not as powerful as C, sorry :-|
Comment: @schroffl i basically want the references in the array pointing to other objects than before
Comment: @Lucker10 That's what I'm saying, Javascript has its limits.
Comment: Then you can just loop through the array and set the elements to an instance of `subclass`
Comment: @schroffl but i have no access to the array. I tried to clarify the question
Answer: You could "simulate" pointers if you are ready to access your objects through indexes. As you can see below, whatever object reference is at index 0, it remains available :
function Person (name) { this.name = name; };
Person.prototype.whoami = function () { return this.name };
memory = [];
memory.push(new Person("Hillary Clinton"));
memory[0].whoami(); // "Hillary Clinton"
memory[0] = new Person("Donald Trump");
memory[0].whoami(); // "Donald Trump"

Good luck though... x-D
Comment: @Lucker10 As far as I know, there is no such possibility.
Comment: I dont want to change the array, but the references stored point to other objects than before..
Comment: @Lucker10 If you always access an object through its index, replacing it with another should be harmless.
Comment: yeah.. in C it would work easy. I thought maybe JS is so hacky that you can set an objects reference by (new mainclass).setReference(..) or something
Comment: @Lucker10 Javascript is not design to perform this kind of stuff, and that's fine. If you really need to touch the memory you can still choose another tool like C. Otherwise there are plenty of ways to bypass your problem while keeping a clean and elegant code :-)
Comment: but no way without accessing the array, or not?
Comment: Read my previous previous comment again : no x-)


Mostrar termos de uma taxonomia aleatoriamente

Question: Eu estou exibindo todas as categorias com suas respectivas imagens, conforme o código abaixo:
<?php
categorias = apply_filters('taxonomy-images-get-terms', '', array( 'taxonomy' => 'category') );
foreach($categorias as $key => $categoria):
    if($key < 6):
    ?>

        <a href="<?php echo get_category_link($categoria->term_id);?>">

                <?php echo wp_get_attachment_image($categoria->image_id,''); ?>

            <img src="<?php bloginfo('template_directory');?>/images/icones/plus.png" class="plus-icone" alt="">

<?php echo $categoria->name;?>

    <?php
    endif;
endforeach;?>

Foi usado a função apply_filters para poder exibir a imagem da categoria, até onde eu consegui achar na internet só dessa forma que é possível fazer isso. Pelo que notei essa função mostra todos os resultados em ordem alfabética e eu preciso mostra-los aleatoriamente ou por data.
É possível fazer isso?
Answer: Ao que parece apply_filters está sempre retornando um array ali, então basta reordenar o array usando shuffle pra ficar embaralhado:
$categorias = apply_filters('taxonomy-images-get-terms', '', array( 'taxonomy' => 'category') );

shuffle( $categorias );

foreach ( $categorias as $key => $categoria ) : //etc

Comment: Você fala em reordenar por data, mas não tem uma referência de data no código. Se souber qual o parâmetro que traz essa informação eu posso colocar um exemplo.
Comment: Não tenho como testar agora, mas ficando embaralhado já vai resolver o meu problema. Amanhã cedo irei testar. Muito obrigado.
Comment: Ricardo, deu certinho, agradeço mais uma vez pela ajuda. Nem tinha pensando em pesquisar uma forma de reordenar o array, fiquei focando nas funções do Wordpress.


Como fazer requisição de arquivo externo NodeJS

Question: Como faço pra requisitar um arquivo externo com o NodeJS?
No caso seria um XML...
http://220.127.116.11:8449/played.html?sid=1&type=xml
Answer: Tu pode fazer o seguinte, usar o modulo http para dar um get e converter o path em xml para uma objeto.
Criei como uma promise assim podes usar await em uma function async para esperar o get.
<code>const http = require('http');
const xml2js = require('xml2js');
const parser = new xml2js.Parser();
const concat = require('concat-stream');

parser.on('error', function(err) { console.log('Parser error', err); });

const getXml = async()=>{
    return new Promise((resolve, reject) =>{
        http.get('http://220.127.116.11:8449/played.html?sid=1&type=xml', function(resp) {
            resp.on('error', function(err) {
                reject(err)
            })
            resp.pipe(concat(function(buffer) {
                const str = buffer.toString();
                parser.parseString(str, function(err, result) {
                    if(err){
                        reject(err);
                    }else{
                        console.log(result);
                        resolve(result);
                    }

                });
            }));

        });
    })
}

//export default getXml

async function init(){
    const data = await getXml()
    console.log(data);
}
init()
</code>

Referência
Comment: Muito obrigado, você é um anjo na minha vida hahaha s2
Comment: Opa tamo ai, não esquece de marca como resposta certa, abraço.


Calculate potential distance travelled by an object

Question: This problem is fairly common in the game of cricket. The distance a cricket ball travels is calculated which is basically from point A to point B. However, this isn't entirely accurate as the some object stands in the way of the traveling ball.
What I'd like to calculate is potential distance the ball could have travelled had there not been any objects. With hawk-eye technology the cameras are able to track the trajectory of the ball.
My question is if we know the speed (s), time (t in secs) and change in speed or deceleration (d), can we calculate the potential distance the ball could have traveled?
How do we incorporate d in the equation dist = s*t
Comment: This is basically why calculus was invented.  Acceleration is the derivative of velocity, and velocity of displacement.
Answer: If you are assuming that the acceleration is constant and if you only want to track the horizontal distance, you can use the popular kinematic equation
$$ \Delta x = v_0t + \frac{1}{2}at^2 $$
or using your naming convention
$$ \text{dist} = st+\frac{1}{2}dt^2 \text{ .} $$
You could go a step farther by saying that the drag is proportional to the speed (or even squared speed) of the ball giving a differential equation for the motion.
$ $
$ $
$\textbf{EDIT:}$
For this edit, I will adopt a more standard notation as follows.

$x$: the cartesian $x$-coordinate
$\hat{v}$: a unit vector in the direction of $\vec{v}$
$v_x$: the velocity component in the $\hat{x}$ direction
$v_{0x}$: the initial $x$-component of the velocity
$\|\cdot \|$: the length of a vector

If we assume that drag is not constant, the above kinematic equations no longer hold. We know that air resistance is dependent in some way on the speed of the ball $$\text{speed} = \| \vec{v} \|$$ and will oppose the direction of motion. Putting these notions together, we will explore two different methods of modeling the drag.
The "easy" way to compute drag is to assume that the flow is approximately non-turbulent and use
$$\vec{F}_D = -b\|\vec{v}\| \hat{v}$$
as a "linear" drag force. Observe that the above equation reduces as
$$\vec{F}_D = -b\|\vec{v}\| \hat{v} = -b\|\vec{v}\| \frac{\vec{v}}{\|\vec{v}\|} = -b \vec{v}$$
thus we can conclude that the $x$-component of the force is independent of the $y$-component of the force. Newton's Laws give us two equations
$$\begin{align}  F_x=ma_x &= -bv_x\\ F_y=ma_y &= -bv_y - mg \text{ .}\end{align}$$
Since we know that velocity is the derivative of position and that acceleration is the derivative of velocity, we can write
$$ \begin{align}  \dfrac{\text{d}^2x}{\text{d}t^2} &= -\frac{b}{m}\frac{\text{d}x}{\text{d}t}\\ \\ \dfrac{\text{d}^2y}{\text{d}t^2} &= -\frac{b}{m}\frac{\text{d}y}{\text{d}t} - g \text{ .}\end{align}$$
With $\beta = b/m$, this is a differential equation for the system
$$ \begin{align} x(t) &=x_0 + \frac{v_{0x}}{\beta}\big( 1-e^{-\beta t} \big) \\ \\ y(t) &=y_0 + \frac{1}{\beta}\big( v_{0y}+\frac{g}{\beta} \big)\big( 1-e^{-\beta t} \big) - \frac{g}{\beta}t\text{ .}\end{align} $$
Solve the above system for the time the ball impacts the ground and substitute that time into $x(t) - x_0$ for the $x$-distance traveled. Please verify the above kinematic equations for yourself! I could have easily made a sign error (or worse) somewhere.
The second model I will address is the more realistic model where we assume a turbulent flow and
$$ \vec{F}_D = -b\|\vec{v}\|^2 \: \hat{v} \text{ .} $$ Now we have that
$$ \vec{F}_D = -b\|\vec{v}\|^2 \: \hat{v} = -b\|\vec{v}\|^2 \: \frac{\vec{v}}{\|\vec{v}\|} = -b\|\vec{v}\| \: \vec{v}$$ therefore Newton gives us that
$$\begin{align}  F_x=ma_x &= -bv_x\sqrt{v_{x}^2 + v_{y}^2}\\ F_y=ma_y &= -bv_y\sqrt{v_{x}^2 + v_{y}^2} - mg \text{ .}\end{align}$$
This is of course the coupled, non-linear system
$$ \begin{align}  m\dfrac{\text{d}^2x}{\text{d}t^2} &= -b\dfrac{\text{d}x}{\text{d}t}\sqrt{\bigg[\dfrac{\text{d}x}{\text{d}t}\bigg]^2 + \bigg[\dfrac{\text{d}y}{\text{d}t}\bigg]^2}\\ m\dfrac{\text{d}^2y}{\text{d}t^2} &= -b\dfrac{\text{d}y}{\text{d}t}\sqrt{\bigg[\dfrac{\text{d}x}{\text{d}t}\bigg]^2 + \bigg[\dfrac{\text{d}y}{\text{d}t}\bigg]^2} - mg \text{ .}\end{align} $$
I'll let you bang your head against that one for a while.
Comment: @kms let me know if you want to see what the equations of motion would look like with drag.
Comment: Note that in the absence of drag, you have a motion with no acceleration in the horizontal direction, but you have a constant acceleration in the vertical direction.
Comment: @vb628 We're assuming deceleration in speed. i.e change (decrease) in speed of the ball is captured.  Would like to see the equation of motion with drag. Thx.
Comment: @kms the equation above assumes drag is constant. I will edit to give you some more realistic alternatives for drag.
Comment: @kms we can of course go farther and look at things like the spin on the ball or the wind which would of course both change the direction of motion and thus complicate the drag even more (and likely force us to consider a third dimension).


Hierarchical model for A/B experiment?

Question: I'm new to Bayesian statistics.
I have a metric that has a very non-parametric distribution, which would make it very difficult to use in an A/B experiment.
However, it can be broken up into multiple levels of distributions like so:
$$
broken = bernoulli(n, p) \\ qualify = bernoulli(n, p) \\ metric \sim lognormal(\mu, \sigma \: | \: broken=0, \: qualify=1) \: + \\ lognormal(\mu, \sigma \: | \: broken=0, \: qualify=0) + \\lognormal(\mu, \sigma \: | \: broken=1)
$$
Is this considered a Bayesian Hierarchical Model? And if I ran an A/B experiment, took the test parameters found for those distributions, and simulated the metric through the model, would that be considered a correct / viable method?
Comment: To clarify, are `broken` and `clarify` predictors?
Comment: It seems a bit odd that `broken` and `qualify` are binom, but your conditionals seem to be binary-valued. Did you perhaps intend them to be Bernoulli-distributed instead?
Comment: Yes they were meant to be bernoulli, I edited it as such. Broken and qualify are not predictors. If either of those conditions vary, they lead to wildly different outcomes with their own distributions.
Answer: If the broken/qualify variables are observable then this sounds appropriate to consider as a hierarchical model; and your process sounds like a reasonable starting point.
However for it to be considered a Bayesian approach you need a few more considerations:

The Bayesian Hierarchical model assumes that the parameter pairs $(\mu_1,\sigma_1), \, (\mu_2, \sigma_2)$ and $(\mu_3,\sigma_3)$ which correspond to each of the modes you describe (eg. not broken, and qualify) come from some underlying distribution. This is the hyperprior, and you would need to define it explicitly (even if defined as being non-informative).
If you sample $Y$ directly using the central parameter estimates then you are not going to be making use of any of the uncertainty in your parameters, which is captured by prior distributions you should specify (including the hyperprior referenced above). The Bayesian approach would be to generate posterior distributions for your (hyper)parameters, sample parameters from these distributions, and then sample $Y$ based on those parameter choices.

The above description is assuming that you can directly observe the broken/qualify values, and that estimating their frequency is not of particular interest.
If this is not the case you may wish to add an extra step which is to consider this as  a mixture-model. That is we suppose the form
$$I \sim P_3,$$
$$Y = \mathcal{L}(\mu_I, \sigma_I), $$
where $P_3$ is some unknown distribution on a set of three indices $\{1,2,3\}$, which describes the mixture probabilities, and $\mathcal L$ denotes a log-normal distribution.
In addition to describing a hyperprior for the parameters $(\mu_i,\sigma_i)$ you will want a prior on $P_3$. A common starting point is to choose a Dirichlet prior as it is the canonical parametric distribution on a countable set; an alternative is the Logistic normal distribution.
The non-informative Dirichlet hyperprior is $\text{Dir}(1/2,1/2,1/2)$, whose mode would be that each of broken / broken - not qualified / broken - qualified is believed a priori to be equally likely.
Chances are that you have prior knowledge of the situation that you can substantially improve this choice; eg. you may well have views on whether breaks are substantially less frequent than non-breaks, and likewise the frequency of qualify/not qualify.
Comment: This is a fantastic answer, can't thank you enough

Hierarchical model for A/B experiment?


Create a User Profile or other Django Object automatically

Question: I have setup a basic Django site and have added login to the site. Additionally, I have created a Student (Profile) model that expands upon the built in User one. It has a OneToOne relationship with the User Model.
However, I have yet not come right with forcing the user to automatically create a Profile the first time they log in. How would I make sure they are not able to progress through anything without creating this?
I have tried by defining the following in views:
def UserCheck(request):
    current_user = request.user
    # Check for or create a Student; set default account type here
    try:
        profile = Student.objects.get(user = request.user)
        if profile == None:
            return redirect('/student/profile/update')
        return True
    except:
        return redirect('/student/profile/update')

And thereafter adding the following:
UserCheck(request)

at the top of each of my views. This however, never seems to redirect a user to create a profile.
Is there a best way to ensure that the User is forced to create a profile object above?
Answer: Looks like you're attempting to do something similar to Django's user_passes_test decorator (documentation). You can turn the function you have into this:
# Side note: Classes are CamelCase, not functions
def user_check(user):
    # Simpler way of seeing if the profile exists
    profile_exists = Student.objects.filter(user=user).exists()
    if profile_exists:
       # The user can continue
       return True
    else:
        # If they don't, they need to be sent elsewhere
        return False

Then, you can add a decorator to your views:
from django.contrib.auth.decorators import user_passes_test

# Login URL is where they will be sent if user_check returns False
@user_passes_test(user_check, login_url='/student/profile/update')
def some_view(request):
    # Do stuff here
    pass


NP completeness path problem.

Question: We have the following decision problem. Let $G=(V,E)$ be a directed graph with edge weights $w:E \to \mathbb{R}_{+}$ and $B \in \mathbb{R}$. Is there a set $K$ consisting of directed vertex-disjoint simple paths (in $G$ ofcourse) such that $w(K) \geq B$? Here $w(K) = \sum_{e \in K} w(e)$. Show that this problem is NP-complete.
I have already shown that it is in NP, but I am having some difficulties showing that it is NP-hard. To be specific, I don't know which NP-complete problem I should use for my reduction. My initial thought was Hamiltonian paths but I didn't know how to use this. Can anyone give a hint?
Answer: Indeed you can use the Hamiltonian path problem.
Let $G$ be some directed graph with $n$ vertices.  We construct a weighted graph $G'$ such that
$G$ has a Hamiltonian path if and only if $G'$ admits a path decomposition $K$ with $w(K) \geq B$, with $B = n - 1$.
In fact we have nothing to construct.  The graph $G'$ can be the same as $G$, but set a weight of $1$ to every edge.  Now, which path decompositions yield $w(K) \geq B$ ? Which ones don't ?

NP completeness path problem.


What is the result of compiling only a subset of code with ASAN?

Question: I am using the address sanitizer for most of my development on Linux (via gcc).
I typically only instrument my own code via the address sanitizer. So my process contains a mix of instrumented libraries and non-instrumented libraries (3rd parties, code from other departments).
What are the consequences of doing so? Will the ASAN function correctly?
Answer: Asan will function correctly when applied to subset of application but it may fail to detect some bugs in this case (it's covered in "Do I also need to build shared libraries?" and "Can I run it with unsanitized executable?" questions in Asan FAQ).
For example an overflow on stack/global buffer which is defined in unsanitized part of application will not be detected (because shadow memory for this buffer will not be instrumented). Similarly any overflow in unsanitized part will not be detected (because there's no instrumentation code to detect it).


Rotate rectangle insider rectangle

Question: My challenge is to rotate a rectangle inside another rectangle.
Problem description:

The inner rectangle, let's call it B, is not allowed to cross the outer rectangle, let's call it A. But if A is rotated further, so that it might come to a legal position again, it should be rotated to it. So the wanted behavior is to stop rotation before it would cross the border and continue the rotation as soon as B would have a legal position again. B is rotated with a mouse and it might be that not every single degree is calculated extra. So the mouse event might point to 20 degrees and in the next iteration to 40 degrees.
My approach is to solve the problem by calculating and working with rotations. I got the alpha rotation pointing to the mouse. Beta is the rotation from alpha to the right top corner of B.
I calculated the bounding sphere (orange) and its collision points with A (Top Left, Top Right, Right Top, Right Bottom (Left and Bot are not mentioned to not complicate the problem).
Made Calculations:

With this approach I managed to calculate when I have to stop, but only for the top side and the right side separately.
For example: Clockwise Rotation -> Top cropping
<code>If (B.leftTopCornerRotation < TopLeft ||  B.leftTopCornerRotation > TopRight) {
    Alpha = TopLeft - Beta;
} else if (B.leftBotCornerRotation < TopLeft ||  B. leftBotCornerRotation > TopRight) {
    Alpha = Topleft + Beta  + PI; // + PI rotates the value by 180 degree
} else if (B.rightBotCornerRotation < TopLeft ||  B. rightBotCornerRotation > TopRight) {
    Alpha = TopLeft - Beta + PI;
} else if (B.rightTopCornerRotation < TopLeft ||  B. rightTopCornerRotation > TopRight) {
    Alpha = TopLeft + Beta;
}
</code>
My first problem is, that if B is out of bounds on more than one line (e.g. top and right) there come situations in which I correct the rotation, so that there is no overstep for the right side, than correct that there is no overstep on the top side. But with the second correction I cause a overstep on the right side. This will lead to an infinite loop.
My second problem is, that this approach seams really complicated.
My question would be, if there is some better / working approach to calculate a valid rotation for B clockwise and counterclockwise, so that it does not overstep the borders. Best case would be when it would look like it just stops at the corner.
Answer: For every B corner find angle intervals when it is lies outside of every A edge (infinite edge for simplicity).
Then make union of these 16 intervals (most of them should be empty if B is not large) and exclude resulting interval set from the full circle range.
Comment: Thank you. I will try this. My problem with this is only, that it gets complicated really fast. Lets say I rotate clockwise. If i get to a rotation which is not allowed I have to somehow get the nearest allowed value counterclockwise.
Comment: With intervals being ready  you can find the closest interval very fast
Comment: Worked like a charm. Thx.

Rotate rectangle insider rectangle


How to run my Ruby programs without installing 20,000 file?

Question: I have written quite a few Ruby programs, but if I need to show my friend one of the program, I need to install Ruby 1.8.6 on that computer, and that is 20,000 files.  For running of my Ruby programs, is there a way besides this?
Comment: First, where exactly are you getting the 20,000 number that you keep repeating? Second, you realize that the number of files doesn't necessarily mean much in terms of disc space used. The source folder for Ruby 1.9.1 is 122MB with 12,627 files. As a quick comparison, a folder in my music directory is 25MB larger, even if it only has 16 files in it.
Comment: It is a typical One-click installer of Ruby 1.8.6... (ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]) On my Win 7, 32 bit machine, it says there are 21,437 files, and 3008 folders.  Size is 75.5MB, and size on hard drive is 142MB (even a 6 byte file takes 4kb on hard drive)
Answer: Have you looked into RubyScript2Exe?
http://www.erikveen.dds.nl/rubyscript2exe/
Comment: Then take your laptop with you.  Or tell your friend to install Ruby before you get there.

You expect to be able to alter code in a language on a machine that doesn't have the language installed?  What language have you ever worked with that leads you to believe that's a reasonable expectation?
Comment: that's a good way. how about, what if i need to change a line and show him how the code behaves differently... and not to install all those 20,000 files?
Answer: Put the files on a flash device
I think you only need to set RUBYLIB to point over to the dev...
Answer: CodePad is neat. It lets you write quick demonstrative scripts, and run them.
Answer: The other answers are quite good, but really, it's an interpreted language.  You should always expect that using it incurs the installation cost of the runtime.  The one-click installer makes installation on windows pretty trivial.  OS X has it preinstalled.  Ubuntu let's you install it with a single apt-get command.  You make it sound like the installation is difficult but it really isn't.
Comment: Note: it's nowhere near 20,000.  :-P
Comment: I see, I was hoping that a Ruby program could be runnable by just installing one EXE or maybe 20, 30 files, instead of 20,000 files.  But of course if that's the way it is, then so be it!
Answer: package your application into .jar and bundle jruby with it. you will get single JAR file at the end, which you can doubleclick to run on most OS/Desktops


$\int_{-\infty}^{\infty}e^{-zt^2 + 2wt} \ dt = e^{w^2 / z} \sqrt{\frac{\pi}{z}}$ for $z,w \in \mathbb{C}, Re(z) > 0$

Question: I'm doing complex analysis out of Gamelin, and I'm stuck on a problem in section V.7 on zeros of analytic functions.
Problem:  Show that $\int_{-\infty}^{\infty}e^{-zt^2 + 2wt} \ dt = e^{w^2 / z} \sqrt{\frac{\pi}{z}}$ for $z,w \in \mathbb{C}, Re(z) > 0$.
Gamelin gives a hint to start with $z$ as a real positive number, and I was able to show this is true when $z$ is a positive real number.  But I do not see how to extend this result to general $z$.
Comment: Fix $w$ and recall two holomorphic functions on an open set that agree on a subset with at least one accumulation point are equal
Answer: First we complete the square and write $-zt^2+2wt=-z(t-w/z)^2+w^2/z$.  Thus, we see that
$$\int_{-\infty}^\infty e^{-zt^2+2wt}\,dt=e^{w^2/z}\int_{-\infty}^\infty e^{-z(t-w/z)^2}\,dt \tag 1$$
Enforcing the substitution $t\to t/\sqrt{z}+w/z$ into $(1)$ yields
$$\int_{-\infty}^\infty e^{-zt^2+2wt}\,dt=\frac{e^{w^2/z}}{\sqrt z}\lim_{L\to \infty}\int_{-\sqrt{z}(L+w/z)}^{\sqrt{z}(L-w/z)} e^{-t^2}\,dt\tag2$$
Denote $\text{Re}(\sqrt{z})=a>0$.  From Cauchy's Integral Theorem, we have
$$\begin{align}
0&=\oint_C e^{-t^2}\,dt\\\\
&=\int_{-\sqrt{z}(L+w/z)}^{\sqrt{z}(L-w/z)}e^{-t^2}\,dt+\int_{\sqrt{z}(L-w/z)}^{aL} e^{-t^2}\,dt+\int_{aL}^{-aL}e^{-t^2}\,dt+\int_{-aL}^{-\sqrt{z}(L+w/z)}e^{-t^2}\tag3
\end{align}$$
As $L\to \infty$, the second and fourth integrals on the right-hand side of $(3)$ approach $0$ where we have tacitly used the fact that since $\text{Re}(z)>0$, then $\text{Im}(\sqrt{z})<\text{Re}(\sqrt{z})$.  Therefore,
$$\lim_{L\to \infty}\int_{-\sqrt{z}(L+w/z)}^{\sqrt{z}(L-w/z)} e^{-t^2}\,dt=\int_{-\infty}^\infty e^{-t^2}\,dt=\sqrt \pi\tag4$$
Substituting $(4)$ into $(2)$ yields

$$\int_{-\infty}^\infty e^{-zt^2+2wt}\,dt=\sqrt{\frac{\pi}{z}}e^{w^2/z}$$

as was to be shown!


Getting value with htaccess and pagination for root

Question: This url
example.com/videos/load.php?cat=video-category

would become
example.com/videos/video-category

Also, there is pagination & limit url
example.com/videos/load.php?cat=video-category&p=2&limit=20

it would become
example.com/videos/video-category?p=2&limit=20

(this links also work)
example.com/videos/video-category?p=2
example.com/videos/video-category?limit=20

Also, there is another root url
example.com/videos/load.php?cat=video-category&name=video-name

it would become
example.com/videos/video-category/video-name

Also, there is pagination & limit url
example.com/videos/load.php?cat=video-category&name=video-name&p=2&limit=20

And it would become
example.com/videos/video-category/video-name?p=2&limit=20

(this links also work)
example.com/videos/video-category/video-name?p=2
example.com/videos/video-category/video-name?limit=20

Here is my htaccess (into videos folder)
Options +FollowSymLinks

RewriteEngine on
RewriteBase /videos/

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]

RewriteCond %{THE_REQUEST} \ /+videos/load\.php\?cat=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ %1?%2 [L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]

how to do this.. plz help me.. thanks..
Answer: Have it this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /videos/

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]

RewriteCond %{THE_REQUEST} \s/+videos/load\.php\?cat=([^&\s]+)&name=([^&\s]+)&?(\S*)
RewriteRule ^ %1/%2?%3 [L,R=302]

RewriteCond %{THE_REQUEST} \s/+videos/load\.php\?cat=([^&\s]+)&?(\S*)
RewriteRule ^ %1?%2 [L,R=302]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ load.php?cat=$1&name=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ load.php?cat=$1 [L,QSA]


Handle unexpected Lauterbach T32-Remote API connection aborts

Question: Imagine you are using or developing an automated test for the T32-Remote API. In this case the C-API is wrapped into Java.
The connection is initialized with:
T32_Config("NODE=", "localhost"));
T32_Config("PACKLEN=", "1024"));
T32_Config("PORT=", "20000"));
T32_Init());
T32_Attach(T32.T32_DEV_ICE);

After that, the test starts and is aborted unexpectedly by the user without calling:
T32_Exit();

If you are restarting the test again now, this results in a connection error at T32_Attach() returning -1 (T32_ERR_COM_RECEIVE_FAIL). Restarting a second time does not cause any issues.
Is there any way from within the remote API to reset Trace32? Closing the program and open it again or restarting the test a second time is not an option.
Answer: I do not really know why this fixed the issue but if an error occurs, simply run the entire initialization routine again.
1st time: T32_Exit() not called
2nd time: simply do this
T32_Config("NODE=", "localhost"));
T32_Config("PACKLEN=", "1024"));
T32_Config("PORT=", "20000"));
T32_Init());

if(T32_Attach(T32.T32_DEV_ICE) != 0){
   T32_Config("NODE=", "localhost"));
   T32_Config("PACKLEN=", "1024"));
   T32_Config("PORT=", "20000"));
   T32_Init());
   T32_Attach(T32.T32_DEV_ICE)
}