How can I keep the piece coherent when I have the rhythm of a Waltz and the form of a Sonata?

Question: I just had this idea come to mind after I analyzed Grande Valse Brillante Op. 18 and asked if it was in Sonata form. What if I combine the Waltz and the Sonata? Here is my proposed form for the piece:

I call the piece that I am composing, Valse Quasi una Sonata, which translates to Waltz almost like a Sonata. As you can see from the image, I use the Dominant more as a bridge than a true Dominant. This is how I bridge to the Subdominant with the Dominant:

C major -> C minor chord(or the note Eb) -> Bb major

I have this bass pattern occurring throughout the piece:

Root, Chord, Chord

Often the 2 chords will be an inversion apart, but when the right hand gets into the bass clef, the chords are sometimes the same. The Root is usually at least a fifth down from the first chord, but particularly with the F major chord, I find that I can only do that when the melody goes up high. If it stays in the first octave, I have to have the first F major chord be in first inversion, a third up from the root(the F an octave below the great octave, F1, is just too low to be the bass note in my Waltz). The hardest part I have found so far about composing this piece is having the rhythm of the Waltz and the form of the Sonata, while keeping the piece coherent.
So, how can I keep the piece coherent when I am combining the rhythm of the Waltz with the form of the Sonata? Use melodic fragments from earlier in the Waltz in the Development(My Development sections tend to have a high ratio of new material to old material)? Have the left hand play the melody in the development, while keeping the Waltz rhythm going in the right hand?
Comment: Perhaps you should look after Sonatas in a Waltz rhythm for comparing how other composers did?
Comment: But, I have no idea who has composed Sonatas in a Waltz rhythm to get an idea of how to get across that contrast that is needed. I know of some atypical sonatas, namely the 2 sonatas by Beethoven in Op. 27, but those are more like a fantasy than a Waltz, even with the constant triplets in the first movement of the Moonlight Sonata. I have no idea where to start as far as sonatas in a Waltz rhythm.
Answer: I see no problems with keeping a waltz in sonata-allegro form coherent. I've composed tougher and more aberrant sonata-allegros. A ragtime sonata-allegro, compete with properly repeated strains of conventional length...a heavy metal sonata-allegro...a 20th century-style toccata in sonata-allegro form, complete with the increased dissonance typical of 20th-century classical music...heck, a sonata-allegro for drum kit and glockenspiel where the first theme group before the transition is played by the drum kit only and is therefore atonal...
Your suggestions to use melodic fragments from the exposition in the development and try swapping which hand gets the melody and which hand gets the accompaniment are great, but I'd use them for any sonata-allegro, not just waltzes in sonata-allegro form.
It's quite possible that your consistent use of a waltz accompaniment alone will keep your sonata-allegro coherent. Keeping the tempo consistent will also help, at least a little bit.
Answer: You'll keep it coherent in exactly the same way you would if it was in 4/4, 6/8 or any other time signature.
The essence of Sonata form is presenting two themes in contrasting keys (the Exposition), messing around with them in a variety of keys (the Development) then repeating them, but both in the same key (the Recapitulation).
You might consider a bit more variety than sticking to a constant 'oom-cha-cha' accompaniment in the left hand!  But that's just the same as suggesting a piece in 4/4 shouldn't ALWAYS have 'oom-cha-oom-cha'.
Look at a simple ternary form piece like Bach's 'Minuet in G'.  It's an ancestor of Sonata form. (And the Minuet is an ancestor of the Waltz.)  Just one theme.  Then a fragment of it is played around with in the dominant key.  Then a return to the theme, in the home key.
Can you write something like that?   Then you'll be ready to move on to the more complex Sonata form.
Answer:
I call the piece that I am composing, Valse Quasi una Sonata, which translates to Waltz almost like a Sonata

One thing to have in mind:
The terms "sonata" and "sonata form" are two entirely different matters, and that can be quite confusing.
Sonata is originally an instrumental composition as opposed to a cantata which is a vocal composition. But that goes way back in history.
A sonata has developed into being a big work with four movements, can also be three movements though or even two, but four is very typical. A sonata for solo violin by Bach is a huge work with four large movements. In baroque music there is also something called a trio sonata which is for three instruments, well actually typical for two melody instruments and continuo. But the sonata form is not part of these sonatas. Later on in history a sonata continues to be a big work, often a work for piano solo or a work for piano and another instrument. If there are more instruments like a string quartet or an orchestra the term sonata is usually not applied although the template with four movements becomes very usual.
And at some point the "sonata form" was invented, but not as a designation of the form of the entire work. The sonata form only refers to the form of one movement and it became a tradition to make the first movement of a sonata in sonata form. I don't know why that form is called sonata form, but a qualified guess is that it is because it is typically the form of the first movement in a sonata. The form can also appear in other movements, but the first movement is the typical movement for the form.
There is of course nothing wrong with making a waltz in sonata form. After all a waltz is often a work in ABA form and the sonata form is in essence also ABA, you could say an extended version of ABA. So a waltz in sonata form will probably be a big waltz. But whether it turns the music into a sonata really has nothing to do with the sonata form as pointed out above. You can of course decide to write a sonata in just one movement. Personally I have once composed a sonata for viola and piano in just one movement, but actually not in sonata form.
Thus "sonata form" is a way of making a movement. It is actually a very good way if you want to make a bigger movement, because it is a form or "template" that gives room for a lot of ideas and developments. You don't need to call a work sonata if you use that form. You can also use the form in a vocal work even that sonata originally means an instrumental composition, but since "sonata form" refers to the form of the movement and not whether it is instrumental or vocal there is no limitation to where you can use the form. As a composer you write the music that you want to write using the forms that appeals to you.


Analyzing Difference in Change between two groups with unpaired samples

Question: I'm currently working with a data set where there is a control group and an intervention group. There are a couple of data points where we're interested in seeing if the intervention group has a statistically significant difference in effect (basically is $x_2 - x_1$ in intervention different than $x_2-x_1$ in control).
Unfortunately, the samples collected within the groups are not paired - in other words, the same people were not interviewed at baseline and endline. I can use an unpaired t-test to compare endline-to-endline, or baseline-to-endline for the groups individually. Were the samples paired, I could just take the difference and use an unpaired t-test to compare the differences. Since they're unpaired, I'm at a bit of a loss.
Is there a way to adjust the t-test for this?
If not, are there appropriate statistical tests that can be done for this situation without artificially pairing up the samples?
(As is too commonly the case, I was not involved in the design of this experiment, but I hope I've conveyed to the group the importance of paired sampling for the future.)
Answer: Let's use $C$ for 'control' and $T$ for treatment (i.e. 'intervention')
It seems like you have the following situation.
There are four quantities you have measurements on:
1) Control, baseline - from which you can estimate $\mu_{C1}$
2) Control, endline - from which you can estimate $\mu_{C2}$
3) Intervention, baseline - from which you can estimate $\mu_{T1}$
4) Intervention, endline - from which you can estimate $\mu_{T2}$
Let $\delta_i=\mu_{i2}-\mu_{i1}$ be the after-before difference.
You're interested in testing against the alternative  $\delta_T\neq\delta_C$ (or maybe $\delta_T>\delta_C$ in a one-tailed test).
This is straightforward; it may be done in a number of ways.

Since you say you'd normally use a t-test, you can do that here, but you'll need some assumptions; the suitability of those assumptions will be a question you'll need to consider carefully.
The numerator of your t-statistic will be
$\hat{\delta}_T-\hat{\delta}_C \,= \hat{\mu}_{T2}-\hat{\mu}_{T1}-(\hat{\mu}_{C2}-\hat{\mu}_{C1}) $
$\quad\quad\quad\quad= \bar{x}_{T2}-\bar{x}_{T1}-(\bar{x}_{C2}-\bar{x}_{C1}) $
The questions come down to whether you will assume independence of all four sets of measurements, and whether you will assume equality of variances (in the second case, if not, some Welch-type adjustment will be required).
a. If you assume independence and equal variances for all four measurements, your denominator would work similarly to the way it does in an ordinary two-sample t-test, so you'd get:
$$t = \frac{\bar{x}_{T2}-\bar{x}_{T1}-(\bar{x}_{C2}-\bar{x}_{C1})}{
s_{p} \cdot \sqrt{\frac{1}{n_{T1}}+\frac{1}{n_{T2}}+\frac{1}{n_{C1}}+\frac{1}{n_{C2}}}}$$
where
$$s_p^2= \frac{(n_{T1}-1)s_{{T1}}^2+(n_{T2}-1)s_{{T2}}^2+(n_{C1}-1)s_{{C1}}^2+(n_{C2}-1)s_{{C2}}^2}{n_{T1}+n_{T2}+n_{C1}+n_{C2}-4}$$
and you have $n_{T1}+n_{T2}+n_{C1}+n_{C2}-4$ d.f. for the $t$ distribution.
b. if they don't all necessarily have the same variance, the Welch-Satterthwaite approximation to the d.f. for the natural test statistic can be used:
$$t = {\overline{x}_{T2} - \overline{x}_{T1}-(\overline{x}_{C2} - \overline{x}_{C1})} \over s_{d}$$
$$s_{d} = \sqrt{{s_{T1}^2 \over n_{T1}} + {s_{T2}^2  \over n_{T2}}+{s_{C1}^2 \over n_{C1}} + {s_{C2}^2  \over n_{C2}}}$$
with d.f:
$$ \frac{(\frac{s_{T1}^2}{n_{T1}} + \frac{s_{T2}^2}{n_{T2}}+\frac{s_{C1}^2}{n_{C1}} + \frac{s_{C2}^2}{n_{C2}})^2}{\frac{(s_{T1}^2/n_{T1})^2}{n_{T1}-1} + \frac{(s_{T2}^2/n_{T2})^2}{n_{T2}-1}+\frac{(s_{C1}^2/n_{C1})^2}{n_{C1}-1} + \frac{(s_{C2}^2/n_{C2})^2}{n_{C2}-1}}$$
It might even make sense to assume that the two baseline measurements have equal variance and that the two endline measurements have equal variance, but that the baseline and endline might not have the same variance; that, too, could be done fairly readily (as could the assumption that the two control measurements had equal variance and the two intervention measurements had equal variance but control and treatment variances might differ).
Alternatively, the equality of that linear combination can be tested as a contrast in a one-way ANOVA. Many packages make this simple. Again, some packages will support the option of doing this with unequal variances.
Another approach is to do it as a two-way ANOVA type model; you can fit a model like:
response ~ group + time + group:time

where group is control or treatement, and time is the baseline/endline ('1' or '2'). In this case the group:time interaction is the thing you're interested in testing for.
You could also construct a permutation test of this hypothesis, or a variety of other tests can be similarly adapted in the way I did above.

If you had paired data (before and after), many people would suggest a different approach to the differences-in-differences style of test here (such as repeated measures, or at least putting the baseline in the model as a control variable), but I don't think those sort of approaches are an option in this situation.

That said, if your sample sizes are equal, you perhaps shouldn't fear pairing up your samples artificially; simulations suggest that actually works surprisingly well.
Comment: Exactly what I was looking for. I do, thankfully, have equal sample sizes in all 4 measurements, so I'm investigating artificial pairing and may go that route. One follow-up (for now): You say that the t-test involves assuming independence among the 4 samples. Control and Intervention are definitely independent, but Control-EL and Control-BL may not be. How would you adjust this in that case?
Answer: Perhaps look at a Mann-Whitney-Wilcoxon test on the two sets of $x_2 - x_1$. The samples do not need to be paired.


