opencv - prediction collision of ball

Question: I want to do a project, which will consist in detecting possible collision of the pool balls, using opencv, webcam and C++ programming language. For now I just want to  prediction collision of 2 balls on minibilard table. I detect them by change rgb to hsv and then use thereshold, in future i will probably use another method for detect a random amount of balls, but it's not so important now.
So, for now I can detect two balls, i know their position, radius, now I'm thinking how to  predict whether there will be a collision between them, if we if we assume that they will move in straight lines. I think that I should check their position in every frame update (and i have to know a time between frames in my webcamera) and by that, i I will be able to determine the value of speed, acceleration and direction of the ball. So, if i will know those parameters for for both balls, I will be able to determine where can they collide, and then, using parametric equastion I will be able to check, if they will be on collision point on the same time.
I wonder if this is the right approach to the problem, maybe there is a simpler and more effective method to do this?
Thanks for any kind of help.
Karol
Comment: Sounds like you're on the right track for your project, but on the wrong track for a good SO question. This question is too broad. Please see [help/on-topic] and [tour].
Comment: if you assume the movement is linear, then yes, what you suggest should work. however you should know that you can find other factors harder to predict, like deceleration of the ball. But you can always create at least 2 lines  (one for each side of the ball), and see if it intersects a circle (the other ball). This will tell you at least if the trayectory will lead to a collision, then you need to predict is where it will stop, if it is before or after the other ball... I think that part is the hard one, unless you have enough frames to calculate the deceleration.
Answer: This sounds like you are on track for a good project...
Calculating acceleration seems, from what I briefly read here, reasonably difficult though. So as a preliminary step, you could just assume a constant velocity. So take the difference between a balls position last frame and current frame as a vector and add it on to the current frames position to find where it will be next frame. Doing this for both balls will allow you to check for a collision.
You can check for a collision by comparing the distance between the balls centers using Pythagoras to the sum of their radii. If the sum of their radii will be greater than the distances between their centers, you have a collision.
Obviously, calculating one frame ahead is not very useful, but if you assume a constant velocity or manage to calculate their acceleration, there is no reason to why you can't calculate 30 or 100 frames in the future with this method.
I recently made a billiards ball simulation in javascript which you could take a quick look at here if you want to see how this could work.
Comment: Thanks for your answer.

For now I just wanna test my calculations so I've just made a simple animation, rectangle moves with constans speed, and i just  wanted to check the speed in every frame, I'm quite confused becouse speed value are between 0.136859 pixel/msec to 0.0113137 pixel/msec. It's a simple animation with one yellow rectangle and black background so it's not becouse of noise or something like this, so i really don't know, why i get those results.

opencv - prediction collision of ball


Impressora Matricial não reconfigura o tamanho do papel

Question: Estou utilizando uma impressora matricial, para impressão de recibos. Instalei a mesma como Genérica somente texto. Mas envio a impressão, ela reconhece a altura como a de papel A4. Como fazer para que a impressora corte o papel quando não houver caracteres.
Segue o código usado:
<code> public class CPrint
    {
        protected StringReader stringToPrint;
        protected Font printFont;

        public int altura { get; set; }

        public int tam { get; set; }

        public CPrint()
        {
        }

        public void PageCreate(string printerName, string file)
        {
            CFormatacao formatar = new CFormatacao();
            StringBuilder sb = new StringBuilder();
            string qs = "";
            //try
            //{
                string texto = "";
                int qtdLinha = 0;
                StreamReader arq = new StreamReader(file, System.Text.Encoding.Default, false, 512);

                while (!arq.EndOfStream)
                {
                    texto += arq.ReadLine() + System.Environment.NewLine;
                    qtdLinha++;
                }

                //for (int i = 0; i < 10; i++)
                //{
                //    texto += System.Environment.NewLine;
                //}

                texto = formatar.RemoveAcentos(texto);
                altura = qtdLinha +10;
                arq.Close();

                stringToPrint = new StringReader(texto);
                printFont = new Font("Arial", 12);

                PrintDocument doc = new PrintDocument();

                doc.PrinterSettings.PrinterName = printerName;

                tam = 827 * altura;
                tam = formatar.ConverteEmInteiro(Math.Round(tam/75M).ToString());
                //Configura um novo papel
                PaperSize ps = new PaperSize("MT", 827, tam);
                doc.DefaultPageSettings.PaperSize = ps;

                doc.PrintPage += new PrintPageEventHandler(this.PagePrint);
                // print the page
                doc.Print();
                stringToPrint.Close();

                qs = "~/frmMensagem.aspx?msg=Recibo Impresso!";

            HttpContext.Current.Response.Redirect(qs);
        }

        private void PagePrint(object sender, PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float linePosition = 0;
            int lineCount = 0;

            float leftMargin = 5;
            float topMargin = 5;
            String line = null;

            linesPerPage = altura;//e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            //linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);

            while (lineCount < linesPerPage && ((line = stringToPrint.ReadLine()) != null))
            {
                linePosition = topMargin + (lineCount * printFont.GetHeight(e.Graphics));
                e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, linePosition, new StringFormat());
                lineCount++;

                if (true)
                {

                }

            }
            line = null;

            if (line != null )
                e.HasMorePages = true;
            else
                e.HasMorePages = false;

        }
    }
