Average of one over sum of complex exponentials

Question: I need to compute a time-average which is of the form
$$ \lim_\limits{T \to \infty} \frac{1}{T} \int_0^T \frac{\mathrm{d}t}{1+\sum_k c_k e^{i x_k t}} $$
where $x_k \in \mathbb{R}$, $c_k \in \mathbb{C}$ and the sum in the denominator is finite, $k=1,\ldots,K$.
I tried substituting $z = e^{it}$ and using the residue theorem but in the end I didn't get far. Is there some way of getting a closed form expression for the limit? Alternatively, I don't mind if I have to use numerical techniques, but preferably I'd do it on a limit where the convergence is more controlled.
Comment: Do we have any guarantees the sum in the denominator is always nonzero in the region of integration?
Comment: Adding the zero term in the sum with $c_0=1,x_0=0$ might make the argument easier
Comment: Actually that can be done in the case I'm looking at. I'll modify the question. The original integrand is $e^{i(x_k-x_{k'})t}/\sum_l|\sum_{k,k'} c_{k,k',l} e^{i(x_k-x_{k'})t}|^2$. One of the exponentials in the nominator is the same as another exponential in the denominator.
Answer: If the $c_k$ are small enough (say, $\sum_k |c_k| < 1$), then expanding the inverse in a Taylor series will work:
$$
\frac{1}{1+\sum_k c_k e^{ix_k t}}=1-\sum_kc_ke^{ix_k t}+\sum_{k,l}c_kc_le^{i(x_k+x_l)t}-\sum_{k,l,m}c_kc_lc_me^{i(x_k+x_l+x_m)t}+\ldots
$$
This can be regrouped by combining terms with the same number of copies of each $x_i$:
$$
=\sum_{n_1=0}^{\infty}\sum_{n_2=0}^{\infty}\cdots\sum_{n_K=0}^{\infty}(n_1+n_2+\ldots+n_K)!\exp\left(i(n_1 x_1+n_2 x_2 +\ldots + n_K x_K)t\right)\prod_{i=1}^{K}\frac{(-c_i)^{n_i}}{n_i!}
$$
Each exponential term averages to $0$ as $T\rightarrow\infty$, unless the weighted sum of the $x$'s in the exponent is zero, in which case the term averages to (indeed, uniformly equals) $1$.  So the form of the result will depend on exactly which sums of the $x$'s vanish.  If none do (except for the trivial sum), the result is $1$.  If $x_1=-x_2$ (and all the other $x_i$ are algebraically independent), then the non-vanishing terms will be those where $n_1=n_2$ (and all the other $n_i=0$), leading to
$$
\sum_{n=0}^{\infty}{{2n}\choose{n}}(c_1 c_2)^n=-1-2c_1c_2+\frac{1}{\sqrt{1-4c_1c_2}}.
$$
And so on, for arbitrarily complicated rational relationships between the $x_i$.
Comment: Nice solution. I'll leave the question open though in case a more general solution is possible, without having to resort to a convergent expansion.

I realised that in the specific case I need, the integral kernel in its original form is something like $\exp(-i x_{k,k'} t) / \sum_{k,k'} c_{k,k'} \exp(-i x_{k,k'} t)$. Here $x_{k,k'} = -x_{k',k}$ holds and also $c_{k,k'} = c_{k',k}^*$ where the asterisk denotes complex conjugation. Further, it can be safely assumed that $\sum_k |c_{k,k}|^2 > 0$.

Would this be enough for a solution that does not need a Taylor series?


msvcr110d.dll!memcpy error while testing structures and arrays

Question: I was wondering if anyone could take a look at why I'm getting runtime errors here.

First-chance exception at 0x6FBDEBC2 (msvcr110d.dll) in Project1.exe:
  0xC0000005: Access violation writing location 0x7CB67DEB. Unhandled
  exception at 0x6FBDEBC2 (msvcr110d.dll) in Project1.exe: 0xC0000005:
  Access violation writing location 0x7CB67DEB.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
    struct person {
        string name;
        int age;
        int weight;
        string nickname;
    } ;

    person people[1];

    for(int i = 0; i < sizeof(people); i++){
        string s[4];
        for(int x = 0; x < 4; x++){
            cout << "Please enter person " << i << "'s " << (x == 0 ? "name" : x == 1 ? "age" : x == 2 ? "weight" : x == 3 ? "nickname" : "unknown") << "." << endl;
            getline(cin, s[x]);
        }
        people[i].name = s[0];
        stringstream(s[1]) >> people[i].age;
        stringstream(s[2]) >> people[i].weight;
        people[i].nickname = s[3];
    }

    for(int i = 0; i < sizeof(people); i++)
        cout << "Person " << i << ": name = " << people[i].name << ", age = " << people[i].age << ", weight = " << people[i].weight << ", nickname = " << people[i].nickname << endl;

    cout << "Please press enter to continue.";
    fflush(stdin);
    cin.clear();
    cin.get();
}

It works out just fine up until the second for loop, where it appears to run the error.
Comment: `sizeof(people)`???  C'mon - you know better than that :)  SUGGESTION: use std::vector instead of an array.  IMHO...
Comment: For starters, `sizeof(people)` is bytes, not elements.
Comment: I'm an idiot! Thanks, just learning C++ from Java!
Answer: Your problem lies here:
sizeof(people)

This does not give you the length of the people array, but the total size of the array in bytes. You can use std::begin(people) and std::end(people) to get iterators to the beginning and one past the end of the array.
for (auto it = std::begin(people); it != std::end(people); ++it)
{
  // it is an iterator to an element of people
  it->name = ....;
}

Alternatively, you can use a range based loop:
for (auto& p : people)
{
  // p is a reference to an element of people here
  p.name = ....;
}

Answer: To prevent this kind of issue, consider this approach, which allows you to store the size of a static array:
int ARRAY_SIZE = 1;
person people[ARRAY_SIZE];

Then your for loop:
for(int i = 0; i < ARRAY_SIZE; i++)

Comment: Careful, the size of an array must be a compile time constant (in `C++`).


Probability of Team A defeating Team B in the first half, given probability for full game

Question: This is probably an easy question, but it's really confusing me.
Suppose that, given that the result is NOT a draw, Team A has a probability of beating Team B of 60%.
I want to ignore every external factor (players tired, different strategies, etc) and focus only on the math. If the first half was also not a draw, is the probability of Team A defeating Team B in the first half also 60%, or less than 60%?
My intuition would, at first, say 60%, because if they play that first half an infinite number of times, then I guess A would win 60%.
However, I also think that it would make sense to be less than 60%, because, since it's a smaller time frame, Team B would have a better chance of getting 'lucky', and therefore, it's probability of winning would increase.
Which of my reasonings is correct, if any?
Comment: Even upon adopting gross simplifying assumptions the underlying question appears unanswerable, because the answer depends on the probability distribution of the point differential in any given half.  That suggests the answers to the questions you did ask are "neither reason can be correct."  That uncertainty, together with the need to invent strong assumptions, make this question a poor fit to the format of this site.  Perhaps you could offer more specific circumstances for consideration?
Comment: Check out Gerd Gigerenzer's work on fast and frugal heuristics. Specifically, he develops a simple model predicting NBA team wins based on metrics like yours and compares it to a full information Bayesian model. His FFH model outperformed. Here's one summary of that work...http://library.mpib-berlin.mpg.de/ft/wb/WB_Fast_2006.pdf
Answer: The answer clearly requires a lot of assumptions, but here is a try. Let $Y_1$ and $Y_2$ be the score differential achieved in the first and second halves (A minus B), respectively. Then the overall score differential is $Y=Y_1+Y_2$, and team A wins if $Y > 0$. You told us that $P(Y>0)=0.6$.
Your assumptions of "ignoring every external factor" could be translated as having two independent and identically distributed halves, so let $Y_1$ and $Y_2$ follow the same distribution with mean $\mu>0$ and standard deviation $\sigma$. Then $Y$ has mean $2\mu$ and standard deviation $\sqrt{2}\sigma$.
This means that after the first half, the value of 0 is $z_1=\frac{\mu}{\sigma}$ standard deviations from the mean, while after the entire game it is $z=\sqrt{2}\frac{\mu}{\sigma}$ standard deviations from the mean. At this point it is still difficult to compare $P(Y_1>0)$ to $P(Y>0)$, because the distribution of $Y_1$ and $Y$ could have different shapes (and the value of $z$ does not drive the tail probabilities of every distribution), but in general we would expect that the larger the mean/SD value, the higher the probability of being above 0. So we would expect lower probability of winning in the first half.
We can work out the probabilities explicitly if we assume that the score difference is approximately normally distributed. Since $0.6 = P(Y>0) = P(\frac{Y-2\mu}{\sqrt{2}\sigma}>\frac{-2\mu}{\sqrt{2}\sigma})=\Phi(\sqrt{2}\frac{\mu}{\sigma})$, where $\Phi$ is the normal cdf, this implies $\sqrt{2}\frac{\mu}{\sigma}=0.253$, and $\frac{\mu}{\sigma}=0.179$. So $P(Y_1>0) = P(\frac{Y_1-\mu}{\sigma} > -\frac{\mu}{\sigma})=\Phi(0.179)= 0.571$, that is the team wins the first half 57.1% of the time.
If the two halves are identically distributed, but positively correlated, the effect gets weaker, because the standard deviation of $Y$ becomes $\sqrt{2(1+\rho)}\sigma$, where $\rho$ is the correlation coefficient. As $\rho \rightarrow 1$, we have $z = \frac{\sqrt{2}}{\sqrt{1+\rho}} \rightarrow z_1$.


What is the on-field role of the captain?

Question: After watching the John Terry "stripped of captaincy" episode in the England national football team, my question is what exactly does the captain do on the field?
The team tactics are decided by the manager, and the managers do pass on instructions during the game via the closest available player to the touchline/dugout.
What does the captain do on the field?
I assume off the field they can be providing strategic inputs and mentoring youngsters and so on
Answer: The FIFA Rules of the Game published for the 2011-2012 season make the following rules applicable for the captain;

The referee tosses a coin and the team whose captain wins the toss decides
whether to take the fi rst or the second kick
[In the event of a penalty shootout...] If one team has a greater number of players than its opponents, it must reduce its numbers to equate with that of its opponents
and the team captain must inform the referee of the name and number of
each player excluded. Any player thus excluded may not participate in kicks
from the penalty mark.
[On the subject of dissent...] The captain of a team has no special status or privileges under the Laws of the Game but he has a degree of responsibility for the behaviour of his team.

However, outside of the laws, it is often expected the captain has the responsibility of...