Prevent Sql Injection (Java)

Question: I want to secure my application from SQL Injection attacks.
First question: What is better way to do it?
The first method: I convert every request to json here:
public JsonObject requestToJson(HttpServletRequest request) throws UnsupportedEncodingException{

        request.setCharacterEncoding("UTF-8");

        StringBuffer jb = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null)
                jb.append(line);
        } catch (Exception e) { /*report an error*/ }

        return new JsonParser().parse(jb.toString()).getAsJsonObject();
    }

If it is best way, to prevent it here, then second question: how to do it here?
The second method: It can be done by Hibernate level. Second question: how to do it?
Comment: How would converting the request to JSON prevent SQL Injection? Why do you think HIbernate is vulnerable to SQL Injection?
Comment: no, converting to json is not preventing injection of course. I mean, as I'm converting every request to json, I can put this validation in this function.
Comment: I'm new in hibernate. I'm using several ways to generate SQL: JPARepository, CriteriaBuilder and HQL. My question is: "it's already preventing from SQL Injection"?
Comment: See [this question](http://stackoverflow.com/questions/9596424/how-much-safe-from-sql-injection-if-using-hibernate) and [this question](http://stackoverflow.com/questions/3441193/are-sql-injection-attacks-possible-in-jpa).
Comment: Ok, about HQL, JpaRepository I understood. But what about CriteriaBuilder. Is it safe?
Comment: What *about* [CriteriaBuilder](http://stackoverflow.com/questions/15065906/does-hibernate-criteria-api-completely-protect-from-sql-injection)?
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95402/discussion-between-annoirq-and-elliott-frisch).
Answer: Thanks this user: Elliott Frisch. He answered in comment.
JPARepository like this already prevented from SQL Injection:
public interface UserRepository extends JpaRepository<User, Integer> {
    User findByPhoneNumber(String phoneNumber);
}

Just need to prevent if you using HQL:
String query1 = "select * from MyBean where id = "+ id;
String query2 = "select * from MyBean where id = :id";

Second one, will be secured.
Thanks, everyone.

Prevent Sql Injection (Java)


How to kill a multi-container pod if one container fails?

Question: I'm using Jenkins Kubernetes Plugin which starts Pods in a Kubernetes Cluster which serve as Jenkins agents. The pods contain 3 containers in order to provide the slave logic, a Docker socket as well as the gcloud command line tool.
The usual workflow is that the slave does its job and notifies the master that it completed. Then the master terminates the pod. However, if the slave container crashes due to a lost network connection, the container terminates with error code 255, the other two containers keep running and so does the pod. This is a problem because the pods have large CPU requests and setup is cheap with the slave running only when they have to, but having multiple machines running for 24h or over the weekend is a noticable financial damage.
I'm aware that starting multiple containers in the same pod is not fine Kubernetes arts, however ok if I know what I'm doing and I assume I do. I'm sure it's hard to solve this differently given the way the Jenkins Kubernetes Plugin works.
Can I make the pod terminate if one container fails without it respawn? As solution with a timeout is acceptable as well, however less preferred.
Answer: Disclaimer, I have a rather limited knowledge of kubernetes, but given the question:
Maybe you can run the forth container that exposes one simple endpoint of "liveness"
It can run ps -ef or any other way to contact 3 existing containers just to make sure they're alive.
This endpoint could return "OK" only if all the containers are running, and "ERROR" if at least one of them was detected as "crushed"
Then you could setup a liveness probe of kubernetes so that it would stop the pod upon the error returned from that forth container.
Of course if this 4th process will crash by itself for any reason (well it shouldn't unless there is a bug or something) then the liveness probe won't respond and kubernetes is supposed to stop the pod anyway, which is probably what you really want to achieve.
Comment: Interesting idea that put me on a good path, thank you. The feature of combining liveness probes (which would avoid the extra pod) is discussed at https://github.com/kubernetes/kubernetes/issues/37218.
Comment: Nice discussion. Thanks for the link, Karl. I wasn't aware of it...


Getting user mentioned's "XP". JSON, Discord.py

Question: I'm trying to make a command where users can steal other user's XP (dollars). I'm using random.randintto make it so they are either successful or not, but I want to do a check to make sure the user that is being mentioned(robbed) has enough XP (dollars) for the author to steal. I've tried a few ways such as if (get_dollars(message.mentions[0].id) < 25:and if (get_dollars(user.mention.id) < 25: with no success. Here's the "full" code:
client = discord.Client()

try:
    with open("cash.json") as fp:
        cash = json.load(fp)
except Exception:
    cash = {}

def save_cash():
    with open("cash.json", "w+") as fp:
        json.dump(cash, fp, sort_keys=True, indent=4)

def add_dollars(user: discord.User, dollars: int):
    id = user.id
    if id not in cash:
        cash[id] = {}
    cash[id]["dollars"] = cash[id].get("dollars", 0) + dollars
    print("{} now has {} dollars".format(user.name, cash[id]["dollars"]))
    save_cash()

def remove_dollars(user: discord.User, dollars: int):
    id = user.id
    if id not in cash:
        cash[id] = {}
    cash[id]["dollars"] = cash[id].get("dollars", 0) - dollars
    print("{} now has {} dollars".format(user.name, cash[id]["dollars"]))
    save_cash()

def get_dollars(user: discord.User):
    id = user.id
    if id in cash:
        return cash[id].get("dollars", 0)
    return 0

@client.event
async def on_message(message):
    steal = random.randint(1,3)
    stealdollars = random.randint(25, 250)
    if message.content.startswith('!rob'):
        user = message.mentions[0]
        if (get_dollars(user.mention)) < 25:
            await client.send_message(message.channel, "{} is too broke to rob ".format(user.mention))
        elif steal == 1:
            print("{} catches {} slippin and sticks them up for ${} ".format(message.author.mention, user.metion, stealdollars))
            await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} ".format(message.author.mention, user.mention, stealdollars))
            remove_dollars(user.mention, stealdollars)
            add_dollars(message.author, stealdollars)
        elif steal == 2:
            print("{} kicks {}'s door down but doesn't find any cash ".format(message.author.mention, user.mention))
            await client.send_message(message.channel, "{} kicks {}'s door down but doesn't find any cash".format(message.author.mention, user.metion))
        elif steal == 3:
            print("{} gets arrested trying to rob {} and has to post $250 bail ".format(message.author.mention, user.metion))
            await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post $250 bail ".format(message.author.mention, user.metion))
            remove_dollars(message.author, 250)

Answer: Sorry had a brain fart. I ended up using
 for user in message.mentions:
                if (get_dollars(user)) < 25:


Inequality of Fourier Transform

Question: We all know that on $\mathbb R$,
$$
\|\hat f\|_\infty\leq \|f\|_1.
$$
In particular, equality holds if $f\geq 0$, but in most of the cases we have strict inequality. But is the following true: for every $\epsilon>0$, there is an $0\neq f\in L^1$ with
$$
\|\hat f\|_\infty\leq \epsilon\|f\|_1?
$$
Answer: Let's define $f_n(x)$ piecewise by $f_n(x) = e^{2\pi i k x}$ if $0\leq k < x < k+1 \leq n$ and $0$ otherwise. Then $\lVert f\rVert_1 = 2\pi n$, but
$$|\widehat {f_n}(\xi)| = \left| \int_{\mathbb R} e^{-2\pi i \xi x}f(x) dx\right| = \left|\sum_{k=0}^{n-1} \int_0^{1}e^{2\pi i (k-\xi) (x+k)}dx\right|\leq \\\sum_{k=0}^{n-1} |e^{2\pi i k(k-\xi)}|\frac{\left|e^{2\pi i (k-\xi)}-1\right|}{2\pi |k-\xi|} = \mathcal O(\ln(n)).$$
Thus, $\lVert \widehat{f_n} \rVert_\infty \leq \frac{C\ln n}{n}\lVert f_n \rVert_1$ for some $C$ independent of $n$. The idea here was to construct a function which "resonates" with no particular frequency, such that it's fourier transform takes no large values.
Comment: Why was your second integral from 0 to 1 but not from $k$ to $k+1$?
Comment: @SingfookSangwood My mistake, I did a substitution in my head that wasn't really possible that way (shifting the limits of the integral). Doesn't change anything, though.

Inequality of Fourier Transform


Tag script não é impressa com jQuery

Question: Eu preciso imprimir um script utilizando jQuery porém, todo o código é impresso e o script não, alguem sabe como resolver isso? Segue código abaixo:
codigo = '
<input type="submit" id="botao" value="Send" />
';
$('#iframe5').parent().after(codigo);

Comment: Se você tiver utilizando algum CMS, pode ser que o mesmo impeça inserir tags script diretamente na página.
Answer: Você não precisa "injetar" o Script dessa forma. Na verdade, isso pode criar problemas se o seletor pegar mais de um elemento no frame, duplicando os listeners.
//código html a ser injetado na página
var codigo = '
<input type="submit" id="botao" value="Send" />
';

//insere o elemento no local adequado
$('#iframe5').parent().after(codigo);

//adicione um handler no botão adicionado pelo ID
$("#botao").click(function() { alert("test"); });

Coloquei um exemplo funcional no jsfiddle.
Também é possível recuperar uma referência do tipo jQuery para os elementos criados e então adicionar o listener ao botão:
//transforma código html a ser injetado na página em elementos reais com o jQuery
var codigo = $('
<input type="submit" id="botao" value="Send" />
');

//localiza o botão a adiciona o listener
codigo.find('input').click(function() { alert("test"); });

//insere o elemento no local adequado
$('#iframe5').parent().after(codigo);

A vantagem disso é que você não precisa fixar um id.
Outra abordagem é usar a função on do jQuery para não precisar associar o evento ao botão recém criado. Exemplo:
$(document).on("click", "#botao", function() { alert("test"); });

Executando o código acima uma única vez na inicialização da página irá funcionar para qualquer elemento com id botao criado a qualquer momento.
Nota: tome cuidado para não criar vários elementos com um mesmo id. Isso não é uma boa prática e pode levar a problemas.
Answer: A melhor solução aqui é criar um novo elemento script com javascript puro e fazer append para o elemento que você quer.
var script = document.createElement( "script" );
script.type = "text/javascript";
script.src = "scriptname.js";
$("#teste").append(script);

Outra solução, para elementos que são carregados dinamicamente é usar o .on() para delegar o evento. Assim você pode usar este código logo de inicio ser precisar que o elemento #botao já esteja carregado.
$(document).on('click', "#botao", function() { alert("test"); });

Note que se você acrescentar vários botões, têm de lhes dar um ID único. Ou então melhor usar class, para não dar erros.
Answer: O JQuery não permite tags de script embutidas no html, para conseguir algo similar, deve-se usar javascript puro:
var script = document.createElement( "script" );
script.type = "text/javascript";
script.src = "scriptname.js";
$("#temp").append(script);

ou você pode utilizar outra função como innerHtml para embutir scripts.
Editado
Alguns navegadores não lidam bem com essa situação, se quiser uma solução rápida, deve-se separar a tag de script na string:
$("#leaderBoard").html('<sc'+'ript language="javascript1.1">alert(1);</sc'+'ript>');

Tag script não é impressa com jQuery


Is the set $A = [0,1]\setminus\mathbb{Q}$ countable or not?

Question:
Is the set $A = [0,1]\setminus\mathbb{Q}$ countable or not?

What I am thinking is $A$ consist of irrational numbers in the interval $[0,1]$ hence it is subset of irrational numbers. As set of irrational numbers is uncountable so I think set $A$ is also uncountable.
Comment: not every subset of irrational numbers is uncountable, but if $A$ and $\mathbb Q$ were both countable then so would be $A\cup \mathbb Q$ and therefore $[0,1]\subset A\cup \mathbb Q$
Comment: $\varnothing$ is also a subset of the irrational numbers.
Answer: Yes, it is uncountable, but not for that reason. For instance, $\left\{\sqrt2+n\,\middle|\, n\in\Bbb N\right\}$ is also a set of irrational numbers, but it is countable.
However, if $[0,1]\setminus\Bbb Q$ was countable, then, since $\Bbb Q\cap[0,1]$ is countable, $[0,1]$ would be countable too, since it's the union of them.
Comment: @J.W.Tanner I've edited my answer. Thank you.
Comment: technically, $[0,1]$ is *contained* in the union of $A$ and $\mathbb Q$
Answer: All countable set of $R$ have Lebesgue measure equal to $0$.
So Lebesgue measure of $[0, 1] - \mathbb{Q}$ is $1$.
Eventually by contraposition, $[0, 1] - \mathbb{Q}$ is uncoutable.
Answer: What you are thinking does not work.
For example, $\{n\pi\mid n\in\mathbb N\setminus\{0\}\}$ is a subset of irrational numbers but countable.
Here's an argument that works.  If $A$ were countable, then, since $\mathbb Q$ is countable,
$A\cup \mathbb Q$ would be countable,
and therefore $[0,1]$, which is a subset of $A\cup \mathbb Q$, would be countable,
and that is a contradiction.


why does all of my background images not display

Question: I have a div and I want that div to only have a background images, I have set the background in the css and have given it a height but for some reason it won't all display not matter what I set the right to.
Here is my code

Come to Church

We would love to meet you! Find out what's happing each Sunday and how to get here.

What's On?

Find out what we have going on within our church and why not come along.

Get In Touch

Have a question? why not get in touch

Here is my css

#podcast {

    background-image: url("images/iStock_000033667452_Large.jpg");
    background-position: 0px 350px;
     background-repeat: repeat-x;
    background-size: 800px 575px;
    height: 500px;
}

#church
{
    padding-left: 100px;
    padding-top: 40px;
    padding-bottom: 20px;
    width: 200px;
   float: left;
    line-height: 1.5;
}

#whatson

{
    padding-left: 100px;
    padding-top: 40px;
    padding-bottom: 20px;
    width: 200px;
    float: left;
    line-height: 1.5;
}

#getintouch

{
    padding-left: 100px;
    padding-top: 40px;
    padding-bottom: 20px;
    width: 200px;
    float: left;
    line-height: 1.5;
}
Comment: remember that `url()` in CSS is relative to the location of the css. if that css is in an external file, and in a different directory than the html, then your urls are incorrect. it'd have to be `url(../whatever/...)`.
Comment: my url is correct as part of the images shows but not all of it and it doesn't show no matter what i set the height to
Comment: Hard for us to tell because your snippet doesn't work.  When you say "doesn't show all" can you be more descriptive in how it's cutting off?
Comment: it shows only haft of the images and not the full image
Comment: background-size: contain; or background-size: cover;
Answer: It works for me? I'm guessing your not calling the URL correctly. Do you have a link to the site?

#podcast {

    background-image: url("http://placehold.it/600x300&text=placehold.it+rocks!");
    background-position:50%;
    background-size:cover;
    height: 500px;
}

#church
{
    padding-left: 100px;
    padding-top: 40px;
    padding-bottom: 20px;
    width: 200px;
   float: left;
    line-height: 1.5;
}

#whatson

{
    padding-left: 100px;
    padding-top: 40px;
    padding-bottom: 20px;
    width: 200px;
    float: left;
    line-height: 1.5;
}

#getintouch

{
    padding-left: 100px;
    padding-top: 40px;
    padding-bottom: 20px;
    width: 200px;
    float: left;
    line-height: 1.5;
}

Come to Church

We would love to meet you! Find out what's happing each Sunday and how to get here.

What's On?

Find out what we have going on within our church and why not come along.

Get In Touch

Have a question? why not get in touch

Comment: Bit leat someone already helped me fix it as you should have seen in the comments above


Show only nodes in a view which have the same value of a text field (field_phone_number)

Question: I have a content type "ABC" with the "telephone" field (field_phone_number) added to it,
and I already have on my website around 1K nodes of this content type "ABC".
I want to display a table view on every node of type "ABC", displaying the title of all other nodes that have the same
"telephone value" as the content type being viewed.
I have tried to use the EVA module but with no success.
This is what I have done so far:

Create an EVA View that show: "content" of type: "ABC".
Add all the field that I want to display.
EVA Settings:

Entity Type: Content
Bundles: ABC
Arguments: Check the "Use tokens from the entity the view is attached to" and add: "[node:field_phone_number]"

Add contextual filter: "Content ID" with provide default value: "Content ID from URL"

Results:
A table view displayed on every node with only the node itself is displayed inside the table !!!! (Not what I need)
For example:
If lets say i have 12 nodes that have the same phone number value: "123456789".
What I am expecting to see is a view table displaying all the title of those 12 nodes every time I visit the page of each
and every node of those 12 nodes.
Usually doing the above by using an entity reference field as the comon value between nodes is very easy, but the problem is that I do not have an entity reference
field in commun between the nodes, rather I have the "Phone Number" field which is obviously it is not of type "entity reference".
Any help ?
Thank you,
Answer: You're on the right track with EVA. To list sibling nodes based on common taxonomy terms or entity reference fields you can just use Views configuration, but if you need to filter based on a different common field EVA is the way to go.
You did everything right except for the last step - you don't need Content ID Contextual filter, you're not comparing nodes, you're comparing Phone number fields! And the argument you set for EVA is the phone number, so that is the information you need to include there.
Add a Contextual filter for the phone field, you can configure it:
When the filter value is NOT available
Display contents of "No results found"
When the filter value IS available or a default is provided
Specify validation criteria
Numeric (or whatever is applicable in your case)
Hide view (if it doesn't validate)
That should list all the nodes that share that field, including the node you're currently viewing.
If you want to exclude the current node from that list you need to add another Contextual filter, this time it will be the Content ID one:
When the filter value is NOT available
Provide default value
Content ID from URL
When the filter value IS available or a default is provided
Specify validation criteria
Content
    [ABC]
More
Exclude
That one will exlude the current node from that list.
Comment: I'm glad to help :)
Comment: thank you million times, you just saved me tons of working hours trying to figure it out on my own, especially with lack of tutorials on the internet about such use case ! You rock mate!


Como remover tabela dos resultados de um find() do CakePHP?

Question: Tenho 3 tabelas relacionadas: Usuario, Movimentacao e Grupo
Estou usando:
$usuarios = $this->Usuario->find('all',array(
    'recursive' => 1,
    'fields' => "Usuario.id, Usuario.nome, Usuario.email"
    'order' => array('Usuario.nome ASC')
));

Porém, o recursive => 1 está trazendo as três tabelas. Se coloco 0, retorna apenas Usuario. Eu preciso retornar apenas Usuario e Grupo, pois, trazendo Movimentacao, a query fica lenta demais. Como faço pra eliminar Movimetacao da query?
Answer: Você pode desassociar o outro model temporariamente:
$this->Usuario->unbindModel(array('hasAndBelongsToMany' => array('Movimentacao')));

(Substitua hasAndBelongsToMany pelo tipo de relacionamento que está usando.)
Comment: Funcionou perfeitamente! Tnx!
Answer: Ao dar unbind, você retira totalmente o relacionamento deste model durante a execução.
Recomendo fazer a seleção de dados de 2 maneiras:
Para associações diretas, Você pode passar o recursive em conjunto com o fields e limitando assim os dados retornados. Por exemplo:
$result = $this->Usuario->find('all', array(
    'conditions' => array('Usuario.status' => 'ativo'),
    'recursive' => 1,
    'fields' => array('Usuario.nome', 'Grupo.nome')
));

Para associações profundas utilizar o Containable Behavior (http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html)
Exemplo de utilização em um controller:
$this->Usuario->Behaviors->load('Containable');
$result = $this->Usuario->find('all', array(
    'conditions' => array('Usuario.status' => 'ativo'),
    'contain' => array('Grupo')
));


Is this function bounded?

Question: Suppose $f:\mathbb{R}\rightarrow\mathbb{R}$ is a $C^1$ function which satisfied the following differential inequality:
$$\frac{df}{dt}\leq C(f+f^{\frac{3}{2}}).$$
If $f>0$ and $f(t)\rightarrow 0$ as $t\rightarrow\infty$, then is $f$ bounded?
Answer: No, any monotonically decreasing positive $C^1$ function that goes to infinity for $t\to-\infty$ is a counterexample, for instance
$$f(t)=\begin{cases}1-t&t\lt0\\\mathrm e^{-t}&t\ge0\;.\end{cases}$$
Comment: Thank you so much. Then I should modify my question then.
Comment: @Paul, You have already given that $f(t) \to 0$ as $t \to \infty$. So it is clearly bounded as $t \to \infty$. More precisely, the function is bounded in the interval $(a, \infty)$ for any $a \in \mathbb R$.
Comment: @Srivatsan Narayanan: Yes, you are right! Actually I want to see if $\frac{df}{dt}$ is bounded or not as $t\rightarrow\infty$.

Is this function bounded?


Unbalanced memory in a PowerEdge 1950

Question: The Dell PowerEdge 1950 has eight memory slots. Previously, it had 4 x 2GB in there. We need a bit more memory so added an extra 2 x 2GB. On boot, the BIOS warned us that the preferred configuration is to fill all 4 additional slots but allowed the system to boot.
I'm aware of the requirement for pairs of memory but what have we "lost" by leaving it potentially unbalanced like this?
Answer: Performance - that's it.
Comment: Game of swings and roundabouts then ;-) We've increase memory thus reduced page file thrashing but lost a bit of speed at the same time. 2 x 2GB wasn't horrendously expensive so will probably buy a couple more
Comment: Oh you'll need the memory more than the slow down - so it's still a win, just not as big as if you'd balanced them.
Comment: Make sure you get the exact same spec if you buy more and want a chance of the chipset using them in quad-channel mode. You  may need to get a new box of four that is rated for quad use. Memory controllers will err on the side of caution an not enable multi-channel modes if there is any hint of possible timing discrepancies between the modules in a given bank of slots.
Answer: The Intel 5000X chipset of your PowerEdge had to disable quad-channel access to memory so you'll have somewhat less memory bandwidth. I don't actually know if it's able to go quad on your 4x2GB and go slower on the remaining 2x2GB but I suspect it can't use different access methods and it'll use the slower mode on the whole memory range.
On many server chipsets softwares like memtest86+ will be able to detect and report the exact memory access mode and effective speed - you could probably use that to compare how the memory subsystem performs with and without the additional 2x2GB.
Comment: When checking with memtest or similar, do remember that it is not intended as a real-world benchmark at all. Any speed difference it shows may be much smaller than you'll notice in real-world operation due to the many factors it won't take into consideration (its job being to check for errors, not performance). I suspect the difference will be small anyway - several orders of magnitude smaller than the difference due to reduced disk I/O from needing to swap less and/or from being able to cache more.
Comment: A real world application that is compute intensive is probably also memory intensive. In such an application I experienced about a 40% loss in performance when running 6 DIMMs instead of the recommended 4 or 8 DIMMs. It's a big deal for some applications and barely noticeable for other applications. Surprisingly, 4 DIMMs totaling less memory can outperform 6 DIMMs that total to a greater amount of memory if quantity of memory is not a constraint.

Unbalanced memory in a PowerEdge 1950


How do pass in a limit to a query run inside a mysql 5 stored procedure?

Question: Here's a sample of what I'd like to do.
The stored procedure fails to create while the LIMIT is present on the query.
If I take the LIMIT off the procedure creates.
CREATE PROCEDURE LimitTest(IN in_start INTEGER UNSIGNED, IN in_limit INTEGER UNSIGNED)
BEGIN
   DECLARE no_more_results INTEGER DEFAULT 0;
   DECLARE var_result INTEGER UNSIGNED;
   DECLARE row CURSOR FOR SELECT id from test LIMIT in_start,in_limit;
   DECLARE CONTINUE HANDLER FOR NOT FOUND
    SET no_more_results = 1;
   OPEN  row;
      FETCH  row INTO var_result;
      REPEAT
         INSERT INTO results VALUES(var_result);
         FETCH  row INTO var_result;
      UNTIL no_more_results = 1
      END REPEAT;
   CLOSE row;
END

Answer: The issue is that the number behind limit must be a constant and can't be a parameter which according to this bug report remains unfixed in later versions of MySQL. You could try the (ugly as hell) workaround outlined here:-
CREATE PROCEDURE sp (
IN LimitStart_ INT,
IN LimitCnt_ INT
)
BEGIN
SET @lim = CONCAT(' LIMIT ', LimitStart_, ',', LimitCnt_);
SET @q = "SELECT mycol FROM mytable";

SET @q = CONCAT(@q, @lim);
PREPARE st FROM @q;
EXECUTE st;
DEALLOCATE PREPARE st;
END;

Comment: I looked into that, but I couldn't figure out how to get my results into a cursor so that I could use them in a later insert.


How do we need to define the acceptance ratio of the Metropolis-Hastings algorithm for a vanishing proposal density?

Question: I've seen that different authors define the acceptance probability $\alpha$ of the Metropolis-Hastings algorithm with target distribution density $p$ and proposal kernel density $q$ differently; some define $$\alpha(x,y):=\begin{cases}\displaystyle1\wedge\frac{p(y)q(y,x)}{p(x)q(x,y)}&\text{, if }p(x)q(x,y)>0\\\color{red}1&\text{, otherwise}\end{cases}\tag1$$ and others define $$\alpha(x,y):=\begin{cases}\displaystyle1\wedge\frac{p(y)q(y,x)}{p(x)q(x,y)}&\text{, if }p(x)q(x,y)>0\\\color{red}0&\text{, otherwise}.\end{cases}\tag2$$
So, is one of them wrong or does it simply not matter which one we use?
Answer: While the Markov chain $(X_t)$ is producing values such that $p(x_t)=0$ it has clearly not "reached" stationarity, so it should keep moving over the state space. Any way to move around until the support of $p$ is reached is valid, including (1) of course.
However, solution (2) is clearly wrong since, if $p(x)=0$, the chain will never move away from its current value.

Note also that$$p(x)q(x,y)=0$$implies $$p(x)=0\qquad \text{a.s.}$$when $y$ is a
  realisation from the density $q(x,\cdot)$.
Comment: $q(x,y)=0$ is impossible if $Y$ is simulated via this conditional density...
Comment: You're right. $(2)$ doesn't make sense if $p(x)=0$. I was more thinking about the case $p(x)>0$ and $q(x,y)=0$. Intuitively it would make more sense to me to reject such a problem, but according to $(1)$ we would need to accept it. So, could we use $$\alpha(x,y):=\begin{cases}\displaystyle1\wedge\frac{p(y)q(y,x)}{p(x)q(x,y)}&\text{, if }p(x)q(x,y)>0\\0&\text{, if }p(x)>0\text{ and }q(x,y)=0\\1&\text{, otherwise}\end{cases}\tag3$$ instead?


Drop columnns from dataframe where last three characters equal

Question: I wish to only select particular columns from a dataframe, however the columns I don't want all end with "Nav".
How can I accomplish this?
I've tried something similar to the below
jsonDF2.select([c for c in jsonDF2.columns if c not in {'%Nav'}])

Any advice would be appreciated.
UPDATE
Currently using
#jsonDF2 = jsonDF2.select("d.*")

because I'm exploding some JSON that is nested in "d." so using blackbishops code, it currently places all the JSON within one column, instead of multiples screenshots of example below:
Code Used & Result:
jsonDF2 = jsonDF2.select("d.*")

Suggested Code
jsonDF2.select(*[F.col(c) for c in jsonDF2.columns if not c.endswith("Nav")])

I've tried place the d. prior the the * in the suggested code but got no joy. I know that "F" is obviously for the import. I tried also place the d. before the "c" and got no joy as well.
Answer: Try this:
from pyspark.sql import functions as F

jsonDF2.select(*[F.col(c) for c in jsonDF2.columns if not c.endswith("Nav")])

Comment: Hi, I've updated my question above as I am missing a bit I've not been clear on. I'm exploding JSON so the column is prefixed with a the letter d and using this code doesn't show the dataframe as expected. PLease see above.
Comment: @Lynchie first do `df = jsonDF2.select("d.*")` then `df.select(*[F.col(c) for c in df.columns if not c.endswith("Nav")])`
Answer: This should do it
[c for c in jsonDF2.columns if c[-3:] != 'Nav']

Answer: DataFrame.colRegex
You can use colRegex which is available in spark >= 2.3
df.select(df.colRegex('`.*(?<!nav)`'))


Integrate $\int_0^{\int_0^{\vdots}\frac{1}{\sqrt{x}}\text{d}x}\frac{1}{\sqrt{x}}\text{d}x$ and monotonicity of integrals

Question: I must evaluate
$$\int_0^{\int_0^{\vdots}\frac{1}{\sqrt{x}}\text{d}x}\frac{1}{\sqrt{x}}\text{d}x$$
My idea is that if we set
$$L:=\int_0^{\vdots}\frac{1}{\sqrt{x}}\text{d}x$$
Then the integral must satisfy the equation
$$\int_0^L \frac{1}{\sqrt{x}}\text{d}x=L$$
And we have that
$$\int_0^L \frac{1}{\sqrt{x}}\text{d}x=\lim_{\varepsilon \to 0^+}\int_\varepsilon^L\frac{1}{\sqrt{x}}\text{d}x=2\sqrt{L}$$
So we have the equation $2\sqrt{L}=L$, that leads to the solutions $L_1=0$ and $L_2=4$; now I suspect that
$$\int_0^{\int_0^{\vdots}\frac{1}{\sqrt{x}}\text{d}x}\frac{1}{\sqrt{x}}\text{d}x > 0$$
So the only choice left if $L_2=4$, but I don't actually know how to prove it rigorously; I'm sure that the last integral is $\geq0$ because $\sqrt{x}\geq0$, but maybe it is $>0$ because the square root can't be $0$ being at the denominator.
Two questions:
1) is my argument right? I'm not sure if it is rigorous, especially when I "substitute" the upper bound with $L$; maybe I can approach it with sequences.
2) In this case the square root was at the denominator so somehow I've excluded the fact that the integrand could be $\geq0$ (if my argument is correct), but in general how can I prove that if $f(x)>0$ then $\int_a^b f(x) \text{d}x >0$ and not $\int_a^b f(x)\text{d}x \geq 0$ (if this is true)?
Thanks.
Comment: Your expression makes no sense, at least not  to me.The upper bound of the upper bound is a bunch of dots. Can you put the expression in the form of  function  of x to be integrated from x=number to x=another number?
Comment: @P.Lawrence Thanks for your interest. It means that the upper bound goes like that an infinite amount of times, like $x^{x^{x^{\dots}}}$ means "$x$ to the power of $x$ to..." an infinite amount of times.
Comment: Actually, both the values can be considered since there is no meaningful interpretation of the integral.
Answer: 1)
I'll try to make it more rigorous.
Let
$$
a_0= \int_0^\alpha \frac{1}{\sqrt{x}} \mathrm{d}x = 2 \sqrt{\alpha}\\[3ex]
a_{n+1}=\int_0^{a_n}\frac{1}{\sqrt{x}} \mathrm{d}x = 2 \sqrt{a_n}
$$
for some $\alpha \ge 0$. We are interested in evalulate
$$L=\lim_{n \to \infty} a_n. $$
From the general equation we have
$$L =2 \sqrt{L} $$
so if we suppose the limit exists, then a priori we have three possibilities:

