Equivalent chords

Question: I'm just starting to learn music theory and this is one of the (possibly silly) doubts I've come into.
Let's say we are in C major and we find this chord F-Ab-B-D. Well, it is just a Fdim seventh chord in root position, but we look at it and it's totally the same as a Bdim seventh chord in second inversion!
Are they the same? Is there a reason why it's just one of the two? Are they functionally equivalent chords, so to say?
Thank you =D
Comment: So aren't there (up to inversions) just **three** chords, namely Cdim7, C#dim7 and Ddim7?
Comment: This has been answered before somewhere on this site. The short answer is that these chords resolve differently (they have different uses) even though they have the same notes. A chord name is determined by its usage rather than by its notes. In a non-equal temperament; the two chords would be different. There are only four fully diminished seventh chords (in equal temperament); these are symmetric in consisting of three diminished thirds; any one of these can act as the root depending on the chord's use.
Answer: You've discovered the symmetry of dim7 chords. A diminished seventh chord is made of four stacked minor thirds. Each minor third is 3 semitones, and there are 12 semitones in an octave. Hence, they are symmetrical.
So, in fact, we can actually make four dim7s using your collection of four notes:
Fdim7 (F, A♭, B, E♭♭)
A♭dim7 (A♭, C♭, D, G♭♭)
Bdim7 (B, D, F, A♭)
Ddim7 (D, F, A♭, C♭)
Note that I've used some enharmonic equivalents (D = E♭♭, F = G♭♭, B = C♭). I'm sure you could also call it C♭dim7, E♭♭dim7 and G♭♭dim7, but that's probably going a bit far.
In fact, there are really only three unique dim7 chords. You could call them Cdim7, C♯dim7 and Ddim7,  but they have many possible names. We're talking about one of those three.
In any given key, you're most likely to run in to the dim7 chord that belongs to that key. If you're in C Major, it's probably a Bdim7. If you're in E♭ Major, it's probably a Ddim7. That being said, the note in the bass is also going to suggest the most likely option.
Comment: Isn't the second dim chord the same as the fourth? There are only three. I think there are four *augmenteds*.
Comment: More the voice leading than the note in the bass. Most of the time, the dim7 is going to act like a truncated dominant m9, which isn't always in root position either (although the ninth in the bass was long considered a no-no, at least until a few years after _Verklärte Nacht_). @Tim, yeah - 3 different diminished seventh chords, 4 different augmented triads. It's the number of notes that can fit within a minor third or major third (excluding the next chord tone, i.e., 3 and 4 respectively) __x__ the number of notes in the chord (4 and 3 respectively) = 12.
Comment: @Tim they are all the same chord. I'm not trying to illustrate the three unique dim7 chords; I'm trying to show that you can call that one specific chord by multiple names. I'll edit to make that clearer.
Answer: It's the nature of diminished chords, using the usual formula of 1-3-5-7. Start with a root, make the next note up a minor third. The 5th is then diminished, and the 7th also.So, using your case, root=F, min3=Ab, dim5=Cb and dim7=Ebb. Not really 'stacked minor 3rds', but effectively the same.
So, yes, you're correct. That's the same sounding chord as B dim - B, D, F and Ab. Slightly different names, technically, due to different roots, but effectively the same sounding.
As there are only 12 notes available in Western music, that means there are, in a way, three 'different' diminished 7ths, because, as you saw, by the time you move up chromatically through those three, the next will contain the same notes as the first, and so on. They usually get named from the lowest, or root note, but I've seen the same chord shown with two different names in the same piece, probably due to each needing, or being given, a different root note.
Comment: It's way too early to decide this answer is the one you like best. Other, better ones will probably follow!
Comment: Oh, sorry, I'm new here! By now this is the one I prefer, by the way =)

Equivalent chords


How to answer a request but continue processing code in WebApi

Question: I would like to answer a request, but continue processing code.
I tried something like:
[HttpPost]
public async Task<HttpResponseMessage> SendAsync(MyRequest sms)
{
    await Task.Run(() => Process(sms)); //need to run in a separate thread
    var response = new MyRequest(sms) { Ack = true };
    return Request.CreateResponse(HttpStatusCode.Created, response.ToString());
}

private async void Process(MyRequest sms)
{
    var validationResult = new MyRequestValidation(_p2pContext, _carrierService).Validate(sms);
    if (string.IsNullOrWhiteSpace(validationResult.Errors[0].PropertyName)) // Request not valid
        return;

    Message msg;

    if (validationResult.IsValid)
    {
        msg = await _messageService.ProcessAsync(sms);
    }
    else // Create message as finished
    {
        msg = _messageService.MessageFromMyRequest(sms,
                finished: true,
                withEventSource: validationResult.Errors[0].CustomState.ToString()
              );
    }

    // Salve in db
    _p2pContext.MessageRepository.Create(msg);
    _p2pContext.Save();
}

Comment: You could use AMQP/ rabbitmq or kafka messaging if you want to return a quick 202 response and continue execution of code
Answer:
I would like to answer a request, but continue processing code.

Are you sure you want to do this in ASP.NET? This is not a situation that ASP.NET (or any web server) was designed to handle.
The classic (correct) way to do this is to queue the work to a persistent queue and have a separate backend process do the actual processing of that work.
There are all kinds of dangers with doing processing inside of ASP.NET outside of a request context. In general, you can't assume that the work will ever actually be done. If you're OK with that (or just like to live dangerously), then you can use HostingEnvironment.QueueBackgroundWorkItem.
I have a blog post that goes into more detail.
Comment: Excellent post! I chose to use the Hangfire. Thanks.
Comment: Believe me, it's not how I would do it!  I'll read your post.


What can I do with these two time series?

Question:
Info:
Blue line is the official inflation statistics by US Bureau of Labour Stats
Red line is by independent researchers who claim to have created better way to measure inflation
Blue line has been sampled once every month
Red line has been sampled daily
It can be seen that the red line is tracking inflation ahead of the blue line.

Basically these are two time series, with different sampling frequencies, that are tracking the same phenomenon, i.e. change in general price levels. I need advice on how I should go about analysing these processes. What are some possible hypothesis I could be testing for?
UPDATE: After mpiktas' suggestion, my current plan is to a MIDAS regression with some polynomial function for the weights and report the forecasting capabilities of the model. (and perhaps contrast this to a model using a simple-time-averaged red line)
Some of you might have done something similar to this, is there anything I should keep in mind or anything additional I could be doing that would complement this 6000 word project?
Additional:

If you can link me to some very relevant papers that have done similar analysis that would help.

Any input will be apreciated
Comment: Economists have traditionally used low frequency data (like monthly or quarterly). There has been a lot of work on developing higher frequency data to improve forecasts. So you might look into mixed frequency techniques (Midas or Kalman Filters might be a start).
Comment: Before asking the data anything, make sure that you study and fully understand what does it mean, and what does it take, to try to compare/examine/contrast data of different frequency.
Comment: Yes, this a natural start. You might also want to use rolling forecasts and the forecasting model $Y_{t+h}=\sum_{j=0}^kX_{tm-h}+\varepsilon_{t+h}$, i.e. forecast the t+h observation using the data until $t$, this is how usually MIDAS regression is used. For example you can forecast the current monthly value using half-month data of high frequency regressor.
Comment: Running tests without some goal is usually meaningless. What are you trying to achieve? To what end will you use this data?
Comment: @mpiktas I was thinking about showing that it is possible to use the red line as a predictor for the blue line. This is important because a lot of policy decisions depend on the blue line and if I can show that the red line forecasts the blue line, then there would be some real world relevance to this analysis. I also have a feeling that it would be overkill to do tests or analysis to show that the red line is a predictor, its already really obvious from the plot. What are your thoughts? Im struggling to find a decent project idea for my course.
Comment: Then use MIDAS regression, which is specifically designed to regress low frequency time series on high frequency time series. There is an R package for doing that, http://cran.r-project.org/web/packages/midasr/. That package will enable to quickly test out whether it is possible to predict blue line with red line. The package comes with the user guide, with the examples, try following them. Note, I am the developer of this package, so I am a little biased in giving suggestions.
Comment: Also please update the question with the info that you want show that blue line can be predicted by red line. This will make question more answerable in my opinion.
Comment: @mpiktas hey, i've updated the question. thanks for the r package link, it looks excellent for this question and im going to start studying it. I was thinking of seperating my data into 70% training and 30% cv, and testing to see how accurate the predictive nature of the red line is. do you think thats a good idea?
Answer: For forecasting low frequency variables with high frequency you can use MIDAS regression. The idea behind this regression is quite simple, average the high-frequency variable and then use it as a regressor. The key is to use custom weights. Suppose we have $Y_t$ which is sampled monthly and $X_\tau$, which is sampled daily. Then MIDAS regression is defined as follows:
$$Y_t=\sum_{h=0}^k\beta_hX_{tm-h}+\varepsilon_t$$
where we assume that for observation $t=s$ we have $m$ observations $\tau=sm-m+1,...,sm$. We also assume that $\beta_h=g(h,\theta)$, for some function $g$ and hyperparameter $\theta$.
So if you want to test out whether the $X_\tau$ is a good predictor for $Y_t$, fit a MIDAS regression with different weight functions and inspect the results.
If you enter MIDAS regression into google you'll find many articles. Forecasting monthly CPI with daily variable was investigated in the article "Forecasting with mixed frequencies" by Armesto, Engemann and Owyang.
The MIDAS regression idea was introduced by Eric Ghysels, you can look into his articles.
There are two software packages for fitting MIDAS regression: MIDAS Matlab toolbox and midasr R package. They both have user guides, where you can find more detailed examples and links to other literature.
Note, this is only one possible way of solving your problem. Others surely exist too, but as I am the developer of midasr R package, I am biased in my suggestions.
Comment: You must make all the months to have an equal number of days. And then set frequency to that number. You can achieve that by discarding data, padding NAs or extrapolating. If you use lags smaller than 28, padding NAs is the easiest solution.
Comment: Hey mpiktas, I converted my daily data into a zoo object(it seems zoo is easier to work with for daily), and my dependent variable is a monthly variable which I converted into a ts object. during estimation, (i.e. midas_r(yy ~ mls(yy,1,1) + mls(xx,1:3,30)), what should i set the frequency for the xx as?   Is there a better way to deal with monthly data regressed on daily data?
Comment: Thanks for the reply mpiktas, could I please direct you to this related question of mine, http://stackoverflow.com/questions/22572162/strange-situation-with-dates-in-r
Answer: For starters, I would check if the draws from list A are two standard deviations outside of the draws from list B.
Secondly, I would check if a 1 month moving average of list A is significantly different from the more slowly sampled list B.
Answer: Fit seasonal time series models to both and then compare the seasonality.
The TBATS model can handle daily data. It is available in the forecast package in R.


Problema com array em javascript

Question: Estou tentando usar o conteúdo de uma variável como nome de um Array, mas está retornando uma letra do conteúdo dessa variável.
Exemplo:
var posicaoArray = 2;
var nomeArray = "frutas";
var frutas = new Array();
frutas [0] = "Banana";
frutas [1] = "Melancia";
frutas [2] = "Maçã";
frutas [3] = "Laranja";
document.write(nomeArray[posicaoArray]);

Irá retornar a letra " u ", letra número 2 da palavra "frutas" se contarmos a partir de 0, ao em vez de aparecer "maçã".
Porque acontece isso?
Comment: Isso está no escopo global? Se estiver, pode usar `window[nomeArray][posicaoArray]`.
Comment: Nesse caso, acredito que para fins de estudo, não tem problema uso do `eval()`, mas é importante a leitura de vários artigos sobre ele (inclusive li vários agora há pouco), para que nunca o use de forma errada ou de formas que comprometam sua aplicação.
Comment: Testei aqui e funcionou: `console.log(eval(nomeArray)[posicaoArray]);`. Desconheço se é a melhor maneira, mas é uma alternativa.
Comment: @RafaelWithoeft funcionou! muito obrigado,ajudou bastante.
Answer: Isso que você está tentando fazer está errado, o resultado apresentado é totalmente previsível. Imagine a seguinte variável:
var palavra = "carro";

Em JavaScript, tudo é um objeto, logo, podemos pegar partes da palavra normalmente usando um array. Para pegar a letra "c", usamos o index 0, já que é o primeiro. Exemplo:

var palavra = "carro";
document.body.innerHTML += palavra[0];

Ou seja, você está pegando posições da sua string, e não do seu array. Você pode guardar tudo em um array multidimensional. O primeiro index será o nome da sua variável, e os segundo index será uma cópia do outro array. Exemplo:

var posicaoArray = 2;
var nomeArray = "frutas";
var frutas = new Array();
frutas [0] = "Banana";
frutas [1] = "Melancia";
frutas [2] = "Maçã";
frutas [3] = "Laranja";

var data = {};
data[nomeArray] = frutas;

document.body.innerHTML += data[nomeArray][posicaoArray];

Problema com array em javascript


Textarea with padding vertical-align top next to label

Question: When I add padding to my textarea then vertical-align top does not work.
Browser: Firefox
CSS:
.form-aligned textarea-label {
    vertical-align: top;
    display: inline-block;
    width: 120px;
    text-align: left;
}

textarea {
    padding: 6px 12px;
}

I am looking for a result like this but with padding added to textarea:

It drives me crazy it should be simple :-)
Comment: Provide HTML, I can't reproduce the issue [fiddle](https://jsfiddle.net/8cz2epwc/)
Comment: why dont use margin.
or you can use padding-left or right.
Comment: Is margin better to use for this situation?
Answer: You need to use divs to separate the label from the textarea.

