R-Squared for real valued label under non linear regression learner

Question: Below are my questions of R-Squared for real valued label under non linear regression learner. It may be a large problem, if there is no easy answer, could you give me some references?
Firstly for the real valued label, except for R-squared, is there any good value to evaluate the performance of fitting? I know that small MSE, MAE, e.t.c usually mean the good fitting. However they may be not as intuitive as a ratio like R-Sqaured (how small is good?).
Secondly does R-Squared of non linear regression learner really make sense? Since linear regression has sum of squared error decomposition, R-Squared is always between 0 and 1 and the closer R-sqaured gets to 1, the better fitting is. However other learner cannot guarantee sum of squared error decomposition, R-Squared even can go to large negative. Here we really want the small residual error, however does it make sense to compare with total error? One way to understand is that the total error (variance of sample) is a non-model dependent value, therefore we can use it as a benchmark?
Comment: I address your second question [here](https://stats.stackexchange.com/a/551916/247274). Regarding the first question, what constitutes a good $R^2?$
Comment: @Dave  Here we really want the small residual error, however does it make sense to compare with total error? So is there any value to intuitively describe how small residual error is good? Or could I understand total error (variance of sample) is a non-model dependent value, therefore we can use it as a benchmark?
Comment: What I meant was more song the lines of judging the $R^2$ value. There are fields where $R^2=0.5$ is unbelievable, and I can imagine problems where $R^2=0.9$ is rather pedestrian.
Comment: Have you figured out what constitutes a good $R^2$ value? (I have yet to do so, and for this reason, I'm not a huge fan of $R^2$. I fear it can mislead people into thinking in terms of grades in school.)
Answer: $R^2$ can get us thinking in terms of letter grades in school, where $R^2=0.9$ is the kind of score that seems like an $\text{A}$-grade that makes us happy while $R^2=0.4$ is the kind of score that seems like an $\text{F}$-grade that makes us sad. Some people see this as an upside to $R^2$. I do not agree. There may be situations where $R^2=0.4$ is outstanding (I have seen papers in top journals where $R^2 < 0.10$), and some situations where $R^2=0.9$ is rather pedestrian. As is remarked here by our mkt:

There is no context-free way to decide whether model metrics such as $R^2$
are good or not.

What $R^2$ does do, at least depending on how you perform the calculation (I have a strong opinion), is compare the performance of your model to the performance of a "must-beat" model. Failing to beat that performance manifests in $R^2\le 0$, with $R^2<0$ indicating performance even worse than the "must-beat" level. In that sense, $R^2$ can be useful.
Metrics like $(R)MSE$ and $MAE$ are helpful, too, since they give a sense of how large typical error magnitudes are. If you look at those values and see that about half of your errors have magnitudes of over a centimeter, you might know that you need to be within a milimeter almost every time in order to have a model worth anything (maybe a competitor has such performance, or maybe you just know the science and requirements of your problem). Likewise, you might be elated with that centimeter, if you know that the tolerance is three centimeters.
As is discussed in the linked answer, however, no performance metric tells the whole story unless you give it context.


Functional Analysis(Normed Spaces)

Question: Let $X$  be the space of all complex valued square Riemann- integrable functions on $[0,1]$ with $2$- norm. Define the map $F:X\to X$  by $F(u)=v$ with $v(t)=\int\limits_{0}^{t}{ u^2(s) ds}$, then show that $F$ is continuous (but not uniformly continuous)?
Any hint to the above problem ..A beginner in Functional Analysis !!
Comment: @Mann It was not the task to prove continuity with respect to the topology of $X$.
Comment: Take any two element $u_1(t)$ and $u_2(t)$ and corresponding $v_1(t)$ and $v_2(t)$ then consider the expression $v_2(t)-v_1(t)=\int_0^t\left[u_1(s)+u_2(s)\right]\times \left[u_2(s)-u_1(s)\right]\times \mathbb{d}s$. For continuity, show that as $u_1(t)\to u_2(t)$, $v_1(t)\to v_2(t)$ and to disprove show that the difference of $v_2(t)-v_1(t)$ depend upon choice of $u_1(t)$ and $u_2(t)$ use epsilon delta proof. Hard part is to do it with function rather than numbers.
Comment: disapprove uniform continuity*
Answer: For $u,v\in X$ we have
\begin{align*}
\|Fu - Fv\|_2^2
&= \int_0^1|(Fu)(t) - (Fv)(t)|^2\,dt = \int_0^1\left|\int_0^t(u^2-v^2)\,ds\right|^2\,dt\\
&= \int_0^1\left|\int_0^t(u-v)(u+v)\,ds\right|^2\,dt\le\int_0^1\left(\int_0^t|u-v||u+v|\,ds\right)^2\,dt\\
&\le \int_0^1\left(\int_0^t|u-v|^2\,ds \right)\left(\int_0^t|u+v|^2\,ds \right)\,dt\\
&\le \|u-v\|_2^2\|u+v\|_2^2.
\end{align*}
Hence,
$$
\|Fu - Fv\|_2\le \|u-v\|_2\|u+v\|_2\le\|u-v\|_2\big(\|u-v\|_2 + 2\|v\|_2\big).
$$
The continuity of $F$ follows from here.
Comment: Thanks for the explaination !! Continuity is clear and why its not uniformly continuous?(Since we can't choose a \delta for entire space ,its norm dependent )

Functional Analysis(Normed Spaces)


Ajustar tempo para expirar $_SESSION

Question: Como ajustar o tempo para expirar $_SESSION?
tentativas..
Incluir no top dos arquivos (funcionou no localhost offline)
ini_set('session.gc_maxlifetime', 3600); // 1 hora
ini_set('session.cookie_lifetime', 3600);
session_start();

E também
session_set_cookie_params(3600); // 1 hora
session_start();

Alterar direto no php.ini
session.gc_maxlifetime = 3600
session.cookie_lifetime = 3600

mas nenhum dos métodos funcionaram no servidor Online
Answer: No geral, utilizo sempre essa forma. Implementando um tempo limite na sessão:
if (isset($_SESSION['ultima_atividade']) && (time() - $_SESSION['ultima_atividade'] > 3600)) {

    // última atividade foi mais de 60 minutos atrás
    session_unset();     // unset $_SESSION
    session_destroy();   // destroindo session data
}
$_SESSION['ultima_atividade'] = time(); // update da ultima atividade

session.gc_maxlifetime e session.cookie_lifetime  não são confiáveis.
Criando um simples código para deslogar ao expirar a sessão:
     session_start();

     ini_set('session.use_trans_sid', 0);

     if (!isset($_SESSION['name'])){
       $_SESSION['name']="Guest";

     }

     if ($_SESSION['name']!="Guest"){
        $counter = time();

        if (!isset($_SESSION['count'])){
          $_SESSION['count']= $counter;
        }

        if ($counter - $_SESSION['count'] >= 3600 ){
header('Location: http://www.meusite.com.br/sair.php');

       }
        $_SESSION['count']= $counter;

    }

Leia mais clicando aqui.
Comment: dúvida... ao destruir a session as variáveis setadas nela serão perdidas, e o usuário terá de refazer o processo caso ele tenha começado e parado até expirar o tempo, isso?
Comment: Claro. Quando expirar o tempo, a sessão é destruída.
Comment: legal @Lollipop, esse código devo por no arquivo que checa se o usuário está logado ou não né?
Comment: Atualizei, com base no seu comentário.

Ajustar tempo para expirar $_SESSION


Tell BIND to not forward for domains used by DNS block list services

Question: How can I tell BIND to stop forwarding queries to my provider's DNS servers for a specific list of domains, but to forward for everything else?

I'm using BIND as an authorative nameserver for my domain.  It also provides DNS resolution for other queries on the host, by forwarding them to my provider's DNS servers:
forward first;
forwarders {
    22.214.171.124;
    220.127.116.11;
};

However I am running SpamAssassin and my queries to various DNS blocklists are being rejected, since they are going through my provider's nameservers (along with many of my fellow customers) and reaching the limit for free queries originating from their IP addresses.  Since I only have a small number of queries myself (maybe 5-10 a day) I would like to configure BIND so that it does NOT forward DNSBL queries to my provider's servers, but it DOES forward everything else.
This should allow me to continue to use DNS blocklists to spam check the small amount of e-mail I get, without having to turn off the forwarding/caching entirely.
How can I tell BIND not to forward just a handful of domains, but keep it forwarding everything else?
Comment: Is there any particular reason why you don't want to perform your own recursion? forwarders are meant to be exceptions to your recursion policy.
Comment: Only because there are so many people using my provider's servers it is significantly faster than performing recursion myself.
Answer: Try configuring zones for your blacklists, and configure them not to forward.  Something like this should work.  You will need one of these for each blacklist zone.
zone "blacklist.example.com" {
    type static-stub;
    recursion yes;
};

You will also need to setup the root zone so recursion will work.  You likely already have done this.
Comment: Thanks for the suggestion - unfortunately it looks like this only works with BIND 9.8.0 and newer, but my server is running Debian which is still on 9.7.3, so I get an error about `static-stub` being an unknown type.
Answer: Thanks to @BillThor I was able to come up with this entry which works with older (9.7.3) versions of BIND:
zone "blacklist.example.com" {
    type forward;
    forwarders {};
};

On my Debian machine I added it to /etc/bind/named.conf.local


Conflicting signing certificate attributes present - Timestamp from TSA for PDF

Question: Our TSA recently upgraded their TSA servers, but this is causing the "Conflicting signing certificate attributes present" error to occur when we attempt to Digitally Sign and Timestamp our PDFs (using C# code).
If I used the TSA's old server or SignFiles test server, then everything works as expected, but once I switch to the new server, then issues occur.
We are using SignFiles which looks to be a simple wrapper around BouncyCastle. The error occurs within the Bouncy Castle "Validate(...)" method.
VS Code Showing Error
I believe the error is happening as a result of the highlighted calls below.
BouncyCastle Code - Validate(...) where error happens
At this point, I am a bit lost as I do not know what values the above code is checking, but I am thinking it might be the "Enhanced Key Usage" values.
If yes, then I see the following differences between the old Timestamp (left side) and the new one that does not work (right).
Working and Non-Working Certificates Returned by TSA
Can anyone advise if this is the correct values that it is checking?
Can anyone advise why this error is happening and if its something that the TSA can correct within their settings and how the Timestamp is generated OR does Bouncy Castle need to be updated to work with new TSA servers?
We are continuing to use the legacy servers, but need to get this fixed to take advantage of the new faster servers.
I only found one other question regarding this at:
http://bouncy-castle.1462172.n4.nabble.com/Time-Stamp-with-both-SigningCertificate-and-SigningCertificateV2-td3457605.html
Thank you,
Rob
Answer: It seems that your client library is internally using BouncyCastle older than 1.8 which was incorrectly processing timestamps. As you have correctly found out this problem was reported (by me) in JIRA issue 85 and fixed in BouncyCastle 1.8.
Original RFC3161 compliant timestamps included only SigningCertificate (OID 1.2.840.1135126.96.36.199.2.12) CMS attribute with SHA-1 hash of TSA certificate. But since whole world abandoned SHA-1, RFC5035 updated RFC3161 and allowed the usage of SigningCertificateV2 (OID 1.2.840.1135126.96.36.199.2.47) CMS attribute with SHA-256 (or any other) hash of TSA certificate. It also allowed the usage of both attributes at the same time to support legacy clients that may not support new hash algorithms. So IMO it is perfectly OK for TSA to include both attributes.
You have two options:

Ask the provider of your client library to upgrade it to BouncyCastle 1.8 where the issue has been fixed. IMO this would be easier.
Ask your timestamp provider to reconfigure their TSA server so it does not include both CMS attributes in timestamps it generates. IMO this would be harder.

Let me know if something is not clear enough or you need more details.
Comment: Thank you for the response. In our case, we are using the latest package from SignFiles (https://www.signfiles.com/signature-library/) which I would assume has the latest Bouncy Castle code?

The highlighted code I found above is NOT from my machine, but rather from looking up that error and finding that code with the error message.

I will verify that my code is updated.
Comment: Is the "Enhanced Key.." the Signing Cert as I thought? From the values you provided, it looks like I am wrong. Where would I find the Signing Certificate values that you indicate?

Is there a reason why they would want to include both Signing Certificates?
(I want a valid reason for them to change these values without causing issues for other clients)

Thank you
Comment: @user2032 well then it looks like package from SignFiles uses old BouncyCastle code.
Comment: @user2032 `SigningCertificate` and `SigningCertificateV2` are CMS attributes. They are not in present in TSA certificate. They are present in each issued timestamp. You can read more about them in [RFC5035](https://www.ietf.org/rfc/rfc5035.txt).
Comment: @user2032 I've updated my answer to provide more details.
Comment: Thank you "Jariq".
SignFiles was able to correct their code quickly (good support and free update). Thanks for the information.


Accessing global variables in PHP function

Question: I'm new to php and I can't understand why is it that the variable "cons" isn't recognized by the the compiler when used inside the function "func", in the following code:
$cons = 1;

function plusCons($num) {
   return $num + $cons;
}

is it impossible to use global variables inside of a function's scope?
Comment: `function plusCons($num) use($cons) { ...`
Comment: declare it `global  $cons;` inside function. See: http://php.net/manual/en/language.variables.scope.php
Comment: You can, but it generally should be avoided. You should just pass the variable in as a parameter and then update it when the function returns, or use a class.
Answer: In order to access global variables within a PHP function, you need to use the global keyword to import the variable:
$cons = 1;

function plusCons($num) {
   global $cons;

   return $num + $cons;
}

Comment: this will not work in a lot of contexts, e.g. assiging a closure to a variable inside a class method. Use `use(...)`.
Comment: Right.  Fortunately that isn't what the OP was inquiring about.
Answer: This also will work for you:
$cons = 1;

function plusCons($num ,$cons) {
  return $num + $cons;
}

echo plusCons(2 , $cons); // this will output 3


The code is pushed to the main thread, how to fix it?

Question: This code works even when I exit the activity and go to another activity. In order to understand this, I went to another activity and deleted the path in Firebase and the application crashed due to the fact that this path no longer exists. I would like to disable this code when I exit the activity, how can I do this?
private void addRoomEventListener() {
        messageRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (role.equals("host:")) {
                    if (snapshot.getValue(String.class).contains("guest:")) {
                        step.setEnabled(true);
                        Toast.makeText(DuelGame.this, "" +
                                snapshot.getValue(String.class).replace("guest:", ""), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (snapshot.getValue(String.class).contains("host:")) {
                        step.setEnabled(true);
                        Toast.makeText(DuelGame.this, "" +
                                snapshot.getValue(String.class).replace("host:", ""), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                messageRef.setValue(message);
            }
        });
    }

Answer: You can save addValueEventLister call in a variable yourListener and then do this when switching to another activity or when the current activity is going to finish ( usually in onPause() or onStop()),
dbref.removeEventListener(yourListener);

Comment: What should I put under "your Listener"? `messageRef`?
Comment: No, you have to save the entire addValueEventListener in a variable and pass that variable to removeEventListener
Comment: I already figured it out, thanks


Meaning of the mapping

Question: I am reading basic sets. In the section of sesquilinear mapping,
I came across this mapping
 $f: X \to X^*$
 i.e  $x \ $ maps to $ (x|.)$
Here I know $X$ is a function space, I guess
$X^*$ stands for its dual. $(|)$ is inner mapping.
My question:  What exactly is $(x|.)$?
Thanks in advance for the explanation.
Comment: It's the linear functional $(x |\cdot ): y\mapsto (x|y)$.  At least, this is what I would say if $X$ was a vector space and $X^\ast$ is dual space.
Answer: It is the map
$$\begin{array} .(x\:|\:\cdot): & X & \to & \mathbb{C} \\
 & y & \mapsto & (x|y) \end{array}$$
Comment: Thanks for the answer. It is now clear.

Meaning of the mapping


Unexpected behavior when extracting minima using turnpoints (Pastecs package in R)

Question: I have some data, height.
> print(height)
             V3          V4          V5          V6          V7          V8          V9         V10         V11         V12         V13         V14         V15
      0.0000000  -3.1486232  39.1502937  -2.8437808 -24.7633269 -35.4022061 -36.2401943  46.4133021 -19.1868097   0.4067331  22.6768200 -38.8550971 -41.3161054
            V16         V17         V18         V19         V20
     33.0446273  41.4934899  19.9558741   3.3477473   0.3611974

I then apply turnpoints(), and use extract() to get what looks like a logical vector indicating the positions of the minima.
tp = turnpoints(height)
pitpositions = extract(tp, peak = FALSE, pit = TRUE)
print(pitpositions) # [1] 0 1 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0

I would expect that height[pitpositions] would give the values of the minima, and that height[!pitpositions] would give the values of the non-minima.
The latter works as expected:
> height[!pitpositions]
         V3          V5          V6          V7          V8         V10         V12         V13         V14         V16         V17         V18         V19
  0.0000000  39.1502937  -2.8437808 -24.7633269 -35.4022061  46.4133021   0.4067331  22.6768200 -38.8550971  33.0446273  41.4934899  19.9558741   3.3477473
        V20
  0.3611974

But the former doesn't, and I don't know how to interpret the results:
> height[pitpositions]
V3 V3 V3 V3
 0  0  0  0

So my questions are:

Why is this happening?
How can I get the result that I'm looking for (basically the opposite of height[!pitpositions]?

Edit:
To answer question number 2, I can get what I'm looking for using height[tp$pits].  tp$pits seems to have the same information as pitpositions, but when I print out pitpositoins I get a series of TRUE FALSE, whereas when I print out tp$pits I get a series of 0 1.  I would have thought these were equivalent, as 0 == FALSE and 1 == TRUE.
Answer: I don't think extract is doing what you want. It's serendipitous that height[!pitpositions] is giving something you like.
I'd recommend something more like just using the components of tp directly.
Rgames> tp$peaks
 [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
[13] FALSE FALSE  TRUE FALSE FALSE FALSE
Rgames> tp$pits
 [1] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
[13]  TRUE FALSE FALSE FALSE FALSE FALSE
Rgames> height[tp$peaks]
[1] 39.15029 46.41330 22.67682 41.49349
Rgames> height[tp$pits]
[1]  -3.148623 -36.240194 -19.186810 -41.316105

EDIT:  it appears that the default method for extract on a turnpoints object is:
Rgames> extract(tp)
 [1]  0 -1  1  0  0  0 -1  1 -1  0  1  0 -1  0  1  0  0  0

So, rather than extracting a subset of that, you would probably want to do something like
height[extract(tp)== -1]

for pits, and
height[extract(tp)== 1]

for peaks. I suspect your original extract formula as posted returns all values not equal to 1  (or not equal to -1), hence the zeroes, and the change in length of the vector.
Comment: Thanks, yeah, that's doing exactly what I need.  Can you give an example of when one would use extract()?


Complex Linq Query with multiple joins

Question: I am not really familiar with Linq and I need to create a query to get the following data:
Tables:
Product
Id,
GroupId,
ProductCode,
SpecificCode
...
ProductGroup
Id,
Status
...
Rule
Id,
GroupId,
ProductCode,
SpecificCode
...
Parameters:
ProductCode:string,
SpecificCode:string
Requirement:
1.Get all ProductGroups where Status = Active, and which is associated with at least one Product with ProductCode and SpecificCode matching the parameters.
2.Get all rules assocated with these groups.
3.Filter out the rules that don't have a matching Productcode + SpecificCode on the rule itself.

I have written it out in multiple steps because that helps me understand the process. It may be possible in one query - I don't know. TBH I don't really care if I need multiple queries, I just need it to get the correct result set, with no duplicates (which is where I am really struggling).
Many Thanks!
Comment: you might want to provide some sample data so people get a better understanding of what you mean by no duplicate data
Answer:         from pg in ProductGroup
        where pg.status == "active"
        select new
        {
        ProductGroup = pg,
        Products = (from p in Product
              && p.GroupId = pg.Id
              && p.productcode == inputproductcode
              && p.specificcode == inputspcode).ToList(),

        Rules = pg.Rules.Where(r=>r..productcode == inputproductcode && r.specificcode == inputspcode).ToList()
        }

Something like this should work.
Comment: Try something like that, it's weird though, that property should be set up.
Answer: What about this? Assuming your properties are Products, ProductGroups and Rules:
var productGroups = (from p in Products
                     join pg in ProductGroups on p.GroupId equals pg.Id
                     where pg.Status = "Active"
                     select new
                            {
                                p.ProductCode,
                                p.SpecificCode,
                                Rules = Rules.Where(r => r.ProductCode == p.ProductCode
                                    && r.SpecificCode == p.SpecificCode).ToList()
                            })
                     .ToList();

Comment: ProductGroup doesn't have ProductCode and SpecificCode peoperties
Comment: Ah okay, what links a Product to a ProductGroup?
Comment: When Product.GroupId == ProductGroup.Id
Comment: That's getting close, but I think it ignores requirement 2 - I think it should have another where call - .Where(r => r.GroupId == p.GroupId)
Comment: Also, I think there is a Distinct needed somewhere, as it returns a lot of duplicates (lets say rules 1 and 2 are correctly returned) - it shows multiple instances of these as a list of lists - [[1,2][1,2][1,2]...] - I just need [1,2]. I could call First() on the result I suppose but I wonder if that would jut be a hack.

Complex Linq Query with multiple joins


Validation Split and Checkpoint Best Model in Keras

Question: Let us use a validation split of 0.3 when fitting a Sequential model. What will be used for validation, the first or the last 30% samples?
Secondly, checkpointing the best model saves the best model weights in .hdf5 file format. Does this mean that, for a certain experiment, the saved model is the best tuned model?
Comment: Please ask unrelated questions separately
Comment: Did my answer solve your problem?
Answer: For your first question, the last 30% samples will be used for validation.
From Keras documentation:

validation_split: Float between 0 and 1. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling

For your second question, I assume that you're talking about ModelCheckpoint with save_best_only=True. In this case, this callback saves the weights of a given epoch only if monitor ('val_loss', by default) is better than the best monitored value. Concretely, this happens here. If monitor is 'val_loss', this should be the tuned model for a particular setting of hyperparameters, according to the validation loss.


Has I constructed LSTM model in a right way in Keras?

Question: Here's a description of a model I'd like to get:

A vanilla LSTM model with similar size as our model. Specifically, a
  two-layer sacked LSTM is  constructed,  with  128  and  32  hidden
  states,  respectively, followed by a fully connected layer for the
  final output. This neural network also takes 28 days as input, and predicts the next day.

# design network
model = Sequential()
model.add(LSTM(128, input_shape=train_X.shape)) % 28 days
model.add(LSTM(32, input_shape=128)) % 128 units for previous layer
model.add(Dense(1))

Answer: This looks okay as long as the input shape is correct.  input_shape is (number_steps, dimension_features), so in this case it would be (28, dim_feat).  If dim_feat is not 1, then you would also need to change the dimension of the output of the Dense layer to be dim_feat.


How to handle a webglcontextrestored event

Question: What is the right way of dealing with a context restored event?
Do i go ahead and create a new WebGL Rendering Context? What happens to all cached textures, programs, shaders that I had?
Answer: When you lose the webgl context all your textures, programs, shaders, renderbuffers and buffers are gone.
The webglcontextrestored event is basically just an event telling you you have a new webgl context, start over.


Perceptron find weight exercise

Question: I have some difficulties with the following exercise.

There are three different diagrams. If possible, find the perceptron-weights $w_0, w_1,$ and $w_2$ for each of them (the decision surface is clearly divided into two regions, one "positive" the other one "negative").

Solution: for diagram 1 it's obvious that the function is $x_2=x_1$, does it mean that $w_0=0,w_1=-1,w_2=1$?
for diagram 2, the function is $0.7x_1+x_2+1.4=0$, does it mean that $w_0=1.4, w_1=0.7, w_2=1$? If the direction makes any sense here?
for diagram 3, $x_2=|x_1|$, I am not sure how to define weight.
Answer: Your solutions for 1 and 2 are correct, since the perceptron's weight vector is perpendicular to the decision boundary.
For diagram 3, there is no solution because the perceptron is a linear classifier (i.e. the decision boundary is always a single line).
Answer: Classifier for 1 is correct. Classifier for 2 is incorrect:
Test with your classifier ($w_0=+1.4, w_1=+0.7, w_2=+1$) and $x_1=-3, x_2=0$:
$$
y=f_step(w0+w1*x1+w2*x2)=f_step(+1.4+(-3*+0.7)+(0*1)=f_step(+1.4-
2.1)=f_step(-0.7)=-1
$$
 --> incorrect classification as (-), should be (+)
Classifier 2 should be: $w_0=-1.4, w_1=-0.7, w_2=-1$.
Test for classifier 2 with x1=-3, x2=0:
$$
y=f_step(w_0+w_1*x_1+w_2*x_2)=f_step(-1.4+(-3*-0.7)+(0*-1)=f_step(-1.4+2.1)=f_step(+0.7)=1
$$
--> correct classifcation as (+)
As already mentioned in the above answer, the 3rd example cannot be separated by a single perceptron.

Perceptron find weight exercise


How do I update the cache for an applet?

Question: I am trying to update the cache for an applet.  The applet properly caches, but afterwards, no matter how stale the cache is, it won't update.  If I manually delete the cache, a new one will be created upon the next page load, and all changes to the .jar file I am trying to cache take effect.  Having to do this, though, is not acceptable.
I have tried using cache_archive in conjunction with cache_version in my index file like so:
if (navigator.appVersion.indexOf("Win")!=-1){
    var attributes = { id:'manager', code:'HardwareManagerApplet_FileWriter',          width:1, height:1} ;
    var parameters = {jnlp_href: '/java/HardwareManagerApplet.jnlp', codebase: '/java/hardwaremanager.jar', cache_archive:'hardwaremanager.jar',  cache_version:'0.0.0.7'};
    check = deployJava.runApplet(attributes, parameters, '1.6');
    hardware_enabled = true
    console.log("Applet started")
}

This is having no effect.  I have tried moving cache_archive and cache_version to attributes and as various permutations between to no avail.  I have tried enabling cache_option set to first browser, then tried again setting it to plugin.  No dice.  I've looked into ETags, and I'm not confident that will be the best solution.  I've also looked into last-Modified, but A) I'm not sure how to implement it into the http so the cache will update and B) that will open a whole new can if I have to go that route.
Is there any other reasonable alternative?  Better yet, am I simply implementing cache_archive et al incorrectly?
And actually, even if I can get it to cache every single time the page loads, I will be satisfied.
Thanks for all and any help!
Edit: The block of code about is the ONLY implementation I've made of cache_archive etc.  Do I need to put in a couple lines elsewhere?  As far as Oracle's documentation went, I didn't find that to be very clear.
Answer: Your missing the parameter for the cache option they are as follows
The cache_option attribute can take one of three values:

No - Disable applet installation. Always download the file from the web server.
Browser - Run applets from the browser cache (default).
Plugin -  Run applets from the new Java Plug-in cache.

By defaulting to browser you would have to clear the browser cache for the updated values to take effect.  Use plugin to take full advantage of the versioning you want to implement.
Comment: I wondered about that... I tried placing cache_option:plugin before cache_archive in the parameters var, but I was kind of just shooting in the dark.  If I were to place cache_option:plugin back into that line, should it, in theory, work?  It didn't last time, but I might be doing _that_ wrong or something else wrong too.
Comment: It definitely didn't solve everything, although it probably would have been a problem down the road.  Thank you.
Comment: Its still not being cached properly?
Comment: Have you tried using the older object html tag to load the applet? It may also make things a little easier to debug.
Answer: The caching of an applet is the work of both the virtual machine (JVM) and your browser. The browser as well as the JVM are optimized for caching applet for performance purpose.
My solution were one of both:

Clear the cache of the virtual machine after every classes build: open the Java console and type x (ClassLoader-Cache clear).  Then refresh your browser.
or
Close your browser and start it all afresh

I prefer solution 1.


How do I select a child node based off the contents of a same level child node?

Question: For example I want to only select a child node if the other child has the contents "example":

         capture this text

    example</lable>

Basically it needs label's inner-text to equal "example" or I do not want the contents of the strong.
I'm fairly new to XPath and I'm not quite sure how to go about this.
Comment: The `following-sibling::` axis is the answer.
Answer: You can filter parent
 node by it's  child like this :
//ul[@class='thisClass' and label='example']/ul/strong

Or you can filter child
 node by it's following-sibling :
//ul[@class='thisClass']/ul[following-sibling::label[.='example']]/strong


Dockerfile ERROR: standard_init_linux.go:207: exec user process caused "no such file or directory

Question: I'm having this error when trying to create service ERROR: standard_init_linux.go:207: exec user process caused "no such file or directory".. This does not happen when I use ubuntu:latest as base image.
So I try to run service with:
docker service create --name mon --publish 9999:9999 --mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock mon:1

And the service is not successfully created.
Here is my dockerfile with golang:alpine:
FROM golang:alpine
COPY . /edg
RUN chmod a+x /edg/edgmn
EXPOSE 9999
CMD ["/edg/edgmn", "--rules.reactive", "--swarm.environment", "--alert.address=X:9091"]

I've tried a lot of things but nothing works.
Any ideas?
Thanks
Comment: "/edg - file not found". Please extract and provide a [mcve], along with the docker comand you ran and its output.
Answer: Your binary is likely dynamically linked against one or more libraries that is not available in the alpine image. For example, if your binary is linked to glibc (typical if you compiled it on Ubuntu), it will not work in alpine (which uses musl instead of glibc), but would work with ubuntu:latest.
See How do I build a static Go binary for the Docker Alpine image?
In short, compiling your binary with CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build <args> before copying it into the alpine container will often solve the problem.
Comment: You could add `RUN ldd /edge/edgmn` to your Dockerfile if you want to see the output of `ldd` from within the alpine container.
Comment: Is there any way to know which are the libraries causing problems? Thanks @hmm
Comment: Run `ldd ` to see linked libraries. If run from within the alpine container, it will tell if any libraries are not found. It's best to compile statically (edited my answer with a link).
Comment: linux-vdso.so.1 (0x00007fff2698a000)
 libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2866e50000)
 libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2866a5f000)
 /lib64/ld-linux-x86-64.so.2 (0x00007f286706f000)
this is the result of ldd. Is the problem caused by any of this libraries?
Thanks @hmm
Comment: GNU `libc` is the obvious one. You might try compiling your binary with `CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ...` before copying it into the alpine container.
Comment: I can't run from the inside of container, because it is not created.
Comment: It works now. But it still might be something wrong because virtual size is 400MB. 300MB bigger than with ubuntu:latest image. It should be smaller. Thanks in advance
Comment: The `golang:alpine` image contains all of the Go tools. You don't need all that to run a static binary. Just use `alpine:latest`.
Comment: Yep. 46MB. Thank you so much! @hmm


Reverse order a for each on a array

Question: I have picked some code up another developer built. Poorly documented and not even sure where the api connection to the real estate listings. Anyway to the point, one of the pages on the site shows business listings in oldest to newest. I want to change that order.
This piece of code cycles through the properties to list these is like so:
foreach ( $properties->Listings as $property )

How can I reverse the order on this list?
Answer: You just need to use array_reverse() function. So that:
array_reverse($properties->Listings)

You even don't need to use foreach.
Source: https://www.php.net/manual/en/function.array-reverse.php

Reverse order a for each on a array


Block Matrix - Prove Invertibility

Question: There's matrix $M =\left[\begin{array}{cc} I &B\\ B^T& I\end{array}\right]$, where $B$ is $m × n$ (note that the upper left corner is the $m × m$ identity, while the lower right corner is the $n × n$ identity).
(a) Show that if $I − (B^T)B$ is invertible then $M$ is also invertible.
(b) Show that if $I − (B^T)B$ is singular then $M$ is also singular.
What I tried: I tried calculating $\det(M) = \det (I) \cdot \det (I-(B^T)B)$, and since $I-(B^T)B$ is invertible, then $\det(M)$ is not equal to zero and then $M$ is invertible. I am not sure if this is correct and I am looking for other methods than the determinant method.
Comment: Could you use MathJax so the matrices and operations your doing are more readable please?
Comment: How did you end up with the formula $\det(M) = \det (I) \cdot \det (I-(B^T)B)$? Are you familiar with the "Schur complement"?
Answer: Note that
$$
\pmatrix{I & 0\\-B^T & I}\pmatrix{I & B\\B^T & I} = \pmatrix{I & B\\0 & I - B^TB},
$$
which means that
$$
\det \pmatrix{I & 0\\-B^T & I}\det \pmatrix{I & B\\B^T & I} = \det\pmatrix{I & B\\0 & I - B^TB} \implies\\
1 \cdot \det\pmatrix{I & B\\B^T & I} = \det(I - B^TB).
$$

Block Matrix - Prove Invertibility


Redirect, change or replace the "user/register" link

Question: How to redirect, change or replace the "user/register" link
to another registration page, such as "civicrm/contribute/transact&reset=1&id=1"?
Why? Because our "Drupal users" are (paid) members, they have to choose a membership type, they get informed about the corresponding fee (and how to pay offline) and have to provide additional member information (postal address, day of birth,...). All this is handled by a civiMember page, which creates also a Drupal user (user name and password), blocked at the beginning (changed to active as soon as payment is received).
We probably have to keep the Drupal user login block, for registered users (paid members) to login.
Comment: Very similar question has been asked and answered: [Alter login form redirect doesn't work](http://drupal.stackexchange.com/questions/111131/altering-create-new-account-tab-link-in-user-login-page)
Answer: The easiest solution I think here is just redirect it at the Apache (.htaccess) level with a rewrite rule:
<code>RewriteRule ^user/register$ /civicrm/contribute/transact&reset=1&id=1 [NC,L,R=301]
</code>
Comment: RewriteRule is not allowed by our shared hosting provider.
Answer: The Drupal Redirect module can push people from user/register to a civi form
Comment: It works... but I get an "no access rights" error.

The URL is transformed:
[domain]?q=civicrm/contribute/transact%26reset%3D1%26id%3D1

The original URL gives no error:
[domain]?q=civicrm/contribute/transact&reset=1&id=1

Isn't the "translation" from & and = into % values something neutral in a URL? so why the & and = works, and the % does not.
Comment: Sorry, I think you need to use Alias and Redirect, eg set Alias for civicrm/contribute/transact?reset=1&id=2 to /join. And set Redirect from user/register to join. If that resolves it i will update my answer
Comment: great thx for your help, but... same result. Alias and Redirect both are working perfectly, but i still get the "no access rights" error. Again, replacing the new alias by the original civiCRM url gives no error, displays the "new member" page as wanted.
Comment: Redirecting to a page with drupal_goto() seems to work (I will test later):

`drupal_goto('civicrm/contribute/transact',
  array('query'=>array('reset'=>'1','id'=>'1',))
 );`
Comment: The suggestion of using drupal_goto() was found here (the syntax has changed since 2008):
https://civicactions.com/blog/tech_tuesday_clean_urls_in_civicrm_or_how_to_use_menus_to_link_to_civicrm_forms


HMT or HABTM for Orders and Items

Question: Fairly new to rails and trying to understand which relationships to use before going forward.
I have two models: orders and items. This is a many to many relationship, but I'm unsure of which relationship to use.
Orders might have delivery time, quantity of items, etc.
Lastly, what would you call the model joining orders and items if using HMT?
Comment: Which one to use?  http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many
Comment: @Emma here is a bare bones Rails implementation: https://gist.github.com/halilim/53aa2402fb2321f3ab27 You can look at [Spree's source code](https://github.com/spree/spree) for a much more advanced example, and [Magento's database diagram](http://www.magereverse.com/) if you want to see more than you'd ever hope.
Comment: Do you need to be able to go backwards:  Given an Item, what orders does it appear on?  If so, it's the classic case of HABTM, and there's a perfect example here: http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
Comment: And the default join table would be named items_orders (alphabetical order)
Comment: @Daiku sorry on mobile device acc - I don't need to go backwards given an item. Only need to see which items an order has. Should I use HMT? What would you call the model if I use HMT?
Comment: As the last link says - it depends on if the join table will have anything other than the two IDs.  If not, than use the HABTM, as it will do the join logic for you automatically (except creating the join table in the migration).  If you do HMT, then you will be defining the third model as an active record model, and you can name it anything you want.
Comment: Why it's many to many? How do `items` have many `orders`? Normally orders would have many items and each item would have a product.
Comment: @HalilÖzgür I was thinking many orders have many items and items belong to many orders. Is that incorrect?
Comment: @Emma in a normal e-commerce app, order items should belong to a single order, i.e. an item (a product with a quantity) can't exist in multiple orders. Even if a product is ordered in the exact same quantity in multiple orders, each one would be a different item. Otherwise if an item is canceled from an order, it would be canceled from another person's order too inadvertently. An example e-commerce db schema: http://www.databaseanswers.org/data_models/b2c.htm
Comment: @HalilÖzgür In the db schema example you provided, isn't `Order_Items` a HMT association joining `Orders` and `Products`?
Comment: @HalilÖzgür Great examples! From the bare bones implementation, it looks like the `Order_Items` as HMT is correct?
Comment: @Emma Firstly, I assume you mean "has many through" by HMT, right? If so, products have many orders through order items and orders have many products through order items. You can indirectly relate products and orders to each other through order items. Having said that, if we are to continue with the gist, exactly which part did you find confusing?
Comment: @HalilÖzgür I was confused because your comment above says: `Why it's many to many? How do items have many orders? Normally orders would have many items and each item would have a product.` But the gist you created shows that items(products) and orders have a many to many association.
Comment: @Emma well, I thought you meant it's possible that `OrderItem` can have a `has_many` or a `has_and_belongs_to_many` to `Order`, which doesn't make sense normally. Sorry if I've taken it wrong.
Answer: If you need to know anything else about the relationship of the item on a particular order, you need HMT.
If your items change price in the future, do you want to know how much they were sold for on orders in the past?
In this type of requirement, I've always had many "LineItem" records for an order, and the line_item instances belong_to to the item and order, and record the pricing and/or quantity for that order.
HMT vs HABTM? There are so few times that all you need is a many-to-many, that I'd almost always go with HMT for the extra ability to add more information to the association.
Answer: This seems like a classic case of HABTM, and the example given in the Rails Guides is perfect.  The choice comes down to whether you need any other data or logic on the join model itself.  If so, then use the HMT, where you will create a third active_record model to serve as the join table.  You can name that anything you want.  But it seems like HABTM will work for you, and all you need to setup is the join table with the default name (items_orders) in your migration, and rails will take care of everything else for you.
class Order < ActiveRecord::Base
  has_and_belongs_to_many :items
end

class Item < ActiveRecord::Base
  has_and_belongs_to_many :orders
end

HMT or HABTM for Orders and Items


Time Series Forecasting with (week)day data in R

Question: I was hoping to ask this community for a little advice. I'm doing a bit of daily forecasting, and, because it has always served me so well, I'm using Prof. Hyndman's now famous 'forecast' package. At a high level, I'm simply feeding auto.arima a timeseries object of frequency 5 (I'm only using business days), along with 2 columns of simple exogenous regressors—a dummy variable for "Holiday" and a dummy variable for "last business day of the month".  For most of my product forecasts, this works quite well, but for the odd product or two, I'm not quite satisfied with the model output. My questions, then are:
1.  Do you think that I should add additional dummy variables for things like "Monday" or "Tuesday after a holiday" or perhaps every day of the week (save one)? I'm worried about overfitting, but I'm also worried about the seasonal AR/MA terms getting confused by situations where Mondays are either higher than the average weekday or zero (in the case of a holiday)
2.  I only have 9 months or so of data…do you think I should try to add additional seasonality to the model, or wait until I have a full year?
Answer: I would add 4 day-of-the-week and also allow for changes in the day-of-the-week and a holiday variable for each holiday as you are assuming all holidays have the same effect. I would look for level shifts and local time trends over time. I would look for monthly effects and weekly effects . I would look for and deal with non-constanterror variance using weighted least squares or power tranforms . I would look for one-time anomalies and create dummy pulse indicators as needed. I would look for day-of-the-month effects.
Comment: Extremely good advice, and very helpful! Thank you!
Answer:
A dummy variable for the day after a holiday can be effective (and sometimes also the day before a holiday). I wouldn't add a dummy for every different day of the week following a holiday as that will probably overfit. Any regular seasonality should be captured by the seasonal ARMA part of the model, although you could instead handle that with four day-dummies but then make the ARMA part of the model non-seasonal.
You can't add additional annual seasonality until you have at least a year of data as you can't separate trend from annual seasonality.
Comment: This is great advice. Thank you so much!


How secure is ASP.NET Identity session cookie?

Question: Of course cookies can be stolen and sessions hijacked, but how secure is the encryption itself of the session cookies (in ASP.NET Identity)? Could it be manipulated with modern hardware and a little bit of time?
I'm asking because I would like to add a claim that identifies a group, but this is only secure if the cookie is really secure. Otherwise an attacker could sign up for a legit account and then break into other groups without even stealing passwords.
Comment: have a look at this [question](http://stackoverflow.com/questions/4306543/is-it-safe-to-store-only-userid-using-setauthcookie-in-asp-net). there is a comment "The entire contents of the FormsAuth token are encrypted and signed, including the user name / user id, so it cannot be tampered with." which i think might answer your question
Comment: Sort of, but there's also criticism of Microsofts standard password hashing, which many consider below standard now. So still wondering if cookies are signed and encrypted in a strong way. If an attacker could break it in a week, it's not good enough.
Answer: Answer updated for .NET Framework 4 and greater:
The machineKey element which is used for:

encryption, decryption, and validation of forms-authentication data
  and view-state data

uses AES as the default decryption algorithm, with a minimum key length of 128 bits.
It also uses HMACSHA256 as the default for validation which is HMAC over SHA-256. A HMAC prevents a length extension attack which is a way for an attacker to append data to the value that has the hash over it in such a way that the hash still validates.
There are no known practical attacks against AES-128 (unlike 192 and 256 that can suffer from a related key attack), and SHA-256 is nowhere near being broken, although there is an academic attack against its sister SHA-1 that questions its collision resistance. Collision resistance doesn't really matter when authenticating token values, however it is theoretically one step towards breaking preimage resistance. Therefore, you can rest assured you are secure against attacks with AES-128 and HMAC SHA-256.
However, as with any encryption or hash algorithm, it will all boil down to how secure your key is. AutoGenerate and IsolateApps settings will be fine, however if you set your own make sure that they are generated by a cryptographically secure pseudo random number generator (CSPRNG) and are 128bits in length. Anything less than that means you could be compromised by a brute force attack (in theory). 128 bits means that even using every computer in the world in a distributed brute-force attack on your key would never complete in your lifetime. Of course, attackers may try and get round this by grabbing your key some other way. Keep it secure.
Comment: I believe ASP.NET Identity uses a more modern algorithm and number of hash cycles than was previously the case in ASP.NET - so this answer may no longer be fully accurate.


Branches of analytic functions

Question:
Find a branch of $\log(z^2+1)$ that is analytic at $z=0$ and takes the
  value of $2\pi i$ there. Also, determine a branch of $\log(z^2+2z+3)$ that is analytic at $z=-1$.

If I plug in $z=0$ and $z=-1$ to there respective functions then I get $\log(1)$ and $\log(2)$ but then what do I have to do to find a branch?

Define a branch of $(z^2-1)^{1/2}$ that is analytic in the exterior of
  the unit circle $|z|>1$.

If I transform this function into $e^{(1/2)Log(z^2-1)}$, why will the principal branch not work here?
Comment: To start with, do you know the branch points of the function $\ln(z^2+1)$?
Comment: @MhenniBenghorbal No, I do not. Can you elaborate a bit more, please?
Comment: The function $\ln(z^2+1)$ has two [logarithmic branch points](http://en.wikipedia.org/wiki/Branch_point#Transcendental_and_logarithmic_branch_points), namely $z=i,-i$.
Comment: @MhenniBenghorbal Oh that is what you meant by branch points. Yes, I understand that part
Comment: @MhenniBenghorbal For part 2 of this question why exactly does it have branch cuts in the y-asix?
Answer: For your first question: To find a branch of $\log(z^2+1)$ means to find an analytic function $f$ such that $\exp(f(z)) = z^2+1$.  Here you haven't specified what the domain of $f$ should be, but presumably it should be as large as possible (and of course containing 0, which is stated in the problem).
Hint: Factor $z^2+1$ and use the standard properties of logarithms from the real numbers.  Even though these properties don't hold in all cases for complex logarithms, they will help you to guess the answer, which you can then prove directly by exponentiating, as I described in the answer to your other question.
For your second question: The principal branch does not work because for some values of $z$ in $|z|>1$ (for example $z=2i$), the value of $z^2-1$ will be a negative real number, where $Log$ is not defined.
Comment: Thanks Ted, but I still do not know how to do the first part. The $z$ is throwing me off. Can you give some more hints? Also, to the second question, what branch will work then if not the prinicpal?
Comment: What do you mean by "the $z$ is throwing me off" ?  How far did you get with the hint?
Comment: What I meant by that is I know how to find a branch of , e.g log(1) or log(i), but I haven't seen an example where z is in log. And I didn't get far at all with the hint.
Comment: In the other question you posted, you proved that $Log(-z) + i \pi$ is a branch of $\log(z)$, by showing that $\exp(Log(-z)+i\pi) = z$.  This is similar, only you're not given the function this time, you have to find it, and show that it exponentiates to $z^2+1$.
Comment: So I have to show this $\exp(Log(z^2 +1)$?
Comment: Also, I am still not understanding the second part. What if it were in the interior of the circle instead of the exterior: $|z|<1$?
Comment: $Log(z^2+1)$ is a branch of $\log(z^2+1)$, but the problem also says that the branch has to take the value $2 \pi i$ at $z=0$. Does $Log(z^2+1)$ at take the value $2 \pi i$ at $z=0$?  (No it doesn't) So you have to choose a different branch.
Comment: For the second part: If it were the interior, the principal branch $Log(z^2-1)$ still does not work because e.g. at $z=0$, the value of $z^2-1$ is a negative real number.
Comment: Does then $Log(z^2+1)$ take the value of $\pi i$ at z=0?
Comment: I hope I am not annoying you but if you dont mind Ted, I want to ask a more specific question to this because I really want to understand. For part 2, why exactly does it has branch cuts in the y-asix?
Answer: Let me give a simpler function so you can see what's going on. Let's consider the function $f(z)=\ln(z)$. We would like to find a branch where $\ln(z)$ is analytic at $z=1$ and has a value $2\pi i$ there. Now, you know that
$$ \ln(z)=\ln|z|+i(\theta+2k\pi) \longrightarrow  (1). $$
We want to find the branch of the function that has value $2\pi i$ at
$$z=1 \implies z=e^{0i} \implies \theta = 0 . $$
Substituting $\theta =0 $ in $(1)$, $z=1$, and choosing the right branch, in our case $k=1$, we have
$$ \ln(1)= 0 + i(0+2\pi)=2\pi i. $$
I hope this example makes it clear for you.
Comment: Thanks a lot! It does clear some issues. You got $\theta = 0$ because $ln(1)=0$, is that correct? Also, what exactly does it mean when you said has a value of $2\pi i$, I want to understand it intuitively ?
Comment: Ohh okay, I see now since $z=1$ iff $z = e^{\theta i}$ , where $\theta = 0$. Thanks a lot for clearing that up for me!! Last question, what exactly does it mean when you said has a value of 2πi? Does that mean the arg must equal $2πi$?
Comment: @Q.matin: It is the value of $\ln(z)$ at the point $z=1$.
Comment: @Q.matin: We get $\theta =0$ by writing the complex number $z$ in polar coordinates. In our case, the complex number is $z=1$. Remember that $z=|z|e^{i\theta}$.
Comment: Well, I take it that you assumed, in your text,  $-\pi<\theta<\pi$. Is it the case ?

Branches of analytic functions


Why does the melodic minor scale turn into natural minor when descending?

Question: I understand the construction of the scales; natural minor, harmonic minor (raises the 7th) and melodic minor (raises both 6&7 when ascending).
What I don't understand is why, when I play melodic minor descending, it flattens the 6&7 ,giving me the natural minor. So my questions is: Why is a descending melodic minor scale the same as a natural minor scale? Thanks.
Comment: That's just the way it is.  This particular construct yields music folks liked, so they gave it a special name.  You can build all sorts of nonstandard scales; they just aren't named.
Comment: Related [question](http://music.stackexchange.com/q/9798/2600).
Comment: Similar related question here (http://music.stackexchange.com/q/43363/16897)
Comment: Because natural minor is magical
Answer: The main "purpose" of the raised degrees in ascending melodic and harmonic minor is to create a strong leading note:
X:1
L:1/4
M:none
K:Am
V:1 clef=treble
e ^f "V₇ ↗"^g "i"a
%

Clearly, this leading tone only makes sense if you actually do resolve it upwards. If you go down instead, this can rather leave a feeling of unresolved tension.
X:1
L:1/4
M:none
K:Am
V:1 clef=treble
a "↗"^g "?"^f "??"e
%

In harmonic minor, this unresolvedness is not as apparent because the attention will be more drawn to the exotic (if not somewhat jarring) 1½ tone step:
X:1
L:1/4
M:none
K:Am
V:1 clef=treble
a (^g "!↘"f) e
%

Melodic minor avoids these 1½ tone steps to create smoother melody lines. When going up, you still want the leading tone, hence you raise the ⅶ and also the ⅵ. But when going down, it's more sensible to just leave both of them natural.
Of course that doesn't mean you can't use raised ⅵ and ⅶ degrees when going downwards. In particular if one voice already has one of these raised tones, it would be problematic to un-raise them in another voice. For example, the fugue that RRR brought up has this passage:
X:1
L:1/16
M:C
K:Am
%%score (T1 T2) (B1)
V:T1           clef=treble
V:T2           clef=treble
V:B1           clef=bass
% 1
[V:T1] (d8            d2 )
[V:T2] A,2dc   BA^G^F E2
[V:B1] G,2^F,E,F,2^G,2 A,2

Here, the bass is already going up in melodic minor to the A root, and especially if the bass does this you really want the leading note effect, so it is F♯-G♯-A. At the same time, the alto needs to cross the ⅵ / ⅶ territory to get down to the Ⅴ (e). Using a g-natural there would cause a pretty bad clash between the voices:
X:1
L:1/16
M:C
K:Am
%%score (T1 T2) (B1)
V:T1           clef=treble
V:T2           clef=treble
V:B1           clef=bass
% 1
[V:T1] (d8            d2 )
[V:T2] A,2dc   BA"⚡"GF E2
[V:B1] G,2^F,E,F,2^G,2 A,2

On alternative would be to use harmonic-descending there
X:1
L:1/16
M:C
K:Am
%%score (T1 T2) (B1)
V:T1           clef=treble
V:T2           clef=treble
V:B1           clef=bass
% 1
[V:T1] (d8            d2 )
[V:T2] A,2dc   BA^G"¿¡"F E2
[V:B1] G,2^F,E,F,2^G,2 A,2

but again, that sounds a bit Spanish. Especially in a middle voice it's more innocuous to just adapt to the context of melodic-minor from the bass voice there, even if it means traversing the scale in the "wrong" direction.
Comment: @RRR I don't think "every time you alter any note in the scale it changes the key" is a standard music theory viewpoint; if anything, it's the opposite: ideas like the "melodic minor" are an (arguably clumsy) attempt to allow variations in tonality *without* seeing every variation as a key change.
Comment: @RRR what you say may very well hold true for Jazz, but in classical music there's not really any such thing as "the chord set". Harmony arises from counterpoint between different voices, and what chords can be created this way is just a consequence of the notes used by each voice. And it's not necessarily possible to say what scale each single note belongs to; quite often an accidental is just used to cause a secondary leading effect without really shifting the tonal center. Especially for longer chromatic runs, it would be impractical to interpret every accidental as a key change.
Comment: @RRR ...whilst in pop/rock, you might have a fairly simple cyclic chord progression made up of a set of chords, the constituent notes of which wouldn't fit in the diatonic scale - but in many cases it wouldn't be common to analyse such a chord progression as involving key changes.
Comment: The problem with this standard answer is that every time you alter any note in the scale it changes the key. When the key changes the chord set changes. In Western music the scale always matches the chords.
Comment: @RRR that's depending on what you mean by "alter any note in the scale", of course. If it's a 'temporary' deviation, especially in a melody, that could just be a bit of melodic 'colour'. If you permanently alter a note, then you may be in a different key, but it would only be definitively seen as a key change if you'd changed the tonal centre of the piece too.
Comment: @topo morto the whole idea is that every time you alter any note in the scale it changes the key! So, every time you change a note you change the key. That's what key means. There's no variation in the note set without a change in the chord set. Otherwise scales have no meaning. You can't have it both ways. The Harmonic minor and the major and the melodic minor are distinct entities.  THey can be strung together horizontally but not vertically. Otherwise the result is nonsense.
Comment: @RRR are you saying that every accidental in a piece of music is a key in change?
Answer: Because it gives a smoother melodic line when running up and down the scale.  hence "melodic" minor.
Is this maybe arising from the common misconception that there's "a scale for a chord" and you're meant to stick to it?  Not so, and you've just discovered one example!
Answer: As already mentioned, a strong leading tone, one semitone lower than the root note, sounds better with a smaller change. This then made a tone and a half gap between notes 6 and 7, so 6 was also put up a semitone (from where it was in the natural minor). The natural minor contains all of the notes found in the relative major. Hence the same key sig. When, in early music, the melody went down, it sounded fine to use the notes from the natural minor, but ascending melodies were thought to be better sounding using the sharpened notes.
As an aside, all three minors use the same first 5 notes in their scales: natural then continues with notes from the relative major; harmonic continues with natural minor notes, except the raised leading tone; melodic continues with the parallel major notes ascending. There is also the jazz melodic, which favours the old ascending melodic notes both ways.
Modal minors are for another day...
Comment: ET?  The "call home" guy?
Comment: @Tim I just realised from your answer why some diplomat called it the Jazz Melodic, cos he didn't want to be bothered by loony ideologs. "Well ishnot YOR Melodic, ishda the Jazz Melodic, shee? na leeme alone!".
Comment: For a long time we were quite content with "modal" flattened sevenths.   Then, for a time, we became obsessed with dominants and tonics, which required the sharpened leading note.  Don't knock it, a WHOLE lot of excellent music came out of that system (and still does).
Comment: I'm not knocking anything! Of course it works (worked), and gave us music that sounded as it did. But so did tuning until ET came along. Would we revert? Probably not, generally speaking. Or did I miss the point?
Comment: @Laurence I believe Tim means "Equal Temperament," as distinguished from "Well Temperament" e.g., in "The Well-Tempered Clavier" (q.v.).
Comment: @RRR possibly the basis of most advances in theory! :)
Answer: Another way to look at things (popular in the Common Practice Period) is to consider that each key has two modes, major, and minor. The most important (or commonly used) notes in a major key are the usual diatonic notes (like C,D,E,F,G,A,B,...) when written with the key note as 1. A minor key has a minor third (C,D,Eb,F,G...) and two mutable tones on steps 6 and 7. Both the "natural" versions (Ab, Bb) and the "raised" versions (A, B) are for the most part considered diatonic to the key. Historically, the major mode seems to have come from the Lydian and Mixolydian modes. The minor mode comes from the Dorian and Phrygian modes. Fashions in cadential and melodic procedures lead the major and minor modes. The two most obvious are the raised seventh degree at a cadence and the avoidance of an augmented second between the usual sixth and raised seventh scale degrees, especially in vocal music.
The use of raised steps in ascending and lowed steps in descending passages is a reasonable rule, but it really only holds when the passage has tonic-oriented underlying harmony (like c-minor chords in the key of c-minor.) With subtonic-oriented harmony, the tendency is to used both scale steps in their lowered form (Ab and Bb against an F minor chord). Similarly, the raised scales steps tend to be preferred against dominant harmony (A and B vs a G or G7 chord.)
There are several other exceptional procedures; the most common is to ignore any of the above procedures for color reasons. It sounds a bit more exotic to use the augmented second. This is no problem for instrumental music. Also, the Ab-B scale step is often used against a dominant harmony with a dominant ninth chord (G9 in the key of C). Arpeggiating a dominant ninth chord uses F,G,Ab,B, four succsive scale steps. The raised 6 step is rarely (if ever) used as an upper neighbor the fifth step (Ab is the neighbor to G). (I'm not sure about lower neighbors to the seventh step, even in a major key.)
There are several books that describe these developments. They do not all agree with each other.
Joel Lester: "Between Modes and Keys"
Joel Lester: "Compositional Theory in the Eighteenth Century"
Carl Dahlhaus: "Studies on the Origin of Harmonic Tonality"
These and a search through the Internet (and Google Scholar) can be helpful.
Answer: It doesn't.

(from The Chord Scale Theory & Jazz Harmony by B. Nettles and R. Graf / Berklee College of Music)
As you can see from the third bar of this wonderful example by @Matt L. it descends the same way as it ascends, or it ain't the Melodic Minor! (It's usualy the Aeolian i.e. the Relative Major.)
If Am "Melodic" "descends" as A,G,F,E,D,C,B,A then thats the Aeolian. (i'm going to call it A Aeolian, but you can call it C Aeolian if you like.)
A Aeolian is the same as the key of C Major. The key of C major is not the same as the key of A Melodic Minor. Thats a key change. You aren't in A Melodic Minor at that point. End of story.
For the purposes of any kind of basic improvisation at all the Melodic Minor must be played descending the same way as ascending, (A,G#,F#,E,D,C,B,A) as J.S. Bach shows us.
Very obviously, from any point of view, the basic baby A Melodic Minor chord set:
---Am, Bm, C+, D, E(7), F# dim, G# dim
has absolutely nothing to do with the set of notes:
A,B,C,D,E,F,G
And you know what? Playing them in the other direction isn't gonna make them work either!
And if you're not intending to improvise, how can practicing two completely different sounds and somehow "pretending" that they have anything to do with each other help a child, let alone an adult?
Where on earth did this misleading musical pedagogy come from? I'm not being rhetorical here, I'd really like to know. I've asked dozens of top musicians and they all shrug and admit that it's daft, but oy vey!
So gentle seeker, breath a sigh of relief. You have been lied to.
It's not a question. The question is moot. It's two different things, and music teachers be crazy!!
For a hilarous example of the above, which descends, slowly but inevitably,  (as this thread well might) into an appauling cat fight, check out
[http://www.abrsm.org/forum/index.php?showtopic=54590]
Enjoy!
Comment: I disagree with your conclusion – it's generally _not_ wrong to naturalise the ⅵ and ⅶ when going down, in fact it usually makes sense to do this – but +1 for bringing up an example where the circumstances indeed make it necessary to keep these degrees raised while going down. I added a discussion of that to my answer.
Comment: I got hauled over the coals for calling melodic minor a key. However, I'm with you all the way (except mode naming!). It's the old adage - if it sounds good... But someone had to make up a rule for it, didn't they?
Comment: @Tim WHO hauled you over the coals? And whats your beef with mode nameing ? Do you mean that I should call It C Aeolian in this  example? I just added a link to the post. it's fun.
Comment: @Tim What does the number by my name mean? it keeps going up!!??
Comment: @RRR http://meta.stackexchange.com/questions/7237/how-does-reputation-work, assuming you're not just showing false modesty :)
Comment: @topo morto thanks and er ...blimey! how Trumpesque! Positively Byzantine! (the scheme not your comment). Obviously designed by and for males only. Don't worry, I have no modesty whatsoever, false or otherwise.
Comment: @RRR the scheme is just designed to give a little bit of emotional validation to the dreadful types of people you will meet on these stack exchange sites. Pay as much or little heed to it as you will.
Comment: @topo morto lol "dreadful types of people"! DEPLORABLES. Napoleon invented medals for when there was no booty. GIVE US CASH!
Comment: @leftaroundabout It's not WRONG to lower the 7 and 6. It's merely not Melodic! It's Aeolian!  (And If you just lower the 7 it's Dorian).  I invite you to show me one single example of what you refer to from any where at all that's NOT patently Aeolian. Proove it. You proove it, I'll thank you for showing me the way.
Comment: @RRR yes, it is Aeolian. Melodic-minor is just a variation of Aeolian after all, at least in baroque / classical music. In impressionism, Jazz etc., melodic-minor is also used in a different sense that has more to do with modes / chords like you say, but that's quite difficult from the classical notion of melodic-minor (in fact I'd rather call this different, as it has little to do with melody).
Answer: The main purpose of the raised sixth scale degree was because, for the longest time in melody writing, writers wanted to avoid the augmented second interval between the sixth and seventh scale degrees.
So the theorist was left with a dilemma, you want a strong raised leading tone so that the Dominant to Tonic resolution in the cadences but still this augmented second led to unsatisfactory melodies.
So they came up with the idea of raising the sixth note of the minor scale as well to solve these problems they were having. The melodic minor has its roots very much in melody writing.
When your line is descending then you can have a minor seventh and then also you would not want a raised sixth when your scale is descending, so that is how we came up with the natural minor form (ie a minor scale that has all the notes of the relative Major.)
Answer: This "why" may be a question with different edges

The convention of the name "melodic minor scale" is that, a convention that surely appeared at some point in the history of musical theory; probably it appeared first in a book and we would have to do an historical study on the author, how music was conceived at that time, and so on. On this, there is a tendency to now call the "melodic minor scale" the scale that rises and falls in the same way, and that could be the convention of this time, but we always need to consider that traditionally the minor melodic scale went up as a melodic minor scale and went down as a natural minor scale.
Another thing may be the "why", as aesthetic reason, of using "melodic minor scale" with that decrease other than its rise. Well, each style has its followers and detractors, I prefer the aesthetics of the harmonic minor scale, but I understand that the second augmented, is very striking, very different from the rest of seconds and probably at that time  they did not want a second That emphasizes so much, in both cases conserving the vii as leading tone (not sub tonic), of course.
Comment: Jazz melodic is how it's often named. Same as ascending classical, both ways.
Comment: Exact!, that "often named" is what i called "a convention"
Answer: Since major scale and natural minor scale were the two modes in the modal system, I postulate that the melodic minor scale was accidentally found by lower the 3rd a half step. Take C major scale for example, if you were just only flatting one-note, the E a half step to Eb, you're automatically in the C melodic minor scale ascending (let's just ignore about the 3 flats key signature). There was no need to raise the 6th and the 7th for they were already there, to begin with. We just follow the interval distances to form other melodic minor scales.
This comes to the 2nd postulation of mine about why do we have the natural minor scale descending in the melodic minor scale. If someone sings a full C melodic minor scale, with the descending staying as exactly as the ascending intervals, the natural chance of singing the C major scale will be like 98% when descending because we get used to the Fa-Mi Ti-Do of a major scale, it is quite difficult (Not impossible, but hard) to sing the note in Eb! Try it


Pendulum with moving support

Question: Consider a mass $m$ connected to an oscillating pivot by a massless rod of fixed length $l$. The pivot is located at $(x_0(t), 0)$, where $x_0(t)=A_0\cos(\omega_0t)$. The $y$-axis is positive downwards. The angle between $y$ and the rod is $\theta(t)$. $\theta$ is the only generalized coordinate.
I have a hard time deriving the kinetic energy, which should be $T=\frac{1}{2}m\left(l^2\dot{\theta}^2+2l\dot{\theta}\dot{x_0}\cos\theta+\dot{x}_0^2\right)$.
Here's my thinking:
$$v_{rod}=l\dot{\theta}$$
$$v_{pivot}=\dot{r}_{pivot}$$
$r_{pivot}=x_0\implies\dot{r}_{pivot}=\dot{x}_0$
So $$v_{total}^2=\left(l\dot{\theta}+\dot{x}_0\right)^2$$
But something doesn't add up. I think that something is wrong with the velocity equation for the pivot, because if $\vec{r}_{pivot}=r\hat{r}$ then it somehow depends on $\theta$, but the angle of the moving pivot with the $y$-axis is always fixed. I'm quite confused. Would appreciate some help.
Comment: "if $\vec{r}_{pivot}=r\hat{r}$" ... What are $r$ and $\hat r$ (this is the first mention of either of those symbols), and why should $\vec{r}_{pivot}$ equal their product?
Comment: This is just a standard convention for polar coordinates $(r,\theta)$. $r$ is $\lVert \vec{r} \rVert$, $\hat{r}$ is the unit base vector, hence $\vec{r}=r\hat{r}$.
Answer: For me, the first step is to make a good sketch, then write the position vector and check it against the sketch.  Carefully checking it for various angles is so important.  When $\vec{r}(t)$ is correct, then take the derivative.
$$\vec{r} = \vec{r}_{pivot} + \vec{r}_{rod} \\
\vec{r}(t) = x_0(t) \hat{i} + (L\sin\theta(t) \hat{i} + L\cos\theta(t) \hat{j})  \\
\vec{v}(t) = [\dot{x}_0(t) + L\dot{\theta}\cos\theta(t)] \hat{i} - L\dot{\theta}\sin\theta(t) \hat{j} \\
\vec{v}\cdot\vec{v} = (\dot{x}_0 + L\dot{\theta}\cos\theta)^2 + L^2\dot{\theta}^2\sin^2\theta \\
v^2 = \dot{x}_0^2 + 2L\dot{x}_0\dot{\theta}\cos\theta + L^2\dot{\theta}^2$$
Having a good expression for the position vector also comes in handy for writing the potential energy function, $PE=-\vec{F}_{gravity}\cdot\vec{r}$.
Clarifications:
The position of the mass $m$ is given by $\vec{r}$, which always points from the origin to the mass at the end of the pendulum rod.  There is also a vector $\vec{r}_{pivot}$ that always points from the origin to the pivot point of the pendulum.  The position of the pivot is given as always on the $x$-axis at a distance $x_0(t)$ from the origin, so $\vec{r}_{pivot}=x_0(t)\hat{i}$
The vector $\vec{r}_{rod}$ always points along the pendulum rod from the pivot point to the mass no matter what angle, $\theta$, the rod makes with the vertical.  The components of $\vec{r}_{rod}$ are found by projecting $\vec{r}_{rod}$ onto the horizontal and vertical axes.  The length of $\vec{r}_{rod}$ is always $L$.  By our choice of $\theta$, the vertical projection is $L\cos\theta$ and the horizontal projection is $L\sin\theta$.
$\theta$ is the angle that the rod makes with the vertical line through the pivot point.  This is an important point for two reasons.  First, $\theta$ is often used as the polar coordinate about the origin, but here it is a generalized coordinate that indicates the rotation of the pendulum rod about its pivot point. Second, we have chosen $\theta$ so that it is zero when the pendulum is in equilibrium.  We could have used a different angle, such as the angle $\phi$ between the rod and horizontal, say, but then we would have a different expression for $v^2$.
Thus, the vector from the origin to the mass is the sum of the vector from the origin to the pivot and the vector from the pivot to the mass.
As for the formula for the potential energy, let $\vec{F}$ be the gravitational force on mass $m$ at position $\vec{r}$ in a uniform gravitational field.  We can calculate the force two ways.  One is simply $\vec{F}=mg\hat{j}$, with the unit vector $\hat{j}$ pointing down along the positive $y$-axis.  The other is $\vec{F}=-\vec{\nabla}{V(\vec{r})}$, where $V(\vec{r})$ is the potential energy function.  Since the gravitational field is uniform, $\vec{F}$ has no $\vec{r}$ dependence.  It is not difficult to let $V(\vec{r})=-\vec{F}\cdot\vec{r}$ and verify that $$-\vec{\nabla}{V(\vec{r})} = -\vec{\nabla}{(-\vec{F} \cdot \vec{r})}=(\vec{F} \cdot \vec{\nabla})\vec{r} = \vec{F} $$
Therefore $V(\vec{r}) = -\vec{F} \cdot \vec{r}$ satisfies our one requirement for it to be the potential energy function.  When we have a complicated expression for $\vec{r}$ and we need the potential energy, this is a very handy formula.  Two warnings, however:  First, our force $\vec{F}$ must be the same everywhere, no $\vec{r}$ dependence, which is often the case, for this formula to be valid.  Second, the potential energy is not a unique quantity, so we might not get the exact same function as someone else, the difference usually being an additive constant.  We are free to add or subtract any constant we want to our potential energy.
Comment: The simplest way to see it is to use the usual (x,y,z) coordinates:
$(\vec{F}\cdot\nabla)\vec{r}=(F_x\frac{\partial}{\partial x} + F_y\frac{\partial}{\partial y} + F_z\frac{\partial}{\partial z})(x\hat{i} + y\hat{j} + z\hat{k})=F_x\hat{i}+F_y\hat{j}+F_z\hat{k}=\vec{F}$
Comment: Can you please clarify why $\vec{r}=\vec{r}_{pivot}+\vec{r}_{rod}$? What does this $\vec{r}$ mean and how is it justified? Also, why is $PE=-\vec{F}_{gravity}\cdot\vec{r}$?
Comment: Thank you for your detailed reply. Why is $(\vec{F}\cdot\nabla)\vec{r}=\vec{F}$?
Answer: The definition of velocity has to allow an object (in this case the pivot) to move only along a straight line through the origin. In fact you have the correct magnitude for the velocity of the pivot; it is $\dot x_0$.
Both the position and the velocity of the pivot
do indeed have directions as well as magnitudes,
but the directions are always horizontal.
To properly understand the other angles and directions in this problem,
however, it pays to have a picture.
The figure below is consistent with the way this problem apparently was
intended to be solved.

In this figure the $x$ axis is directed to the right and the
angle between the $y$ axis and the pendulum is measured counterclockwise
from the $y$ axis. The pendulum is shown as a solid red line segment,
with a large black dot at the end representing the mass.
At the instant of time represented in this figure, $t$, the pivot is
at position $(x_0(t))\hat x$ relative to the origin.
Now consider what happens during a very short period of time $\Delta t$
after time $t$.
In that time the pendulum rotates through an angle $\Delta\theta$;
let $\Delta t$ be small enough so that
$\frac{\Delta\theta}{\Delta t} \approx \dot\theta$,
and so that $\Delta\theta$ also is small enough to say that this
rotation displaces the mass by a distance of approximately
$\ell \Delta\theta$, approximately perpendicular to the pendulum rod.
That displacement vector is represented by the black arrow whose tail
is at the mass.
In addition to the displacement of the mass corresponding to the
rotation of the pendulum, however, the pivot also moves a distance
$\Delta x_0$ to the right, where
$\frac{\Delta x_0}{\Delta t} \approx \dot x_0$.
Since we have already supposed that the pendulum will have rotated to
angle $\theta + \Delta\theta$ at time $t + \Delta t$,
the motion of the pivot means the entire pendulum (including the mass
at the end) moves a distance $\Delta x_0$ to the right.
This movement is represented by the two horizontal black arrows,
one along the $x$ axis and the other whose tail is at the head of the
arrow we obtained from the pendulum's rotation.
The movement of the mass from time $t$ to time $t + \Delta t$
is the composition of these two motions, that is, the vector sum
of the motion due to the rotation
(a vector of length approximately $\ell \dot\theta \Delta t$ at an angle
approximately $\theta$ counterclockwise from the $x$ axis)
and the motion due to the moving pivot
(a vector of length $\Delta x_0$ parallel to the $x$ axis).
The vector sum is shown as a longer purple vector with its
tail at the position of the mass.
You could also get the same vector sum by moving the entire pendulum
$\Delta x_0$ to the right and then rotating it $\Delta \theta$.
As we let $\Delta t$ shrink toward zero, the approximations in the figure
become better. In the limit, the average velocity of the mass during this
timestep approaches the instantaneous velocity of the mass,
which is the vector sum of two vectors, one of magnitude $\ell\dot\theta$
at angle $\theta$ with the $x$ axis, and one of magnitude $\dot x_0$
parallel to the $x$ axis.
When actually working a problem like this, I would usually skip all the
"small timestep" and "approaching zero" business and simply write the
velocity of the mass as the vector sum described at the end of the
previous paragraph. All the $\Delta$s and dashed red lines are merely
there to try to make the leap to that vector sum a little less mysterious.
The magnitude of the mass's velocity according to your
computation, $v_\text{total} = \ell\dot\theta + \dot x_0$,
is actually the correct value when $\theta = 0$, because
in that case the two vectors in the vector sum are parallel.
In every other case, however, the magnitude of the velocity
will be less than $\ell\dot\theta + \dot x_0$.
You can find the correct magnitude by applying the Law of Cosines
to the triangle representing the summation of the motion vectors,
keeping in mind that $\theta$ is the exterior angle
of the sides of length $\ell\dot\theta$ and $\dot x_0$.
Alternatively, you can work out the $x$ and $y$ components of the
rotational part of the velocity,
$\vec v_\text{rod} = \ell\dot\theta(\cos\theta)\hat x
  - \ell\dot\theta(\sin\theta)\hat y$,
add this componentwise to the velocity of the pivot, $\dot x_0 \hat x$,
and then find the magnitude of the resulting vector from its
$x$ and $y$ components.

There is actually a bit of ambiguity in the problem statement.
We are not told which way the $x$ axis is directed (left or right),
nor the direction in which the angle $\theta$ is measured.
In the figure, I chose to orient $\hat x$ and $\theta$ in such a way
that when $x_0$ and $\theta$ simultaneously increase, the mass moves
farther than it would by making the same increase in either coordinate
without changing the other.
If I had reversed the direction of the $x$ axis or the direction in which
$\theta$ is measured (but not both), an increase in $x_0$ and an increase in $\theta$ would have partially canceled each other; the kinetic energy then
would have been
$\frac12 m(\ell^2\dot\theta^2 - 2\ell\dot\theta\dot x_0\cos\theta
  + \dot x_0^2)$
instead of
$\frac12 m(\ell^2\dot\theta^2 + 2\ell\dot\theta\dot x_0\cos\theta
  + \dot x_0^2)$.
The difference is that one of the factors $\dot\theta$
or $\dot x_0$ in the middle term would have changed sign.
Answer: The x coordinate of the mass:
$$x(t)=x_0(t)+l\sin\theta$$
The y coordinate
$$y(t)=l\cos \theta$$
So
$$\dot x(t)=\dot x_0(t)+l\dot \theta\cos\theta $$
$$\dot y(t)=-l\dot \theta\sin \theta$$
$$T=\frac{1}{2}m(\dot x^2+\dot y^2)$$

Pendulum with moving support


Does the exact same load use the same amount of power on different processors?

Question: Let's say that we have a set workload doing a set amount of work.
If I have two identical servers, one equipped with a E5-2660 and one with a E5-2670 and otherwise identical hardware, will the total amount of power consumed by the system while processing this workload (regarded as work units/second, not a total amount of work to be done) differ?
If we compare these very similar processors we see the following differences:
E5-2660: Clock speed 2.2/3GHz, TDP 95W
E5-2670: Clock speed 2.6/3.3GHz, TDP 115W
It certainly makes sense that we can load up the E5-2670 with more load and it will thus have a higher TDP, but if we're only loading it up to:

0%
1000 transactions/second (arbitrary workload value mentioned above)
100% of the capacity of a E5-2660

then what should I expect the actual wattage draw of the system to be (when compared with the E5-2660 system)?

Available frequencies:

E5-2660: 2201000 2200000 2100000 2000000 1900000 1800000 1700000 1600000 1500000 1400000 1300000 1200000
E5-2670: 2601000 2600000 2500000 2400000 2300000 2200000 2100000 2000000 1900000 1800000 1700000 1600000 1500000 1400000 1300000 1200000

Turbo limits:

E5-2660: Multiplier 99.96MHz
Maximum turbo limit with 1/2/3/4/5/6 cores active is 30/30/29/29/28/28
E5-2670: Multiplier 99.73MHz
Maximum turbo limit with 1/2/3/4/5/6 cores active is 33/33/32/32/31/31

Running idle with the performance profile and the ondemand governor means both types of processor (on RHEL6) clock down to 1.2MHz and spend 50% of their time in C1.
Answer: I think that will depend on so many different things that the only reasonable answer is "try it". Some things that could make a difference:

Is the branch prediction significantly better in one of them?
Is the layout of the pipeline different?
Is the size of the L1 cache the same?
Do various internal paths and paths to buses differ?
Does the OS do power management better for one of them for some reason?

The pages you linked to mention no such details, so set up a lab and try it out :)
Comment: Well, the pages at least say that the L1 cache is the same. I'd lay good money on the rest of them being the same given that they are very nearly the same processor in the same family, just with different multipliers.
Answer: Even within batches of the same processor, there will be differences in power load.
In the overclocking community it's widely known that you can get "good" CPUs and "bad" CPUs. The bad ones generate a lot more heat under load (even at stock speeds) and thus will fail more quickly, thus limiting how much you can overclock them.
This is generic, standard CPU binning:

All processors start out from the same  line. Unlike what some may think, chip makers do not have dedicated lines for processors of different speed grades. Instead, they have a single line from which all processors of the same model are made. Even then, these chips will not turn out exactly the same.
At the end of the fabrication process, some chips will invariably be non-functioning or malfunctioning. Those that work though will not have the same characteristics. Some will be able to run faster while others can only run at a lower clock speed. However, it would take an extraordinarily long time to test each and every chip and discover their maximum potential.
To save time, chip makers use a method called "speed-binning". Instead of testing and selling chips at their maximum clock speed, they test and sell chips at certain speed grades - like 1GHz, 1.2GHz, etc. This is one of the reasons why overclocking is possible.
Suppose a chip maker sells a particular processor at 3 different speed grades - 1GHz, 1.2GHz and 1.4GHz. After every processor is made, it is first tested at 1.4GHz. If it passes, then it's labelled and sold as a 1.4GHz processor. If it fails at that clock speed, it's retested at 1.2GHz and labelled as a 1.2GHz processor if it passes. Otherwise, it's tested at 1GHz. If the processor fails at that speed, then it's discarded.

In addition to that, as I mentioned above, now that all the voltage regulation stuff is on-die, CPU manufacturers can easily tell CPUs "hey, you're not quite working at 1.2 Ghz but we really want you to. Turn up your voltage a little."
Even at the same clocks, Intel may set the on-die voltage to a higher level to compensate for minor manufacturing defects. So if you get a CPU that has a relatively high factory default voltage, it's going to produce more heat and use more power to get to the same clock speeds.
You might be able to view the factory voltage settings of your CPUs via something like CPU-Z.
Comment: Noted. In this particular case, the load would be across 20,000 processors so variations within a batch should even out, but statistical small differences could potentially have a large impact. Another consideration brought up by someone I asked about this is that if the E5-2670 is clocking up higher yet running for a shorter period of time, the consumption may follow a square law resulting in a net power consumption increase.


Comparing mclust() and k-means centroids

Question: I have some code that looks for clusters in x,y data. To check the number of clusters I use, I want to get the BIC. This is not possible (easily) using kmeans(), and so I've switched to the mclust package. Specifically, I'm trying to replace kmeans() from the R stats package, with Mclust() from the mclust package.
Using Mclust() requires me to specify which model should be used for the clustering. According to ?Mclust, the following models can be used in Mclust():
univariate mixture
"E"  =   equal variance (one-dimensional)
"V"  =   variable variance (one-dimensional)
multivariate mixture
"EII"    =   spherical, equal volume
"VII"    =   spherical, unequal volume
"EEI"    =   diagonal, equal volume and shape
"VEI"    =   diagonal, varying volume, equal shape
"EVI"    =   diagonal, equal volume, varying shape
"VVI"    =   diagonal, varying volume and shape
"EEE"    =   ellipsoidal, equal volume, shape, and orientation
"EEV"    =   ellipsoidal, equal volume and equal shape
"VEV"    =   ellipsoidal, equal shape
"VVV"    =   ellipsoidal, varying volume, shape, and orientation
single component
"X"  =   univariate normal
"XII"    =   spherical multivariate normal
"XXI"    =   diagonal multivariate normal
"XXX"    =   ellipsoidal multivariate normal

I'm presuming that k-means in stats is a "spherical, unequal volume" model, ie. to get k-means(x = data, centers = 6) to match mclust(), I should use mclust(data, G = 6, modelNames = c("VII")).
However, in the limited tests I've done, this gives different cluster centroids. The example below uses 6 clusters with some test data. The centroids obtained through each method are shown.

Can anyone confirm which mclust() model is equivalent to kmeans()?
Comment: I doubt that there can be any equivalence. `mclust` presents normal-model-based EM clustering, wherease k-means is not dependent on any type of the distribution, it is not model-based. These are different algorithms.
Comment: I hadn't realised/noticed that `Mclust()` uses gaussian mixture modeling. Therefore there's no way the two methods are really comparable. I'll leave this question here for people that make the same mistake...
Answer: Gaussian Mixture Modeling is not the same as k-means.
None of the models has a 1:1 correspondence to k-means. The closes is probably
"EII"    =   spherical, equal volume

but Mclust will still use a soft assignment, whereas k-means uses a hard assignment. There is a closer relationship between EII and fuzzy c-means (although I don't think they are the same either, due to different weighting functions).
Also note that k-means is commonly randomly seeded, so different runs of k-means will often yield different results, too!
Comment: I completely missed the bit about `Mclust()` using GMM. If I'd seen that (or not been so infatuated with the fact that it reported BIC), I'd never have posted this question...


Hyper-V 2012 Cluster with only one DC

Question: What happens with a Hyper-V 2012 Cluster with only one DC, if the DC suddenly goes down?
Would the Hyper-V Cluster works after a reboot, if the DC is missing?
Or should we have two DCs?
Comment: You should *always* have two DCs as a best practice. Bad Things™ happen if you lose it.
Answer: If your only domain controller goes down, you will not be able to authenticate to the domain or access configuration data stored therein. Things such as the cluster service account will be unable to log on. Your domain controller is probably hosting DNS too, which you're also going to need to stay up if you want a happy cluster. The list goes on.
You always want at least two domain controllers in your domain if at all possible. And at least one of those DCs needs to be outside of your cluster.
Comment: Thanks. Is it okay to host one of the DCs on the cluster as a VM? Or should it be running at the host it selfs.
Comment: No do not put Active Directory in the parent partition of a Hyper-V host. No other roles should be installed on a Hyper-V host. But yes it is OK to have one of your DCs as a VM in the cluster. I just recommend a physical one that is outside cluster in case one or both of the cluster nodes blows up for some reason. Even an inexpensive, 1U entry level HP would be fine.
Comment: I'm not saying that you cannot technically install AD and Hyper-V on the same machine, I'm just saying that it's not recommended. The idea is to keep your infrastructure as modular as possible, so that if you suffer a hardware failure one machine, you don't lose everything.
Answer: AFAIK, cluster bootstrapping in Windows Server 2012 allows an existing cluster to not only boot but to operate without a Domain Controller being available. An available DC is required to create a cluster but once it is created the cluster can boot, operate and be joined without a DC being available. Microsoft also removed the AD dependency from Cluster Shared Volumes so that your CSV can be mounted and accessed without an available DC.
In Windows Server 2012 Microsoft has removed or remediated many of the "chicken or the egg" dependencies related to the Cluster and CSV dependency on AD.
I can't speak to Windows Server prior to 2012 but in Windows Server 2012 the Cluster service runs as Local System.
Comment: Upvoting you because I was thinking about speaking to this exact topic, but, well you want a DC up anyway, right? :)
Comment: I agree but I did want to point out the improvements made in Windows Server 2012 related to AD dependencies and failover clusters.
Answer:
Would the Hyper-V Cluster works after a reboot, if the DC is missing?

How can it, given that the Cluster configuration data is stored on the DC. YOu would not be the first to run into a boot problem (though most people were "smarter" and put the DC's onto VM's in the cluster - same result on a reboot).
ALWAYS have at least 2 DC's.
Answer: I completely agree with the above, always have at least 2 DCs. That being said, Microsoft introduced some new capabilities in Windows 2012 Failover Clustering to make it less reliable on DCs during startup of the cluster services. This will enable a failover cluster to boot to an operational state without having access to a DC.
This is described here:
http://blogs.technet.com/b/wincat/archive/2012/08/29/windows-server-2012-failover-cluster-enhanced-integration-with-active-directory-ad.aspx (Search for "AD-less Cluster Bootstrapping")
Comment: It almost sounds like the new 2012 cluster, is build to run all DCs on VMs. But all other posts here, do not sounds like that.
Comment: All I'm saying is what Microsoft is saying :-) Personally I would always go with minimum 2 DCs per site. The cost is minimal compared to the resiliency you achieve. Just be sure to run the DC VMs on separate nodes in your cluster.

Hyper-V 2012 Cluster with only one DC


Looking for first course textbooks on probability and statistics for math majors

Question: I am taking a probability and statistics course soon and would like to find a text book that is targeted more towards math majors rather than engineers (which is what this class is).
The book my class will officially be using is Probability and Statistics for Engineering and the Sciences by Devore (ISBN-13: 978-0538733526). To be clear I have not seen the book yet but for now I am assuming it is not what I am looking for.
To give an idea of mathematical maturity and (possibly) relevant background by the time I plan to really start looking through whatever book I end up getting I will be through the integration chapter of Rudin's Principles (ch6), be through a course of point set topology (Topology and Groupoids), and I have familiarity with Linear Algebra (I have been learning from Course of linear algebra and multidimensional geometry by Ruslan Sharipov and getting exercises from various books).
I don't have any real specification other than it doesn't focus on ad-hoc calculation skills and has a good amount of theory. Also the book should be a first course book, so it shouldn't assume previous probably and statistics classes. Also the book could be probability or statistics(not necessarily both).  Here are some things that would be nice to have but not necessary:  a book that covers a lot of the material from the book that will be used in the class; a book that has special topics or applications to things like number theory or combinatorics (basically anything separate from probability and statistics); not greater than 500 pages; reasonably priced.
There are already many questions that have asked for probability or statistics books, but it is not obvious from the questions and answers (that I have seen) that any of those books fit what I am looking for. It looks like this answer might have what I am looking for but it is hard to tell and its expensive.
Edit:
What am looking for is not necessarily a replacement for this class, I just figure that I might as well learn some probability or statistics at a more sophisticated level while I am taking the required class that I suspect is more focused on calculations.
Comment: Though it doesn't contain applications, I like Sheldon Ross' *A First Course in Probability*.
Comment: North American books with titles that include both *probability* and *statistics* are designed for a particular kind of course. They can be good, but are not designed for somebody with your background.
Comment: The Devore book has been around a long time, I used it quite a number of years ago. It is good of its kind,
Comment: @AndréNicolas I figured that is the case. I guess I should put a little more emphasis that the text book I am looking for is not necessarily a replacement for this class, I just figure that I mine as well actually learn some probability or statistics while I am taking the required class.
Comment: Does this answer your question? [Recommend a statistics fundamentals book](https://math.stackexchange.com/questions/6254/recommend-a-statistics-fundamentals-book)
Comment: I'd highly recommend supplementing a first course in probability theory with a first course in information theory, which is more or less "the logarithm of probability theory".  [Cover and Thomas](http://staff.ustc.edu.cn/~cgong821/Wiley.Interscience.Elements.of.Information.Theory.Jul.2006.eBook-DDU.pdf) is a fantastic reference for that.
Comment: Does this answer your question? [What is the best book to learn probability?](https://math.stackexchange.com/questions/31838/what-is-the-best-book-to-learn-probability)
Answer: I have answered some similar questions earlier, so give references for those.
Se my answer to
https://stats.stackexchange.com/questions/63760/inference-for-the-skeptical-but-not-math-averse-reader/63820#63820
but maybe my answer to this one
https://stats.stackexchange.com/questions/38082/frequentist-statistics-references-for-someone-well-versed-in-modern-probability/38084#38084
is even more relevant!