$L=0$;
$L=4$;
$L= +\infty$.

Notice that for the first terms of the sequence we have
$$
a_0 = 2 \alpha^{\frac{1}{2}}\\
a_1 = 2 \sqrt{a_0}= 2 \sqrt{2 \alpha^{\frac{1}{2}}} = 2 \cdot 2^{\frac{1}{2}}  \cdot\alpha^{\frac{1}{2^2}}\\
a_2 =  2 \sqrt{a_1} = 2 \cdot 2^{\frac{1}{2}} \cdot 2^{\frac{1}{2^2}}\cdot\alpha^{\frac{1}{2^3}}
$$
and, in general,
$$
a_n=2^{\sum_{i=0}^n \big(\frac{1}{2}\big)^i} \cdot \alpha^{\frac{1}{2^{n+1}}}
$$
(note that if $\alpha = 0$ then $a_n = 0$ for every $n > 0$ ).
So, since
$$
\sum_{i=0}^n \bigg(\frac{1}{2}\bigg)^i = \frac{1-\frac{1}{2^{n+1}}}{1 - \frac{1}{2}} = 2 - \frac{1}{2^n}
$$
(see Formula for geometric series ), we get
$$
a_n = 4 \cdot 2^\frac{1}{2^{n+1}} \cdot  \alpha^{\frac{1}{2^{n+1}}}
$$
Now
$$\lim_{n \to \infty} 2^\frac{1}{2^{n+1}}=2^0=1$$
and hence if $\alpha \neq 0$,
$$
\lim_{n \to \infty}a_n = 4 \cdot \lim_{n \to \infty} 2^\frac{1}{2^{n+1}}\cdot \lim_{n \to \infty}\alpha^{\frac{1}{2^{n+1}}} = 4 \cdot\lim_{n \to \infty}\exp \left( \frac{\ln \alpha}{2^{n+1}} \right) = 4 \cdot\exp \left(\ln \alpha\cdot \lim_{n \to \infty} \frac{1}{2^{n+1}} \right)=4 \cdot e^0 = 4
$$
finally
$$ \lim_{n \to \infty} a_n = \begin{cases} 0 & \text{if  $\alpha =
 0$}; \\ 4 & \text{if  $\alpha > 0$} \\ \end{cases} $$