Incident:

<textarea id="txtIncident"></textarea>

label {
    vertical-align: top;
    width: 120px;
    text-align: left;
}

textarea {
    padding: 6px 12px;
}

https://jsfiddle.net/7z0uwtLu/
Comment: Thanks for this quick response, i found out if i remove rows and cols attribute from my textarea then it also works without divs. My mistake I forgot to mention this in the css code :-)


How to sample from a reconstructed pdf

Question: Suppose, that we have a process that generates events. Subsequently these events are recorded and stored in a histogram. Say we have a source that emits particles and subsequently a histogram is build based on each individual particle's energy. Suppose further, that the bins of the histogram have varying widths, some being wider and some narrower. Following this process one can construct a spectral density $\phi(e_i)$ by counting how many times a certain energy falls within a particular histogram energy interval corresponding to a bin
$e_{i-1},e_i$ with a width
$$
\Delta_i=e_{i}-e_{i-1}.
$$
This histogram represents a discrete pdf. A discrete pdf with bins of uniform width can be sampled by inverting the cumulative cdf given by
$$
F_E(e)=\sum_{e_i\le e} \phi(e_i),
$$
with the associated inverse cdf
$$
F^{-1}(x)=\inf\{e:F(e)\ge x\}
$$
The question is how to sample if the histogram's bins have varying widths. Namely, since the widths $\Delta_i$ of the bins are non uniform, should one sample from
$$
\phi(e_i) \text{ or from } \frac{\phi(e_i)}{\Delta_i}.
$$
With the associated pdfs
$$
F_E(e)=\sum_{e_i\le e} \phi(e_i) \text{ or } F_E(e)=\sum_{e_i\le e} \frac{\phi(e_i)}{\Delta_i}.
$$
I would appreciate your help.
Answer: Use the distribution function $F(x)$ directly.  Generate a random number $R$ and find the interval where $F(e_{i-1})\lt R\le F(e_i)$.
Comment: It looks like we're OK.  It seems that the problem was in the definition of histogram.  The division by the bin width seems to lead to confusion.
Comment: That is correct, the question is how to compute the cdf. Which one of the two methods as given in  the question.
Comment: Use $F(e)=\sum_{e_i\le e}\phi(e_i)$.  Equal spacing is irrelevant.
Comment: I don't understand this, could you please elaborate.
Comment: I am a little confused about your definition of $\phi$.  What I have in mind is first get the total number of events.  Next divide the number in each interval by the total, call that $f((e_i)$ for the interval.  Then $F(e)=\sum_{e_i\le e}f(e_i)$.
Comment: Consider the histogram to be an energy spectrum. The tricky part is that the bins of the histogram are with different widths. Since pdf is probability per unit mass, I guess that i would have to divide by the width of the bins.
Comment: To get the pdf, you first normalize the histogram by dividing the number of events is each bin by the total number of events.  This results in the total probability $f(e_i)$ for the interval.  To get the pdf you want you divide each $f(e_i)$ by the length of the interval.  To clarify our confusion, you need to describe exactly the mathematics to define $\phi(e_i)$.  Is it the fraction of events in the interval or something else?
Comment: $\phi(e_i)$ is the fraction of events. If all slots/bins are equal width then it does not matter. The question only arises when the bins are non-equal length. A narrow bin would get less events than a wide one. How to take that fact into account when sampling the empirical pdf?
Comment: I think I finally understand your problem.  The pdf is as you described $\frac{\phi(e_i)}{\Delta_i}$.  However when integrating the pdf to get the cdf you must take into account the length of the interval, so the summation terms are $\frac{\phi(e_i)}{\Delta_i}\times \Delta_i=\phi(e_i)$.
Comment: If you remember from elementary calculus, the integral of a constant (c) over an interval [a,b] is c(b-a).
Comment: The integral is not important here. The question is, whether I should divide by the bin width. I am accumulating a histogram with different width bins, from which I would like to draw samples. The answer to the question is NO and I checked it numerically. Therefore we are on a different tune.
Comment: In order to draw a sample you need to first get the bin from which to sample.  To do that you need to use the pdf , using the random number as I described.  Once you get a bin, sample from the bin by assuming uniform within the bin.
Comment: Well this i know already and is is also what i wrote in my question. The problem was whether or whether not i have to divide by the bin widths to "normalize the pdf". The answer is no. The cdf is obtained just by summing the "raw" histogram values. Checked that one numerically
Comment: Clarification:  The pdf is defined by dividing the histogram by the bin width.  To get the cdf you multiply the pdf by the bin width (getting the histogram back).  Your approach is correct.  In essence the histogram for each bin is the integral of the pdf over that bin.
Comment: I hope so :). At least the numerical example proved the point. My confusion comes from the fact that in physics energy plots are normalized to the bin width. If i would do that to the histogram, however, the results are wrong.

How to sample from a reconstructed pdf


Topology Theorem on closure of a set

Question: If A is a subset of a metric space X ,then A closure is a closed set and is a subset of every closed set containing A
Comment: What is your question? What is your definition of closure?
Comment: A common way to prove two sets are equal is to prove each is contained in the other.  Have you tried that?  What have you tried?
Comment: A closure is union of set A and derived set of A
Comment: Henno Bradsma What does A" and A' mean
Answer: In a metric space, for any subset $A$ we have $A'' \subseteq A'$ (in general spaces this might fail, but it holds in $T_1$ spaces), and a set $B$ is closed iff $B' \subseteq B$ (take this as the definition if you will).
Now defining $\operatorname{cl}{A}=A \cup A'$, as per the comments, we see
that $$(\operatorname{cl}{A})' = (A \cup A')' = A' \cup A'' \subseteq A' \cup A' = A' \subseteq \operatorname{cl}{A}$$
so that $\operatorname{cl}{A}$ is closed, and if $C$ is closed and $A \subseteq C$ we have $A' \subseteq C'$ and so
$$\operatorname{cl}{A} = A \cup A' \subseteq C \cup C' \subseteq C$$ as $C' \subseteq C$ because $C$ is closed, proving the final claim.
Comment: @user11870400 $A'$ is the derived set of $A$ and *its* derived set is $A''$.
Comment: Thanku you very much
Comment: What is T1 spaces
Comment: Proof of A' subset of C'
Comment: @user11870400 if $A$ is a subset of $B$, the same relation holds for their derived sets. This is standard. $T_1$ spaces are not needed if you're working in metric spaces.
Comment: Does A" subset of A' holds for metric space
Comment: @user11870400 yes, it does. Also standard.

Topology Theorem on closure of a set


Formatar resultado com campo concatenado

Question: Preciso trazer de meu banco de dados um TIMEDIFF com campo formatado assim: 12:50 (horas e minutos). Utilizo o seguinte trecho:
CONCAT(HOUR(TIMEDIFF(Campo.expired,NOW())),":",MINUTE(TIMEDIFF(Campo.expired,NOW()))) AS expired

Meu problema está no fato de que quando o horário é de apenas 1 dígito, o resultado fica assim: 2:4 (2 horas e 40 minutos). Preciso exibir esta data corretamente, ou seja, 02:40.
Já tentei usar o DATE_FORMAT dentro do CONCAT, sem sucesso.
Comment: A coluna `expired` é de qual tipo? Como você tentou usar o `DATE_FORMAT`?
Answer: Você não precisa usar CONCAT pra fazer isso.
Pode ser feito assim:
TIME_FORMAT(SEC_TO_TIME(TIMESTAMPDIFF(SECOND, Campo.expired, NOW())), '%H:%i')

Isso vai calcular a diferença entre as datas em segundos, transformar os segundos em TIME e usar TIME_FORMAT para formatar do modo que você quer (ex. 02:40)


USB MIDI connection lost when restarting the keyboard

Question:
Cubase 5.5.2  with Synthogy Ivory
Windows 7
Standard USB MIDI drivers from Microsoft

I connect a Kawai VPC-1 Keyboard via USB (=USB-MIDI).
Problem: Everytime the keyboard is powered off and on, I am losing the connection to the USB MIDI. When Cubase is restarted, it works.
Can this be solved?
I guess that the USB driver is generating a new device everytime the keyboard is powered on, and cubase does not get this correctly.
Answer: Your guess is true. In the Good Old Times™, when USB did not yet exist, MIDI ports were fixed hardware devices that stayed there even if the cable was unplugged. So the Windows MIDI API has no easy way to report changes.
You sequencer might re-read the device list when you open its configuration dialog to select another port. But if not, then the only time when the software enumerates devices is when it starts.
If your keyboard has a 'real' MIDI output, you could connect that to a separate (USB) MIDI interface that always stays on.
Comment: Thanks, I understand now how it works. In fact I would need an USB driver which stays "stable". I tried the genuine KAWAI USB driver, but this is not better. I went back to MIDI and it works. Sorry, can't upvote, because I have < 15 points.
Answer: This is standard behaviour in music applications.  You can't 'hotplug' a USB device.  Have everything connected and powered up before starting the program.
Don't be surprised if a USB device loses contact after the computer goes into a power-saving standby mode either.
The solution to both these issues is simply 'Don't do that, then!'
Comment: Thanks! I went now back to MIDI and it works. I understand now. In fact you need an USB driver which stays "stable". I tried the genuine KAWAI USB driver, but this is not better. Sorry, can't upvote, because I have < 15 points.
Comment: BTW: It's interesting that the sound is now different and to me even better and more realistic, although the sound is produced by Ivory software synth. So the MIDI data seems to be different !?
Comment: Is it really a problem?   Before starting to work in Cubase, turn on your equipment.  All of it!  What was the old setup? The one where Ivory sounded different?
Comment: No, itr's not a problem; I'Ve found that


Split costs and one pay $X%$ more?

Question: <code>If combined costs are 23000 and you where to split it in two
and then one should pay 18% more, how do you count that?

23 000 / 2 = 11 500
11 500 * 0.82 = 9 430
11 500 - 9 430 = 2 070
11 500 + 2 070 = 13 570

so its 13 570 for one to pay and 9 430 for the other? , isnt that 30% more?

