Application of different fingering pattern exercises to improvisation on the guitar

Question: I've been spending quite some time lately practising various interval-based fingering exercises for soloing on the guitar. These are basically exercises where I ascend or descend a scale (the major scale) in thirds, fourths, fifths, etc. For example, if I'm practising thirds on the C major scale, this would be
C-E-D-F-E-G-F-A-G-B-A-C ... and so on (and reverse)
... or for fourths, it would be
C-F-D-G-E-A-F-B-G-C ... and so on (and reverse).
I'm trying to use these while jamming on backing tracks (say a simple ii-V-I progression) or something suitable to a certain mode of the major scale. I find it a bit awkward or unusual, for instance, to use the ascending/descending fifths pattern on certain chord progressions or at certain times within the same chord progression.
Can anyone advise me on how I can try to gain more control on this application of such patterns while improvising on chord progressions? I mean, are there any simple rules of thumb about connecting the intervals that these patterns highlight with chords/chord changes?
I suppose one can say that it's relative how good/bad it sounds, but I'm talking more about control in using the motor spontaneity these exercises facilitate while soloing over chord progressions.
Answer: These interval exercises are good for dexterity but in my opinion they are only of limited use when trying to improvise a melodic solo. The patterns sound too predictable when used without modifications. Furthermore, all notes of the scale are given the same weight (or importance), and this is usually not the most musical way of using the notes of a scale, because each of these notes has a different meaning and a different importance.
Having said that, you can of course make use of small subsets of these exercises in actual solos. I would suggest you experiment with larger intervals, especially sixths, because they get you out of the typical linear runs or third intervals which are so typical for guitar (because these can be played with relative technical ease). Also fourths and fifths are great. One good exercise with fifths is playing up or down a scale on a pair of strings in perfect fifths (skipping the awkward diminished fifth). E.g. on the b and e string you could do something like this (in C major):

e     3   5        7  8        10  12          15  ...
b  1        3   5        6  8          10  13      ...

Use slides for moving up. Sixths are most easily played on a pair of strings with one string in between, e.g. on the g and e strings.
What is most important to make all of these exercises useful in a musical context is the use of interesting rhythms. The basic exercises are often written down as simple eighth or sixteenth note runs, but that gets old pretty soon. Experiment with rests, triplets, syncopations, etc. to keep it exciting and alive.
Last thing: when practicing technical stuff like that, also train your ears at the same time. Try to predict the next note in your pattern in your head (or by singing or whistling it). Can you always correctly predict it? If so, then you'll always know if it actually sounds good in the given musical context (i.e. when you play over a backing track or with a band). If not, break the pattern and do something else, because now you're not practicing anymore but you're actually playing music!
Answer: Any chord will have certain notes which will match it better than others. Using your C key example, C-E, E-G will work well, as those notes appear in the C chord. If you played C-E, D-F, E-G on beats 1, 2 and 3 of a bar it would sound fine. Starting on beat 1 with an appropriate pair of notes always works well.
With 4ths, you'll have to be more picky. G-C will obviously work over C, but the upwards pattern will not really lend itself to the rest of the bar. Same with 5ths, and parallel 5ths were taboo in classical pieces for the reason they just don't sound good.
Better, maybe, to use a 5th, play another note or two, then another 5th appropriate to the chord at the time, or play on a C bar, say, E,F,G.C and so on, as those notes work towards your 4th, and fit the chord well. As exercises, the concept is good, but it's not particularly helpful to use as a basis for building up a set of licks.
Listen to stuff - the consecutive idea is used, but usually only in short bursts, rather than a whole line. A bit like - we learn a scale, but rarely use all the notes in that order in a solo.
Comment: Do you really mean "consecutive 5ths", or maybe "parallel 5ths"? Because consecutive 5ths are great, and Andy Summers would need to revise at least half of his guitar parts if they were taboo ... (OK, I know you mentioned classical music, but I believe even there consecutive 5ths would be OK).
Comment: Sorted!Thanks, Matt. Yes, commonplace on gtr now. As is the Devil's interval, banned a couple of hundred yrs ago.
Answer: "Strong" melodic notes are notes that are in the chord being played, especially if they're played on the beats.
Use your interval patterns as embellishments on the strong melodic notes that you play or as a way of breaking up or embellishing scalar runs.   It's good in small doses.
It's especially effective when you shred using very fast runs up and down and across the fretboard.  Musicians as diverse as Frank Zappa and Eddie Van Halen used this technique.
Another place where interval patterns are effective are when you're playing an ostinato under other instruments. Embellishing an ostinato with intervals above and below the ostinato notes make for a very interesting addition that engages the audience without them understanding exactly what's going on.


python3 tkinter scrollbar problemas a inserir

Question: bom dia,
Eu estava a criar uma interface gráfica com python e para isso usei o tkinter e estava a tentar criar uma scrollbar dentro do meu projeto porem ele nao reconhece o comando para conectar a scroolbar as label's existentes(segue o exemplo):
from tkinter import *

class JanelaPrincipal(Frame):
        def __init__(self,master):
                Frame.__init__(self,master)
                self.master.title('Avocado')
                self.master.geometry('800x600')
                self.configure(height=200,width=200)
                #scroll bar
                self.scroll=Scrollbar(master)
                self.scroll.pack(side=RIGHT, fill="y")
                self.batata=Label(self.master,width="800",height="10",text="Algum texto",relief="raise",yscrollcommand=self.scroll.set)
                self.batata.pack(expand=0, fill=BOTH, side=TOP)

root=Tk()
app=JanelaPrincipal(root)
root.mainloop()

O erro que retorna é:
Traceback (most recent call last):
  File "pap.py", line 16, in <module>
    app=JanelaPrincipal(root)
  File "pap.py", line 12, in __init__
    self.batata=Label(self.master,width="800",height="10",text="Algum texto",relief="raise",yscrollcommand=self.scroll.set)
  File "/usr/lib/python3.6/tkinter/__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "/usr/lib/python3.6/tkinter/__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-yscrollcommand"

Já pesquisei bastante porem todos usam aquele argumento e quando tento usar o interpretador da erro, se alguem poder ajudar agradeço
Answer: Acontece que você está tentando criar uma scroll em um Label e essa classe não tem a opção yscrollcommand.
Você pode utilizar essa propriedade em Listbox, Canvas etc.
Veja um exemplo de utilização dela com a classe Text, utilizando seu código como base:
from tkinter import *

class JanelaPrincipal(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.master.title('Avocado')
        self.master.geometry('800x600')
        self.configure(height=200,width=200)
        #scroll bar
        self.scroll=Scrollbar(master)
        self.scroll.pack(side=RIGHT, fill="y")
        self.batata=Text(self.master,width=800,height=10,relief="raise",yscrollcommand=self.scroll.set)
        self.batata.insert(END, "Algum texto")
        self.batata.pack(expand=0, fill=BOTH, side=LEFT)

root=Tk()
app=JanelaPrincipal(root)
root.mainloop()


Cron not working when called from SSH php curl - Elysia cron module activated

Question: I have Elysia cron module activated, with some cron task scheduled.
I have a crontab pointing to an sh every */2 minutes. The sh executes a php with a curl call to cron.php?cron_key=CRON-KEY. It was working perfectly some weeks ago. Suddenly, it has stopped working. When I launch the same cron.php address by browser, it works perfectly, executes the cron with all the scheduled tasks in order as defined by Elysia config.
But when the same cron.php is called via the SSH php (console or just through the cron daemon), it does not work. It gives no error or hint, just does not do anything. I have tested curl in the same domain, with GET parameters, to a different php file, and it is working. I have no idea what can be happening, I have re-installed Elysia cron, and the same results. I cannot understand why Drupal knows that the script is called via a non-browser script and why it stops working.
Answer: After much debugging (too much debugging, I am afraid), I discovered that the problem was in that the cron did not go through all the steps (bootstrap). The problem (which was not logged) was in the Domain Access module, in which I deleted the English language in one of the domain. IMHO, this is a bug, but not sure if of the module, the core or what. I cannot afford wasting 8 hours of my time looking for errors when I make a simple change that, in principle, should not affect the entire performance of Drupal. This is the kind of things that make Drupal not trustworthy.


Is it bad practice to use the same hook twice?

Question: I'm working a quite a big project and I use hook_entity_update() in three of my modules. I do this to keep my project structured and have different functions of my site in different modules.

Is it bad performance wise to have this hook called multiple times from different modules?
Since this way of working makes the number of custom modules grow, does the number of modules affect the performance of the site or is it only the code inside that can give performance issues?
Answer: Of course you would have to benchmark your modules to get an exact answer, but generally speaking, the benefits of breaking up complex code into multiple modules separated by function is the way to go.
The performance impact of calling the hook is minimal; the larger performance impact will be whatever code the hook runs.
The number of modules does not matter nearly as much as the code that the modules are running.
Evidence: Moving from Drupal 7 to Drupal 8/9, many major Drupal modules have gone from "minimize the number of modules" to "provide many more function-specific modules that can be enabled as needed":

AdvAgg
Commerce
Metatag
Webform

TLDR: The actual code that you run in hooks/modules is way more important for performance than the fact that you are calling hooks multiple times in modules.
However, one thing you have to keep aware of is that if you organize your modules poorly, you can end up adding custom code to the same hook in multiple modules that could result in bugs, such as if you are changing the same field in hook_entity_update().  So make sure you organize your code in a way that makes it clear which modules are responsible for which functions.
As a developer, it is definitely easier to maintain custom modules when they are separated by function, especially in the case that you end up having to use some but not all of the functions/modules on multiple sites.
Comment: Thanks for the extensive answer, very clear and very helpful
Answer: The number of installed modules may influence performance. That's why, for example, Drupal core keeps in cache the name of the implemented hooks.
Given what Drupal core does, increasing the number of installed modules would have a lower impact on performance.
The aim for developers should not be reducing the number of modules/sub-modules, but writing modules that implement a specific feature. Clearly, if implementing a feature requires few lines, probably the module should be merged with another module, especially if that module is a sub-module.
To make an example, instead of the Node and User modules, there could be a single module (called, for example, Base Content). Since those modules are normally required from the installation profile, it would reduce the number of installed modules. It could also reduce the number of implemented hooks, since (for example) node_user_predelete() could be merged with a class method or another function.
Anyway, doing so, two modules with different purposes would be merged together; the hooks could be reduced, but in some cases the code could now need to first check some conditions are met; probably more services should be lazy-loaded, which means that there would be two classes instead of one (the proxy class and the class implementing the service). Those classes would need to be synchronized with each other: Any change on the service class needs to be done on the proxy class too. (There is a script for doing that, but it works only on installed modules, which means the developer would need a local server running Drupal and remember to run the script for every lazy-loaded service.)
It's not just a matter of implemented hooks: In Drupal 9, a module that is complex enough implements many classes, some of which are service or plugin implementations. Even modules that are simple enough to just need a single route implement code in at least a class.


What kind of non-chord tone dissonance is this?

Question:
What is the correct term for this type of non-chord tone? I really like the sound of it and since I see it a lot I was wondering if an "official" name exists for it. It is not a suspension because the P4th does not resolve over the next chord (Eb) at the beginning of the second bar. Instead, a second dissonance is created (M2) before resolving to a M3 on the upbeat.
Comment: It's not really a suspension because the F is non-chordal, but it behaves like one: resolving to the fifth of the C-minor 1st inversion chord.
Comment: The treble upper stave(s) should be included here. Whatever is happening there could impact the answer by changing the OP-given analysis of C minor followed by Eb major.
Comment: @Aaron, there is nothing there. It is just the two voices. THanks for checking
Answer: Fundamentally, it's a passing tone, but with a delayed resolution. There's no "official" music theory term for this.
Comment: there are 2 intervals created one after the other so how can it be a passing tone? isnt that only against one other stationary note?
Comment: @armani A passing tone is any non-chord tone that serves to connect two chord tones. It doesn't matter that the harmony happens to change.
Comment: @armani Consider also that since this is a two-voice texture, the second bar doesn't necessarily start with Eb major — it could be C minor in second inversion.
Comment: ok, and f it were Cmin in second inversion, how does that change anything? doesnt it not matter since the dissonance is still a P4 and M2 against the bass notes
Comment: also, dont you mean 1st inversion? C/Eb
Comment: @armani Yes, first inversion. The intervals don't matter. What matters is that the F is leading from a chord tone to another chord tone. The arrival just happens to be delayed.
Comment: I really like this interpretation. Although the texturing gives the appearance of a suspension-like dissonance, if you look closely this is indeed a passing tone happening inside of a C minor harmony.
Comment: Thanks, @Ootagu. I agree — it really does "feel" suspended; calling it a "passing tone", while technically correct, doesn't really capture the feel.
Answer: the harmony in this bar is Eb (respective to your earlier posting)
the note F is an appoggiatura resolving to the 3rd (G) - in common practice it was on the beat but - in pop and jazz it is ahead of  the beat:
https://en.wikipedia.org/wiki/Appoggiatura
Answer: Perhaps we could call it an anticipated suspension?
Comment: Nice! You are thinking outside the box :)
Answer: The first F occuring against the C bass can also be seen as a chord tone! The F against the C implies F-minor. Although this creates a fourth with the bass C and and is often defined as a dissonance in strict counterpoint, in strict harmony it is a chord change as opposed to a dissonance. After the barline it then becomes a dissonance against the E-flat and could be called a retardation resolving upwards to the G. (On a side note the harmony after the barline is also ambiguous; it can taken as carrying on the C minor previous)
Comment: THanks for your analysis. What did you mean by "strict harmony"?
Comment: I admit it was a strange term which I made up myself. I was trying to emphasize a purely harmonic mode of thinking because I know oftentimes we can get caught in the idea that a fourth is always dissonant.


