Using a VAR model to predict stock prices

Question: I ran into an issue while trying to predict stock prices using a Vector Autoregression (VAR) model. After noticing that all the series are non-stationary (see example below):

I took first differences of all variables, making them stationary (this result was confirmed by an Augmented Dickey Fuller test):

However, when fitting a VAR(1) model, I noticed that forecasted values only capture the overall upward/downward trend of the stock price, without being able to predict whether it will go up or down.

On the other hand, if I arbitrarily crank up the number of lags and I fit a VAR(200) model, predictions are more accurate at least in predicting the movements of the stocks:

I have only provided one stock as reference, although the model contains 30 of them and their respective plots are fairly similar. Now, my question is: since I am quite sure that a VAR(200) (or higher) is not a reasonable choice of parametrization because of overfitting, what should I do to improve the forecast on my VAR(1) model?
One final note: If I run a lag order selection algorithm setting the maximum number of lags as 10 (I am using Python's statsmodels), I get the following output suggesting a zero-lag model (??):
VAR Order Selection (* highlights the minimums)
==================================================
       AIC         BIC         FPE         HQIC
--------------------------------------------------
0       18.30*      18.44*  8.846e+07*      18.35*
1        18.31       22.59   8.969e+07       19.94
2        18.48       26.91   1.069e+08       21.69
3        18.75       31.31   1.405e+08       23.52
4        18.99       35.69   1.818e+08       25.34
5        19.25       40.09   2.428e+08       27.17
6        19.59       44.58   3.594e+08       29.09
7        19.84       48.97   4.903e+08       30.91
8        20.21       53.48   7.732e+08       32.86
9        20.27       57.68   9.210e+08       34.49
10       20.52       62.06   1.352e+09       36.31

while if I set the maximum number of lags to 32 I get this output suggesting 32 as the optimal lag length:
  VAR Order Selection (* highlights the minimums)
==================================================
       AIC         BIC         FPE         HQIC
--------------------------------------------------
0        18.50      18.65*   1.084e+08       18.56
1        18.51       22.87   1.096e+08       20.17
2        18.69       27.27   1.321e+08       21.96
3        18.96       31.75   1.735e+08       23.82
4        19.20       36.20   2.252e+08       25.67
5        19.45       40.67   2.979e+08       27.52
6        19.79       45.23   4.424e+08       29.47
7        20.03       49.68   6.018e+08       31.32
8        20.37       54.24   9.262e+08       33.26
9        20.44       58.52   1.120e+09       34.93
10       20.68       62.97   1.648e+09       36.77
:          :           :         :             :
:          :           :         :             :
:          :           :         :             :
20       18.54       103.0   2.228e+10       50.67
21       17.87       106.5   3.225e+10       51.61
:          :           :         :             :
:          :           :         :             :
:          :           :         :             :
29      -2.060       120.3   2.226e+10       44.51
30      -8.543       118.0   1.276e+10       39.63
31      -19.91       110.9   6.047e+08       29.87
32     -36.84*       98.18  1.363e+07*      14.54*

If I try to input a higher number as maxlag the function does not converge and predictions for a  VAR(32) are wildly inaccurate. I am not sure about what to make of this behaviour. Could there be something in the structure of the series that makes the model completely unable to provide meaningful predictions?
Comment: You can't predict stock prices like this
Comment: Could you tell how you chose the values for the intercepts and coefficients in the VAR formula?
Comment: In the 1st panel of the 2nd set of plots, you can plainly see that it is not stationary as the variance changes over time. You shouldn't rely blindly on tests like ADF, especially when they are sensitive to assumptions (which are violated here). This might sound like a small detail, but it isn't: investing requires balancing risk and reward, and here you have disregarded risk (variance) and focused only on the reward (the point forecast), implicitly assuming that you could come up with a VAR model that would be "good enough" that the risk was zero. This is not a good way to invest money.
Answer:
Could there be something in the structure of the series that makes the model completely unable to provide meaningful predictions?

Yes, indeed. Due to the nature of stock markets, price prediction with a VAR model is hopeless for daily data. The best model for price differences is VAR(0). Your VAR(200) model is certainly overfitted and it should be worse than VAR(0) for out-of-sample forecasts by any reasonable metric.
Also note that the trajectory of the best prediction does not necessarily mimic the patterns seen in historical data. E.g. if your data is i.i.d. N(0,1), the optimal prediction under a symmetric loss function is 0. If you iterate it over time, you get a straight horizontal line -- nothing like the past data. Yet such a prediction is impossible to beat (under a symmetric loss function, that is). The case of stock price increments is qualitatively similar to that.
Comment: That is what I suspected. Is there any stock-related daily data that behaves differently (for instance volume or returns)? If not, is there any "classical" linear model that is able to capture these dynamics? Or should I just go with some nonlinear function like neural networks or random forests?
Comment: @RoyHogg, volume might have some patterns in it that are amenable to VAR analysis, though univariate AR might be more relevant. For stock prices, their changes or returns nothing will work. Neural networks and random forests will overfit just as much as a high-order VAR. This is the nature of the stock market. A qualitative argument is given in my last paragraph. As you may notice, the problem is not that VAR is inadequate. The problem is that in a stock market, nothing beats a straight horizontal line.


Update params in Ruby on Rails controller

Question: I have a controller in which I want to update the value of one of the parameters before the update so that the update is carried out in the same save to the database. Unfortunately, the following code does not set z in the database:
if @model.x == "YES" && @model.z.blank?
   model_params[:z] = Time.now
end
@model.update_attributes(model_params)

def model_params
  params.require(:model).permit(:x, :y, :z)
end

Answer: This should do it:
def update
  attributes = model_params.clone
  if @model.x == "YES" && @model.z.blank?
    attributes[:z] = Time.now
  end
  @model.update_attributes(attributes)
end

def model_params
  params.require(:model).permit(:x, :y, :z)
end

You code did not work on the first place because calling model_params calls the method, not the actual local variable. You need to create a clone of the returned Hash and use it for the update_attributes.
Comment: You got the first answer in, but you've missed a great opportunity to explain what's happening (you've since edited) or improve the code.
Comment: A safer method in Rails 5, would be to use `update!` as opposed to `update_attributes`. This will cause active record to check for model validations
Comment: `update_attributes` is an alias of `update` and both of them perform the validations (see https://apidock.com/rails/ActiveRecord/Persistence/update_attributes and https://apidock.com/rails/ActiveRecord/Persistence/update)


Hyperbolic area

Question:
Define Hyperbolic area of a subset $E$ of the unit disk $D$ to be $\displaystyle 4\int \int_E \frac{dx dy}{(1-|z|^2)^2}$. Show that the hyperbolic area is invariant under conformal self maps of $D$.

I am new to hyperbolic geometry. So I am confused with some concepts. This problem seems like a standard one but I couldn't find it in MSE. So if you find a duplicate please be kind enough not to downvote this and instead please just copy a link, then I will delete this problem.
I know that hyperbolic lengths are invariant under conformal self maps of $D$. So I tried a similar technique that has been used in the proof of that fact.
So if $w=f(z)$ then by Pick's lemma we have $\displaystyle \frac{|dw|}{1-|w|^2} =\frac{|dz|}{1-|z|^2}$.
By squaring both sides,  $\displaystyle \frac{|dw|^2}{(1-|w|^2)^2} =\frac{|dz|^2}{(1-|z|^2)^2}$. Hence, $\displaystyle \frac{dw\overline{dw}}{(1-|w|^2)^2} =\frac{dz\overline{dz}}{(1-|z|^2)^2}$
After that I was stuck since I don't know how to transform into $dxdy$ form. Can somebody please help me?
Answer: Take a Möbius transformation $T(z)=(az+\overline c)/(\overline c z+a)$ with $|a|^2-|c|^2=1$ (all orientation-preserving isometries are of this form) and compute. Namely, if $T(z)=u+iv$, then the Jacobian is
$$
\det\frac{\partial(u,v)}{\partial(x,y)}=\frac{\partial u}{\partial x}\frac{\partial v}{\partial y}-\frac{\partial u}{\partial y}\frac{\partial v}{\partial x}=\left(\frac{\partial u}{\partial x}\right)^2+\left(\frac{\partial v}{\partial x}\right)^2=|T'(z)|^2=\frac{1}{|\overline cz+a|^4}
$$
(using Cauchy-Riemann in the second identity). Other then this you need to show that
$$
|T(z)|^2=\frac{(az+\overline c)(\overline a \overline z+c)}{(\overline c z+a)( c \overline z+\overline a)}
$$
satisfies
$$
\frac{1}{(1-|T(z)|^2)^2}=\frac{|\overline cz+a|^4}{(1-|z|^2)^2}.
$$
The two powers cancel and give the desired result.

Hyperbolic area


Wpf show items over control

Question: I need to create control wich can show items over itself (just dont clip them if there is no space)

All textblocks added to grid. Grid and control has property ClipToBounds = false.
Why items clipped by control in my case?
<code>   <Style TargetType="{x:Type local:SomeControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SomeControl}">
                <!-- Root Grid-->
                <Grid x:Name="LayoutRoot"
                      Background="{TemplateBinding Background}">
                    <Grid x:Name="TicksHolder"
                          ClipToBounds="False"
                          Background="Transparent" >
                 <!-- Items Dynamically added here-->
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</code>
And as you can see from image only right border clips items. rectangles out of right border doesnot clipped!
I found how to reproduce with more simple code:
<code>      <Grid HorizontalAlignment="Left" Height="112" VerticalAlignment="Top" Width="52">
                <TextBlock TextWrapping="Wrap" Text="TextBlock" Width="78" Margin="37,0,0,0"/>
            </Grid>
</code>
And also i noticed that Grid clips content and Canvas is not
Comment: share a XAML, it will be easier to guess having a code
Answer: ClipToBounds property
http://msdn.microsoft.com/en-us/library/system.windows.uielement.cliptobounds.aspx
Comment: You need to check what is clipping the contents. Use Snoop.
Comment: http://snoopwpf.codeplex.com/ a tool to inspect wpf application visual tree at runtime
Answer: I Think that only one way out is to use Canvas instead of grid
Comment: If you have a large complex Grid you do not need to switch out the entire Grid with a canvas.  You can just wrap whatever element you need to clip inside of a canvas, and set the canvas to the correct place with grid.row and grid.column

Wpf show items over control


Row and Column Picture of a 3 x 3 Singular Matrix (Strang P43, 2.1.32)

Question:
Suppose $\mathbf{u}$ and $\mathbf{v}$ are the first two columns of a 3 by 3 matrix $A$. Which third columns
  $\mathbf{w}$ would make this matrix singular? Describe a typical column picture of $A\mathbf{x} = \mathbf{b}$ in that singular case, and a typical row picture (for a random $\mathbf{b}$).
$\boxed{\text{(P38)  Column picture:}}$ $A\mathbf{x} = \mathbf{b}$ asks for a combination of columns to produce $\mathbf{b}$.
$\boxed{\text{(P38)  Row picture:}}$ Each equation in $A\mathbf{x} = \mathbf{b}$ gives a line (n = 2) or a plane (n = 3) or a "hyperplane" (n > 3). They intersect at the solution or solutions, if any.
Answer: $A$ is singular when its third column $\mathbf{w}$ is a combination $c\mathbf{u} + d\mathbf{v}$ of the first columns.
  $\color{red}{\Large{[}}$ A typical column picture has $\mathbf{b}$ outside the plane of $\mathbf{u, v, w}. \color{red}{\Large{]}}$ $\color{#0070FF}{\Large{[}}$ A typical row picture has the intersection line of two planes parallel to the third plane. $\color{#0070FF}{\Large{]}}$ Then no solution.

"Singularity" hasn't been formally defined, but P27 identifies it with dependent columns. So the third column depends on the first two, ie: $\mathbf{w} =c\mathbf{u} + d\mathbf{v} \; \forall \; c, d \in \mathbb{R}$. Moreover, $\mathbf{w}$ is a plane on which the vectors $\mathbf{u, v}$ lie. I seize this paragraph.
$\large{1.}$ How and why is the red bracket true? $\qquad \large{2.}$ How and why is the blue bracket true?
Please mind that this question is from but Section 2.1 of IoLA, 4th ed, by Strang, so please keep answers rudimentary.
Answer: Your first question:
the red bracket is true because b is randomly chosen, it can be either on the plane or out of the plane of u, v, w. Furthermore, the possibility that it's out of the plane is bigger than that it's on the plane.


Demonstrate: $(s \Longrightarrow u) \Longrightarrow (p \wedge((-(u \wedge t) \wedge s ) \vee (q \wedge r))\Longrightarrow -t\vee q\vee r)$

Question: Demonstrate the following theorem:
 $(s \Longrightarrow u) \Longrightarrow \Bigg(p \wedge \bigg( \Big(-(u \wedge t) \wedge s \Big)  \vee (q \wedge r)\bigg)\Longrightarrow  -t\vee q\vee r\Bigg)$
I understand how to demonstrate theorems with "$\Leftrightarrow$". However, in this problem, there aren't any "$\Leftrightarrow$" but many "$\Longrightarrow$".
What's the result I should try to get to solve this problem? I don't even know which part of the expression that I should transform... I'd appreciate any hint or help.
Thank you!
Comment: What does it mean for an implication to be true?  How could you write your statement without using implications?
Comment: Generally speaking, say you want to show $A \Rightarrow B \Rightarrow C$, then you simply show $A \Rightarrow B$ then show $B \Rightarrow C$.
Comment: Ahh I see. $(s \Longrightarrow u) = (-s \vee u)$. I could do this to remove every implication. Or should I assume that s is true, so u is true as well.
Comment: @hobomath No, you assume $s \Rightarrow u$. You don't assume $s$, since you are not trying to *show* $s \Rightarrow ...$.
Comment: Alright, I think I work with that! Thanks for your help!
Answer: For this you can use Conditional Proofs:
To prove $\phi \Rightarrow \psi$ using a Conditional proof, you simply assume that $\phi$ is true, and then try to show that $\psi$ will then have to be true as well.
So in your case:
Assume $s \Rightarrow u$
Now show [what comes to the right of the $\Rightarrow$]
but since that is a $\Rightarrow$ as well, do another Conditional Proof, i.e. Also Assume that
$p \land ((\neg (u \land t) \land s) \lor (q \land r))$
And try to show
$\neg t \lor q \lor r$
And that should not be hard:
from the second assumption it follows that $(\neg (u \land t) \land s) \lor (q \land r)$, and thus you have either $\neg (u \land t) \land s$ or you have $q \land r$, and in either case you can establish your goal $\neg t \lor q \lor r$ (use the initial assumption for the first case)
So you don't really transform any statements here like you did algebraically when working with $\Leftrightarrow$'s. Rather, you infer things from other things ...  And thus you get $\Rightarrow$'s, since inference is (typically) a 'one-way street'.


Para que serve -EPERM em C?

Question: Para que serve o -EPERM, depois que compara se o ponteiro p é null
 if(p == NULL) return -EPERM ;

E tenho que colocar essas duas bibliotecas.
#include <errno.h>
#include <stddef.h>

Answer: Documentação dessa biblioteca de erros padrões.
Lá diz que este código representa um erro de operação não permitida, talvez por falta de privilégios para executar.
No caso específico, ou o null ocorre quando há algo errado que aconteceu quando tentou executar uma operação, ou o código foi usado para indicar que a aplicação de null aí está errado, o que me parece que seria um uso errado dele, já que esses códigos são para operações de IO.
Obviamente tem que acrescentar a biblioteca onde contém sua definição. Lá tem os nomes dos erros que é um pouco melhor que usar um número sem significado.
Pode estar se perguntando porque precisa usar o negativo. O código é positivo, mas em muitos casos um número positivo pode indicar que funcionou e os negativos indicam erro. Vários códigos se baseiam no sinal para determinar se a ação falhou.
Fora isso precisaria de mais contexto do código para explicar.
Leitura complementar.

Para que serve -EPERM em C?


Relationship between SD of MC test scores and test reliability?

Question: The scores on a 80 question multiple-choice test had a mean 70% correct and standard deviation (SD) of 10 (SD of % correct).  This SD is 2.5 times higher than the theoretical SD of 4 (based on N, p and q; SD = sqrt of Npq).
I take this higher value to mean that there is real variability in the ability of the test-takers.
The question is: Is there a simple conceptual or mathematical relationship between the SD of the scores and the reliability of the test?
Answer: In classical test theory, reliability of a test is not a property of a test, it's a property of a test in a population.
If the mean is higher, you've either changed the test, or you've changed the population. In which case the reliability is different.
In more modern test theory (item response theory) reliability is a property of a score - a person's ability is measured with a certain reliability, which depends on their answers. But we can also talk about the reliability of a test at a particular ability level.
A test where the mean score is 70/80 (0.875) would probably have greater ability to distinguish between low and average ability (because these people can have a score from 0-70) than between average and high ability (because these people can have a score from 70-80).
A plot that shows the relationship between reliability and ability is called an item information curve. These curves are not necessarily smooth - here are some examples: https://www.researchgate.net/figure/Item-Information-Curves-Left-Panel-Test-Information-Curve-Upper-Right-Panel-and_fig3_261513596
Comment: Extreme example: people answering randomly will have reliability of zero and 50% mean score. Other extreme example: everyone gets questions 1:10 right, and 11:20 wrong.
Comment: Interesting information, but have you answered the question which asked about the relationship between test reliability and SD of test score? BTW - I clarified the posting: the mean was 70% (not a raw score of 70 out of 80).
Comment: I didn't answer, because I don't think you can change the mean without changing the test. And if you change the test the reliability will change. You expect maximum reliability at 50% correct, but that doesn't mean you will get it.
Comment: Under what conditions would a test with a mean of 50% not have the highest reliability?
Comment: Interesting. Might you have examples that are more plausible in a real life testing setting?


Retornar determinado valores de um array

Question: Consumo uma Api cujo retorno é um array com 10 objetos dentro, quero mostrar na tela apenas os cinco primeiros do array, que são sempre os mais recentes na Api, como podem ver no código abaixo, no teste com o while consegui pegar 5, mas, somente os 5 últimos, tentei tbm dar um reverse() antes do
length, porém, bugou mais ainda a lista:

let api = [
  {
    "id": 44,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Filtro de data cartão virtual ",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 39,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Testes mais testes",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 27,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferi 100,00 reais",
    "institutionName": "0221 20** **** 4895",
  },
  {
    "id": 26,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transf. de 350 reais",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 25,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferência de Duzentão",
    "institutionName": "0221 30** **** 0114",
  },
  {
    "id": 11,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "fdfdfd",
    "institutionName": "0221 20** **** 0843",
  },
  {
    "id": 9,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferindo 400",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 8,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Teste 200",
    "institutionName": "0321 20** **** 1995",
  },
  {
    "id": 7,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "recarga 100",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 5,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Primeira Recarga",
    "institutionName": "0221 20** **** 0843",
  }
]

let i = 5;

while(i < api.length) {
  console.log(api[i]);
  i++;
}

Como posso pegar apenas os cinco primeiros resultados do array, e guardá-los se possível em um array, pois, estou utilizando em uma tabela com Angular e faço um ngFor.
Comment: tente mudar o let i = 5; pra let i = 0; e o while(i < api.length) para while(i < 5)
Comment: @Sumback, poste uma resposta, é bom para ficar registrado no site.
Answer: Use o método splice, passando por parâmetro a posição de inicio e fim para atribuir as 5 primeiras posições em um novo array;
Ai em seu ngFor utilize o array criado para iteração.

let api = [
  {
    "id": 44,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Filtro de data cartão virtual ",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 39,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Testes mais testes",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 27,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferi 100,00 reais",
    "institutionName": "0221 20** **** 4895",
  },
  {
    "id": 26,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transf. de 350 reais",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 25,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferência de Duzentão",
    "institutionName": "0221 30** **** 0114",
  },
  {
    "id": 11,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "fdfdfd",
    "institutionName": "0221 20** **** 0843",
  },
  {
    "id": 9,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferindo 400",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 8,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Teste 200",
    "institutionName": "0321 20** **** 1995",
  },
  {
    "id": 7,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "recarga 100",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 5,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Primeira Recarga",
    "institutionName": "0221 20** **** 0843",
  }
]

let arr = api.splice(0,5);
console.log(arr)
Answer: Podes usar o .slice(0, 5) que te dá exatamente os primeiros 5. Ou seja cria uma nova array só com uma parte da array inicial.
A API do .slice é .slice(<indice onde começar>, <indice onde acabar>).

let api = [{
    "id": 44,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Filtro de data cartão virtual ",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 39,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Testes mais testes",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 27,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferi 100,00 reais",
    "institutionName": "0221 20** **** 4895",
  },
  {
    "id": 26,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transf. de 350 reais",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 25,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferência de Duzentão",
    "institutionName": "0221 30** **** 0114",
  },
  {
    "id": 11,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "fdfdfd",
    "institutionName": "0221 20** **** 0843",
  },
  {
    "id": 9,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Transferindo 400",
    "institutionName": "0721 30** **** 7893",
  },
  {
    "id": 8,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Teste 200",
    "institutionName": "0321 20** **** 1995",
  },
  {
    "id": 7,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "recarga 100",
    "institutionName": "0021 30** **** 7404",
  },
  {
    "id": 5,
    "owner": null,
    "ownerName": null,
    "bankAccountId": "e8b2eb06-0df8-43bb-8012-ea42b63fb91f",
    "description": "Primeira Recarga",
    "institutionName": "0221 20** **** 0843",
  }
]

const primeiros5 = api.slice(0, 5);

console.log(primeiros5);


Primefaces inputText Ajax, retorna valor faltando o último caracter

Question: Estou com com um problema usando o ajax do primefaces, tentei também com o ajax do jsf.
Tenho o seguinte trecho de código:
<h:panelGroup>

 <h:outputText id="teste"value="Aqui o teste do que foi digitado: #{cadastroProdutoPapBean.produto.sku}" style="color:red"></h:outputText>
</h:panelGroup>

Testei usando:
p:ajax event="keyup"
p:ajax event="blur"

E nas duas situações ao trazer o valor digitado para o método ele fica faltando o último caracter
Exemplo do retorno:
Valor do sku digitado: 33221

Coloquei um outputText no trecho apresentado para verificar o que estava sendo impresso e retorna o valor correto.

Alguém já passou por isso, e teria uma solução pra esse problema?
Estou usando no meu bean @ViewScoped e busco o valor digitado com:
System.out.println("Valor do sku digitado: " + produto.getSku());

Comment: Coloca o código do MBean então, que está trazendo sem o último dígito.
Comment: Meu bean é bem grande, então coloquei as informações relevantes do bean.
Comment: Onde está inserido o system out que imprime o valor com um dígito a menos? Coloca o contexto em que ele está inserido.
Comment: Olá Giuliana, consegui resolver o problema, achava que era na página jsf mas na verdade era no método, bobeira que estava fazendo. Vou fazer uma resposta com a solução.
Comment: Por isso precisava do post do mbean, entende? Só com o que você expôs na tua pergunta não dava pra saber o problema. Coloca o código do MBean na pergunta.
Comment: Olá Giuliana, obrigada pelo interesse em ajudar, como achava não ser um problema no método e por ele conter muita informação achei que não havia necessidade, engano meu. Mas na resposta especifiquei o problema e a solução!
Answer: Consegui resolver, segue solução caso alguém passe pelo mesmo problema. Acontece que estava usando um método com evento e fazendo a chamada da seguinte forma:
public void verificar(AjaxBehaviorEvent event) {
    String valor = produto.getSku();
    System.out.println("Valor do sku digitado: " + valor);
    ...
}

Quando iniciei o processo de validação dos dados mudei meu método para:
 public void validaProduto(FacesContext context, UIComponent comp, Object value) {
    String valor = produto.getSku();
    System.out.println("Valor do sku digitado: " + valor);
    ...
 }

Aí que esta o problema, pois ao usar o validator do inputText eu tenho o valor do Object value e não de uma String com o valor do get declarado, pois para as funcionalidades do método Validate funcionar de forma correta, o valor retornado pelo método tem que ser o valor do Object value. Segue abaixo como ficou a parte que recebe o valor vindo da página:
public void validaProduto(FacesContext context, UIComponent comp, Object value) {
  String valor = (String) value;
  System.out.println("Valor do sku digitado: " + valor);
  ...
}


Probability to win a series of three games, with a given advantage for the home team

Question:
In a game with two equal teams, the home team wins any game with probability $p>\frac12$. In a best of three playoff series, a team with a home advantage has play at home, followed by a game away, followed by a home play if necessary. The series is over as soon as one team wins two games.
What is $P[T]$, the probability that the team with home advantage wins the series? Prove that $P[T]>p$ for all $p>\frac12$.

In the first sentence, I think there's a typo. I think it meant to say $p=\frac12$ (I don't know how you can get a number as your answer if its not equal to $\frac12$)
I drew a tree diagram as shown below, and calculated the probability that Team A wins.

$.5^2 + .5^3 + .5^3 = .5$
Comment: I think you are to calculate an answer _in terms of $P$_, not a numerical answer. Your tree is a good way to start.
Comment: It's often easier to imagine all possible sequences of three games (even though the winner may well be decided before the third game is played).  The ultimate winner in all cases is the one that wins at least two of the three.  Doing it this way lets you just use the properties of the binomial distribution.
Comment: There's no typo. You are using $P=\frac{1}{2}$ but $P>\frac{1}{2}$. This is a binomial distribution where some of the outcomes never happen (If A wins all it's games, the series is shorter).
Comment: if p>1/2, wouldn't P(T) be less than that?
Comment: @David You're actually right. The sense of the last inequality in the question should be inverted (see my answer for working).
Answer: This is not a typo. $p$ is a variable and you are supposed to include it in your answer.
There are three sequences of results leading to the home advantage team winning, as you found out: WW ($p(1-p)$), WLW ($p^3$) and LWW ($(1-p)^2p$). Thus
$$P[T]=p(1-p)+p(1-p)^2+p^3=(p-p^2)(2-p)+p^3=2p-3p^2+2p^3=p(2-3p+2p^2)$$
and if $p=\frac12+d$ with $d>0$
$$P[T]=p\left(2-3\left(\frac12+d\right)+2\left(\frac12+d\right)^2\right)$$
$$=p\left(2d^2-d+1\right)$$
$$=p\left(1+d(2d-1)\right)<p$$
where the last inequality is because $2d-1<0$ for $0<d<\frac12$. Indeed, the inequality is reversed compared to what is printed: conversely, $P[T]>p$ when $p<\frac12$.
Comment: So d is between 0 and 1/2, and P(T) is less than, not greater than p, because if you multiply any p by what you get in this equation - p(2d^2−d+1) - you get something smaller than p?
Comment: @David yes. And that is the _real_ typo in the question.
Answer: I'll add an answer to point out that you can approach the analysis somewhat differently by extending the series to three games even when the same team wins the first two.  This is permissible because the third game doesn't change the outcome in those cases; that is, after all, why they then don't usually play the third game.  We'll have them play it just to simplify things a bit.
Then the team with home advantage wins its two home games with probability $p^3$ (the home team wins all three games) plus $2p(1-p)^2$ (it splits its home games and wins away).  It wins all three games with probability $p^2(1-p)$, for a total of
$$
P(\text{home team wins series}) = 2p-3p^2+2p^3
$$
To see that this is actually less than $p$, we write
\begin{align}
2p-3p^2+2p^3-p & = p(2p^3-3p+1) \\
               & = p(2p-1)(p-1)
\end{align}
This shows that for the interval $0 \leq p \leq 1$, the left-hand side is positive when $0 < p < 1/2$, and negative when $1/2 < p < 1$.  The latter is our situation, and since $(2p-3p^2+2p^3)-p > 0$, it must be the case that $P(\text{home team wins the series}) = 2p-3p^2+2p^3 > p$.
Comment: Oops, totally misread the rules.  Thanks, I'll delete and edit! :-o
Comment: Thanks @littleO for pointing out my misinterpretation (I read too quickly, is what really happened).  Unfortunately, my original approach won't work, and what remains is not as simple as I'd hoped, but I'll leave the answer here just because it's a *little* different from Parcly Taxel's, and maybe the difference will be helpful.


Number of pairs

Question: I am trying to write a code that takes

m. a, a list of integers
n. b, an integer
and returns the number of pairs (m,n) with m,n in a such that |m-n|<=b.

So far, I've got this
def nearest_pairs(a, b):
    m= []
    n= int
    num_pairs = 0
    return num_pairs

def main():
    # The nearest pairs are (1,2), (2,1), (2,5) and (5,2)

    x = nearest_pairs( [1,2,5] , 3 )
    print( "nearest_pairs([1, 2, 5], 3) = " , nearest_pairs([1, 2, 5], 3) )

    # The nearest pairs are (1,2) and (2,1)
    y = nearest_pairs( [1, 2, 5] , 2 )
    print( "nearest_pairs([1, 2, 5], 2) = " , nearest_pairs([1, 2, 5], 2) )

if __name__ == '__main__':
    main()

The desired output should look like
>>> nearest_pairs([1,2,5],3) = 4

where 4 is the number of close pairs according to the restrictions. However, I get an error. Could anyone lead me to the right direction?
Comment: Let's  be real here - you don't really have anything. You wrote some boilerplate code and testcases.
Comment: `nearest_pairs(a, b)` is a silly way to write `return 0`
Comment: What are those 4 pairs in that example?
Comment: They are (1,2), (2,1), (2,5) and (5,2)
Comment: Try `itertools.combinations`
Answer: from itertools import permutations
def nearest_pairs(a, b):
    for m, n in permutations(a, 2):
        if abs(m - n) <= b:
            yield (m, n)

>>> list(nearest_pairs([1, 2, 5], 3))
[(1, 2), (2, 1), (2, 5), (5, 2)]
>>> list(nearest_pairs([1, 2, 5], 2))
[(1, 2), (2, 1)]

If you just want the count:
def nearest_pairs_count(a, b):
    c, l = 0, len(a)
    for i in range(l):
        for j in range(i + 1, l):
            if abs(a[i] - a[j]) <= b:
                c += 2
    return c

Comment: I don't see `O(N * log(N))` solution. Both solutions are `O(N**2)`. Note: if `a` contains duplicates then `c += 2` might be incorrect.
Comment: @orlp: do you mean a solution that uses a [spatial index](http://en.wikipedia.org/wiki/Spatial_database#Spatial_index) or something simpler?
Comment: @J.F.Sebastian Hint: sort the list first. Then for every `x` in the list, do a binary search for `b-x`. What does this tell you about the number of pairs smaller than or equal to `b`?
Comment: This is _O(n²)_ though, I know of at least an _O(n log n)_ algorithm.
Comment: @poke thank you, i see where you are going with this! However, this does not answer my question. In the output I should have a counted number of pairs like nearest_pairs([1,2,5],3) = 4
Comment: @Marie_ohoho If you can't figure out how to get the length of that list I suggest doing some studying (since this  question sounds like HW).
Comment: @orlp you probably want to search for both `(x-b)` and `(x+b)`
Comment: @orlp How about you submit this as an answer?
Comment: @poke Can't be bothered to flesh out the details.
Answer: Yours doesn't make sense. No idea what you're trying with len(a, b), but it's not even allowed, since len takes only one argument. And returning something just when you found the first counting pair? Here's a fix:
def close_pairs(l, d):
    ctr = 0
    for a,b in permutations(l, 2):
        if (a - b) <= d and (b - a) <= d:
            ctr += 1
    return ctr

And here's how I'd do it:
def close_pairs(l, d):
    return sum(abs(a-b) <= d for a, b in permutations(l, 2))

Number of pairs


Add custom views options

Question: I am trying to add some custom view options. I am not even sure if what I'm trying to do is possible.
Basically I want to put a custom option under the Other category.
It would look like:
My custom options: disabled
When the user clicks on "disabled", it should show a custom form where the user can set some settings for that view.
I have looked into this, but couldn't find anything that would let me add custom options here.
Comment: Hard to think of a Views option that isn't already there ;)
Comment: What settings are you intending to add?
Comment: what options are you wanting the user to be allowed to select?
Comment: I am creating a new module. In simple terms, the option could just be a true/false value. The user will be able to enable/disable a feature that I am creating. I will create a post_execute() hook I think, to check if the value is true/false, and then act on that value.
Answer: What you're actually looking to do is to implement a custom Views Display Extender plugin (https://api.drupal.org/api/views/views.api.php/group/views_plugins/7.x-3.x).
It's not particularly trivial and I got most of my information from this wonderful tutorial here: How to write a Views Plugin Display Extender (D7) by Bryan Ollendyke
Here's a basic outline:
-create a custom module (let's call it mymodule) and implement hook_views_api().
/**
 * Implements hook_views_api().
 */
function mymodule_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'mymodule'),
  );
}

-Create a mymodule.views.inc file (remembering to add the file to your mymodule.info file) and implement hook_views_plugins(). It should look something like this:
/**
* Implements hook_views_plugins().
 */
function mymodule_views_plugins() {
  $path = drupal_get_path('module', 'my_module');
  $plugins = array();

  $plugins['display_extender'] = array(
    'mymodule' => array(
      'title' => t('Some Setting'),
      'help' => t('A description of the setting.'),
      'path' => $path,
      'handler' => 'mymodule_views_plugin_display_extender',
    ),
  );

  return $plugins;
}

-Note the "handler" index in the above array. You'll need to implement that in a file called mymodule_views_plugin_display_extender.inc (again, remembering to add that in your mymodule.info file). The implementation should look something like this:
class mymodule_views_plugin_display_extender extends views_plugin_display_extender {
  /**
   * Provide a form to edit options for this plugin.
   */
  function options_definition_alter(&$options) {
    $options['my_setting'] = array(
      'default' => 0,
    );
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  function options_form(&$form, &$form_state) {
      parent::options_form($form, $form_state);

      if ($form_state['section'] === 'mymodule') {
        $form['my_setting'] = array(
          '#type' => 'checkbox',
          '#title' => t('Some Setting'),
          '#description' => t('A sample checkbox.'),
          '#default_value' => $this->display->get_option('my_setting'),
        );
      }
  }

  /**
   * Handle any special handling on the validate form.
   */
  function options_submit(&$form, &$form_state) {
    $this->display->set_option('my_setting', $form_state['values']['my_setting']);
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    $options['mymodule'] = array(
      'category' => 'other',
      'title' => t('Some Setting'),
      'value' => ($this->display->get_option('my_setting')) ? 'Yes' : 'No',
      'desc' => t('Set a setting.'),
    );
  }

}

After all that your mymodule.info file should look something like this:
name = My Module
description = Sample views display extender
core = 7.x
version = 7.x-1.0

dependencies[] = views

files[] = mymodule.views.inc
files[] = mymodule_views_plugin_display_extender.inc

If that all works out, you should see a setting show up under "Other" (you can change that using the 'category' in the options_summary implementation in mymodule_views_plugin_display_extender.inc.
After that, in your module, you can access the option from the view using something like:
$view->display_handler->get_option('my_setting');

For instance, if you were implementing `hook_views_post_execute().
/**
 * Implements hook_views_post_execute().
 */
function mymodule_views_post_execute(&$view) {
  $my_setting = $view->display_handler->get_option('my_setting');
}

Answer: The form ID of the view edit form is views_ui_edit_form
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_ui_edit_form')
  {
        $other = &$form['displays']['settings']['settings_content']['tab_content']['details']['columns']['third']['collapse']['other'];
        $other['custom']['#theme'] = 'views_ui_display_tab_setting';
        $other['custom']['#description'] = 'Custom';
        $other['custom']['#link'] = 'Custom';

  }

}

The form item for the OTHER section is
['displays']['settings']['settings_content']['tab_content']['details']['columns']['third']['collapse']['other']

so in the function this form element is taken into the $other variable as reference for ease of use.
This one simply adds a link to the node/1 you can edit it to do what you want. And the example does not take ajax into concern you can print out the $other variable to get an idea of what all details are given and how it all works.

Add custom views options


expected 'except' or 'finally' block

Question: I am new in python
I try to understand "try expect" a simple code
but I have this msg in my terminal "expected 'except' or 'finally' block"
what does that mean
thank you
try:

    result=10/0
    value=input("enter a number")
    print("value")

expect  ZeroDivisionError :
    print("you cannot devide by zero ")

print("success")

Comment: After `try` you should use `except` not `expect` there is difference between them
Comment: Is the PHP tag relevant?
Comment: If anyone wonders "Which PHP tag?": I removed 4 irrelevant tags. @MariamSaad It's kind of rude to add tags that have nothing to do with the question.
Answer: expect is not a keyword. except is.

expected 'except' or 'finally' block


Receber a altura errada do header

Question: Estou a tentar apanhar a altura do .ui-header para poder ter o meu programa dinâmico.
Acontece que a altura que me está a dar está incorrecta.
O código para apanhar a altura do .ui-header:
//Altura do header
        var lv_headerHeight =  $(".ui-header").outerHeight();
        alert("HEADER " + lv_headerHeight);

Esse alert está a returnar:

mas usando as ferramentas de dev do Chrome, indica que a altura do .ui-header é:
Como é que consigo apanhar este ultimo valor?
Obrigado pela atenção
Ou seja, tenho este header:

O alert está a dar-me um valor incorrecto da sua altura enquanto o Chrome dev tools dá-me o valor correcto. Mas não estou a conseguir apanha-lo
Comment: Acho que é necessário mais informações, existe alguma animação acontecendo no elemento? o resultado é compatível com a altura do elemneto no momento em que o alert é chamado? em um exemplo simples, funciona como o esperado http://jsfiddle.net/4o17h3xe/
Comment: Não existe nenhuma animação. Eu pensei em tentar passar o problema para o fiddle, mas estou a usar o jquery-ui e acho que no fiddle não tem disso.
Comment: nem um slidedown? digo isso porque pelo print a class do elemento cita um slide down que poderia estar acontecendo. edit, vi agora seu comentário atualizado, um exemplo de fiddle usando ui http://jsfiddle.net/xBB5x/2511/
Comment: Acredito que esse slidedown é imposto pela UI ou algo do genero, pois tive agora a percorrer o código e não tenho nada daquilo
Answer: Tente assim, irá pegar as coordenadas X/Y do ponto esquerdo/topo:
img = document.getElementById('box');
imagemTopRightX = img.offsetLeft;
imagemTopRightY = img.offsetTop;
alert(imagemTopRightX);
alert(imagemTopRightY);

Receber a altura errada do header


If $f$ is Lipschitz, $X_n$ converges in distribution and $d(X_n,Y_n)$ converges stochastically to $0$, then $\limsup_{n\to\infty}E[|f(X_n)-f(Y_n)|]=0$

Question: Let

$(E,d)$ be a separable metric space
$X,X_n,Y_n$ be random variables with values in $E$ such that $(X_n)_{n\in\mathbb{N}}$ converges in distribution to $X$ and $\left(d(X_n,Y_n)\right)_{n\in\mathbb{N}}$ converges stochastically to $0$
$f:E\to\mathbb{R}$ be bounded and Lipschitz continuous with Lipschitz constant $K$, i.e. $$\left|f(x)-f(y)\right|\le \min\left\{Kd(x,y),\left\|f\right\|_\infty\right\}$$

I've read (and don't understand) how $$\limsup_{n\to\infty}\operatorname{E}\left[\left|f(X_n)-f(Y_n)\right|\right]=0$$ from the dominated convergence theorem. It seems like an easy implicaiton, but I don't find an approach to show it.
Comment: You seem to neglect some other assumptions.  What properties does the sequence have?  For example, if $X_n=X$ and $Y_n=Y$ for all $n$, then why should limiting behavior converge to 0?
Comment: @Michael You're right, I'd missed some important properties (edited now).
Comment: Could anyone explain to me, why four people have voted to close this question?
Comment: @oxbadfood Well, probably because an (very important) assumption was missing. (I haven't downvoted your question.)
Answer: It follows from the Lipschitz-continuity and boundedness of $f$ that
$$\begin{align*} \mathbb{E}(|f(X_n)-f(Y_n)|) &\leq K \int_{d(X_n,Y_n) \leq R} d(X_n,Y_n) \, d\mathbb{P} + 2\|f\|_{\infty} \mathbb{P}(d(X_n,Y_n) >R) \\ &\leq K R + 2 \|f\|_{\infty} \mathbb{P}(d(X_n,Y_n) >R). \end{align*}$$
Letting $n \to \infty$, we get
$$\limsup_{n \to \infty} \mathbb{E}(|f(X_n)-f(Y_n)|)  \leq K R.$$
Letting $R \to 0$ finishes the proof.
Comment: I don't think that we need $R$ and I don't understand why we need to deal with $2\left\|f\right\|_\infty$ instead of $\left\|f\right\|_\infty$ itself. Furthermore, I'm wondering why the statement uses the limit superior instead of the usual limit. Since the integral of a nonnegative function is nonnegative, $$\limsup_{n\to\infty}\operatorname{E}\left[\left|\cdots\right|\right]=0$$ should imply the existence of the limit.
Comment: @oxbadfood A priori we do not know that $\limsup \mathbb{E}(\dots)=0$. *After* we have shown that this holds true, we get automatically $\lim \mathbb{E}(\dots)=0$. Regarding $2 \|f\|_{\infty}$: Well, by the triangle inequality: $$|f(X_n)-f(Y_n)| \leq |f(X_n)|+|f(Y_n)| \leq 2 \|f\|_{\infty} $$ ... or what estimate do you use? (In any case, the constant $2$ doesn't matter at all.) Regarding $R$: I need it in my proof.
Comment: You don't need the triangle inequality. Since $f$ is Lipschitz continuous and bounded, we've got $$\left|f(X_n)-f(Y_n)\right|=\underbrace{1_{A_n^C}\left|f(X_n)-f(Y_n)\right|}_{\le Kd(X_n,Y_n)}+\underbrace{1_{A_n}\left|f(X_n)-f(Y_n)\right|}_{\le\left\|f\right\|_\infty}$$ (where $A_n$ is defined in my answer). So, you don't need $R$.
Comment: @oxbadfood I've never claimed that there are not other ways to prove this. However, in **my** proof I need $R$ and $2\|f\|_{\infty}$.
Comment: Don't mind. I just thought I'd missed something.
Answer: Let $$A_n:=\left\{Kd\left(X_n,Y_n\right)>\left\|f\right\|_\infty\right\}$$
Since $X_n,Y_n$ maps to $E$ and $f$ is Lipschitz continuous on $E$, we've got $$\operatorname{E}\left[\left|f(X_n)-f(Y_n)\right|\right]\le k\operatorname{E}\left[\underbrace{1_{A_n^C}d\left(X_n,Y_n\right)}_{=:Z_n}\right]+\left\|f\right\|_\infty\operatorname{P}\left[A_n\right]$$ by the monotonicity and linearity of expectation. Now, $Z_n\le\left\|f\right\|_\infty<\infty$ is a random variable with values in $[0,\infty)$ which converges stochastically to $0$ (by assumption). Thus, the dominated convergence theorem yields $$\lim_{n\to\infty}\operatorname{E}\left[Z_n\right]=0$$ Due to the stochastic convergence we've got $$\lim_{n\to\infty}\operatorname{P}\left[A_n\right]=0$$ in addition, which completes the proof.


What happens when RDS freeable memory runs out?

Question: We have a rather large mysql instance on RDS and the freeable memory is starting to get low (<1GB) in the last 24 hours.
What will happen if we reach 0 freeable memory? Does that mean all memory available is used? does that mean the database will crash? Is that a disaster waiting to happen or will that memory simply be recycled?
Answer: Freeable memory is used by db engine for caching. Old data will be removed to get more room.
This is not important as cpu or iops, so you should not worry too much. But, more memory will allow your db engine to respond faster.


Can't stop looking at the metronome when I'm practising with it

Question: I can barely stay in time with the metronome when I'm trying to listen to it with my ears, but when I watch it with my eyes I can play in time fine if I'm focusing really hard (but even with focusing really hard with my ears it doesn't work for me).
Is looking at the metronome ticking a bad habit to get into?
I find it extremely hard to play in time with or without a metronome and I often am playing in a completely different rhythm to the metronome even when I'm trying to use one. It's just really hard for me and I don't know why and I really have to improve. Any other advice is welcome.
Edit: turns out I can't play in time with the metronome no matter what I do. It's really frustrating.
Comment: Best to start by learning to clap, or play a single note, on every tick.  Once that works, do the same exercise but two evenly-spaced notes per tick.  And so on.
Comment: For one thing, if you look at the metronome, you won't be looking at the sheet music, which means you won't be training yourself to associate metronome beats with note timings.
Comment: another way would be tapping your foot or bobbing your head (if possible). looking at a metronome prevents you from looking at the music, and in a band or live performance, there isnt one to look at. Turning up the volume might help a little bit.
Comment: I hate playing with a metronome so much that I never do it. It's not like you _have_ to. If it's going to make you not want to keep playing, then don't use one.
Comment: @AlbrechtHügli Only if you also stop breathing... respiratory sinus arrhythmia.
Comment: Maybe it's my years doing drumline, but I'm the exact opposite.  I can't internalize a beat from a blinking light (old school swinging metronomes are ok), but I can play along with a beep like a duck floats on water.
Comment: Don't feel alone - I have been working with a metronome for over 5 years and still I struggle with all my scales. You could experiment with different sounds of the metronome, and place it where you can't see it. Explore the options, in the long run it'll will be time well invested.
Answer: Ideally you want to feel the beat rather than see it, so yeah watching it is not a great habit. Some people would say the goal is to INTERNALISE the beat.
How long have you been practicing with a metronome? If it's less than 10,000 hours what you need to do is practice more ;)
Place the metronome behind you, practice, practice, practice, and remember that it's a process and a long term goal so be patient with yourself.
Comment: Learning to play with the metronome is a great foundational musical skill. It's also a horrible, infuriating exercise. As Jon says, 2 weeks is nothing. Patient practice is the key to success. Come back is 2 years and let us know how you get on, I believe in you ;)
Comment: 2 wks is nothing.
Comment: I've been trying with a metronome for 2 weeks now and I cannot play more than a few lines of music without completely losing the metronome
Comment: @Featherball why don't you try memorising the rhythm and playing that... is this bad musical habits guys?
Answer: Set a moderate tempo. Playing very fast ...and very slow tempos is hard.
Set the metronome behind you or behind something so you don't see it.

I find it extremely hard to play in time with or without a metronome

The issue doesn't really seem to be about looking at the metronome, but about counting and rhythm.
Count the beats out loud while playing. This will probably be the most effective way to start internalizing the beat.
Keep rhythms simple, gradually increase complexity. Something like the following will put accents on the strong beats and includes a few simple multiple/divisions of the beat by half...

You can also do counting practice by patting your hands on your lap. Do the rhythm above with two hands. One hand matches the beat which you count out loud. Switch the rhythms between hands. Try to make the hand switch without stopping the beat...

You can adapt that to piano with simple five finger or scale patterns in the two hands.
Depending on how much trouble you have the metronome may be a hindrance in the beginning. As you work on coordinating your timing your tempo may be uneven, but with time it will become even. During that transitional period the metronome will not give you a break! Wait until you get a pretty even tempo, then work with the metronome. Like with any other training short, frequent sessions are best. Try morning/evening sessions daily for a few weeks and then assess your progress.
Comment: +1 for a great answer but especially "**Count the beats out loud while playing**". This is the most important and most helpful possible technique.
Comment: "Count the beats out loud while playing" doesn't work for wind players. Got any suggestions? (Flute player here.) Thanks
Comment: @LouisB. I suppose the first practical thing is tap with your foot. When I do that I often try to move my foot a bit for the first beat. Like beat one is a bit to the left and the weak beats to the right. Of course, the two hand patting can be done by anyone.
Answer: I use a metronome to set the really slow pace when I'm working through a new exercise and I need to regulate the evenness of my finger movements until I begin to acquire the flow of the exercise. I utilize both the sound and the visual by placing the metronome just to the side of the sheet music where I can see it in the corner of my eye and still read the exercises. What I'm really doing here is a form of multi-tasking and I had to practice at it in order to get pretty good at it. I have never noticed any detrimental effects caused by watching the metronome swing as I practice.
One thing I've noticed about practicing with a metronome is that when I play with others who have not used a metronome or drum machine to learn and practice with, I have to let go of my practiced sense of rhythm and adapt myself to the undeveloped rhythm of the other players, where they speed up and slow down without noticing what they are doing.
Answer: A lot of folk find playing along to a metronome isn't easy. Especially one that just ticks. If it pings as well, it's easier to keep in time with.
I find a drum track - on a drum machine or off the computer - is a much better bet. But maybe that's because I play with drummers (and play drums!). That will give a more live feel to any rhythm, as well as give a far better sense of rhythm to play along to.
Don't set it at a too-fast pace for anything you play. And eventually, when you improve, use it in different ways - make it play on 2 and 4 instead of 1 and 3; make it play on the &s instead of the beat. There's all sorts of different ways you can regard the clicks. Double/halve the tempo. The beat goes on...
Comment: I agree that alternating tones and/or drum tracks make it much easier to keep in time than with a monotone click, probably because they add a musical quality that is easier to internalize and much more interesting to play along with.
Answer: If you are able to walk and co-ordinated enough to walk steadily, then you already have an internal metronome. I suggest you start simply by playing a song or a beat through headphones while you go for a walk. If you can keep in step, then you know you have the ability to keep time.
Warning: Because you will be concentrating, walk where there is no danger of being hit by traffic etc!
At home, try marching in the spot to various beats. Then march on the spot to the metronome.
If you can do all of that, then your problem is not keeping time. It is probably that your playing is at a level where you can't actually listen to the metronome because all your focus in on your fingers.
This is one reason that scales can be useful. Memorise a scale so that you are incapable of going wrong and then play the scale to the metronome.
But scales are boring!
Yes they are but it doesn't matter. If you have to practise actual tunes hundreds of times because it's too much for your current skills, the tune will become boring. You will never actually want to play it by the time you have learned it.
Conclusion
Start simple and win at every step. Be an expert at every step - even if it is just walking in rhythm. Then move to the next step and perfect that.
Good luck!
Answer: A lot of useful tips here, like putting the metronome behind you or tapping your foot, but I'd like to add a practice tip that might cost you no time at all, depending on your lifestyle and whether you're in 2020 lockdown :(
Whenever you're listening to music, say you're waiting, traveling, eating, walking, driving (I don't know your age) or whatever, tap the beat.  It doesn't have to be obvious and make you look crazy, just so long as you know.  Heck, if you're at a concert, just go for it! At first, tap the 4/4 (1 2 3 4).  Then maybe just the 1,3,4 (1 (2) 3 4).  Then maybe (1 2 3 and 4). If the music is right tap (3+3+2)/8 (so 1, (2) and (3) 4) or whatever fits.  Advanced exercises include tapping triplets, maybe 6 to a measure when the beat is 4/4.  It will be frustrating at first, but work up slowly, set yourself goals, and you'll quickly enjoy the Guitar-Hero-like challenge of beating a whole song.  If the song has a silent part but the beat continues solidly through it (probably because the drum actually is a computer) then so much the better.
By the way, if you want to play jazz, then eventually you'll want to tap your foot on 2 and 4, rather than 1 and 3.  But this can come later, once you've got the basic beat down.
Comment: I thought I was the only person who did this.  Tapping the beat to music is a good form of ear training.
Answer: Depending on what instrument you play, here's another tip:
First, without your instrument, make sure you are able to tap along with the metronome with your foot.
Then, whenever you play, you always keep said foot tapping (whether you are using the metronome or not, or playing with a drummer/band/orchestra, just always).
Now, at the very beginning (but only at the very beginning) this will make it a little harder to keep time, since you now have a foot to worry about too. But before you know it this foot will become your anchor, the very thing that keeps you in time, always. You will notice that whenever you play along with something, you will naturally start with tapping your foot, even before you play a single note.
Eventually, you will learn to "mentally" tap your foot, without even moving it. That's when you will have (as mentioned by others) internalized the beat.
Also, if you are still having trouble keeping time in general, it pays to tap your foot (discreetly) whenever you hear music, whether you are playing or not. And dance, if you're so inclined, dancing is an awesome way to get a feel for the beat.
Lastly, don't feel bad about not getting it right the first time, we all had to learn this. Some might have learned it so young that they forgot they ever did, but I only learned to keep time (properly) in my early twenties and specifically remember being very frustrated by it, just like you are now. Nowadays it's second nature to me, not because of talent, but simply because I practiced the above. Hang in there, it'll get better!
P.S. This is not specifically a beginner technique, take a look a their feet whenever you see musicians playing, you'll see what I mean.
Comment: @WayneConrad I've noticed that when starting out, a lot of musicians will instinctively tap the accents, not the the beat. Usually, it will take a little attention and a little practice to get it right, but the return on (time) investment is more than worth it imho.
Comment: Then there are foot tappers who can't keep the beat.  It's crazy to watch their foot match their out-of-time playing.  Training your foot first with the metronome sounds like a great way to avoid having a foot that can't keep the beat.
Answer: You're not a bad musician, you're a visual musician.
According to Neuro-linguistic Programming, there are three kinds of people: auditive, visual and kinesthetic (representational systems). This means every person has their main sense focused on sounds, images or physical sensations (touch, smell, pressure etc). If you work with your eyes, go for it. Also, there are Vibrating Metronomes that pulse instead of making TICK TOCK noises, which are great for drummers who cannot get deaf practicing with high volumes.
Answer: Some great answers here. One other suggestion I haven't seen mentioned is rather than counting numbers to stay in time, with or without a metronome, recite a rhythmic phrase instead. It can just be gibberish, or maybe something lyrical, but either way it should help you feel and internalize the timing better than mechanically saying/thinking "1 and 2 and 3 and 4 and…"
As an example, if you are accustomed to drum sounds keeping time, maybe start simple with a repeating "Boom-boom Bop-boom Boom-boom Bop-boom…" (1-and 2-and 3-and 4-and…)
Or mix it up more like "Boom-boom Bop-boom Boom-bop Boom-bah…"
Use whatever words or sounds feel natural and don't take up excessive mental bandwidth to remember. Move the hard and soft consonant endings around to accentuate beats where and how you like. Make it fun!


Foreach with a multidimensional array - Laravel Blade

Question: I am working on an air ticket reservation system project at laravel 8. When I call API I get a multidimensional array in return. Here is the array. I need to show 'ResultID', 'BaseFare','Tax' under the 'Fares' array
array:3 [▼
"SearchId" => "fca84241-dd9b-4cf2-bd6f-2f221dcc2b91"
"Results" => array:8 [▼
0 => array:14 [▼
  "ResultID" => "f0a54871-d728-424a-8e97-94e22308ac44"
  "IsRefundable" => true
  "Fares" => array:1 [▼
    0 => array:9 [▼
      "BaseFare" => 2775.0
      "Tax" => 725.0
      "Currency" => "BDT"
      "OtherCharges" => 0.0
      "Discount" => 0.0
      "AgentMarkUp" => 0.0
      "PaxType" => "Adult"
      "PassengerCount" => 1
      "ServiceFee" => 0.0
    ]
  ]
  "Discount" => 0.0
  "Validatingcarrier" => "BG"
  "LastTicketDate" => "2022-05-30T00:00:00"
  "segments" => array:1 [▶]
  "TotalFare" => 3500.0
  "TotalFareWithAgentMarkup" => 3500.0
  "Currency" => "BDT"
  "Availabilty" => 1
  "FareType" => "InstantTicketing"
  "isMiniRulesAvailable" => false
  "HoldAllowed" => false
]
1 => array:14 [▶]
2 => array:14 [▶]
3 => array:14 [▶]
4 => array:14 [▶]
5 => array:14 [▶]
6 => array:14 [▶]
7 => array:14 [▶]
]
"Error" => null
]

This is my controller function if it is useful in any case.
       $response = $client->post($url,
               ['form_params' => $travel_data]
       );

       $response = json_decode($response->getBody(), true);

       $data['flight']= $response;
       $data['app']= App_setting::find(1);
       return view('agent.flights.search_results',$data);

Below is my code block in blade.php but it gives me the error, I tried several methods but was failed to create the loop.
@foreach ($flight as $key => $teamArray)
         @foreach($teamArray as $team_Array)
               @php
                      echo "
";
                      print_r($teamArray['Fares'][0]['BaseFare']);
               @endphp

         @endforeach

   @endforeach

Comment: Please share the error
Comment: show the error plus I guess the foreach does not satisfy what you need to fetch
Answer: You are using the variable teamArray instead of team_Array in the inner foreach, but you can omit the second foreach.
To get the value of * SearchId* and the values of BaseFare and Tax:
$flight = [
    [
        "SearchId" => "fca84241-dd9b-4cf2-bd6f-2f221dcc2b91",
        "Results" => [
            0 => [
                "ResultID" => "f0a54871-d728-424a-8e97-94e22308ac44",
                "IsRefundable" => true,
                "Fares" => [
                    0 => [
                        "BaseFare" => 2775.0,
                        "Tax" => 725.0,
                        "Currency" => "BDT"
                    ]
                ],
                "Discount" => 0.0,
                "Validatingcarrier" => "BG"
            ]
        ]
    ]
];

foreach ($flight as $key => $teamArray) {
    echo $teamArray["SearchId"] . PHP_EOL;
    foreach($teamArray["Results"] as $result) {
        echo ($result["Fares"][0]["BaseFare"]) . PHP_EOL;
        echo ($result["Fares"][0]["Tax"]) . PHP_EOL;
    }
}

Output
fca84241-dd9b-4cf2-bd6f-2f221dcc2b91
2775
725

See a PHP demo
Comment: @ The fourth bird- its working thanks :)
Comment: ErrorException
Invalid argument supplied for foreach() (View: /home/softcombd/public_html/air_ticket/resources/views/agent/flights/search_results.blade.php)
Comment: @NoorMohammadBabu Which ones of the `foreach` is giving this error? Is `teamArray` an array?
Comment: 2nd foreach giving this error.   @foreach ($teamArray as $team_Array)
Comment: @NoorMohammadBabu Then you can `var_dump($teamArray);` and see what it gives you
Comment: @NoorMohammadBabu What if you omit that foreach and use `foreach ($flight as $key => $teamArray) {
    echo $teamArray["ResultID"] . PHP_EOL;
    echo ($teamArray["Fares"][0]["BaseFare"]) . PHP_EOL;
    echo ($teamArray["Fares"][0]["Tax"]) . PHP_EOL;
}`
Comment: How to convert  this array type Array
([0] => Array (  [ResultID] => 0e54aded-d86b-4099-a264-cbf28deacb07  [Fares] => Array
            ( [0] => Array ( [BaseFare] => 4175
                        [Tax] => 725
                        [Currency] => BDT
     --------------  to   ---------------$flight1 = [
                        [ "ResultID" => "0e54aded-d86b-4099-a264-cbf28deacb07", "Fares" => [
                                [ "BaseFare" => 4175, "Tax" => 725 ] ] ]
                    ];
Comment: 1st one is my array and 2nd one is your array. your array working fine. how to convert this
Comment: @NoorMohammadBabu Can you do a `var_export($flight)` of your source array and update the question with that one?
Comment: @ The fourth bird-  update source array please check
Comment: @NoorMohammadBabu Like this? https://3v4l.org/a04IR


Routing in Drupal 8 for basic pages

Question: I've just begun the transition process from D7 to D8.  I installed D8 and added a basic page w/ a menu link.  I then began to explore the new D8 routing system and expected to find a .yml file that contained information about where to find the added page but I don't see it. This is not a added module so it doesn't have a moduleName.routing.yml.
I'm looking for a conceptual description of why this does or doesn't follow the new .yml convention.
Comment: The routing system in drupal 8 (much like drupal 7) uses placeholders to describe dynamic routes. If you would look at node.routing.yml you would find the route that describes node pages.
Comment: @Eyal Probably not, it's an entity - those routes are defined elsewhere IIRC
Comment: Looks like they're in the [entity annotation itself](https://api.drupal.org/api/drupal/core!modules!node!src!Entity!Node.php/class/Node/8) @Eyal
Comment: Defining the links in the entity annotation does not create a routing object. Though it is possible to use a route subscriber and use the link templates.
Comment: @Eyal Any idea where they are in that case? They're not in node.routing.yml
Comment: @Clive see my answer below
Comment: Cheers @Eyal, must learn to read files all the way through...
Comment: Here is a very good in depth discussion on routing in Drupal - https://cipix.nl/understanding-drupal-8-part-3-routing
Answer: Node routes are declared using what is known as a route provider.
The node route provider class is \Drupal\node\Entity\NodeRouteProvider and it is declared in the annotation of the node class:
<code>/*
 * @ContentEntityType(
 *   id = "node",
 *   label = @Translation("Content"),
 *   bundle_label = @Translation("Content type"),
 *   handlers = {
 *     ...
 *     "route_provider" = {
 *       "html" = "Drupal\node\Entity\NodeRouteProvider",
 *     },
 *     ...
 */
class Node extends ContentEntityBase implements NodeInterface {
//  ...
}
</code>

Routing in Drupal 8 for basic pages


Gridview- 2 SQL sources, adding and editing

Question: Im trying to accomplish the following with a gridview control and two sql data sources:

Load SQL data from one db into gridview
Have a line to add data into a different SQL DB
(tricky part) If someone was to edit the data, have the gridview loaded with the original info from DB#1, then the add/edit line auto-populated with the added data from DB#2

I've been looking into this for ages but cant seem to find a proper solution. Hopefully my explanation makes sense. If its foolish, and theres a much better route to take I would love to hear some suggestions.
Thank you!
Answer: You cannot bind a control to two data sources. Gather the data first and populate an in-memory store with it - then bind to the in-memory store, such as a DataSet/DataTable.


Can an holomorphic function have a convergent sequence of points in its domain with only real values?

Question: Let $D \subset \mathbb{C}$ be a open subset of the complex plane
Let $f: D \to \mathbb{C}$ be a holomorphic function
Let $\{z_n\}_{n \in \mathbb{N}} \subset D$ be a sequence in $D$ such that $z_n \to s \in D$ and we have $f(z_n) \to f(s)=0$
My question is:
Is there a subsequence $\{z_{n_k}\}_{k \in \mathbb{N}}$ such that $\forall k \in \mathbb{N}: \Im(f(z_{n_k})) \neq 0$
Thanks.
Answer: Well... Not necessarily. Let $f(z)=z$ and $z_n=1/n$ for example.
Comment: Thanks for your answer @GiuseppeNegro
Comment: @MateyMath: you are welcome but it is really trivial. Probably you are not asking the right question.
Comment: you are right it was a trivial attempt to solve this https://math.stackexchange.com/questions/3142477/an-equation-with-gamma-euler-function-in-critical-strip


Inner Join Laravel - Erro where clause is ambiguous (23000)

Question: Estou aprendendo a utilizar o FramWork Laravel e me deparei com o seguinte erro as realizar um inner Join (lembrando que já tenho um banco de dados com informações por isso não estou utilizado migrations)
$colaborador = DB::table('colaboradores')
    ->join('beneficios','beneficios.colaborador_id','=','colaboradores.colaboradores_id')
    ->where('colaborador_id',$request->colaborador_id)
    ->get();

E gera o erro:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'colaborador_id' in where clause is ambiguous (23000)

mas se eu acessar o banco de dados e fazer o comando:
select * from `colaboradores` inner join `beneficios` ON beneficios.colaborador_id = colaboradores.colaborador_id

Funciona perfeitamente.
Answer: Seu Select funciona pois está sem cláusula where. Se você colocar um where colaborador_id = 1 na query que executa no banco, você vai ter o mesmo erro.
Isso acontece pois ambas as tabelas possuem colaborador_id então no seu where você rpecisa especificar qual colaborador_id você quer utilizar, o da colaboradores ou da beneficios.
Para fazer isso basta inserir o nomedatabela.campo no seu where
$colaborador = DB::table('colaboradores')
    ->join('beneficios','beneficios.colaborador_id','=','colaboradores.colaboradores_id')
    ->where('beneficios.colaborador_id',$request->colaborador_id)
    ->get();

Comment: Muito obrigado consegui arrumar aqui


Only need the first task, ignore the rest

Question: I want to call a task at a specified interval. And avoid calling a new task unless the last has already completed.
private async void OnTimerTick(object sender, object e)
{
    if (_criticalSection.IsEntered()) return; // only allow 1 at any given time, ignore the rest
    using (var section = await _criticalSection.EnterAsync())
    {
        await update();
    }
}

How do I achieve this? Any suggestions for a better pattern?
Comment: Your code is not threadsafe, it is a race.
Comment: @SriramSakthivel: you mean there's a race between the release in the continuation  of the using block and IsEntered? but if the SynchronizationContext is the same, how can there be a race?
Answer: A critical section (like a Window's mutex) is for mutual exclusion: only allowing a single thread into a code path.
But that's not what you are trying to do: you need something that will tell you if something is happening.
A better approach would be a Manual Reset Event: set it (also know as signalled) at the start of the task and reset at the end. Then you can check if it is signalled by waiting on it with a timeout of zero for a normal Window's event, or with the applicable member for other types of event.
As this appears to be all in a single process a good starting point is System.Threading.ManualRestEventSlim. Used something like:
// One off initialisation somewhere at class scope
private static ManualResetEventSlim taskRunning = new ManualResetEventSlim();
private static object taskLock = new Object();

// code called from the timer, do in a lock to avoid race conditions with two
// or more threads call this.
lock (taskLock) {
  if (!taskRunning.IsSet) {
    StartTheTask(); // assuming this does not return until task is running.
  }
}

// At the outermost scope of the code in the task:
try {
  Debug.Assert(!taskRunning.IsSet); // Paranoia is good when doing threading
  taskRunning.Set();

  // Task impementation

} finally {
  Debug.Assert(taskRunning.IsSet); // Paranoia is good when doing threading
  taskRunning.Reset();
}

Another approach would be to always start the task, but have it check the event, if set then immediately exit. This would still need the lock to avoid races between the IsSet and Set() calls across threads. This second approach keeps the checking code together at the cost of briefly having another task running (unless that is common I would likely take this approach for the code locality).
Comment: Nice and quick response, thanks. But what is the reasoning behind Win32's TryEnterCriticalSection(), and SemaphoreSlim's timeout parameter? Aren't they logically applicable?
Comment: @user3127135 As noted in the answer critical sections (like muxtexes) are the wrong kind of synchronisation objects. A semaphore could be used, but you don't need the count it provides: it would be more complicated.
Comment: And is there any difference between using ManualResetEventSlim and a thread-safe boolean flag?
Comment: @Ricard. Ah, I see. It can provide the means, but it's not what it should be used for. Both for readability, complexity and intention. OK, good :)


LEFT JOIN with db_select()

Question: I'm trying to do a left join but it isn't working.
I have 2 tables:
TABLE profile
- uid
- pid

TABLE field_data_field_naam
- entity_id
- field_naam_value

I want to select the field_naam_value where uid is equal to .. and pid equal to entity_id. My normal query looks like this:
SELECT fdfm.field_naam_value, pf.uid
FROM field_data_field_naam fdfm
LEFT JOIN profile pf
ON fdfm.entity_id=pf.pid
WHERE pf.uid = 4

When I execute this on my database it works. Then I have this:
$naam = db_select('field_data_field_naam', 'fdfm')
     ->fields('fdfm', array('field_naam_value'));
$naam->leftJoin('profile', 'pf', 'fdfm.entity_id = pf.pid');
$naam->condition('uid', 4)
     ->execute();

And when I do the following:
foreach ($naam as $record) {
    die;
}

nothing happens... (he doesn't kill the app). Also tried to add ->fetchAll() but no success.
Answer: Use the below code for fetching values. It will give you field_naam_value field data.
$naam = db_select('profile', 'pf');
$naam->join('field_data_field_naam', 'fd', 'pf.pid = fd.entity_id');
$all_names = $naam->fields('fd', ['field_naam_value'])
  ->condition('pf.uid', 4, '=')
  ->execute()
  ->fetchAll();
foreach ($all_names as $record) {
  die;
}

LEFT JOIN with db_select()


Is it valid to write a hypothesis using the result of THIS experiment to predict an original independent variable?

Question: There is a dataset/study that has two independent binary variables, and a continuous dependent variable.
Variable A    Variable B    Measurement C
1               1               32.4
0               1               29.1
1               0               15.8
1               1               25.9
...

The binary variables were manipulated by the researchers. This simple study was observing animal behavior, by varying two binary variables in their environment, and then taking a measurement (continuous / ratio).
1. A and B only occur during a time when this animal behavior (C) occurs, and thus can be measured.  The behavior does not occur when A and B are absent.
2. A and B always occur together, but they are independent.
3. The animal behavior being measured (C), can't affect either A or B.
Is it valid to take this dataset and use logistic regression to:

Independent variable is C; dependent variable is B.
Independent variables are C and A; dependent variable is B.

I can write a null hypothesis that says the odd ratio is 1, and use the data.
Mathematically, you can do this, but is the causal inference biased?
Comment: It's really unclear what you're asking, and what you wrote in the title doesn't correspond to what you wrote in the question text. What is your research question, and what is the alternate way you are thinking of it? What does `->` mean your title and text? Please make this question clearer and we will be able to help you better.
Comment: Okay.  Give me a minute to work on that.
Comment: @Noah - Updated.
Comment: If you construct a hypothesis from the collected data, and then test the hypothesis on the data (surprise! the data support the hypothesis!) that is clearly the wrong way forward. Why were the variables collected in the first place (what was that hypothesis)? Why isn't it being tested with the data.
Comment: @Michelle I think OP is using the word "result" to mean "outcome variable". They are asking whether it makes sense to regress experimental condition on the observed outcome. It's not about the sequence of hypothesis generation and data collection.
Comment: @Michelle - You are assuming A and or B predict C.  But assume they do.  A, B, and C all occur in nature.  So if you measure C, can it tell you about A and B? But how valid is that?  There are two issues: does it tell you something outside of the actual data used (the reverse test), and is it valid to test the original test another way, by looking at it in reverse, using C to tell you about B.
Comment: @Noah I see your point. The question is so vaguely worded.
Answer: You can do this, but it wouldn't tell you much. I could imagine you might be interested in predicting environmental conditions from animal behavior if it was cheap to observe the behavior and expensive to measure the environment (e.g., using a canary in a coal mine), in which case it might make sense to model the environmental condition using the animal behavior. You wouldn't know the baseline prevalence of the environmental conditions, however, so you wouldn't be able to predict its specific value from the measured behavior of animals in the wild, making the model close to useless.
In science, though, we are often interested in causal relationships, which are associations that have specific properties (temporal precedence, no confounding, etc.). The odds ratio for the relationship between the environmental condition and animal behavior is a measure of association that is free of confounding, but it doesn't represent the temporal precedence of the relationship. The animal's behavior does not cause changes in the environment (or if it does, that is not the relationship you are investigating), so the odds ratio has no causal interpretation and doesn't tell you anything about how the environment would change if you forced changes in an animal's behavior or how the animal's behavior would change if you forced changes in the environment (like you did in the experiment). So this odds ratio would tell you nothing of interest.
The causal parameter of interest is likely the difference in the means of the animal's behavior between the environmental conditions. This parameter has a causal interpretation because it can be identified as the typical change in the animal's behavior that occurs when the environment is intervened upon (i.e., changed by a force external to the animal). This parameter is useful for science because it helps explain animal behavior, which the odds ratio in the previous model does not.
Finally, the second regression, with A predicting B, makes no sense at all because A and B are independent if manipulated separately by the experimenter, so the coefficient on A tells you absolutely nothing. In fact, the coefficient will likely be spuriously nonzero due to collider bias and should not be interpreted (see Elwert & Winship, 2014, for an explanation of this).

One interesting tidbit is the finding that if two groups are normally distributed with the same variance, the parameters in the logistic regression model predicting group membership from the continuous variable can be directly computed from the means and variances, implying a specific mathematical connection between the difference in means and the odds ratio. This was described here in a very clean and clear derivation and is worth a look.
Comment: $A$ and $B$ are independent in your experiment and cause $C$. Conditioning on $C$ to estimate the effect of $A$ on $B$, which is what you do when you include $C$ in a regression of $B$ on $A$, as in model 2, is the *definition* of conditioning on a collider, which is exactly what the article is about.
Comment: A and B are independent of each other, they both occur in nature in either state (here, 0 and 1), and they can't be affected by the animal being observed.  I will read your references tomorrow morning. Thanks.  (I've updated the post with this information.)
Comment: Also, the animal can be observed (measuring C) independently of A and B being manipulated, and the natural state of A and B can be noted when taking the measurement.
Comment: ... and, the researcher expected A to moderate B.
Comment: I read a lot of that article (Elwert). It's very good. I'm not convinced it's relevant. Here, the environment will always consist of one of the four states used in the study. In Elwert's simple example, beauty and/or talent leads to acting success. Turning that around and saying acting success without beauty means there's talent is biased. Intuitively, there could be other explanations, such as charisma or charm - so the weight assigned to talent could be overestimated. -- That is not the case here. There are no other alternatives. I'm updating the description with this information.
Comment: @Noah I'm not sure that analysis should be black and white.  I think it's intended as a rule of thumb.  A generalization is not being made outside of A and B being present.
Answer: Great question. As A, B, C occur together or not at all, I don't think any of the bias categories are applicable.
Regarding logistic regression, if there is an interaction effect between A and B when predicting C, then the value of A would be useful in predicting B - when including C. Logistic regression relies on the linear separability of classes, so if there is no interaction effect, A won't be helpful in predicting B. (I'm not talking about any expansions beyond just using A, B, and C in a new logistic regression model. Whether there's a more effective modeling technique is a separate issue.)
Consider the following dataset, where there is an interaction effect (extreme) between A and B on C.  C and A predict B.
A  B  C

2  1  40
2  0  20
3  1  60
3  0  30
4  1  80
4  0  40
0  1  10
...  ...  ...

Comment: a+b is defining an interaction.  "if a=0 then rnorm(b)" , "if a=1 then rnorm(b+1)" . ---  In my example dataset in my proposed answer, "if a=0 then b*10, elseif b=0 then a*10" elseif b=1 then a*20" .
Comment: @psboonstra Your own reference defines your model as an interaction - "In statistics, an interaction ... describes a situation in which the effect of one causal variable on an outcome depends on the state of a second causal variable."  As Jason said,  if a=0 then c = rnorm(b); if a=1 then c = rnorm(b+1).  It's more than an effect, 'a' defines how 'b' will be used.
Comment: when you say "If there is no interaction effect, A won't be helpful, assuming A and B are independent (and they should be)." I don't agree. If you imagine a simple DAG with three nodes A, B, and C and two arrows starting at A and B, respectively, and both pointing to C, then A and B will be conditionally associated given C, even when (i) there is no marginal association between A and B and (i) and A and B do not have an interaction
Comment: @psboonstra The context is logistic regression, which works with classes that are linearly separable. If A isn't affecting B's contribution to C, and B isn't effecting A's contribution to C, how is A (or B) providing information about B (or A), given C? -- I didn't consider other techniques, such as neural networks, which could be capable of finding non-linearly separable associations between the three variables.  (I updated my answer to limit my statement to logistic regression.)
Comment: I believe my comment still applies. I have in mind something like the DAG on [page 8, equation (29) here](http://www.cs.columbia.edu/~blei/fogm/2016F/doc/graphical-models.pdf). Matching notation, I am thinking that X and Z in that DAG respectively correspond to A and B in this example, and Y corresponds to C. Conditioning on C may induce correlation between A and B.
Comment: The same idea is illustrated [in Example 17.11, p271 of Wasserman's All of Statisticis](https://link.springer.com/content/pdf/10.1007%2F978-0-387-21736-9_17.pdf)
Comment: @psboonstra  I'm not seeing something there there that can be picked up with logistic regression. Can you come up with a simple dataset demonstrating this? (With two binary columns, and a continuous column).
Comment: `n = 1e4`
`a <- rbinom(n, 1, 0.5)`
`b <- rbinom(n, 1, 0.5)`
`c <- rnorm(n, a + b)`
`#no marginal association between a and b:`
`summary(glm(a ~ b, family = "binomial"))`
`#but conditional association between a and b given c:`
`summary(glm(a ~ b + c, family = "binomial"))`
Comment: Stackexchange is telling me that I should 'avoid extended discussions in comments', so if we're still not on the same page, we should move this discussion to a chat. Fortunately we've identified the source of our misunderstanding, namely that we have different understandings of the definition of an interaction. See [Wikipedia's section on Interactions](https://en.wikipedia.org/wiki/Interaction_(statistics)). I'm not saying anything about whether your generating model has an interaction or not; however, my true generating model does *not* contain an interaction. Happy to talk more over chat.
Comment: I'm happy to talk more in a StackExchange chat I created: it's called Interactions and you can find it [here](https://chat.stackexchange.com/) I would first encourage you to scroll down to the Introduction section of the Wikipedia page I linked to, which contrasts an example of a model with two explanatory variables without interactions (which I am using) against a model with two explanatory variables with an interaction.  Hopefully that clears up our confusion.


Why I can still access a vector's element after taking ownership of it without using reference?

Question: fn main() {
    let number_list = vec![1, 2, 3, 4, 5];

    let n = number_list[0];
    let r = &number_list[0];

    println!("{} : {} : {} : {}", n, r, number_list[0], &number_list[0]);
}

The output is:
1 : 1 : 1 : 1

Another question is what is the difference between vector indexing with a reference and a non-reference except taking the reference?
Answer: You have a vector of integers (i32 to be specific), and i32 implements the Copy trait.
The index syntax returns a dereferenced value. Since the indexed type implements Copy, the compiler copies it implicitly.
You cannot take ownership of an item from a vector using the indexing syntax at all.

what is the difference between vector indexing with a reference and a non-reference except taking the reference

Without the &, the value is copied (but only because it implements Copy). With the &, you have a reference to the value inside the vector.


Is a linear functional which is positive on linearly generating set of projections positive?

Question: Let $A$ be a unital C$^*$ algebra, and suppose there is a set of projections $P \subset \mathcal{P}(A)$ whose linear span is dense in $A$. If $\varphi \in A^*$ has $\varphi(p) \ge 0$ for all $p \in P$, does it follow that $\varphi \ge 0$?
Note that this does hold if every element of $A$ can be approximated in norm by a linear combination of mutually orthogonal projections in $P$ (given any $x^*x \in A_+$, such an approximation for $x$ will lead to an approximation of $x^*x$ by a linear combination with positive coefficients), but is there any reason to believe it in general?
Answer: No.
Take $A=\mathbb C^2$, $\varphi(a,b)=2a-b$, and
$$
\mathcal P=\{(1,0),(1,1)\}.
$$
Then $\operatorname{span}\mathcal P=A$, and $\varphi(1,0)=2$, $\varphi(1,1)=1$. But $\varphi$ is not positive, since $\varphi(0,1)=-1$.
Comment: The etiquette here is to ask a new question. Users look down on edits to questions that invalidate existing answers.
Comment: Thank you for your answer! You are absolutely right. As you may have guessed, this isn't quite the question I intended to ask (I am interested in the case where $A$ is concretely represented and $P$ is a complemented lattice). But I am new to stack exchange, so I'm not sure how to proceed. Is the appropriate procedure to accept this answer and ask a new question, or should I edit this one? Thank you again for your help!
Comment: Ok thanks, that was my intuition. Thank you again!
Comment: No problem :) $ $
Comment: What if $A$ is simple ?
Comment: See [here](https://math.stackexchange.com/a/3112219/22857).


Harmonizing Melodies a 3rd or 4th Below

Question: When writing second parts for melodies, I've read that a good starting point can be to write something basically parallel either a 3rd above or a 3rd below.
In both major and minor keys, the 3rd below option seems to present the problem of it being likely that the harmony note will not be in the "implied" chord. So in the key of C major, if I harmonise the note C with the note A, then if the implied chord for the melody was C major, the A is not in the triad, so I'm kind of creating a Cmaj6 chord.
Likewise, in A minor, using F to harmonise the note A seems to create some kind of Amb6 chord.
What am I missing here please? Is it generally OK/common practice to use these 3rd below harmonies and simply hear them as "colour" against the implied triadic chords?
What about starting a 4th below instead - this would seem more logical as the 4th below gives the 5th of the implied chord. Is this a better/more common approach?
Any help much appreciated.
Comment: This is exactly why the rule of thumb states "a third below **or** above". Neither works for all notes in all melodies. You also need to exercise judgement from the get-go.
Answer:
What am I missing here please?

Often, if the chord is C, the melody will have E or G.  In such cases, the third below is also a chord tone.

in the key of C major, if I harmonise the note C with the note A, then if the implied chord for the melody was C major, the A is not in the triad, so I'm kind of creating a Cmaj6 chord

But "if the implied chord for the melody was C major" is a big if.  Not every occurrence of C in a C-major melody is harmonized with a C major chord.  It could easily be A minor, F major, D minor seventh, D dominant seventh, or even A flat major or something else more chromatic.
The third above is probably more common because the melody usually ends on the root of the tonic chord, but even then if the harmony has been a parallel third below, one can break the parallelism and go to the fourth or sixth below.  There's nothing saying that a parallel harmony has to be strictly parallel, and they often aren't.

What about starting a 4th below instead?

This could work, but parallel fourths give a very different feel from parallel thirds.  Try it and see whether you like it!  It may or may not be stylistically appropriate, depending on the style of music you're making.
Answer:
What am I missing here please?

You are able to figure out what the resulting chords are, no problem there. But you're supposed to listen to it and keep what you like. :) If you don't want to make the C chord a C6, then don't add an A vocal harmony, add something else.
There are basically two styles for creating backing vocal harmonies:

(A) Follow the steps of the melody along the scale, at a distance of a fixed interval. Change the interval if it's necessary for the chord you want to create, or keep a fixed interval if you like the way it sounds. Style A in its simplest mechanical form only needs to know the melody, a scale and a set of intervals.
(B) Do not follow the steps of the melody. Backing vocals sing chords like an organ chord or synth pad. Lead melody goes up and down, but backing vocals only change pitch when the backing chord changes. Style B needs information about backing chord changes.

Notation examples of the two. First, style A, lead melody in black, harmony notes in blue.

Then style B, harmony notes shown in green.

Style B seems to be used more in rock music, and style A more in pop/jazz. In style B, sometimes the lead melody notes can even overlap with the backing voices, and when the lead melody steps up and down, there can be close intervals like seconds etc. In traditional classical styles this would probably have been frowned upon, but I think it can be a nice effect and is really a matter of taste.
There's also a combination of the two: sing tones of the written backing chords only, but move to a higher or lower chord tone, if the melody steps on your current note.
If you look at vocal harmonizer effect devices, they are divided in these two categories. There are scale-based harmonizers where you set a scale and supply the lead vocal into an audio input. And then there are chord-based harmonizers with a lead vocal input and a backing chords input, where you connect e.g. a guitar that plays chords. Some harmonizers have hybrid modes where you set a default scale (by specifying a key), but you can override or augment it by playing chords.
Comment: @RobinAndrews I had A and B the wrong way around in that sentence, I fixed the mistake now. :)
Comment: This is very helpful, and I will reflect on what you have written. I didn't even know vocal harmonizers existed, so that's certainly something to check out. One thing you wrote confuses me though: you said style B only needs to know the melody and scale, but you also described style B as changing pitch when the backing chord changes. So are you saying that it doesn't matter what the chords are, as long as the backing vocals change pitch somehow at the time a chord change happens, or something else?
Answer: Either a 3rd below or a 3rd above. But don't stick slavishly to one of those, use whichever is appropriate.
Yes, if the melody starts on the tonic, a 3rd below might sound odd. Better to choose a 3rd above (or a 6th below, which is the same thing).
If the melody lingers on the dominant note (and the harmony at that point is V) better choose the 3rd above.   (But the harmony might be I, in which case... yes you've got it :-)
If you want a 'rule', we can go back to the old concept of 'avoid notes'.   An added 2nd or 6th doesn't hurt and can add colour.  But avoid harmony notes that are a semitone away from a chord note.   If the chord is C major, obviously you can use the chord notes C, E and G.   D and A are colourful but do little harm.   But B (a semitone away from C) and F (a semitone away from E) will clash.
But...  and it would be easy to go on.  The maj7 is hardly considered dissonant these days.  Other intervals can sound good.  Not Bach chorale-style good perhaps, but still good.  I transcribed some Everly Brothers recordings recently.  Solid, respectable uncontroversial harmonies, yes?   I was surprised to discover they sometimes harmonised with a string of consecutive 5ths!
However.  Yes, 3rds above and 3rds below are good.  But you probably won't get through a whole song without swapping between the two!
Comment: Thanks that's helpful. One of my use cases is recorders, where higher notes can dominate too much, so "rules" for harmonies below are of particular interest. Also, in Am, the 3rd below gives F, which is a minor 6th again the Am triad. This seems like it should be an "avoid," but I seem to remember it sounding OK. So "let your ears decide," I guess?
Comment: F under A is quite a clash if the chord is Am.   You could go Melodic Minor though.  F# is pretty!

Harmonizing Melodies a 3rd or 4th Below