( (13 570-9 430) / 13 570) = 0.31 = 31% ?

-----------------------------------------------

Is this calc wrong?

218 = 100 + 100 + X% (X = 18)

23 000 / 218 = 105.5

105.5 * 100 = 10 550
105.5 * 118 = 12 449.5

so one pay 10 550 and the other 12 449.5?
</code>
Comment: The wording in the question is a bit unclear, but I took a stab at it. Let me know if I understood the question. Also, where are the 0.83 and 0.17 coming from? Is the problem asking for 18% or 17%?
Comment: yes, typo, fixed the example to have 18% instead, :D
Answer: Let me see if I understand the question. See below...

We have $23,000. It must be split among two individual such that one person pays 18% more than the other person. What does each person pay?

Let $x$ represent the smaller amount that one individual pays. Then the other individual, who pays $18\%$ more must pay $x+0.18x = 1.18x$.
Together, they must have $23,000, so
$$x + 1.18x = 23000$$
$$2.18x = 23000$$
$$x \approx \$10550.46$$
That's the amount one pays. The other pays
$$1.18x \approx \$12449.54$$
Comment: Yes, an amount of money split in two and one should pay 18% more, so my second calculation (despite some typos) was most correct it seems.

Split costs and one pay $X%$ more?


What is the variance of an estimator in terms of central moments