prove $\prod_{i=0}^{n}x-\frac{i}{n}\le\frac{n!}{4n^{n+1}}$ for any $x\in[0,1]$ and $n>2$

Question: prove $\prod_{i=0}^{n}x-\frac{i}{n}\le\frac{n!}{4n^{n+1}}$ for any $x\in[0,1]$ and natural $n>2$.
I tried using AM-GM, but found nothing interesting.
I was looking also for a Taylor series. no luck
Comment: What do you mean by "averages inequality"?
Comment: https://en.wikipedia.org/wiki/Inequality_of_arithmetic_and_geometric_means
Comment: I've added that link into your question because there are other average-comparing inequalities.
Answer: Let $(\sigma_0,\ldots,\sigma_n)$ be a permutation of $(0,\ldots,n)$ such that $$|x-\tfrac{\sigma_0}{n}| \le |x-\tfrac{\sigma_1}{n}| \le \cdots \le |x-\tfrac{\sigma_{n-1}}{n}| \le |x-\tfrac{\sigma_n}{n}|.$$ In other words, of the points $\{\tfrac{0}{n},\tfrac{1}{n},\ldots,\tfrac{n-1}{n},\tfrac{n}{n}\}$, $\tfrac{\sigma_0}{n}$ is the closest to $x$, $\tfrac{\sigma_1}{n}$ is the 2nd closest to $x$, ..., and $\tfrac{\sigma_n}{n}$ is the farthest from $x$, with ties broken arbitrarily.
If $x = \tfrac{k}{n}$ for some $k = 0,\ldots,n$, then $\prod_{i = 0}^{n}|x-\tfrac{i}{n}| = 0 \le \tfrac{n!}{4n^{n+1}}$ holds trivially.
Now, suppose $x \in (\tfrac{k-1}{n},\tfrac{k}{n})$ for some $k = 1,\ldots,n$. Then $\{\sigma_0,\sigma_1\} = \{k-1,k\}$, and thus, $$\left|x-\tfrac{\sigma_0}{n}\right| \cdot \left|x-\tfrac{\sigma_1}{n}\right| = (x-\tfrac{k-1}{n})(\tfrac{k}{n}-x) \le \left(\dfrac{(x-\tfrac{k-1}{n})+(\tfrac{k}{n}-x)}{2}\right)^2 = \dfrac{1}{4n^2}.$$
Also, for $i = 2,\ldots,n$, we have $|x-\tfrac{\sigma_i}{n}| \le \tfrac{i}{n}$ since at least $i+1$ of the points $\{\tfrac{0}{n},\tfrac{1}{n},\ldots,\tfrac{n-1}{n},\tfrac{n}{n}\}$ are within $\tfrac{i}{n}$ of the point $x$. Hence, $$\prod_{i = 0}^{n}|x-\tfrac{i}{n}| = \prod_{i = 0}^{n}|x-\tfrac{\sigma_i}{n}| = \left|x-\tfrac{\sigma_0}{n}\right| \cdot \left|x-\tfrac{\sigma_1}{n}\right| \cdot \prod_{i = 2}^{n}\left|x-\tfrac{\sigma_i}{n}\right| \le \dfrac{1}{4n^2}\prod_{i = 2}^{n}\dfrac{i}{n} = \dfrac{n!}{4n^{n+1}}.$$
Answer: Let $f(x)=\prod_{i=0}^n\left|x-\frac{i}{n}\right|$. Since $f$ is continuous on the compact interval $[0,1]$, it attains its max at some $y\in[0,1]$. As $f(x)=f(1-x)$, we may assume WLOG that $y\in[0,1/2]$.
If $y>1/n$, then
$$f\left(y-\frac{1}{n}\right)=\left(1-\frac{1}{n}-y\right)\cdot\prod_{i=0}^{n-1}\left|y-\frac{1}{n}-\frac{i}{n}\right| > y\cdot\prod_{i=1}^n\left|y-\frac{i}{n}\right|=f(y).$$
So we must have $0\leq y\leq\frac{1}{n}$. Then by AM-GM, $y\left(\frac{1}{n}-y\right)\leq\frac{1}{4n^2}$, so
$$f(y)=y\left(\frac{1}{n}-y\right)\cdot\prod_{i=2}\left(\frac{i}{n}-y\right)\leq\frac{1}{4n^2}\cdot\prod_{i=2}^n\frac{i}{n}=\frac{n!}{4n^{n+1}}.$$


In arangodb how to save decimal with trailing zeroes in order to keep the precision of the value

Question: So if the user inputs 12.00000 I need to save the trailing zeroes 5 in this example.
I am evaluating arangodb. But there is no clear information about this.
I would use BigDecimal in Java to handle the numbers or any other way that is ok by arango.
Answer: Arango uses the same types as Javascript. You are essentially storing JSON data - which doesn't allow you to specify types.
So there are no decimal based numerical types. You would need to store the value as a string if you want to keep the decimals exactly, without conversion to floats.


Enviar Informações do Link para uma Modal

Question: Estou desenvolvendo usando Bootstrap e ASP.NET MVC. Estou utilizando uma Table onde em cada linha tenho os botões Editar e Deletar. Estou utilizando o seguinte código:

    @foreach (var item in Model.REGISTRO_PERGUNTAS)
    {

            @item.ID_ARQUIVO
            @item.NUMERO_PERGUNTA
            @item.PERGUNTA

                @Html.ActionLink("Editar", "Editar", "Cadastros",
                new { id = item.NUMERO_PERGUNTA},
                new { href = "#myModal", data_toggle = "modal" })

                @Html.ActionLink("Deletar","Deletar", "Cadastros",
                new { id = item.NUMERO_PERGUNTA, pergunta = item.PERGUNTA },
                new { href = "#myModal", data_toggle = "modal" })

    }

Gostaria de saber como faço para pegar os valores da linha que eu clicar em Editar ou Deletar e envia-los para a modal que eu criei?
Comment: Você quer chamar o método que devolve uma `ActionResult` no Controller e exibir uma modal em JS, é isso?
Comment: A modal já funciona como mostro no trecho de código acima quando clico em Editar ele gera o modal pra mim,mas não consigo pegar os valores da linha que eu cliquei. Daí de alguma forma retornar estes valores para o modal que acredito deva ser feito pela ActionResult.
Comment: Va verdade o retorno é pelo JS mesmo.
Answer: Adicione atributos de data-nome-do-atributo no link da seguinte forma
Editar

e usar o evento on show do modal do bootstrap
$('#my_modal').on('show.bs.modal', function(e) {
    var idPergunta = $(e.relatedTarget).data('id-pergunta');
    $(e.currentTarget).find('input[name="perguntaId"]').val(idPergunta);
});

Se utilizar frameworks JS para front end como Angular ou KnockoutJs seria ainda mais simples, mas acho que assim deva lhe atender
JsFiddle
Answer: A forma mais prática que me ocorre é seres tu a chamar a modal sempre que precisas, fazendo-o dentro de uma função onde primeiro recolhes os dados que precisas e envias os mesmos para dentro da referida modal.
Retira do teu código a chamada da modal via atributo data:

jQuery
$(document).ready(function(){

  // anexamos o evento à classe de CSS "modal-trigger"
  $('.modal-trigger').on("click", function(e) {
    e.preventDefault();

    // recolhemos os dados que pretendemos
    var $this  = $(this),
        $modal = $($this.data("target")),
        name   = $this.data("name"),
        action = $this.data("action"),
        title  = $this.closest("tr").find("td:first-child").html();

    // se a markup da modal existe, alteramos o que pretendemos
    if ($modal.size()==1) {

      $modal.find('#myModalLabel').html("A " + action + " " + title);
      $modal.find('.modal-body').html("Hei " + name + ", é para " + action + "?");
      // chamamos a modal
      $modal.modal('show');
    }
  });
 });

No código em cima e exemplo em baixo, o que é feito:

Anexar evento de clique para que ao clicar num elemento da página que contenha a classe de CSS modal-trigger seja executado determinado código;
Anulamos o comportamento por defeito desse elemento com uso do método preventDefault();
Recolhemos dados presentes no botão com recurso ao método .data(), adicionalmente recolhemos um texto da linha da tabela onde o botão se encontra;
Verificamos se a modal está presente no DOM fazendo uma contagem do total de elementos com o ID da mesma, ver método .size();
Se existe vamos alterar o título da modal e o conteúdo da mesma com os dados recebidos;
Por fim, chamamos a modal fazendo uso do método .modal('show').