</code>
Comment: Olá! Dá uma olhada nesse link. Talvez te ajude! [impressora-matricial-não-reconfigura-o-tamanho-do-papel](http://pt.stackoverflow.com/questions/91445/como-montar-uma-p%C3%A1gina-para-impress%C3%A3o-dentro-do-c%C3%B3digo-da-aplica%C3%A7%C3%A3o)
Answer: Você pode configurar a impressora ao invés de fazer via código, coloque o tipo de papel como formulário continuo alemão e corte com quebra :
Comment: Além desse código na imagem.
Comment: http://www.hetec.inf.br/base/diminuir-o-avanco-de-papel-no-driver-de-impressora-generica-do-windows/
Comment: É isso mesmo Igor Baesse. Obrigado!
Answer: Assumindo que você esteja usando o Windows, vá no painel de controle e escolha a opção "Impressoras e Scanners", na "Preferencias de impressão" da referida impressora (aonde você costuma escolher o tamanho da pagina da impressora), existe uma opção para criar um tamanho de pagina, clique nele e define o tamanho conforme a sua necessidade.


How to replace table attributes?

Question: Well I have the following HTML:

So I validated my HTML document and I found that these are obsolete. But if I remove them my website alignment won't work as I want. I need a good enough way to replace the attrbutes with CSS code.
Comment: possible duplicate of [How to set cellpadding and cellspacing in CSS?](http://stackoverflow.com/questions/339923/how-to-set-cellpadding-and-cellspacing-in-css)
Comment: why dont you just add width to the table itself? For Instance, ``?
Comment: @NathanLee , he is looking to take it OUT of the table markup?
Answer:

Answer: You have to write in css this things:
border-collapse: collapse;
border-spacing: 0px;
width: 100%;

How to replace table attributes?


Evaluating time series clustering through EM/GMM

Question: I perform clustering of time series into k=[2,N] number of clusters by using either DTW+kmedoids or DTW+single linkage+hierarchical clustering (HC), as advised in a previous post:  Dynamic Time Warping Clustering
Regarding the evaluation of optimum number of clusters, I want to use expectation–maximization (EM) Gaussian Mixture Models (GMM) and determine the k that maximizes the log-likelihood for each approach.
My questions are:

What should be the input dataset in EM/GMM? The DTW similarity distance
or the raw dataset?
Can the initializations in EM/GMM be the clusters
centers of either the kmedoids or randomly selected seeds from the HC
clusters?
Should I run k-fold cross-validation of the input dataset with the EM/GMM and return the average log-likelihood value?
Comment: In fact..based on [this paper](http://DOI:%20http://dx.doi.org/10.1109/TSG.2015.2409786) you can cluster time series with GMM/EM. I am working on how to implement with the well-known scikit-learn tools however. For an evaluation of hte optimal number of clusters, BIC/AIC are what you want to observe. These are also in the library for convenience. The [link](https://jakevdp.github.io/PythonDataScienceHandbook/05.12-gaussian-mixtures.html) contains a great summary of how to use GMM/EM on certain data but not time series unfortunately. I'd like to hear of your updates as well.
Answer: Gaussian Mixture Modeling assumes ypur input data are coordinates in $R^d$ and contain Gaussian-shaped clusters. I don't think you can use this on time series.
Also, GMM is a clustering approach on its own, and I don't see how you could use that to evaluate other clusterings.
Comment: There was some research on energy data for outliers detection with time-series, which they used EM/GMM in this [paper](https://www.mdpi.com/1996-1073/8/11/12337) in cycled after applying z-score. However, I tried to re-implement it on similar energy data unsuccessfully. They amid to refill the gaps by this approach.


Solving $\sin\left(\frac{x}{x^2+1}\right)+\sin\left(\frac{1}{x^2+x+2}\right)=0$

Question:
I'm looking for a more elegant way to find the real solution
$$\sin\left(\frac{x}{x^2+1}\right)+\sin\left(\frac{1}{x^2+x+2}\right)=0.$$

Here is my way:
First write the equation as
$$\sin\left(\frac{x}{2(x^2+1)}+\frac{1}{2(x^2+x+2)}\right)\cos\left(\frac{x}{2(x^2+1)}-\frac{1}{2(x^2+x+2)}\right)=0.$$
Then the real solution can be find by
$$\frac{x}{2(x^2+1)}+\frac{1}{2(x^2+x+2)}=0$$
which gives $x=-1$.
Answer: I do not know if this is more elegant.
Let $f(x)=\sin x$, $g(x)=\frac{x}{x^2+1}$, and $h(x)=\frac{1}{x^2+x+2}$.
Then your equation becomes
$$f\left(g(x)\right)+f\left(h(x)\right)=0$$
Since $f(x)=\sin x$ is an odd function,
$$-f\left(h(x)\right)=f\left(-h(x)\right).$$
Then, your equation is that
$$f\left(g(x)\right)=f\left(-h(x)\right).$$
It is known that in the range $\left(-\frac{\pi}{2},\frac{\pi}{2}\right)$ the function $f(x)$ is increasing, and since $-\frac{1}{2}\leq g(x)\leq\frac{1}{2}$ and $0<h(x) \leq \frac{4}{7}$, your equation is equivalent to
$$g(x)=-h(x)\leftrightarrow \frac{x}{x^2+1}=-\frac{1}{x^2+x+2}.$$
Therefore, the real solution comes from
$$x^3+2x^2+2x+1=0,$$
which gives $x=-1$.
Answer: It must be $$\frac{x}{2(x^2+1)}+\frac{1}{(2x^2+x+1)}=k\pi$$
or
$$\frac{x}{2(x^2+1)}+\frac{1}{(2x^2+x+1)}=\frac{(2k+1)\pi}{2}$$


Delay of game to get a better field-goal angle

Question: I recall reading a story in the 1980s — but I don't recall when it was supposed to have happened — about an American-football game.  (I think it was a college game.)  A team thought it most advantageous to kick a field goal, but was too close to get a good angle.  So the manager sent replacement players in, the players on the field refused to leave, and so it went until the referee called a delay-of-game penalty of some few yards, enough to get the kicker a better angle.  (Of course, I may have gotten some of the details wrong.)

(My main question.)  Does anyone know when this happened, with what teams, etc.?  A Web search isn't helping me.
(Just out of additional curiosity.)  I don't know that much about football, but why can't the kicker just pull back a few yards without having been penalized?  I'd think a team is allowed to create its own "disadvantage" (quotation marks because it wasn't actually a disadvantage in this case.)
Comment: For (2), see [Why don't teams snap the ball back further than 7 yards on close-range field goal attempts?](https://sports.stackexchange.com/questions/28292/why-dont-teams-snap-the-ball-back-further-than-7-yards-on-close-range-field-goa) tl;dr: they _can_ but they generally choose not to.
Comment: @PhilipKendall ah, the guy transferring the ball to the kicker is not allowed to stand near him if he (the kicker) is farther back? That sounds like an answer (to #2). Many thanks. But let me edit the question to clarify that my main focus is #1…
Answer: Found it!

In the Rose Bowl game of January 1, 1950 between Ohio State and California, the score was tied at 14-all, with less than a minute and a half to play. Ohio State had the ball on California's 6-yard line, fourth down, 3 yards to go. Coach Wes Fesler pointed to his kicking specialist Jim Hague and told him to go into the game to try a field goal. Dick Widdoes was also sent in to hold the ball for Hague.
But when the kicker and his holder trotted onto the field, three Ohio State players began to scream at them. "Go back! Go back! We want the touchdown!"… Hague returned to coach Fesler, who was now enraged.…
By this time, Ohio state had taken too much time. The referee picked up the football and marched off a 5-yard penalty for delay of game. He placed the ball down on the California 11-yard line, on the hash mark 20 yards in from the sidelines. Now it would be foolish to try for the touchdown.
When Hague finally got into the huddle, Krall and Hamilton, now smiling, trotted off the field.
"What's the big idea?" Hague demanded in the huddle.… Morrison pointed out that if the play had started on the 6-yard line, the kicking angle would be bad. The five-yard penalty made the kick longer, but improved the angle. The Ohio State players had disobeyed the coach in order to get a penalty!
Holder Widdoes knelt at the California 18-yard line. The ball came back and he placed it down. Hague stepped forward and kicked. The ball just made it over the crossbar. The kick was good, and Ohio State won, 17-14.


Why are translation invariant operators on $L^2$ multiplier operators

Question: For $m \in L^\infty$, we can define the multiplier operator $T_m \in L(L^2,L^2)$ implicitly by
$\mathcal F (T_m f)(\xi) = m(\xi) \cdot (\mathcal F T_m)(\xi)$
where $\mathcal F$ is the Fourier transform. It is obvious from the defintion that $T_m$ commutes with translations.
How can you show the converse, i.e. every translation invariant $T \in L(L^2,L^2)$ is induced by a multiplier $m_T$? I have no idea how this might work.
Comment: How is it obvious that $T_m$ commutes with translations?
Answer: Here's a sketch proof, which I think gets to the core reason why this is true.  Remember that the fourier transform takes translations to multiplication by characters.  So if $T$ commutes with translation, $\hat T = \mathcal{F} T \mathcal{F}^{-1}$ will commute with multiplication by characters.  So $\hat T$ commutes with all operators which are in the closure of the linear span of the operators given by multiplication by characters.  That is, $\hat T$ commutes with multiplication by any continuous function.  It's not so hard to then show that $\hat T$ must itself by multiplication by some $m\in L^\infty$; under your definition, this means precisely that $T = T_m$.
Comment: If the group is non-compact (for example, $\mathbb{R}$), then the uniform closure of the linear span of the characters yields only almost periodical functions (not all bounded uniformly continuous functions). It would be interesting to see a simple solution of the original problem for this case.
Comment: @Egor I think a weak topology (any of the operator topologies would work) comes to the rescue.  If an operator T commutes with translation operators, it commutes with any operator in the WOT closure of the translations.  The same is true after taking the Fourier transform, and so we can look at the WOT closure of the characters (and not just the uniform closure).  That's enough.
Comment: Could you explain how you pass to $\hat T$ commuting with the closed linear span of the characters? I can't seem to get the limits to work out right.
Comment: @Matthew Dows Good point! Thank you very much for the idea!
Answer: This is Theorem 3.16 in the first chapter of Introduction to Fourier Analysis on Euclidean Spaces, by E.M. Stein and G. Weiss.
Comment: perfect, thank you very much.


block ip with iptables ubuntu server

Question: I am trying to block one IP with iptables in my Ubuntu server 12.04.
The foreign IP is 184.108.40.206.dyna. You can take a look at the next image:

I have added this ip to iptables with the next command:
sudo iptables -A INPUT -s 18.104.22.168 -p tcp -j DROP
sudo service iptables-persistent save
sudo service iptables-persistent restart

However, when i run again the command netstat -tap, this IP is not blocked.
How can I remove/block/delete this IP from my Ubuntu server?
Comment: add a -n to the netstat command and confirm the IP address is correct.
Comment: Please use [Markdown](http://serverfault.com/editing-help) and/or the formatting options in the edit menu to properly type-set your posts to improve their readability. Also use cut-and-paste for posting console output and format it as "`code`" rather than posting screenshots. That improves readability, attracts better answers  and allows indexing by search engines, which may help people with similar questions.
Comment: If you want the `-p` option to `netstat` to actually work, you must run it with `sudo`.
Answer: iptables -A will append the rule to the end of your rule set. If there is any rule before that IP allowing these connections somehow, the rule will never be hit, since the order of processing in iptable rules are up to down. Furthermore. There may also be rules like stateful iptable rules (eg RELATED,ESTABLISHED) which will allow connections if the session was initiated by your host.
Check with iptables -L -v -n and read  the output. There are counters there which can tell if your rule has ever been in effect.
Also, you can append your rule to the top of the INPUT chain instead of to the bottom by replacing the -A (append) with -I (insert).
Comment: That's a good answer. When you say counter, you mean `pkts` ?
Comment: indeed yes pkts!
Answer: IP adresses contains only digits. So '126.96.36.199.dyna' is definitely not IP, it is FQDN. I suspect that real ip is reversed. So, try to block 184.108.40.206
Or just to ensure try netstat with '-n' arg, it will prevent IPs from resolving to FQDNs

block ip with iptables ubuntu server


Exercise about multiple integration

Question: Exercise text$\ \ \ $ Calculate $$\ \ \iiint_R y \ dxdydz\ \ $$
where $R$ is the cube portion $\ 0 \le x,y,z\le1 \ $ which is under the plane $\ x+y+z=2\ $ and above the plane $\ y+z=1\ $.
My solution
Let $D=\{x+y\le2 \ ,\ y\ge1\}$ we have
\begin{equation}
\begin{split}
\iiint_R y\;dxdydz=&\iint_D y\;dxdy\int_{1-y}^{2-x-y}\;dz\\
=&\iint_D\ {y(1-x)}\;dxdy\\
=&\int_0^2(1-x)\;dx\int_1^{2-x}y\;dy\\
=&\frac{1}{2}\int_0^2(1-x)(4+x^2-4x-1)\;dx\\
=&\frac{2}{3}
\end{split}
\end{equation}
I'm not sure of the validity of my solution, could someone help me? Thanks in advance!
Answer: Definitely wrong: the intersection between the planes $x + y + z = 2$ and $y + z = 1$ is the same that the intersection of the planes $y + z = 1$ and $x = 1$ [$x = x + 1 - 1 = (x + (y + z)) - 1 = 2 - 1 = 1$], but $x = 1$ contains one of the faces of the unit cube, so the projection of the body on the $XY$ plane is the unit square, and the integral is
$$
\int_0^1\int_0^1\int_{\max(0,1 - y)}^{\min(1,2 - x - y)}1\,dz\,dy\,dx =
\int_0^1\int_0^1\int_{1-y}^{\min(1,2 - x - y)}1\,dz\,dy\,dx = \cdots
$$
(why $\max(0,1 - y) = 1 - y$?). Now, $2 - x - y\le 1$ iff $x + y\ge 1$ and
$$
\cdots =
\int_0^1\int_0^{1-x}\int_{1-y}^1 1\,dz\,dy\,dx +
\int_0^1\int_{1-x}^1\int_{1-y}^{2-x-y}1\,dz\,dy\,dx = \cdots
$$

Exercise about multiple integration


How do I add a node add link ( for a different content type ) in the node add form (drupal 7)

Question: I'm overriding the node add / edit form using panels. One of the fields that I have added is using the auto complete widget (it is a node reference field). I also want to add a link underneath the autocomplete field to add a new node (one of the referenced nodes). I'm wondering if there is a way to add this link.
If it's not possible within panels, perhaps it is possible someone can give me an idea of how to add this sort of link to node add form using templates.
Comment: There is a way to do this in a custom module, I just have not done so yet.  I would try looking into panels codebase for the code that outputs the panel view you want to overide, and overide it in your module. You will have answers on this, I will have panels to overide myself in upcoming project so it`s a question I very well could have asked in upcoming weeks.
Comment: Looking into panels_node.module, on line 316, the function panels_node_hook_view, there may be an alter hook for that and you just change $node content in the alter hook. Or even simply override that hook yourself, and change $node`s content to add your links. There very well coud be other better options. Templates seems to be easier way to do this?...
Answer: The module your are looking for is References dialog.
Also, you can use contextual administration. It's a more general approach, that won't put the link exactly where you want, but is much more flexible on the other hand. It allows you to use the same rule set as Panels, such as "Selection rule: Node type", for example to node/add/[other-type]. Fits like a glove together with Panels.
Comment: I ran into this same issue. References dialog is brilliant. There's not much documentation for it yet but this issue did help me achieve this: http://drupal.org/node/1491576
Answer: Thanks for the answers, not sure the question was really clear. I ended up doing it by adding "custom content" to the panels pane, I then just switched the options to html and made and html link to the other nodes add / edit page. I know this is not the most elegant solution, but it was really quick and easy.


a promise was created in a handler but was not returned from it in bluebird

Question: I'm using bluebird in my app and I use babel to compile my code into es5. However, I always got this warning and I've checked that every pieces of Promise has return value.
Here's my code:
Promise.promisifyAll(fs);

use.login().then((result) => {
  console.log(result);
  doSomething('../test.png');
});

function doSomething(filepath) {
  return fs.readFileAsync(filepath).then((bufs) => (
      doPost(url, bufs, filepath)
        .then((res) => (
          res.error ? Promise.reject(res.error) : Promise.resolve(res))
        )
  )).catch((err) => {
    console.error(err);
    return err;
  });
}

function doPost(url, bufs = null, filepath = null) {
  return new Promise((resolve, reject) => (
    unirest.post(url)
      .headers(config.Headers)
      .timeout(120000)
      .field(bufs)
      .attach('files', filepath)
      .end((res) => (
        res.error ? reject(res.error) : resolve(res)
      ))
  ));
}

Details of error messages:
Warning: a promise was created in a handler but was not returned from it
at doSomething (/home/test/Documents/test/lib/abc.js:2:27)
// This line number is referred to the compiled code which is equal to line 4:12 in the above code

Comment: Where are you calling `doSomething`? Is that your whole code?
Comment: @Bergi No, that is not my whole code but I only got the warning in the above code. I will call `doSomething` when I need to read a file, ex. `doSomething('../test.png')`
Comment: I meant that maybe you are calling `doSomething` from a promise handler that didn't `return` anything. Is that the whole call stack of the warning? It would be helpful if you could show the whole code.
Comment: OH!!! @Bergi yes, you're right !!! I didn't `return` anything in my doSomething call stack!! The warning is gone after I add `return` after `doSomething`. Thanks a lot !! I've updated the code above to show my code which will cause warning.
Answer: Thanks @Bergi that I didn't check the promise stack where I call doSomething so the warning disappeared after I modify my code as below
use.login().then((result) => {
  console.log(result);
  doSomething('../test.png');
  return null; // Add this line to make promise return anything, then the warning gone
});


How to end a file in a newline in c?

Question:     printf("\n");
    return 0;
}
#endif

hello everyone,
These are the last few lines of my code, is this how you end a file with a newline in C? Or do they mean something else when they say you should always end a file with a newline?
When i tried to put a newline under endif it gave me an error, so i could only put it there or am i not supposed to print the newline? I am supposed to hand it in with a newline according to my school but im not sure if this is correct.
Comment: Are you taking about newlines in a data file your program generates, or the newlines in the source code file you have shown?
Comment: So it is about the [trailing newline in the code file](https://unix.stackexchange.com/q/18743). You realize that you have put quite a lot of newlines already in that file? There is one after `printf("\n");`, there is one after `return 0;`, there is one after the `}`. So you should be familiar with putting newlines in there; so do after the `#endif` what you have done to all the other lines to cause there to be a newline.
Comment: You're using tools (like VS Code) made for Windows, aren't you? On Windows, newlines _separate_ lines (so they go between lines, but there's no expectation to have one at the end of the file); but on UNIX, newlines _terminate_ lines (so a line isn't valid unless it has a newline on the end).
Comment: Last time when i handed it in he looked at my code like this and didnt see a newline, they only said that it should end in a newline not how to do it, I looked it up online and there seems to be a general rule that you should do that, but i dont know how to apply it.
Comment: Perhaps you should always end a multiline text file with a newline, but equally you should never assume that a text file you are reading ends with a newline.
Comment: I geuss that could be it? I do know that my TA said that apparently if you don't end a  file with a newline it could brake the program.
Comment: Oooh now i get it Thank you very much!!
Answer: That fragment ends with f (from #endif).  The file should end with an empty line:
    printf("\n");
    return 0;
}
#endif

The problem (in theory) is that any file can be #include'd, so if for example you had a header xx.h ending in #endif and say a yy.h starting #if, code:
#include "xx.h"
#include "yy.h"
The pre-processor after inclusion would see:
#endif#if

all on one line and would fail.  There are of course many other syntactical nonsenses that can be generated in this way.  Often you get away with it because most source-files of any kind start with a boiler-plate comment which is most often semantically and syntactically benign.  On modern compilers you generally get an diagnostic warning of the malformed source.
In C++ the compiler is required to behave as if there was a newline, even if it is not explicit to avoid this problem.  That may be in later C standards too (I have not looked), or a compiler may I guess choose to add it implicitly in any case and issue a warning.
Comment: You don't need an explicit empty line if you're saving the file as a proper UNIX text file (as on UNIX, newlines _terminate_ lines and are required after each and every line rather than _separating_ lines and being required only between them). Only Windows-oriented tools require an extra/blank line to be added to have a terminating newline.
Comment: It avoids far more issues than just this one: for example, a `while read` loop in UNIX shell will ignore any line with no trailing newline. And this is black-letter POSIX specification, not any mere convention: see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_403 and https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
Comment: There absolutely is enforcement. The Open Group are the people who decide if your operating system is certified as UNIX and able to use the trademark.
Comment: You're telling people to make files that git will flag warnings over (as it does with blank lines at the end of files and blank space at the end of lines) as a workaround for misconfigurations that can be avoided in the first place by consistently using correct editor configuration for your target platform (and C preprocessors built for that same platform); this is indefensible, and I would be constantly correcting files committed by coworkers who followed your "advice" (or, rather: I would be adjusting our CI to reject those files so they never made it into a shared branch at all).

How to end a file in a newline in c?


Lyapunov's inequality in Probability

Question: I have a question about the proof of the inequality. The well known result stats
Let $Z$ be a RV and let $0<s<t$. Then
$$E(|Z|^s)^{1/s} \leq E(|Z|^t)^{1/t}$$
The proof follows almost immediately from the Holder Inequality
$$E(|XY|)\leq E(|X|^p)^{1/p} E(|Y|^q)^{1/q}$$
for $1<p,q$ such that $1/p+1/q =1$.
Taking $Z=|X|^s, Y=1, p= t/s $ we can derive the Lyapunov's inequality.
even though it's not mentioned , I think one must assume that $E(Z) =E(|X|^s)<\infty$ to apply the inequality . However, I'm not assuming that in my hypothesis.
My question is, what happens if $E(Z)=\infty$? Does the inequality still holds? In other words, $E(|X|^t)=\infty$ as well? (If $1<s<t $ it follows easyly but I don't know how to argue in other cases)
Comment: You should not be afraid to write $\text{something} \leq \infty$. It's true.
Comment: Holder's inequality as stated by you holds always! Integrals of non-negative measurable functions are always defined, but the values may be $\infty$.
Comment: What I mean, if $E(Z) =E(|X^p|)= \infty$, I get $\infty \leq$ **something **, following your notation. But working on @KaviRamaMurthy comment I think that the proof still remains valid under the assumption of $E(Z) = \infty$
Answer: I believe you could argue thus under the above assumptions: First  $ |X|^s > 1 \Rightarrow |X| > 1 \Rightarrow |X|^s < |X|^t $. So one could write:
$$ \infty = \int |X|^s = \int_{X>1} |X|^s + \int_{X \leq 1} |X|^s \leq \int_{X>1} |X|^s + 1 \leq \int_{X>1} |X|^t + 1 \leq \int |X|^t + 1, $$
from which it follows that $ E(|X|^t) = \infty $.

Lyapunov's inequality in Probability


Classification when each level has probability assigned to it

Question: The response variable in my dataset is a factor with six levels. For each observation, each level has been assigned a probability to be interpreted as the probability that the given observation is classified as that level. Here is a minimum working example with three predictors (X1, X2, X3) where the response factor has three levels (A, B, C).
set.seed(1)
X1 <- rnorm(n = 1000, mean = 5, sd = 1)
X2 <- rnorm(n = 1000, mean = 3, sd = 0.5)
X3 <- rnorm(n = 1000, mean = 10, sd = 2)
prob_A <- runif(n = 1000, min = 0, max = 0.5)
prob_B <- runif(n = 1000, min = 0, max = 0.5)
prob_C <- 1 - prob_A - prob_B
dat <- data.frame(X1, X2, X3, prob_A, prob_B, prob_C)
head(round(dat, 3))

     X1    X2     X3 prob_A prob_B prob_C
1 4.374 3.567  8.228  0.385  0.349  0.266
2 5.184 3.556  6.155  0.345  0.227  0.427
3 4.164 2.565 13.239  0.325  0.324  0.351
4 6.595 3.105 11.039  0.037  0.307  0.656
5 5.330 3.035  9.888  0.451  0.133  0.415
6 4.180 2.169 11.393  0.066  0.464  0.470

So the probability that the first observation should be classified as level A is 0.385 and so forth. Note that the probabilities in each row add to 1.
I've read elsewhere that beta regression is useful in cases where the response is a probability, but I don't know how to apply it when the response is a vector of probabilities. I've tried running a separate beta regression for each level with unimpressive results. Is there a classification method available that uses all of the probabilities in a single model?
Comment: Without dealing with your last paragraph, I think the language you used makes it unclear.  There is a difference between "can be classified as" and "is".
Answer: Dirichlet regression is the extension of beta regression for compositional data (i.e., proportions for several levels that sum to 1). Dirichlet::Beta regression is like Multinomial::Binomial. Thus, the Dirichlet distribution is a family for multivariate continuous observations that sum to 1. The common parameterization of the corresponding regression model links the shape parameters of the distribution to linear predictors. An alternative parameterization links expectations and a precision parameter to linear predictors. Both are available in the R package DirichletReg at https://CRAN.R-project.org/package=DirichletReg. It also comes with a working paper (http://epub.wu.ac.at/4077/) that explains the model and accompanying software implementation.
For your simulated data you can first set up the response in the package's data structure
library("DirichletReg")
dat$prob <- DR_data(dat[, 4:6])
plot(dat$prob)

And then the common model yields:
m1 <- DirichReg(prob ~ X1 + X2 + X3, data = dat)
summary(m1)
## Call:
## DirichReg(formula = prob ~ X1 + X2 + X3, data = dat)
##
## Standardized Residuals:
##             Min       1Q   Median      3Q     Max
## prob_A  -1.5162  -0.7482   0.0073  0.7027  1.5018
## prob_B  -1.5230  -0.7092  -0.0718  0.7132  1.5352
## prob_C  -2.4742  -0.6599   0.0198  0.7237  2.4825
##
## ------------------------------------------------------------------
## Beta-Coefficients for variable no. 1: prob_A
##              Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.149975   0.290485   0.516    0.606
## X1           0.041932   0.032236   1.301    0.193
## X2          -0.014891   0.060937  -0.244    0.807
## X3           0.002259   0.015774   0.143    0.886
## ------------------------------------------------------------------
## Beta-Coefficients for variable no. 2: prob_B
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.21412    0.29939  -0.715    0.474
## X1           0.04501    0.02928   1.538    0.124
## X2           0.05603    0.06143   0.912    0.362
## X3           0.01039    0.01596   0.651    0.515
## ------------------------------------------------------------------
## Beta-Coefficients for variable no. 3: prob_C
##              Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.959877   0.290775   3.301 0.000963 ***
## X1          -0.014603   0.032007  -0.456 0.648229
## X2          -0.004648   0.063289  -0.073 0.941459
## X3           0.009748   0.017118   0.569 0.569035
## ------------------------------------------------------------------
## Significance codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log-likelihood: 1035 on 12 df (91 BFGS + 1 NR Iterations)
## AIC: -2045, BIC: -1986
## Number of Observations: 1000
## Link: Log
## Parametrization: common

Similarly for the alternative parametrization:
m2 <- DirichReg(prob ~ X1 + X2 + X3 | X1 + X2 + X3, data = dat, model = "alternative")
summary(m2)
## Call:
## DirichReg(formula = prob ~ X1 + X2 + X3 | X1 + X2 + X3, data = dat, model =
## "alternative")
##
## Standardized Residuals:
##             Min       1Q   Median      3Q     Max
## prob_A  -1.5171  -0.7489   0.0069  0.7019  1.5066
## prob_B  -1.5229  -0.7090  -0.0722  0.7113  1.5360
## prob_C  -2.4771  -0.6590   0.0191  0.7222  2.4842
##
## MEAN MODELS:
## ------------------------------------------------------------------
## Coefficients for variable no. 1: prob_A
## - variable omitted (reference category) -
## ------------------------------------------------------------------
## Coefficients for variable no. 2: prob_B
##              Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.364618   0.284715  -1.281    0.200
## X1           0.002721   0.030753   0.088    0.929
## X2           0.071877   0.061001   1.178    0.239
## X3           0.008084   0.015302   0.528    0.597
## ------------------------------------------------------------------
## Coefficients for variable no. 3: prob_C
##              Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.809268   0.251950   3.212  0.00132 **
## X1          -0.057788   0.028343  -2.039  0.04146 *
## X2           0.011392   0.055545   0.205  0.83750
## X3           0.007835   0.014150   0.554  0.57976
## ------------------------------------------------------------------
##
## PRECISION MODEL:
## ------------------------------------------------------------------
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.503549   0.253671   5.927 3.08e-09 ***
## X1          0.014554   0.027081   0.537    0.591
## X2          0.007847   0.053569   0.146    0.884
## X3          0.008116   0.014361   0.565    0.572
## ------------------------------------------------------------------
## Significance codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log-likelihood: 1035 on 12 df (85 BFGS + 1 NR Iterations)
## AIC: -2045, BIC: -1986
## Number of Observations: 1000
## Links: Logit (Means) and Log (Precision)
## Parametrization: alternative

Comment: I'd like to learn more about Dirichlet regression.  Can you give us a few sentences that motivates its use?
Comment: @FrankHarrell, thanks, I've edited my post and added a few sentences at the beginning along with a link to Marco Meier's working paper on the DirichletReg package. The main idea is that multinomial is to binomial like Dirichlet to beta regression.
Comment: Thanks, this like exactly what I need. I've run a model but I'm getting an odd error when trying to make predictions with a test set. I've posted another question here: https://stackoverflow.com/questions/47626861/error-when-predicting-with-dirichletreg-package-in-r


Can Aug7 chords be used in place of any dominant chord?

Question: I am trying to analyze a chord progression that has an augmented 7 chord but since it doesn't resolve, I am trying to figure out what to make of it. Here is a part of the song that uses it.

My take is that the diminished chord walks up to what might usually be a F#7 (E lydian mode) chord but the composer probably just used an Aug7 chord in its place. Is this how augmented 7th chords are normally used? The G# dim is also an inversion of E7b9 without the bass so it is just like the E goes from an E > E7 > F#7. Is that the simplified version of it?
Comment: 7th aug. isn't the same as an aug.7th. Be very careful how the chords are named.
Comment: aug = +5 referring to the aug. 5th ( C double sharp). I don't try to explain all progressions as functional harmony, but I'll try to give an answer.
Comment: Revisiting an old question ... is there anything more required for a satisfying answer?
Comment: @Aaron - Yes, the measure after the Aug. 7th chord - I'd like to make sure that the Aug. 7th chord in fact "doesn't resolve".
Comment: @Dekkadeci Heh! It didn't dawn on me that someone other than OP would reply to my comment. Fair point.
Comment: @Dekkadeci Got it covered. Turns out it does resolve later on in the verse.
Answer: TL;DR
The complete functional chord progression of the verse is:
(3x) I  viio65/V/V  V+7/V :||  V9sus4  I.
For the published chord progression see Google Search: coldplay everything's not lost sheet music.

Can aug7 chords be used in place of any dominant chord?
No. Depending on the context, the augmented 5 is going to be inappropriately dissonant to the music. Consider "Twinkle, Twinkle, Little Star": an aug7 is going to sound very strange in that context, particularly when the the aug5 in the harmony coincides with non-aug5 in the melody.

"… but since it doesn't resolve …": or does it? (Hint: it does, eventually)
In the published versions of the song the chord progression occurs three times, but on the third time, it proceeds to F#m7/B and then E.
F#m7/B can be reconsidered as B9sus4. In that context, we see that F#m7/B is serving as the V chord, which then proceeds to E.

"I am trying to decide what to make of it": The analysis goes like this…
Noting that F#m7/B = B9sus4 (above) reveals that F#aug7 is actually V/V (i.e., Vaug7/V), which, while initially unresolved, eventually moves to V and I — a very typical progression (V/V  V  I), but with alterations (Vaug7/V  V7sus4  I).
Typical uses of aug7 chords
The "rule" in Tonal theory is that augmented intervals resolve upward. So, in general, augmented chords serve as transitional chords between, say, I and IV or I and iv in first inversion.
X:0
K:C major
L:1/2
[CEG] [CE^G] | [CFA]2 || [CEG] [CE^G] | [CEA]2 |

This is true even when the seventh is present. So, for example:
X:0
K:C major
L:1/2
[CEG_B] [CE^G_B] | [CFA]2 || [CEG_B] [CE^G_B] | [CEA]2 |

Is this a simplified version of E, E7, F#7?
Close, but not quite. It's related, but I would suggest it's "complexified" and a slightly different, but related, functional progression.
G#dim is enharmonically equivalent to E#dim, and E# is the leading-tone for F#. G#dim is really E#dim, which is viio/F#.
In other words, the complete progression is
I  viio65/V/V  V+7/V  V9sus4  I.
Comment: Ok, that makes sense. Also, the G note is not in an F#7aug chord so then isn't it incorrect to call it a F#7aug5
Comment: F#7aug and F#7aug5 mean the same thing, and you're correct that the G is not included. More correctly, the chord would be indicated as F#7aug(b9) or F#7#5b9. IMO, the "correct" rendering of the chord in this song is F#b9b13.
Answer: We can't always press a progression in acommon schema, but this song reminds me on something well known:
E C#7 F#7 B7 (whereby The dominant B7 is omitted in the 2 second bar = 2 beats rest!)
The secondary dominant7b9 of F# can be replaced by the secondary VIIdim7 of F#.
So C#7 (C#-E#-G#-B) is substituted by G#dim, which actually is the 1st inversion of E#dim7 (VIIdim7 to F#): E#- G#-B-D (F => enharmonic E#)
Now the F#aug7: augmented is referring to the 5th, the Cx = C## (double sharpened!)
F#aug7 = F#7+5 = F#7 with augmented 5th = F#7 (#5=Cx)
This secondary dominant chord doesn't resolve - as the B7 in the second half of bar 2 is omitted.
P.S.
The first jazz progression I've ever learnt was I-vi-ii-V. There's also a usual progression I VI7 II7 V, where II is V/V and VI is V/II.
(In C this would be C am dm G7 respectively C A7 D7 G7)
in E major: E c#m f#m B7 and E C#7 F#7 B7.
The first pattern is the iso called 1 6 2 5 progression     (I vi ii V). The other with major chords can be interpreted as 5th fall sequence of secondary dominants I (V7/V7/V7 I)  or the major chords VI - II could be considered as the parallel chords of the minor variants vi - ii. V7/ii can be substituted by VIIdim7/ii, as VIIdim7 is also V7b9 without root tone.
So the song goes:
E - E#7/G# - F#7+5 - (B7)  in parenthesis as omitted
I - VIIdim7 - V7/V ....
Comment: thanks, I just read the other reply and I didn't see there was a G natural note in the "F#7+5" so does it make more sense to call the chord a F#7b9b13?


Why are there particular "rules" for using triads in second inversion but not for using seventh chords in second inversion?

Question: In the Classical style, there are three correct uses of the six-four triad (or sometimes four; see When, if ever, are arpeggiated six-four chords really "real"?). The logic is that the interval of the fourth, historically a dissonance, needs to be correctly handled when it's between the bass and an upper voice.
So why do these same rules not apply to second-inversion seventh chords, which also have a fourth between the bass and an upper voice? The only difference between the two is that the second-inversion seventh chord has a third above the bass in addition to the sixth and fourth.
In other words, a second-inversion triad must be a passing, pedal, or cadential six-four. Why isn't this also true for second-inversion seventh chords?
Is there a historical reason why the triad is treated differently than the seventh chord? Perhaps an explanation from the thoroughbass tradition?
Or perhaps they do follow these same guidelines, but current theory textbooks don't make that clear enough?
Comment: Rereading your title, there are rules for resolving seventh chords in second inversion. For a dominant seventh chord, the resolution of the tritone predominates: the leading tone resolves upward to the tonic, and the chordal seventh resolves downward to the scalar third. The root becomes the fifth of the chord of resolution, and the chordal fifth may resolve upward to the scalar third or downward to the root.
Comment: All seventh chords will have a major or minor second and/or a tritone, which is a much stronger dissonance than the only-sometimes-dissonant perfect fourth. Also, the fourth in a 6-4 triad is "exposed"; whereas, in a seventh chord there is an intervening consonant third to "soften" the fourth (and create the stronger dissonance of a second).
Comment: Also, consider a G6-4 triad moving to a C6-3 triad. Both chords contain a fourth. In this case it's not about resolving the fourth but resolving the leading tone.
Comment: @Aaron But only one of those chords has a fourth *with the bass*, no?
Comment: In my triad example, yes, fair enough.
Comment: @Aaron Yes, but I'm asking less about their resolution and more about using them in a progression. A second-inversion triad, for instance, must be a passing, pedal, or cadential six-four. Why isn't this true for second-inversion seventh chords?
Comment: The dominant seventh chord can also resolve in multiple ways, it's just that we respell the pitches and relabel the chord. A dominant seventh chord, regardless of inversion, can function as a dominant, an augmented sixth, or a common-tone augmented sixth. But the "rules" are dominated by resolving the most dissonant interval (tritone, augmented sixth). In the triad case, the fourth is the most dissonant interval.
Comment: My first though was in agreement with your last sentence, that it would be more or less the same, except with the addition of handling of the seventh.
Comment: @Aaron I understand everything you're saying, but I'm afraid we're still missing each other regarding the point of my question. Imagine we're in C major and we're on a root-position IV chord. We can't leap in the bass down to a V64, because that's an illegal six-four. But we *can* leap down to a V43; why is that so?
Comment: My understanding, and you might correct me here, is that a root position IV chord *in close position* cannot proceed to a V64 because the bass is leaping into a fourth with an *adjacent* voice. This doesn't happen in the V43 case -- the adjacent voice is a third. Similarly, I believe -- but correct me if I'm wrong -- it's okay to move from an *open position* IV chord (say,`F C A`) to a V64 chord (`D B G`).
Comment: @Aaron, won't the open position movement to a 4th/11th in the outer voices be considered a problem?
Comment: @MichaelCurtis That's where I'm uncertain, and unfortunately I don't have a straightforward way to look that up. Nevertheless, the idea that the fourth in a V43 chord is "hidden" in an inner voice would still hold.
Answer: In Bach-chorale style harmony the 'rules' about the dissonant fourths in seventh chords would seem to be the same as in triads. But... x43 was only 'really' used in this style as a passing chord, so the fourth above the bass was considered ok just as in the straight 64. Other examples that you'll find of V43 are bass arpeggiations (typically involving V65) - the fourth 'rule' you alluded to and hence also ok. 43 can't be used to substitute a cadential 64  - try it, you'll be setting up a modulation away from your local tonic. It can't be used as an auxiliary/pedal 64 substitution for the same reason. Those 'rules' of course did not survive the 19th century.
Comment: or to put it another way, are there any examples pre Chopin where the fourth above the bass in a 643 chord was actually treated differently to that in a 64?
Answer: From what I've read, the only things to worry about are the treatment of the seventh (resolve downward by step) and the root (move a fifth downward) while avoiding parallels. It's a combination of the rules ("common procedures" would be a better name) for triads in inversion and resolution of sevenths. One may follow these guidelines as closely as needed to get the sound desired. The second inversion V43 gets a nice (rather an ancient sound) of a major sixth expanding to an octave if moving to a I.
The only peculiarity seems to be that third inversion sevenths (4-2) chords tend to resolve to first inversion triads a fifth below (else the seventh doesn't fall.) There are probably some exceptions.
With respect to the fourth in the V43-I53 cadence, the following link suggests that it's a "weak" authentic cadence and not really suited for a final cadence but OK internally. http://www.sfcmtheory.com/harmony_supplements/chap_08.htm
Comment: Do you mean "first-inversion triads" in your last paragraph? And I agree with your upper paragraph, but ultimately my question is: why aren't we bothered by the treatment of the fourth in seventh chords like we are for triads?
Comment: Yes, I meant first-inversion. Edit forthcoming. Thanks. I'll take a look at the resolutions of the fourths; however, there are no parallel fourths here, just an oblique one.
Answer: Rules always are derived from the practice in a certain epoque:
The triad and its second inversion came up in the time of renaissance and early bar(counterpoint and through bass) while the tetrads and seventh chords have been developed later.
If you have a 34 passing chord  (second inversion of any degree) the 3rd (=7th) will be leaded downwards, and the 6th (=lead tone) upwards, the 4th becomes the fifth (edited!) of the succeeding chord and the bass will continue its line. E.g. ReFaSoTi => DoMiSoDo.
The case of the ambivalent I46 = Vsus64 as tetrad doesn't appear.
Comment: Did you mean the 4th comes the _fifth_ of the succeeding chord?
Comment: ReFaSoTi => DoMiSoDo. Thank you, Michael. (The fourth *is the root* of the actual 34 chord... )


Identificação de combinação de palavras no R

Question: Eu tenho centenas de sentenças como essas abaixo. E gostaria de identificar as frases que contenham as palavras Telefone e apagar. Mas, essas duas palavras devem ter somente um termo entre elas, no caso um número de telefone.
Eu tentei utilizando a função grepl, mas ela retorna todas as frases com Telefone e apagar, independentemente de quantos termos existe entre eles.
teles <- c("1 Telefone 44221201 apagar Bairro CENTRO apagar Notebook apagar",
           "2 Telefone 44221201 44221202 Bairro CENTRO2 apagar",
           "3 Telefone 44221203 44221202 EQUIPAMENTOS Blue-ray apagar",
           "4 Telefone 44220000 apagar EQUIPAMENTOS Televisão apagar",
           "5 EQUIPAMENTOS Televisão apagar Telefone 64221201 apagar",
           "6 EQUIPAMENTOS Antena apagar Telefone 54221201 apagar",
           "7 EQUIPAMENTOS DVD apagar EQUIPAMENTOS Antena apagar",
           "8 EQUIPAMENTOS DVD apagar EQUIPAMENTOS Antena apagar")

tel_apagar1 <- grepl("Telefone[^\\.,!?:;]*apagar", teles)
tel_apagar1

Neste caso, a função retorna:
#[1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE

Mas, eu gostaria de filtrar somente as frases:
1, 4, 5, 6

Que possuem a palavra Telefone (um número) apagar.
Assim, obteria a seguinte sequência
#[1]  TRUE  FALSE  FALSE  TRUE  TRUE  TRUE FALSE FALSE

Answer: A seguinte expressão regular faz o que a pergunta pede.
grep("Telefone\\s*[[:alnum:]]+\\s*apagar", teles, ignore.case = TRUE)
#[1] 1 4 5 6


Formal proof of Occam's razor for nested models

Question: I consider 2 models $M_0$ and $M_1$, $M_1$ being more complicated than $M_0$ in the sense that it has more parameters (I usually assume than $M_0$ is nested within $M_1$). They are respectively parametrized by $\theta_0$ and $\theta_1$. I assume that

$\theta_0 \subset \theta_1$ (i.e. $M_1$ has the same parameters as $M_0$ plus extra parameters)
$p(\theta_0|M_1) = p(\theta_0|M_0)$ (both models have the same priors for the parameters they have in common)

I would like to prove the following inequality:
$\forall \theta_0 \\ \langle \log p(\mathcal{D | M_0}) \rangle _{p(\mathcal{D | \theta_0, M_0})} \geq \langle \log  p(\mathcal{D | M_1}) \rangle _{p(\mathcal{D | \theta_0, M_0})}$
i.e. that on average, if my data $\mathcal{D}$ are generated from $M_0$ parametrized with a given $\theta_0$, then the Bayes factor is going to favor $M_0$ over $M_1$.
Has it already been done ? Intuitively, it is an application of Occam's razor (a simpler and true model will be favored over a more complicated one), but I lack a formal proof.
Precision on the notations :  $p(\mathcal{D}|M_0,\theta_0)$ is not the same as $p(\mathcal{D}|M_0)$, and I thus cannot use the positivity of the Kullback-Leibler divergence.
In "$M_0,\theta_0$", I specify both the model and its parameters. In "$M_0$", I only specify the model. $p(\mathcal{D}|M_0,\theta_0)$ is the probability that the data $\mathcal{D}$ are generated from model $M_0$ with parameters $\theta_0$, while $p(\mathcal{D}|M_0)$ is the marginal likelihood over all parameters (the one we use to compute the Bayes factor) : $\int_{\theta} p(\mathcal{D}|M_0,\theta)p(\theta|M_0)$ where $p(\theta|M_0)$ is the prior of parameters under model $M_0$.
Comment: If I understand your notation correctly, the difference between the two expressions would be the [Kullback-Leibler divergence](https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence#Definition) or "relative entropy" between the models (for any fixed values of $\theta_0$ and $\theta_1$). Your inequality appears to be [Gibbs' Inequality](https://en.wikipedia.org/wiki/Gibbs%27_inequality).
Comment: The result cannot hold in general as it depends on the choice of the priors over both models. As an extreme example. take priors degenerated at $\theta_0$.
Comment: This cannot be proven true because there seems to be no guarantees that $p(\theta|M_0)$ won't assign $0$ probability to $\theta_0$, while $p(\theta|M_1)$ assigning non-zero probability to $\theta_0$
Comment: @CagdasOzgenc an implicit assumption is that $p(\theta_0|M_0)$ is not zero. As data $\mathcal{D}$ were generated from $M_0$ parametrized with $\theta_0$, $\theta_0$ is a possible value for the parameters.
Comment: @Xi'an I indeed realize that model complexity depends not only on the number of parameters, but also on their priors. For instance, by choosing a Dirac centered on a certain value for the prior of the parameter, we over-simplify the model. I clarified my question (see both assumptions at the beginning) to assume that both models have the same priors for the parameters they have in common.
Comment: Sadly, it is not. $p(\mathcal{D}|M_0,\theta_0)$ is not the same as $p(\mathcal{D}|M_0)$, and I thus cannot use the positivity of the Kullback-Leibler divergence.
Comment: Then could you please clarify the difference between "$M_0,\theta_0$" and "$M_0$"?
Comment: In "$M_0,\theta_0$", I specify both the model and its parameters. In "$M_0$", I only specify the model. $p(\mathcal{D}|M_0,\theta_0)$ is the probability that the data $\mathcal{D}$ are generated from model $M_0$ with parameters $\theta_0$, while $p(\mathcal{D}|M_0)$ is the marginal likelihood over all parameters (the one we use to compute the Bayes factor) : $p(\mathcal{D}|M_0) = \int_{\theta}p(\mathcal{D}|M_0,\theta)p(\theta|M_0)$ where $p(\theta|M_0)$ is the prior of parameters under model $M_0$.
Comment: Thank you.  Because this is your first mention of a prior distribution over the parameters, it would be best to make that explicitly in your post.  Currently, the notation is sufficiently compact that it leaves too much up to the interpretations of each reader.
Answer: Here is my attempt at answering the question:
Proposition: Let $\mathcal{M}_0$ and $\mathcal{M}_1$ two nested models such that $\mathcal{M}_0 \preceq \mathcal{M}_1$. We note $\Theta_0$ and $\Theta_1$ the space of possible parameters for $\mathcal{M}_0$ and $\mathcal{M}_1$, with $\Theta_0 \subset \Theta_1$. If data generated from $\mathcal{M}_0$ and $\mathcal{M}_1$ are IID, then the following inequality holds $\forall \theta_0^* \in \Theta_0$:
\begin{equation} \label{eq:proposition1}
     \langle \log p(\mathcal{D}|\mathcal{M}_0) \rangle _{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)} \geq \langle \log p(\mathcal{D}|\mathcal{M}_1) \rangle _{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)}
\end{equation}
If data are not IID, a sufficient condition for the inequality to hold is
\begin{equation} \label{eq:condition1}
k_{\mathcal{M}_0} \log (2 \pi) - \sum_{i=1}^{k_{\mathcal{M}_0}} \langle \log (\lambda_{i}^0) \rangle _{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)} \geq
k_{\mathcal{M}_1} \log (2 \pi) - \sum_{i=1}^{k_{\mathcal{M}_1}} \langle \log (\lambda_{i}^1) \rangle _{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)}
\end{equation}
where
$k_{\mathcal{M}_0}$ and $k_{\mathcal{M}_1}$ are the number of independent parameters of $\mathcal{M}_0$ and $\mathcal{M}_1$;
$H_0(\hat{\theta}_0)$ and $H_1(\hat{\theta}_1)$ are the Hessian matrices of the log-likelihoods $p(\mathcal{D}|\theta_0,\mathcal{M}_0)$ and $p(\mathcal{D}|\theta_1,\mathcal{M}_1)$ expressed at their respective MLEs;
$\{\lambda^0_i\}_{1 \leq i \leq k_{\mathcal{M}_0}}$ and $\{\lambda^1_i\}_{1 \leq i \leq k_{\mathcal{M}_1}}$ are the respective eigenvalues of $-H_0(\hat{\theta}_0)$ and $-H_1(\hat{\theta}_1)$.
Proof: using the same approximation as in the derivation of the BIC for $p(\mathcal{D}|\mathcal{M}_0)$ and $p(\mathcal{D}|\mathcal{M}_1)$ yields
\begin{gather}
\log p(\mathcal{D}|\mathcal{M}_0) = \log p(\mathcal{D}|\hat{\theta}_0,\mathcal{M}_0) +
\log \pi(\hat{\theta}_0|\mathcal{M}_0)+
\frac{k_{\mathcal{M}_0}}{2} \log (2 \pi) - \frac{1}{2} \log (|-H_0(\hat{\theta}_0)|)\\
\log p(\mathcal{D}|\mathcal{M}_1) = \log p(\mathcal{D}|\hat{\theta}_1,\mathcal{M}_1) +
\log \pi(\hat{\theta}_1|\mathcal{M}_1)+
\frac{k_{\mathcal{M}_1}}{2} \log (2 \pi) - \frac{1}{2} \log (|-H_1(\hat{\theta}_1)|)
\end{gather}
Both quantities then need to be averaged over $\langle \cdot \rangle_{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)}$. Assuming
\begin{equation}
\langle \log p(\mathcal{D}|\hat{\theta}_0, \mathcal{M}_0) \rangle _{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)} \approx \langle \log p(\mathcal{D}|{\theta}_0^*, \mathcal{M}_0) \rangle _{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)}
\end{equation}
(i.e. that the maximum likelihood estimator $\hat{\theta}_0$ will be close to the true value $\theta_0^*$ from which data were generated) yields $\langle \log p(\mathcal{D}|\hat{\theta}_0, \mathcal{M}_0) \rangle _{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)} \geq \langle \log p(\mathcal{D}|\hat{\theta}_1, \mathcal{M}_1) \rangle _{p(\mathcal{D}| \theta_0^*,\mathcal{M}_0)}$ (under Gibbs's inequality). Furthermore, $k_{\mathcal{M}_0} \leq k_{\mathcal{M}_1}$ yields $\pi(\hat{\theta}_0|\mathcal{M}_0) \geq \pi(\hat{\theta}_0|\mathcal{M}_1)$ (these quantities do not depend on $\mathcal{D}$). The inequality is thus met for the first two terms on the right-hand side.
For the last two terms, if data are IID and if the number of data points $T$ in $\mathcal{D}$ is sufficiently large, the same approximation as in the derivation of the BIC can be made:
$$
\frac{k_{\mathcal{M}}}{2} \log (2 \pi) - \frac{1}{2} \log (|-H(\hat{\theta})|) \approx -\frac{k_{\mathcal{M}}}{2} \log (T)
$$
Since $k_{\mathcal{M}_0} \leq k_{\mathcal{M}_1}$, the inequality thus holds if data generated from $\mathcal{M}_0$ and $\mathcal{M}_1$ are IID.
If data are correlated, the above approximation does not hold. However, the determinant of the Hessian (which is a symmetric matrix) can be written as the product of the eigenvalues, which finally leads to the necessary condition. This inequality can also be seen as a more general version of a result presented in the following paper using less stringent approximations :
Heavens, Alan F., T. D. Kitching, and L. Verde. "On model selection forecasting, dark energy and modified gravity." Monthly Notices of the Royal Astronomical Society 380.3 (2007): 1029-1035.


Calculating Statistical Power after Multiple comparison tests?

Question: I'm currently using the multtest package in R, and I am curious to see how the statistical power holds up in a FWER vs. FDR based corrections.
Say for example:
pval <- c(0.018,0.034,0.726)
mt.rawp2adjp(pval,"Bonferroni")
mt.rawp2adjp(pval,"TSBH")

Is there anyway I can get the details of the power of the test retrospectively, using either this package or any other package? Or is what I am asking not making anysense?
Answer: What you are asking doesn't really make sense. Statistical power is a function of your effect size, sample size, and significance criterion. It's a measure of how frequently you'll accept the null hypothesis when you should actually reject it. Power can't be computed from just a list of p-values.
At any rate, it's not really useful to compute the power of your test after you've applied it and gotten p-values. If you find significant p-values, your test was "powerful enough", otherwise, there's either no effect or your test didn't have enough power.
Comment: Power is a useful thing to compute in advance of a study, using hypothetical effect sizes that represent targets of practical interest. Power is not useful to calculate retrospectively; it's an empty exercise, sort of like wondering if you included enough respondents in your pre-election poll after the election is over and the votes are counted.
Comment: The reason I'm after this exercise is that I have read FDR based adjustments preserve the power of the test much more than FWER [1]

now before you say that I must use the right tool for the right job, and FWER and FDR are actually asking two different questions etc.. I want to show myself that the FDR correction does indeed preserve more power...or is this something I can not realistically do?

[1] Shaffer J.P. (1995) Multiple hypothesis testing, Annual Review of Psychology 46:561-584,
Comment: If you have benchmark data where you know what variables actually DO have an effect, you can directly estimate power by calculating how many false negatives you get for each test.
Comment: If needed, I can use randomly generated normally distributed data, so that shouldn't be a problem. How do I go about doing this?


Finite State Machine issue

Question: Building a FSM for my Game AI class using C# in Unity3D game engine. I have 2 simple game objects right now, a Cube (AI) and a bullet (instantiates when a function is called, code below). Just building the foundation for a much more complex FSM, but early in the semester so building it as I'm learning.
My AI shoots out bullets in throws 5 bullets at a time, when bulletCount is 5 then it changes the state. So essentially I just want it to shoot 5 bullets, wait for a time I choose, reload, shoot 5 more, and continue same process. Basically what happens is perfectly what I want it to do first, as soon as it exits my IEnumerator, it shoots an infinite amount of bullets, even though the first time it did what I wanted.
AIClass
using UnityEngine;
using System.Collections;

public class AIClass : MonoBehaviour
{

public GameObject bullet;
int bulletCount;
float stunned;

public enum CombatAIStates
{
    Firing = 0,
    Stunned = 1,
    Reloading = 2,
    Following = 3,
    Idle = 4
}

public CombatAIStates currentState = CombatAIStates.Firing;

void Update()
{

    switch (currentState)
    {
    case CombatAIStates.Firing:
        StartCoroutine (WaitMethod ());

        if(bulletCount <= 5)
        {
            spawnBullets ();
            Debug.Log ("Firing. ");
            Debug.Log ("Bullet: ");
            Debug.Log (bulletCount);
            StartCoroutine (WaitMethod ());
            ++bulletCount;
        }

        if(bulletCount > 5)
        {
            currentState = CombatAIStates.Reloading;

        }
        break;

    case CombatAIStates.Stunned:
            Debug.Log ("Stunned.");
        StartCoroutine(WaitMethod());
        currentState = CombatAIStates.Firing;
        //currentState = CombatAIStates.Firing;
        break;

    case CombatAIStates.Reloading:
        Debug.Log ("Reloading.");
        StartCoroutine (WaitMethod ());
        currentState = CombatAIStates.Stunned;
        break;
    }

}

IEnumerator WaitMethod()
{
    float waitTime = 10;
    Debug.Log ("Before yield.");
    yield return new WaitForSeconds (waitTime);
    Debug.Log ("After yield.");
    bulletCount = 0;

}

void spawnBullets()
{
    Instantiate(bullet, transform.position, transform.rotation);
}
}

Comment: I don't know much about Unity, or what a CoRoutine is - but it looks like you're resetting bulletcount in the wrong place. Try resetting it in the reloading / stunned state rather than in the enumerator
Comment: `StartCoroutine` is not a blocking call(nor is it a yield return) so all those `StartCoroutine(WaitMethod())` calls in `Update()` are getting sent repeatedly.  I'd bet that after the first 5 bullets are fired you have 5 `WaitMethods` running which means your bullet count will reset 5 times in a row.
Comment: actually you are right, except when I do that then it doesn't stop at 5 bullets, even the first time it just spits out infinity amount. Thanks for the help though! (you are right, I just am doing something wrong here then).
Comment: Looks like you're also waiting too much - try taking the waits out of the firing state. That should give you fire * 5 ...pause...fire * 5 ...pause...
Comment: ok I think the issue is more it's not doing the waiting. I looked at my debug and it's sending all the neccessary messages, just not doing the waiting! Here's the console when it runs:

Firing.
Bullet:
0
Firing.
Bullet:
1
Firing.
Bullet:
2
Firing.
Bullet:
3
Firing.
Bullet:
4
Firing.
Bullet:
5
Reloading.
Before yield.
Stunned
Before yield.
(repeats from top)

It doesn't stop the rest of the FSM when it hits the waitTime
Answer: I suspect this is what you are trying to do, stripped down slightly for simplicity:
public int BulletCount = 0;
public enum CombatAIStates
{
    Firing = 0,
    Reloading = 1,
}
CombatAIStates currentState = CombatAIStates.Firing;

// Update is called once per frame
void Update () {
    switch (currentState) {
        case CombatAIStates.Firing:
            if (BulletCount < 5) {
                Debug.Log ("Firing: " + BulletCount);
                ++BulletCount;
            } else {
                currentState = CombatAIStates.Reloading;
                StartCoroutine(Reload ());
            }
            break;
        case CombatAIStates.Reloading:
            // Nothing to do here, Reload() coroutine is handling things.
            // Maybe play a 10 second animation here or twiddle thumbs
            break;
    }
}
IEnumerator Reload()
{
    yield return new WaitForSeconds (10.0f);
    BulletCount = 0;

    //Now update the current combat state
    currentState = CombatAIStates.Firing;
}

I didn't change much with the original code.  I just moved a state change in to a Reload coroutine that switches back to Firing after 10 seconds and resets the bulletCount.  Alternatively you could have the state change in the reload case of your switch.  But instead of calling a coroutine just check if bulletCount >= 5, if not then reloading is done and you can switch back to firing.
Comment: ha yes I see what you did. Works exactly what I want it to. So, for future reference, should I be putting most things that change/method calls in the IEnumerator?
Comment: @user3308129 It's up to you.  I did the state change in the `coroutine` because you needed a 10 second wait and I didn't want to add messy `if(bulletCount>0)` code to your switch statement.  Ideally I like to keep state logic and transitions encapsulated in a single class/function (per state).

Finite State Machine issue


Using prior conjugates to find posterior distribution

Question: If we have a negative binomial like
$N \sim NegBin(r, p)$.
Then
$(N = i \mid p) = \binom{i - 1}{r - 1} p^r (1 - p)^{i - r} \mathbb{1}(\{i \in \{r, r + 1, \dots)$
We are given that $p$  has a beta prior with distribution $a$ and $b$.
I am trying to find the posterior distribution $[p∣N,r,a,b]$. Do I need to find the density of the negative binomial distribution first?
Answer: The negative binomial distribution is a discrete distribution so has a probability mass function (as you have stated - note there are other parametrisations of the negative binomial) rather than a density.  It is the Beta distribution which has a density, proportional to $p^a(1-p)^b$.
If you have a single observation $x$ from your negative binomial distribution with $x \in \{r,r+1,\ldots\}$ then the posterior density for $p$ given $x$ will be (ignoring multiplicative constants) proportional to $p^a (1-p)^b p^r (1-p)^{x-r}$ when $0 <p<1$, with the first part coming from the Beta prior and the second part from the negative binomial likelihood.
Tidying this up gives a posterior density proportional to $p^{a+r}(1-p)^{b+x-r}$, which is the density of a Beta distribution with parameters $a+r$ and $b+x-r$.
If instead you had $n$ observations, the posterior distribution would be a  Beta distribution with parameters $a+nr$ and $b+(\sum x_j)-nr$.


State Space models: rewriting the Likelihood to estimate the covariance matrix

Question: I have a State Space model
$
\begin{matrix}
Y_t & = & FX_t +R_{t}^{1/2}\epsilon_t \\
X_{t+1} & = & GX_{t}+Du_t
\end{matrix}  $
where $\epsilon_t$ and $u_{t}$ distributed as $N(0,I)$ so that $R_{t}^{1/2} \epsilon_{t}$ is distributed as $N(0,R_{t})$. Notice the time index at $R_{t}$, as $R_{t}$ is a time-varying covariance matrix (suppose pd for simplicity). My question is: does it make any difference to compute and maximize the likelihood of $Y_{t}$ with respect to $R_{t}$ or maximize the likelihood of $R_{t}^{-1/2}Y_{t}$ with respect to $R_{t}$? The difference is that, in the latter case, you can conveniently set your covariance at the unit matrix I and write the negative log likelihood for $R_{t}^{-1/2}Y_{t}$ (given the parameters set $\Theta$ and $X_{t}$) to be minimized as:
$$L(R_{t}^{-1/2}Y_{t} | \Theta , X_{t} )= \sum_{t=1}^{T} log(|I|)+(R_{t}^{-1/2}Y_{t}-R_{t}^{-1/2}GX_{t})^{T}I(R_{t}^{-1/2}Y_{t}-R_{t}^{-1/2}GX_{t})$$
instead of
$$L(Y_{t} | \Theta , X_{t} )= \sum_{t=1}^{T} log(|R_{t}|)+(Y_{t}-GX_{t})^{T}R_{t}^{-1}(Y_{t}-GX_{t})$$
and minimize with respect to $R_{t}$. It seems what they are doing at pag. 22 here to simplify a similar problem. But I am not getting what happens to the term $log(|I|)$ which is indeed not comparable to $log(|R_{t}|)$. Are they 100% equivalent? What am I missing?
Thanks
Answer: I will try to give my interpretation here. I take your question as: "given the optimal $\Sigma_{t}$, does this value minimize the two log likelihoods simultaneously?" I will try to show so in an asymtptotically large sample of size $T \rightarrow +inf$.
Let's drop the conditional mean $GX_{t}$ to simplify the notation. So restate the model as
$$Y_{t}=R_{t}^{1/2} \epsilon_{t}$$
and assume that the true value of the unknown parameter $R_{t}$ to be estimated is $A_{t}$ so that the true process is $Y_{t}=A_{t}^{1/2} \epsilon_{t}$, which implies that $A_{t}^{-1/2}Y_{t}=\epsilon_{t}$ and, therefore, $E_{t-1}(A_{t}^{-1/2}Y_{t})=E(A_{t}^{-1/2}Y_{t})=0$. For an asympthotically large sample, the above mentioned properties should hold in the sample when its size approaches infinite and the unknown optimal value of the parameter to be estimated $R_{t}$ approaches $A_{t}$.
Indeed, we know from the consistency of the MLE that, IF the model is correctly specified, then the MLE estimate converges in probability to the true value of the unknown parameter $A_{t}$. Clearly, if that is the true value of the paramter, it must be the true value for both the processes $Y_{t}$ and $A_{t}^{-1/2}Y_{t}$ maximizing their repsective likelihood functions.
Now let's pause for a minute and consider that, if the same parameter $\theta$ simultaneously minimize two functions, call them b and c, then the following happens: when the parameter is at optimum and b is at the corresponding minimum value with respect to that paramter $min(b)_{\theta}$, then c is simultaneously minimized with respect to the same parameter iff the difference $min(b)_{\theta}-min(c)_{\theta}$ is minimum. And, analogously, iff $min(c)_{\theta}-min(b)_{\theta}$ is maximum. This will be useful later. Let's go on.
Given the function
$$L(Y_{t} | \Theta , X_{t} )= \sum_{t=1}^{T} log(|R_{t}|)+Y_{t}^{T}R_{t}^{-1}Y_{t}$$
the last term can be re-written as
$$Y_{t}^{T}R_{t}^{-1}Y_{t}= Tr(R_{t}^{-1}Y_{t}Y_{t}^{T}))= Tr(R_{t}^{-1} (E_{t-1}(Y_{t}Y_{t}^{T}) + WN_{t}))= Tr(R_{t}^{-1}E_{t-1}(Y_{t}Y_{t}^{T}))+Tr(WN_{t})$$ where $WN_{t}$ is a White Noise with unconditional mean of 0 when the sample size tends to +inf and the parameter values converge at their true value. Therefore, for an asympthotically large sample, where the optimal value of the unknown parameter converges to the true value, it is easy to show that $\sum_{t} Tr(WN_{t})$ goes to 0. Since we know that at the optimum this term must converge to 0, we can minimize the reminder of the function with respect to the unknown parameters. The same reasoning applies in case you work with the standardized variable $R_{t}^{-1/2}Y_{t}$. So we minimize
$$\sum_{t=1}^{T} log(|R_{t}|)+Tr(E_{t-1}( R_{t}^{-1}Y_{t}Y_{t})^{T})$$
and
$$\sum_{t=1}^{T} log(|I|)+Tr(E_{t-1}( I^{-1}R_{t}^{-1/2}Y_{t}(R_{t}^{-1/2}Y_{t})^{T})$$
with respect to $R_{t}$ which must be pd. We call these functions -loglik1 and -loglik2. Due to the consistency of MLE, we know that asympthotically for $T \rightarrow +inf$ then the optimum will be $R_{t}=A_{t}$ for each t. Which implies that the first function value at the minimum is (by substitution)
$$\sum_{t=1}^{T} log(|A_{t}|)+Tr(E_{t-1}( A_{t}^{-1}Y_{t}Y_{t})^{T}) = \sum_{t=1}^{T} log(|A_{t}|)+Tr(A_{t}^{-1} E_{t-1}(Y_{t}Y_{t})^{T}) = \sum_{t=1}^{T} log(|A_{t}|)+Tr(I) $$
Before going one, observe that $R_{t}=A_{t}$ minimizes -loglik1 subject to $det(R_{t})>0$. Indeed, provided that the true process of $Y_{t}$ is $Y_{t}=A^{1/2}_{t}\epsilon_{t}$ so that $E_{t-1}(Y_{t}Y_{t})^{T})=A_{t}$,
$$\sum_{t=1}^{T} log(|R_{t}|)+Tr(E_{t-1}( R_{t}^{-1}Y_{t}Y_{t})^{T})= \sum_{t=1}^{T} log(|R_{t}|)+Tr( E_{t-1}(R_{t}^{-1}) E_{t-1}(Y_{t}Y_{t})^{T}) )= \sum_{t=1}^{T} log(|R_{t}|)+Tr( R_{t}^{-1}A_{t})$$
so taking the gradient with respect to $R_{t}$ and setting it equal to $0$ givesn the first order condition $R_{t}=A_{t}$ for each t, where $det(A_{t})>0$ by assumption (so $R_{t}=A_{t}$ minimizes the -loglik1 subject to $det(R_{t})>0$).
Provided that $R_{t}=A_{t}$ minimizes -loglik1 subject to $det(R_{t})>0$ due to the consistency of MLE and this is the minimum for -loglik1, then the parameter value $R_{t}=A_{t}$ will minimize also -loglik2 if the difference $min(-loglik2)_{R_{t}}-min(-loglik1)_{R_{t}}$ is maximum at $R_{t}=A_{t}$. Therefore, take the value of -loglik2 when $R_{t}=A_{t}$ (which is our expected minimum for $T \rightarrow +inf$ under the assumption that MLE is consistent). We derive it through the same passages adopted above for -loglik1:
$$\sum_{t=1}^{T} log(|I_{t}|)+Tr(I)$$
Notice that the difference $min(-loglik2)_{R_{t}}-min(-loglik1)_{R_{t}}$ is
$$\sum_{t=1}^{T} log(|I_{t}|)+Tr(I) - \sum_{t=1}^{T} log(|A_{t}|)+Tr(I) = - \sum_{t=1}^{T} log(|A_{t}|) = - log(\prod_{t=1}^{T}|A_{t}|) = - log(\prod_{t=1}^{T} \prod_{i=1}^{N}eig_{i}(A_{t}))$$
as the determinant can be re-expressed as the product of the N eigenvalues. Now, assuming that you are working with real $NxN$ square symmetyric pd matrixes whose eigenvalues are all real and positive (we are assuming those matrixes to be positive definite) and whose determinant is lower than 1 (*see at the end), then you have that, for $T \rightarrow +inf$, the term $log( prod_{t=1}^{T}prod_{i=1}^{N}eig_{i}(A_{t}))$ converges to $-inf$, which clearly implies that its additive inverse $- log(\prod_{t=1}^{T} \prod_{i=1}^{N}eig_{i}(A_{t})) \rightarrow +inf$ and is therefore maximized.
Therefore, if we assume that the true value of the parameter is the optimum for -loglik1, then, since this value is such that $min(-loglik2)_{R_{t}}-min(-loglik1)_{R_{t}}$ is max (diverges to $+inf$ as $T \rightarrow +inf$), we can conclude that the same value of the parameter $R_{t}=A_{t}$ minimizes -loglik2 as well.
(*) The covariance matrix can be represented as the product of 1) a diagonal matrix with standard deviations, 2) the corr matrix 3) again the diagonal matrix with standard deviations. Notice that its determinant is the product of the determinants of those 3 matrixes. Notice also that the determinant of the correlation matrix will be 1 iff such matrix is diagonal, less than 1 otherwise (see here), and notice that the determinant of the stdev matrixes will be less than 1 if the sum of elements of rows is less than 1 (as stated here), so if you have reasonably low values of standard deviations (like stdev < 100%), then you are in this scenario where the determinant of each conditional covariance matrix is less than 1 and $log(det(\Sigma_{t}))$ is negative as a consequence.