Question: I've been asked to define an estimator, $\Theta_c$ for $\sigma^2$, where $c>0$ is a constant.
$\Theta_c = c \sum_{i=1}^n(X_i-\bar{X})^2$
The question is to calculate Var($\Theta_c$) in terms of the central moments $\mu_k'=E[(X_1-\mu)^k],k>0$ and find the value of $c$ that minimizes Var($\Theta_c$). I'm having difficulty expressing the variance in terms of the central moments. I think I'm on the right path, I've rewritten in terms of $Z_i = X_i - \mu$ to eliminate the off center mean. Then $\Theta_c = c\sum_{i=1}^n(X_i-\bar{Z})^2$ and the $k$th moment of $Z_i$ will be the $k$th central moment of $X_i$, but I can't figure out how to use this to express the variance in terms of the central moments.
Comment: Not understanding minimizing the variance over $c.$ As $c\to 0$ the variance of the estimator (and the estimator itself) goes to $0.$  Maybe they mean minimize the mean-squared error?
Comment: Yes, I'm trying to minimize the MSE in the next part, any idea how to do this?
Answer: The trick is to write $$ \Theta_c = c\sum_k((X_k-\mu)+(\mu-\bar X))^2\\=c\sum_k((X_k-\bar X)^2-2(X_k-\mu)(\bar X-\mu)+(\bar X-\mu)^2)\\=c\left(\left(\sum_k (X_k-\mu)^2\right)-2n(\bar X-\mu)(\bar X-\mu)+n(\bar X-\mu)^2\right)\\ =c\left(\left(\sum_k (X_k-\mu)^2\right)-n(\bar X-\mu)^2\right).$$
We would now like to take the expectation. Since the variables are i.i.d. (I assume) the expectation of the first term in parentheses is easy: it's just $n\sigma^2.$ The second term is easy when you remember that the variance of the sum is the sum of the variance. I'll do it out the long way though, to prepare for later in the calculation. We have $$(\bar X-\mu)^2 = \frac{1}{n^2}\left(\sum_{k}(X_k-\mu)\right)^2 = \frac{1}{n^2}\sum_{k,l} (X_k-\mu)(X_l-\mu) \\= \frac{1}{n^2}\left(\sum_k(X_k-\mu)^2 + \sum_{k\ne l}(X_k-\mu)(X_l-\mu)\right).$$
When we take the expected value, all the terms in the second sum are zero cause they're the product of independent, zero mean variables, so we have $$ E((\bar X-\mu)^2) = \frac{1}{n}\sigma^2.$$
Putting it all together, $$ E(\Theta_c) = c(n-1)\sigma^2.$$
To get the variance, we need to square $\Theta_c,$ giving $$ \Theta_c^2 = c^2\left(\left(\sum_k (X_k-\mu)^2\right)^2 - 2n(\bar X-\mu)^2\sum_k (X_k-\mu)^2 + n^2(\bar X-\mu)^4 \right)$$
We can consider each of the terms. For the first, $$ \left(\sum_k (X_k-\mu)^2\right)^2 = \sum_{k,l}(X_k-\mu)^2(X_l-\mu)^2 = \sum_k(X_k-\mu)^4 + \sum_{k\ne l}(X_k-\mu)^2(X_l-\mu)^2.$$
Taking the expected value and seeing that there are $n$ terms in the first sum and $n(n-1)$ in the second gives $$E\left[\left(\sum_k (X_k-\mu)^2\right)^2\right]  = n\mu'_4 + n(n-1)\sigma^4.$$
The next term is $$ (\bar X-\mu)^2\sum_k (X_k-\mu)^2 = \frac{1}{n^2}\sum_{k,l,m}(X_k-\mu)(X_l-\mu)(X_m-\mu)^2 \\ = \frac{1}{n^2}\left(\sum_{k,m}(X_k-\mu)^2(X_m-\mu)^2 + \sum_{(k\ne l), m} (X_k-\mu)(X_l-\mu)(X_m-\mu)^2\right).$$
Again, when we take the expected value, the second term vanishes. The first term we recognize from before. So we get $$ E\left[ (\bar X-\mu)^2\sum_k (X_k-\mu)^2 \right]= \frac{1}{n}\mu'_4 + \frac{n-1}{n}\sigma^4$$
Finally, the third term is $$ (\bar X-\mu)^4 = \frac{1}{n^4} \sum_{k,l,m,n}(X_k-\mu)(X_l-\mu)(X_m-\mu)(X_n-\mu) \\ =\frac{1}{n^4}\left( \sum_k(X_k-\mu)^4+ 3\sum_{k\ne l} (X_k-\mu)^2(X_l-\mu)^2 + \mbox{ other terms}\right)$$ where the 'other terms' are going to have zero expectation. The factor of $3$ is from the three different ways of choosing which terms to pair. We take the expectation to get $$ E\left[(\bar X-\mu)^4\right] = \frac{1}{n^3}\mu'_4+3\frac{n-1}{n^3}\sigma^4.$$
Putting the three terms together gives $$ E(\Theta_c^2) = c^2\left(\left(n-2+\frac{1}{n}\right)\mu'_4 + \left(n-2+\frac{3}{n}\right)(n-1)\sigma^4\right)$$ and we can find the variance is $$ \mathrm{Var}(\Theta_c) = E(\Theta_c^2)-E(\Theta_c)^2 =c^2\left(\left(n-2+\frac{1}{n}\right)\mu'_4  - \left(1-\frac{3}{n}\right)(n-1)\sigma^4\right).$$
Of course, as I mentioned in a comment, to minimize the variance of the estimator, take $c\to 0.$ Of course even though it's low variance, this will not be a good estimator since it's high bias.
A better thing to minimize is the mean-squared error, $$ E((\Theta_c-\sigma^2)^2).$$ To compute the mean-squared error, it's customary to write $$ E[(\Theta_c-\sigma^2)^2] = E[((\Theta_c-E(\Theta_c))-(\sigma^2-E(\Theta_c)))^2] \\ = E[(\Theta_c-E(\Theta_c))^2] +E[\Theta_c-E(\Theta_c)](E(\Theta_c)-\sigma^2) + (E(\Theta_c)-\sigma^2)^2 \\= \mathrm{Var}(\Theta_c) + (E(\Theta_c)-\sigma^2)^2 $$ so that it is seen that it is composed of a variance term and a bias-sqaured term.
Plugging in what we've computed, $$ E[(\Theta_c-\sigma^2)^2]=c^2\left(\left(n-2+\frac{1}{n}\right)\mu'_4  - \left(1-\frac{3}{n}\right)(n-1)\sigma^4\right) + ((c(n-1)-1)\sigma^2 )^2 \\ = c^2\left(\left(n-2+\frac{1}{n}\right)\mu'_4  + \left(n-2+\frac{3}{n}\right)(n-1)\sigma^4\right) -2c(n-1)\sigma^4+\sigma^4.$$
Minimizing over c gives $$ c_m = \frac{(n-1)\sigma^4}{\left(n-2+\frac{1}{n}\right)\mu'_4  + \left(n-2+\frac{3}{n}\right)(n-1)\sigma^4}.$$
For a Gaussian (i.e. with $\mu'_4 = 3\sigma^4$) this gives a minimum mean-squared error variance estimator of $$\frac{1}{n+1} \sum_k(X_k-\bar X)^2.$$
(This isn't necessarily the minimum MSE estimator overall, just the minimum of estimators that are proportional to the sample variance. Notice that it's smaller than the unbiased estimator which has coefficient $c=\frac{1}{n-1}$.)


Como eu posso melhorar este loop que tem um foreach dentro de outro?

Question: Estou fazendo este código que tem 2 foreach's e carrega 350 imóveis com mais ou menos 3500 links de imagens, na verdade não chamei as fotos, só os links, isso tudo carrega em meio segundo.
A variável $array armazena conteúdo de um XML. Dentro desse XML tem várias informações inclusive arrays de informações como Media que armazena todas as imagens do imóvel.
No caso abaixo, o segundo array pega $imovel->Media->Item e faz uma listagem das fotos que estão no imóvel.
<?php

$xml = simplexml_load_file("xml_vivareal.xml") or die("Error: Cannot create object");
$array = $xml->Listings->Listing;
$number = 1;

foreach ($array as $imovel) {
    echo $number++ ." - ". $imovel->ListingID . "
";
    echo $imovel->Title . "
";

    foreach ($imovel->Media->Item as $fotos) {
        echo $fotos . "
";
    }

    echo "
";
}

Resultado produzido:
Comment: acho que não pois suas imagens trazem  registros múltiplos de outro objeto
Comment: @MarcosBrinner então qier dizer que os dois foreachs dessa forma estão corretos.
Comment: [Veja isso.](http://stackoverflow.com/questions/4480803/two-arrays-in-foreach-loop)
Comment: Não há problema em usar um loop dentro de outro. Uma coisa que vc pode fazer pra melhorar **a entrega dos dados** é criar um buffer (`ob_start()`/`ob_end_flush()`) pra entregar tudo de uma única vez pro navegador. Talvez "aparente mais desempenho".
@EmersonBarcellos acho que não é válido pra esse caso, uma vez que `$imovel->Media->Item` é da `array` do primeiro loop.
Answer: Uma maneira alternativa é armazenando o conteúdo HTML e imprimir (echo) tudo no final, podendo assim usar a função implode no lugar do foreach interno.
Veja o código abaixo:
<?php
$xml = simplexml_load_file("xml_vivareal.xml") or die("Error: Cannot create object");
$array = $xml->Listings->Listing;
$number = 1;
$html = '';

foreach ($array as $imovel) {
    $html .= $number++ ." - ". $imovel->ListingID . "
";
    $html .= $imovel->Title . "
";

    $html .= implode("
", $imovel->Media->Item);

    $html .= "
";
}

echo $html;

?>

Acredito que dessa maneira o código fique mais limpo e elegante. Com relação à performance, seria bom que você fizesse um teste nas duas versões e postasse o resultado.


Is it safe to use guitar polish everywhere on the guitar?

Question: I have owned my first electric guitar for half a year and have only been cleaning it with a damp cloth (without any cleaning agent). Since I'm going to change strings, I think it might be a good time to clean everything up.
I watched a video about how to clean a guitar. He used a guitar polish everywhere on his guitar. Is it safe to do so? Does it depend on the finish of the guitar? Do I need to pay attention to the ingredient of the polish?
Sorry I'm throwing so many questions at once, but it's my first guitar so I'm a bit nervous :)
Answer: There are essentially two types of finishes that are used on guitars these days: Nitrocellulose and Polyurethane. There are others, such as waterborne finishes, but I'm not going to cover them here since they aren't very common on mainstream instruments. Nitro is a relic from the old days--but a couple of larger manufacturers such as Gibson and Fender still use them on some of their vintage reissue guitars. Nitro is very picky and doesn't age well. Poly is poly :D--if you've ever finished your deck then you've probably used it and seen how durable it can be. Polyurethane is very durable and you can clean it with slightly more abrasive chemicals--although I don't recommend it.
If you want the most out of your finish--whether polyurethane or the finicky nitrocellulose--then you should definitely pay attention to what's in your guitar polish. I personally don't use polish frequently at all. Instead I use some alcohol and a little distilled water on a damp cloth to clean the sweat and grime off my instrument. Then, whenever I do a complete ground-up restring I will wipe the body and fretboard down really well, then shine the body up a little with some polish and finish with a healthy dosage of mineral oil on the fretboard.
Your fretboard, depending on what woods it is constructed out of, should be paid special attention to. Rosewood, for example, is a very resilient wood but it can greatly benefit from an occasional rubdown with some mineral oil. I use Gerlitz Guitar Honey with much success. Mineral oil will bring out some beautiful colors in Brazilian Rosewood and other unfinished fretboard woods. A finished fretboard can be treated exactly like the guitar's body. To clean a fretboard, you can use naptha, or common lighter fluid, to cut through the gunk and grease or one of the many other techniques described by answers to this question.
In summary, be careful what you put on the body of your guitar. Stay away from very abrasive petroleum based chemicals, and certainly anything too basic or acidic. You shouldn't need a chemistry degree to understand what a polish will do to your instrument, but you will need to understand the general ideas I have tried to outline here.
Comment: "Nitro is very picky and doesn't age well" It ages fine if reasonable care is taken with the finish. I sold my '78 Les Paul Custom last year that'd I had since it was new and it looked the same as it did when I bought it. My '68 Martin D-35 looks very nice even though it's obvious the finish has become thinner as its aged - the grain is more obvious underneath the finish now. Periodic cleaning with good polish and keeping it out of extreme heat and cold goes a long way toward preserving a finish.
Answer: It sure depends on the finish of the guitar.
For example, I have a Fender Stratocaster with a maple fretboard and I use Mr. C's Spray Detailer. And I use it everywhere in this guitar.
But, when it comes to my Acoustic guitar which has a rosewood fretboard, I use Gibson Luthier's Choice Fretboard Conditioner.
These are just examples of course.


C6 chord followed by Am7

Question: C6 chord over D and 3 C notes in right hand, then Am7.  The chords contain the same notes. The proximity to the right hand doesn't allow for rearrangement of chord notes.  Why does the author not just repeat C6?  How is this intended to be played? What is the best transition?
Comment: This is in measure 7 of "What's New" song, from Real Jazz book.
Answer: The bass part probably changes, maybe from a C to an A. I agree, the difference is subtle, and hardly worthwhile, especially if the rest of the chord voicing is static.A copy of the sample would give a better clue.
Comment: I am new here. How do I include a copy?
Comment: Sorry, I'm too old to know...  I'll have to dig out my fake books.
Comment: That's fine.  If you have the large Real Jazz book, my question refers to p. 402, "What's New?" Measure 7.  Thank you -  your comment above is helpful. However, I think it sounds better to just repeat C6.  I personally don't like going lower, more bass, with the chord.
Answer: That's just for root movement. So, measures 7 ad 8 are:
C6  Am7 Dm7  G7

The Am7 leads to the Dm7 much more naturally that C6 -> Dm7 would.
Also, right before the C6 in measure 7 is a G7 so that resolving to C6 instead of Am7 makes a lot of sense as well.
Remember, the strongest root movement in western music is V -> I. A good first place to start when figuring out why a certain progression is the way it is, is to look at root movements between chords.
Comment: Sorry, I did not specify, I needing piano chord arrangements.
Comment: I should have, first said, thank you.  Your answer is helping me to realize that I need to back off from specifics and study general chord progression concepts.  My issue is that, I rarely play chords in root position, at the piano.  I alter them for ease of fingering transitions from chord to chord. With the proximity of the right hand, in the above example, I am inclined to play both chords the same way.  I am just learning how to interpret fake music notation.  Are the chords meant to be played as written, I.e. root position? Or do I have the liberty to alter them as I see fit?
Comment: The bass note matters, whether you or another instrument is playing it.  The inversion or voicing of the upper notes doesn't (well it DOES, but the chord symbol doesn't define it).   C, C/E, F  is a quite different effect to C, C, F.    Changing the bass note from C to A gives C6 a very different flavour and function.

C6 chord followed by Am7


How to show the values on the bars of a stacked bar graph in 3D?

Question: I have created a Stacked Bar Graph (in 3D) using the Forena reports module, which looks like so:

I was able to successfully create this graph. But I am having problems in showing the corresponding values on the bars. The values I want to show on the graph are:

colombia_blue:0.75
colombia_red:0.25
LATAM_blue:0.57
LATAM_red:0.43

I searched for the frx attributes. I found attributes to change the color of these 2 graphs, but not the label.
Here is the relevant part of my report specification (frx file):
Answer: Option 1 - Try another chart type
I recommend you experiment a bit with some of Forena's other Supported chart types, as documented within the "FrxSvgGraph" section of the "Custom Renderers"chapter (located with /reports/help.renderers#frxsvggraph of your site, and similar to this online equivalent).
More specifically, you seem to be using frx:type="StackedBar3DGraph". I think you will get way better results (to show the corresponding values) if you try with some of the other chart types, especially these ones:

MultiLineGraph
GroupedBarGraph
GroupedCylinderGraph

Option 2 - Add specific SVG Graph settings
If using another chart type is not what you want (= it has to be a Stacked Bar Graph in 3D), then you need to find the appropriate SVG Graph setting(s) (there are dozens, if not hundreds, of them ...) and pass such setting(s) as extra FRX attributes.
In that case, you may want to use some of the SVG Graph general settings. I haven't experimented with them, but I think you may want to try with these settings:

show_data_labels, which is a boolean (default = false), to "display labels for bars, markers or pie slices". In Forena, if you want to set this option to true, you'd specify it in your frx file like frx:show_data_labels="yes".
any of the settings starting with data_labels_... (there are dozens of them ...), to tune the data labels, like their fonts, positioning, etc.

Note: SVGGraph data labels explains some of the options related to labels, and how to use them.


pyspark add new row to dataframe

Question: I am trying to add a new row to dataframe but cant.
my code:
newRow = Row(id='ID123')
newDF= df.insertInto(newRow)
 or
newDF= df.union(newRow)

errors:
AttributeError: _jdf

AttributeError: 'DataFrame' object has no attribute 'insertInto'

Comment: This might be something you are looking for. Try `from pyspark.sql import Row`, create a dictionary and then update the dictionary. https://stackoverflow.com/questions/39801691/pyspark-add-a-new-field-to-a-data-frame-row-element
Answer: Simple way to add row in dataframe using pyspark
newRow = spark.createDataFrame([(15,'Alk','Dhl')])
df = df.union(newRow)
df.show()

Answer: Try: (Documentation)
from pyspark.sql import Row
newDf = sc.parallelize([Row(id='ID123')]).toDF()
newDF.show()

Comment: I'm confused. Where is the original df in this response? Not seeing how this answers the original question.
Comment: it creating newDF rather than adding new
Comment: This is not a helpful answer. There is no indication that a dataFrame is being appended to. Alkesh Mahajan's answer is correct.
Comment: dataframes like RDD's are immutable and hence a new once is always created based on any action.
Answer: Operation like is completely useless in practice.  Spark DataFrame is a data structure designed for bulk analytical jobs. It is not intended for fine grained updates.
Although you can create single row DataFrame (as shown by i-n-n-m) and union it won't scale and won't truly distribute the data - Spark will have to keep local copy of the data, and execution plan will grow linearly with the number of inserted objects.
Please consider using proper database instead.
Comment: I needed it just for testing.

pyspark add new row to dataframe


Number of surjections from $\{1,2,3,4,5,6\}$ to $\{a,b,c,d,e\}$

Question: Where $A = \{1,2,3,4,5,6\}$ and $B = \{a,b,c,d,e\}$.
My book says it's:

Select a two-element subset of $A$.
Assign images without repetition to the two-element subset and the four
remaining individual elements of $A$.

This shows that the total number of surjections from $A$ to $B$ is $C(6, 2)5! = 1800$.
I'm confused at why it's multiplied by $5!$ and not by $4!$. Also in part 2, when we assign images, do they mean images in $B$?
Comment: It's not a question of what's left in $A$.  Having paired, say, $1,2$ we now need to count the surjections of $\{P,3,4,5,6\}$ onto $\{a,b,c,d,e\}$, where $P$ denotes the pair $(1,2)$.  There are clearly $5!$ such surjections.
Comment: There are $5$ objects, not $4$.  One object is the double, but that doesn't change anything,
Comment: I thought since we have a subset of 2, we multiply by 4! since there are 4 elements left in A.
Comment: " I thought ..., we multiply by 4! since there are 4 elements left in A." But you haven't chosen which of the 5 elements that subset of 2 map to.  Would it make more sense if we said the (number of ways to chose the two that aren't distinct)(choices for that pair)(choices for what is left) $={6\choose 2}*5*4! $? That's actually the same thing as (number of ways to chose the two that aren't distinct)(number of choices for the four distinct and the pair)$={6\choose 2}*5! $.
Answer: How many ways can $A$ be partitioned into $5$ blocks?
Answer: $\binom{6}{2} = 15$
Given any $5\text{-block}$ partition of $A$, in how many ways can the blocks be bijectively assigned to the $5$ element set $B$?
Answer: $5! =120$
How many surjective functions from $A$ onto $B$ are there?
Answer: $15 \times 120 = 1800$
Answer: Think of it this way:
There is a pair of terms that get mapped to the same element.  Call that pair $\alpha $.  There are four terms remaining.  Call them $\beta,\gamma,\delta$ and $\epsilon $.
There are ${6\choose 2} $ possible pairs that can be $\alpha $.
And we must map $\alpha,\beta,\gamma,\delta,\epsilon $ to $a,b,c,d,e $.  There is $5! $ ways to do that.
Answer: (i). Select  a $2$-member  $A_1\subset A.$ There are $\binom {6}{2}$ ways to do this. Select a $1$-member  $B_1\subset B.$ There are $\binom {5}{1}$ ways to do this. There are $\binom {6}{2}\binom {5}{1}$ such pairs $(A_1,B_1)$ and for each pair  there is a set $F(A_1,B_1)$ of $4!$ surjections $f:A\to B$ such that $\{f(x):x\in A_1\}=B_1.$
And if $(A_1, B_1)\ne (A'_1, B'_1)$ then the sets $F(A_1,B_1), F(A'_1,B'_1)$ are disjoint.
So there are at least $\binom {6}{2}\binom {5}{1}4!=(15)(5)(4!)=(15)(5!)=1800$ surjections.
(ii). Every surjection $f:A\to B$ belongs to some $F(A_1,B_1)$ so there are at most $1800$ surjections.
In other words: (i) we didn't count any $f$ more than once, and (ii) we didn't fail to count any $f$.
Remark. The "mysterious" $5!$ came from two sources: The product of the number $\binom {5}{1}$  of $B_1$'s and the number $4$!  of bijections from a $4$-member set to another.


How can I show/hide a button added to the title bar of an NSWindow?

Question: I have created a method in an NSWindow extension that allows me to add a button next to the text in the title bar. This is similar to the "down chevron" button that appears in the title bar of Pages and Numbers. When the button is clicked, an arbitrary code, expressed as a closure, is run.
While I have that part working fine, I would also like the button to be invisible most of the time and only become visible when the mouse is scrolled into the title bar area. This would be mimicking the way that Pages and Numbers displays the button.
However, I'm having difficulties getting the show/hide to work properly. I believe I can do it if I make it completely custom in the application delegate, and possibly by subclassing NSWindow, but I would really like to keep it as a single method in an NSWindow extension. In this way the code would be easily reusable in multiple applications.
To accomplish this I believe I need to inject an additional handler/listener that will tell me when the mouse enters and leaves the appropriate area. I can define the necessary area using an NSTrackingArea, but I haven't figured out how to "inject" an event listener without the need of subclasses. Does anyone know how (or if) such a thing is possible?
Comment: Which button in Pages do you mean? Is `NSTrackingArea` what you're looking for?
Comment: The button is the "down chevron" button that appears next to the document name in the title bar when the mouse is over that area. Pressing it allows you to move the document to another location. I'm trying to mimic that behaviour, although in my case the sheet that comes down does not have you moving a document, but changing the port that my app is monitoring. I've tried `NSTrackingArea`, but so far haven't managed to get it to solve the problem. Right now I'm thinking I would have to subclass `NSWindow` which would not be the end of the world, but is also not ideal.
Comment: How about hiding the image instead of the button?
Comment: I now have a solution to the problem. It uses an `NSTrackingArea` as suggested by @Willeke, but what I had to do was to add an additional object to the menu bar. Specifically I added an invisible view from a custom class that setup the tracking area and the `mouseEntered` and `mouseExited` methods. That tracking area - which covers the entire title bar - then enables/disables the button of interest.
Comment: I will now cleanup my code and, if the moderators reopen this issue, I will post the complete answer. Essentially I should now have an `NSWindow` extension that adds a method `addTitleActionButton` that puts everything in place.
Answer: The key to handling the show/hide based on the mouse position was to use an NSTrackingArea to signify the portion that we are interested in, and to handle the mouse enter and mouse exit events. But since this can't be done directly on the title bar view (since we have to subclass the view in order to add the event handlers) we need to create an additional NSView that is invisible but covers the area we want to track.
I'll post the full code below, but the key parts related to this question are the TrackingHelper class defined near the bottom of the file and the way it is added to the titleBarView with its constrains set to be equal to the size of the title bar. The class itself is designed to take three closures, one for the mouse enter event, one for the mouse exit, and one for the action to take when the button is pressed. (Technically the latter doesn't really need to be part of the TrackingHelper, but it is a convenient place to put it to ensure it does not go out of scope while the UI still exists. A more correct solution would be to subclass NSButton to keep the closure, but I have always found subclassing NSButton to be a royal pain.)
Here is the full text of the solution. Note that this has a couple of things that depend on another library of mine - but they are not necessary for the understanding of this problem and are used to deal with the button image. If you wish to use this code you will need to replace the getImage function with one that creates the image you want. (And if you want to see what KSSCocoa is adding, you can obtain it from https://github.com/klassen-software-solutions/KSSCore)
//
//  NSWindowExtension.swift
//
//  Created by Steven W. Klassen on 2020-02-24.
//

import os
import Cocoa
import KSSCocoa

public extension NSWindow {
    /**
     Add an action button to the title bar. This will add a "down chevron" icon, similar to the one used in
     Numbers and Pages, just to the right of the title in the title bar. When clicked it will run the given
     lambda.
     */
    @available(OSX 10.14, *)
    func addTitleActionButton(_ lambda: @escaping () -> Void) -> NSButton {
        guard let titleBarView = getTitleBarView() else {
            fatalError("You can only add a title action to an app that has a title bar")
        }
        guard let titleTextField = getTextFieldChild(of: titleBarView) else {
            fatalError("You can only add a title action to an app that has a title field")
        }

        let trackingHelper = TrackingHelper()
        let actionButton = NSButton(image: getImage(),
                                    target: trackingHelper,
                                    action: #selector(trackingHelper.action))
        actionButton.setButtonType(.momentaryPushIn)
        actionButton.translatesAutoresizingMaskIntoConstraints = false
        actionButton.isBordered = false
        actionButton.isEnabled = false
        actionButton.alphaValue = 0

        trackingHelper.translatesAutoresizingMaskIntoConstraints = false
        trackingHelper.onButtonAction = lambda
        trackingHelper.onMouseEntered = {
            actionButton.isEnabled = true
            actionButton.alphaValue = 1
        }
        trackingHelper.onMouseExited = {
            actionButton.isEnabled = false
            actionButton.alphaValue = 0
        }
        titleBarView.addSubview(trackingHelper)
        titleBarView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[trackingHelper]-0-|",
                                                                   options: [], metrics: nil,
                                                                   views: ["trackingHelper": trackingHelper]))
        titleBarView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[trackingHelper]-0-|",
                                                                   options: [], metrics: nil,
                                                                   views: ["trackingHelper": trackingHelper]))

        titleBarView.addSubview(actionButton)
        titleBarView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[titleTextField]-[actionButton(==7)]",
                                                                   options: [], metrics: nil,
                                                                   views: ["actionButton": actionButton,
                                                                           "titleTextField": titleTextField]))
        titleBarView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-1-[actionButton]-3-|",
                                                                   options: [], metrics: nil,
                                                                   views: ["actionButton": actionButton]))

        DistributedNotificationCenter.default().addObserver(
            actionButton,
            selector: #selector(actionButton.onThemeChanged(notification:)),
            name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"),
            object: nil
        )

        return actionButton
    }

    fileprivate func getTitleBarView() -> NSView? {
        return standardWindowButton(.closeButton)?.superview
    }

    fileprivate func getTextFieldChild(of view: NSView) -> NSTextField? {
        for subview in view.subviews {
            if let textField = subview as? NSTextField {
                return textField
            }
        }
        return nil
    }
}