Demonstrações

$(document).ready(function(){

  $('.modal-trigger').on("click", function(e) {
    e.preventDefault();

    var $this  = $(this),
        $modal = $($this.data("target")),
        name   = $this.data("name"),
        action = $this.data("action"),
        title  = $this.closest("tr").find("td:first-child").html();

    if ($modal.size()==1) {

      $modal.find('#myModalLabel').html("A " + action + " " + title);
      $modal.find('.modal-body').html("Hei " + name + ", é para " + action + "?");

      $modal.modal('show');
    }
  });
 });

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/>

        Coisas boas na vida

        <button type="button" class="btn btn-primary btn-sm modal-trigger" data-target="#myModal" data-name="bubu" data-action="Editar">Editar</button>

        <button type="button" class="btn btn-danger btn-sm modal-trigger" data-target="#myModal" data-name="bubu" data-action="Apagar">Apagar</button>

        <button type="button" class="close" data-dismiss="modal">×Fechar</button>

Modal title

        ...

        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
        <button type="button" class="btn btn-primary">Siga...</button>

Demonstração também no JSFiddle.


Why modulo 2 can be express as cos function for optimization?

Question: I have been reading the paper Monotonic optimization based decoding for linear codes. In this paper the author show that modulo 2 operation can be rewritten as cos function.
Particularly, an $\left( {n,k} \right)$ with the corresponding generator matrix $G$ of size $n \times k$ is considered
$G = \left[ {\begin{array}{*{20}{c}}
{{g_1}}\\
{{g_2}}\\
{...}\\
{{g_n}}
\end{array}} \right]$ each row is ${g_i} \in {\left\{ {0,1} \right\}^k}$
Let $u, x$ be the column vectors of the input bit sequence/message and the corresponding
encoder output codeword. This will give the relation $x = Gu\left( {\bmod \,\,2} \right)$. The author then show this without any proof or intuition explain
$Gu\left( {\bmod \,\,2} \right) = \frac{{{1_n} - \cos \left( {\pi Gu} \right)}}{2}$ where
$\cos \left( {\pi Gu} \right): = {\left( {\cos \left( {\pi {g_1}u} \right),\cos \left( {\pi {g_2}u} \right),\cos \left( {\pi {g_3}u} \right),...,\cos \left( {\pi {g_n}u} \right)} \right)^T}$
How can modulo 2 be express as cos function (for optimization latter in the continuous space) ?
Please help me understand this
Thank you very much
Answer: $$\frac{1-\cos0}2 = 0\\
\frac{1-\cos\pi}2 = 1\\
\frac{1-\cos2\pi}2 = 0\\
\frac{1-\cos3\pi}2 = 1\\
\vdots$$


Using MySQL, always have the expected number of elements in a dataset by filling the remaining with previous written data

Question: is it possible to always respect an expected number of element constraint by filling the remaining of a SQL dataset with previous written data, keeping the data insertion in order? Using MySQL?
Edit
In a web store, I always want to show n elements. I update the show elements every w seconds and I want to loop indefinitely.
By example, using table myTable:
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
+----+

Something like
SELECT id FROM myTable WHERE id > 3 ORDER BY id ALWAYS_RETURN_THIS_NUMBER_OF_ELEMENTS 5

would actually return (where ALWAYS_RETURN_THIS_NUMBER_OF_ELEMENTS doesn't exist)
+----+
| id |
+----+
|  4 |
|  5 |
|  4 |
|  5 |
|  4 |
+----+

Comment: Although I find the question highly unusual, I see nothing about it that deserves a down vote.
Comment: I don't understand why my question has a down vote.
Answer: No, that's not what LIMIT does. The LIMIT clause is applied as the last step in the statement execution, after aggregation, after the HAVING clause, and after ordering.
I can't fathom a use case that would require the type of functionality you describe.

FOLLOWUP
The query that Gordon Linoff provided will return the specified result, as long as there is at least one row in myTable that satisfies the predicate. Otherwise, it will return zero rows.

Here's the EXPLAIN output for Gordon's query:
id select_type  table            type  key     rows Extra
-- ------------ ---------------- ----- ------- ---- -------------------------------
 1 PRIMARY      <derived2>       ALL              5 Using temporary; Using filesort
 1 PRIMARY      <derived3>       ALL              5 Using join buffer
 3 DERIVED                                          No tables used
 4 UNION                                            No tables used
 5 UNION                                            No tables used
 6 UNION                                            No tables used
 7 UNION                                            No tables used
   UNION RESULT <union3,4,5,6,7> ALL
 2 DERIVED      myTable          range PRIMARY   10 Using where; Using index

Here's the EXPLAIN output for the original query:
id select_type table             type  key     rows Extra
-- ----------- ----------------- ----- ------- ---- -------------------------------
 1 SIMPLE      myTable           range PRIMARY   10 Using where; Using index

It just seems like it would be a whole lot more efficient to reprocess the resultset from the original query, if that resultset contains fewer than five (and more than zero) rows. (When that number of rows goes from 5 to 1,000 or 150,000, it would be even stranger.)
The code to get multiple copies of rows from a resultset is quite simple: fetch the rows, and if the end of the result set is reached before you've fetched five (or N) rows, then just reset the row pointer back to the first row, so the next fetch will return the first row again. In PHP using mysqli, for example, you could use:
$result->data_seek(0);

Or, for those still using the deprecated mysql_ interface:
mysql_data_seek($result,0);

But if you're returning only five rows, it's likely you aren't even looping through the result at all, and you already stuffed all the rows into an array. Just loop back through the beginning of the array.
For MySQL interfaces that don't support a scrollable cursor, we'd just store the whole resultset and process it multiple times.  With Perl DBI, using the fetchall_arrayref, with JDBC (which is going to store the whole result set in memory anyway without special settings on the connection), we'd store the resultset as an object.
Bottom line, squeezing this requirement (to produce a resultset of exactly five rows) back to the database server, and pulling back duplicate copies of a row and/or storing duplicate copies of a row in memory just seems like the wrong way to satisfy the use case. (If there's rationale for storing duplicate copies of a row in memory, then that can be achieved without pulling duplicate copies of rows back from the database.)
It's just very odd that you say you're using/implementing a "circular buffer", but that you choose not to "circle" back around to the beginning of a resultset which contains fewer than five rows, and instead need to have MySQL return you duplicate rows. Just very, very strange.
Comment: I just edit my question, I just want to respect a number of element. I want to use it in a circular buffer.
Comment: @Pier-AlexandreBouchard, if the buffer's circular, wouldn't two elements circle just as well?
Comment: A circular buffer? You mean like a round wastebasket?
Comment: Excuse me, me question is not really clear. in a web store, I always want to show n elements. I update the show elements every n seconds and I want to loop indefinitely.
Comment: @Pier-Alexandre Bouchard: your use case sounds more like a presentation issue than it does a data retrieval issue; that is, you don't really need for MySQL to return exactly N rows. If MySQL returns fewer than N rows, then just repeat the rows that MySQL *does* return however many times you need. You needn't muck up the query with the logic to do that.
Comment: Maybe the use case I gave sounds like a presentation issue, but in the real case, it's not. The web store example was just to figure out a viable example to build the query.
Comment: @Pier-Alexandre Bouchard: The query that Gordon Linoff provided will return the specified result (as long as there is at least one row that satisfies the predicates). But its very strange that you mention a "circular buffer", yet you can't make your code "circle" back to the first row in a resultset that has less than five (and more than zero) rows. Just very, very strange.
Answer: This is a very strange need.  Here is a method:
select id
from (SELECT id
      FROM myTable
      WHERE id > 3
      ORDER BY id
      LIMIT 5
     ) t cross join
     (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5
     ) n
order by n.n, id
limit 5;

You may need to extend the list of numbers in n to be sure you have enough rows for the final limit.
Comment: Two words for the requirement this statement satisfies: *biz* and *arre*.
Comment: If you read my edit about the use case, maybe it will be easier for you to answer :)
Comment: Using a query like this to satisfy the use case is very strange indeed. The use case sounds much more like a presentation issue than it does a data retrieval issue; mucking up the query that retrieves rows to make it return exactly N rows by repeating rows is just not the right way to satisfy the use case.


Returning an array of all content types in a folder

Question: I exported all my configuration files to config/sync.
Now i'm trying to get all the content types from that folder into an array.
Its a bit like what this code does:
$manager = \Drupal::entityTypeManager();

$manager->getStorage('node_type')->loadMultiple();

But that returns all the Installed content types on my drupal.
I just want the ones from my sync folder because i'm making twig templates from them.
Answer: If you mean node types, every node type generates (amongst others) a file called node.type.NODE_TYPE_MACHINE_NAME.yml when exporting to config/sync
Answer: To know the list of files in your config/sync folder

Open terminal
Go to config/sync folder in your site folder cd config/sync
Run a grep command like ls | grep node.type


Critique on Composition I made

Question: I was hoping someone with a solid background in jazz music theory to give me a solid critique on a jazz blues composition I made last month. Here's the link:
https://musescore.com/user/6040626/scores/6551517?share=copy_link
If the link doesn't work, go to MuseScore and in the search bar, type "Waldario Blues", and my score should pop up.
Comment: Although off-topic for the Q&A part of the site, you can post in the chat room.
Answer: The players might appreciate double barlines and rehearsal letters to mark the sections.  Also chord symbols throughout.
Did you consider a snap ending on the last 8th of 160?
(The topic will probably get moved somewhere soon.)

Critique on Composition I made


Python How to read string from file and insert into URL

Question: I am trying to create an API call on a Raspberry-Pi with no GUI. The URL must have user specific credentials, these credentials will not change so am trying to write to, then read from a file and insert to the correct part of the URL.
WIP code so far:
#!/usr/bin/python

system_id = raw_input("What is your Enphase System ID: ")
user_id = raw_input ("What is your Enphase User ID: ")

f1 = open ("enphase_system_id.txt", 'w+')
value1 = (system_id)
s1 = str (value1)
f1.write(s1)

f2 = open("enphase_user_id.txt", 'w+')
value2 = (user_id)
s2 = str (value2)
f2.write(s2)

url = ("https://api.enphaseenergy.com/api/v2/systems/"
       +f1.read()+
       "/summary?key=6ba121cb00bcdafe7035d57fe623cf1c&usf1c&usf1c&user_id="
       +f2.read())

print url

f1.close()
f2.close()

from urllib2 import urlopen

inverter = urlopen(url)

response = inverter.read()
body = response

print body

The problem is the URL is concatenated without the read strings:
https://api.enphaseenergy.com/api/v2/systems//summary?key=6ba121cb00bcdafe7035d57fe623cf1c&usf1c&usf1c&user_id=
Today is literally my first day programming in any language, your help much appreciated.
Comment: Just to be clear: you are reading things from the console. writing them to open files, then trying to read them back out of those same open files?
Comment: Yes, I know the raw_input and reading form file is redundant, I aim to figure out how to not request raw_input if the files already exist.
Answer: You haven't closed files after writing to them (to be precise, You closed them too late). That's first thing. Next, You need to open those files after closing them in order to read them. Also, You can simply do:
f1.write(value1)
f2.write(value2)

instead of
value1 = (system_id)
s1 = str (value1)
f1.write(s1)

and
value2 = (user_id)
s2 = str (value2)
f2.write(s2)

Comment: Thanks, I read that because the IDs were numeric, I would have to create them as strings before writing to the .txt file.

How do I insert the contents of the files, into the URL?
Comment: They are read as strings, no meter they are numeric. `raw_input` returns string. You are doing good thing with reading (`f1.read()`).
Comment: Ok thanks, it doesnt like the concatenation:
    +f2.read)
TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects
EDIT: Sorry I made a typo, all working now, thanks!


Why doesnt my network activity icon appear in my iPhone app

Question: Im using the standard:
<code>[NSURLConnection sendSynchronousRequest:theRequest
                      returningResponse:&response
                                  error:&error];
</code>
method, but while the request is away, for both waiting for the response and downloading the data, the activity icon in the OS (very top) doesnt spin showing network activity.
Is there a flag or something I have to switch on to get it to work? Or am I not using the correct downloading technique?
Thank
Mark
Answer: You need to enable that from your application.
<code>[ UIApplication sharedApplication ].networkActivityIndicatorVisible = YES;
</code>
Comment: be careful if you have multiple things in your application that can access the network and turn this flag on/off.  A set of utility functions that reference count your use of the network (and turn the flag on/off when needed) will alleviate headaches in the future
Answer: To expand on the other answers: you have to turn the indicator on and off manually with the networkActivityIndicatorVisible property. It has nothing to do with whether and when data actually travels in and out of your app. I originally also thought iPhone automatically intercepts the network traffic and manages the indicator, but nope.
Answer: try this.
<code>UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
</code>


Trouble with XPCOM XPathEvaluator

Question: I'm having trouble evaluating XPaths using the nsIDOMXPathEvaluator from Mozillas XPCOM. I'm running the following javascript code through XPCShell:
[...]
var myPaths = new Array();
myPaths[0] = "/";
myPaths[1] = "/node()";
myPaths[2] = "/html";

for(i in myPaths) {
    myPath = myPaths[i];
    var document = doms[0];
    var xpEval = Components.classes["@mozilla.org/dom/xpath-evaluator;1"].createInstance(Components.interfaces.nsIDOMXPathEvaluator);
    var ns = xpEval.createNSResolver(document.documentElement);
    var type =  Components.interfaces.nsIDOMXPathResult.UNORDERED_NODE_SNAPSHOT_TYPE;

    var res = xpEval.evaluate(myPath, document.documentElement, ns, type, null);
    dump("\nPath: "+myPath+"\n");
    dump("Result length: "+res.snapshotLength+"\n");
    for ( var i=0 ; i < res.snapshotLength; i++ )
        dump("... Node: "+res.snapshotItem(i)+"\n");
        dump("... ... .nodeName: "+res.snapshotItem(i).nodeName+"\n");
}
[...]

The doms-list is an array of html-documents parsed using do_parse_document from the xpcshell testing utilities. The top of all documents are:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

My trouble is that the XPaths I use as input doesn't quite return what I expect. Below is the output from the above snippet:
Path: /
Result length: 1
... Node: [object XMLDocument]
... ... .nodeName: #document

Path: /node()
Result length: 2
... Node: [object DocumentType]
... ... .nodeName: html
... Node: [object HTMLHtmlElement]
... ... .nodeName: html

Path: /html
Result length: 0

Path: //html
Result length: 0

I would expect at least 1 or 2 results from the /html and //html paths. (Those returned when using /node() as path)
I can't get correct output when doing queries like count(//p) either (that one returns 0 although there are plenty of paragraphs in the document).
I have tried calling document.evaluate() instead of xpEval.evaluate() with the same results. I have tried just passing null for namespace, same results.
Am I making some stupid mistake(s), or might there be some quirks in the nsIDOMXPathEvaluator that means I can't use it how I intend?
Thanks for your time!
Regards,
Torin
Comment: This is **FAQ**: name test without prefix match elements with null (or empty) namespace URI. Here you have an element under the `http://www.w3.org/1999/xhtml` namespace URI (default namespace declaration). So, you need to register a prefix to namespace URI binding for the XPath expression evaluation context.
Comment: I'm not too familiar with working with namespaces and I naively thought xpEval.createNSResolver() would solve that for me. Also I didn't consider the difference between working with xhtml and html - the guides I was following stated "null, which can be used for HTML documents or when no namespace prefixes are used.". I guess I should have been more observant on the xmlns part of the html-tag :) -- thanks for the reply anyway - as you can read in the accepted answer, what you say was exactly the issue.
Answer: Dump the namespaceURI property of the document.documentElement node, it is likely in the XHTML namespace http://www.w3.org/1999/xhtml and in that case with XPath 1.0 to select elements in any namespace you need to bind a prefix to the namespace URI and use that prefix. With the API you use you have to make sure your namespace resolver resolves your choosen prefix to the XHTML namespace URI. So you need e.g.
  var nsResolver = function (prefix) { if (prefix === 'xhtml') return 'http://www.w3.org/1999/xhtml'; else return null; };
  var res = xpEval.evaluate(myPath, document, nsResolver, type, null);

and then paths like /xhtml:html or /xhtml:html/xhtml:body/xhtml:h1.

Trouble with XPCOM XPathEvaluator


Exceção de stack overflow sendo gerada no método de instância "Equals()"

Question: Criei uma classe chamada MyList<T>, com a intenção de reproduzir a classe List, e sobrescrevi o método Equals para comparar sua instância com outra da mesma classe, da seguinte forma:
class MyList<T> {

    // Código da classe...

    public override bool Equals(object obj) {

        if (obj is MyList<T>) {
            return Equals(obj, this);
        }
        return false;
    }

    public static bool Equals(MyList<T> listA, MyList<T> listB) {

        if (listA.Count != listB.Count) {
            return false;
        }
        for (long i = 0; i < listA.Count; i++) {
            if (!listA[i].Equals(listB[i])) {
                return false;
            }
        }
        return true;
    }
}

