trace ftp file data in wireshark

Question: I am trying to analyze ftp traffic in Wireshark. I can see commands like Request: List, PORT.
But after FTP
Response 150: Opening data channel for directory list.
what I see immediately is
Response 226: Transfer OK

where is a list of files? Why I don't see it? (I've got this list - I can see it in terminal)
Answer: You're following a single TCP connection. FTP data transfers occur over a second connection. Stop following the connection (or filtering) and you should see the data transfer, assuming you didn't use capture filters and actually captured it.
Comment: It sounds like you need to go read how the protocol works.
Comment: I tried this way but cannot find it. What filters might help? I tried by port 21 and second port of connection
Comment: As I already said, you'll miss it if you filter by port 21. Try not filtering at all, or filtering by IP address.
Comment: I ve found, forget that data comes from port 20. One question: this stream comes to also different port on ftp client but I cannot find where client passed this info to FTP server. Is this that number passed with Port request?
Comment: It might be more complicated in this case because it goes from my machine through TP-Link to my server and back. In FTP-Data frame I see  TP-Link as source, not server which is behind TP-Link
Comment: That's right, because the frame (layer 2) comes from your router (TP-Link).
Answer: In wireshark, use the display filters as -

ftp || ftp-data

This should allow you to see both, the file listing and the commands.
Make sure you don't have any capture filters.

trace ftp file data in wireshark


Can't find an already created Azure-SSIS Integration runtime in linked service configuration (Azure Data Factory)

Question: Azure Admin has prepared an SSIS IR that can access a firewall protected Azure SQL Database. I'm trying to configure that linked service using this designated IR but not finding the IR in the "Connect via integration runtime" drop down menu :

Available IRs :
Answer: In the linked service connect via property, you can only use the Azure integration runtime or self-hosted integration runtime. There is no option to select Azure-SSIS IR in the linked service
Azure SSIS integration is to create an SSISDB instance in your selected database server and deploy packages here. You can schedule to run these packages from the Azure data factory.

Refer to links 1 & 2 for more information on Azure-SSIS Integration runtime.


Qual a diferença entre uma string definida dentro de um arquivo texto e outra definida como uma variável?

Question: Imagine o seguinte:
texto = "They fly quickly over the snow in their sledges; the motion is pleasant, and, in my opinion, far more agreeable than that of an English stagecoach"

texto1 = open("sample.txt")

onde sample.txt tem o mesmo conteúdo da variável texto acima
Ao fazer
for i in texto:
     print(i)

é impresso caractere a caractere.
Ao contrário de:
for i in texto1:
     print(i)

O texto completo é impresso. Por quê?
Answer: A diferença é o tipo do objeto que você está iterando.
No primeiro exemplo o seu laço de repetição está iterando sobre uma string, que quando iterado gera como saída os caracteres um a um. Esse comportamento é definido na própria API em C do Python, como especificação da linguagem.
Já no segundo exemplo o laço de repetição está iterando sobre o retorno da função open, que é um objeto file-like; ou seja, um objeto que implementa a classe io.IOBase, que devida a sua implementação é um objeto iterável que gera a cada iteração uma linha do conteúdo.
Ou seja, no primeiro você itera apenas sobre uma string simples, enquanto no segundo você itera sobre um arquivo, linha a linha. Como o seu arquivo só tem uma linha, o conteúdo inteiro é exibido na primeira iteração.
Para chegar em uma equivalência entre os dois exemplos, no segundo você primeiro deveria carregar todo o conteúdo em memória, na forma de string.
texto1 = open('sample.txt').read()
# O método read lê o arquivo inteiro

for i in texto1:
    print(i)

Comment: pergutnas fáceis, mas completas em que não precisa dar uma volta  e explicar o básico da programação são coisa em falta por aqui - -fica todo mundo em cima.  :-D
Comment: Foi mais veloz que eu... :(
Comment: Mas olha aí, até o jsbueno se manifestou, não era só eu então. :D
Comment: Woss, tu foi muito rápido kkk Ta doido xD
Answer: A string definida na variável e a que vem do texto não tem diferença nenhuma.
O que a função open() retorna é um objeto de<class '_io.TextIOWrapper'>, onde você pode obter a string do arquivo utilizando um desses métodos: read, readline, readlines. Ou seja, o que você obtém desta função, não é uma string !!
Fora isso, esse objeto também é um iterável, e você pode através de for loop percorrer cada linha do arquivo. Por isso no seu exemplo, a string é impressa por completo, porque a cada volta que o for loop dá, ele devolve uma linha completa do seu arquivo.
Já no exemplo da string que você define na sua variável, ele imprime cada letra pois strings também são objetos iteráveis (sequência) e ao realizar um for loop com elas, você obterá em cada volta um elemento da string.
Exemplo:
variavel = "Olá!\n Como você está ?"

file = open("arquivo.txt") # Mesmo conteúdo da variável.

for linha in file:
    print("Nova linha:",linha)

    # Já que a variável "linha" é uma string, vamos passar ela em um for loop.

    for letra in linha:
        print("Letra da linha:",letra)

    print("-----------------------------")

file.close()

A saída disto será:
Nova linha: Olá!

Letra da linha: O
Letra da linha: l
Letra da linha: á
Letra da linha: !
Letra da linha:

-----------------------------
Nova linha:  Como você está ?
Letra da linha:
Letra da linha: C
Letra da linha: o
Letra da linha: m
Letra da linha: o
Letra da linha:
Letra da linha: v
Letra da linha: o
Letra da linha: c
Letra da linha: ê
Letra da linha:
Letra da linha: e
Letra da linha: s
Letra da linha: t
Letra da linha: á
Letra da linha:
Letra da linha: ?
-----------------------------

Comment: Boa resposta Jean, mas se você me permitir dar um pitaco eu não chamaria sequências nem objetos _file-like_ de "iteradores", mas sim de "iteráveis"... Pois iteradores são objetos a parte, que você recebe usando `iter()`. Você mesmo pode testar no terminal: `type("teste")` resulta em ``, enquanto `type(iter("teste"))` resulta em ``.
Comment: Realmente, eu não tinha pensado nisso. Obrigado, vou corrigir a resposta ;-)


Using a Views Output as Options in a Webform in Drupal 7?

Question: I have a list of articles on my site and currently I have a View that lists all of these articles. I want to be able to build out a webform where a user can select certain articles and then have it emailed to an address. I basically want to turn the Views list of articles into a radio button box where it can be selected for inclusion into the webform.
Are there any modules that can do this? Or how would one go about doing this if there is no module that supports this.
Comment: There is a sandbox project out there: https://www.drupal.org/sandbox/mrconnerton/1373760
Answer: I'm a big fan of webform, but in this use case, I would probably use Entity Form and References instead.
Install both modules, and create your entity form.
Add a node reference field. On the field settings, narrow the results by content type "article".
Note, this would show all articles. If you were hoping to narrow down your results using views filters, you might want to investigate the sandbox project that Volker suggested.
Update: The References module allows you to use a view as a list of referenced nodes. This is very useful for drilling your results down to the exact nodes you want to show.


OData without database but with Foreign Keys

Question: I have an OData provider which doesn't use Entity Framework or any other sort of database.
I use the following 2 models:
public class ItemModel : TrackableBaseModel<Item>
{
    [Key]
    public Int32 ItemId { get; set; }

    public String ItemName { get; set; }

    public virtual ICollection<ItemSerialModel> ItemSerials { get; set; }
}

public class ItemSerialModel : TrackableBaseModel<ItemSerial>
{
    public Int32 ItemSerialId { get; set; }

    [ForeignKey("Item")]
    public Int32? ItemId { get; set; }

    public ItemModel Item {get;set; }
}

But the OData $metadata shows:
<EntityType Name="ItemModel">
    <Key>
        <PropertyRef Name="ItemId"/>
    </Key>
    <Property Name="ItemId" Type="Edm.Int32" Nullable="false"/>
    <Property Name="ItemName" Type="Edm.String"/>
    <NavigationProperty Name="ItemSerials" Relationship="UserSite.Models.UserSite_Models_ItemModel_ItemSerials_UserSite_Models_ItemSerialModel_ItemSerialsPartner" ToRole="ItemSerials" FromRole="ItemSerialsPartner"/>
</EntityType>
<EntityType Name="ItemSerialModel">
    <Key>
        <PropertyRef Name="ItemSerialId"/>
    </Key>
    <Property Name="ItemSerialId" Type="Edm.Int32" Nullable="false"/>
    <Property Name="ItemId" Type="Edm.Int32"/>
    <NavigationProperty Name="ItemModel" Relationship="UserSite.Models.UserSite_Models_ItemSerialModel_ItemModel_UserSite_Models_ItemModel_ItemModelPartner" ToRole="ItemModel" FromRole="ItemModelPartner"/>
</EntityType>

But that doesn't make much sense, it comes with ItemModelPartner which doesn't even exist in the project.
How can I setup manual FK's so that the navigation properties will work in OData?
The WebApiConfig defines the models like this:
builder.EntitySet<ItemModel>("Items").EntityType.HasKey(x => x.ItemId);
builder.EntitySet<ItemSerialModel>("ItemSerials").EntityType.HasKey(x => x.ItemSerialId);

Comment: What's the reason for having the foreign key in addition to the `ItemModel.Item` property? Is it ever possible that `ItemSerialModel.Item.ItemId` does not equal `ItemSerialModel.ItemId`? If those two are always equal, I would think you wouldn't need `ItemSerialModel.ItemId` at all. What doesn't make sense about the generated $metadata? ItemModelPartner and ItemSerialsPartner are probably be auto-generated. They describe how entity sets are mapped to each other via this navigation property. I wouldn't worry too much about them unless you have multiple entity sets with the same type.
Comment: Well the problem is when I try to query from it, it throws the exception that the ItemPartner controller cannot be found... Which is odd because that doesn't even exist.
Comment: Oh, I see. Is this using ASP.Net Web API? I'm not very familiar with Web API myself so I'm not sure how the model actually gets created, but I think it'd be helpful to add that info (and the error message) to your question.
Comment: Well the model is created by hand :) As I said there is no underlaying provider, but it's done by hand all.
Comment: If you are using WebApi, can you please post your IEdmModel?
Comment: Where would I find the EdmModel?
Comment: Where are you defining your EdmModel? Are you using the ODataConventionModelBuilder? Most likely in a file named WebApiConfig?
Comment: Sorry I forgot about the EdmModel, it is created in the WebApiConfig, I added the code to the question.
Answer: Try changing your EdmModel to as follows.
ODataConventionModelBuilder v1ODataConventionModelBuilder = new ODataConventionModelBuilder();

v1ODataConventionModelBuilder.EntitySet<ItemModel>("ItemModels");
v1ODataConventionModelBuilder.EntitySet<ItemSerials>("ItemSerials");