fileprivate extension NSButton {
    @available(OSX 10.14, *)
    @objc func onThemeChanged(notification: NSNotification) {
        image = image?.inverted()
    }
}

@available(OSX 10.14, *)
fileprivate func getImage() -> NSImage {
    var image = NSImage(sfSymbolName: "chevron.down")!
    if NSApplication.shared.isDarkMode {
        image = image.inverted()
    }
    return image
}

fileprivate final class TrackingHelper : NSView {
    typealias Callback = ()->Void

    var onMouseEntered: Callback? = nil
    var onMouseExited: Callback? = nil
    var onButtonAction: Callback? = nil

    override func mouseEntered(with event: NSEvent) {
        onMouseEntered?()
    }

    override func mouseExited(with event: NSEvent) {
        onMouseExited?()
    }

    @objc func action() {
        onButtonAction?()
    }

    override func updateTrackingAreas() {
        super.updateTrackingAreas()
        for trackingArea in self.trackingAreas {
            self.removeTrackingArea(trackingArea)
        }

        let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeAlways]
        let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
        self.addTrackingArea(trackingArea)
    }
}


Show $f$ is uniformly continuous

Question:
Let $f$ continuous function on $[0,\infty)$. Lets assume there are $a,b$ such that:
  $\lim_{x\rightarrow \infty} f(x)-(ax+b) = 0$. Prove $f$ is uniformly continuous on $[0,\infty)$.

Well, At infinity $f$ is getting equal to a linear funciton. I can sense the ideal option is to show that $f$ is Lipchitz and therefore unifromly continuous. I wasn't able to formalize this thought.
I'll be glad for help how to show that.
Thanks.
Comment: $f$ might not be Lipchitz. Consider $f = ax+b + g(x)$ where $g$ is almost $0$, but has a tiny,increasingly sharp bump every so often.
Answer: Let $\epsilon >0$, then by definition of
$$\lim_{x \to \infty} f(x)-(ax+b)=0,$$
there is some $x_0 \in \mathbb{R}$ such that $|f(y)-(ay+b)|< \frac{\epsilon}{3}$ for every $y > x_0$. In particular for every $y,z > x_0$ such that $|y-z|<\frac{\epsilon}{3|a|}$ it holds
$$\begin{array}{rcl} |f(y)-f(z)| &=& |f(y)-(ay+b)+(ay+b)-(az+b)+(az+b)-f(z)| \\ &\leq & |f(y)-(ay+b)|+\underbrace{|(ay+b)-(az+b)|}_{=|a||y-z|}+|(az+b)-f(z)| \\ &< & \frac{\epsilon}{3}+  |a|\frac{\epsilon}{3|a|}+\frac{\epsilon}{3} = \epsilon \end{array}$$
Now, since $f$ is continuous on $[0,\infty)$ it is in particular continuous on the compact set $[0,x_0+1]$ and thus uniformly continuous on $[0,x_0+1]$. Thus for every $y,z \in [0,x_0+1]$, there is some $\delta_0 > 0$ such that $|y-z|< \delta_0$ implies $|f(y)-f(z)|< \epsilon$. We finally get that for every $y,z \in \mathbb{R}$ such that $$|y-z| < \delta := \min\{\delta_0,\frac{\epsilon}{3|a|}, \frac{1}{2}\}$$ we must have $$|f(y)-f(z)| < \epsilon,$$
since $|y-z|< 1/2$ implies $y,z \in [0,x_0+1]$ or $y,z> x_0$.
Edit:

Where did the $1/2$ come from?

This is in order to guarantee that if $|y-z| < 1/2$ then both $y,z$ are in one of these intervals $[0,x_0+1]$ or $]x_0,\infty)$. Without loss of generality we may suppose $y \leq z$. If $z \leq x_0+1$ then $y,z \in [0,x_0+1]$. If $z > x_0+1$ then using $z-y \leq 1/2$ we know $y \geq z-1/2 > x_0+1/2$ which imply that $z,y\in ]x_0,\infty)$. Note that we could have taken $[0,x_0+r]$ instead of $[0,x_0+1]$ and $\delta := \min\{\delta_0,\frac{\epsilon}{3|a|}, r\}$ for any $r > 0$. The important thing is that the intersection $[0,x_0+r]\cap [x_0,\infty)$ contains an open interval.
Comment: Wow. Thank you very much!
Comment: Where did the $\frac{1}{2}$ come from?
Comment: @AnnieOK I edited my answer
Answer: Fix $\epsilon > 0$. Let $M > 0$ so that $x > M$ implies $\left|f(x) - (ax + b)\right| < \epsilon/3$.
We have
\begin{align}
\left|f(x) - f(y)\right| &= \left|(f(x) - (ax + b)) - (f(y) - (ay + b)) + a(x  -y)\right| \\
&\le \left|f(x) - (ax + b)\right| + \left|f(y) - (ay + b)\right| + |a|\left|x-y\right|.
\end{align}
Since $f$ is uniformly continuous on $[0, M + 1]$, there is $\delta_1$ corresponding to $\epsilon$ in the definition of uniform continuity. Let $\delta_2 = \dfrac{\epsilon}{3|a|}$. Let $\delta = \min\{1, \delta_1, \delta_2\}$.
Suppose $\left|x - y\right| < \delta$. We consider two cases:
If one of $x$, $y$ is less than $M$, both are less than $M + 1$ and it follows that $\left|f(x) - f(y)\right| < \epsilon$.
If both of $x$, $y$ are strictly larger than $M$, the inequality above again implies that $\left|f(x) - f(y)\right| < \epsilon$.
Answer: I think both answers above are good, although I think it is hard to understand the underlying ideas by looking at these proofs.
Here are som hints, hopefully leading you to a similar proof to those above:

Set $h(x):=f(x)-(ax+b)$.
Show that  $h \rightarrow 0$ as $x\rightarrow \infty$ and that  $h$ is continuous
Use that a continuous function is uniformly continuous on compact sets.
Using the two points above, try to prove that $h$ is uniformly continuous on $[0, \infty)$
Knowing that $h$ is uniformly continuous, show that $f$ is uniformly continuous (uniform continuity is preserved under addition of functions)

Show $f$ is uniformly continuous


Downloadable large remote images using PHP