2)
Riemann integral has the monotonicity proprierties, see here for more details.
Comment: the fact that there are only 3 apriori possibilities assumes the limit exists. it's prolly not hard to show limit exists, but you should address it
Comment: This Is true, thank you! I should have written "the possibile limits are..."
Answer:
1) is my argument right? I'm not sure if it is rigorous, especially when I "substitute" the upper bound with L; maybe I can approach it with sequences.

The question isn't even properly posed, as
$$\int_0^{\int_0^\vdots\frac1{\sqrt x}~\mathrm dx}\frac1{\sqrt x}~\mathrm dx$$
isn't a meaningful expression. You want to instead consider something such as:
$$a_0=\alpha\ge0\\a_{n+1}=\int_0^{a_n}\frac1{\sqrt x}~\mathrm dx=2\sqrt{a_n}$$
and ask what
$$L=\lim_{n\to\infty}a_n$$
is. It can easily be seen that if $\alpha=0$, then $L=a_n=0$ for all $n$. If $0<a_n<4$, then we have
$$a_n<2\sqrt{a_n}=a_{n+1}<4$$
and if $a_n>4$, then we have
$$4<a_{n+1}=2\sqrt{a_n}<a_n$$
so for any $\alpha>0$, it converges, since $a_n$ is monotone and bounded. Now that you know that it converges, it suffices to solve
$$L=2\sqrt L$$
restricted to the interval that we know the solution is in (from the bounds), which in this case gives $L=4$.