Motivating his fellow players (although I'm not sure how important this is; I'm unsure how a captain that is a goalkeeper can motivate his players.)
Keeping a cool head. Calming down fellow players when things get a little heated (obviously this is something all players can do, but it's something you expect the captain to lead).
Furthermore (and perhaps most importantly in the current day) they act as a medium between fellow players and the officials. Gone (or going) are the days where an entire team of players surround the officials to try influence a decision. Instead you tend to see the (outfield) captain making his point of view heard to the referee. Similarly, you often see the captain being a medium between a referee and a player in the event a player gets pulled up... ensuring the player keeps a cool head and a fair conversation takes place (from his point of view :P).

Off the field their most important role is that they are one of the most recognised people of the team; the manager, then the captain. They are the leaders of the team, and (rightly or wrongly) the teams public image can be strongly affected based on the image and actions of these people. The other players in the squad inevitably fall behind these two people. Of course, all members of the squad are public facing, and should act as such, but this is even more so for the captain.
Being specific to the John Terry incident, the worst scenario the FA could have is the captain of the England Football team being criminally charged with racism. Sure, a squad member being criminally charged hurts, but eventually it gets brushed under the carpet... there's 22 other squad members to think about. It is not that easy to forget about the single captain and single manager, however.
Answer: In addition to the FIFA Laws of the Game rules that Matt outlines in his excellent answer, continental and national associations may have slightly different or supplementary rules for captains, although national associations tend to be in line with the continental association they belong to. In any case, captains are expected to be exemplars of on field behaviour.
The most recent regulations for UEFA are the Regulations of the UEFA European Football Championship 2010-2012, and what applies to captains is:

14.04 Before each competition match, each team receives a match sheet on which the
  numbers, full names, dates of  birth and, if applicable, the shirt names of the 18
  players in the squad (23 players in the final tournament) must be entered, together
  with the full names of the officials  seated on the substitutes' bench and on the
  additional technical seats. The match sheet must be properly completed in block
  capitals and signed by the captain and the head of delegation.
...
16.04 If the taking of kicks from the penalty mark cannot be completed because of
  weather conditions or for other reasons beyond control, the results will be decided
  by the drawing of lots by the referee in the presence of the UEFA match delegate
  and the two team captains.
...
23.02 Protests concerning the state of the field of play must be submitted to the referee in writing by the relevant officials before the match. If the  state of the field of play
  becomes questionable in the course of the match, the team captain must inform
  the referee, in the presence of the captain of the opposing team, orally without
  delay.

In some national associations, if a player is cautioned or sent off and a suspension applies, the period of suspension is doubled if the player was captain at the time of the offence. Although I can't find a reliable reference for, I think that the doubling of the suspension time applies even if a player was acting captain at the time, receiving the armband after the regular captain went off field due to injury or substitution.
At club level, captains have various roles that can range from traditional to official:

They lead negotiations with club owners and managers regarding internal regulations, bonuses, etc, and in some associations such regulations are only valid if signed by all captains,
They are generally expected to act as the public face of the players, it's not uncommon to see captains taking full responsibility after a loss,
They are generally expected to act as mentors to younger players, especially players in youth and reserve teams.
Answer: They usually participate in huddles with the refs for stuff like the coin toss to decide initial possession and stuff like that - and are generally regarded as one of the better players/leaders of the team. Other than that I suspect it's just a glorified title.
Comment: This answer seems directed towards American football. The football tag is for association football (aka soccer, futbol, etc).
Comment: As far as what the team captain(s) role is - I suspect that between football (soccer) and american-football, it's largely the same from what I've seen when watching both sports.


Fit an adaptive elastic-net model with glmnet

Question: Is there a way to fit an adaptive elastic-net model with glmnet ? In other words, I would want my "penalty.factor" parameter applied only to the L1 penalty of the model and to keep the same level of L2 penalty.
I would like to be able to reproduce the algorithm described in this paper :
https://projecteuclid.org/euclid.aos/1245332831
I know the R package gcdnet is able to fit such models, but the computational time is far higher than with glmnet.
Thanks
Comment: Are you saying you want LASSO?
Comment: No I want to be able to reproduce the algorithm described here [link] (https://projecteuclid.org/euclid.aos/1245332831).
Answer: No, the glmnet package allows you to specify penalization factors for each feature, but this applies to both L1 and L2 norm equally.
However, there is a package called gcdnet which allows fitting of adaptive elastic net models. The intersections of authors of the paper you cite and authors of the package is not empty, so this would probably your best option.
Disclaimer: I have not worked with either the adaptive elastic net or the gcdnet, so don't regard this as a recommendation of either the package or the method.
Comment: Thanks for your answer. I know the R package gcdnet is able to fit such models, but the computational time is far higher than with glmnet. Since I was planning to perform a large simulation study, the computational time with gcdnet is an important problem.


Advanced techniques for reducing the page weight of hidden HTML forms

Question: I'm working on a large application with lots of editable objects on a page. One page for example contains a list of categories and each category has a list of tasks. Both categories and tasks can be editted via a modal dialog window with a form inside.
The current solution is to embed a hidden form prepopulated with data next to each object in the html. When the edit button is clicked, the form is displayed inside the modal dialog and submitted via ajax. The advantage to this solution is that modal dialogs pop up quick. The disadvantage is the growing number of dom elements and page size as each object requires another hidden form.
I am looking for ways to reduce the page weight and reduce the number of dom elements. I am looking for a balance of performance vs maintainability, as there are lots of pages with lots of different objects to consider.

Use ajax to dynamically load the modal populated form. This reduces the page weight considerably, but causes a delay when the edit button is clicked and is the worst for user experience. It also requires each form to have an ajax handler for this specific function.
Store the form parameters as json data and populate the form values from a cloned template form when an element is edited. This reduces dom elements significantly, and the page weight a little as well. A generic piece of javascript needs to be written to insert the json data into input elements.
Store the entire form HTML as a json object inside a data attribute of a dom element. This does nothing for page weight but will reduce the number of dom elements. On edit, the html can be injected into the modal dialog. Its also the easiest to implement.
Scrape the content of the page for the values of the form, and inject them into a cloned template form. This reduces the duplication of content, and does the most to reduce page weight and dom elements. However it feels brittle and intrusive, there needs to be id coordination for content and special care needs to be taken so that hidden input fields are also injected into the page.

Those are the options I've come up with. The JSON solutions are the most appealing to me currently. I'm curious if anyone has other solutions or insight to any of these?
Answer: The most light-weight is probably store the entire form definition as JSON and write some sort of engine that would dynamically build the form HTML on the fly. You'd then use another JSON file to represent the data layer that populates/contains the form data.
The fun you run into is how to manage the layout of the form on top of all this. For a standard two-column layout it's simply. Beyond that it's an awful lot of work.
I started writing one using prototype.js about five years ago. I never finished it but it works. Feel free to view the source and steal the code.
If you look at advanced client libraries like ext.js, this is how they're doing it.
Comment: Thanks for the answer! In order to combat the layout issue, my solution is to embed one form only per object into the html, to act as a template to the others. This way the layout is included and the js merely clones the template form and injects json data into it.
Answer: If you're going for speed, and don't have a framework handy, don't think "HTML", think "DOM element."  Maintain as much data as possible in a "global" data object, and only go to the server for information you don't have.  Initialize a modal <div> either on demand or asynchronously after form load, and populate its options via JavaScript in response to a handled event from each <input> element.
For performance, consider storing categorical information in some localStorage option.
Realistically, however, you want a framework.  (If you don't use one someone else wrote, you should write your own.)  I'm a fan of the Enyo framework leftover from WebOS personally, but there are others out there to help you do this.


Verify Demorgan's Law Algebraically

Question: If $\overline X \equiv \text { not }X$,
De Morgan's Laws are stated as:

$ \overline{(A + B)}=  \overline A\cdot \overline B$
$ \overline{(A\cdot B)} = \overline A + \overline B$

Verify the above laws algebraically.
I can prove this using truth tables and logic gates but algebraically, I don't know any intuitive way to prove it.
$$
\begin{array}{ |c|c| }
  \hline
   {\text{Axioms}}\\
  \hline
   \text{Property of 0} & X + 0 = X \space ;\space X\cdot0 = 0 \\
   \text{Property of 1} & X + 1 = 1 \space;\space X\cdot1 = X\\
   \text{Idempotence Law} & X + X = X \space;\space X\cdot X = X\\
   \text{Involution Law} & \overline{\overline X} = X\\
   \text{Complementarity Law} & X + \overline X = 1 \space;\space X\cdot\overline X = 0\\
   \text{Commutative Law} & X+Y = Y+X \space;\space X\cdot Y = Y\cdot X\\
   \text{Associative Law} & (X+Y)+Z = X+(Y+Z) \space;\space (X\cdot Y)\cdot Z = X\cdot (Y\cdot Z)\\
   \text{Distributive Law} & X(Y+Z) = XY + XZ \space;\space X + YZ = (X+Y)(X+Z)\\
   \text{Absorption Law} & X + XY = X \space;\space X(X+Y) = X\\
   \text{Other (3rd Distributive)} & X + \overline XY = X+Y\\
  \hline
\end{array}
$$
Comment: What are the axioms?
Comment: Please list the axioms in the question and not just their names, but the actual syntactic formulation of them.
Comment: @GitGud: What do you mean? Like the law of ones, zeroes, Idompotence law etc?
Comment: Yes. And don't forget the rules regarding $\neg$.
Comment: @GitGud: Involution and the Complementarity Laws? How do I use that to prove this?
Comment: @GitGud: I'm so sorry. I thought these were generally known. I have added as many *axioms* as I know.
Answer: By Complementarity Law,
$$P + \overline P = 1 \space\text{ and }\space P \cdot \overline P = 0$$
(Note: I shall only be using $P + \overline P = 1$ as its dual is automatically true)

First Law::
DeMorgan's $1^{\text{st}}$ law states $\overline{X+Y} = \overline X \cdot \overline Y$
It is sufficient to prove that $(X + Y) +  \overline X \cdot \overline Y = 1$
$$
\begin{align}
\text{LHS} &= Y + (X + \overline X \cdot \overline Y)\\
&= Y +  X + \overline Y\\
&= (Y+\overline Y) + X\\
&= 1 + X \\&= 1 = \text{RHS}
\end{align}
$$

Second Law:: DeMorgan's $2^{\text{nd}}$ Law states that $\overline{X\cdot Y} = \overline X + \overline Y$
It is sufficient to prove that $X\cdot Y + (\overline X + \overline Y) = 1$
$$
\begin{align}
\text{LHS} &= \overline Y + (\overline X + \overline{\overline X}\cdot Y)\\
&= \overline Y + (\overline X + Y)\\
&= (Y + \overline Y) + \overline X\\
&= 1 + \overline X\\
&= 1 = \text{RHS}
\end{align}
$$

Hence, DeMorgan's Laws are verified algebraically.
Comment: This is not a complete proof, since proof must be made using both of the Complementarity Law, to ensure that the proofed law (DeMorgan's Laws) is equal.  The current proof only shown like `<=`, thus not also `>=`, so `==` thus can't be inferred.  See similarity in [proof of De Morgan's laws for sets](https://en.wikipedia.org/wiki/De_Morgan%27s_laws#Formal_proof).
Comment: Both have to be satisfied. For example, given the boolean algebra $(P(X),\cup ,\cap )$, where $P(X)$ is the powerset of $X$. Take $A, B \in P(X)$, $A\cup B = 1$ doesn't imply $A\cap B = 0$. For example, $X = \{ 1,2,3,4,5 \}$, $A = \{ 1,2 \}$ and $B = \{ 4,5 \}$.
Comment: I am refering to both statements of the Complementarity Law.
Comment: I didn't explain myself correctly. $B=\bar{A}$ if and only if $A\cup B = 1$ **and** $A\cap B = 0$. Now, you can't use the duality principle for this last statement because these identities don't hold for any Boolean algebra, as the one I mentioned before.
Comment: Why is it sufficient to show that $X+Y+\overline{X}\cdot \overline{Y} = 1$? In boolean algebra $a+b=1$ does not necessarily imply $b=\overline{a}$.
Answer: Lemma: $ A + B = 1 \land AB = 0 \implies A = \overline B $
Proof:
Given $ A + B = 1 $, then:
$ (A + B) \overline B = 1 \cdot \overline B = \overline B $
$\implies A \overline B + B \overline B = A \overline B + 0 = A \overline B = \overline B $   (1)
Given $ AB = 0 $, we combine it with (1) to obtain:
$ A \overline B + AB = \overline B + 0 $
$\implies A (\overline B + B) = A \cdot 1 = A = \overline B$   QED

First law:
$ (X + Y) + \overline X \overline Y = Y + (X + \overline X \overline Y) = $
  $ = Y + (X + \overline Y) = (Y + \overline Y) + X = $
  $ = 1 + X = 1 $   (2)
$ (X + Y) \cdot \overline X \overline Y = X \cdot \overline X \overline Y + Y \cdot \overline X \overline Y = $
  $ = (X \overline X) \cdot \overline Y + (Y \overline Y) \cdot \overline X = 0 \cdot \overline Y + 0 \cdot \overline X = $
  $ = 0 + 0 = 0 $   (3)
By (2), (3), and the lemma, we have $ \overline { X + Y } = \overline X \overline Y $ -- QED

Second law:
$ \overline { X Y } = \overline { { \overline { \overline X } } \cdot { \overline { \overline Y } } } = $   (by First law)
  $ = \overline { \overline { \overline X + \overline Y } } = \overline X + \overline Y $   QED

Notes

Contrary to the approach in the first answer, it is necessary to prove both that $ A + B = 1 $ and $ AB = 0 $ in order to claim that $ A = \overline B $.
Absorption Law and 3rd Distributive are redundant as axioms, following from Property of 1, Complementarity Law, Idempotence Law, and Associative Law.
Idempotence Law is redundant as an axiom, following from Property of 1 and Distributive Law
Comment: Alternatively, if one has already shown basic properties of the component then $(3)$ follows easily from $(2)$. In particular, if one has shown that the complement of $X$ is unique _and_ that $\overline{\overline{X}}=X$, then showing $(X+Y)+(\overline{X}\cdot \overline{Y})=1$ gives $(X\cdot Y)\cdot (\overline{X}+\overline{Y})=0$ by duality. Substituting $X$ by $\overline{X}$ and using the fact that $\overline{\overline{X}}=X$ we get $(\overline{X}\cdot \overline{Y})\cdot (X+Y)=0$.

Verify Demorgan's Law Algebraically


Hindustani music: Evolution of instruments

Question: So, I was thinking about the evolution of instruments in Indian classical (Hindustani, to be precise).
We know Sa and Pa are achala swaras, and don't have any tivra or komal forms. Sa and Pa form the structure of musical system itself, hence they are held invariant. But, has this had any influence on the different instruments used during performance? For instance, the Tanpura as a drone generates Sa and Pa as reference (please correct me if I am wrong). Has this "invariance" of the two notes caused any changes in the way the instruments were fabricated over time, and the way they were played?
I know, the question sounds vague, so please do comment if clarifications are needed. And I am a beginner with a bit of idea on Classical music, so please excuse me for trivial mistakes.
Answer: I would have assumed that it has affected the creation of almost all instruments. Sa Pa form a Perfect Fifth, the constancy of which is a cornerstone of most music. Maybe approaching this question from the eyes of Western Music might give you some new insights?
This lecture by Leonard Bernstein explains a lot about how these intervals might have come to us.
Again, your question is slightly vague and I'm not sure what kind of an answer you were expecting, but I hope there is enough information here for you to start searching for your answer
Answer: The shruti (note placement) invariances do influence the design of musical instruments when they need to be used in Indian classical music. You need tunability, since even a minute deviation can create aberrations.
So keyed instrument that do not have tunability have limitations. For example, for best results, a harmonium must be tuned beforehand with respect to the reference note that the performer has chosen. This tuning not only covers the basic Sa-Pa ratio, but it also covers the placements of other notes used in the Raga.
A harmonium tuning for one raga for one choice of Sa (the chosen reference note or the drone) may not be usable for another Raga if you are too sensitive to minute changes. For most people, that difference may not be easily noticeable. However, this has lead to the development of tunable harmoniums.
For Indian classical music, an instrument also needs to have the ability to produce an unbroken, that is to say a continuous, frequency change from one note to another. This is achieved by techniques such as string pulling and sliding in the case of string instruments, and by controlling hole openings, flows, and pressures in the case of air-column based instruments such as flutes and shehnai, which use fixed hole placements.
With flutes, since the holes are not tunable once they are drilled in, they indeed need to get the perfect Sa-Pa invariance. The maths of the physics of sound is what goes into perfect drill placements in a flute.
If that goes wrong, you will see that such flutes do not produce the needed ratios correctly. On the other hand, string instruments can readily produce the needed ratios due to tunability.
One aspect they need to make sure of is the reachability of Pa and Sa from both directions on a continuously varying frequency scale. In most tunings, a string change may occur on these notes. So in such cases, the same notes are also produceable on another string at a different place not only directly, but more importantly, also through the continuous frequency change techniques listed above.
Answer: The only influence would be that the instruments could match the pitch of male and female sa-pa scale so that they can be played in harmony.


Equivalent of R-squared in Generalized Linear Model Regression Results?

Question: How do we assess degree of fitness in a Generalized Linear Model (GLM) since R-squared is not given. For example, following are results of regression in iris dataset with code: smf.glm('SL~Species+SW+PL', data=irisdf, family=sm.families.Gaussian(sm.families.links.log)).fit() using statsmodels.
                 Generalized Linear Model Regression Results
==============================================================================
Dep. Variable:                     SL   No. Observations:                  150
Model:                            GLM   Df Residuals:                      145
Model Family:                Gaussian   Df Model:                            4
Link Function:                    log   Scale:                        0.096285
Method:                          IRLS   Log-Likelihood:                -34.807
Date:                Fri, 17 Jul 2020   Deviance:                       13.961
Time:                        12:44:23   Pearson chi2:                     14.0
No. Iterations:                     6
Covariance Type:            nonrobust
=========================================================================================
                            coef    std err          z      P>|z|      [0.025      0.975]
-----------------------------------------------------------------------------------------
Intercept                 1.1842      0.046     26.024      0.000       1.095       1.273
Species[T.versicolor]    -0.1335      0.035     -3.772      0.000      -0.203      -0.064
Species[T.virginica]     -0.2046      0.046     -4.405      0.000      -0.296      -0.114
SW                        0.0713      0.014      5.118      0.000       0.044       0.099
PL                        0.1244      0.010     12.214      0.000       0.104       0.144
=========================================================================================

==================== Summary2() ====================
                  Results: Generalized linear model
=====================================================================
Model:                 GLM                AIC:              79.6145
Link Function:         log                BIC:              -712.5809
Dependent Variable:    SL                 Log-Likelihood:   -34.807
Date:                  2020-07-17 12:44   LL-Null:          -492.86
No. Observations:      150                Deviance:         13.961
Df Model:              4                  Pearson chi2:     14.0
Df Residuals:          145                Scale:            0.096285
Method:                IRLS
---------------------------------------------------------------------
                       Coef.  Std.Err.    z    P>|z|   [0.025  0.975]
---------------------------------------------------------------------
Intercept              1.1842   0.0455 26.0241 0.0000  1.0950  1.2734
Species[T.versicolor] -0.1335   0.0354 -3.7717 0.0002 -0.2028 -0.0641
Species[T.virginica]  -0.2046   0.0464 -4.4051 0.0000 -0.2956 -0.1136
SW                     0.0713   0.0139  5.1183 0.0000  0.0440  0.0986
PL                     0.1244   0.0102 12.2141 0.0000  0.1044  0.1443
=====================================================================

What is the equivalent of R-squared in above analysis?
Comment: Does this answer your question? [How to calculate goodness of fit in glm (R)](https://stats.stackexchange.com/questions/46345/how-to-calculate-goodness-of-fit-in-glm-r)
Comment: There is no equivalent, but there are a number of pseudo $R^2$. This question has been asked before, have a look [here](https://stats.stackexchange.com/questions/46345/how-to-calculate-goodness-of-fit-in-glm-r), [here](https://stats.stackexchange.com/questions/11676/pseudo-r-squared-formula-for-glms), or [here](https://stats.stackexchange.com/questions/168982/how-to-calculate-r-squared-in-glm), for example.
Comment: Thanks for the links. I will go through them and try to determine how good is fit in above example.
Answer: First, the answer. You should be able to calculate the R2 for your model by hand then, sometimes statsmodel provides a pseudo R2 as well:
sst = sum(map(lambda x: np.power(x,2),y-np.mean(y)))
sse = sum(map(lambda x: np.power(x,2),your_model.resid_response))
r2 = 1.0 - sse/sst

But, that being said I do not think assessing your regression model with R2 is the best solution in your case. Why do you not use AIC?
There are many discussion whether or not R2 is really the 'golden standard' of assessing your regression. One quote from an interesting post about misunderstandings in statistics says about R2:
"Equating a high R2 with a "good model" (or equivalently, lamenting - or, in the case of referees of papers, criticizing - that R2 is "too" low)."
Maybe look into this discussion here and here and re-consider how to report your model performance.
Comment: Specifically, what would you say is the performance of example model that I have given in my question above? I would like to see the readout there and determine rather than doing any more calculations.
Comment: Also just to be clear, it is `1.0 - (sse/sst)` and not `(1.0 - sse)/sst` ?
Comment: If I had to asses the performance of your model, I would definitely calculate the RMSE as it tells you more about the average error performed by the model in predicting the outcome for an observation. Just look into how the residuals are between observation and prediction. And, it is SSE / SST and then you subtract that from 1, to my knowledge.


Chrome: After removed formatted text, new typed text has the same styles

Question: When I remove all content from contenteditable field, and then start typing  new text wrapped into the same tag as deleted text despite that in debugger there are no relevant elements.
Examples:

Expectable behavior is following (got in Firefox):

Could somebody help me to solve this issue?
P.S. If any extra information is needed please let me know.
Comment: According to the discussion on [this post](https://stackoverflow.com/questions/14145970/chrome-empty-contenteditable-retaining-styling), it looks like a bug in Chrome that has been present for several years ([this bug sheet](https://bugs.webkit.org/show_bug.cgi?id=34608) dates from 2010). No workaround has been proposed, apparently.
Comment: I would like to play around with your code. Could you share your code so we know what WYSIWYG and what not you are using?
Comment: I guess it's because Chrome caches selected element, so add an event listener on `delete` button press and get a selection with JS, call `removeAllRanges()` or `collapse()` or `collapseToEnd()`. I hope it can help
Comment: @SergeySahakyan - I think you are correct, but I was curious, do you have any documentation for the element being cached?
Answer: Try pasting text from the font size you normally would want. This will change the font size for you. If you want to choose the font size for the entire contenteditable field, try adding that to your CSS.


Django get data of object from ManyToManyField form

Question: Hello i created this form :
class CartForm(ModelForm):
 class Meta:
     model = Cart
     fields =( 'products',)

from theses models :
class Product(models.Model):
 title = models.CharField("Titre", max_length=120)
 subtitle = models.CharField("Sous-titre", max_length=250)
 description = models.TextField("Description")
 picture = models.ImageField(upload_to='objects/')
 enabled = models.BooleanField("Activé")

class Cart(models.Model):
 products = models.ManyToManyField(Product)

and i want to display on my template an list of choice with their data
So i send form from views but i don't find any way to get the products description i only get their names !
here is my view :
def home(request):
 categories = Category.objects.annotate(test=Count('product')).filter(test__gt=0)
 # categories = Category.objects.order_by(
 #     'id')
 test = CartForm()
 return render(request, 'boutique.html', {"categories": categories, "test":test})

and what i tried in my template :
    {% for ee in test.products %}
        {{ ee.description }}

    {% endfor %}

please help me
have a nice day
Comment: Can you show your template and the view?
Comment: @SLDem yes sorry i just updated my post
Comment: I answered, try this solution
Answer: Ok so theres a couple issues here:
First of all you actually need to define what is gonna happen when you submit the form, so in your view do this:
views.py
def home(request):
 categories = Category.objects.annotate(test=Count('product')).filter(test__gt=0)
 if request.method == 'POST':
     test = CartForm(request.POST)
     if form.is_valid():
         cart = form.save(commit=False)
         for product in request.POST.getlist('products'):
             cart.add(product)
     else:
         pass
  else:
      form = CartForm()
 return render(request, 'boutique.html', {"categories": categories, "test": test})

then in your template you actually have to render the form (test):
boutique.html
<form method="post">
    {% csrf_token %}
    {{ test.as_p }}
    <button type="submit"> Add Items </button>
</form>

Now you should see the list of products in template.
edit
if you want to show a different model field in your form rewrite its __str__ method like this:
def __str__(self):
    return self.description # to show description

Comment: Thank for your answer, showing the form isn't the problem, I want to display other data from the object like description or subtitle
Comment: Thanks but that only change the title of the field, i want to get multiple data not just description or title but the two
Comment: then simply do `return str(self.title) + ' ' + str(self.description)` in the method


template for loop couldn't handle array in ie11

Question: We have a problem. We use a template for to add checkbox. But couldn't work on ie11. It's work on chrome,edge,firefox but couldn't work ie11.
  # for (var i = 0; i < items.length; i++) { #
    # item = items[i]#
<input type="checkbox" #if(item.visible) {#checked#}# name="#:item.field#" data-bind="events:{ click: checkListener}" value="#:item.Field#"/>

#: item.title #
#}#

When I debug it, kendo was success. On back yard all data is available. But here doesn't work on just ie11. For loop seems work good, but item = items[i] doesn't work.
Comment: Have you tried to debug for js errors in IE?
Answer: As stated in Kendo-UI docs here, custom-variable declarations within templates should conform to following (usual to javascript, actually) pattern:
# var myCustomVariable = "foo"; #

    #= myCustomVariable #

It's most likely, that in IE custom variables with ommited var identifier/trailing semicolon are treated differently by kendo-ui templater, or there is an issue with scopes/attribute access for global-scoped variables in compatibility mode in IE.
Comment: I turned your answer green tick. You should edit your answer as your command. If anybody get same problem on ie11, your answer will be visited first by he or she. thank you again.
Comment: no it doesn't matter, I tried that but we couldn't get any action. it work good on chrome or firefox or edge.
Comment: Maybe, `# for (var i = 0, item = items[i]; i < items.length; item = items[++i]) { #`, and delete that `# item = items[i]#` ?
Comment: or use `# var item = items[i]; #`
Comment: ahahahahah I solved like you said "var item = items[i]. thank you so much.


OS X Mounting Samba shares using logon script - Using Open LDAP

Question: Is it possible to mount a Samba share at logon using a standard OS X logon script, when using an Open LDAP directory for authentication?
Obviously you can get the username from the $1 variable, but can we temporarily capture the password to mount the share?
The share is to be mounted as the user's home directory, which is stored on the Samba share.
To pre-empt the obvious questions:

No  we can't use Kerberos
We do use network account information in the LDAP schema for certain accounts, but can't here for this machine.
Yes we have to use Samba, not AFP/NFS
Keychain is no use as it is stored in ~/Library, which is in the very directory (~/) that needs to be mounted.
Answer: I understand I'm someway "bypassing" the question, but I'm not sure you have already evaluated alternative approaches.
Alternative approach/suggestion:

you can create an alias to the share
D&D the alias into the System prefereces -> Accounts -> Login items
you can connect and flag the "remember password" option to store it in you login keyring

This way you'll have the share auto-mounted on login, that is what you wanted I hope :)
Note that this is aware of the authentication back-end you use, it is a general solution from this point of view.
Furthermore, I experimented that you can avoid alias creation (that is quite ugly to me) if you can announce your samba share via Bonjour; if it is you can D&D the share without having to create the alias.
Comment: I've edited my question to make it clearer that it is the users home drive that needs to be mounted, so whilst your answer certainly has merit, it won't solve my issue!
Answer: Is automounting at login time but not being part of the script an option?
You can use AutoFS to automatically mount the share at boot time. Apple has posted a white paper on it: http://images.apple.com/business/docs/Autofs.pdf . I personally haven't actually set one up with these steps however (we use AFP shares and Mac OS X Server which did all the configuration in LDAP for us).
In regards to not being able to use Kerberos from page 24 of the above mentioned white paper:

Since autofs does not mount AFP or SMB shares using Kerberos or individual user credentials, the automounted home shares are unmounted by Login Window and remounted using the user's credentials from the Login Window. This allows the shares to be mounted with the correct credentials and permissions.
Comment: Sadly this method only works for NFS.
You can only use SMB or AFP when using Managed clients via Open Directory.
Comment: Didn't realize that. Sorry.
Answer: LDAP (and one day Kerberos) seems to be the only way!


Drummer: Follow Bassist, Guitarist, or Lead

Question: I spent the first 10 years of my drumming life identifying the flashiest elements of songs I played, and charting my performance around those accents.  What this meant is that I found myself following the guitarist a lot of the time, following the lead vocalist some of the time, and following the bassist very little.  I'm sure we tended to be in synch some of the time by nature of the format of the song; but I feel I alienated myself from my bassists because I didn't pay much attention to them in those years.
In recent years I find myself paying the majority of my attention to the bassist.  I do this now out of a sense that it is the "right" thing to do, and also because I find that recently my bassists have been able to adjust and ruff off what I am doing.
In talking to many other musicians, it seems the consensus is that it is the drummer's responsibility to stay tethered to the bassist. It does make sense somewhat, because the guitarist(s) will play over the top of what the accompaniment is doing.
But now I wonder if I am getting away from the drummer I was in my formative years.  I listen back to my recorded material and hear the fearless musician I once was.  I can also hear in these recordings where I am essentially leading for most of the song.
So I pose to you all: Does your drummer annoy you when he does not follow the bassist?  Does he have to?
Comment: The players in the rhythm section should listen to each other. When I play guitar in a rock band setting. I listen to the drummer for the clock, the bass for the feel/groove, myself to make sure I'm not screwing up, and the vocalist for cues as to where in the song we are.
Comment: As long as the drummer is not following me (I play guitar), I'm happy. I'm relying on that dude for driving the song along and keeping the tempo...
Comment: I think the structure of a song and the time keeping is a team effort I find myself in certain places following everyone at different times. its just what clicks to me that "Oh now I should be playing this riff" and so on..
Comment: A drummer would annoy me more if they **did** follow the bassist, unless everyone were on the same page that the bassist had the best timing and tempo sense of anyone in the band. Normally I prefer the drummer be that person, and that more or less everyone follows the drummer. The "less" being when there should be emotive tempo or timing variation driven by the singer or some other instrument - perhaps in an intro or bridge or solo.
Comment: Thanks, Todd.  I guess the original reason I asked the question was because early in my 'career', my drumming almost always ignored the bassist.  I've since adjusted for this.  It's all about sensing the direction of the whole band.
Comment: Charlie Watts follows Keith so you're in good company. They reckon it explains much of the feel of the band,.
Answer: It's not just a case of 'following the bassist'.That presumes the bassist is leading. A team needs to be formed within the rhythm section, which could also involve guitar and/or keys.Whilst the drummer should be relied on to keep a steady beat in the majority of songs, the bassist will often follow the bass drum pattern to put some notes to the drum beat. It's when they don't listen to each other that I get annoyed.So it could be argued that the bassist follows the drummer.
However, listening is probably the most important factor, for everyone.Being able to complement what is being played; putting a piece into the jigsaw that fits; making an offbeat work together the second time around, after one of the team has put one in first time, etc.
The more annoying factor is working with a drummer who can't keep time. Surely this is the most important job he's got.I'd rather be working with a solid drummer who just plods on, than one who puts flashy breaks in (often in he wrong place), and fluctuates in tempo. I know music can stretch and squash, but the best person to follow has to be the drummer.
When I'm on bass, I try to mesh in with the drums, and expect him to reciprocate.That keeps things tight. Then the keys or guitar can provide the syncopated bits, safe in the knowledge that the beat goes on.
For me, whether on guitar, bass or keys, one of the nice bits of a gig is when you put an off-beat or similar in, and the next time round, the drummer does the same. Proves he's listening, and willing to be part of a team.And it works both ways.
Of all the quoted options, following the guitarist is maybe the weakest, unless you're following particular rhythm patterns he's playing on chords. Wouldn't bother following lead, unless there are accentuated parts, in which case, the whole band may follow suit.
Answer: I think the standard is "everybody follows the drummer", and the drummer anticipates the groove.  The drummer provides the heartbeat of the band.  The heartbeat has to adapt to the needs of the music, but it's still regular.  If the groove takes up pace, the heartbeat goes along.
The drummer has to keep in harmony with the band as such but without picking up any irregularities.  That's not the same as leading, at least when the rest of the band does a good job being, well, a band.
Drum breaks/solos are somewhat different in that they tend to be schizophrenic: part of the drummer goes steady, part of the drummer has rhythmic freedom.
Answer: I play keyboards now but was raised playing bass for 50 years. I currently work with a few vastly different drummers from time to time. I heartily agree with Tim who complains of drummers who try to include flashy stuff which actually gets in the way of the natural timing of songs. One fellow is particularly annoying is that he will suddenly start rolling on his double bass drums in such a way that there is no longer an identifiable downbeat to each measure.
He somehow thinks he is playing faster than the original song but what he doing actually forces me to slow down everything else about the tempo of the song in order he enforce any synchronicity at all. He is remarkably tone deaf to this fact and insists that he is "improving the drive" of the songs by doing that.
On the other hand I like playing syncopated accents on the bass or the left hand when I'm using the electric piano as the bass instrument, say in between lines of vocals and my better drummers will recognize this fact and play a flam or a short crescendo snare roll to reinforce that fact when they hear it coming by way of my deliberate hesitation in delivering certain line of vocals.
Answer: Following the guitar just gives the song more power, look at John Bonham one of the greatest drummers ever yet I never seem to hear him following John Paul Jones and the bass rather I seem to hear him following Jimmy page. Following the guitar just gives the song more power, there are a lot of guitarists on here who say that they don't like for the drums to follow them because then they don't know the tempo but I myself don't find it bothersome if you can't feel the music without a drummer going beat for beat then they shouldn't even be a musician
Answer: The drummer is the metronome . Period . If he can't keep time like a metronome you need a different one .
The band members all have a resposibility of time keeping in their own head but basically the band needs to follow the metronome .


Statistical significance test for ordered data and related effect size

Question: I have data of the following form:

Rating
1
2
3

control
0
20
11

treatment
6
14
12

Where 1 is a plant of top quality, 2 is a plant of lesser quality that USED TO BE top quality, and 3 is a plant of poor quality that USED TO BE of type 2 quality. The values listed are the counts of each type of plant from an untreated control group and with a treated group.
I had been using a simple chi-square test to determine whether these treatments had any effect, but I've come to learn that that test assumes no order to the categories, whereas my categories do have an order.
Can someone please help me to understand how to determine:
A)  whether the control and treatment are statistically significantly different from one another while taking into account the ordering of the categories.
B) How to determine an effect size from these results to use in statistical power calculations to determine sample size requirements for future studies.
For example, ordinal regression has been suggested, but it's not clear to me how such a calculation would be performed in this case (What is the dependent variable? How does one determine the significance parameter? How is effect size determined?)
Another suggestion has been the Kruskal-Wallis test, but I'm not clear how the order of the values is represented in that test.
Thanks for any advice that you can provide.
Comment: Mann-Whitney Wilcoxon rank sum test for zeroth-order stochastic dominance of treatment vs controls?
Answer: I'll assume you want to treat Treatment as the independent variable and Rating as the dependent variable.
You can use the Wilcoxon-Mann-Whitney test. Equivalently, you could use Kruskal-Wallis.
In most software applications, you would first need to convert your counts to "long format" data.  Simply, your count of 6 for "treatment", "1" would be converted to six observations, each with Treatment="treatment" and Rating="1".
An effect size statistic would report the probability that the value for an observation in one Treatment group would be greater than an observation in the other group. Vargha and Delaney's A reports this.  Similar measures that are scaled to a range of 0 to 1 are Glass rank biserial coefficient and Cliff's delta.
There are alternative tests. Kendall correlation (particularly tau-c), Spearman correlation, and Cochran-Armitage test are all candidates.
Addition:
In response to some some of the comments, I added an analysis and results for this data in R using Kruskal-Wallis test and ordinal regression.
<code>Control   = c(rep(1, 0), rep(2, 20), rep(3, 11))
Treatment = c(rep(1, 6), rep(2, 14), rep(3, 12))

Data = data.frame(Group = c(rep("Control", length(Control)),
                            rep("Treatment", length(Treatment))),
                  Rating = c(Control, Treatment))

######################

kruskal.test(Rating ~ Group, data=Data)

   ### Kruskal-Wallis rank sum test
   ###
   ### Kruskal-Wallis chi-squared = 0.59551, df = 1, p-value = 0.4403

######################

Data$Rating.f = factor(Data$Rating)

library(ordinal)

model = clm(Rating.f ~ Group, data=Data)

anova(model, type="II")

   ### Type II Analysis of Deviance Table with Wald chi-square tests
   ###
   ###       Df  Chisq Pr(>Chisq)
   ### Group  1 0.6063     0.4362
</code>
Comment: Kruskal-Wallis makes sense if things are individually ranked (no duplicates), but is it really correct if there are repeated rankings? It seems that a set that is all ranked 2 would have the same rank some as another group that is half 1 and half 3 and that they would show no difference under this test even though they are obviously different.
Comment: Yes, Kruskal-Wallis will handle ties fine in most software implementations.  And, yes, (2,2,2,2,2,2) and (1,1,1,3,3,3) have the same average ranking, and so be equivalent responses according to Kruskal-Wallis.  If you want these two to be different, you might consider a chi-square test of association.
Comment: Sal Magniafico thank you so much for your answer. Can you let me know what you mean by "most implementations? My plan was to use python, but the test seems simple enough to write up in Excel if I wanted to. Can you let me know what it means to "handle ties fine" and what it means to not do so?
Comment: What I mean by "most implementations" is the relevant functions in, say, R and Python and SAS.  ...  It's a good idea to conduct the test by hand (Excel) and then you can always compare the test to the output from a software package.  Be aware, tho, at least in the two-sample Wilcoxon-Mann-Whitney test (the two-sample case for Kruskal-Wallis) , software packages often have different options that will produce somewhat different output. ... If you do it by hand, you just need to follow the procedure that accounts for ties.  (cont')
Comment: And that's what I mean by "handles fine".  It follows such a procedure.  And also, that the results are sensible.  Like if you compare the results to ordinal regression, having many ties in Kruskal-Wallis gives similar results.
Comment: Thanks again Sal Mangiafico. If you would, could you say a few words about how ordinal regression would work for determining whether a statistically significant difference exists between the treatment and control groups? From what I've seen, ordinal regression allows continuous datasets to be used to predict ordered response variables, but in my case it's just two datasets with three ordered categories; I don't see how such a calculation would be performed.
Comment: Ordinal regression can have categorical independent variables --- just like usual general linear models --- coded with dummy variables.
Comment: I added analysis and results for these data, in R, using Kruskal-Wallis and ordinal regression.
Comment: BTW, treating *Rating* as a nominal variable rather than an ordinal variable would lead to a different result.


Evaluation and optimization in machine learning

Question: I am reading the article "A few useful things to know about machine learning" by P. Domingos. Now, I am confused about two of the three components of learning, i.e., the evaluation and optimization. From the article, the evaluation function is the one used to distinguish good classifiers from bad ones, while the optimization is the method to search among all the classifiers for the highest-scoring one. So, they are kind of similar, right? Am I misunderstanding something here? Many thanks for your time and attention.
Comment: Is [this](https://homes.cs.washington.edu/~pedrod/papers/cacm12.pdf) the article?
Answer: Evaluation is a score/metric that tells us how we are doing. For example, if we are trying to predict a number across a test set of size $n$, we can compute Mean Absolute Error = $\frac{1}{n}\sum_{i=1}^{n}|observed_i - predicted_i|$ or we can compute Root Mean Squared Error = $\sqrt{\frac{1}{n}\sum_{i=1}^{n}(observed_i - predicted_i)^2}$
Optimization refers to the representation and how we can choose different ways to optimize it. For example, we can try to simply try every possible hypothesis in our hypothesis space. Or we can use a more intelligent method to try the most promising hypothesis only. And as we are optimizing, we use our evaluation function to know if this particular hypotheses is better than another.
Comment: @JosephStone Yes to the first half of the statement. However, the mean absolute error and root mean squared error are examples of the evaluation function. Examples of the representation are the familiar algorithms such as Decision Trees and Neural Networks. Sorry for the confusion. I've updated the answer slightly
Comment: So the evaluation is the standard to use to see which hypothesis is better and the optimization is the process to choose among all different hypothesis. Right? In your answer, the mean absolute error or the root mean squared error is some examples of representation, right?
Comment: Also, as both evaluation and optimisation require some truth (i.e. true decision for some objects), they musn't ever use the same information, because it will make evaluation result biased and may hide overfitting.
Answer: Here is a very clean answer.
Optimization consists of minimizing or maximizing an objective function with respect to one or more variables (a.k.a. parameters, a.k.a. arguments), possibly subject to constraints on those variables.
Viewed in this framework, "evaluation" means evaluating the objective function at a particular combination of variable values.  The optimizer has to evaluate the objective function (and possibly its gradient and maybe Hessian) at various values of the variables in order to carry out the optimization.


How can I pre-select a dropdown option using previously submitted data from database, MySQLI

Question: I have two tables: orders and companies. Note that both tables has a company_id row.
Orders Table:
order_id   |   order_name   |   company_id   |   .....
Companies Table:
company_id   |   company_name   |   .....
I have a page with a form that INSERTS data for an order into the orders table in my database. Part of the order is selecting a company from the companies table to go with it, so I use a dropdown for this.
I also have another page with a form that UPDATES that data (an edit page). When I click on the button to bring up the edit page, I'd like to have the previously submitted data in the input fields. With text fields, I just use value="<?php echo $company['whatever']; ?>". However, with dropdown menu's I can't figure out how to pre-select the correct option.
Here is my URL structure:
http://website.com/orders.php?page=edit&order_id=1

Here is what I have to create my dropdown menu:
$getOrder   = mysqli_query($db, "SELECT * FROM orders WHERE order_id = ".$_GET['order_id']);
$getCompany = mysqli_query($db, "SELECT * FROM companies ORDER BY company_name ASC");

....

<select class="form-control" name="company_id" id="company_id">
<?php
    if($getCompany) {
        while($company = mysqli_fetch_assoc($getCompany)) { ?>
            <option value="<?php echo $company['company_id']; ?>">
                <?php echo $company['company_name']; ?>
            </option>
        <?php }
    }
?>

How can I get selected on the option that's been previously selected?
Comment: your question is little bit confusing? what you actually want ? show us expected outcome?
Answer: If $getOrder has the order details (including company_id), and $getCompany has the company details (including company_id), then you can compare the two.
If they are equal, echo out selected as an attribute in the option, like so:
<select class="form-control" name="company_id" id="company_id">
<?php
    if($getCompany) {

        //Get company ID from Order
        $orderID = $getOrder["company_id"];

        while($company = mysqli_fetch_assoc($getCompany)) { ?>
            <option value="<?php echo $company['company_id']; ?>"
            <?php
                //Compare and echo `selected` if they are equal
                if($orderId==$company["company_id"]) echo "selected";
            ?>>
                <?php echo $company['company_name']; ?>
            </option>
        <?php }
    }
?>

That code can be cleaned up, however:
<select class="form-control" name="company_id" id="company_id">
<?php
    if($getCompany) {
        $orderID = $getOrder["company_id"];
        while($company = mysqli_fetch_assoc($getCompany)) {
            echo "<option value='{$company['company_id']}".($getOrder["company_id"]==$getCompany["company_id"] ? " selected" : null).">{$company['company_name']}</option>";
        }
    }
?>


Use a command line program from within Java

Question: How can I use a command line program from within Java?
I'm trying to pass a graph definition in the dot-language (see Wikipedia) to the interpreter program dot (see GraphViz) through java.
The problem is, that the program does not answer, after I have sent the dot-graph to its InputStream, because it does not know, that I'm finished sending the description.
This is, what I currently have:
<code>package exercise4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class Main {
    public static void main(String[] args) {
        PrintStream out = System.out;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        try {
            final String start =
                "strict graph LSR%1$d {\n" +
                " node [shape=circle color=lightblue style=filled];\n\n" +
                " {rank=same; A--B [label=6];}\n" +
                " {rank=same; C--D [label=12]; D--E [label=4];}\n" +
                " A--C [label=4]; B--D [label=4]; B--E [label=9];\n\n" +
                " node [shape=record color=\"#000000FF\" fillcolor=\"#00000000\"];\n}\n";
            Process dot = Runtime.getRuntime().exec("dot -Tsvg");
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintStream(dot.getOutputStream(), false, "UTF-8");
            out.printf(start, 0);

            out.flush();
            out.close();

            while(in.ready()) {
                System.out.println(in.readLine());
            }
            in.close();
            dot.destroy();
        } catch (UnsupportedEncodingException ex) {
        } catch (IOException ex) {
        } finally {
            out.close();
        }
    }
}
</code>
Comment: Why do you think it doesn't know you're finished? By what mechanism is it supposed to know you are?
Comment: 1- Use ProcessBuilder, it will make your life easier. 2- It's not recommended to use readLine when reading from processes, not all process will output line endings. Use Process#waitFor to get the processes exit code, which may help you diagnose the problem
Comment: Go through the Java World article linked from the [`exec` info. page](http://stackoverflow.com/tags/runtime.exec/info), implement **all** the recommendations, then break the args into a `String` and use a `ProcessBuilder` to create the `Process`.
Answer: Looks as if you are reading from the wrong input stream.  Have a look at this answer: https://stackoverflow.com/a/4741987/1686330


Read :hover pseudo class with javascript

Question: I made a function that overwrite the the :hover of some elements on a page. It fades between the normal and the :hover effect. That for i had to create a .hover class in my CSS file. I think this is a little unclean. How could i read the the :hover pseudo class contents?
Comment: you might want to revisit this question. The owner of the accepted answer has edited it to point out that it is wrong.
Answer: Using getComputedStyle as on the accepted answer won't work, because:

The computed style for the hover state is only available when the element is actually on that state.
The second parameter to getComputedStyle should be empty or a pseudo-element. It doesn't work with :hover because it's a pseudo-class.

Here is an alternative solution:
function getCssPropertyForRule(rule, prop) {
    var sheets = document.styleSheets;
    var slen = sheets.length;
    for(var i=0; i<slen; i++) {
        var rules = document.styleSheets[i].cssRules;
        var rlen = rules.length;
        for(var j=0; j<rlen; j++) {
            if(rules[j].selectorText == rule) {
                return rules[j].style[prop];
            }
        }
    }
}

// Get the "color" value defined on a "div:hover" rule,
// and output it to the console
console.log(getCssPropertyForRule('div:hover', 'color'));

Demo
Comment: This will not work on stylesheets on other domains due to domain restrictions. so it's partial solution
Comment: Suspiciously identical, but [this answer](http://stackoverflow.com/a/16966533/2231440)'s method allows for selectors that aren't the entire rule selector.
Answer: You could access document.styleSheets and look for a rule that is applied on that specific element. But that's not any cleaner than using a simple additional class.
Comment: stylesheets loaded from external files are not available in document.styleSheets.
Comment: @JacobRask — That's not true. **External** files are accessible. **Cross-origin** files are not by default but can be accessed via `` **+** a suitable `access-control-allow-origin` header on the response from example.com.
Answer: UPDATE: I somehow got this wrong. The below example doesn't work. See @bfavaretto's comment for an explanation.
In Firefox, Opera and Chrome or any other browser that correctly implements window.getComputedStyle is very simple. You just have to pass "hover" as the second argument:
<!DOCTYPE html>

<meta charset="UTF-8">

But I don't believe there's yet a solution for Internet Explorer, except for using document.styleSheets as Gumbo suggested. But there will be differences. So, having a .hover class is the best solution so far. Not unclean at all.
Comment: This won't work, because `getComputedStyle` expects a pseudo-element as the second parameter, and `hover` is a pseudo class. If you remove that, it will work, but will only give you the hover value if it runs while the element is actually on `hover` state. [demo](http://jsfiddle.net/A82Ax/).
Comment: thx! Maybe its not unclean but the person that uses my function has to know that he has to do .hover class. It would be nice to avoid this.
Comment: @david, out of curiosity, how are you going to handle the IE issue?
Comment: @Ionut: this is a great example of some browser inconsistency which isn't worth your time fixing. If someone is using an inferior browser, they'll still get a working website, it just won't have the nice little touches.
Comment: @nickf, I totally agree with that, but there are cases when the client desperately wants the same look in the browser she's using (IE6). I'm experiencing such issues. Otherwise progressive enhancement is the way to go.
Comment: i always do progressive enhancement. But we still have something like 30% IE6 hits on our servers, so i cant just ignore it :/ but i would love to
Comment: @Ionut: I don't know yet how i gonna handle this. Maybe there just gonna be this ".hover" class to add to the CSS if the user want to use the function in IE browser. In the other case its juste gonna do nothing and let IE use the standart css :hover function.
Comment: This doesn't seem work for me in either Firefox 3.6, Chrome 4 or Opera 9.5. The alert just shows the value of the non-hovered background colour. In Firefox and Chrome it alerts `rgb(255, 0, 0)`. In Opera it alerts `#ff0000`.
Comment: @bfavaretto hmm, you're right. I wonder why I put this here back then. Maybe I misinterpreted the value I saw in the alert box, and thought it worked. I'll delete the answer. Thanks for pointing out the mistake.
Comment: I added an alternative solution as an answer. This question caught my attention after [another question](http://stackoverflow.com/q/12626480/825789) was closed as a duplicate of this one.
Answer: If there are any people here who use the questions accepted answer but it won't work, here's a nice function that might:
function getPseudoStyle(id, style) {
    var all = document.getElementsByTagName("*");
    for (var i=0, max=all.length; i < max; i++) {
        var targetrule = "";
        if (all[i].id === id) {
            if(all[i].selectorText.toLowerCase()== id + ":" + style) { //example. find "a:hover" rule
                targetrule=myrules[i]
            }
        }
        return targetrule;
    }
}

Comment: This is a **very** specific approach that will only work if  (a) the CSS selector that applies the rule uses a single ID selector + single psuedo-class (b) it isn't overridden by the cascade and (c) you define `myrules` (which, as it stands, will cause the code to throw a reference error) and (d) the element you are looking for is the **last** element in the document which meets condition A.
Answer: There is an alterantive way to get :hover pseudo class with javascript. You can write your styles of hover pseudo class in a content property.
p::before,
p::after{
  content: 'background-color: blue; color:blue; font-size: 14px;';
}

then read from it via getComputedStyle() method:
console.log(getComputedStyle(document.querySelector('p'),':before').getPropertyValue('content'));

Comment: The is an incredibly fragile approach that depends on the developer keeping the `content` value and `:hover` rules in sync *while accounting for the cascade*.


set multiple taxonomy values programmatically

Question: I have a view that has a "Has taxonomy term ID" contextual filter and also a nid contextual  filter.
I execute this view programmatically, but I need to pass also multiple values for the taxonomy contextual filter.
I tried this:
$view->set_arguments(array($tid, $nid));
$view->execute();

and it works only if $tid is only a number.
I tried to put more than one tid using "+", but it doesn't work.
(example 3+5+8)
Any idea on how I can set a multiple taxonomy argument programmatically?
thank you in advance

I found a solution, but I don't like it. I removed the filter from contextual filter and I added an exposed filter for the same field. Then I changed the code in this way:
$view->set_arguments(array($nid));
$view->exposed_input['field_myfield_tid'] = $tid;  //note that $tid is an array of tid
$view->execute();

Comment: possible duplicate of [Set multiple items for an argument in embeded view](http://drupal.stackexchange.com/questions/6466/set-multiple-items-for-an-argument-in-embeded-view)
Comment: thank you, but I think that it is not. I have no problem in passing multiple arguments (look at: array($tid, $nid)). Here the problem is related on how to pass multiple values for the same argument ($tid = $tid1 . "+" . $tid2 . "+" etc.)
Comment: Separate multiple terms with a plus sign. I have tried it and worked.
`$view->set_arguments(array('23+4585+84', 123)); // 123 is the NID`
Answer: You could also implement the "hook_views_pre_view" function in order to alter the contextual filter arguments of your view and display. In order to add multiple taxonomy values for the same argument, you can change the $args[0] element from the $args variable of the hook.
For example: $args[0] = '22+14+64';
Remember to check the "Allow multiple values" option of the contextual filter.
Take into account that altering contextual filter arguments inside the previous hook may produce troubles when using a views exposed form. Specifically, the form "action" attribute may have a wrong URL. If that issue appears, you can solve it by implementing the "hook_form_views_exposed_form_alter" function and changing the $form['#action'] element with the correct URL.
Answer: For achieving this requirement there are several methods , the first one is
in your Taxonomy Contextual filter  enable Specify validation criteria
and put Filter value type on Term IDs separated by , or +,

with this settings the following code will works for you.
   $tid = "3,4,5"; // or  $tid = "3+4+5";
   $view->set_arguments(array($tid, $nid)); $view->execute();


Definition of quotient ring

Question: I have a question about the definition of quotient ring (I mean a commutative ring  case) .
Let $(a+I)$ be a coset of $R$ and $(b+I)$ be another left coset of $R$.
Then the multiplication in the quotient ring is defined as
$(a+I)(b+I)=(ab+I)$
However, if we consider the setwise product of $(a+I)$ and $(b+I)$ we know that their product is just a subset of a fixed coset of $I$. Then why should I believe the multiplication in the quotient ring is well-defined?
Comment: See [this question](http://math.stackexchange.com/questions/446492/what-does-defining-multiplication-in-quotient-rings-actually-mean) for more details. Egreg's answer shows why you "should believe" that it is well-defined.
Comment: I would recommend reviewing the proof of the fundamental homomorphism theorem for groups, which is more illuminating than the one for rings since it shows why the normality condition is necessary.
Comment: Why are you considering their setwise product?
Comment: Doubting that "multiplication in the quotient ring is well defined" when confronted with two multiplications is silly. You make it sound as if you expect there is some absolute multiplication that rules them all. It only makes sense to doubt whether or not a particular multiplication is well-defined.
Comment: Multiplication in a quotient ring has nothing to do with the setwise product. Quotient ring multiplication is saying that if you multiply any two elements of $I + a$ and $I + b$, you get an element of $I + ab$, which is why the quotient map is well-defined and a homomorphism. It need not be possible to write every element of $I + ab$ as the product of two elements in $I + a$ and $I + b$, in which case the setwise product would give the same answer.
Comment: For any $r\in a+I$ and $s\in b+I$ it is a straightforward exercise to show that $rs\in ab+I$ using the fact that $r=a+i$, $s=b+j$ for some $i,j\in I$ and the absorption property of multiplication by an element of an ideal.
Answer: The importance of ideals comes from the fact that they're the kernels of a ring homomorphism as consequence of the first isomorphism theorem. From perspective if I have a ring homomorphism $\varphi : R \rightarrow Q$ then we can interpret the cosets as the preimages of singletons, ie $\varphi^{-1}(q)$ for $q \in Q$, which is $q + I$ in the usual quotient construction.
This gives us the motivation to define the multiplication of ideals in a way that respects this construction. So by $(q + I)(p + I)$ we mean there exist $r,s \in R$
with $\varphi(r)=q$ and $\varphi(s)=q$ and we are interested in the preimage $\varphi^{-1}(\varphi(r)\varphi(s))=\varphi^{-1}(\varphi(rs))$ which is $(qp+I)$.
This is why multiplication of cosets is not defined setwise. Even simple ideas like $(2) \subset \mathbb{Z}$ cause trouble immediately since $(2)(2)$ which should simply be $(2)$ since it's $0\cdot 0$ but setwise it would be $(4)$, which as you noted isn't even a coset.
Answer: An Excellent Question! I am also troubled by this question for a long time. The key point here is to remember why we define the product on the quotient ring this way. You may think this is a set equation, however what really matters is that we can verify the product is well-defined. In another word, it is independent of the choice of the representation element. You can check the definition of quotient group, you will find out the definition there is actually a set equation (What a bad coincidence, which may make you think the set equation is the crucial thing). But if you check a textbook instead of just a definition, you will find out the reason why we define a normal subgroup is to keep the product well-defined!
Comment: Welcome, Yanyu Wang. I don't think this answers the question. The aim of the quotient ring isn't just to get a "well defined product". That doesn't say much about its ring structure, only that it has one.
Comment: Well, you can consider the case when $I$ is a left ideal instead of a two-sided ideal, then you will find out that you can just prove that $R\I$ is a left $R$ module instead of a ring, in fact, you can verify that (a+I)(b+I)=(ab+I) is not well-defined right now. So the ring structure itself is actually interesting enough, In many cases, you can't even get a ring structure.
Answer: I suppose you mean that you are considering cosets of an ideal $I$ in a ring $R$, then $$(a+I)(b+I) = ab +aI+bI +I*I = ab + I + I + I= ab + I$$. Because $aI = I$, $I*I = I$ and $I + I=I$ as consequences of the definition of an ideal.
Comment: $I\cdot I = I$ is usually not true.  For example, $(2)\cdot (2) = (4)$.
Comment: You do not multiply ideals together in the product of cosets in a quotient ring! The reason why it's well defined is that the product of any two elements in $I + a$ and $I + b$ is in $I + ab$. That's the whole reason why we can define the product of $I +a$ and $I + b$ to be $I + ab$ and have the quotient map be a homomorphism. There is absolutely no multiplication of ideals going on here.
Comment: @Slade OK, I shoud have written $I\cdot I \subseteq I $, or better used representatives $i_a, i_b \in I$ so that $i_ai_b \in I$

Definition of quotient ring


State the points on the curve where the tangent is horizontal AND vertical

Question: x = t^3 - 3t
y = t^2 -3
I tried the problem and this is what I got:
Horizontal: (0,-3)
Vertical: (2,-2) and (-2,-2)
Also I'll throw this one in since I have no one to check my work:
Find the equation of a tangent line to the curve at the point t=1
x = 1+4t-t^2
y = 2-t^3
For my equation I got:
y=-3x/2 + 7
Answer: Hint. One may recall that

$$
x'(t_0)\neq 0,\quad y'(t_0)= 0
$$

gives an horizontal tangent at the point $(x(t_0),y(t_0))$ and that

$$
y'(t_0)\neq 0,\quad x'(t_0)= 0
$$

gives an vertical tangent at the point $(x(t_0),y(t_0))$.
Here one gets
$$
x'(t_0)=3t^2-3, \quad y'(t_0)=2t.
$$
Can you take it from here?
Comment: I think that is what I did
Comment: @dmsc I've checked your two answers: they are correct.
Answer: $$\frac{dx}{dt}=3t^{2}-3=0=>t=^{+}_{-}1$$
for $t=1$ we have
$$x=1^{3}-3\cdot1=-2$$
$$y=1^{2}-3=-2$$
this equal to point $$(-2,-2)$$
for $t=-1$ doing the same procedure, we have the point (2,-2).
Now for the vertical:
$$\frac{dy}{dt}=2t=0=>t=0$$
for t=0, we have:
$$x=0^{3}-3\cdot0=0$$
and
$$y=0^{2}-3=-3$$
wich is the point (0,-3). Now the second question we need the slope m of the curve first, for this we have the next $m=\frac{dy}{dx}=\frac{\frac{dy}{dt}}{\frac{dx}{dt}}$,so.
$$\frac{dy}{dt}=-3t^{2}$$
for $t=1$ we have
$$\frac{dy}{dt}=-3$$
now we calculate for $\frac{dx}{dt}$
$$\frac{dx}{dt}=4-2t$$
for t=1 we have
$$\frac{dx}{dt}=4-2=2$$
then the slope m are
$$m=\frac{dy}{dx}=\frac{\frac{dy}{dt}}{\frac{dx}{dt}}=-\frac{3}{2}$$, now calculate point $(x(1),y(1))$
$$x(1)=1+4-1=4=>y(1)=2-1=1 =>[x(1),y(1)]=[4,1]$$
we have the slope and the point, we can calculate now finally:
$$y-1=-\frac{3}{2}(x-4)<=>y=\frac{3}{2}x+7$$
in conclution your answer are good.
Comment: sorry.I answer late again, is dificult to me write fast in LaTeX
Comment: It's ok. do you know if my second answer to the second problem is correct?
Comment: I complete the answer above, your second answer is good too


clustering over sphere surface

Question: I have two algorithms, both of which create points on the surface of a higher dimensional sphere (say in $\mathbb{R}^n$). Now, I want to check, which algorithm gives a more uniform spread of points over the surface of the sphere. In lower dimensional cases like $\mathbb{R}^2$ and $\mathbb{R}^3$ both perform well but I want to check in higher dimension, say $n \geq 50$. I believe, some kind of clustering algorithm may help but I don't know which one to use in this situation (since the points are on surface and not inside). Is there a way to solve this problem ?
I can generate as many points as required for both the algorithms, if that helps.
Answer: Your question doesn't have many specifics, so my assumption is that you need a way to check how uniform a distribution of points over a sphere is, so as to compare your two algos.
Here are two simple suggestions:

Empirically estimate the entropy or uniformity. Essentially, divide the sphere into segments (e.g. in spherical coordinates, but be careful) and count the number of points that fall into each segment. You could then measure the entropy of the histogram (i.e. if $n_i$ points fall into area $i$, and $N$ points were sampled in total, then $p_i = n_i/N$ and the entropy is $\mathbb{H}=-\sum_ip_i\log p_i$. Higher entropy means more uniformity. You could also compute the variance in the set of observed probability values to estimate uniformity, i.e. $\mu_p = \frac{1}{N}\sum_ip_i$, then $V = \frac{1}{N-1}\sum_j (p_j - \mu_p)^2$.
Fit a von Mises distribution to the data, and simply check the concentration parameter $\kappa$. As $\kappa\rightarrow 0$, the distribution approaches the uniform one. (See the link for how to do parameter estimation).

clustering over sphere surface


How to find Organic search data using google analytics 4 data API?

Question: I'm using the google analytics 4 API to retrieve the analytics data. While I'm having an issue to retrieve the Organic search data.
Working code for other metrics.
$response = $this->data_client->runReport([
  'property' => $propertyId,
  'dateRanges' => [new DateRange(['start_date' => '7daysAgo', 'end_date' => '1daysAgo'])],
  'metrics' => [
    new Metric(['name' => 'totalUsers']),
    new Metric(['name' => 'newUsers']),
    new Metric(['name' => 'sessions']),
    new Metric(['name' => 'screenPageViews']),
    new Metric(['name' => 'engagedSessions']),
    new Metric(['name' => 'activeUsers']),
  ],
  // 'dimensions' => [
  //   new Dimension(['name' => 'streamName']),
  //   ]
]);

foreach ($response->getRows() as $row) {
  foreach ($row->getDimensionValues() as $dimensionValue) {
    print 'Dimension Value: ' . $dimensionValue->getValue() . PHP_EOL;
  }
  foreach ($row->getMetricValues() as $metricValue) {
    print 'Metric Value: ' . $metricValue->getValue() . PHP_EOL;
  }
}

I'm trying to use the defaultChannelGrouping to retrieve the organic search but not getting any useful documents how to do.
Sample code which is not working.
 $response = $this->data_client->runReport([
  'property' => $propertyId,
  'dateRanges' => [new DateRange(['start_date' => '7daysAgo', 'end_date' => '1daysAgo'])],
  'dimensions' => [
    new Dimension(['name' => 'defaultChannelGrouping']),
  ]
]);

foreach ($response->getRows() as $row) {
  foreach ($row->getDimensionValues() as $dimensionValue) {
    print 'Dimension Value: ' . $dimensionValue->getValue() . "
";
  }
}

Answer: Try using sessionDefaultChannelGrouping rather than defaultChannelGrouping.
In the migration guides, GA4 recommends using sessionDefaultChannelGrouping in GA4 where you might have used ga:channelGrouping in Universal Analytics.
defaultChannelGrouping is an attribution dimension. If your GA4 property's event attribution model is Data-driven, defaultChannelGrouping will return no rows if your GA4 property has not had a conversion in the date range.


Memory effective Linear interpolation with PHP

Question: I have a 1D array (XYData), e.g.
$TE = array(
      "1"=>"20",
      "2"=>"30",
      "5"=>"50",
      "10"=>"90"
   )

I would like to create a memory effective PHP function which do the Linear interpolation of the passed X value and return the corresponding Y value. e.g.
calling function interpolate($TE,9.5)
then it should return 86
Is there any way to avoid the array search  as the XYData set may be very long, say more then 100 points.
Thank you in advance!
Comment: That doesn't look much like a 2-d array to me, it's only one-dimensional, key/value pairs.... given that it's a non-linear projection, you'll need to use a regression to calculate the intermediate values
Comment: Yes. this should be a 1D array.
Comment: http://blog.creativum.nl/2011/09/linear-interpolation-with-php/
Comment: Thank you @Hanky웃Panky, the example is returning the X value by providing the Y value, how can I do it in reverse? and make it a more memory effective code?
Answer: No, you cannot avoid looking at your array. To make it more efficient you have to restructure your data. Do this by recursively looking for the middle, and then split it at that point into two parts. For your short example you would get this:
$TER = array("2 and lower"  => array("1" => "20",
                                     "2" => "30"),
             "5 and higher" => array("5" => "50",
                                     "8" => "100"));

No recursion is shown, and it really doesn't make any sense for such a small set of data, but when the dataset becomes large there's a clear advantage. It's basically a simple binary search tree.
But I have my doubts implementing it would be useful in this case. I'm not going to work it all out, you really should have 100.000 items or more to make this useful. If not, then just work through the array.


Docker Error creating external connectivity network

Question: i am trying to deploy a portainer/agent stack with the recommended docker compose configuration as suggested in the official documentation (https://portainer.readthedocs.io/en/stable/agent.html), and it deploys and runs correctly on all the nodes except for 1.
on the node that fails, i check the error and it says:
starting container failed: error creating external connectivity network: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network

this is strange because this is a latest installation of raspbian. and i have just installed docker using the script from the official raspberry pi website (https://www.raspberrypi.org/blog/docker-comes-to-raspberry-pi/). this is how docker has been set up on the other raspberry pi's too (the other raspberry pi's are working fine).
the docker compose file is:
version: '3.2'

services:
  agent:
    image: portainer/agent
    environment:
      AGENT_CLUSTER_ADDR: tasks.agent
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    ports:
      - target: 9001
        published: 9001
        protocol: tcp
        mode: host
    networks:
      - portainer_agent
    deploy:
      mode: global

networks:
  portainer_agent:
    driver: overlay

Answer: I think that this is a Docker issue, last time I hitted this error I had to reboot my Docker host to fix it and be able to create a container.
I was using an old version of Docker though, might have been fixed since.
Comment: i have rebooted many times and tried upgrading my docker version many times trying to figure this out.
Answer: This will happen if you have used all the available networks in your pool of subnet spaces. For overlay networks, you can limit the available address pools with the --default-addr-pool option on docker swarm init which you'll see under the Swarm section of docker info. Once docker has allocated all the subnets from that pool, you'll need to delete networks to free up space in the pool (or create your swarm with a bigger pool, I'm not aware of a way to change this setting after creating the swarm).
Note that if a subnet is routeable, docker will treat it like an allocated network, so beware of things like VPN's that create a large route that includes all the private networks.


Identifying sheaves of sections of fiber bundles

Question: Given a topological space $X$, there's a fundamental category equivalence between local homeomorphisms to $X$ and sheaves of sets over $X$. One direction takes a local homeomorphism to its sheaf of sections, and the other takes a sheaf, and constructs the projection from its étalé space, comprised of stalks.
The above restricts to an equivalence between covering maps and locally constant sheaves.
Covering maps being fiber bundles with a discrete fiber, is there perhaps an intermediate equivalence describing sheaves of sections of general fiber bundles? At least, what distinguishing properties do sheaves of sections of fiber bundles possess?
Comment: oh, agreed. Apologies. This is an interesting question...+1
Answer: Fiber bundles with non-discrete fibers are not local homeomorphisms. Hence at the very least there is no "intermediate" equivalence.
Interestingly, vector bundles are equivalent to modules over the sheaf of continuous real functions (itself the sheaf of sections of the trivial fiber bundle with fiber $\mathbb R$ over $X$) , so structured fiber bundles admit nice descriptions.
Perhaps fiber bundles can somehow be described using some canonical sheaf on the space, but I don't see how.


How can I take a screenshot of my app running on a Samsung Smart TV?

Question: Is there any way to take a screenshot of my app running on a Samsung Smart TV 2014?
Any help is appreciated.
Comment: this is not a programming question.  Stack overflow is for programming questions only.
Comment: I guess it is. I have DEVELOPED an app for Samsung Smart TV and I need to take the screenshot of my own app that I need it in the submission process.
Comment: @La-comadreja I guess it would be polite to take your down-vote to my question back or give some reason for that.
Answer: 1 take out your smart phone/ camera
2 take a pic of your TV screen
Comment: I was actually kidding :)
Comment: Thanks for the answer. I was wondering if there is any tools for that like Roku.
Answer: I am answering my own question. Hope it will help somebody:
Based on my research, there is not such a thing. You need to use the emulator to take screenshots.
Answer: Press Home and Power buttons together on your TV to take screenshots ;)
Comment: How will this get saved in your desktop/laptop?
Answer: Ran into the same situation.
I like the emulator idea, but another one is if you are able to run your app in a browser, you can run the browser full screen and take screen shots.


Domain member server's time is drifting

Question: I have a Windows 2008R2 server that is a member of a domain.  The other day I found that its time was different from the domain controller by seven seconds.  I ran the following command...
w32tm /config /syncfromflags:domhier /update

net stop w32time

net start w32time

I then rebooted the server just to be sure.   The time on the server was 'correct' in no time.  But a day or two later the server has drifted again by 3 seconds.
All other member servers on this domain are fine (time is in sync to well within a second)
I've checked the settings in regedit and the 'Type' key shows 'NT4DS' which I believe is correct for a server synced from the domain.   All other registry entries under that folder match a working server.
I believe all the correct ports are open between the server and the domain.   The server has no other domain related problems.
The server is virtual (VMware).  The virtual machine is not set to sync time with the host.  Most of the other servers (that don't have this problem) are also virtual, on the same host pair.
Any ideas? Or any further areas I can check to verify the config?
Edit:  New information.  I just ran w32tm /query /status and got this...

Leap Indicator: 3(last minute has 61 seconds)
Stratum: 0 (unspecified)
Precision: -6 (15.625ms per tick)
Root Delay: 0.0000000s
Root Dispersion: 0.0000000s
ReferenceId: 0x00000000 (unspecified)
Last Successful Sync Time: unspecified
Source: Free-running System Clock
Poll Interval: 10 (1024s)

On a different server the 'Source' paramater lists a domain controller.  So there is definitely a 'good' reason for this server to have the problem.  I will investigate this myself but providing it here for the benifit of those following this question.

Edit to provide screenshot of time synchronization task history...
Comment: What is the value of the `SpecialPollInterval when executing ` `Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient`?
Comment: Please also show us the history of the SynchronizeTime task (\Microsoft\Windows\Time Synchronization\SynchronizeTime)
Comment: SpecialPollInterval 3600 (or 0x0000e10)

There is no 'TimeSynchronization' folder under '\Microsoft\Windows'   Are you refering to the registry still?
Comment: No, to the task.
Answer: "Free-running System" or "Local CMOS Clock" = not synchronizing with the domain hierarchy.  You may want to enable w32tm debug logging to get more information.  Also check the domain controller it is synchronizing with, if it is having a w32tm issue/not advertising, it will not be able to sync clients.
w32tm /debug /enable /file:c:\Windows\debug\w32time.log /size:10000000 /entries:0-116

Enable Windows Time Service Debug Logging
http://technet.microsoft.com/en-us/library/cc816838%28v=ws.10%29.aspx
Answer: The Windows Time Service wasn't designed nor intended for such accuracy.
http://support.microsoft.com/kb/939322
http://blogs.technet.com/b/askds/archive/2007/10/23/high-accuracy-w32time-requirements.aspx
Comment: It's difficult to accept this.  In all the years and on all the virtual machines I look after, the time has never drifted by more than a second on correctly configured machines, until this one.
Comment: It may be difficult to accept, but based on my understanding of the Windows Time Service, and according to the links I provided, that is the way it is. You can jump through however many hoops you like to "resolve" it but the fact is that it wasn't designed or intended to be as accurate as you or I would like it to be.
Comment: So there must be something else at play here.  I am not saying you are incorrect.  I am asking why this isn't a problem for the many other vms I've looked after for years.
Comment: I just ran w32tm /query /status and I get 'Free running clock' for the source, whereas I get a domain controller's hostname when I run this command on other servers.
Comment: I'm neither correct or incorrect. I'm simply passing on the information from Microsoft, as shown in the two links in my answer.
Comment: Wasn't saying you are incorrect/correct.  All I'm doing is sticking to my guns that this isn't how it 'should' be in my case :)   No hard feelings.  Even though Microsoft say the w32time service isn't meant to provide time accuracy to this level, in my experience a member server set to get its time from a domain controller (which itself gets its time from a reliable public time service) generally stays around a tenth of a second of the domain controller's time.

Domain member server's time is drifting