Question: I know there are lots of posts similar to this, but after crawling SO, still not found the answer.
I am looking to write a script that acts as a proxy for downloading large remote images (around  10mb each). So far I am using curl to read in the remote image url and then using headers to force a download. Something like (not the full script):
function getRemoteFile($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

header('Content-Type: octet/stream');
header('Content-Disposition: attachment; filename="random.jpg"');
header('Content-Length: ' . strlen($file));
echo $file;

This works, but is there a better way as this script may see quite a lot of traffic - maybe 300 concurrent users with 10 requests each?
The images will be served from a server on the same network.
Answer: 10mb is pretty large with 300 concurrent users with 10 requests.
You are saying 10 * 300 * 10 = 30,000 MB = 30GB
I Suggest you use a Job Queue
You can use Gearman
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("download", "getRemoteFile");
while ($worker->work());

You can not use AJAX and to check if the image is downloaded and display it
I would also recommend you look at the following

zeromq
beanstalkd
dropr
Comment: Yes - the usage is an issue. I have just started installing Gearman to give it a whirl. Thanks for a speedy response!


How to retrieve helm release manifest via terraform helm_release resource?

Question: I am trying to retrieve the deployed manifest and metadata information using the terraform
"helm_release" resource
Metadata values are coming out nicely however its failing in getting the manifest

Sample code:
`output "show_release_md" {
value=helm_release.testdeploy.metadata
}`   -- This works

output "show_release_manifest" { value=helm_release.testdeploy.manifest }  - Manifest fails
Unsupported attribute. This object has no argument,nested block or exported attributes named
"manifest"

Any ideas?

Answer: You must enable the manifest feature flag in experiments block of the Helm provider first. For reference see the Helm provider experiments block documentation and helm_release attribute reference.
e.g.
provider "helm" {
  kubernetes {
    config_path = "~/.kube/config"
  }
  experiments {
    manifest = true
  }
}

Comment: Hi, Thanks for the update. Tried adding experiments and I am getting below error

Error: Unsupported block type
17:24:54
17:24:54    on main.tf line 26, in provider "helm":
17:24:54    26:   experiments {
17:24:54
17:24:54  Blocks of type "experiments" are not expected here.

sample code:

`provider "helm" {
  kubernetes {
    host                   =  xxxx
    token.                = xxxxx

  }
  version = "2.0.2"
  experiments {
     manifest = true
  }
}`

Any ideas?
Comment: Hi All, Tried to remove the version = 2.0.2 and then the manifest feature started working . Thanks a lot ...


D3 line graph not filling entire svg width

Question: As the title states, I have created a D3 line/area graph, and I am finding it difficult to get the graph's width to remain constant, depending on the amount of data I have given it to render, it scales the width of the graph accordingly, but I am unsure of how I can get it to remain at a constant width, regardless of the amount of data given, which is what I would like to achieve.
I imagine it has something to do with the scaling of the x and y coordinates, but I am stuck at the moment and can't seem to figure out why it is doing this.
Here is the code I have thus far,
  //dimensions and margins
  var width = 625,
  height = 350,
  margin = 5,

  // get the svg and set it's width/height
  svg = d3.select("#main")
    .attr("width", width)
    .attr("height", height);

  //initialize the graph
  init([
        [12345,42345,32345,22345,72345,62345,32345,92345,52345,22345],
        [1234,4234,3234,2234,7234,6234,3234,9234,5234,2234]
      ]);

  $("button").live('click', function(){
      var id = $(this).attr("id");
      if(id == "one"){
        updateGraph([
                [52345,32345,12345,22345,62345,72345,92345,32345,22345,22345,52345,32345,12345,22345,62345,72345,92345,32345,22345,22345,52345,32345,12345,22345,62345,72345,92345,32345,22345,22345],
                [4234,12345,2234,32345,6234,7234,9234,3234,2234,2234,4234,1234,2234,3234,6234,7234,9234,3234,2234,2234,4234,1234,2234,3234,6234,7234,9234,3234,2234,2234]
              ]);
      }else if(id == "two"){
        updateGraph([
              [12345,42345,32345,22345,72345,62345,32345,92345,52345,22345,12345,42345,32345,22345,72345,62345,32345,92345,52345,22345,12345,42345,32345,22345,72345],
              [1234,2345,3234,2234,7234,6234,3234,9234,5234,2234,1234,4234,3234,2234,7234,6234,3234,9234,5234,2234,1234,4234,3234,2234,7234]
            ]);
      }

  });

function init(data){

  var x = d3.scale.linear()
    .domain([0,data[0].length])
    .range([margin, width-margin]),

  y = d3.scale.linear()
    .domain([0,d3.max(data[0])])
    .range([height-margin, margin]),

  /* line path generator */
  line = d3.svg.line().interpolate('monotone')
    .x(function(d,i) { return x(i); })
    .y(function(d) { return y(d); }),

  /* area path generator */
  area = d3.svg.area().interpolate('monotone')
    .x(line.x())
    .y1(line.y())
    .y0(y(0)),

  groups = svg.selectAll("g")
    .data(data)
    .enter()
    .append("g");

  svg.select("g")
    .selectAll("circle")
  .data(data[0])
  .enter()
    .append("circle")
    .attr("class", "dot")
    .attr("cx", line.x())
    .attr("cy", line.y())
    .attr("r", 4);

  /* add the areas */
  groups.append("path")
    .attr("class", "area")
    .attr("d",area)
    .style("fill", function(d,i) { return (i == 0 ? "steelblue" : "red" ); });

  /* add the lines */
  groups.append("path")
    .attr("class", "line")
    .attr("d", line);
}

function updateGraph(data){

  var x = d3.scale.linear()
    .domain([0,data[0].length])
    .range([margin, width-margin]),

  y = d3.scale.linear()
    .domain([0,d3.max(data[0])])
    .range([height-margin, margin]),

  /* line path generator */
  line = d3.svg.line().interpolate('monotone')
    .x(function(d,i) { return x(i); })
    .y(function(d) { return y(d); }),

  /* area path generator */
  area = d3.svg.area().interpolate('monotone')
    .x(line.x())
    .y1(line.y())
    .y0(y(0));

  groups = svg.selectAll("g")
    .data(data),

  circles = svg.select("g")
    .selectAll("circle");

  circles.data(data[0])
      .exit().remove();

  circles.data(data[0])
    .enter().append("circle")
        .attr("class", "dot")
        .attr("cx", line.x())
        .attr("cy", line.y())
        .attr("r", 4);

  /* animate circles */
  circles.data(data[0])
    .transition()
    .duration(1000)
    .attr("cx", line.x())
    .attr("cy", line.y());

  /* animate the lines */
  groups.select('.line')
    .transition()
    .duration(1000)
    .attr("d",line);

  /* animate the areas */
  groups.select('.area')
    .transition()
    .duration(1000)
    .attr("d",area);

}

As well as a fiddle http://jsfiddle.net/JL33M/
Thank you!
Answer: The width of the graph depends on the range() you give it. range([0,100]) will always "stretch" the domain() values to take up 100 units.
That's what your code is currently doing:
var x = d3.scale.linear()
  .domain([0,data[0].length])
  .range([margin, width-margin]);// <-- always a fixed width

You want the width to depend on the number of data entries. Say you've decided you want each data point to take up 5 units, then range() needs to depend on the size of the dataset:
var x = d3.scale.linear()
  .domain([0,data[0].length])
  .range([margin, 5 * data[0].length]);// <-- 5 units per data point

Of course, under these conditions, your graph width grows with the dataset; if you give it a really long data array of, say, 500 points, the graph would be 2500 units wide and likely run off screen. But if your data is such that you know the maximum length of it, then you'll be fine.
On an unrelated note, I think your code could use a refactoring to be less repetitive. You should be able to achieve what you're doing with a single update() function, without the need for the init() function.
This tutorial by mbostock describe the "general update pattern" I'm referring to. Parts II and III then go on to explaining how to work transitions into this pattern.
Comment: Thanks for the detailed answer! :) One thing that is confusing me a bit though, is that even though I specify a fixed width, the graph never fills up the entire container, I understand that it scales, but should it not scale the graph to the width and height I gave it? In my honest opinion it is very inconsistent and even though it makes sense, it creates a real problem when trying to achieve consistency between data sets. So if I understand you correctly, my code as it is should yield a graph that stretches to fit the entire width I specified it to use?
Comment: If only it would "stretch" the domain values, in some sane way.  There is no pattern to the result at all.  Sometimes larger values get a skinny result, and smaller get a wide result.  Sometimes the opposite is true.  What should happen, is the longest bar is set to the max-width, and all others proportionately scaled.
Comment: Isn't it because you're subtracting the margin?
Comment: would like to know how to make that work @JosephK
Comment: @Hugo Somewhat unclear what you're asking, but if your javascript code "knows" the exact pixel width you want to fill up (either bc it's a hardcoded width, OR bc you measured the container using getBoundingBox() or related methods) then just set the scale's range based on those measurements. On the other hand, if you don't know or don't want to measure the available space, try using percentage-based widths instead. In that case, the scale's range would be [0, 100], so it will yield values from 0 to 100, and then you append '%' to the value (rather than the usual 'px').


How to store the string with function in C#

Question: I'm beginner in C# programming. I'm struck with this simple program where I want to display the eligible candidates. My question is that how can I store the name of candidate after knowing that he/she is eligible.
    int eligble = 0;    //For counting the eligble Number of candidates

    bool retry;         //For trying until the Number of eligble candidates is reached
    retry = true;
    while (retry)
    {
        string candidatename;   //Intilization for Candidate Name ,Date of Birth ,10th and 12th Percentages
        int tper, twper;
        string  dob;

        Console.WriteLine("Please enter your Name");    //Getting user input values
        candidatename = Console.ReadLine();
        Console.WriteLine("Please enter your date of birth in dd/mm/yyyy format");
        dob = Console.ReadLine();
        DateTime dt = Convert.ToDateTime(dob);
        Console.WriteLine("Please enter your 12th percentange");
        twper = Convert.ToInt16(Console.ReadLine());
        Console.WriteLine("Please enter your 10th percentange");
        tper = Convert.ToInt16(Console.ReadLine());

        int age1 = age(dt);
        if (eligble > 5)        //Checking whether we have selected the Eligble amount of candidates
        {
            Console.WriteLine("We have selected five eligble candidtes");
            retry = false;
            Console.WriteLine("n");
        }
        else
        {
            if (age1 > 20 && twper > 65 && tper > 60)   //Checking Whether the candidate have satisfiyed the Conditions
            {
                eligble += 1;
                string grad = rade(twper, tper);
            }
            else
            {
                eligble -= 1;
            }
        }
    }
}

static int age(DateTime _dt)                                   //Function for calculating the age of the candidate
{
    DateTime n = DateTime.Now;                                  // To avoid a race condition around midnight
    int age = n.Year - _dt.Year;

    if (n.Month < _dt.Month || (n.Month == _dt.Month && n.Day < _dt.Day))
        age--;

    return age;
}

static string rade(int _tper, int _twper)
{
    string grade1;
    int avg= ( _tper+_twper)/ 2;
    if (avg > 90)
    {
         grade1 = "a";
        return grade1;
    }
    else if (avg > 80 && avg < 80)
    {
       grade1 = "b";
        return grade1;
    }
    else if (avg > 70 && avg < 79)
    {
         grade1 = "c";
        return grade1;
    }
    else
    {
         grade1 ="d";
        return grade1;
    }
}

Comment: How do you want to "store"? In a text file?
Comment: Top part of your code sample is missing
Comment: No..I want store this dynamically and display it @Ian
Comment: @vikram in that case, you may consider of using `List` and on top of that, consider of creating your `Candidate` `class` or `struct`, just in case you want to store multiple elements of the `Candidate` than just a single `string`. In such scenario, `List` would not be enough. But `List` will. :)
Comment: Is the above `if-else` is the  right method for checking the eligible candidates??
Answer: "Store" has broad meaning. You can store your data in memory for the time your program is running. In that case C# offers plenty of collections. List will work if you just want to keep them.
var names = new List<string>();
names.Add(candidate.Name);