2) In this case the square root was at the denominator so somehow I've excluded the fact that the integrand could be $\ge0$ (if my argument is correct), but in general how can I prove that if $f(x)>0$ then $\int^b_af(x)dx>0$ and not $\int^b_af(x)dx\ge0$ (if this is true)?

This is not true unless $a<b$ and $f$ is Riemann integrable, in which case you can see Is the Riemann integral of a strictly positive function positive?.
Comment: Thanks for the answer, it is all clear except one thing: when you are considering $\alpha=0$ we have $\int_0^0 \frac{1}{\sqrt{x}} \text{d}x$; now I know that $\int_a^a f(x) \text{d}x=0$, but $f$ is divergent when $x \to 0$.
Why can we say that the integral is $0$? It is because $\frac{1}{\sqrt{x}}$ is integrable when $x \to 0$? Are there some cases where I can't say that $\int_a^a f(x) \text{d}x = 0$, for example if $f$ hasn't finite integral for $x \to a$?
Comment: These are improper Riemann integrals, so we are interpreting it as $\lim_{a,b\to0^+}\int_a^b\frac{\mathrm dx}{\sqrt x}$, which is zero. In fact, if this converges, then it will always be zero.


How draw antialiasing lines over texture in OpenGL ES?

Question: I have texture (background image) and i trying draw antialiasing lines.
If i do these steps:
1) draw texture, draw shapes, glReadPixels() -> ugly lines
2) draw texture, draw shapes, glResolveMultisampleFramebufferAPPLE(), glReadPixels() -> slow performance
Can i make something like this?
framebuffer1: draw texture
framebuffer2: draw shapes, glResolveMultisampleFramebufferAPPLE()
then merge frame buffers, glReadPixels()
Comment: Why are you using `glReadPixels`? It's slow (unless you are using PBOs) and you don't need to use it if you are rendering to textures.
Comment: @ColonelThirtyTwo, I'm trying to make a video editor.
Later i create CVPixelBufferRef, and add it to AVAssetWriterInputPixelBufferAdaptor.
Answer: I would do this in three passes. First draw your lines normally with a frame buffer object with an assigned texture. Use this texture in a second pass with an FXAA (Fast approximate Anti-Aliasing) shader on another frame buffer object with a texture attached to it. At last you merge the resulting texture from the FXAA pass with the background image you mentioned.


Rails way to detect mobile device?

Question: Is there a "rails way" to detect whether the user is using a mobile device? What I mean is a method I can use in erb, something like this:
<% if mobile? %>

Answer: You can do it that way by defining a function like below:
def mobile_device?
  if session[:mobile_param]
    session[:mobile_param] == "1"
  else
    request.user_agent =~ /Mobile|webOS/
  end
end

Or you can use gems to detect mobile devices like

https://github.com/tscolari/mobylette
https://github.com/shenoudab/active_device
Comment: Looks like active_device is inactive.
Comment: could you please update the answer to a mix of yours and the one below as browser gem is more used as shown in https://www.ruby-toolbox.com/projects/browser
Comment: how to test it, I mean how do I tell respect that I want to run it now as mobile devise?
Comment: agent sniffing is an pretty imperfect way to do this, and whatever implementation you choose to sniff, it gets less accurate over time
Answer: Use browser gem. You are able to detect the browser by the custom user_agent.
Comment: Do you know if Google Chrome's mobile browser emulator would detect `browser.mobile?` with that gem?
Comment: `browser` is nice but requires > ruby 2. if you're stuck on 1.9.3 (like me) check out https://github.com/tscolari/mobylette
Comment: @JoeMorano it does work in Chrome's emulator for me.
Comment: `browser` appears to still be maintained in 2020 and has only increased in usage over time.
Answer: In application_helper.rb:
def device
  agent = request.user_agent
  return "tablet" if agent =~ /(tablet|ipad)|(android(?!.*mobile))/i
  return "mobile" if agent =~ /Mobile/
  return "desktop"
end

Then you can use it in your views:
<% if device == "mobile" %>
  Show something for mobile users.
<% end %>

Answer: I would suggest checking out device_detector, which is quite fast and easy to deploy. You just have to pass user_agent string to it, which can be done using request.user_agent for Rails 5:
@user_agent = request.user_agent

@client = DeviceDetector.new(@user_agent)

@client.device_type

Note: Device types can be one of the following: desktop, smartphone, tablet, console, portable media player, tv, car browser, camera
https://github.com/podigee/device_detector
Answer: You can solve this too by simply including <meta name="viewport" content="width=device-width, initial-scale=1.0"> on the application.html.erb header tag
As long as your site is responsive (e.g. using Bootstrap), you should be fine.
Comment: The question seeks to check if the user agent is a mobile device. Your answer aims to enable a responsive behavior, which is pretty different from the question intention.

Rails way to detect mobile device?


What's the derivative of $\underbrace{x^{x^{x^{x^...}}}}_n$? Are my calculations right?