config.Routes.MapODataRoute("routename", "routeprefix/", v1ODataConventionModelBuilder.GetEdmModel()

You should also be able to remove the following property
[ForeignKey("Item")]
public Int32? ItemId { get; set; }

Comment: OData doesn't really use ForeignKeys. Instead of thinking of the data model like a relational database (where related entities are joined via foreign keys), think of this like an object oriented data structure. If you want to create a relationship from class Bar to class Foo, you just need to have a property on Bar that has type Foo.
Comment: Tried your suggestion but didn't help. If I remove the ForeinKey attribute, how would OData know where the relations are?
Comment: Ah ok I see I didn't know that. Unfortunately with the adjusted code it will comes up with the Partner in my $metadata, is this common behavior?
Comment: Yes. What I would suggest doing is using a program like LinqPad or any other OData metadata browser. That will give you a better representation of your edm.
Comment: You are right, I noticed now that the EDM / $metadata was correct but that the following step: connecting to JayData is not working properly with WebApi OData.
Comment: @JenS Hey Jen, like Rogier here, I would also have assumed the foreign keys were needed. I'm finding it hard to learn more about OData 4 modelling (in the absence of a database, all tutorials share the storage model, usually EF which makes it hard to work out what's needed for OData and what's for EF). Is there a good resource out there for OData modelling practices?
Answer: For your ItemSerialModel, you should add "virtual" at the last line. Just like what you did in ItemModel.
public class ItemSerialModel : TrackableBaseModel<ItemSerial>
{
    public Int32 ItemSerialId { get; set; }

    [ForeignKey("Item")]
    public Int32? ItemId { get; set; }

    public virtual ItemModel Item {get;set; }
 }

And as for Edm Model, you should do what Schandlich suggest. Hope this will help.
Comment: What does adding `virtual` do for OData?


in which part is used the splitting criteria in AdaBoost?

Question: I have been reading the original article about AdaBoost and by comparing with other reading material it has come some doubts about this model. Please feel free to correct me if in any part I am wrong. For what I see this model uses something called a Decision stump which is only a node that considers. For example, in the following image:

Only one feature at a time is used for making the decision stump, am I right?
Also, and I was not able to find this part in the original algorithm, is it necessary to have a splitting criteria like Gini or Entropy? or in this case it only relies in the error that each weak classifier has? I mention this because I was not able to find some explanation about if its needed and when should I consider this splitting criteria.
Thanks, and sorry for this simple question.
Comment: Does this answer your questions: [How to use decision stump as weak learner in Adaboost?](https://stats.stackexchange.com/q/157220/930)
Comment: Thank you @chl, but it answers it partially; because it does not mention in which part or if its needed to use Gini or entropy for splitting the decision stumps.
Answer:
Only one feature at a time is used for making the decision stump, am I right?

In the sense that the trained decision stump only uses one feature, yes.  But training the decision stump is like any other tree model: it chooses which feature to split on, and what point at which to split that feature, based on some impurity criterion.


Sum of function and its inverse

Question: Suppose that $f$ is continuous and maps $[0,1]$ one-to-one onto $[0,1]$. Therefore, it is obvious that inverse function $f^{-1}$ exists. Then, show that
$$
\int_{0}^{1} f(x) d x+\int_{0}^{1} f^{-1}(y) d y=1
$$
and give a geometric interpretation of this equation.
There is a similar problem in the following link; however, the major strategy does not work since $f$ is not an increasing function. Moreover, the substitution method does not work as well, since we need compact interval $[0, 1]$ for integration. So, what other alternatives are there for solving this problem?
Show rigorously that the sum of integrals of $f$ and of its inverse is $bf(b)-af(a)$
Comment: Does it map $0$ to $0$ and $1$ to $1$ ?
Comment: Not necessary, $f(0)$, $f(1)$ can be different from zero, one, respectively. If it would be, then this problem could be easily derived from the provided link
Answer: The statement is false if $f$ is decreasing.
Consider $f(x) = 1 - x^2$ on $[0,1]$. Then $f^{-1}(x) = \sqrt{1-x}$ but
$$
\int_0^1 (1-x^2) \ dx + \int_0^1 \sqrt{1-x} \ dx = \frac{2}{3} + \frac{2}{3} = \frac{4}{3} \ne 1
$$
If $f$ is increasing, we can interpret the result geometrically as follows: The area under the graph of $f$ plus the area under the graph of $f^{-1}$ is equal to the area of the unit square, which is $1$.
Comment: It seems that if $f$ was given an increasing function, then $f(0) = 0$ and $f(1) = 1$ would satisfy, and we could proceed on the provided link problem, right?!
Comment: @Snowflake yes, if $f(0) = 0$ and $f(1) = 0$, then we can apply the linked result. See also the edit to my answer.
Comment: If $f$ is not decreasing, would it be necessarily true as well? Which conditions would be enough to ensure this holds true? Is it the fact $f(0) = 0$ and $f(1) = 1$ playing crucial role to conclude the correctness of this problem?
Comment: Thank you very much for coming up with such counterexample.

Sum of function and its inverse


Why does Cdim7 contain an A instead of a Bb?

Question: CM7 - C, E, G, B
C7 - C, E, G, Bb
Cm7 - C, Eb, G, Bb
Caug7 - C, E, G#, Bb
But...
Cdim7 - C, Eb, Gb, A.
Why Does Cdim7 contain an A and not a Bb?
Comment: The other answerers have already addressed the question more than adequately so I won't duplicate them, but I will just point out additionally that there does exist a chord "C Eb Gb Bb" and that it's called the "half diminished" chord, because it has a diminished five but a non-diminished 7th.  It's often written as Cø and is also a useful chord (it's really pretty).
Comment: I have put an A because I saw more such thing in the dictionaries that it has an A
Answer: C°7 actually includes a B doubleflat.
A major seventh above C is B and a minor seventh above C is B♭. This means that a diminished seventh above C is actually B♭♭, which is enharmonic to A.
But the seventh is not A, because A is just a sixth above C. A is enharmonic to B♭♭, but since we want the seventh above C, it must be B♭♭, not A.
But it gets more interesting: since fully diminished sevenths are just stacked minor thirds, they can be spelled four different ways, with each pitch as root; we say that these chords are enharmonic, just like we say that A is enharmonic to B♭♭.
This then means that C–E♭–G♭–A is a diminished seventh chord, but with A as the root: A–C–E♭–G♭. If E♭ is the root, we'd rather spell it as D♯–F♯–A–C. If G♭ is the root, we'd spell it as F♯–A–C–E♭.
These chords all sound the same as C°7, but they are spelled differently.
Comment: 'We'd rather spell Ebo as D#o'. Because Ebo would be Eb--Gb--Bbb--Dbb. Good reason!
Answer: Other answers have pointed out that it's a B♭♭, not an A. To answer the question "why not a B♭":
The chord in question is the chord of the diminished 7th. As David Bowling and Richard have stated, if the root is C, the diminished 7th is B♭♭.
The chord is also called the "diminished 7th chord". This name and its chord symbol Cdim7 are perhaps confusing. I don't know if this is what you thought, but, just for the record, "Cdim7" does not mean

the diminished triad C-E♭-G♭ as indicated by "Cdim"
the pitch B♭ as indicated by "7", just as C7 means a C triad plus B♭

It's not a [ diminished triad ] with a seventh. It's a chord of the [ diminished seventh ].
Comment: Except it *has* a diminished triad. It has a m3 and a dim5. Then on top, a dim7. If it was a chord with a dim7 only, it'd sound like a M6.
Answer: A Cdim7 chord is actually spelled C-Eb-Gb-Bbb, where the interval from C to Bbb is called a diminished seventh. The interval from C to A is a major sixth, but sometimes you do see people spell Cdim7 as C-Eb-Gb-A for convenience to avoid the double flat, it is just technically incorrect. This chord is sometimes called a fully-diminished chord, in contrast to a half-diminished chord. A half-diminished C chord (Cmin7b5, C-7b5, or C∅) is spelled: C-Eb-Gb-Bb. These are often found in ii-V-i progressions in jazz.
Comment: @RosieF -- not sure I understand your point; **Adim7** and **Cdim7** are _enharmonically_ spelled the same way, but are not the same chords.
Comment: @RosieF -- it _is_ a technically incorrect way to spell **Cdim7**; it is only a correct spelling for **Adim7**.
Comment: C-E♭-G♭-A is also the correct spelling of the pitches in Adim7. And it's OK to put the C (or indeed E♭ or G♭) in the bass.
Comment: Indeed. I just didn't want anyone reading your answer to think the spelling with an A is as you say "technically incorrect". A composer may write an A and yet be technically correct. It's just that the technically correct chord symbol would then be Adim7. When it comes to deciding how to spell a dim7 chord, let voice leading be your guide. And yes, perhaps simplify for convenience if you feel that's right. The chord symbol comes later. The music determines the chord symbol, not the other way around. The composer needn't let the symbol determine the spelling.
Answer: It actually contains a Bbb  (double flat).  Often (mis)spelt as A for convenience.  You'll also often see it notated as C, Eb, F#, A.
A dim7 chord is a pile of minor 3rds.  A minor 3rd up from Gb is Bbb.
Answer: Adding a little to the others. Cdim7 cannot contain a Bb note. Bb only produces a minor 7th from the root. To make it a diminished 7th, the space between root and m7 needs to be one semitone smaller. That's the reason it gets called Bbb. It sounds just like A, but since C>A is a M6, that wouldn't be correct if the chord was to be called dim7.


Why does making the 'wrong' $u$-substitution still work in this example?

Question: I was evaluating the integral
$$
\int 2x \cos(x^2)\,dx
$$
and realised that it could be written in the form
$$
\int f'(g(x))g'(x)\,dx
$$
and so substitution could be used to help evaluate it. Setting $u$ equal to $x^2$ (the inner function $g(x)$), we have
$$
du=2x\,dx
$$
and so
$$
\int 2x \cos(x^2)\,dx=\int \cos(u)\,du=\sin(u)+C=\sin(x^2)+C
$$
However, it appears that making the wrong substitution $u=2x$ still works, even though $u$ is not equal to the inner function $g(x)$:
$$
u=2x \implies du=2\,dx \\
\int 2x \cos(x^2)\,dx=\int\frac{u}{2}\cos\left(\frac{u^2}{4}\right)
$$
Admittedly, solving this is ugly. In fact, it requires two more substitutions, and so it was clearly a bad idea to set $u=2x$. However, the final answer did indeed come out as $\sin (x^2)+C$. My question is about why this works at all. If I understand correctly, substitution is only a valid method when $u=g(x)$, as this justifies the treating of $du$ and $dx$ as manipulable terms. We know that
$$
\int f'(u)\,du=f(u)+C=f(g(x))+C
$$
Therefore,
$$
\int f'(u)\,du = \int f'(g(x))g'(x)\,dx=\int f(g(x))+C
$$
meaning that $g'(x)\,dx$ can be 'replaced' by $du$. If $u\neq g(x)$, then this method seems to fall apart. Despite this, setting $u=2x$ actually did end up working. Was this just a coincidence?
Comment: The substitution rule is always valid, regardless of the substitution made (so long as it is well defined). This is because the theorem that introduces substitutions effectively states "integral A equals integral B" where the connection between them is through an arbitrary substitution. For the purposes of evaluating we try to pick substitutions that change the integral to something easier on the eyes, even though we are actually changing nothing about the problem itself.
Comment: There's nothing "wrong" about the substitution. Any substitution is a valid transformation of the integral, it may just be easier or harder to arrive the final answer using different substitutions.
Answer: Note that in the link you provided, the calculator sets $v=u^2=4x^2$ and then $w=v/4=x^2$, i.e. effectively it has substituted for $x^2$ only albeit in a more circuitous manner.
Answer: Integration by substitution is most straightforwardly useful when you can pick $f,g$ such that the integrand is $f'(g(x)) g'(x)$. In this case, because of the chain rule for derivatives, you can just apply the FTC immediately and you're done.
However, it is possible that you can write the integral as, say, $h(g(x)) g'(x)$, but you don't immediately know how to integrate $h$ (and thus don't really know what $f$ should be). In this case you can still apply integration by substitution. This reduces the problem to integrating $h$. But there is no guarantee that this is easier than the original problem.
Thus the goal of integration by substitution basically boils down to choosing $h,g$ such that the integrand is $h(g(x)) g'(x)$ and $h$ is easier to integrate than the original integrand. It is still technically correct, just not useful, if $h$ is just as hard or harder to integrate than the original integrand.
As kind of an artificial example of my point, consider
$$\int \sin(x) e^{\sin(x)} \cos(x) \, dx.$$
Here we recognize the integrand is a function depending only on $\sin(x)$ times the derivative of $\sin(x)$, so we do integration by substitution and get to
$$\int u e^u \, du.$$
You might not immediately know what the antiderivative of $h(u)=ue^u$ is. But you still have an equality between the first integral and the second, so you can work from here, instead of having to work directly on the original problem.
Comment: Thank you very much for this answer. So, in summary, integration by substitution is not *just* the reverse chain rule—it works in every case, even if this is not useful. I have a question, though. I don't know understand how FTC is relevant in this case. I have often heard that FTC tells us that 'integration is the reverse of differentiation', but this has never really made any sense to me. I thought that this was true by definition. My understanding is that FTC tells us that indefinite integrals can be computed by definite integrals (or vice versa). Is it possible if you shed some light here?
Comment: @JoeLamond First: integration by substitution is ultimately just the reverse chain rule. When I say that you're left to integrate $h$, mathematically I still have a "reverse chain rule" kind of statement. The only catch is that I don't have to have an explicit formula for the $f$ that I need. I can still introduce $f$ as "an indefinite integral of $h$", and then go off and get an explicit formula for such a thing separately.
Comment: @JoeLamond Second: FTC tells you how to compute derivatives of definite integrals with variable limits, and how to evaluate definite integrals by computing antiderivatives. It is the reason why "antiderivative" and "indefinite integral" refer to the same thing. If you imagine a bizarro world with no FTC, the indefinite integral of a function $f$ would just be the family of functions of the form $\int_a^x f(y) dy$ where $a$ is in the domain of $f$, and this wouldn't have anything to do with (anti)derivatives (only Riemann sums and limits).
Comment: I'm still a little confused, sorry. Say we are trying to integrate $\int f'(g(x))g'(x)$, and we find that the result is $f(g(x))+C$. Doesn't this just the fact that $\frac{d}{dx}f(g(x))+C=f'(g(x))g'(x)$? I know that FTC is important in the ways you described, but I just can't see how that applies.
Comment: @JoeLamond The connection is really to $H(x)=\int_a^x f'(g(y)) g'(y) dy$; FTC says that $H(x)=f(g(x))-f(g(a))$. The whole concept of the indefinite integral as anything other than a computational aid muddles the discussion.
Comment: I thought that the first FTC told us that if $H(x)=\int_{x}^{a}f'(g(y))g'(y)dy$, then $H'(x)=f'(g(x))g'(x)$. Is that correct, and if so, then how does that link to what you wrote? Thank you very much for your continued help, by the way.
Comment: @JoeLamond FTC says both: the derivative of the definite integral with upper limit $x$ (and fixed lower limit) is the integrand evaluated at $x$, and the definite integral of the derivative of $f$ from $a$ to $b$ is $f(b)-f(a)$. These are really distinct statements, which is something that isn't always apparent from the usual calculus curriculum (perhaps because the first case is almost never backed up by any modeling examples).
Answer: I think I've managed to answer my own question. Actually, writing $u=2x$ does allow the integral to be written in the form $\int f'(g(x))g'(x)$, albeit in a much uglier way than before. Here is what we must set $f$ and $g$ to for the substitution to be valid:
\begin{align}
\text{Let }
f(x)&=\sin\left(\frac{x^2}{4}\right) \\
\implies f'(x)&= \cos\left(\frac{x^2}{4}\right)\frac{x}{2}\\
\text{Let }
g(x)&=2x \\
\implies g'(x)&=2 \\
\int f'(g(x))g'(x)dx&=f(g(x))=\cos(x^2)2x
\end{align}
So, often $u$ does equal $g(x)$ if we so choose, meaning that $g'(x)dx$ can be substituted by $du$. As Ian points out, it is not always clear what the antiderivative of $f'(x)$, but as along as we know that it can be integrated, there is no problem.


Different linear programming versions of optimal transport

Question: What is the difference between these two different versions of the linear programming optimization set-up for optimal transport (OT)?  how to reconcile them mathematically to show that they are equivalent?
OT Linear Programming #1
(Source)
\begin{align*}
\min_{\boldsymbol\Gamma} \quad & \langle\mathbf{C},\boldsymbol\Gamma  \rangle = \sum_{ij}\mathbf{C}_{ij}\boldsymbol\Gamma_{ij} \\
\mathrm{s. t.}  \quad  & \boldsymbol\Gamma \mathbf{1} = \mathbf{\alpha} \\
&\boldsymbol\Gamma^{\top}\mathbf{1} = \mathbf{\beta}
\end{align*}

$\boldsymbol C$ is the distance matrix, which contains elements$\Vert x - y \Vert$,
$\boldsymbol \Gamma$ is the transport matrix, which contains elements $\gamma(x,y)$,
$\alpha$ and $\beta$ are the source and target probability distributions, respectively, of variables $X$ and $Y$.

OT Linear Programming #2
(Source)
$$
\begin{array}{rrcl}
\min_{\mathbf{x}} \ & \mathbf{c}^T \mathbf{x} &  & \ \\
\mathrm{s.t.} \ & \mathbf{A} \mathbf{x} & = & \ \mathbf{b} \\
 \  & \mathbf{x} & \geq &\ \mathbf{0}
\end{array}$$

$\mathbf{c}$ is a vectorization of the distance matrix $\boldsymbol C$,
$\mathbf{x}$ is a vectorization of the transport matrix  $\boldsymbol\Gamma$,
while the source and target distributions are $\mathbf{b} = \begin{bmatrix} \alpha \\ \beta\end{bmatrix}$
Comment: The first version is how you naturally think about it in mathematical terms and write in papers. The second version is how you have to reformulate it to actually fit it into canonical LP form (and possibly feed into a solver).
Comment: about reconciling the two, how can it be shown that the constraints in version #1 are a rewording of the constraints in version #2?
Answer: To reconcile both, note that $\bf x$ is obtained by stacking the columns of $\bf \Gamma$. For the cost, $\bf c$ is obtained by stacking the columns of the cost matrix $\bf C$.
Also, let $\mathbb I_n$ be an $n\times n$ identity matrix. Therefore, make:
$$
{\bf A} = \begin{bmatrix}
{\bf 1}_n^\top \otimes \mathbb I_m\\
\mathbb I_n \otimes {\bf 1}_m^\top
\end{bmatrix}
$$
Finally, the constraint ${\bf x}\geq 0$ is the same as the constraint that the transport plan $\bf \Gamma$ must be positive. This can be found in page 38 in the book Computational Optimal Transport, by Peyré and Cuturi. The pdf of the book is freely avaiable.
Comment: If it's 1D, then there is an actual Monge map, which is given by the composition of the CDF and the inverse CDf
Comment: For discrete cases you can hard code it. I answered in your question
Comment: I know that Program #2 shown in the question appears in *Computational optimal transport*, but do you know if Program #1 does too, or if they make reference to it?
Comment: Yeah, this #1 formulation of the problem is actually much more present throughout the book, while the second one is just briefly mentioned. As you pointed out, most algorithms for solving LP use the second formulation, and that is the only reason (I guess), the authors actually show it.
Comment: if $\boldsymbol c$ contains distances $| x - y|$, how can I reconcile the fact that $\mathbf{b} = \begin{bmatrix} \alpha \\ \beta\end{bmatrix}$ is just a figurative  representation of $\bar{\boldsymbol{b}} = \begin{bmatrix} \boldsymbol x \\ \boldsymbol y \end{bmatrix}$, where $\alpha$ is by definition $\boldsymbol x$, the source distribution, and $\beta$ is by definition $\boldsymbol y$, the target distribution?
Comment: Sorry, not sure I understand... I mean, $b$ is a vector containing the marginal distributions. I don't understand what is the $\bar b$.
Comment: It's my version of $\boldsymbol b$ that reflects what $\boldsymbol c$ was computed from: the samples of $\alpha$ and $\beta$, which are $x$ and $y$
Comment: I think I understand your question now. I think your misconception is the following. $\alpha$ is not the vector of coordinates, but the vector of weights. I will edit my answer and provide an example.
Comment: Actually, just take a look at this computational implementation, and this might answer your questions. https://github.com/davibarreira/OptimalTransport.jl/blob/gallery/examples/example_2.ipynb
Comment: thanks for the link. is there a way for me to flatten the example to 1D instead of 2D? it is very similar to Peyre's code which is also 2D http://nbviewer.ipython.org/github/gpeyre/numerical-tours/blob/master/python/optimaltransp_1_linprog.ipynb
Comment: To flatten you just need to use coordinates in 1D instead of 2D. Note that $\alpha$ and $\beta$ contain only weights, so they are always a $n\times1$ vectors. While the actual coordinates will be used to construct matrix $C$. Note, $C_{1,1} = |x_1 - y_1|$.
Comment: Did you know that `scipy.stats.wasserstein_distance` does not use linear programming. It's code is in the following link  (which only does 1D). If it's not using LP, how can optimal transport be solved without LP. What method/approach is being used?  https://github.com/scipy/scipy/blob/aa8ebbc3fc0e8057ae2cbd3255a702dd8f0ab94d/scipy/stats/stats.py#L7887
Comment: aren't Monge **transport maps** unable to perform mass-splitting from the source to target distributions, unlike the Kantorovich **transport plan**?
Comment: You are right. But the solution still works (although it will not be a Monge Map). Look on page 32 of Computational Optimal Transport book.
Comment: You just need to use the quantile function and it's inverse to compute the Wasserstein distance.
Comment: I don't see quantile or inverse `.ppf` functions used in the code for  `wasserstein_distance` though https://math.stackexchange.com/questions/3866825/why-doesnt-scipys-wasserstein-distance-use-linear-programming


How to define an object with variable number of keys and with keys from an enum?

Question: Well, when I started with Angular I came in touch with Typescript the first time - and I did a huge mistake: I used always any as datatype.
So now I have a few 10.000 lines of code and want to make it "type-save". Better late then never... and here comes the problem:
I have an object that has a property validity with a variable number of key-values, like
obj.validity = {
    time: true,
    feePayed: true,
    approved: true
}

or
obj.validity = {
    time: true,
    feePayed: true,
}

or
obj.validity = {
    startpass: true,
    passport: true
}

the possible keys are defined in an enum:
export enum VerificationType {
    TIME = 'time',
    STARTPASS = 'startpass',
    PASSPORT = 'passport',
    FEEPAYED = 'feePayed',
    ALLOWPAYPENT = 'allowPayment',
    MANUALRELEASEPAYMENT = 'manualReleasePayment',
    MANUALAPPROVE = 'manualApprove',
    ALLPAYED = 'allPayed',
    APPROVED = 'approved',
}

In the object declaration I tried now something like that:
class obj {
    validity: ISkaterValidation
}

and
export type ISkaterValidation = {
    [key in VerificationType]: boolean;
};

This fails with error TS2339: Property 'ANY PROPERTY NAME' does not exist on type 'ISkaterValidation'.
I'm still a beginner with Typescript typings and hope that somebody can give me a solution, please.
Answer: Your type expects every key inside the object. You need to make it optional:
export type ISkaterValidation = {
    [key in VerificationType]?: boolean;
};

That tells TypeScript that the key doesn't have to exist on the type, but also no other key should be there.


How to access raw data in twig template using views table format

Question: I use a table format view to display a list of articles article-list using draggable views to allow editors to order the list. I customize the content of rows depending on a boolean field is_header and a formatted text field title_override. I have sub-themed views-view-table--article-list.html.twig to accomplish this. It works well enough.
In the template, I test the truth of is_header and the non-emptiness of title_override as follows:
    {% if row.columns.field_is_header|render|striptags|trim %}

and
    {% if row.columns.field_title_override|render|striptags|trim %}

These tests yield the proper results, but they make the template very slow! Anything less than render|striptags|trim doesn't work.
QUESTION:
Is there a way to directly access the raw data values of these variables to avoid the render|striptags|trim overhead?
I've seen suggestions to use something like row._entity.field_is_header, but this way of using _entity seems to work only in templates for the HTML or Unformatted view styles. Seems like there should be an easy way to do this.
As a note, switching to using an HTML or Unformatted style doesn't seem possible because AFAIK draggable views can be set up only with the table format.
ANSWER:
Thanks to 4k4 for the answer to this question (see below). To apply his/her general answer to my specific question, here is a snippet of the twig script I ended up with:
{% for key, row in rows %}
  {% if view.field.field_is_header.value(view.result[key]) %}
    <conditional stuff>
  {% endif %}
  {% if view.field.field_title_override.value(view.result[key]) %}
    <stuff when the override is non-empty>
  {% else %}
    <stuff when the override is empty>
  {% endif %}
{% endfor %}

Note that rows is an array normally passed to the views-view-table.html.twig template, as documented in the original template's header comments. Note also that the availability of the view array is NOT documented in the original template's header comments (not that it would have saved me without 4k4's help).
Comment: You can't theme views field like entity fields. Most times formatting views fields in UI is the better approach. If you need a boolean field only to configure the format of other fields add the boolean field first, exclude it from display and use it as replacement token to rewrite the result of other fields.
Comment: See https://drupal.stackexchange.com/questions/218022/drupal-8-views-theming-question
Comment: Thanks (and an upvote) @4k4. To simplify, I left out some details, one of which is that the boolean `is_header` is used for other purposes in the template. At your suggestion, I did the field formatting in Rewrite Results for of one of the fields (I assume that's what you were suggesting). It works well and seems better than what I was doing. I'll try figure out a way to handle `is_header` differently.
Comment: Still seeking an answer to this. It's hard to imagine that the raw, original data isn't available somewhere in the array passed to the twig template. But several hours of reading and trial-and-error have yielded no success.
Answer: You can try to apply getValue() of the field handler on the result row, for example:
{% if view.field.field_name.value(view.result[key]) %}

Edit:
In a views table template row is not of the type ResultRow, so you need to get it directly from the view object using key of the loop {% for key, row in rows %}.
See these questions for different views templates:
How can I get data from ResultRow object in views template and How to preprocess view field?
Comment: YES! This is the answer I was seeking. Thanks, @4k4. I have accepted this as the correct answer and have added it to my question so anyone who comes looking can find it easily. (I always wonder how you or others come to know this kind of stuff.)
Comment: You answered this a long time ago. I'm still using it. Thanks! Can you tell me how to get the node id for the node associated with a row? Is the nid also in the view object for the views table template row?
Comment: You can use the same code on the field `nid`, if configured in the field list of the View. Or get the `_entity` property from the ResultRow, if it is available: `{{ view.result[key]._entity.id }}`
Comment: Thanks. It's the second way you show that I couldn't figure out. The only examples I found were for rows of type ResultRow.


Draw $13$ cards out of $52$ with one suit missing

Question: Here's a probability problem which is causing me some trouble:

In a deck of $52$ cards, in how many ways can you draw $13$ cards such that at least one suit is missing.

What I've done at first was: $\binom{4}{1}\binom{13}{13}+ \binom{4}{2}\binom{26}{13} + \binom{4}{3}\binom{39}{13}$, but I think that in here, I'm counting some elements twice. I know about the Inclusion-Exclusion Principle but have no idea on how to apply it to this problem.
Thanks a lot for your help!
Comment: Those are usually referred to as "suits".  The colors are usually: red or black.
Comment: Are you familiar with the [Inclusion-Exclusion Principle](https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle)?  What have you attempted?  Where are you stuck?
Comment: There are four chances for the missing color, so that modelling the probability space $\Omega$ for the experiment with the set of subsets of $\{2,3,4,5,6,7,8,9,10,J,Q,K,A\}\times\{\clubsuit, \diamondsuit,\heartsuit, \spadesuit\}$ we have $|\Omega|=\binom {52}{13}$, and the "good card distributions" with one missing color are $4\cdot\binom{52-13}{13}$. Now, sometimes two colors are missing, or three...
Comment: @dan_fulea How many times are you counting a draw with no clubs and no spades?
Comment: Have you tried counting the number of ways to draw 13 cards such that all suits are there?
Comment: @almagest i was only trying to explain why the inclusion exclusion principle was mentioned. (...but the enter after each proposition is submitting the comment, and i hardly stop hitting it in time.)
Comment: Hey guys, thanks for your quick answers. What I've done at first was:
$\binom{4}{1} \binom{13}{13} + \binom{4}{2} \binom{26}{13} + \binom{4}{3} \binom{39}{13} $ but I think that in here, I'm counting some elements twice.
I know about the Inclusion-Exclusion Principle but have no idea on how to apply it to this problem.
Comment: Solving we get there are 0.0510 or 5.10% chances that at least one suit is missing
Answer: There are $\binom{4}{1}$ ways to exclude a suit and $\binom{39}{13}$ ways to choose the $13$ cards from the remaining three suits, giving an initial count of
$$\binom{4}{1}\binom{39}{13}$$
However, we have counted those distributions in which two suits are missing twice, once for each way we could have designated one of the missing suits as the missing suit.
There are $\binom{4}{2}$ ways to exclude two suits and $\binom{26}{13}$ ways to choose the $13$ cards from the remaining two suits, so we must subtract
$$\binom{4}{2}\binom{26}{13}$$
from our initial count.
However, doing so subtracts too much.  We counted each case in which three suits are missing three times in our initial count, once for each way we could have designated one of the missing suits as the missing suit, and subtracted each case in which three suits are missing three times, once for each of the $\binom{3}{2}$ ways we could have designated two of the missing suits as the missing pair of suits.  Therefore, we have not counted those cases in which three suits are missing at all.  We must add them to our total.
There are $\binom{4}{3}$ ways to exclude three of the four suits, and one way to choose all $13$ cards of that suit.  Hence, there are
$$\binom{4}{3}\binom{13}{13}$$
ways to exclude three of the suits.
By the Inclusion-Exclusion Principle, the number of $13$ card hands from which at least one suit is missing is
$$\binom{4}{1}\binom{39}{13} - \binom{4}{2}\binom{26}{13} + \binom{4}{3}\binom{13}{13}$$
Therefore, the probability that at least one suit is missing when $13$ cards are drawn from a standard $52$-card deck is
$$\frac{\dbinom{4}{1}\dbinom{39}{13} - \dbinom{4}{2}\dbinom{26}{13} + \dbinom{4}{3}\dbinom{13}{13}}{\dbinom{52}{13}}$$
Answer: Yes, there's some double counting going on.  Certainly $${4\choose3}{39\choose13}$$ covers all the cases, but cases where only two suits occur have been counted twice.  (For example, hands with only Hearts and Spades, have been counted as having only Clubs, Hearts and Spades, and also as having only Diamonds, Hearts, and Spades.)  So we must subtract these, giving $${4\choose3}{39\choose13}-{4\choose2}{26\choose13}$$  Now, what about hands with only one suit?  The have been added in ${3\choose2}$ times, and subtracted out ${3\choose1}$ times, so we haven't counted them at all, and we need to add them in.  The final answer is $${4\choose3}{39\choose13}-{4\choose2}{26\choose13}+{4\choose1}{13\choose13}$$


Send request from https to local http(s) server

Question: I try to explain our problem.
We are coding a little tool (with Java 1.8 and Jetty+Jersey) which provides a rest-server for saving data (documents, ...) on hard drive or printing documents directly.
The tool can be started/accessed on local computer (localhost) or on a different device in local network.
The problem is you can't send a request from a https-page to the local tool if it's not ssl-encrypted.
What we need is a ssl-certificate for our tool.
But the problem is we can't know on which device/ip-address our customers want to run the tool and you have to bind a domain to a ssl-certificate.
We can create ssl-certificate and bind it with (for example) tool.domain.com, but the tool can run on 127.0.0.1 as well as on 192.168.10.2 or something similar.
So my question is, is there a way to (programmatically) "bypass" this wall?
One "way" would be to add an entry to local hosts file but we want as few as possible effort for our customers and you can't set hosts entry on non rooted mobile devices.
Edit: Added some code snippets.
        ServletContextHandler context = new ServletContextHandler(server, "/*");

        // vhosts
//      context.setContextPath("/*");
        context.setVirtualHosts(new String[]{
             "127.0.0.1",
             "192.168.6.195",
             "localhost",
             "tool.domain.com"
        });

There we add the virtual host to the jetty server. We have a self-signed certificate (for testing) for our domain.
We want to archive something like this:

Here some JS-Code for testing/visualization:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://127.0.0.1:7331/test/echo/test1111');
xhr.setRequestHeader('Host', 'https://tool.domain.com');
xhr.send();

But Chrome (and I think other browser, too) blocking the Host-Header
Comment: if it's from https, it better be to https
Comment: We want to add a ssl-certificate to our tool, but the problem is the target. The target can have each ip-address. It's hard to explain the problem.
Answer: If you have your own company, and your own company domain name, then this is possible.
Lets say you have dominice-so.com
If you created a dns entry for something like self.dominice-so.com to always return 127.0.0.1 then you can ship a keystore/truststore with a valid certificate for the domain name self.dominice-so.com in your product.
The browser should be able to connect just fine, using something like ...
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://self.dominice-so.com:7331/test/echo/test1111');
xhr.setRequestHeader('Host', 'https://tool.domain.com');
xhr.send();

Its up to you to secure that keystore/truststore password so that it doesn't get into nefarious hands. (and that is out of scope of this question)
Comment: Thanks for your answer. But that's the problem, we don't only want 127.0.0.1 but other ip-addresses, too, like 192.168.2.50 if it's a network-pc (like a server machine). We need something with as less as possible effort for our customers.
One way would be to create a wildcard-certificate and create a subdomain for each customer with his required ip-address. But this is "much" effort for us.
Comment: In that case, you'll have to generate in-environment certificates based on a CA known to that environment (each company might have different CA rules).
Comment: We are now implementing this way. I think it's the best way. Thanks again


Finding the derivative of $\log_ax$

Question: $\log_ax$
I know there is a formula for this, but I am trying to understand the logic behind the formula.
I know that $$\log_ax = \frac{\ln x}{\ln a} = \ln x \cdot \ln a ^{-1}$$
Because this is in the form $f(x)g(x)$, I have to use $f'(x)g(x)+f(x)g'(x)$ to solve this, and so I have:
$$f(x) = \ln x \\ f'(x) = \frac{1}{x} \\ g(x) = \ln a ^{-1} \\ g'(x) = -\frac{1}{a\ln^2 a}$$
So I did:
$$\frac{1}{x} \cdot \ln a ^{-1} + \ln x \cdot  -\frac{1}{a\ln^2 a} = \frac{1}{x \ln a} - \frac{\ln x}{a\ln^2 a} = \frac{a \ln^2(a)-\ln(x)x\ln(a)}{ax \ln^3(a)}= \frac{a \ln(a) - \ln (x)x}{ax\ln^2(a)} = \text{???}$$
I think this is wrong. I put it on Wolfram Alpha http://www.wolframalpha.com/input/?i=(aln(a)-ln(x)x)%2F(axln(a)%5E2) and it doesn't seem to simplify to $\frac{1}{\ln(a)x}$, which is the correct answer.
What did I do wrong?
Comment: It is wrong because $a$ is a constant.
Comment: $(\frac{1}{\ln(a)} )' = 0$
Comment: @Arnaldo Oh, I see. I should have used $(kg(x))' = kg'(x)$ instead, because $\ln a ^{-1}$ Is a constant. I'll try, thanks
Comment: g(x) is a constant function
Comment: Have you learned implicit differentation?
Comment: You should treat 'a' as a constant.
Comment: In standard usage, $\ln a^{-1}$ means $\ln(a^{-1}),$ but you're using that notation to mean $(\ln a)^{-1},$ without even mentioning that that's what you meant instead.
Answer: You simply treat $(\ln a)^{-1}$ as a constant. That is $\frac{d}{dx} ((\ln a)^{-1} \ln x )= (\ln a)^{-1}  \frac{d}{dx} \ln x  =  (\ln a)^{-1} \cdot \frac{1}{x}$
Answer: Since $\log_a{x}=\frac{\ln{x}}{\ln{a}}$, and $a$ is just a constant, not a variable, you do not need to use the product rule.
Therefore:
$$\frac{d}{dx}(\log_a{x})=\frac{d}{dx}\left(\frac{1}{\ln{a}}\cdot \ln{x}\right)=\frac{1}{\ln{a}}\cdot \frac{d}{dx}(\ln{x})=\frac{1}{\ln{a}}\cdot \frac{1}{x}=\frac{1}{x\ln{a}}$$

Finding the derivative of $\log_ax$


Repeated measures or not?

Question: I'm looking at cross-sectional survey data where respondents were presented with a series of clinical scenarios. Each scenario varied by patient age (3-levels), condition severity (mild/severe), and risk (high/low). For each scenario respondents were asked to check all treatment options that apply (from a list of 12). Does this count as a repeated measures design, or not? My predecessor used a logistic model with GEE, but left no notes or programs, so I'm starting from scratch and cannot do that procedure in JMP (although I may have SPSS soon). Is there a better option, such as a mixed model? The outcomes are the treatment options endorsed by the respondents.
Thanks!
Answer:
respondents were presented with a series of clinical scenarios

That is, each subject saw more than one clinical scenario? Then yes, you have repeated measures.
Comment: Correct. Thanks. Would a mixed model best fit these data or the logistic with GEE? I don't think either can be done in JMP, but I may be able to take it to SPSS
Comment: @JWislar I think it really depends on the details like your research questions, how the different treatment options relate to each other, how many subjects there are, how many scenarios there are per subject, etc. But I'm not familiar with GEE, in any case.

Repeated measures or not?


How can I inject a string into my classes with unity

Question: I have a lot of classes like so public class GetA:IGetA{} public class GetB:IGetB{} etc etc all in a particular namespace
I register them with a block of code like so
  container.RegisterTypes(
            AllClasses.FromLoadedAssemblies().Where(
                t => t.Namespace == "Getters"),
            WithMappings.FromMatchingInterface,
            WithName.Default,
            WithLifetime.ContainerControlled);

This works perfectly well.
I now have to introduce to some of them a dependency on a certain string.
public class GetA:IGetA
{
   public GetA(string separator){}
}

The separator is the same for all of the classes that require it. How am I best to inject this dependency?
Should I inject a factory class instead? This seems more complex than I want!
Comment: Where do you resolve your classes?
Comment: The getters are constructor injected into other classes that use them. The resolution is done by unity magic.
Comment: Where are the root objects resolved?
Comment: It is in an mvc site which uses an IDependencyResolver. I never call resolve on anything, just use interfaces in constructors and the magic happens. If I am being terribly dense then please guide me to the fact you are searching for! It is all a bit black box for me...
Comment: Take a look at the answer of the question [here](http://stackoverflow.com/questions/14649545/how-to-inject-constructor-in-controllers-by-unity-on-mvc-4). This does not solve your issue. But if you use a controller factory, then you get to call the `Resolve` method manually to resolve the controller. There is an overload of the `Resolve` method that allows you to override dependencies. You could do something like this: `container.Resolve(new ParameterOverride("separator", "myvalue"));`
Answer: This may be a horror scenario but this is what I have done...
I have made my getter class depend on a Func<string>
public class GetA : IGetA
{
   private readonly string separator;

   public GetA(Func<string> separator){
     this.separator = separator();
    }
}

Then in my UnityConfig.cs I have put
        container.RegisterType<Func<string>>(
            new InjectionFactory(f => new Func<string>(() => "The separator... really it comes from a database call though")
                ));

In my test I can do
new GetA (() => ", "); which maybe looks a bit funky but keeps me separate from the database call to get the separator..
I am not really happy with this as if I want a Func<string> elsewhere then I will step on my toes. Perhaps I do need an IGetSeperatorFactory but I don't want one!


Configuration page for module does not apear?

Question: This is my hook_menu.

function coverflow_menu(){
        $items = array();

        $items['coverflow']  = array(
            'title'           => 'Coverflow Slider',
            'page callback'     => 'coverflow_page',
            'access arguments'=> array('access content'),
        );

        $items['admin/config/coverflow'] = array(
                'title'                 => 'Coverflow Settings',
                'description'         => 'Coverflow Slider Settings.',
                'type'                  => 'MENU_NORMAL_ITEM',
                'page callback'     => 'drupal_get_form',
                'page arguments'    => array('coverflow_admin_form'),
                'access arguments'=> array('administer site configuration'),
                'file'            => 'coverflow_admin_form.inc',
                'file path'       => drupal_get_path('module', 'coverflow'),
            );

        return $items;
   }

And this is my callback function for coverflow_admin_form

function coverflow_admin_form($form, &$form_state){

    $form           = array();

    $form['height'] = array(
      '#title' => t('Height'),
      '#type' => 'textfield',
      '#size' => 40,
      '#maxlength' => 120,
      '#required' => TRUE,
    );

    $form['width'] = array(
      '#title' => t('Width'),
      '#type' => 'textfield',
      '#size' => 40,
      '#maxlength' => 120,
      '#required' => TRUE,
    );

    $form['font-size'] = array(
      '#title' => t('Font Size'),
      '#type'   => 'textfield',
      '#size'   => 40,
      '#maxlength' => 120,
      '#required' => TRUE,
    );

    $form['font-family'] = array(
      '#title' => t('Font Family'),
      '#type'   => 'textfield',
      '#size'   => 40,
      '#maxlength' => 120,
      '#required' => TRUE,
    );

    $form['font-color'] = array(
      '#title' => t('Font Color'),
      '#type'   => 'textfield',
      '#size'   => 40,
      '#maxlength' => 120,
      '#required' => TRUE,
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save settings'),
    );

        return system_settings_form($form);

}

After installing module im not getting configuration link at admin/config/coverflow.
What is wrong in this code?
Comment: Url for configuration page is not correct. It should be `admin/config/development/coverflow` or `admin/config/content/coverflow` or for any other section in admin.
Comment: ok let me try this option.
Comment: after changing admin/config/coverflow to admin/config/development/coverflow, my hook_menu link is disappear and complete code is not working.
Comment: Have you clear the cache and test it?
Comment: yes after updating module files, im clearing cache and sometimes uninstalling module and then installing again to check updates. but still its not working.
Comment: How do i debug module file step by step to check where is the actual problem, Is there any technique for debugging module.
Comment: you can use `debug` module to debug site. I have to install your module into my test serve and then let you know.
Comment: ok thanx i will try to debug.
Answer: Got the solution, just have to remove '' from 'MENU_NORMAL_ITEM' because MENU_NORMAL_ITEM is drupal constant for handling Menu Items, after removing '' the code is working as expected.
Comment: I was also adding the same as my answer. But your url was also not proper for configuration page.


Select an element that doesn't have a background set

Question: Using jQuery, I need to select all elements that do not have a background color or image defined, and apply at least a white background to it.
Comment: @tscully, why do you need to use jQuery to do this?
Comment: I'm creating a content slider using jQuery and I would like to make it as user-friendly as possible. e.g. if a designer doesn't set a background color/image, it will set one for them so the slider still looks right.
Comment: Yes, why not define a css rule like *{background-color:#FFF;}
Comment: because if the designer does set a background color/image, that would overwrite their style.
Answer: If you know which specific elements you're looking for you could do something like this:
<code>var els = $('div');

els.each(function(idx, el){
    if ($(el).css('background-color') == '' || $(el).css('background-image') == '')
    {
        $(el).addClass('white-background');
    }
});
</code>
Comment: ok, so this worked great! The only thing now, is that it doesn't detect styles applied via a css file...only inline styles. Any ideas with that?
Comment: ok, nevermind, it is reading the css, but the values are not defaulting to ''. They are defaulting background-color to 'transparent' and background-image to 'none'. So I just adjusted the function above accordingly.

Thanks for your help!


Variable importance from SVM

Question: How to obtain a variable (attribute) importance using SVM?
Answer: If you use l-1 penalty on the weight vector, it does automatic feature selection as the weights corresponding to irrelevant attributes are automatically set to zero. See this paper. The (absolute) magnitude of each non-zero weights can give an idea about the importance of the corresponding attribute.
Also look at this paper which uses criteria derived from SVMs to guide the attribute selection.
Comment: Yes, take a look at the R penalizedSVM package. Other packages of interest are : penalized, elasticnet, ppls, lars, or more generally: http://cran.r-project.org/web/views/MachineLearning.html
Comment: Has any one of those algos been implemented in R or other software?
Comment: The website linked to the first "this paper" is no longer valid
Answer: Isabelle Guyon, André Elisseeff, "An Introduction to Variable and Feature Selection", JMLR, 3(Mar):1157-1182, 2003. http://jmlr.csail.mit.edu/papers/v3/guyon03a.html
is well worth reading, it will give a good overview of approaches and issues.  The one thing I would add is that feature selection doesn't necessarily improve predictive performance, and can easily make it worse (beacuse it is easy to over-fit the feature selection criterion).  One of the advantages of (especially linear) SVMs is that they work well with large numbers of features (providing you tune the regularisation parameter properly), so there is often no need if you are only interested in prediction.
Comment: I would even recommend the entire book from I. Guyon and coworkers, http://j.mp/anblwx. The ESL book from Hastie et al., http://j.mp/bW3Hr4, provides also interesting discussions around this 'hot' topic.
Comment: I disagree with your claim; FS is interesting on its own for some explanatory information that it delivers (marker/SNPs selection is an example when it is a main aim of analysis). The feature selection overfit is of course a problem, but there are methods to omit it.
Comment: I was making the point that FS doesn't necessarily improve predictive importance, and can make it worse.  If finding the informative features is of intrinsic importance, then of course FS should be used, but it may well be that predictive performance is compromised if over-fitting the feature selection criterion occurrs (which happens rather easily).  For tasks like micro-array analysis, I would use (bagged) ridge regression for predictions and something like the LASSO for determing the key features (for gaining understanding of the biology).  There is no need to do both in the same model.
Answer: If you use R, the variable importance can be calculated with Importance method in rminer package. This is my sample code:
<code>library(rminer)
M <- fit(y~., data=train, model="svm", kpar=list(sigma=0.10), C=2)
svm.imp <- Importance(M, data=train)
</code>
In detail, refer to the following link https://cran.r-project.org/web/packages/rminer/rminer.pdf
Comment: This answer is incomplete.  It does not describe what the variable importance in that package is trying to communicate.
Comment: I've added sample code

Variable importance from SVM


how to delete the nodes having a specific entity reference but not the parent

Question: I have a content type "students" and another "courses".
Student nodes are created with an entity reference to a course node.
I need to delete all students of "couses123" in a quick way.
How can I make a view which has a dropdown of all course names and when selected it shows list of all students.
From there, I can use the VBO to show a checkbox next to them for selection and deletion .
How can I achieve this?
Answer: If this a one-time need, you could build a custom query DB query if you wanted to go the developer route. However, if this is an ongoing need you're better off with the site builder route you've hinted at in your question.
In your case, you want to a create a node view filtered by content type Students. Add a Entity Reference: Referenced Entity for the field that contains your references to Courses. The last step is to add an exposed filter for the same Courses field making sure enable relationship that you added; that will present a select list of referenced Courses from which you can filter down the Students and present your VBO delete widget.


high cpu usage on 2811 router

Question: there is high cpu usage and high intterupt rate on 2811 router while there is low rate traffic about 10 Mbit. How can i fix this problem. output of sh processes cpu sorted 5sec | exc 0.00 command that
<code>CPU utilization for five seconds: 82%/80%; one minute: 42%; five minutes: 42%
 PID Runtime(ms)     Invoked      uSecs   5Sec   1Min   5Min TTY Process
 182     1123064    20291509         55  0.79%  1.26%  1.24%   0 HQF Shaper Backg
  19     1223404      345072       3545  0.55%  0.44%  0.44%   0 ARP Input
   2       83312       17269       4824  0.39%  0.16%  0.27%   0 Load Meter
 119      462956       93754       4937  0.15%  0.19%  0.20%   0 IP Input
  64       90784      344541        263  0.07%  0.02%  0.02%   0 Netclock Backgro
 151       52000      130817        397  0.07%  0.06%  0.07%   0 CEF: IPv4 proces
 183      207784      847165        245  0.07%  0.07%  0.07%   0 RBSCP Background
 260      103688      430250        240  0.07%  0.03%  0.02%   0 Atheros LED Ctro
</code>
Comment: 28xx-series routers are famous for allowing complex ACLs to impact their CPU performance - have you got any?
Comment: there are a few ACL configuration which include static route. but there is also vpn configuration to communicate center side 3945 router.is vpn config cause high cpu usage and interrupt?
Comment: Perhaps, they are very low power routers I'm afraid
Comment: Can I check station of power via command.
Answer: Cisco provides a troubleshooting document for this issue, it's relatively useful.
http://www.cisco.com/c/en/us/support/docs/routers/10000-series-routers/15095-highcpu.html

high cpu usage on 2811 router


Centralização de botão incorreta

Question: Eu tenho um button dentro de uma div da qual eu não consigo centralizar ele. Por meio de margin-left eu consigo fazer isso, porém para tratar o mesmo na responsividade é bem massante.
Código do button juntamente com a div:

  <button type="button" title="Adicionar ao carrinho" class="button btn-cart responsivo amarelo" onclick="addCartao('<?php echo $_product->getId(); ?>')" name="cartaoMensagem<?php echo $_product->getId(); ?>" id="cartaoMensagem<?php echo $_product->getId(); ?>">Adicionar</button>

CSS utilizado:
.actions.responsivo {
    text-align: center;
}
.button.btn-cart.responsivo.amarelo {
    color: #ffffff;
    font-size: 12px;
    text-transform: uppercase;
    background: #e7af19;
    border-radius: 3px;
    padding: 7px 50px;
    border: none;
    cursor: pointer;
    display: block;
    margin-top: 19px;
    text-align: center;
    line-height: normal;
    margin-left: 12px !important;
    font-family: 'CoHeadlineCorp-Light';
    font-weight: bold;
}

Answer: Você pode utilizar o transform abaixo para centralizar horizontalmente qualquer elemento:
.centralizado{
  -webkit-transform: translate(-50%,0);
  -moz-transform: translate(-50%,0);
  transform: translate(-50%,0);
  left: 50%;
  position: relative;
}

Comment: Obrigado me ajudou também!
Comment: Deu certo! Além de que irei usar isso para outros elementos também, assim me ajudando muito de todas as formas. Muito obrigado pela ajuda!
Answer: Uma forma bem moderna e simples de fazer essa centralização é utilizando display:flex e aplicando justify-content:center. Acaba até por facilitar se tiver que centralizar mais botões por exemplo, ou centralizar na vertical também com a propriedade align-items.
Exemplo:

.actions.responsivo {
  text-align: center;
  display:flex; /*<----------------- */
  justify-content:center; /*<------- */
}

.button.btn-cart.responsivo.amarelo {
  color: #ffffff;
  font-size: 12px;
  text-transform: uppercase;
  background: #e7af19;
  border-radius: 3px;
  padding: 7px 50px;
  border: none;
  cursor: pointer;
  display: block;
  margin-top: 19px;
  text-align: center;
  line-height: normal;
  margin-left: 12px !important;
  font-family: 'CoHeadlineCorp-Light';
  font-weight: bold;
}

  <button type="button" title="Adicionar ao carrinho" class="button btn-cart responsivo amarelo" onclick="addCartao('<?php echo $_product->getId(); ?>')" name="cartaoMensagem<?php echo $_product->getId(); ?>" id="cartaoMensagem<?php echo $_product->getId(); ?>">Adicionar</button>

Documentação:

display:flex
justify-content

Centralização de botão incorreta


Finding rank of matrix from its characteristic polynomial

Question:
I have to find rank of the matrix from the given characteristic polynomial.
Here is my attempt.

Now how to check other possibilities of rank ?
Comment: Do you know  the Jordan normal form of a matrix?
Comment: Jordan form is probably overkill here.
Answer: Recall that the geometric multiplicity of an eigenvalue is less than or equal to the algebraic multiplicity. In your case, since $\beta \neq 0$, the algebraic multiplicity of the eigenvalue $0$ is $3$ and the geometric multiplicity of $0$ is given by $\dim \ker A$ so we have $1 \leq \dim \ker A \leq 3$ (or equivalently, $2 \leq \operatorname{rank} A \leq 4$). By playing with examples of block diagonal matrices, you can see that indeed all three cases are possible.
Comment: You mean the geometric multiplicity of eigenvalue 0 is numerically equal to the nulity of matrix ?
Comment: @AbhishekChandra: Yep.
Answer: Hint: The eigenspace associated with the eigenvalue $0$ is also the kernel (AKA nullspace) of $A$.  How does this relate to the rank?  What are the possible dimensions of this eigenspace?
Comment: @JEFFYELTON I think this is the best answer possible (provided one doesn't do all the work for the OP).
Comment: I feel like this should be a comment, not an answer.
Comment: @JEFFYELTON I respectfully disagree, pending feedback from the asker.


Is there a way to conditional sort criteria in views

Question: Is there a way to make a conditional sort criteria with views in D7?
I want to do the following stuff:
<code>if node property = x
  use this sort criteria
else
  use this one
</code>
Answer: You could achieve this using Views PHP.
Excerpt from the page:

This module allows the admin account (user 1) to add fields, filters
  and sorts to views which use PHP code. It also supports additional
  accounts when the core PHP Filter module is enabled.
Comment: You could use this module but I think putting PHP into the database is poor form.
Answer: <code>/**
 * Implements hook_views_query_alter().
 */
function HOOK_views_query_alter(&$view, &$query) {
  if ($view->name == 'view_name' && $view->current_display == 'page') {
    $query->orderby = array(
      array(
        'field' => 'CASE WHEN node.property = x THEN node.y ELSE node.z END',
        'direction' => 'ASC',
      )
    );
  }
}
</code>
https://www.drupal.org/node/2133879


Java 3D Texture Deformation

Question: I have a question about Texture 2D on QuadArray 3D.
When I apply a texture on quad array the result is not what I expect:
Texture on quadarray I have
Since the quad is plane I expect a more smooth texture deformation:
Texture I expect
Here is the code I use:
<code>public class Test3D extends JPanel {
SimpleUniverse simpleU;
static boolean application = false;

public BranchGroup createSceneGraph() {
    BranchGroup objRoot = new BranchGroup();
    Appearance polygon1Appearance = new Appearance();

    GeometryInfo polygon1 = new GeometryInfo(GeometryInfo.QUAD_ARRAY);

    polygon1.setTextureCoordinateParams(1, 2);

    polygon1.setCoordinates(new Point3f[]{new Point3f(0f, 0f, 0f), new Point3f(2f, 0f, 0f), new Point3f(3f, 3f, 0f), new Point3f(-3f, 3f, 0f)});

    polygon1.setTextureCoordinates(0, new TexCoord4f[]{new TexCoord2f(0.0f, 0.0f)//
            , new TexCoord2f(1.0f, 0.0f) //
            , new TexCoord2f(1.0f, 1.0f) //
            , new TexCoord2f(0.0f, 1.0f)});

   NormalGenerator normalGenerator = new NormalGenerator();
   normalGenerator.generateNormals(polygon1);

   Texture texImage = new TextureLoader("texture.png", this).getTexture();

   polygon1Appearance.setTexture(texImage);
   objRoot.addChild(new Shape3D(polygon1.getGeometryArray(), polygon1Appearance));
   return objRoot;
}

public Test3D() {
    init();
}

public void init() {
    setLayout(new BorderLayout());

    Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    add("Center", c);
    BranchGroup scene = createSceneGraph();
    simpleU = new SimpleUniverse(c);
    TransformGroup tg = simpleU.getViewingPlatform().getViewPlatformTransform();
    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(0f, 0f, 10f));
    tg.setTransform(t3d);
    scene.compile();
    simpleU.addBranchGraph(scene);
}

public static void main(final String[] args) {
    application = true;
    JFrame frame = new JFrame();
    frame.setSize(500, 500);
    frame.add(new Test3D());
    frame.setVisible(true);
}}
</code>
Does anybody have any solution to solve this problem?
Comment: Hello, The shape is a plane trapeze , there is not any triangle. By changing positions, i have almost the same result as if Java3d split the trapeze into 2 triangles and put the texture on it...
Comment: *as if Java3d split the trapeze into 2 triangles and put the texture on it* [spoiler: if not by Java, then by your graphics card] you'll have to deal with that fact.
Comment: The issue is explained here: [http://www.xyzw.us/~cass/qcoord/](http://www.xyzw.us/~cass/qcoord/)
Answer: Unfortunatly, the solution in http://www.xyzw.us/~cass/qcoord/ works for trapezoidal geometry, but for non specific quads this solution does not work.
Ex :
<code>  polygon1.setCoordinates(new Point3f[]{new Point3f(0f, 0f, 0f), new Point3f(2f, -1f, 0f), new Point3f(3f, 3f, 0f), new Point3f(-4f, 3f, 0f)});
</code>

Java 3D Texture Deformation


How to find if infinite series converges uniformly using M test.

Question: So, here is the series: $$\Sigma_{k=1}^{\infty} \arctan(x^6ne^{-nx^2})$$
I try to find sup series by deriving the main term:
 $$\frac{1}{1 + (x^6ne^{-nx^2})^2} \cdot n(6x^5e^{-nx^2} - 2nx^7e^{-nx^2})$$
but I think I did something incorrectly because I can't express $x$ out of this. Function $\arctan x$ is not bounded by a constant so I cannot get the sup series by bounding the function, maybe I can use as a bounding series something like $\sum^{\infty}_{k=1} \arctan(x^6 n )$, but still I don't know how to proceed.
Answer: Let $[a,b]\subset\mathbb{R}$ and $x\in[a,b]$ then
$$ \forall x\in\mathbb{R},\left|\arctan\left(x^6ne^{-nx^2}\right)\right|\leqslant x^6n e^{-nx^2}\leqslant b^6ne^{-na^2} $$
and the series $\sum b^6ne^{-na^2}$ converges, thus the series converges uniformly on the segments of $\mathbb{R}$.
Comment: How do you know that the series $\Sigma b^6 ne^{-na^2}$ converges?
Comment: Because it is a $o\left(\frac{1}{n^2}\right)$.
Comment: @Tuvasvien Do you mean that $\frac{1}{n^2} \ge b^6ne^{-n^2} \forall n \in N$? Also, is it because $n^2 \le c^n$, where $c \in R$?
Comment: It is because $\lim\limits_{n\rightarrow+\infty}n^2\times b^6ne^{-na^2}=0$.
Comment: Here is some elaboration on the inequality $|\arctan(t)|\leq t$. We may use the Mean Value Theorem to obtain $|\arctan x - \arctan y| = |\frac{1}{1+z^2}(x-y)|\leq |x-y|$ where $z$ is some number in between $x$ and $y$. Plugging in $(x, y) = (t, 0)$ gives the desired bound.


How do I convert this curl request to Apache HttpClient java code?

Question: I'm trying to send this in java:
curl -H "Content-Type:application/json" -XPOST 'http://www.example.com/foo' -d '{"rootURL": "http://www.subway.com"}'

Here's the code I have:
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.example.com/foo");

    post.setHeader("Content-Type", "application/json");

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("rootURL", "http://www.subway.com"));
    post.setEntity(new UrlEncodedFormEntity(urlParameters));
    client.execute(post);

but I'm getting a 400 error:
Unexpected character ('r' (code 98)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: org.apache.catalina.connector.CoyoteInputStream@a18ba7b; line: 1, column: 2]

If I change this line:
urlParameters.add(new BasicNameValuePair("rootURL", "http://www.subway.com"));

to
urlParameters.add(new BasicNameValuePair("bootURL", "http://www.subway.com"));

I get the following error:
Unexpected character ('b' (code 98)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: org.apache.catalina.connector.CoyoteInputStream@a18ba7b; line: 1, column: 2]

Does anyone know what the problem is?
Comment: possible duplicate of [HTTP POST using JSON in Java](http://stackoverflow.com/questions/7181534/http-post-using-json-in-java)
Answer: for some reason it works if I replace:
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("rootURL", "http://www.subway.com"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));

with:
post.setEntity(new StringEntity("{\"rootURL\":\"http://www.subway.com\"}", "UTF-8"));

Here's the link I found this suggestion from, but it doesn't answer why. Can anyone explain why it won't work with UrlEncodedFormEntity?
Android: Http post with parameters not working
Comment: My guess is when using UrlEncodedFormEntity the key/value pairs are added into the request URL. However, remote server looks for key/value pair in the request body formatted as json which the StringEntity does.


Output Custom Field in Drupal 8

Question: How can I output a custom field in Drupal 8 (Bootstrap theme)? Editing the field.html.twig file, if I have a custom field named field_title I thought
{{content.field_title}}

Would display it, but it doesn't. I've also tried:
{{item.content['#field_title']}}

To no avail.
The only thing that's worked for me is:
{{items[0]}}

But that picks up the custom field by its index number, instead of its associative name in the array.
What am I doing wrong?
Answer: {{ content.field_title }}
This should work if you've got a field with that machine name.  If you're trying to print the title, the current syntax is:
{{ label }}

An easier way to figure out if the variables that you are trying to use is actually usable is to download the Devel module and enable Kint. Then in your .html.twig template file, you can enter:
{{ kint(content.field_myCustomfield) }}

and you would get an array with all the available variables.  If you don't want to install the devel module, you can just use:  I would recommend Devel/kint, it makes everything prettier and easier
{{ dump() }}

You're only going to need to dig into the array if you're trying to do something really custom. Eg: I used the following code in one of my projects to dig down into a link field so that i can output the exact url into an A tag inorder to wrap an entire region in a link.

You can read more about outputting Variables in twig templates here:
https://sqndr.github.io/d8-theming-guide/twig/twig-debug.html
and on drupal.org here: https://www.drupal.org/theme-guide/8
Answer: I found out my problem was I was editing the wrong template file. The file I needed to edit was:
/themes/bootstrap_subtheme/templates/node/node.html.twig

Once I added {{ content.field_title }} there, it worked!

Output Custom Field in Drupal 8


2nd derivative of $\sigma^2$ in normal MLE

Question: Many times I differentiated the MLE of the normal distribution, but when it came to $\sigma$ I always stopped at the first derivative, showing that indeed:
$$\hat\sigma^2 = \frac{\sum(y_i-\bar y)^2}{n}
$$
But I haven't seen anywhere a proof this is indeed a maximum point. I tried now to differentiate it again, and look at the the 2nd derivative on that point, and yet I get (unless I made a mistake) that for some small value of $\sum(y_i-\bar y)^2$ the 2nd derivative on this point can actually be positive.
So how do you prove that $\hat\sigma^2$ is indeed a maximum?
Comment: Use $\eta=\sigma^{-2}$ as your parameter, in which case the second derivative is $-n/2\eta^2$, which is negative.
Answer: The calculation of the derivative of the log-likelihood is shown here.
From there, you can find the second derivative is $$\frac{n}{\sigma^2}(1-\frac{3}{\sigma^2} \hat{\sigma}^2)$$
If you plug in $\hat{\sigma}^2$ for ${\sigma}^2$, then you get $\frac{n}{\hat{\sigma}^2}(1-3)=-2 \frac{n}{\hat{\sigma}^2}$


Usando Linq to sql, como eu obtenho a consulta sql gerada?

Question: Olá.
Se eu tiver um exemplo:
        var agendamento = _repository.First(a => a.iid == id
                                 && a.locIid == _usuario.LocalidadeContextoId
                                 && a.activeVersion == 0, "", true);

Consigo saber a consulta SQL gerada?
Comment: @VirgilioNovic banco oracle
Comment: Qual é o banco de dados?
Comment: @itasouza você está usando `Entity Framework`?
Comment: @VirgilioNovic sim
Answer: Utilizando o IQueryable, é possível obter o SQL gerado.
IQueryable<SuaClasse> query = _repository.First(a => a.iid == id
                             && a.locIid == _usuario.LocalidadeContextoId
                             && a.activeVersion == 0, "", true);

 var querySql = query.ToString();

Answer: Faça o seguinte no seu context:
public DatabaseContext() : base("MeuContext")
    {
        //Trecho abaixo utilizado para ver as querys geradas pelo Entity
        #if DEBUG
        Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
        #endif
    }

Esse trecho System.Diagnostics.Debug.WriteLine(s) vai fazer com que toda query gerada apareça no Output quando você estiver debugando.
A resposta que utiliza o IQueryable também é válida, mas geralmente é melhor usar da forma que apresentei aqui, pois você quer conferir como ficou sua consulta durante o desenvolvimento e manutenção da aplicação para garantir que está trazendo as informações corretamente.
Claro que isso não tira a validade da opção usando IQueryble e dependendo do que deseja ele pode ser mais útil.


Detect stationary section in an overall non-stationary time series

Question: I'm working with a time series data from neuron firing rate. This time series is usually overall non-stationary due to experimental noise. However, I want to extract stationary section in this overall non-stationary time series. People in the field usually just distinguish stationary section manually.
However, I want to implement a program that can automatically extract stationary section from a time series. Can you suggest some statistics test or algorithms that can regconize stationary section in a time series? For example, in this graph, I want to extract the stationary section from 400 to 1200. Thank you very much!
Comment: Could you please explain to us what you mean by a "stationary section"?  In a time series context this refers to an invariance property of the joint distribution under a time shift, but that is such a severe condition that I suspect you might mean something far less restrictive, such as "constant mean."
Comment: @Stats Does your suggested approach assume there are no outliers as unusual values can distort changes in intercepts (level/step shifts) or changes in slopes(trends) . Does it make assumptions about the form of the arima structure ? Does it make assumptions about the error variance changing either stochastically or deterministically at particular points in time ( not necessarily where the expected value changes) ? Does it extend to models with stochastic predictors or is it a pure univariate approach.. Upon re-reading tour comments it appears that you are suggesting that one stabilize the vari
Comment: Assumes $f_t$ is a deterministic, one-dimensional, piecewise-constant signal (note trends can be accommodated too) with change-points whose number $N$ and locations $η_1,...,η_N$ unknown. Also that random sequence $\{ε_t\}$  i.i.d. Gaussian with mean zero and variance 1 and that The sequence $\{f_t\}$ bounded. A few other technical assumptions wrt locations of change points. No ARIMA structure is assumed, variance can be stabilized (e.g. via  Haar-Fisz transform) prior to detection. For further info do read paper https://arxiv.org/pdf/1411.0858.pdf
Comment: @Stats Do you have to pre-specify  T and can you specify a level of confidence OR a minimum step size in order to minimize false positives or meaningless shifts ?. These things and others including variance change break-points are discussed here http://docplayer.net/12080848-Outliers-level-shifts-and-variance-changes-in-time-series.html some 30 years ago while your reference is 5 years ago. If you are not identifying possible companion arima structure your suggested approach could easily be confused by stochastic trends which appear to be assumed away in this approach.
Comment: @IrishStat There is a minimum step size, and it is the one we see in statistical signal processing literature. Specifically, the magnitudes $f'_i$ of the jumps satisfy $\min_{i=1,...,N} f′_i ≥ \underline{f}_{T}$ , where  $\sqrt{\delta_T}\underline{f}_{T}≥ C \sqrt{\log T}$ for a large enough C, and $\delta_T$ is the lower bound of the minimum spacing between changepoints.  This paper assumes constant variance, but variance can be stabilised, break- points is what this algorithm identifies. The whole essence of the procedure is piecewise and ARIMA can't be a good model for this case.
Comment: Obviously no algorithm is perfect, their efficiency depends on the extend to which the underlying assumptions are satisfied. It is also true that efficient drops when there are many moving parts. But we don't always have to use complex assumptions and models, sometimes we can afford use simpler ones and when we can, that may come with gains in efficiency.
Comment: ARIMA structure handles the within group relationship between successive values. It is always useful even if it is (0,0,0)(0,0,0) reflecting zero auto-correlation within a set of contiguous values for each of the possible T groups. I understand that the user can not set a minim step size or a minimum group size ...the algorithm dictates that
Answer: First, as I don't see any long stationary section on your data (the part  "from 400 to 1200" is definitely not stationary) I have come to understood that what you do look for is for areas where the trend evolves in a similar way.
One way to so is by using a change point algorithm. One such algorithm can be found at P. Fryzlewicz, WILD BINARY SEGMENTATION FOR MULTIPLE CHANGE-POINT DETECTION (note there is a package called WBS available at CRAN)
Consider a simple model with change-points of the form
\begin{equation}
X_t = f_t + ε_t, \qquad t = 1, . . . , T,
\end{equation}
where $f_t$ is a deterministic, one-dimensional, piecewise-constant signal with
change-points whose number N and locations $η_1, . . . , η_N$ are unknown. The
sequence $ε_t$ is random and such that $\mathbb{E}(ε_t)$ is exactly or approximately zero. A suitable algorithm would estimate $N$ and $η_1, . . . , η_N$ under various assumptions on $N$, the magnitudes of the jumps and the minimum
permitted distance between the change-point locations.
So WBS works this way:

In the first stage we randomly draw a number of subsamples i.e.
  vectors $(X_s, X_{s+1}, . . . , X_e)$, where $s$ and $e$ are integers
  such that $1 ≤ s < e ≤ T$, and compute the CUSUM statistic on each
  subsample. We then maximise each CUSUM, choose the largest maximiser
  over the entire collection of CUSUMs, and take it to be the first
  change-point candidate to be tested against a certain threshold. If it
  is considered to be significant, the same procedure is then repeated
  recursively to the left and to the right of it. The hope is that even
  a relatively small number of random draws will contain a particularly
  'favourable' draw in which, for example, the randomly drawn interval
  $(s, e)$ contains only one change-point, sufficiently separated from
  both $s$ and $e$: a set-up in which our CUSUM estimator of the
  change-point location works particularly well as it coincides with the
  maximum likelihood estimator (in the case of $ε_t$ being i.i.d.
  Gaussian). We provide a lower bound for the number of draws that
  guarantees such favourable draws with a high probability. Apart from
  the threshold-based stopping criterion for WBS, we also introduce
  another, based on what we call the strengthened Schwarz information
  criterion.

And that is a practical example

However note, the assumptions of the algorithm might not be satisfied by your data (especially the constant Gaussian variance of the error term). Hence, you may want to stabilize the variance first for more efficient estimation of the change-points.
Comment: ....................................................................................................


MLE for a constant that is not an explicit parameter of the distribution

Question: The following is a homework question from an undergrad intro stats class.
"Suppose we have a random sample $Y_1, \dots, Y_n$ from the shifted exponential distribution
$$
f(y|\theta) = \begin{cases}
e^{-(y-\theta)} & y \geq 0 \\
0 & y < 0.
\end{cases}
$$
Find MLE for $\text{Var}(Y)$, where $Y \sim f$."
Now I was under the impression that MLEs are only defined for the explicit parameters $\theta$ of the distribution, whereas here $\text{Var}(Y) = 1$ is not a parameter of $f$ and indeed doesn't even depend on $\theta$. So in my view asking for the MLE of $\text{Var}(Y)$ is not a well-posed question.
On the other hand, my friend argued that the MLE for $\text{Var}(Y)$ does exist and is equal to $1$. His reasoning was that the likelihood function is not defined except when $\text{Var}(Y) = 1$, and so $1$ is trivially the maximum likelihood estimate.
Whose view is more correct? And as a side question, do you think this question is too focused on a technical detail of maximum likelihood estimation to be included on a first homework about MLEs?
Answer: Well, it looks like a strange question for a first homework set on mle. But, technically, the answer you gave, 1 (the constant function 1) seems to be correct. Maximum likelihood is not only defined for "explicit parameters", whatever that is. We can define a (finite-dimensional) parameter of a distribution as a functional defined on the distributions (in the given family). This way:  $\theta = \theta (F)$, in your example we have $\sigma^2 = \sigma^2(F)$, the functional that to each $F$ (distribution function) assigns the value $\DeclareMathOperator{\E}{\mathbb{E}} \sigma^2(F) = \E (X-\E(X) )^2$ where $X$ is a random variable with the distribution $F$.
For the invariance property of mle useful in this context, see Invariance property of maximum likelihood estimator?


Why QTcpSocket doesn't write ? I need to flush it to make it send my data

Question: I read in the documentation that to send a packet with QTcpSocket I can just use the write method.
But when I do it, my server doesn't receive it. I need to call flush or waitForByteWritten to send it.
TCPClient::TCPClient(Client *babel , const std::string &hostname, unsigned short port, QObject *parent) : QObject(parent), client(babel),  hostName(hostname), port(port), is_connected(false)
{
  this->tcpSocket = new QTcpSocket(this);
  connect(this->tcpSocket, SIGNAL(readyRead()),this, SLOT(readMessage()));
  connect(this->tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
  connect(this->tcpSocket, SIGNAL(connected()), this, SLOT(connectReady()));
  connect(this->tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnectedReady()));

}

TCPClient::~TCPClient()
{

}

bool TCPClient::initiateService()
{
  this->tcpSocket->connectToHost(this->hostName.c_str(), this->port);
  this->tcpSocket->waitForConnected(5000);
}

void TCPClient::connectReady()
{
  this->is_connected = true;
}

void TCPClient::disconnectedReady()
{

}

bool TCPClient::sendBabelPacket(Protocol::BabelPacket &packet)
{
  if(this->tcpSocket->state() == QAbstractSocket::ConnectedState && is_connected)
  {
    std::cout << "writing ... size : " << sizeof(Protocol::BabelPacket) + packet.dataLength<<std::endl;
    this->tcpSocket->write((const char *)&packet, sizeof(Protocol::BabelPacket) + packet.dataLength);
    //this->tcpSocket->flush();
    return true;
  }
  else
  {
    std::cout << "socket close" << std::endl;
    return false;
  }
}

void TCPClient::shutDown()
{
  this->tcpSocket->abort();
}

void TCPClient::displayError(QAbstractSocket::SocketError socketError)
{

}

void TCPClient::readMessage()
{
  char buffer[sizeof(Protocol::BabelPacket)];
  this->tcpSocket->read(buffer, sizeof(Protocol::BabelPacket));
}

When I call write, it returns the right amount of bytes.
I use this class in a GUI context, in the same thread.
Comment: Can you provide a link to the documentation that you're referring to? Your summary sounds mistaken in several different ways (for example, packets are a TCP implementation thing and have nothing to do with this API) and I think I could clear up your misconceptions better if I could point out what the documentation you were looking at actually say. (Assuming it's correct, which .... who knows?)
Answer: QTcpSocket is a buffered device, so data is not written directly, but into internal buffer. Documentation states:

Note: TCP sockets cannot be opened in QIODevice::Unbuffered mode.

Because of it you should call flush or waitForBytesWritten to be sure something is transferred.
There is the difference between them. flush writes as much as possible from the internal write buffer to the underlying network socket, without blocking. waitForBytesWritten blocks until at least one byte has been written on the socket and the bytesWritten() signal has been emitted.
Comment: @DimitriDanilov `readyRead()` emitted when new data arrived, are you sure your server sends response?
Comment: @DimitriDanilov For GUI applications Qt signals rely's on the event-loop started during initialization of `QApplication`. `readyRead()` should be emitted if new data arrived and event-loop processes signals. Check that no long operations executed on your GUI thread and check that your `tcpSocket` is not `disconnected()` and there is no `error()`.
Comment: @DimitriDanilov Try to call `QApplication::processEvents()` after `flush()` and return from `sendBabelPacket` to see if some pending `readyRead()` event is not processed by event-loop.
Comment: I was just stupid, I instanciated my network before instanciating the QApplication... Thanks for your help anyway !
Comment: Ok, I thought that it was because my write isn't working fine that readyRead is never emmited, but that doesn't seems to be the case ... Have you an idea why it is never emitted ?
Comment: Yes I am sure, readyRead() is emmited only when I send my buffer with waitForBytesWritten(), but it is blocking ... When I flush the data arrived to my server, but I don't get his response because readyRead() isn't triggered. Maybe when I am in the GUI event loop it can't be triggered ?
Comment: I start my application with app.exec(), that's all I do. My GUI is doing nothing when I should get a request, and I don't get any errors.


Multiple inheritance using Super(Subclass, self) in python 2.7

Question: I'd like to understand if multiple inheritance is allowed in Python 2.7 from a class whose parent is not an object ?
Ref:TypeError in Python single inheritance with "super" attribute do provide some examples but I'd like to use super(Subclass, self) differently as below
Animal --> Mammal --> CannoFly & CannotSwim --> Dog
So Dog class inherits from CannotFly and CannotSwim classes. Each of the CannotFly and CannotSwim class inherits from Mammal which inherit from Animal classes
    class Animal:
    def __init__(self, animalName):
        print(animalName, 'is an animal.');

# Mammal inherits Animal
class Mammal(Animal):
    def __init__(self, mammalName):
        print(mammalName, 'is a mammal.')
        super(Mammal,self).__init__(mammalName)

# CannotFly inherits Mammal
class CannotFly(Mammal):
    def __init__(self, mammalThatCantFly):
        print(mammalThatCantFly, "cannot fly.")
        super(CannotFly,self).__init__(mammalThatCantFly)

# CannotSwim inherits Mammal
class CannotSwim(Mammal):
    def __init__(self, mammalThatCantSwim):
        print(mammalThatCantSwim, "cannot swim.")
        super(CannotSwim,self).__init__(mammalThatCantSwim)

# Dog inherits CannotSwim and CannotFly
class Dog(CannotSwim, CannotFly):
    def __init__(self,arg):
        print("I am a barking dog")
        super(Dog, self).__init__(arg)

# Driver code
Dog1 = Dog('Bark')

When I run it I get the error  "TypeError: must be type, not classobj" which is because the CanotSwim() & CannotFly() classes are derived from Mammal which is not the base class but inheriting from Animal class.  If that was not the case, then the Super(Subclass, self) works perfectly.
Comment: Obligatory ["Python 2 is no longer supported and you should upgrade as soon as possible"](https://www.python.org/doc/sunset-python-2/) comment.
Comment: To inherit try looking for super().__init__
Answer: In Python 2, super does not work with objects that don't inherit (directly or indirectly) from object.
Multiple inheritance is allowed with old-style classes, but may have unexpected behavior due to the Diamond Problem (see section 2.3 here).
For these reasons (and many others) it is recommended to always inherit  from object in Python 2.
Comment: Thanks @0x5453 very much for the reference , I understand the classic Diamond problem but what I'm unable to understand is was it allowed to have multiple inheritance from two classes which inherited from another class which inherited from another class /object. The reason is I can deal with the functional attribute/method overlook as in the Diamond problem but the code throws a run-time error and doesn't execute. Or simply I would be interested to understand best way to implement multiple inheritance in Python 2.7 for a common parent class , if at all it is possible.