If you prefer to store them with some kind of a key and then use the key to get the value from the collection better choice would be a dictionary:
var myEligibleCandidates = new Dictionary<string, string>();
myEligibleCandidates[candidate.Id] = candidate.Name;

These option will preserve the values for the time the application is running.
If you want your values to be stored also after the program is not running you can do that using a file. A static File class can be a good start:
public void WriteToFile(string path, List<string> names)
{
    File.WriteAllText(path, string.Join(";", names));
}

This method will take a list of names as a parameter and will write them separated by semicolon to a file. path is the path of the file.
Then there is option to save your data in the database. If that's what you want to do then take a look at Entity Framework and ADO.NET. Though, I would wait with these two options till you get better understanding of the first and second solution.
Answer: make a new List object, to do so do:
List<string> eligableCandidates = new List<string>();

and then when you want to add something to the list do:
eligableCandidates.Add(candidateName);

Hope this helps,
Jason.


How are coefficients of a linear regression affected by absence of normal distribution?

Question: The distribution of my residuals or dependant variable where not normal (skewness to the right and Kurtosis). the lnskew0 command in STATA transformed my dependent variable (DV) for solving the problem by doing ln (-DV - (-k) ) where K is calculated by stata. Now the skewness has disapeared, but some kurtosis is still presente. 1) Do you know how to solve the Kurtosis problem? 2) Now that I have a different B coefficients, p values and R2 for the regression before and after the transformation of my DV, which ones should I show as a result? the p values and R2 of the regression after the transformation, but the coefficients of the regression before transformation or other? B coeffcients of the regression after transformation are not easy to interpret for the readers and they are very different from the original values.
Answer: Even with non-normal residuals, the coefficient estimates are consistent. Non-normal residuals are not the biggest evil in model construction. Your main objective is capturing the dependence between the dependent variable and independent variables most accurately. It is not clear if the original model was better or if the transformation has improved the fit. To check that, you should use a model selection criterion, like Akaike Information Criterion (AIC) or Bayesian Information Criterion (BIC).
In my experience, if the residuals are not normal, fighting that fully is difficult. In most cases a transformation just mitigates the problem. Therefore, you should use bootstrap-based tests to assess statistical significance of the parameter estimates.
Comment: After the transformation of my dependent variable, the residuals of the regression are normal distributed.
Comment: According to the skewness test of my residuals,after the transformation  they are normal distributed, if I'm not mistaken the concepts! If as you say the coefficient estimates are consistent, so I guess I can take the coefficients of the first regression before the transformation, but the p values and R2 of the second regression with the transformed values ?
Comment: FOR MY REGRESSION WITH NOT TRANSFORMED DEPENDENT VARIABLE
. predict r, residuals
(1316 missing values generated)

. sktest r
   Skewness/Kurtosis tests for Normality
                                                         ------- joint ------
    Variable |     Obs   Pr(Skewness)   Pr(Kurtosis)  adj chi2(2)    Prob>chi2
-------------+---------------------------------------------------------------
           r |     907      0.0000         0.4707          47.52         0.0000
Comment: FOR MY REGRESSION WITH TRANFORMED DEPENDENT VARIABLE
. predict rr, residuals
(1316 missing values generated)
. sktest rr
                    Skewness/Kurtosis tests for Normality
                                                        ------- joint ------
    Variable |     Obs Pr(Skewness)   Pr(Kurtosis)  adj chi2(2)    Prob>chi2
-------------+---------------------------------------------------------------
          rr  |     907      0.8416          0.0000         39.75         0.0000
Comment: Sorry stans, it is not well shown, but the skewness test for my residuals went from 0,000 to 0,8416 and the histrogram of the residuals show that its distribution is now normalmy. What do you think?
Comment: As I mentioned, normality of residuals is not the most important characteristic of a model. You should compare the two models using AIC or BIC. Each reported block of results should come from the same model but you are totally allowed to report both models, space permitting.
Comment: Perfect, I understand, thank you very much stans!!


What are the properties of a half Cauchy distribution?