How do you reorder the shipping services for drupal commerce?

Question: There doesn't seem to be a weight option. How would one reorder the shipping services for drupal commerce?
Answer: You can use a custom hook to reorder services:
<code><?php
/**
 * Implements hook_commerce_shipping_service_info_alter().
 */
function mymodule_commerce_shipping_service_info_alter(&$shipping_services) {
  if (isset($shipping_services['my_flat_rate'])) {
    $shipping_services['my_flat_rate']['weight'] = -10;
  }
}
?>
</code>
See discussion here: https://drupal.org/node/1537394#comment-7003814
Steps would be create a custom module or add this hook to existing module for your site, set the weight as code above ranging from -10 to 10 and you should be all set till there is a patch or update to overall Shipping module.
Comment: i guess ill just live with it for now. don't want to add more code bloat to the site.


How can you prove that there are more real number between 0 and 2 than between 0 and 1?

Question: I know the question sounds a little stupid, but yesterday laying on my bed this question came up in my mind ( I had watched the movie pi before it). We know that there surely are more real numbers between 0 and 2 than between 0 and 1, but still how can you prove something like that, like mathematically prove. You can't just take let
x = real numbers between 0 and 1

and since
2 > 1, thus real numbers between 0 and 2 > x.

Because x is infinity. And adding, subtracting anything to x dosent make any sense, because it is infinite. How do you suppose we solve this.
Comment: This depends on what you mean by "more". In set theoretic terms, there are exactly the same number. In measure theoretic terms (with the usual measure), the answer is essentially by definition.
Comment: The map $x\mapsto 2x$ establishes a $1:1$ correspondence between the intervals $[0,1]$ and $[0,2]$.  That's what is meant by saying that there are "the same number of reals in the two intervals."
Comment: We can't...${}{}{}$
Comment: Does this answer your question? [Explaining Infinite Sets and The Fault in Our Stars](https://math.stackexchange.com/questions/867143/explaining-infinite-sets-and-the-fault-in-our-stars)
Comment: Also: [Comparing numbers between (0 and 2) and (0 and 1).](https://math.stackexchange.com/questions/2271759/comparing-numbers-between-0-and-2-and-0-and-1)
Comment: "laying on my bed this question came up in my mind ..." -- you have a promising future in mathematics.
Answer: In fact, there aren't "more" (in the  sense of cardinality) real numbers in $[0,2]$ than in $[0,1]$. There is exactly the "same amount of numbers " in the two sets. Nevertheless, $[0,1] $ is by sure a proper subset of $[0,2]$. You should become acquainted with the idea that this is not a contradiction.
I suggest to start with the article of wikipedia:
https://en.wikipedia.org/wiki/Cardinality
Answer: There are exactly same number of elements in both the intervals as there exists a bijection $f:[0,2]\rightarrow [0,1]$ defined by $f(x)=x/2$. For each number $x$ you choose from $[0,2]$, one can give you a unique number $x/2$ from $[0,1]$. For instance, $0\rightarrow 0, 1/2\rightarrow 1/4, 1/3\rightarrow 1/6,......,2\rightarrow 1$.
Answer: As answered by @diegog7, they are actually same in terms of their set cardinality.
In fact any interval over real number line can be mapped to the entire real number line using one-one functions.
For Example : (-1,1) can be mapped to R using one-one function : tan(πx/2)
Similarly, any number on real number line can be mapped to interval (0,1) using the sigmoid function.
Sigmoid function is defined as 1/(1+e^(-x)).
Using the result stated above, it is further claimed that any there are exactly equal number of reals between any two distinct reals.
Now, coming to your question about real numbers between A=[0,1] and B=[0,2] :
I give a bijection f:A-->B as f= 2x.
You can see that every element of A has been mapped to a unique element in B. Thus, proving that their cardinalities are actually equal and they had to be because they were individually equal to R.
Now, a reasonable doubt would be what if the function proposed was f=x (seems cardinality of A is less than that of B) or f=3x (seems cardinality of A is greater than that of B) .
Well, I never actually intended to define cardinality that way for infinite sets. Greater than and less than are actually much stronger relations than (greater than or equal to) and (less than or equal to) in case of infinite sets. To prove strict inequalities like A greater than B you need to show that there exists no injection from A to B.
Actually it's better to say in the case of f=x that |A| ≤|B| and in f=2x that |A|≥|B|,  both of which lead to the result that |A|=|B|.
Now proving this way might seem trivial to you but this is a very important result in the study of cardinalities of infinite sets and is popularly known as Schröeder-Bernstein theorem.
Link to Schröeder-Bernstein proof https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.whitman.edu/mathematics/higher_math_online/section04.09.html&ved=2ahUKEwj1wsKC5OHoAhXr6nMBHVYrDAQQFjAHegQIAhAB&usg=AOvVaw3bIHH8zE1se3z8H_gV8O4Z


How to center buttons in a div

Question: Here is my CSS and HTML.

.btn-group button {
  border: 3px solid green;
  padding: 10px;
  background-color: #04AA6D;
  /* Green background */
  border: 1px solid green;
  /* Green border */
  color: white;
  /* White text */
  padding: 10px 24px;
  /* Some padding */
  cursor: pointer;
  /* Pointer/hand icon */
  float: left;
  /* Float the buttons side by side */
}

.btn-group button:not(:last-child) {
  border-right: none;
  /* Prevent double borders */
}

/* Clear floats (clearfix hack) */

.btn-group:after {
  content: "";
  clear: both;
  display: table;
}

/* Add a background color on hover */

.btn-group button:hover {
  background-color: #3e8e41;
}

  <button type="button" class="button1">Faucet</button>
  <button type="button" class="button2">About Nano/Faucet</button>
  <button type="button" class="button3">Donate</button>

I want to center these buttons under the text in the screen's center!
I have tried so many things nothing works for me.
Comment: try this `.btn-group{display:table; margin:0 auto;}`
Comment: It works without my css, but if I add my CSS I made it wont work.
Answer: You want to use flex on .btn-group and make it take 100% of the width of the parent to stretch the div.

.btn-group {
  width: 100%;
  display: flex;
  justify-content: center;
  background-color: skyblue;
}

.btn-group button {
  border: 3px solid green;
  padding: 10px;
  background-color: #04AA6D;
  /* Green background */
  border: 1px solid green;
  /* Green border */
  color: white;
  /* White text */
  padding: 10px 24px;
  /* Some padding */
  cursor: pointer;
  /* Pointer/hand icon */
}

.btn-group button:not(:last-child) {
  border-right: none;
  /* Prevent double borders */
}

/* Clear floats (clearfix hack) */

.btn-group:after {
  content: "";
  clear: both;
  display: table;
}

/* Add a background color on hover */

.btn-group button:hover {
  background-color: #3e8e41;
}

  <button type="button" class="button1">Faucet</button>
  <button type="button" class="button2">About Nano/Faucet</button>
  <button type="button" class="button3">Donate</button>

Comment: This seemed to work for me!
Answer: You can add to your css:
.btn-group{
  display:flex;
  justify-content:center;
}

It will look like this
Answer: You can use Flexbox
.btn-group {
  display:flex;
  flex-direction: row;
  justify-content:center;
}
.btn-group button {

  border: 3px solid green;
  padding: 10px;
  background-color: #04AA6D; /* Green background */
  border: 1px solid green; /* Green border */
  color: white; /* White text */
  padding: 10px 24px; /* Some padding */
  cursor: pointer; /* Pointer/hand icon */
}

.btn-group button:not(:last-child) {
  border-right: none; /* Prevent double borders */
}

/* Clear floats (clearfix hack) */
.btn-group:after {
  content: "";
  clear: both;
  display: table;
}

/* Add a background color on hover */
.btn-group button:hover {
  background-color: #3e8e41;
}

    <button type="button" class="button1">Faucet</button>
    <button type="button" class="button2">About Nano/Faucet</button>
    <button type="button" class="button3">Donate</button>

Comment: This also seemed to work for me!
Answer: As easy as adding this rule to style the container using display:flex
.btn-group{
  display: flex;
  justify-content: center;
}

And this is the whole snippet:

.btn-group{
  display: flex;
  justify-content: center;
}

.btn-group button {
  border: 3px solid green;
  padding: 10px;
  background-color: #04AA6D; /* Green background */
  border: 1px solid green; /* Green border */
  color: white; /* White text */
  padding: 10px 24px; /* Some padding */
  cursor: pointer; /* Pointer/hand icon */
  float: left; /* Float the buttons side by side */
}

.btn-group button:not(:last-child) {
  border-right: none; /* Prevent double borders */
}

/* Clear floats (clearfix hack) */
.btn-group:after {
  content: "";
  clear: both;
  display: table;
}

/* Add a background color on hover */
.btn-group button:hover {
  background-color: #3e8e41;
}

  <button type="button" class="button1">Faucet</button>
  <button type="button" class="button2">About Nano/Faucet</button>
  <button type="button" class="button3">Donate</button>

Comment: please accept the answer arriving first... I see everyone just said more or less the same thing
Comment: This seemed to work for me!
Answer: add .btn-group{display: table; margin: 0 auto;}

    .btn-group{display: table; margin: 0 auto;}

    .btn-group button {
      border: 3px solid green;
      padding: 10px;
      background-color: #04AA6D;
      /* Green background */
      border: 1px solid green;
      /* Green border */
      color: white;
      /* White text */
      padding: 10px 24px;
      /* Some padding */
      cursor: pointer;
      /* Pointer/hand icon */
      float: left;
      /* Float the buttons side by side */
    }

    .btn-group button:not(:last-child) {
      border-right: none;
      /* Prevent double borders */
    }

    /* Clear floats (clearfix hack) */

    .btn-group:after {
      content: "";
      clear: both;
      display: table;
    }

    /* Add a background color on hover */

    .btn-group button:hover {
      background-color: #3e8e41;
    }

      <button type="button" class="button1">Faucet</button>
      <button type="button" class="button2">About Nano/Faucet</button>
      <button type="button" class="button3">Donate</button>

Answer: Please add the following CSS to your code. Display be flex then use Justify content center to get output.
.btn-group {
    position: relative;
    display: flex;
    vertical-align: middle;
    justify-content: center;
}

Answer: Use Flex.

.btn-group {
  display: flex;
  justify-content: center;
}

.btn-group button {
  border: 3px solid green;
  padding: 10px;
  background-color: #04AA6D;
  /* Green background */
  border: 1px solid green;
  /* Green border */
  color: white;
  /* White text */
  padding: 10px 24px;
  /* Some padding */
  cursor: pointer;
  /* Pointer/hand icon */
  float: left;
  /* Float the buttons side by side */
}

.btn-group button:not(:last-child) {
  border-right: none;
  /* Prevent double borders */
}

/* Clear floats (clearfix hack) */

.btn-group:after {
  content: "";
  clear: both;
  display: table;
}

/* Add a background color on hover */

.btn-group button:hover {
  background-color: #3e8e41;
}

  <button type="button" class="button1">Faucet</button>
  <button type="button" class="button2">About Nano/Faucet</button>
  <button type="button" class="button3">Donate</button>

How to center buttons in a div


New public IP address - Email, web server

Question: I support a small office of about 100 users. We run a small virtual environment and host our own on-premises Exchange server and Web Server.
We are in the process of upgrading our internet connection from a 100mpbs line on a 100mbps bearer to 200mbps on 1gbps bearer. I've been told by our ISP that since we are getting a new bearer, we will be getting a new block of static IP addresses.
Since we still host our own internal mail server and web server, we will need to update our public MX and A records accordingly. My question is, is there a way we could pre configure the MX record with the new IP address in advance so as to minimize downtime on our email when we come to install our new ISP provided router?
ie. Can I configure a second MX record with the new IP address before it is live so that when we make the switch, the first MX record dies and the second one works straight away?
I'm trying to avoid a situation where we have to switch the boxes, I change the MX record and it takes 24 hours to go live across public DNS servers. Leaving us with no email during that time.
The same also applies to a web server hosted on site. What is the best way to deal with the switch over in this case? I can only have one A record pointing to www.website.com, right? No way of having two A records configured and if one doesn't work, the other does?
Any help greatly appreciated.
Answer: Working with a second mx is definitively the way to go for you:
Create a second mx entry that has the new IP- address but a lower preference number. As long as the first mx is available, nothing will happen, but DNS- servers will already cache the second mx. As soon as you switch IPs, the first mx will not be available, and ever mail server will -after a short period- try the second one.
Set the dns time to live of your mx records as little as possible, then after the switch you just change the preferences of the mx entries or remove the old IP address completely.
That way the most that can happen is a 5 minute delay in mail delivery, as that is the default time, some systems wait before trying again, but most of the systems try the second mx immediately if the first one is not available.
And regarding DNS- entries: indeed you can configure one A- record to have more than one IP- address. This is called "DNS round robin". BUT: Browsers most of the time use the first IP address in the list and only switch to the second after 30 seconds. This can only be reduced, if both IP- addresses are up at the same time...
Comment: Great. I assumed I could do something along these lines but wasn't quite sure. Thanks for the help.  Any idea of a similar way to keep downtime on our webserver to a minimum? Perhaps just decrease the TTL of the A record in advance?
Comment: Yes keep a low TTL on the web server. As browser retry behavior is not standard, easiest would be to tolerate DNS taking a few minutes to update.


database migrations while deploying rails app

Question: I'm curious about where I should run "rake db:migrate RAILS_ENV="production" when I'm deploying to my server. Should I run it locally and then deploy? Or should I deploy then run the migration from the /current directory on the server? Thanks in advance!
Answer: Are you using capistrano to deploy?  If so, running "cap deploy:migrations" from your development machine, will deploy and run the migrations on the server at the same time.
If you have a more complicated deployment recipe or have sharding or replication, you could run "cap deploy:update_code", which will only copy your project.  Then you can login to the server and run the migrations locally.