Question: The problem:
To make things easier on us so we don't have to use an underbrace, I'm going to declare:
$$\Psi(x,n) := \underbrace{x^{x^{x^{x^{\dots}}}}}_n$$
And we evaluate $f$ from the topright to the bottom left (like a calculator does).
Question: What is
$$\Psi'(x,n) = \frac{\partial}{\partial x}\Psi(x,n) = \text{?}$$
My attempt:
Since we know that
$$\begin{align}\frac{\partial}{\partial x}f(x)^x &= \frac{\partial}{\partial x} e^{x \ln f(x)} = \\ &= e^{x \ln f(x)} \left(\frac{x f'(x)}{f(x)} + \ln f(x)\right) = \\ &= f(x)^x \left(\frac{x f'(x)}{f(x)} + \ln f(x)\right),\end{align}$$
We can substitute
$$f(x) := \Psi(x,n-1).$$
So we get
$$\begin{align}
\frac{\partial}{\partial x}\Psi(x,n) &= \frac{\partial}{\partial x}\Psi(x,n-1)^x = \\ &= \Psi(x,n-1)^x \left(\frac{x \cdot \frac{\partial}{\partial x} \Psi(x,n-1)}{\Psi(x,n)} + \ln \Psi(x,n-1)\right) = \\ &= \Psi(x,n) \left(\frac{x \cdot \frac{\partial}{\partial x} \Psi(x,n-1)}{\Psi(x,n)} + \ln\underbrace{x^{x^{x^{x^{\dots}}}}}_{n-1}\right) = \\ &= x \cdot \frac{\partial}{\partial x} \Psi(x,n-1) + \Psi(x,n)x^{n-2}\ln x
.\end{align}$$
We were able to reduce $n$ by one and get the derivative for $\Psi(x,n)$ in terms of $\Psi(x,n-1)$. If we keep repeating this for $n-1,n-2,\dots,2,1$, we should get the full derivative. However, this is what I'm not quite sure of:
$$\begin{align}
\frac{\partial}{\partial x}\Psi(x,n) &= x \cdot \frac{\partial}{\partial x} \Psi(x,n-1) + \Psi(x,n)x^{n-2}\ln x = \\ &= x \cdot \frac{\partial}{\partial x} \Psi(x,n-2) + x \cdot \Psi(x,n-1)x^{n-3}\ln x + \Psi(x,n)x^{n-2}\ln x = \dots \\ \dots &= x \cdot \frac{\partial}{\partial x} \Psi(x,1) + \Psi(x,2)x^{n-2}\ln x + \dots + \Psi(x,n)x^{n-2}\ln x = \\ &= x + \ln x \cdot x^{n-2} \cdot \sum_{k=2}^n \Psi(x,k) = x + \ln \Psi(x,n-1) \sum_{k=2}^n \Psi(x,k)
\end{align}$$
So rewriting it with the underbrace notation we get:
$$\frac{\partial}{\partial x} \underbrace{x^{x^{x^{x^{\dots}}}}}_n = x + \ln \underbrace{x^{x^{x^{x^{\dots}}}}}_{n-1} \sum_{k=2}^n \underbrace{x^{x^{x^{x^{\dots}}}}}_k$$
Questions:
$1$. Is this correct? Are my calculations right?
$2$. Is there a better way to write the final answer?
$3$. Is there a name for this $(\Psi)$ function?
$4$. Can we generalize $\Psi(x,n)$ for rational (and perhaps irrational) $n$'s?
$5$. What could be the derivative of $\Psi(x,n)$ for such rational / irrational $n$'s?
Comment: I would suggest looking into tetration.
Comment: @PeterForeman Thank you! I was searching all over the internet but Google didn't like my search term of "x^x^x..." .I guess you just really need to know the name of things to be able to find them :)
Comment: I don't think it is correct.
Comment: The problem is that if $\Psi(x,n)$ is evaluated from the top right to left, your recursion is $$\Psi(x,n)=x^{\Psi(x,n-1)}$$ and not $$\Psi(x,n)=(\Psi(x,n-1))^x.$$ With your recursion, $\Psi(x,n)=x^{x^{n-1}},$ which is much easier to take the derivative of. But this is the left-to-right interpretation.
Answer: I think your answer is incorrect. Your recursion $$\Psi(x,n)=\left(\Psi(x,n-1)\right)^x$$ defines the left-to-right interpretation of your expression. The value of the left-to-right interpretation yields a simple $\Psi(x,n)=x^{(x^{n-1})}.$  The derivative in this left-to-right case is $$\frac{\partial}{\partial x}\Psi(x,n)=\Psi(x,n)\left(\frac{1}{x}x^{n-1}+(n-1)x^{n-2}\log x \right)=\Psi(x,n)x^{n-2}\left(1+(n-1)\log x\right)$$
But your question asked for a right-to-left reading, which yields the recursion: $$\Psi(x,n)=x^{\Psi(x,n-1)}$$
The derivative here is going to be messier.
Write it as $\Psi_n(x)$ for slight ease. Then, for $n=0,$ $\Psi_0(x)=1$ and for $n>0,$ $\Psi_n(x)=x^{\Psi_{n-1}(x)}=e^{\log(x)\Psi_{n-1}(x)}$. Then we apply the chain rule: $$\Psi_n'(x)=\left(\frac{1}{x}\Psi_{n-1}(x)+\log(x)\Psi_{n-1}'(x))\right)\Psi_n(x)$$
If we then write this in terms of $$\Phi_n(x)=\frac{\Psi_n'(x)}{\Psi_n(x)}$$ (which we can see as the derivative of $\log\Psi_n(x).$) Then $\Phi_0(x)=0$ and when $n>0$:
$$\Phi_n(x)=\Psi_{n-1}(x)\left(\frac{1}{x}+\log(x)\Phi_{n-1}(x)\right)$$
This is going to get ugly, but:
$$\begin{align}\Phi_n(x)&=\Psi_{n-1}(x)\left(\frac{1}{x}+\log(x)\Psi_{n-2}(x)\left(\frac{1}{x}+\Psi_{n-3}(x)\log(x)\left(\frac{1}{x}+\cdots\right)\right)\right)\\
&=\frac{1}{x}\sum_{k=1}^{n}\left(\log^{k-1}(x)\Psi_{n-1}(x)\Psi_{n-2}(x)\cdots \Psi_{n-k}(x)\right)
\end{align}$$
Then $$\Psi_n'(x)=\Psi_n(x)\Phi_n(x)=\frac{1}{x}\sum_{k=1}^{n}\left(\log^{k-1}(x)\Psi_n(x)\Psi_{n-1}(x)\Psi_{n-2}(x)\cdots \Psi_{n-k}(x)\right)$$
For example, $$\begin{align}\Psi_2'(x)&=\frac{1}{x}\Psi_2(x)\Psi_1(x)\left(1+ \log(x)\Psi_0(x)\right)\\
&=\frac1x \cdot x^x\cdot x(1+\log(x))
\\&=x^x \left(1+\log(x)\right)
\end{align}$$
and $$\begin{align}\Psi_3'(x) &= \frac{1}{x}\Psi_3(x)\Psi_2(x)\left(1+\log(x)\Psi_1(x)+\log^2(x)\Psi_1(x)\Psi_0(x)\right)\\
&=\frac{1}{x}\cdot x^{x^x}\cdot x^x\left(1+x\log(x)+x\log^2(x)\right)
\end{align}$$


GET em vez de DELETE no webservice REST

Question: Estou tentando fazer um delete no meu banco utilizando um webservice rest(JAVA), ele carrega tudo certinho, mas hora que mando o teste ele da Method not allowed.
Obs: Estou utilizando Netbeans.

Pelo que eu percebi o WS esta tentando realizar um GET e não um DELETE: GET SolicitaçãoFailed RequestFailed --> Status: (405)
então olhei minha classe e não faço ideia do que esta errado:
WS:
@DELETE
    @Path("Banco/delete/{id}")
    public String deleteBanco(@PathParam("id") int id){

        BancoDAO dao = new BancoDAO();
        String resposta = dao.deleteBanco(id);

        Gson gson = new Gson();
        return gson.toJson(resposta);
    }

DAO:
public String deleteBanco(int codigo) {
        try {
            conn = Conexao.obtemConexao();
            String delete = "DELETE FROM BANCO WHERE BCO_COIGO = '?'";

            stmt = conn.prepareStatement(delete);
            int i = stmt.executeUpdate();

            if (i == 0) {
                return "Erro ao excluir banco";
            } else if (i == 1) {
                return "Banco removido com sucesso...";
            } else if (i > 1) {
                return "Erro 001: \n Problema de SQL, mais de uma informação "
                        + "foi removida, favor entrar em contado com suporte "
            } else {
                return "Erro 301: \n Erro não identificado, "
                        + "favor entrar em contato com suporte.";
            }

        } catch (Exception e) {
            return "Erro ao excluir banco: " + e.getMessage();
        }
    }

Único método GET que possuo:
@GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("Banco/get/{nome}")
    public String getBanco(@PathParam("nome") String nome){
        List<BancoCTR> lista = new ArrayList<BancoCTR>();
        BancoDAO banco = new BancoDAO();

        if (nome.equals("null")){
            lista = banco.listBanco(0, nome);
        }else{
            lista = banco.listBanco(1, nome);
        }

        Gson gson = new Gson();
        return gson.toJson(lista);
    }

Comment: Se aceita uma dica fora da sua pergunta: olhando os endereços rest que criou, sugiro dar uma olhada em alterá-los para ficar mais aderente as boas práticas. Exemplo: mude de `Banco/delete/{id}"` para apenas `bancos/{id}"`
Comment: Se você estiver testando no browser, lembre-se que o mesmo só faz requisições via GET, para fazer um uma requisição via DELETE você pode usar uma ferramenta como o Postman, como já foi sugerido pelo LR10 na resposta dele, e aproveitando o comentário do @Dherik, vale a pena você procurar algum material teórico sobre o assunto, pois realmente o seu mapeamento não ficou muito condizente com a arquitetura Rest, é importante entender a proposta, e assim você saberá como usa-la da melhor forma.
Answer: O seu cliente está fazendo um request do tipo GET muda à requisição para DELETE como no exemplo abaixo. Estou usando POSTMAN
sua URL eve ficar mais ou menos a sim: localhost:8080/Banco/delete/1

GET em vez de DELETE no webservice REST


Software to sync with tempo in violin

Question: I am a beginner in violin (about 2 month) and i have trouble with syncing my tempo with metronome.
Beside the fact that thinking about everything makes me lose tempo, I use my phone as metronome and because of the volume of instrument itself I barely hear metronome beats.
I was thinking is there any software (or other technique) to help with that? for example a software being able to show notes with duration notations which I can visually validate myself?
Comment: If you put the violin away for a moment, can you _sing_ the rhythms in tempo without going out of sync? Just the rhythms, if you disregard the pitch aspect and everything about operating the violin.
Comment: See also: [Is there midi software that can show me if I'm hitting notes on the beat?](https://music.stackexchange.com/q/75580/70803).
Comment: Do you want to see the notes you play, or do you want to see if you managed to play the displayed notes? I made a program for the first variant, but it had issues detecting the correct pitch so I think the second option might be more useful.
Answer: If you want something which is like your phone app metronome only louder then the best thing is a separate dedicated digital metronome.The volume on mine can be turned up painfully high.
If you want something more visual then an old fashioned metronome is best where you can see an actual thin stick moving back and forwards at the set rhythm.
Answer: The solution is actually very simple. If you like your phone's metronome simply:

Connect your phone to an external speaker. Smaller inexpensive wired or Bluetooth speakers are available for a very reasonable price.

Use a pair of open air headphones or use a single earbud placed loosely in your ear so you can hear both the click and your instrument clearly.

As for the software you speak of that shows note durations in real time, software recommendations are off topic on this site but most DAW's (digital audio workstation software) can show a piano roll with real time notes and durations. The only issue is the desired music has to be either programmed or found and downloaded online in MIDI format.
Answer: The problem is not a mechanic issue, but the lack of internalizing the tempo.
Are you able to do any sort of rhythmic activity with the music? (Dance, tap your feet/fingers, bob head, etc.) You actually need to learn to internalize the rhythm. A metronome is only one tool to do this, and ultimately no skilled player ever plays music with the metronome blasting.
Although using the metronome is useful, it may not necessarily be needed as an adult. You have been exposed to music before, and with different context. If you can walk in beat with music, or exercise with music and change pace according to what you hear, then you probably can try forgoing the metronome and simply internalize the beat instead.

Software to sync with tempo in violin


Concurrent users without database

Question: I can't seem to get this concept right in my head. If I have a website that gets 1 million concurrent users, without any databases at all, will I need to scale? I'm Using Node.js and Socket.IO. Also is there a way I could simulate something like this on my localhost?
Answer: Having one million user, or connections, on Socke.io, doesn't mean you have to scale, but depending on what they are doing, you would probably do. Having a data base adds storage but has nothing more to do with the need for scaling the Node.JS server.
You can create a test to try to insert as much as you want using a loop to connect and then try to emit an event for each of then.
Answer: For scaling node you can use a cluster. A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load. https://nodejs.org/api/cluster.html#cluster_cluster
To simulate high load, there are open source tools you can use for free: http://www.opensourcetesting.org/category/performance/

Concurrent users without database


Calculation for inter-rater reliability where raters don't overlap and different number per candidate?

Question: I want to calculate the degree to which the gymnastics judges agree on balance beam scores, i.e., "inter-rater reliability". However, not all judges judge the same candidates, and the number of judges per candidate also varies. There are around 30 judges making roughly 1500 observations.
The data looks like this:

Can you please tell me how to do this statistically, perhaps using Cronbach's alpha?
STATA set-up advice would help, as well.
Comment: Questions solely about how software works are off topic here, but you may have
a real statistical question buried here. You may want to edit your question to
clarify the underlying statistical issue. You may find that when you understand
the statistical concepts involved, the software-specific elements are
self-evident or at least easy to get from the documentation.
Answer: Here's an example using the kappa-statistic measure of interrater agreement. Before proceeding, we will need to reshape the data so that each row is a gymnast  but each score variable corresponds to single judge.
clear
input byte Gymnast str9 Judge double Score
1 Smith 5.5
1 Bartlet 6
1 Baily 8
2 Smith 10
2 Patterson 9.5
3 Baily 8
3 Patterson 7
3 Smith 7.5
4 Bartlet 7.5
end
rename Score Score_
reshape wide Score_, i(Gymnast) j(Judge, string)
kap Score_*

The combined kappa is -0.1912, which would be considered poor. Stata recommends the following RoTs for summarizing agreement:
below 0.0 Poor
0.00 – 0.20 Slight
0.21 – 0.40 Fair
0.41 – 0.60 Moderate
0.61 – 0.80 Substantial
0.81 – 1.00 Almost perfect

Comment: If I do that, I get:  "Judge not unique within Gymnast;
there are multiple observations at the same Judge within Gymnast." I'm not sure what that means except I think it's because not all Gymnasts have the same set of Judges....
Comment: It's probably not that since in my example the set of judges varies across gymnasts and it works. Before you reshape, try `duplicates tag Gymnast Judge, gen(dups)` and `tab dups`. If dups is ever above zero, you have the same gymnast judged twice by the same judge. This could happen if the gymnast competes in two separate competitions. If that is the case, you have panel data, which is harder to model and will require some  additional assumptions before you can proceed.
Comment: Maybe I have 2 duplicates? In which case, perhaps I can just drop those points?

    `Dups    Freq            Percent        Cum1`
    `0             1140         99.82           99.82`
    `1              2                0.18            100`
    `Total      1142   100.00`
Comment: Looks like you have 2 duplicates. It might well be a data entry error, so take a look at the scores. They might be the same, so you can just drop one of the rows. If not, you have to use your judgment.
Comment: I went ahead and dropped the row. But now I get:                                          "kap Score* matsize too small. You have attempted to create a matrix with too many rows or columns or attempted to fit a model with too many variables.  You need  to increase matsize; it is currently 800...."
Comment: I can't replicate your error, but this can probably be fixed with `set matsize XXX`, where XXX is some suitably large integer like 1500.
Comment: @EconoQ Did this help?
Comment: Yes, this helped. My STATA version isn't powerful enough to finish the calculation, but your advice did help. As it turns out, I think I'll have to calculate inter-rater reliability in blocks of gymnasts with the same judges.... and then the average difference between judges in each block should correlated to the average difference between judges of the other blocks (even if one block of gymnasts scores significantly less than the others). Then, I could also enter all gymnasts of a single block into a regression with the judges to make sure there's no correlation...right?
Comment: @EconoQ I am not sure what you want to accomplish, so I cannot help here. I might ask this question on Statalist with the added constraint about Stata version.


How can you know which mySQL code to use in which program?

Question: I'm working on mySQL and have installed WAMP-server and mySQL workbench.
I know mySQL is a standard, but with various dialects. My question is, how can I know when to use which code?
Sometimes when creating a table, you have to write test and sometimes 'test'.
Some examples:

CREATE TABLE test (id INT NOT NULL PRIMARY KEY,name VARCHAR(15) NOT NULL, no INT FOREIGN KEY REFERENCES Persons(no);
CREATE TABLE 'test'('id' NOT NULL PRIMARY KEY, 'name' VARCHAR(15) NOT NULL, 'no' INT FOREIGN KEY REFERENCES Persons(no));
CREATE TABLE test(id INT NOT NULL, name VARCHAR(15) NOT NULL, no INT NOT NULL, PRIMARY KEY(id), FOREIGN KEY(no) REFERENCES Persons(no));

Some places you need to end the command with ; and some places not...
Same with ' .
And some places you write the PRIMARY KEY at the back of the decleration and some places you enter it at the end.
WAMP server DOS, WAMP server phpmyadmin and mySQL workbench is all different.
THanks in advance for the answer:)
Answer: For table and columns names use backticks `tableName`,but they are necessary only when using reserved names.Reserved names in Mysql
Quotes are used for strings '123'-this is a string
Semicolon indicates that the statement ends,so it is necessary when you have multiple statements.
Primary key you can add it on the column definition,or add it later.
Comment: Ahh, I didn't realize that it was different with backticks and quotes. It's hard to see the differences in my book. So to sum up what you're saying. I only need the backticks when if I want to call a variable i.e. GENERAL, then it should be ` general `? And I can use them all the time if I want?
Comment: @user2963949 again backticks are for column and tables names.I don't know what you call a variable.IN your example test,id,name and no.Yes you can use them all the time and yes general is a reserved word and if you want to name a table general you need backticks.


Take 6: What chords are making this style so jazzy?

Question: Unfortunately this question has been closed as off-topic:
What is this Chord Progression
But the arrangements from Take 6 sound very special and interesting to me: I couldn't identify at "first sight" the means of chord-technique that makes it sound so jazzy.

The album was critically acclaimed, receiving Grammy Awards in both the jazz and gospel categories. "In a diverse selection of songs, Take 6 merges an a cappella sound with gospel and big-band jazz arrangements... Their harmonies and melodies are consistent throughout this outstanding collection of songs," said Craig Lytle for AMG
Are these all seventh chords? And If yes, where are the 7ths lying?
Comment: It used to be called 'close harmony' singing. Oh rats! I was half way through transcribing it when Tim mentioned someone had done it.
Comment: @Albrecht I was using the piano, and looping the audio. Yes - good practice :)
Comment: @OldBrixtonian - correct on both counts, sir!
Comment: Are you asking specifically about the recording in the linked question, or are you asking generally? Either way, it would be helpful if you'd link to one or more recordings in your post, even if it's the same recording.
Comment: I mean this video, but I guess there are many other similar 6-voice settings by this group, e.g. *over the hill is home* ...
Comment: There's a full transcription on you tube - Google *he never sleeps take 6 full transcription*.  'fill yer boots!'
Comment: I know, Tim. I've seen this too. That's why I've posted this question hoping someone will be answering like you're commenting. How can the question of ketchup be transformed that it won't be off-topic? We've answered many more simple questions about chords and progressions ... and no one found they were off topic!
Comment: Oh, sorry, I meant to answer it myself by posting the youtube link. But it must have been a good practice for you ;). Well, I'm not sure I would have been able to transcribe it without help of a keyboard.
Comment: @OldBrixtonian - sorry! Carry on, though, there's lots on the 'net that isn't accurate...
Comment: @Tim You must be joking! Whatever's out there is close enough for me! I wasn't doing it properly anyway: just chord symbols.
Comment: @OldBrixtonian - funnily enough - I was...
Comment: @Tim Where's the emphasis? Are you saying 'funnily enough I WAS joking', 'funnily enough **I** was just transcribing it as chords' or 'funnilly enough I **was** doing it properly'?
Comment: @OldBrixtonian - all emphases were omitted on purpose..!
Comment: @Tim Slippery blighter!
Answer: Yes, as the comments say: these are close sevenths and ninths chords that produce this typical acapella jazzy sound. The seventh lays often in the  5th part (close to the bass note).
Here is the full transcription:
Comment: I bet it was in G originally. Once they knew it they could sing it at any pitch, depending on how the top and bottom singers were feeling. Putting it down a semi made the bottom B almost unreachable by the bass. You can hardly hear it but it is there. You don't often hear a bass sing a bottom B.
Comment: Why would anyone need to sing this in F#? F or G would be just as good - easier to transcribe, and easier to play, should someone wish to compress it all down to the grand stave.
Comment: Eventually G was too high for the Soprano (resp. counter tenor) and F too low for the Bass. Maybe there exist already a transposable file in musescore. It is ieasy to read this here above in F - letting  drop the sharps and changing naturals in to flats.
Comment: [Here](https://www.youtube.com/watch?v=gMnhK2D8MrU) is the YouTube video -- in case you want the full YouTube functionality including full-screen.
Comment: see my new question concerning *speak my Lord* (Erik Leidzen)
Answer: The chords do have jazzy sevenths, ninths, etc. sometimes voiced very close together like a semitone, sometimes with quartal/quintal voicings and so on, but my subjective feeling is that somehow the voicings and notes alone don't create the whole effect. Something more is needed and that is the singers, the human voices.
Here's an example of two six-voice chords, first with a piano sound and then the same chords sung (with a heavy dose of MIDI-enforced pitch correction)

Do you agree? In my opinion, the chords, when played on a piano, don't feel that special, but when they're sung, that's when the magic starts to happen.
Comment: I also think the factor human voice is very important.
Comment: There's some kind of processing on the original but I'm not sure what. Apart from the soloists there's no vibrato. It's extremely precise (apart from the end of the first line, where the end of the word 'sleeps' is oddly ragged: 'sleeps-ps'). It's difficult harmony to pitch accurately. I think all those things create what you call the magic.
Comment: @OldBrixtonian AFAIK, these questions are related to orchestration - it's not just the notes, it's what instruments play the notes, and different instrument combinations can create very different sounds for the same notes. Someone asking about the chords might actually be interested in reproducing the whole sound, not just the notes.
Answer: I think the chords are a bit more complicated than 7th and 9ths. In fact they're at that end of the jazz harmony spectrum where chord names aren't much use: where 11's and 13's don't leave anything out, and where it might be better to call them eg D/C7 (D over C7), meaning C E G Bb D F# A and so on, and to use I, II and III to show any inversions: DII/F#7(no1, no5), meaning A# E A D F# - at which point the part becomes un-sight-readable or unreadable and a written-out part, with actual notes, is preferable!
Thanks to the OP for pointing out Take 6 and to Albrecht for insisting it stay open. Interesting. I wish we were allowed to raise subjects with just "Here's something interesting". We could always decide later what question had been answered!
Comment: Agree. Maybe this is something for the meta?
Comment: @AlbrechtHügli I need to read up on Meta. I'm still newish here and don't quite get how it works.


What is elastic net regularization, and how does it solve the drawbacks of Ridge ($L^2$) and Lasso ($L^1$)?

Question: Is elastic net regularization always preferred to Lasso & Ridge since it seems to solve the drawbacks of these methods? What is the intuition and what is the math behind elastic net?
Comment: Check out Hastie et al. ["The Elements of Statistical Learning"](http://www-stat.stanford.edu/~tibs/ElemStatLearn/download.html) chapters 3 and 18 (search "elastic net").
Answer: 1. Which method is preferred?
Yes, elastic net is always preferred over lasso & ridge regression because it solves the limitations of both methods, while also including each as special cases. So if the ridge or lasso solution is, indeed, the best, then any good model selection routine will identify that as part of the modeling process.
Comments to my post have pointed out that the advantages of elastic net are not unqualified. I persist in my belief that the generality of the elastic net regression is still preferable to either $L^1$ or $L^2$ regularization on its own. Specifically, I think that the points of contention between myself and others are directly tied to what assumptions we are willing to make about the modeling process. In the presence of strong knowledge about the underlying data, some methods will be preferred to others. However, my preference for elastic net is rooted in my skepticism that one will confidently know that $L^1$ or $L^2$ is the true model.

Claim: Prior knowledge may obviate one of the need to use elastic net regression.

This is somewhat circular. Forgive me if this is somewhat glib, but if you know that LASSO (ridge) is the best solution, then you won't ask yourself how to appropriately model it; you'll just fit a LASSO (ridge) model. If you're absolutely sure that the correct answer is LASSO (ridge) regression, then you're clearly convinced that there would be no reason to waste time fitting an elastic net. But if you're slightly less certain whether LASSO (ridge) is the correct way to proceed, I believe it makes sense to estimate a more flexible model, and evaluate how strongly the data support the prior belief.

Claim: Modestly large data will not permit discovery of $L^1$ or $L^2$ solutions as preferred, even in cases when the $L^1$ or $L^2$ solution is the true model.

This is also true, but I think it's circular for a similar reason: if you've estimated an optimal solution and find that $\alpha\not\in \{0,1\},$ then that's the model that the data support. On the one hand, yes, your estimated model is not the true model, but I must wonder how one would know that the true model is $\alpha=1$ (or $\alpha=0$) prior to any model estimation. There might be domains where you have this kind of prior knowledge, but my professional work is not one of them.

Claim: Introducing additional hyperparameters increases the computational cost of estimating the model.

This is only relevant if you have tight time/computer limitations; otherwise it's just a nuisance. GLMNET is the gold-standard algorithm for estimating elastic net solutions. The user supplies some value of alpha, and it uses the path properties of the regularization solution to quickly estimate a family of models for a variety of values of the penalization magnitude $\lambda$, and it can often estimate this family of solutions more quickly than estimating just one solution for a specific value $\lambda$. So, yes, using GLMNET does consign you to the domain of using grid-style methods (iterate over some values of $\alpha$ and let GLMNET try a variety of $\lambda$s), but it's pretty fast.

Claim: Improved performance of elastic net over LASSO or ridge regression is not guaranteed.

This is true, but at the step where one is contemplating which method to use, one will not know which of elastic net, ridge or LASSO is the best. If one reasons that the best solution must be LASSO or ridge regression, then we're in the domain of claim (1). If we're still uncertain which is best, then we can test LASSO, ridge and elastic net solutions, and make a choice of a final model at that point (or, if you're an academic, just write your paper about all three). This situation of prior uncertainty will either place us in the domain of claim (2), where the true model is LASSO/ridge but we did not know so ahead of time, and we accidentally select the wrong model due to poorly identified hyperparameters, or elastic net is actually the best solution.

Claim: Hyperparameter selection without cross-validation is highly biased and error-prone.

Proper model validation is an integral part of any machine learning enterprise. Model validation is usually an expensive step, too, so one would seek to minimize inefficiencies here -- if one of those inefficiencies is needlessly trying $\alpha$ values that are known to be futile, then one suggestion might be to do so. Yes, by all means do that, if you're comfortable with the strong statement that you're making about how your data are arranged -- but we're back to the territory of claim (1) and claim (2).
2. What's the intuition and math behind elastic net?
I strongly suggest reading the literature on these methods, starting with the original paper on the elastic net. The paper develops the intuition and the math, and is highly readable. Reproducing it here would only be to the detriment of the authors' explanation. But the high-level summary is that the elastic net is a convex sum of ridge and lasso penalties, so the objective function for a Gaussian error model looks like
$$\text{Residual Mean Square Error}+\alpha \cdot \text{Ridge Penalty}+(1-\alpha)\cdot \text{LASSO Penalty}$$
for $\alpha\in[0,1].$
Hui Zou and Trevor Hastie. "Regularization and variable selection via the elastic net." J. R. Statistic. Soc., vol 67 (2005), Part 2., pp. 301-320.
Richard Hardy points out that this is developed in more detail in Hastie et al. "The Elements of Statistical Learning" chapters 3 and 18.
3. What if you add additional $L^q$ norms?
This is a question posed to me in the comments:

Let me suggest one further argument against your point of view that elastic net is uniformly better than lasso or ridge alone. Imagine that we add another penalty to the elastic net cost function, e.g. an $L^3$ cost, with a hyperparameter $\gamma$. I don't think there is much research on that, but I would bet you that if you do a cross-validation search on a 3d parameter grid, then you will get $\gamma\not =0$ as the optimal value. If so, would you then argue that it is always a good idea to include $L^3$ cost too.

I appreciate that the spirit of the question is "If it's as you claim and two penalties are good, why not add another?" But I think the answer lies in why we regularize in the first place.
$L^1$ regularization tends to produce sparse solutions, but also tends to select the feature most strongly correlated with the outcome and zero out the rest. Moreover, in a data set with $n$ observations, it can select at most $n$ features. $L_2$ regularization is suited to deal with ill-posed problems resulting from highly (or perfectly) correlated features. In a data set with $p$ features, $L_2$ regularization can be used to uniquely identify a model in the $p>n$ case.
Setting aside either of these problems, the regularized model can still out-perform the ML model because the shrinkage properties of the estimators are "pessimistic" and pull coefficients toward 0.
But I am not aware of the statistical properties for $L^3$ regularization. In the problems I've worked on, we generally face both problems: the inclusion of poorly correlated features (hypotheses that are not borne out by the data), and co-linear features.
Indeed, there are compelling reasons that $L^1$ and $L^2$ penalties on parameters are the only ones typically used.
In Why do we only see $L_1$ and $L_2$ regularization but not other norms?, @whuber offers this comment:

I haven't investigated this question specifically, but experience with similar situations suggests there may be a nice qualitative answer: all norms that are second differentiable at the origin will be locally equivalent to each other, of which the $L^2$ norm is the standard. All other norms will not be differentiable at the origin and $L^1$ qualitatively reproduces their behavior. That covers the gamut. In effect, a linear combination of an $L^1$ and $L^2$ norm approximates any norm to second order at the origin--and this is what matters most in regression without outlying residuals.

So we can effectively cover the range of options which could possibly be provided by $L^q$ norms as combinations of $L^1$ and $L^2$ norms -- all without requiring additional hyperparameter tuning.
Comment: +1 for in-depth discussion, but let me suggest one further argument against your point of view that elastic net is uniformly better than lasso or ridge alone. Imagine that we add *another* penalty to the elastic net cost function, e.g. an L3 cost, with a hyperparameter $\gamma$. I don't think there is much research on that, but I would bet you that if you do a cross-validation search on a 3d parameter grid, then you will get $\gamma \ne 0$ as the optimal value. If so, would you then argue that it is always a good idea to include L3 cost too?
Comment: @amoeba's question was very shrewd, & I think in answering it you seem to have changed your standards somewhat. Unless you're *absolutely sure* that a mix of $L_1$ & $L_2$ penalization is best then why not let the data decide how much $L_3$ penalization to apply? Your arguments still seem just a bit too strong & appear to justify adding more (hyper)parameters in almost any situation.
Comment: Saying that "elastic net is always preferred over lasso & ridge regression" may be a little too strong. In small or medium samples elastic net may not select pure LASSO or pure ridge solution even if the former or the latter is actually the relevant one. Given strong prior knowledge it could make sense to choose LASSO or ridge in place of elastic net. However, in absence of prior knowledge, elastic net should be the preferred solution.
Comment: I have to dissent: introducing $\alpha$ as another hyperparameter means it has to be set or tuned & improved performance is not guaranteed - see [How bad is hyperparameter tuning outside cross-validation?](http://stats.stackexchange.com/q/137481/17230)
Comment: "We can test LASSO, ridge and elastic net solutions, and make a choice of a final model" - we can, but of course that itself is a new procedure, optimizing a criterion subject to random error, which may or may not perform better than LASSo, or ridge regression, or elastic net alone.
Comment: Although I praised your edit above, I think there are a couple of points which I do not buy. First, the OP is very clear and precise, and the direct answer to the question should be "No" - because of the numerous qualifications; still, you kept an unqualified statement in the first sentence of your answer. Second (and less importantly), I do not buy the "unspoken context to OP's question". Your answer better suits a question "Should I normally prefer elastic net to LASSO and ridge?" rather than the actual question.
Comment: @Scortchi Yes, I can see now that I've backed myself into a corner. However, I'm not sure that anyone has escaped this particular corner: commentators and myself seem to agree that you should use an elastic net regression except when you know it's the wrong answer, but my point is that you don't have evidence of what model is the right answer until you can compare alternatives. Others appear able to will themselves into knowing which model is right ahead of time, but this baffles me.
Comment: I'm not certain how refine my answer satisfactorily to account for my description here, so I will leave this comment here, with the snarky recommendation that this area of statistics be the topic of further research.
Comment: You want the fewest tuning parameters possible, to reduce the variance of the procedure. An argument against the inclusion of an $\ell_3$ norm in the penalty might just amount to an argument that $\ell_1$ or $\ell_2$ penalization is more important than $\ell_3$ penalization. In settings where you are not interested in coefficient clustering, this is true.
Comment: Fair points, and a welcome elaboration.
Comment: Thanks! I'll read with care & see if I have anything to comment further. But my first comment was in response to what you wrote with no caveat - always better because it includes both as special cases - rather than to what I thought you might think.
Comment: @amoeba I've added a section to answer your question. I'd be interested in hearing your thoughts.
Comment: I would add that LASSO, ridge and elastic net are estimators, not models. Therefore, expressions such as *$L_1$ is a true model* do not make sense. This does not invalidate your general points, though.
Comment: @RichardHardy This is a fair point. I do want to rewrite this answer eventually, because I feel like I've learned a lot since... oh my, 2015!
Comment: @Sycorax, wow, I had not realized the age of the post and the answers either! I guess I have learned a bit over time, too, as I did not initially catch the problem with terminology.
Answer: I generally agree with the answer by @Sycorax, but I would like to add some qualification.
Saying that "elastic net is always preferred over lasso & ridge regression" may be a little too strong. In small or medium samples elastic net may not select pure LASSO or pure ridge solution even if the former or the latter is actually the relevant one. Given strong prior knowledge it could make sense to choose LASSO or ridge in place of elastic net. However, in absence of prior knowledge, elastic net should be the preferred solution.
Also, elastic net is computationally more expensive than LASSO or ridge as the relative weight of LASSO versus ridge has to be selected using cross validation. If a reasonable grid of alpha values is [0,1] with a step size of 0.1, that would mean elastic net is roughly 11 times as computationally expensive as LASSO or ridge. (Since LASSO and ridge do not have quite the same computational complexity, the result is just a rough guess.)

What sort of prior knowledge would lead one to prefer Lasso and what sort of prior knowledge would lead one to prefer ridge?

If it is plausible that all regressors are relevant, but they are highly correlated, then no variable selection is needed and thus ridge could be preferred. Some economic phenomena are like that. If, on the other hand, some of the regressors are likely to be completely irrelevant (but we just do not know which ones) then variable selection is needed and LASSO could be preferred. Gene expression problems are probably like that (but I am not a biologist, so please correct me if I am wrong). Generally, this knowledge would be taken from the subject-matter domain.
Comment: What sort of prior knowledge would lead one to prefer Lasso and what sort of prior knowledge would lead one to prefer ridge?
Comment: @amoeba, if it is plausible that all regressors are relevant, but they are highly correlated, then no variable selection is needed and thus ridge could be preferred. If, on the other hand, some of the regressors are likely to be completely irrelevant (but we just do not know which ones) then variable selection is needed and LASSO could be preferred. This knowledge would be taken from the subject-matter domain. I think there may be some examples in Hastie et al. "The Elements of Statistical Learning" or in the related literature, I just do not remember where I read that.
Comment: Or indeed LASSO or ridge regression may not give improved predictive performance over unpenalized regression.
Comment: @kjetilbhalvorsen, thank you, that was helpful.
Comment: @amoeba, ridge is better for correlated data, where L2 encourage many small weights (averaging) over the inputs..the classic example being repeated measurements with independent noise ( eg signal processing, or eg multiple exams of same subject), whereas l1 is better where 1 var dominates the other, classic case being hierarchical data: where coefficients should be estimated at highest level in hierarchy.
Comment: @Richard Hardy:  See my answer here:  https://stats.stackexchange.com/questions/264016/why-lasso-or-elasticnet-perform-better-than-ridge-when-the-features-are-correlat/264118#264118


Como descobrir o ID dos chats pela API do Telegram?

Question: Eu estou configurando a API de BOT do Telegram através do Guzzle. Eu estou tentando entender o que seria o parâmetro chat_id descrito na documentação do método sendMessage.
Tenho o seguinte código:
   $cli = new \GuzzleHttp\Client([
        'base_uri' => sprintf('https://api.telegram.org/bot%s/', static::TOKEN),
   ]);

   $cli->post('sendMessage', [
      'query' =>  [
          'chat_id' => $ID_DO_CHAT,
          'text' => $this->text,
          'parse_mode' => 'markdown'
       ]
   ]);

O que eu preciso para poder descobrir qual é o ID do chat?
Eu gostaria que o BOT enviasse mensagens para mim.
Answer: Para descobrir o ID do chat em questão, você precisa acessar o método getUpdates da API do telegram.
Usando o próprio Guzzle, ficaria assim:
$cli = new \GuzzleHttp\Client([
    'base_uri' => sprintf('https://api.telegram.org/bot%s/', static::TOKEN),
]);

$response = $cli->get('getUpdates');

print_r(json_decode((string) $response->getBody(), true));

O resultado será simliar a isso:
Array
(
    [ok] => 1
    [result] => Array
        (
            [0] => Array
                (
                    [update_id] => 26020178
                    [message] => Array
                        (
                            [message_id] => 113
                            [from] => Array
                                (
                                    [id] => 9999999999
                                    [is_bot] =>
                                    [first_name] => Nick
                                    [last_name] => Qualquer
                                    [username] => nickqualquer
                                    [language_code] => pt-br
                                )

                            [chat] => Array
                                (
                                    [id] => 9999999999
                                    [first_name] => Nick
                                    [last_name] => Qualquer
                                    [username] => nickqualquer
                                    [type] => private
                                )

                            [date] => 1536859308
                            [text] => UM TEXTO DE EXEMPLO
                            [entities] => Array
                                (
                                    [0] => Array
                                        (
                                            [offset] => 0
                                            [length] => 43
                                            [type] => url
                                        )

                                )

                        )

                )

        )

)

O lugar com o valor 9999999999 é o ID do chat e você deverá usar como parâmetro chat_id na chamada de sendMessage.
No meu caso, eu deixei o mesmo salvo num arquivo de configuração, já que o valor é constante.
Observação: Talvez seja necessário enviar uma mensagem para o BOT do seu usuário, pra você poder ver o ID do chat aparecendo na lista mostrada acima.