O problema é que sempre que eu tento verificar se uma lista é igual a outra, o método de instância Equals() entra em um loop infinito e gera StackOverflowException com a seguinte traceback:
Stack overflow.
Repeat 35393 times:
--------------------------------
   at MyList`1[[System.__Canon, System.Private.CoreLib, Version=18.104.22.168, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Equals(System.Object)
   at System.Object.Equals(System.Object, System.Object)
--------------------------------
   at MyList`1[[System.__Canon, System.Private.CoreLib, Version=220.127.116.11, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Equals(System.Object)
   at TestCSharp.Program.Main(System.String[])

Se eu utilizar apenas o método estático Equals(), passando as duas instâncias de MyList<T>, a verificação ocorre normalmente. Dito isso, creio que o problema esteja no método de instância.
Console.WriteLine(MyList<string>.Equals(list, list2)); // Retorna true ou false
Console.WriteLine(list.Equals(list2));                 // Stack Overflow

O que eu estou fazendo de errado? Onde está a causa do loop infinito?
Comment: Tem algum motivo para estar utilizando `long`? `List.Count` é `int`...
Comment: @LuizFelipe Eu sei, mas eu quis minha propriedade como `long` com a intenção da minha lista poder conter mais elementos.
Answer: O método de instância está chamando o método errado pelas regras de resolução de sobrecarga. Esse método está recebendo um objeto do tipo Object e está passando para um método que espera Object, portanto não é o método estático Equals() que você escreveu ali na classe, já que ele espera um MyList e não um Object, Para resolver isso você tem que garantir que a chamada seja feito com um objeto MyList, fazendo um cast.
Isto é uma pena porque a versão 1.0 do C# não tinha mecanismos adequados e agora paga um preço eterno por isso, no 2.0 passou ter o mecanismo certo, mas era tarde demais.
Teste:
using static System.Console;

public class Program {
    public static void Main() {
        var x = new MyList<int>();
        var y = x;
        WriteLine(x.Equals(y));
    }
}

class MyList<T> {
    public override bool Equals(object obj) {
        WriteLine("instância");
        if (obj is MyList<T>) {
            return Equals((MyList<T>)obj, this);
        }
        return false;
    }

    public static bool Equals(MyList<T> listA, MyList<T> listB) {
        WriteLine("estático");
        return true;
    }
}

Veja funcionando no ideone. E no .NET Fiddle. Também coloquei no GitHub para referência futura.
Como otimização poderia verificar se as referências são iguais, assim não precisa verificar algo custoso. Em geral não se aconselha criar um método que será adotado pelo operador de igualdade que pode potencialmente levar horas para executar.
Comment: A que mecanismos você se refere?
Comment: Principalmente Generics.


Read Windows Event Log with PHP

Question: Is there any way to read Windows Event Log with PHP?
To be specific, I want to read windows users' logon & logoff activities with corresponding user names. I am using PHP with wamp server.
Comment: Related question: http://stackoverflow.com/questions/30517516/how-to-read-xml-microsoft-event-logs-with-php
Comment: In the link you provided they use a exported xml file. But in my case need to read event log directly.
Answer: I couldn't find a way to read Window's Event Log directly. But in my case I wanted have Logon & Logoff info. So I manage that in another way.
wrote two .bat files as follows.
LogOn.bat;
Which includes the following code.
echo Login %Date% %TIME% %USERNAME% >> {Directory path to log folder}\%Date%.txt

LogOff.bat;
which includes the following code.
echo LogOff %Date% %TIME% %USERNAME% >> {Directory path to log folder}\%Date%.txt

Then added these files to logon & logoff scripts. Bellow are the steps.

Open "Edit Group Policy" in windows.
Double click on "User Configuration" from the right pane.
Double click on "Windows Settings" from the right pane.
Double click on "Scripts (Logon/Logoff)"
Then double click on "Logon" and add previously prepared LogOn.bat file to the container.
Do same for the "Logoff" and add LogOff.bat.

Note;
This will log, logon & logoff information into a file name with Day. You can change the file name as your preference.
At the end I could read the logged text files via PHP. Hope this helps to someone.

Read Windows Event Log with PHP


Como excluir um dado de uma ou mais tabelas

Question: Eu tenho um id que aparece em varias tabelas mas, eu preciso excluir ele na pagina principal e que exclua em todas as outras tabelas também.
ex :
DELETE FROM admissao_dominio.*,  admissao.*,  bancarios.*,  boas_vindas.*,  documentacao.*,  exame_admissional.*,  gestao.*,  interno.*,  parametros_captacao.*,  propostas_contratacoes.*,  sede.*,  suporte_interno.*,  tipo.*,  usuario_atv.*,  vencimentos.*,  vias_documentos_funcionarios
WHERE EMAIL = '{$_GET['id']}'";

tá errado mas é tipo alguma coisa desse tipo que eu preciso.
Comment: Essas tabelas possuem relacionamentos entre si? Se elas se relacionam, você pode deletar usando inner join.
Comment: sim elas estão todas ligadas pelo id_usuário, ou por alguma outra chamada, tipo é um cadastro de usuário e os dados aparecem em diferentes lugares, mas quando eu clicar em excluir eu preciso excluir de todas as tabelas
Comment: Dá uma olhada nisso [aqui](https://stackoverflow.com/a/1233479/6580921), é bem provável que resolva o seu problema.
Answer: Pra deletar mais de uma tabela, você precisa juntar todas elas.
DELETE admissao_dominio,  admissao,  bancarios/*,  boas_vindas,  documentacao,  exame_admissional,  gestao, interno,  parametros_captacao,  propostas_contratacoes,  sede,  suporte_interno,  tipo,  usuario_atv,  vencimentos, vias_documentos_funcionarios */
FROM admissao_dominio
JOIN admissao ON admissao.ID = admissao_dominio.ADIMISSAO_ID
JOIT bancarios ON bancarios.ADMISSAO_ID = admissao.ID
/* join das demais tabelas */
WHERE EMAIL = '{$_GET['id']}'";

Tenta assim, fazendo join entre todas as tabelas, isso deve excluir corretamente!


if $a^{-1}ba=b^i$ then $a^nba^{-n}=b^{(i^n)}$ for all $n$

Question: Let $a$ and $b$ be to elements in a group such that $a^{-1}ba=b^i$ for some natural $i$.
prove that $a^nba^{-n}=b^{(i^n)}$ for all $n$.
I tried to manipulate the given expression but I didn't manage to reach the desired equality.
Comment: you mean $a^{-n}ba^{n}$ i think
Comment: it seems more reasonable but that's what written in my textbook. Do you think it's a typo?
Comment: because the way i proved, it comes out to be what i suggested.
Answer: you have $a^{-1}ba=b^i$. So multiply $a^{-1}$ and $a$ on left and right respectively to get $a^{-2}ba^2=a^{-1}b^{i}a=(a^{-1}ba)^i=b^{i^2}$. Now proceed by induction.


Meaning of Class count metrics

Question: We are working on a bigger project with Ruby on Rails. As you might know, Rails works with MVC pattern and typically enforces the following class types for Domain objects:

Controllers (+ Helpers)
Models
Views (e.g. erb)
Serializers
Mailers

We experienced - like many other teams - that this is not feasible for bigger, long-running projects.
So in our current project, we added some layers, so we typically have

Controllers (+ Helpers)
Policies (access control per controller)
Presenters (a.k.a ViewModel)
Forms (containing all attributes available in an HTML form)
Views
Models
Queries (static ActiveRecord queries)
Serializers
Mailers
Services (Domain Service which extracts business code from controller)

It is much more testable, maintainable and understandable.
However, our boss disagrees with "These are too many classes".
As he is typically a proficient architect (doing consulting), I don't want to simply reject this response. However, his time as active Java developer are gone and he often explains such sentences with his improved "gut feeling".
Of course, there are metrics that count classes - but is there a general approach to classify something like 'Classes per Domain object' or 'Class per Business case', which can be taken for an argument?
Answer: I have never heard of a rule or best practice which limits a number of classes per project, domain or whatever. One should create as many classes as he REALLY needs. The key word here is "REALLY" because quite often developers over complicate things and create unnecessary classes, layers or even tiers. Without the details about your project I suggest you do the following:

Ask your boss to elaborate on the "Too many classes" phrase. Ask for his concerns. If he is a good architect and a team member he understands his responsibility of coaching people.
Make sure you clearly explained to him why you needed those classes. Provide examples.
If your boss is stubborn, his best days as a developer are gone and he just wants to show his significance, remind him about his responsibility for a successful project. For example, tell him he is an architect and a person who makes the final decision and you will follow it because he is the one who is responsible for the project success but on the other hand it's your responsibility to be proactive and express any concerns you have to help your boss make the right decision. This should make your boss think again as he doesn't want to fail the project because of his "gut feeling".

Hope it helps!
Comment: Thanks for your answer. Our reason for adding those layers mainly is, that we are writing the 2nd incarnation of the project, because the first one wasn't maintainable anymore, because:
1. technical layers were mixed too much (e.g. view contained too much controller logic, controller to much model...)
2. business code was scattered throughout model/controller/view (mainly controllers :( )
Comment: Right but have you expressed all these concerns to your boss clearly? if yes, did he just say it was too much classes?
Comment: Yep, we did. Main problem is IMHO, that he feels, that the work for this project is going on too slow (while other projects with "less good architecture" are fine. However, the first incarnation was fast at first and then got slower and slower - while we now can put in features at the same pace over a longer time. This is a fact, we still trying to teach him...
Comment: wasn't he responsible for the first incarnation failure ?
Comment: Well, thats a topic for another discussion. As we are a small company, he is boss, product owner and customer at the same time, which brings both him as us into some difficult discussions. However, I thank you for your arguments.

Meaning of Class count metrics


Working on Confidence Interval and its meaning

Question: Supposed in a population, I take a sample with a size of 1500 to build a 96% confidence interval for the true mean $\mu$. Then such an interval is assumed to range from 6.8 to 8.0. Assume the sample mean is $\overline{x}$. Then I conclude that:
if a sample with a size of 1500 were repeatedly constructed, then over
 a long run 96% of the confidence interval formed would contain the true
mean $\mu$. (Another trivial interpretation is that I am 96% confident
that the true mean will lie between 6.8 and 8.0 so I don't want to
bother to mention this interpretation).
Am I correct with my above conclusion?
Thank you!
Comment: @DavidKozak But the statistics textbook states that for the second interpretation. This is an introduction course so I think they try to make it as simple and straightforward as possible. However, I am sure when I go deeper in the Stat major, I will understand more of your comment above. So I apologize in advance for my ignorance. And of course we will never touch Bayesian Paradigm in this intro course. Thanks David! I appreciate it.
Comment: which textbook are you using? This post will likely soon be flagged as a duplicate question and closed. I strongly urge you to click on some of the related questions and see more thorough explanations.
Comment: @DavidKozak Statistics: The Art and Science of Learning from Data (4th Edition) [link] (https://www.amazon.com/Statistics-Art-Science-Learning-Data/dp/0321997832/ref=sr_1_2?ie=UTF8&qid=1511322190&sr=8-2&keywords=statistics+the+art+and+science+of+learning+from+data)
Comment: @DavidKozak can you please try to post a quick response to my question so then I can mark your answer as accepted? This way, I don't need more contribution to the topic and it can be closed. Thanks!
Comment: [Searching for questions that contain both the "confidence-interval" and the "interpretation" tags](https://stats.stackexchange.com/questions/tagged/confidence-interval%2binterpretation) yields [the canonical answer](https://stats.stackexchange.com/q/26450/1352).
Answer: In a frequentist paradigm we treat the true mean $\mu$ as fixed. Since we are taking a sample from the generating distribution the randomness comes from this sample. In this way we construct a confidence interval. In this way, your first interpretation is correct: if we were to sample 1500 observations from the same distribution repeatedly then a confidence level of $\alpha$ implies that the true parameter $\mu$  would fall within the confidence interval $\alpha$% of the time.
The second, seemingly more natural interpretation is incorrect from a frequentist perspective. It is more akin to the credible interval of the Bayesian paradigm. Recall that a Bayesian treats the parameter $\mu$ as random. An $\alpha$% credible interval of [a, b] suggests that the subjective (due to the observed data) probability that $\mu \in [a, b]$ is $\alpha$%.


Different number of decimals for prices and totals

Question: I use Drupal Commerce and I have a problem with the number of decimals in prices. In Spain (and I suppose in other countries too), I could have prices of products with more than two decimals, but at the end, the totals are rounded to two decimals. With currency settings module I can set the number of decimals, but It is for all (prices and totals). Is there anyway I could have four decimals for prices and two for totals?
Thanks in advance.
Answer: If you can set the number of decimal places site wide... I would set it to most precision I am likely to need and shorten it through the theme/js in the places where I need it shorter.  It may not be as graceful as variable precision, but if no other answers come up, it will get the job done...
Comment: Thanks for your comment but It can't be done as you say. For example, if I set decimals to four, then taxs and totals will be internaly rounded to four decimals that is wrong.
Comment: Have you explored https://www.drupal.org/project/commerce_price_decimals_formatter ?
Comment: Yes, but It's the same. This module let you change the display but not the process of rounding. Internaly, It will continue doing calculations using two decimals.
Comment: commerce.currency.inc

'decimals' => 3,   <-- or whatever number of decimal places you need...

You may have to set up different currencies to vary the precision
Comment: Thanks, but It's the same. I can put it whatever number of decimals I need, but I doesn't solve the problem. For example, I have a product wich costs is 0,0233 €. I need 4 decimals price, but totals in Euros are with 2 decimals.


How to find variable name from string-array on strings.xml

Question: Maybe this is a bit complicated but I have an Kotlin Android Studio app that says the year and the month the car was registered based on the Letters of the 2 letters of the license plate.
The letters goes AA to ZZ (example: AA, AB, AC ... AZ, BA, BB ... ZX ZZ)
and I have in the file strings.xml the values stored
Example:
<code> <string-array name='AP'>
        <item>2005</item>
        <item>9</item>
    </string-array>
    <string-array name='AQ'>
        <item>2005</item>
        <item>9</item>
    </string-array>
    <string-array name='AR'>
        <item>2005</item>
        <item>10</item>
    </string-array>
    <string-array name='AS'>
        <item>2005</item>
        <item>10</item>
    </string-array>
    <string-array name='AT'>
        <item>2005</item>
        <item>10</item>
   </string-array>
    <string-array name='AU'>
        <item>2005</item>
        <item>11</item>
    </string-array>
</code>
Then, based in the user input on EditText checks the letters and finds on the Strings.xml
The problem is if I do
<code>when (x) {
    "AA" -> print("x == 1")
    "AB" -> print("x == 2")
    .....
}
</code>
will result in 529 lines of code just to check the the letters on the license plate...
My question is if there is an better and tidier way of doing this, like this:
<code> val stringArray = resources.getStringArray(R.array."EditText.getText()")
</code>
Sounds complicated... and I'm an beginner in Kotlin
Comment: Store those string array in JSON format. It will be more easy to parse and lightweight.
Comment: Do the values have to be stored as resources?
Comment: Tried several times but JSON is way too much complicated to implement... Just used a when case to an array.
Answer: String array resources are constants. You can't call R.array.somevariable. You should use XML parser instead.


Is there a way to compare two list and check for the value that are the same

Question: I am create a social media type app I want to create a follower and following list like Instagram, I want when I go on some else profile and click on their followers or following list it shows a list of all the users that is following that person and if I am also following someone in the list it give me the option to unfollow that person and if I am not following the same person it gives me the option to follow them.
The code below is what I was to check if I am following any one in profile user followers list,  What I have done is I query the data from firestore of all the user id the I am following and the same for user profile that is currently open and store the list in two separate variable and what it should do is check both list and if a user Id is in both list it means I am also following that user and should show that I am following that user and give the option to unfollow but what happens is instead of showing that I am only following the user who's id is in both list it show that I am following every one.
checkfollowers(BuildContext context, String? id) async {
    final auth = Provider.of<AuthState>(context, listen: false);
    List<String> followingList = [];
    List<String> myFollowingList = [];

    try {
      final QuerySnapshot following = await _firestore
          .collection('following')
          .doc(auth.getCurrentUser.uid)
          .collection('userFollowing')
          .get();

      QuerySnapshot userfollowing = await _firestore
          .collection('followers')
          .doc(id)
          .collection('userFollowers')
          .get();

      following.docs.forEach((element) {
        myFollowingList.add(element.id);
      });
      userfollowing.docs.forEach((element) {
        followingList.add(element.id);
      });

      // followingList.where((item) => myFollowingList.contains(item));

      check(value) => myFollowingList.contains(value);
      isFollowedByMe = followingList.any(check);
      notifyListeners();

      print(followingList);
      print(myFollowingList);
    } catch (err) {
      print(err.toString() + 'this error is coming from profileState');
    }
  }

below code is how I build the follower/following list
 final FirebaseFirestore _firestore = FirebaseFirestore.instance;
  UserModel? users;

  @override
  void initState() {
    final profileState = Provider.of<ProfileState>(context, listen: false);
    profileState.checkfollowers(context, widget.proFileId);
    super.initState();
  }

  userComponent(UserModel? model, BuildContext context) {
    final profileState = Provider.of<ProfileState>(context);
    final auth = Provider.of<AuthState>(context);

    return Container(
      margin: EdgeInsets.symmetric(horizontal: 10),
      padding: EdgeInsets.only(top: 10, bottom: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) =>
                      ProfilePage(profileId: '${model?.userId}'),
                ),
              );
            },
            child: Row(
              children: [
                Container(
                  width: 60,
                  height: 60,
                  child: CircleAvatar(
                    radius: 50,
                    backgroundImage: NetworkImage('${model?.profilePic}'),
                  ),
                ),
                SizedBox(width: 10),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text('${model?.userName}',
                        style: TextStyle(
                            color: Colors.black, fontWeight: FontWeight.w500)),
                    SizedBox(
                      height: 5,
                    ),
                    Text(
                      '${model?.displayName}',
                      style: TextStyle(
                        color: Colors.grey[500],
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
          auth.getCurrentUser.uid == model?.userId
              ? Container()
              : GestureDetector(
                  onTap: () {
                    if (auth.getCurrentUser.uid == model?.userId) {
                      print('you can not follow your self');
                    } else if (profileState.isFollowedByMe == true) {
                      profileState.setIsFollowedByMeToFalse();
                      profileState.handleUnFollow(context, model?.userId);
                    } else if (profileState.isFollowedByMe == false) {
                      profileState.setIsFollowedByMeToTrue();
                      profileState.handleFollow(context, model?.userId);
                    }
                  },
                  child: AnimatedContainer(
                    height: 35,
                    width: 110,
                    duration: Duration(milliseconds: 300),
                    decoration: BoxDecoration(
                      color: profileState.isFollowedByMe == true
                          ? AppColors.white
                          : AppColors.pinkaccent,
                      borderRadius: BorderRadius.circular(5),
                      border: Border.all(
                        color: Colors.grey.shade700,
                      ),
                    ),
                    child: Center(
                      child: Text(
                        profileState.isFollowedByMe == true
                            ? 'UnFollow'
                            : 'Follow',
                        style: TextStyle(
                            color: profileState.isFollowedByMe == true
                                ? Colors.black
                                : Colors.white),
                      ),
                    ),
                  ),
                )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.white,
        title: Container(
          height: 38,
          child: TextField(
            onChanged: (value) {},
            decoration: InputDecoration(
                filled: true,
                fillColor: Colors.white,
                contentPadding: EdgeInsets.all(0),
                prefixIcon: Icon(
                  Icons.search,
                  color: Colors.grey.shade500,
                ),
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(50),
                    borderSide: BorderSide.none),
                hintStyle: TextStyle(fontSize: 14, color: Colors.grey.shade500),
                hintText: "Search users"),
          ),
        ),
      ),
      body: StreamListWrapper(
        stream: _firestore
            .collection('followers')
            .doc(widget.proFileId)
            .collection('userFollowers')
            .snapshots(),
        itemBuilder: (context, DocumentSnapshot snapshot) {
          var data = snapshot.data() as Map<String, dynamic>;
          users = UserModel.fromJson(data);
          return userComponent(users, context);
        },
        text: '${widget.user?.userName} as no Followers',
      ),
    );

Answer: It would be simpler (and more performant) to use the Set data structure rather than List. The intersection method on Set returns the items contained in both sets.
for example:
void main() {
  Set<String> followingSet = {'Jeff', 'Mike', 'Joe', 'Jess'};
  Set<String> myFollowingSet = {'Jess', 'Matt', 'Mike', 'Frank'};
  Set<String> usersInBothSets = followingSet.intersection(myFollowingSet);
  print(usersInBothSets); // {Mike, Jess}
}


Accessing files using folder name and ~folder name

Question: When I try to access a folder:
cd "c:\users\myname\google drive\apache-tomcat-7.0.25"

it works fine. However when I try:
cd C:\Users\myname\GOOGLE~1\apache-tomcat-7.0.25

it doesn't (folder not found). It seems that when I go to C:\Users\myname\GOOGLE~1\ and try to look for apache-tomcat-7.0.25, i'ts not there. Is there some way I could make windows re-index that folder or something so that it knows apache-tomcat-7.0.25 is under C:\Users\myname\GOOGLE~1 too?
Comment: Is 8.3 filename creation enabled on your system? http://support.microsoft.com/kb/121007
Comment: Is there another "Google something" and the one you want is actually Google~2 or Google~3, etc. ?
Answer: I did the following steps

I had another folder called Google Drive 2 within C:\Users\myname\ . I deleted that (didnt need it).
I used dir /x /a to get short names for all folders (even hidden)
I found that the short name was Google~2, and used it in my CATALINA_HOME. It works now.


How to open add contact page on android platform in flutter?

Question: I'm making an app using flutter that uses the contacts(using contact_services plugin) from user's phone. Thus it needs as well to give the ability to add a new contact using the system default add contact page. However there isn't such functionality in contacts_services. What is the way using plugin or existing contact_services plugin to open the add new contact page?
void openAddContacts() async{
if (Platform.isAndroid) {
  final AndroidIntent intent = AndroidIntent(
    action: 'ContactsContract.Intents.Insert.ACTION',
    category: 'ContactsContract.RawContacts.CONTENT_TYPE',
  );
  await intent.launch();
 }
}

Comment: I am searching for this feature as well and didnt find anything at all. Quite sad that it isnt easy to use such a simple and basic functionality.
Comment: have you got the answer, please reply
Answer: This may help you : https://github.com/differenz-system/Addressbook.Flutter
Or if you xant to dive deeper and want to understand the mechanism under the hood, I suggestion you to use Platform Channel to communicate with native codes. Here is an example proposed by Google for address-book : https://developers.google.com/protocol-buffers/docs/darttutorial
Comment: While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
Answer: if you use this plugin https://pub.dev/packages/contacts_service you can look full working example here https://github.com/lukasgit/flutter_contacts/blob/master/example/lib/main.dart
Add permissions
Android #
Add the following permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

iOS #
Set the NSContactsUsageDescription in your Info.plist file
<key>NSContactsUsageDescription</key>
<string>This app requires contacts access to function properly.</string>

And after you can use code from the example by link above.
Comment: Thanks for your suggestion. But the example in the link is an in-app add contact page. What I'm trying to achieve something exactly like what whatsapp does when you tap on "New Contact" button in select contact
Comment: Sorry, I didn't fully understand your question. I made small research - you are right, right now there is no easy way to add "New Contact" with Native functions.
It is possible to do, the best way is to make a new package and implement this functionality for Android and iOS.
Answer: I found the solution. Basically you have to invoke the method from MainActivity.kt and we will use Intent to open the default contact app, just like what WhatsApp does.
First, on your .dart file create the channel that will connect to the MainActivity.kt, like this :
static const openCreateContact = MethodChannel('com.example.yourApp/openCreateContact');
Also create a function that will invoke the method that we will later implement in MainActivity.kt, you can then call it on onPressed():
 onPressed: () async {
 String phone= "+212 666 000 111";
 String name = "John Doe";
await openCreateContactMethod(name , phone);
},

Future openCreateContactMethod(name, phone) async {
await openCreateContact.invokeMethod('openCreateContact', {
  'name': name,
  'phone': phone,
});

}
Then, head towards MainActivity.kt, it will look something like this:
package com.example.yourApp

import android.content.Intent
import android.provider.ContactsContract
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
private val channel = "com.example.yourApp/openCreateContact";

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger,channel).setMethodCallHandler{
            call, result ->

            if (call.method == "openCreateContact") {
                val name = call.argument<String>("name")
                val phone = call.argument<String>("phone")
                openCreateContact(name!!, phone!!)
                result.success(true)
            } else {
                result.notImplemented()
            }
        }

    }

    fun openCreateContact(name: String, phone: String) {
        // Create a new Intent to open the Contacts app with a pre-filled contact form
        val intent = Intent(Intent.ACTION_INSERT)
        intent.type = ContactsContract.Contacts.CONTENT_TYPE

        // Set the contact fields using the data provided by the Flutter app
        intent.putExtra(ContactsContract.Intents.Insert.NAME, name)
        intent.putExtra(ContactsContract.Intents.Insert.PHONE, phone)

        // Launch the Contacts app with the pre-filled contact form
        startActivity(intent)
    }
}


Adding a link with nid in Global: results summary in Views

Question: I have a Views block listing a certain amount of nodes, and I've added a header field: Global: Result summary.
I am on the page: http://myexample.com/node/145 for example and with the header field, I want to link to: http://myexample.com/node/145/events. I have put the following code in the field:
(@total)
But this omits the nid and goes directly to:
http://myexample.com/node/events.
Does anyone know how to pass a URL argument for that field? I would ideally want to use arg(1).
Answer: Because the header uses the default Drupal PHP interpreter, Views variables aren't available there like they are in the arg php code. You can get them by doing something like this:
<?php
global $current_view;
print $current_view->args[0];
?>

Update: If you want to use PHP in views header, you can go to configure header, select "Global: Text area" and have to change text format to PHP code; you have to first install the PHP filter module.
Also if you use Global: Text area with PHP filter enabled this should work:
@total

Answer: You don't get to add [nid] there.However you can use wildcard %.In your field of Global:Result Summary write like this
@total

Hope it works for you.
Comment: Sorry, but this does not work. It sends me to node/%/events...


How is a Quarterback's passer rating calculated in the NFL?

Question: In the NFL, all of the stats like YPG, and average fantasy points are calculated somehow.
How is a passer's rating calculated?
Does it have something to do with the average yards gained per completion, or other countless QB statistics?
What is the formula that is used to calculate the passer rating?
Answer: I'm not aware how fantasy points factor into a QB's passer rating.
That said, there are four separate calculations made with respect to passing values:

((completions / attempts) - .3) * 5
((yards / attempts) - 3) * .25
(touchdowns / attempts) * 20
2.375 - ((interceptions / attempts) * 25)

For each one, set the result to 2.375 if above that value, or set it to zero if negative.
Take these four calculations, divide the sum by six, and multiply by 100. And voila, the passer rating.
Source


How to solve $T(n)=4T(n/4)+n^2$ by recursion tree and master theroem?

Question: My solution is different between master thereom and recursion tree... How to solve it?

Recursion Tree

In the problem, when n=1, T(n)=c (constant). So In recursion tree, I found pattern.
$4^0\frac{n^2}{4^0}, 4^1\frac{n^2}{4^1}, 4^2\frac{n^2}{4^2}...$
I got a height by this way.
$\frac{n^2}{4^x}=c$ , so, $x=\log_4 {n^2c^{-1}}=log_4 n^2 -log_4c$
From above height $x$,
$T(n)=\sum_{i=0}^{log_4 n^2}n^2=n^2(log_4 n^2 + 1)=n^2log_4n^2+n^2=2n^2log_4n+n^2=\theta(n^2log_4 n)$

Master Theroem

a=4, b=4
$n^2=\Omega(n^{{log_4 4}+\epsilon})$ it is correct when $\epsilon=1$, $4\frac{n^2}{4^2}\le cn^2$ it is correct when $c\ge\frac{1}{4}$
$\therefore T(n)=\theta(f(n)=\theta(n^2)$

Why I get a different answer?
Comment: What are the two solutions?  How do you get yours?
Comment: I edited now. plz see
Comment: Re-posted on SO: https://stackoverflow.com/q/49548375/781723.  Please [do not re-post your question on multiple sites](https://meta.stackexchange.com/q/64068). Each community should have an honest shot at answering without anybody's time being wasted.
Answer: You missed power in the denominator.
$$T(n) = 4^0 \frac{n^2}{(4^0)^2}+4^1 \frac{n^2}{(4^1)^2}+4^2 \frac{n^2}{(4^2)^2}+\dots+4^{\log_{4}{n}} \frac{n^2}{{(4^{\log_{4}{n}})}^2}$$
So we have:
$$T(n) = n^2*(\sum_{i=0}^{\log_{4}{n}}{\frac{1}{4^i}})=\Theta(n^2)$$


Limit of $\frac{\log2^{x}\log\left(\log2^{x}\right)\log\left(\log\left(\log2^{x}\right)\right)^{p}}{\left(\log x\right)^{p}}$

Question: How to determine the following limit:
$$\lim_{x \to +\infty}\frac{\log2^{x}\log\left(\log2^{x}\right)\log\left(\log\left(\log2^{x}\right)\right)^{p}}{\left(\log x\right)^{p}}$$
Trying L'Hôpital's rule takes much time and I'm not even sure if we need use that just for once, so what is really the best way to determining the value of the limit?

I tried Wolframalpha ,but that was not helpful.
Answer: The numerator can be rewritten as
$$x\cdot p\cdot\log 2 \cdot (\log x + \log 2) \cdot \log(\log x + \log 2)$$
For $x > e^3$ we have that the numerator is greater than $ x \cdot (p\log 2)$
Thus the limit
$$L > \lim_{x\to\infty} p\log 2\cdot\frac{x}{(\log x)^p} \to \infty$$
since exponentials dominate any polynomial growth.


If an auto-regressive time series model is non-linear, does it still require stationarity?

Question: Thinking about using recurrent neural networks for time series forecasting.
They basically implement a sort of generalized non-linear auto-regression, compared to ARMA and ARIMA models which use linear auto-regression.
If we are performing non-linear auto-regression, is it still necessary for the time series to be stationary and would we need to perform differencing the way we do in ARIMA models?
Or does the non-linear character of the model give it the ability to handle non stationary time series?

To put the question another way: Is the stationarity requirement (in the mean and variance) for ARMA and ARIMA models due to the fact that these models are linear, or is it because of something else?
Comment: Can you give an example of nonlinear ARIMA you think of?
Comment: @Aksakal I'm not thinking of a "non linear ARIMA" but more of an "alternative to ARIMA" that is non linear - for example Amazon's DeepAR autoregressive neural networks.
Answer: If the purpose of your model is prediction and forecasting, then the short answer is YES, but the stationarity doesn't need to be on levels.
I'll explain. If you boil down forecasting to its most basic form, it's going to be extraction of the invariant. Consider this: you cannot forecast what's changing. If I tell you tomorrow is going to be different than today in every imaginable aspect, you will not be able to produce any kind of forecast.
It's only when you're able to extend something from today to tomorrow, you can produce any kind of a prediction. I'll give you a few examples.

You know that the distribution of the tomorrow's average temperature is going to be about the same as today. In this case, you can take today's temperature as your prediction for tomorrow, the naive forecast $\hat x_{t+1}= x_t$
You observe a car at mile 10 on a road driving at the rate of speed $v=60 $ mph. In a minute it's probably going to be around mile 11 or 9. If you know that it's driving toward mile 11, then it's going to be around mile 11. Given that its speed and direction are constant. Note, that the location is not stationary here, only the rate of speed is. In this regard it's analogous to a difference model like ARIMA(p,1,q) or a constant trend model like $x_t\sim v t$
Your neighbor is drunk every Friday. Is he going to be drunk next Friday? Yes, as long as he doesn't change his behavior
and so on

In every case of a reasonable forecast, we first extract something that is constant from the process, and extend it to future. Hence, my answer: yes, the time series need to be stationary if variance and mean are the invariants that you are going to extend into the future from history. Moreover, you want the relationships to regressors to be stable too.
Simply identify what is an invariant in your model, whether it's a mean level, a rate of change or something else. These things need to stay the same in future if you want your model to have any forecasting power.
Holt Winters Example
Holt Winters filter was mentioned in the comments. It's a popular choice for smoothing and forecasting certain kinds of seasonal series, and it can deal with nonstationary series. Particularly, it can handle series where the mean level grows linearly with time. In other words where the slope is stable. In my terminology the slope is one of the invariants that this approach extracts from the series. Let's see how it fails when the slope is unstable.
In this plot I'm showing the deterministic series with exponential growth and additive seasonality. In other words, the slope keeps getting steeper with time:

You can see how filter seems to fit the data very well. The fitted line is red. However, if you attempt to predict with this filter, it fails miserably. The true line is black, and the red if fitted with blue confidence bounds on the next plot:

The reason why it fails is easy to see by examining Holt Winters model equations. It extracts the slope from past, and extends to future. This works very well when the slope is stable, but when it is consistently growing the filter can't keep up, it's one step behind and the effect accumulates into an increasing forecast error.
R code:
t=1:150
a = 0.04
x=ts(exp(a*t)+sin(t/5)*sin(t/2),deltat = 1/12,start=0)

xt = window(x,0,99/12)
plot(xt)
(m <- HoltWinters(xt))
plot(m)
plot(fitted(m))

xp = window(x,8.33)
p <- predict(m, 50, prediction.interval = TRUE)
plot(m, p)
lines(xp,col="black")

In this example you may be able to improve filter performance by simply taking a log of series. When you take a logarithm of exponentially growing series, you make its slope stable again, and give this filter a chance. Here's example:

R code:
t=1:150
a = 0.1
x=ts(exp(a*t)+sin(t/5)*sin(t/2),deltat = 1/12,start=0)

xt = window(log(x),0,99/12)
plot(xt)
(m <- HoltWinters(xt))
plot(m)
plot(fitted(m))

p <- predict(m, 50, prediction.interval = TRUE)
plot(m, exp(p))

xp = window(x,8.33)
lines(xp,col="black")

Comment: _"If you boil down forecasting to its most basic form, it's going to be extraction of the invariant. Consider this: you cannot forecast what's changing. If I tell you tomorrow is going to be different than today in every imaginable aspect, you will not be able to produce any kind of forecast."_ - That's a nice way of describing statistical forecasting, and one I hadn't seen before (explicitly), +1.
Comment: "the time series need to be stationary if variance and mean are the invariants that you are going to extend into the future from history" - intuitively, this makes sense - but elsewhere in this forum somebody (I think it was Rob Hyndman) mentioned that some forecasting models, namely exponential smoothing, work best when the data is *not* stationary.
Comment: @Firebug, thanks, the concepts of invariants and symmetries are important in physics. For instance, mean and variance stationarity reminds of translational symmetry in time, which allows to forecast into future.
Comment: This deserves +10!
Comment: Found the thread https://stats.stackexchange.com/a/77076/89649
Comment: @Alex, it's not that it works best, but that it has advantages in seasonal and trendy series. It will certainly produce better results for stationary series than nonstationary, but for the former it's suboptimal in sense that other methods beat it
Comment: Thanks for an informative and very intuitive explanation. However, you overestimate the depth of my question: I was simply asking whether the "basic" stationarity that is required for ARIMA (i.e. stationary mean and variance) is a requirement for any autoregressive model or just linear auto-regressive models.
Comment: @Alex, I'm planning to use my answer in future as a reference to similar questions. For instance, even ARIMA(p,1,q) is for non-stationary series. Yet my answer still holds :)
Comment: I would also agree with @Aksakal, that if the primary goal is to predict, then the cardinal features of a stationary series have to hold.


TypeError: Cannot read property 'concat' of undefined - TypeScript?

Question: if (dataFindCategories && dataFindCategories.length !== 0) {
    let dataFindProducts: Product[];
    dataFindCategories.forEach((category) => {
        dataFindProducts.concat(this.productRepository.findByCategory(category.id));
    });
}

Quando este forEach é executado ele me retorna este erro:

TypeError: Cannot read property 'concat' of undefined

Para cada category ele ira fazer uma busca pelo ID que me retornara um Product[], eu preciso concatenar cada retorno em uma variável, no caso a dataFindProducts
Comment: isso retornar o que: `productRepository.findByCategory`
Answer: Você está declarando a variável, mas não está inicializando ela, logo o seu dataFindProducts é igual a undefined, experimente inicializar ele com um array vazio
let dataFindProducts: Product[] = [];

Comment: Obrigado era isso mesmo!
Answer: Ja que está utilizando o superset typescript utilize o tipo Array (que é o tipo generico) da seguinte forma, exemplo minimo:
class Produto {
    constructor(public id: number){
    }
}

class Category {
    constructor(public id: number){
    }
}

const dataFindCategories = Array<Category>();
dataFindCategories.push(new Category(1));
dataFindCategories.push(new Category(2));
dataFindCategories.push(new Category(3));

let dataFindProducts = Array<Produto>();
dataFindCategories.forEach((category: Category) => {
    dataFindProducts =
        dataFindProducts.concat(category.id);
});

console.log(dataFindProducts);

e preste atenção que ao utilizar .concat a última alteração deve ser retornada para a variável, porque .concat retorna outro array.
Um outra forma é com Spread Operator:
class Produto {
    constructor(public id: number){
    }
}

class Category {
    constructor(public id: number){
    }
}

const dataFindCategories = [];
dataFindCategories.push(new Category(1));
dataFindCategories.push(new Category(2));
dataFindCategories.push(new Category(3));
dataFindCategories.push(new Category(4));

let dataFindProducts = [];
dataFindCategories.forEach((category: Category) => {
    dataFindProducts = [... dataFindProducts, category.id]; // Spread
});
console.log(dataFindProducts);

Comment: Entendi!, obrigado!


How to find parameters of AR(1) model with Student-$t$ error terms?

Question: I am trying to find parameters of an AR(1) process with error terms $\epsilon\sim$ $t$ distribution.
With $\{X_1, X_2,...,X_t\}$ given, what I have done so far is to fit the linear regression
$$
X_t = \beta_0 + \beta_1 X_{t-1},
$$
and then calculate the residuals $\{r_1,r_2,...,r_{t-1}\}$. The model assumption is that $\{r_t\}$ follows a Student-$t$ distribution, however, the standard deviation of $\{r_t\}$  is smaller than 1 (say 0.3). Therefore it could be a scaled $t$ distribution. I was wondering if there is any method for me to find the scaling parameter and the degrees of freedom of the distribution of these residuals?
Also, is it OK to directly use least square method to find $\beta_0$ and $\beta_1$ first? I know they should still be unbiased but no longer the MLE. I was wondering if I could fit the AR(1) model directly with error terms follow a $t$ distribution?
Comment: @RichardHardy Thanks for your clarification! I found the 'arfima' function in 'rugarch' package, which is not designed for the purpose of fitting an AR process with non-Gaussian error, allows flexible choices of error distributions. I guess I will read their document later to see how it works, but now I have something that works.
Answer:
Also, is it OK to directly use least square method to find $\beta_0$ and $\beta_1$ first? I know they should still be unbiased but no longer the MLE.

In autoregressive models the OLS parameter estimates are biased (but still consistent).

I was wondering if I could fit the AR(1) model directly with error terms follow a t distribution?

You may use maximum likelihood estimation. Formulate the model likelihood for the scaled Student-$t$ case and find the parameter values that maximize it using optimization.
(This works fine in theory, but I think I have read somewhere that the estimates of the degrees of freedom can be quite imprecise in practice. I cannot recall the source, though.)
Comment: Thanks a lot for the answer. Yes I tried a lot of methods for estimating the degrees of freedom, and somehow they are either really unstable or make the estimation as 2.
Answer: You may want to try with vector generalized linear models (VGLMs) applied to time series in R, part of my PhD. My family function AR.studentt.ff(),
through the modelling function vglm(), estimates the three parameters (location, scale and degrees of freedom) of the
the errors distribution, assumed as shift--scaled Student-t, by MLE using Fisher scoring. This family function imposes an AR model on the data, as yours above, also estimated.
My R package, VGAMextra, will be realized soon via CRAN, with further choices. This is an extension of the package VGAM. See below for a very simple example.
Note, only central Student-ts are handled currently.
### Estimate the parameters of the errors distribution for an
## AR(1) model. Sample size = 50

set.seed(20180218)
nn <- 250
y  <- numeric(nn)
ncp   <- 0           # Non--centrality parameter
nu    <- 3.5         # Degrees of freedom.
theta <- 0.45        # AR coefficient
res <- numeric(250)  # Vector of residuals.

y[1] <- rt(1, df = nu, ncp = ncp)
for (ii in 2:nn) {
  res[ii] <- rt(1, df = nu, ncp = ncp)
  y[ii] <- theta * y[ii - 1] + res[ii]
}
# Remove warm up values.
y <- y[-c(1:200)]

### Fitting the model.
AR.stut.er.fit <- vglm(y ~ 1, AR.studentt.ff(order = 1), data = data.frame(y = y), trace = TRUE)

VGLM    linear loop  1 :  loglikelihood = -98.165573
VGLM    linear loop  2 :  loglikelihood = -97.436435
VGLM    linear loop  3 :  loglikelihood = -97.400721
VGLM    linear loop  4 :  loglikelihood = -97.396789
VGLM    linear loop  5 :  loglikelihood = -97.396396
VGLM    linear loop  6 :  loglikelihood = -97.396352
VGLM    linear loop  7 :  loglikelihood = -97.396347
VGLM    linear loop  8 :  loglikelihood = -97.396347

Estimated coefficients of the AR component:
(Intercept)         AR1
   -0.13496     0.55319

# Estimated parameters of the Student--t on the errors.
 Coef(AR.stut.er.fit)
 location    scale       df
 0.053767 1.324088 4.226470

Comment: Victor, the above is a very useful answer.    I could be wrong, but I think the VGAM/VGAMextra packages have been updated.   For example. the function AR.studentt.ff() needs to be ARMA.studentt.ff(), and order=1 in that same function needs to be order=c(1,0).


Does $\operatorname{rank} {A^T}A = \operatorname{rank} A$ holds when $A$ is matrix over finite field?

Question: It's true when $A$ is a matrix over $\mathbb{R}$, see Prove rank $A^TA$ = rank $A$ for any $A_{m \times n}$
Answer: How about $A=\pmatrix{1&1\cr1&1}$ over $\Bbb F_2$?
Comment: This generalizes to show that the statement doesn't hold for *any* finite field, or any field of prime characteristic for that matter: The $p \times p$ matrix $A$ with all entries $1$ has rank $1$ but $A^T A = 0$.
Comment: What if $A$ is $m \times n$ and $\operatorname{rank}{A}=n$ ? does it hold ?


Is there a way to study "how good" is the Newton's Raphson method applied to a function?

Question: My question is as simple as that.
When we're applying the fixed point algorithm, we can see if it's going to converge or diverge finding the derivative and checking if the absolute value of that function is less or greater than $1$ on a given interval, and we can estimate if at any given point if the convergence will be fast or slow.
Is there a way to study the "contractivity" of the Newton's Raphson method?
Thanks for your time.
Comment: Locally the situation is easy: provided $f'(x^*) \neq 0$ you have at least quadratic convergence; as I recall the coefficient is something like $\frac{f''(x^*)}{2 f'(x^*)}$. If $f'(x^*)=0$ then analogous estimates go through to give you linear convergence, with the rate depending on how many higher derivatives also vanish at $x^*$. In particular you have convergence like $2^{-n}$ when $f'(x^*)=0$ and $f''(x^*) \neq 0$. The questions of "how big is "local"?" and "what happens globally?" are much more complicated and problem-specific.
Comment: See [Kantorovich Theorem](https://en.wikipedia.org/wiki/Kantorovich_theorem?wprov=sfsi1)
Answer: As a fixed point iteration
$$
N(x)=x-\frac{f(x)}{f'(x)}
$$
has derivative
$$
N'(x)=\frac{f(x)f''(x)}{f'(x)^2}
$$
and the usual estimates apply. Since at the solution $f(x_*)=0$, you can get intervals around $x_*$ with arbitrarily small contraction constants, compatible with the quadratic convergence speed.


Ajuda problema muito simples (C)

Question:
Sou iniciante, alguém sabe me dizer se tem algo de errado? O software de correção automática só dá WRONG ANSWER, mas os casos de teste não quebraram aqui (C)
#include <stdio.h>
#include <math.h>

int main()
{
float r = 0, areacinza = 0;

scanf("%f", &r);

areacinza = 1.25*3.141592*pow(r, 2);

printf("%.4f\n", areacinza);

return 0;
}

Comment: Uma das possibilidades, caso esteja usando uma plataforma de testes, como [URI Online Judge](https://www.beecrowd.com.br/judge/pt/categories) é que ele esteja usando um número muito grande e o float acabe estourando, poderia tentar substituir o `float` pelo `double`
Comment: Seu cálculo está incorreto. A área cinza se dá `Área do Círculo Maior - Área do Círculo menor = acma - acme`. Como o círculo maior tem duas vezes o raio do menor, basta `acma - acme = pi * (2 * r)^2 - pi * r ^ 2 = 3 * pi * r ^ 2 =` Área cinza.
Answer: O cálculo da área cinza está incorreto
O correto seria:
AreaCirculoMaior - AreaCirculoMenor
O raio do círculo maior equivale a (3r)/2 e portanto a área do círculo maior é PI * ((3r)/2)² e a área do círculo menor é PI * r²
Assim o cálculo final fica:
'(PI * ((3r)/2)²) - (PI * r²)'
Comment: Como está escrito atualmente, sua resposta não é clara. Por favor, pode [edit] para adicionar mais detalhes, que ajudarão outras pessoas a entender como isso resolve a pergunta feita. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).
Answer: Assumi o valor do raio do circulo cinza como 2 vezes o raio do circulo branco.
Isso não fica bem claro no enunciado.
#include <stdio.h>
#include <stdlib.h>

#define pi 3.141592

main()
{
    float raio = 0,
          result = 0;

    for (;;)
    {
        printf ("Digite o raio da circunferencia menor: \n");
        printf (">>>");
        scanf  ("%f", &raio);

        raio *= 2;

        result = pi * (raio * raio);

        raio /= 2;

        result -= pi * (raio * raio);

        printf ("Area da circunferencia maior: %.4f\n", result);
    }

    system ("PAUSE");
    return 0;
}

Peço desculpas por eventuais erros
Comment: Sua resposta pode ser melhorada adicionando informações fundamentais. Basta [edit] para adicionar mais detalhes, como citações ou documentação para que outras pessoas possam confirmar que sua resposta está correta. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).

Ajuda problema muito simples (C)


How do you solve for the mean in a Normal Distribution?

Question: I am working on a problem and am a little bit stuck on how to solve it.
The problem: Find a Normal Distribution with SD 2.5 and 5% Quantile at -15.2.
What I have done so far:
$$X=\mu+2.5Z$$
$$.05=P(\mu+2.5Z\le-15.2)$$
How do we reduce this further? It looks like we have two unknowns.
I know that Q 95% = 1.645 based on the Z Table and that this would correspond to 5% being $-1.645$
Also, $\sigma^2=6.25$, so I know it needs to be something along the lines of $X\sim\mathcal N(x, 6.25)$. I am a little bit confused on the next step though.
Answer: Manipulate the expression inside $P()$:
$$\mu+2.5Z\le-15.2$$
$$Z\le\frac{-15.2-\mu}{2.5}$$
But we know that the critical $Z$-value that will give the 5th percentile is $-1.645$. Thus
$$-1.645=\frac{-15.2-\mu}{2.5}$$
$$\mu=-11.0875$$
Comment: Thank you this makes a lot more sense now!