Question: I am currently working on a problem, where I need to develop a Markov chain Monte Carlo (MCMC) algorithm for  a state space model.
To be able to solve the problem, I have been given the following probability of $\tau$: p($\tau$) = 2I($\tau$>0)/(1+$\tau^2$). $\tau$ being the standard deviation of $x$.
So now I know that it is a half-Cauchy distribution, because I recognise it from seeing examples and, because I was told so.  But I do not fully understand why it is a "Half-Cauchy" distribution and which properties come with it.
In terms of properties I am not sure what I want. I am fairly new to this type of econometrics theory. So it is more for me to understand the distribution and how we use in a state space model context. The model itself looks like this:
\begin{align}
y_t         &= x_t + e_t  \\
x_{t+1}     &= x_t + a_{t+1}  \\[10pt]
a_{t+1}     &\sim ~ N(0, \tau^2)  \\
p(\sigma^2) &\propto 1/\sigma^2  \\[3pt]
p(\tau)     &= \frac{2I(\tau>0)}{\pi(1+\tau^2)}
\end{align}
Edit: I included $\pi$ in p($\tau$). Thank you for pointing this out.
Comment: Please indicate which properties you are interested in: after all, there are infinitely many that one could describe.
Comment: Half-distributions of symmetric distributions have twice the area functional height for their range, which range is a half-range, often but not necessarily starting at zero $x\geq 0$.
Answer: A half-Cauchy is one of the symmetric halves of the Cauchy distribution (if unspecified, it is the right half that's intended):

Since the area of the right half of a Cauchy is $\frac12$ the density must then be doubled. Hence the 2 in your pdf (though it's missing a $\frac{1}{\pi}$ as whuber noted in comments).
The half-Cauchy has many properties; some are useful properties we may want in a prior.
A common choice for a prior on a scale parameter is the inverse gamma  (not least, because it's conjugate for some familiar cases). When a weakly informative prior is desired, very small parameter values are used. However, there are cases where the posterior is somewhat sensitive to the specific choice so perhaps 'weakly informative' isn't a great description of such a prior.
The half-Cauchy is quite heavy tailed and it, too, may be regarded as fairly weakly informative in some situations. Gelman ([1] for example) advocates for half-t priors (including the half-Cauchy) over the inverse gamma because they have better behavior for small parameter values but only regards it as weakly informative  when a large scale parameter is used*. Gelman has focused more on the half-Cauchy in more recent years. The paper by Polson and Scott [2] gives additional reasons for choosing the half-Cauchy in particular.
* your post shows a standard half-Cauchy. Gelman would probably not choose that for a prior. If you have no sense at all of the scale, it corresponds to saying that the scale is as likely to be above 1 as below 1 (which may be what you want) but it wouldn't necessarily fit with some of the things Gelman is arguing for.
[1] A. Gelman (2006),
"Prior distributions for variance parameters in hierarchical models"
Bayesian Analysis, Vol. 1, N. 3, pp. 515–533
http://www.stat.columbia.edu/~gelman/research/published/taumain.pdf
[2] N. G. Polson and J. G. Scott  (2012),
"On the Half-Cauchy Prior for a Global Scale Parameter"
Bayesian Analysis, Vol. 7, No. 4, pp. 887-902
https://projecteuclid.org/euclid.ba/1354024466
Comment: The "2" in the formula is superfluous, because apparently the formula is intended to give the PDF only up to a constant--after all, it is already missing the factor of $1/\pi$.
Comment: @Glen_b, what is the **location** in the half-Cauchy in your answer?
Comment: @morouzian which location measure are you interested in? Regarded as a member of a location-scale family the standard form being discussed has a location of 0 and scale of 1, but I am not sure if that's what you're asking. (Its median is 1, as suggested near the end of my answer, if that helps any.)


CSF *Port Scan* detected - shared hosting

Question: We modified a few options in ConfigServer Firewall configuraion. Ever since the CSF blocking a lot of IP address. Following print sreen:
http://oi60.tinypic.com/kdlnh3.jpg
What can I modify thereto CSF not blocking a lot of IPs ?
Thank you!
Answer: Based on CSF Guide:
You can change PS_LIMIT and PS_INTERVAL based on your needs.

Port Scan Tracking.
This feature tracks port blocks logged by iptables
  to syslog. If an IP address generates a port block that is logged more
  than PS_LIMIT within PS_INTERVAL seconds, the IP address will be
  blocked.
This feature could, for example, be useful for blocking hackers
  attempting to access the standard SSH port if you have moved it to a
  port other than 22 and have removed 22 from the TCP_IN list so that
  connection attempts to the old port are being logged
This feature blocks all iptables blocks from the iptables logs,
  including repeated attempts to one port or SYN flood blocks, etc
Note: This feature will only track iptables blocks from the log file
  set in IPTABLES_LOG below and if you have DROP_LOGGING enabled.
  However, it will cause redundant blocking with DROP_IP_LOGGING enabled
Warning: It's possible that an elaborate DDOS (i.e. from multiple
  IP's) could very quickly fill the iptables rule chains and cause a DOS
  in itself. The DENY_IP_LIMIT should help to mitigate such problems
  with permanent blocks and the DENY_TEMP_IP_LIMIT with temporary blocks
Set PS_INTERVAL to "0" to disable this feature. A value of between 60
  and 300 would be sensible to enable this feature
Comment: Thank! For eg.: Noew PS_LIMIT = 10, PS_INTERVAL = 300. Will I modify a few bigger value ?
Comment: Your welcome. Yes, I think configuring CSF with those values should run OK.


How to show the rate of convergence of this maximum likelihood estimator is $n^{-1}$?

Question: Suppose I have data $\{X_i\}_{i=1}^{n}\sim \text{Uniform}[0,\theta_0]$. We know that the maximum likelihood estimator of $\theta_0$ is $\widehat{\theta}=\max\{X_1,...,X_n\}$. How to show that $\widehat{\theta}$ converges to $\theta_0$ at rate $n^{-1}$?  Thanks!
Comment: https://stats.stackexchange.com/q/130447/119261
Answer: When $(X_i)$ is a sequence of uniformly distributed variables $\mathcal{U}_{[0,\theta_0]}$ we can derive the distribution of the MLE $\hat \theta = \max X_i$.
We will show that $n( \theta_0 - \hat \theta)$ converge towards a non trivial (i.e. not $0$) distribution, which means that the rate of convergence of the MLE is $n^{-1}$.
First note that since $\max X_i \leq \theta_0$, $n( \theta_0 - \hat \theta) \geq 0$.
Then, for $t \geq 0$,
\begin{align*}
\mathbb P\left( n(\theta_0 -\hat \theta) \leq t \right)  &=1- \mathbb P\left( \hat \theta \leq \theta_0 - \frac{t}{n} \right) \\
&=1-\mathbb P\left( \max X_i \leq \theta_0 - \frac{t}{n} \right) \\
\end{align*}
Since $\max X_i \leq \theta_0 - \frac{t}{n} \iff \forall i, X_i \leq \theta_0 - \frac{t}{n}$ and since all $X_i$ are independent and identically distributed,
\begin{align*}
\mathbb P\left( \max X_i \leq \theta_0 - \frac{t}{n} \right) &=\prod_{i=1}^n\mathbb P\left( X_i \leq \theta_0 - \frac{t}{n} \right) \\
&=\mathbb P\left( X_1 \leq \theta_0 - \frac{t}{n} \right)^n
\end{align*}
For $n$ large enough we have $\frac{t}{n} \leq \theta_0$ and then,
\begin{align*}
\mathbb P\left( X_1 \leq \theta_0 - \frac{t}{n} \right)^n  &= \left(\frac{\theta_0 - \frac{t}{n} }{\theta_0} \right)^n \\
&=\left(1 - \frac{t}{\theta_0 n}\right)^n
\end{align*}
As $n \to \infty$ the last line is equivalent to $\exp\left(-\frac{t}{\theta_0}\right).$
So for $t \geq 0$:
$$
\mathbb P\left( n(\theta_0 -\hat \theta) \leq t \right) \xrightarrow[n \to \infty]{} 1- \exp\left(-\frac{t}{\theta_0}\right).
$$
We can see that $1-\exp\left(-\frac{t}{\theta_0}\right)$ is the cumulative distribution function of an exponential random variable with rate $\frac{1}{\theta_0}$.
Thus, $n (\theta_0 - \hat \theta )$ is asymptotically exponentially distributed, and the rate of convergence of $\hat \theta$ is $n^{-1}$.
Here is a R code to illustrate this result (with $\theta_0 = 3$ and $n=50$):
<code>theta0<-3
n<-50
s<-sapply(1:1e4,function(i){
      X<-runif(n,0,theta0)
      return(n*(theta0-max(X)))
})
P_emp<-function(t){
      return(mean(s<=t))
}
#P_emp is an empirical cumulative distribution of n*(theta0 - MLE)
#P_emp should be close to the CDF of an exponential distribution

x<-seq(0,20,length.out = 1000)
y1<-sapply(x,function(t) P_emp(t))

plot(x,y1,type='l',col="red")
lines(x,pexp(x,rate=1/theta0),type='l',col="black")
legend(x=0,y=1,legend = c("Empirical CDF","Exponential
CDF"),lty=1,col=c("red","black"))
</code>
Comment: @JTS365  No I don't think it works for any distribution over $[0, \theta_0]$. Take for example $\theta_0 =1$ and the distribution with density $f(x) = 2-2x$ over $[0,1]$, then $\forall t$, $\mathbb P( n(1- \max X_i) \leq t) \to 0$ which means that $n(1-\max X_i) \to \infty$. In that case the rate is $n^{-\frac{1}{2}}$
Comment: Thanks! This is completely solved my problem! If I change the distribution to other distributions on $[0,\theta_0]$ and want to estimate the boundary ($\theta_0$) using $\underset{i}{max}\{X_i\}$, does the rate $n^{-1}$ still hold?
Comment: Thanks! Does the result generalize to bivariate uniform distribution on a rectangle $[0,\theta_0]\times [0,\theta_1]$?
Comment: @JTS365  Yes it does, if $(X_0,X_1) \sim U_{[0,\theta_0]\times [0,\theta1]}$ then $X_0 \perp X_1$ and $X_i \sim \mathcal U_{[0,\theta_i]}$ thus $\hat \theta_0 = \max X_{0,i}$ and $\hat \theta_1 = \max X_{1,i}$ and the rate for both estimators is $n^{-1}$.


Double Click Android

Question: Estou desenvolvendo um aplicativo que tem uma lista em um RecyclerView e a ideia é que a pessoa escolha um ou mais produtos dessa lista e mude de cor para mostrar que esta selecionado, mas no caso da pessoa escolher errada, ela da um duplo click. Consegui fazer funcionar usando um setOnClickListener no viewHolder, dai quando seleciono algum produto ele muda de cor. Tentei chamar também setOnLongClickListener até funciona, testei com duas corres diferentes, mas não fica tão fácil usar, se tiver como configurar para mudar o tempo do Click longo pode ser uma opção também. Se alguém tiver algum exemplo de como implementar a função onClick em uma outra classe, ou se já existe uma classe que posso usar. Qualquer informação pode ser útil. Obrigadooo
Comment: Pode explicar melhor. Não entendi a razão para querer o double click. Normalmente o primeiro click selecciona, caso se tenha enganado clica novamente e de-selecciona.
Comment: Sim, você entendeu certo. Assim também é uma boa opção, como verifico se foi clickado para não selecionar, faço isso com o setOnClickListener mesmo?
Comment: Depende de como você está a fazer para seleccionar.
Answer: Double click não faz parte do universo mobile, apenas de desktop com uso de mouse, usuários não estão acostumados com isso nos aplicativos, é uma péssima decisão de UX utilizar essa abordagem em mobile.
Uma das melhores forma é como a da imagem a baixo.

Para ativar a multi seleção o usuário usa o long press e com um toque seleciona ou cancela a seleção dos itens da lista, Abra o app Gmail e verifique como funciona, a maioria dos app funcionam dessa forma.

Double Click Android


Requisição ajax leva muito tempo para executar

Question: Eu tenho uma requisição ajax em que o método usado é o POST, as informações que eu passo são com um objeto formData, nele tem algumas fotos que podem variar de 1 até 15, quando eu executo isso no localhost não dura 1 segundo, agora que subi o site para o servidor de hospedagem ele demora 120 e para de executar, a minha pergunta é, o que pode estar ocasionando isso visto que no servidor local executa quase instantaneamente?
<code>$.ajax({
    url: '../controller/inserir_prova_banco.php',
    method: 'POST',
    data: formData,
    processData: false,
    contentType: false,
    success: function(data){}
});
</code>
Comment: Possível duplicata de [Requisição php demorando 1 minuto quando executado no servidor](https://pt.stackoverflow.com/questions/273365/requisi%c3%a7%c3%a3o-php-demorando-1-minuto-quando-executado-no-servidor)
Answer: Amigo, tem vários fatores que pode ocasionar isso.
Primeiro, o servidor local a latência é zero. Ele vai executar instantaneamente.
Agora quando é servidor web, em outro lugar.... ai sim, olha o que pode dar de problema para ficar tão lento.
1- Sua internet é extreamente leta e demora
2- Pode haver problema de rota, do seu computador até o servidor
3- O seu servidor web, pode estar sobre carregando com muitos clientes simultaneos....
4- Sua aplicação ta mal estruturada etc....
São varios fatores... mas o que eu acredito que seja, é que seu servidor está lento.... se não tiver POST de arquivos... acho que é seu servidor mesmo
Comment: Tu diz a estrutura e configuração do meu servidor de hospedagem? Se sim tem algo que eu possa pedir para eles mudarem? Eu acho que tem a ver com os arquivos, pois eu tenho outras requisições ajax que não passam arquivos como parâmetros, só texto, e então funciona.


Click on submit button, should disable the checkbox button Angular JS

Question:
    <input type="checkbox" ng-model="alert.acknowledged" ng-disabled="alert.acknowledged" ng-click="onacknowledgedClick(alert)">

{{alert.acknowledgedComments}}

<input type="text" style="height:30px; width:150px" ng-model="alert.acknowledgedComments"/>
<button type="button" class="btn btn-primary"  ng-click="saveAlert(alert)"> Submit </button>
<button type="button" class="btn btn-primary"  ng-click="cancelAlert(alert)" data-dismiss="modal"> Cancel </button>

When I click on checkbox button, the text textfield with submit button will be popped up. After entering the comments on textfield, when the user clicks on submit button, the checkbox should get disabled. Can anyone give me some inputs on this?
How to disable the checkbox button, once when I click on Submit button?
Answer: You have to use JavaScript to do this. Make an event listener for the submit button that disables the checkbox and the cancel button that does display: none; to the text input. For info about event listeners visit this site: Event Listeners
This is what you should do:

document.getElementById("submit").addEventListener("click", function() {document.getElementById("alertAcknowledged").disabled = true;})
document.getElementById("cancel").addEventListener("click", function() {document.getElementById("text").style.display = "none";})
<input type="checkbox" ng-model="alert.acknowledged" ng-disabled="alert.acknowledged" id="alertAcknowledged" ng-click="onacknowledgedClick(alert)">

                            {{alert.acknowledgedComments}}

                                <input type="text" id="text" style="height:30px; width:150px" ng-model="alert.acknowledgedComments"/>
                                <button type="button" class="btn btn-primary"  ng-click="saveAlert(alert)" id="submit"> Submit </button>
                                <button type="button" id="cancel" class="btn btn-primary"  ng-click="cancelAlert(alert)" data-dismiss="modal"> Cancel </button>
Comment: I am working on Angular JS. Can you help me with Angular js?
Comment: I'm sorry. I don't know angular. Should I delete the answer?
Comment: No. Thanks for the help
Comment: Yes! Can you give me a function when I click on cancel button, the text field shouldn't come. It should be hidden.
Comment: Okay. Thank you so much.
Comment: The addition is complete.
Answer: I'd suggest jQuery:

$("#submit").click(function(){
  $("#alertAcknowledged").attr("disabled", true);
})

<input type="checkbox" ng-model="alert.acknowledged" ng-disabled="alert.acknowledged" id="alertAcknowledged" ng-click="onacknowledgedClick(alert)">

                            {{alert.acknowledgedComments}}

                                <input type="text" style="height:30px; width:150px" ng-model="alert.acknowledgedComments"/>
                                <button type="button" class="btn btn-primary"  ng-click="saveAlert(alert)" id="submit"> Submit </button>
                                <button type="button" class="btn btn-primary"  ng-click="cancelAlert(alert)" data-dismiss="modal"> Cancel </button>
Answer: add disableCheckbox as a paramet and set it inside the click function.
HTML

    <input type="checkbox" ng-model="alert.acknowledged" ng-disabled="alert.disableCheckbox" ng-click="onacknowledgedClick(alert)">

{{alert.acknowledgedComments}}

<input type="text" style="height:30px; width:150px" ng-model="alert.acknowledgedComments"/>
<button type="button" class="btn btn-primary"  ng-click="saveAlert(alert)"> Submit </button>
<button type="button" class="btn btn-primary"  ng-click="cancelAlert(alert)" data-dismiss="modal"> Cancel </button>

CONTROLLER
$scope.onacknowledgedClick = function(alert){
  alert.acknowledgeInProgress = true;
}

$scope.saveAlert = function(alert) {
  /*$scope.alert.acknowledged = true;*/
  alert.disableCheckbox = true;
  alert.acknowledgeInProgress = true;
  var alertUrl =  $incidentsUrl+"/"+alert.alertForIncident.incidentID+"/alerts/"+alert.alertID;

  $http.put(alertUrl, alert).then(function(result) {
    alert.acknowledgeInProgress = false;
  });
}

Comment: This is my saveAlert controller:                                                                                $scope.saveAlert = function(alert) {
   $scope.alert.acknowledged = true;
   alert.acknowledgeInProgress = true;
   var alertUrl = $incidentsUrl+ "/" +alert.alertForIncident.incidentID+"/alerts/"+alert.alertID;
   $http.put(alertUrl, alert).then(function(result) {
    alert.acknowledgeInProgress = false;
            });

  }   It's still the same before clicking on submit button only, the checkbox is getting disabled.
Comment: post your entire controller (if it's not too huge), but also see Christian's answer. he makes a good point.
Comment: $scope.onacknowledgedClick = function(alert){
   alert.acknowledgeInProgress = true;
  }

  $scope.saveAlert = function(alert) {
   /*$scope.alert.acknowledged = true;*/
   alert.acknowledgeInProgress = true;
   var alertUrl = $incidentsUrl+ "/" +alert.alertForIncident.incidentID+"/alerts/"+alert.alertID;
   $http.put(alertUrl, alert).then(function(result) {
    alert.acknowledgeInProgress = false;
            });

  }
Comment: Couldn't you just have a disableCheckbox parameter on alert that is set to true in the click function?
Answer: I would avoid using jquery as much as possible in angular. Here's a good read http://tech.zumba.com/2014/08/02/angularjs-forget-jquery/
So, it looks like your ng-disabled="alert.acknowledged" but ng-click="alert.acknowledged" as well. This means when you click the radio button, alert.acknowledged is set to true, so it gets disabled.
You probably want a different value in ng-disabled, maybe ng-disabled="disableRadioBtn", then in your submit controller you can set $scope.disableRadioBtn = false;