PostgreSQL: FOREIGN KEY/ON DELETE CASCADE

Question: I have two tables like here:
DROP   TABLE  IF EXISTS schemas.book;
DROP   TABLE  IF EXISTS schemas.category;
DROP   SCHEMA IF EXISTS schemas;
CREATE SCHEMA schemas;

CREATE TABLE schemas.category (
  id          BIGSERIAL PRIMARY KEY,
  name        VARCHAR   NOT NULL,
  UNIQUE(name)
);

CREATE TABLE schemas.book (
  id          BIGSERIAL PRIMARY KEY,
  published   DATE      NOT NULL,
  category_id BIGINT    NOT NULL REFERENCES schemas.category
                            ON DELETE CASCADE
                            ON UPDATE CASCADE,
  author      VARCHAR   NOT NULL,
  name        VARCHAR   NOT NULL,
  UNIQUE(published, author, name),
  FOREIGN KEY(category_id) REFERENCES schemas.category (id)
);

So the logic is simple, after user removes all book under category x, x gets removed from cats, i tried method above but doesn't work, after i clean table book, table category still populated, what's wrong?
Comment: Yes, see [crosspost](http://stackoverflow.com/questions/14161212/how-to-create-one-table-which-shares-common-id-with-another-and-which-row-gets-r/14161296#comment19641520_14161296)
Comment: Does this answer your question? [SQL ON DELETE CASCADE, Which Way Does the Deletion Occur?](https://stackoverflow.com/questions/13444859/sql-on-delete-cascade-which-way-does-the-deletion-occur)
Answer: A foreign key with a cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete.
You are saying in a opposite way, this is not that when you delete from child table then records will be deleted from parent table.
UPDATE 1:

ON DELETE CASCADE option is to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behaviour of the database server prevents you from deleting data in a table if other tables reference it.
If you specify this option, later when you delete a row in the parent table, the database server also deletes any rows associated with that row (foreign keys) in a child table. The principal advantage to the cascading-deletes feature is that it allows you to reduce the quantity of SQL statements you need to perform delete actions.
So it's all about what will happen when you delete rows from Parent table not from child table.
So in your case when user removes entries from categories table then rows will be deleted from books table.
Comment: There is no tree structure in SQL. You should really try to rewrite this answer in terms of references, not "parents" nor "children".
Comment: It may be incorrect terminology (none of you have provided a better alternative) but it helps understand what's meant
Comment: There *are* parent tables in the OO-inheritance aspects of postgres, but this is even more reason the answer should avoid parent/child terminology and describe *references*.
Comment: The terminology is ambiguous. Both the parent and child class of a hierarchical structure could be holding the FK constraint, so this terminology does not help understanding which deletion causes the other deletion. It would be better if you call them "the table where the constraint is defined" (or the "owner" of the relation) and "the referenced table".
Comment: I'm pretty convinced @Mari understands what CASCADE specifies, but this is an incorrect answer in terms of terminology. As hinted above, in inherited tables children are always deleted when the (same) row is deleted from the parent table. As a side note I guess that the default behavior of the server would be specific to the actual server. In Postgres the default behavior is RESTRICT.
Comment: The provided alternative can indeed be found in another answer. @user12207064
Comment: Thank youu for the answer after 9 years!
Answer: Excerpt from PostgreSQL documentation:

Restricting and cascading deletes are the two most common options. [...] CASCADE specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well.

This means that if you delete a row in schemas.category, referenced by category_id in schemas.books, any such referencing row will also be deleted by ON DELETE CASCADE.
Example:
CREATE SCHEMA shire;

CREATE TABLE shire.clans (
    id serial PRIMARY KEY,
    clan varchar
);

CREATE TABLE shire.hobbits (
    id serial PRIMARY KEY,
    hobbit varchar,
    clan_id integer REFERENCES shire.clans (id) ON DELETE CASCADE
);

DELETE FROM clans will CASCADE to hobbits by REFERENCES.
sauron@mordor> psql
sauron=# SELECT * FROM shire.clans;
 id |    clan
----+------------
  1 | Baggins
  2 | Gamgi
(2 rows)

sauron=# SELECT * FROM shire.hobbits;
 id |  hobbit  | clan_id
----+----------+---------
  1 | Bilbo    |       1
  2 | Frodo    |       1
  3 | Samwise  |       2
(3 rows)

sauron=# DELETE FROM shire.clans WHERE id = 1 RETURNING *;
 id |  clan
----+---------
  1 | Baggins
(1 row)

DELETE 1
sauron=# SELECT * FROM shire.hobbits;
 id |  hobbit  | clan_id
----+----------+---------
  3 | Samwise  |       2
(1 row)

If you really need the opposite (checked by the database), you will have to write a trigger!
Comment: this has been the explanation that finally made me understand what this REFERENCES / CASCADE syntax actually means, much appreciated!
Answer: In my humble experience with postgres 9.6, cascade delete doesn't work in practice for tables that grow above a trivial size.

Even worse, while the delete cascade is going on, the tables involved
are locked so those tables (and potentially your whole database) is
unusable.
Still worse, it's hard to get postgres to tell you what
it's doing during the delete cascade.  If it's taking a long time,
which table or tables is making it slow?  Perhaps it's somewhere in
the pg_stats information?  It's hard to tell.
Comment: That is the definition of working, in a database with proper transactions. Secondly, this is not an answer to the question.
Comment: In case someone stumbles upon this: probably you have overlooked this explanation in the documentation, at the end of 5.3.5:
"Since a DELETE of a row from the referenced table or an UPDATE of a referenced column will require a scan of the referencing table for rows matching the old value, it is often a good idea to index the referencing columns too."
https://www.postgresql.org/docs/9.5/ddl-constraints.html
Answer:
PostgreSQL Forging Key DELETE, UPDATE CASCADE

CREATE TABLE apps_user(
  user_id SERIAL PRIMARY KEY,
  username character varying(30),
  userpass character varying(50),
  created_on DATE
);

CREATE TABLE apps_profile(
    pro_id SERIAL PRIMARY KEY,
    user_id INT4 REFERENCES apps_user(user_id) ON DELETE CASCADE ON UPDATE CASCADE,
    firstname VARCHAR(30),
    lastname VARCHAR(50),
    email VARCHAR UNIQUE,
    dob DATE
);

Comment: This cut-and-paste is a poor example with no relation to the question, and the most important keyword is swapped for a random that kind of sounds the same.
Comment: to be fair, 'FORGING' sort-of gives it away.


matplotlib markers / mask on image pixels

Question: So I have an image and I have a pixel mask for that image, where the mask is the same size as the image and contains values of 0 and 1, where if it is 0 I don't want to modify the image, and if it is 1 I want to add a transparent color over that pixel of the image.
Basically I want to highlight certain segments of the image but still see what is underneath.
Now I have searched high and low but haven't found a simple way to do this. I used np.where with the mask to get the pixel locations of the 1's to use with the plot functions. I first tried scatter plots with a small marker size and no edge color (small scatter plot markers in matplotlib are always black), but the markers are not one image pixel in size, they seem to be an absolute size and so depending on the size of the figure the transparency is affected and weird patterns are created from the overlapping markers.
Just the regular pyplot plot function created the exact look I desired (where the coloring was smooth and invariant to figure size) but it also colored horizontal connections between disjoint segments in the mask (since it is drawing lines I guess), so I couldn't use that.
What worked the best was patches, which I came across in this question: (How to set a fixed/static size of circle marker on a scatter plot?). I found that rectangular patches with width and height of 1 gave me the exact desired effect, where I could put a transparent color over certain pixels of the image. However this proved to produce a ton (tens of thousands) of rectangles for certain images, and so it was quite slow. Even when using a PatchCollection instead of calling addPatch every time it was still slow.
Now I can probably just join adjacent rectangles to reduce the number of things needing to be drawn, but I was just wondering if there was an easier way to do this?
Thanks.
Answer: You can do a semitransparent overlay either using masked arrays or by setting the alpha values in an RGBA image.  Here are both worked through (using the example of three semitransparent red squares placed over a circular pattern), and they give similar images (so I'll only show one):

from pylab import *
from numpy import ma

x = y = linspace(-6, 6, 100)
X, Y = meshgrid(x, y)

z3 = X*X + Y*Y  # circular pattern

# first, do this with a masked array
figure()
# z4 = 3 diagonal square
# zm = a uniform image (ones), with a mask of squares (~z4)
z4 =  np.repeat(np.repeat(eye(3, dtype=bool), 40, axis=0), 40, axis=1)
zm = ma.masked_where(~z4, ones((120,120)))
imshow(z3, cmap=cm.jet)
imshow(zm, cmap=cm.bwr, alpha=.3, vmin=0, vmax=1) #cm.bwr is an easy way to get red

# do this by changing alpha for each pixel
figure()
z5 = zeros((120, 120, 4), dtype=float)
z5[..., 0] = 1
z5[..., 3] = .4*z4.astype(float)
imshow(z3, cmap=cm.jet)
imshow(z5)

show()

I think both approaches can produce the same results for all cases, but:
1. the masked arrays can be a more direct approach if the mask or composition becomes complicated, and masking gives you more flexibility in drawing your overlay image since, for example, you can use colormaps rather than specifying the full RGBA for every pixel, but,
2. the masked array approach doesn't give full pixel-by-pixel control over the alpha value like RGBA does.

z1 = sin(X*Y)
z1 = cos(2*X)
z2 = cos(5*(X+Y))

zm = ma.masked_where( (z2<.5) & (Y>0), z1)

figure()
imshow(z3)
imshow(zm, cmap=cm.gray, alpha=.4, vmin=-2, vmax=2)
show()

It's a bit crazy, but here's what's going on:  The primary image is a circular pattern that goes from blue to red (z3).  Then there are vertical bars that faintly shade this (z1) but only in half of the figure and in narrow alternate diagonal bands on the other half (due to the mask).  Here's a more complicated image using masked arrays:
Comment: Sorry I am a bit confused on how to use this. So if I had a 800x800 background image, and I have a mask that contains 3 20x20 squares of 1's and everything else is 0, how would I use this to plot those 3 squares as semi-transparent red boxes over the image?
Answer: Just to add on to what tom10 has posted, the masked arrays do work great with colormaps, but I also wrote a small function in the meantime that should work with any RGB color tuple.
def overlayImage(im, mask, col, alpha):
    maskRGB = np.tile(mask[..., np.newaxis], 3)
    untocuhed = (maskRGB == False) * im
    overlayComponent = alpha * np.array(col) * maskRGB
    origImageComponent = (1 - alpha) * maskRGB * im
    return untocuhed + overlayComponent + origImageComponent

im is the rgb image
mask is a boolean mask of the image, such that mask.shape + (3,) = im.shape
col is just the 3-tuple rgb value you want to mask the image with
alpha is just the alpha value / transparency for the mask
Answer: I also needed a clear contour on my areas. Thus, you can easily add a contour plot on top: e.g., create a dummy numpy array and set a different value in each area of interest.
Here's an example build on top of tom10's answer with a different condition:
x = y = linspace(-6, 6, 100)
X, Y = meshgrid(x, y)

z3 = X*X + Y*Y  # circular pattern

# first, do this with a masked array
figure()
imshow(z3, cmap=cm.jet, extent = (-6,6,-6,6));
zm = ma.masked_where((z3>=0.7) & (z3<=1.5), ones(np.shape(z3)));
imshow(zm, cmap=cm.bwr, alpha=.4, vmin=0, vmax=1, extent = (-6,6,-6,6)) #cm.bwr is an easy way to get red
# Build dummy array of 1s and 0s (you can play with different values to obtain different contours for different regions):
temp_vector = ones(np.shape(z3));
temp_vector[(z3>=0.7) & (z3<=1.5)] = 0.0;
temp_vector[(z3>8.2)] = 2.0; # etc.
# Create contour. I found only one contour necessary:
contour(X, Y, temp_vector, 1, colors=['r','g']);

show()

Which yields:


Conditions for convergence of $\sum\limits_{n=1}^\infty{a^nf(n)}$

Question: assume $a>0$, and for all $n$ we have $0 \leq f(n) \leq 1$.
Is there a necessary and sufficient condition on the series $f(n)$ for which
$\sum\limits_{n=1}^\infty{a^nf(n)}<\infty$  ?
Thanks!
Comment: $f(n) \to 0$ does not imply convergence of this series. It would if $a<1$.
Answer: Almost necessary and sufficient condition for the convergence of this series is provided by the root test: Let $r=\limsup_{n\to\infty} \lvert\,f(n)\rvert^{1/n}$. Then
$$
\sum_{n=0}^\infty a^nf(n),
$$
converges if $r\lvert a\lvert<1$ and diverges if $r\lvert a\lvert>1$.
It is almost necessary and sufficient condition as it does not tell us anything about the case $r\lvert a\lvert=1$.
Answer: The most straightforward sufficient condition we'd usually think of is to have that $a < 1$ - then as $0 \leq f(n) \leq 1$ for all $n$, it follows that we have that
$$ \sum_{n=1}^\infty a^n f(n) \leq \sum_{n=1}^\infty a^n < \infty \text{ as } a < 1,$$
so by the comparison test the series converges. However, this condition isn't necessary; for example, if we take $f(n) = a^{-2n}$, then the series converges for all $a > 1$.
Generally, when it comes to do with results regarding convergence of infinite summations, they are rarely both necessary and sufficient (or if they are, they require some strict condition on the terms being summed, such as monotonicity); for example, if we were considering a series $\sum_{n=1}^\infty g(n)$, then $g(n) \to \infty$ as $n \to \infty$ is a necessary but not sufficient condition for convergence of the series.
While there are some tests, such as the ratio and root tests, for convergence of series, they leave the possibility of giving inconclusive results, even for these restricted types of series (this boils down to the limit $L$ in the ratio and root tests having some dependence on $a$ which cannot avoid the $L = 1$ indeterminate case).
In some sense, one can prove for general series there is no "universal" test for convergence. While I'm not too familiar with the proof, I wouldn't be surprised if this held for this class of series also, so for an answer to your question, I'd say that the answer is probably no.


Como deixar a chave primária e estrangeira de uma tabela com a constraint NOT NULL no PostgreSQL?

Question: Estou criando chaves primárias e estrangeiras com  a constraint NOT NULL no PostgreSQL, só o jeito que eu estou fazendo isso é indo nas propriedades na tabela e definindo manualmente as chaves como NOT NULL, eu gostaria de fazer isso através dos códigos de criação da tabela.
Logo abaixo está algumas tabelas do meu banco para que possam entender e me ajudar nessa minha dúvida
CREATE TABLE endereco_fornecedor (
  cd_endereco_fornecedor SERIAL PRIMARY KEY,
  cidade VARCHAR(30),
  bairro VARCHAR(30),
  endereco VARCHAR(30),
  numero_endereco INT
)

CREATE TABLE telefone_fornecedor (
  cd_telefone_fornecedor SERIAL PRIMARY KEY,
  telefone_fornecedor VARCHAR(15)
)

CREATE TABLE fornecedor (
  cd_fornecedor SERIAL PRIMARY KEY,
  cd_endereco_fornecedor INT,
  cd_telefone_fornecedor INT,
  cnpj_fornecedor VARCHAR(18),
  nome_fornecedor VARCHAR(30),
  email_fornecedor VARCHAR(50),
  foreign key (cd_endereco_fornecedor) references endereco_fornecedor (cd_endereco_fornecedor),
  foreign key (cd_telefone_fornecedor) references telefone_fornecedor (cd_telefone_fornecedor)
)

Comment: Você deseja que os campos cd_endereco_fornecedor e cd_telefone_fornecedor não possam ser nulos?
Comment: Sim, ```NOT NULL``` para todas as chaves.
Answer: Para não permitir valores nulos, na criação do campo, basta informar not null, exemplo:
CREATE TABLE fornecedor (
  cd_fornecedor SERIAL PRIMARY KEY,
  cd_endereco_fornecedor INT not null,
  cd_telefone_fornecedor INT not null,
  cnpj_fornecedor VARCHAR(18),
  nome_fornecedor VARCHAR(30),
  email_fornecedor VARCHAR(50),
  foreign key (cd_endereco_fornecedor) references endereco_fornecedor (cd_endereco_fornecedor),
  foreign key (cd_telefone_fornecedor) references telefone_fornecedor (cd_telefone_fornecedor)
);

Veja online: http://sqlfiddle.com/#!17/4b6e9

Caso deseje outros campos com tal condição, basta seguir a sintaxe:
nome_do_campo tipo_do_campo not null

Documentação: https://www.postgresql.org/docs/9.4/ddl-constraints.html
Comment: ```NOT NULL``` é uma constraint que funciona no MySQL, e não no PostgreSQL
Comment: @Iury Hittalo A própria documentação do Postgres tem esse exemplo e o link do exemplo online testa em um banco Postgres.
Comment: @Iury: Como não funciona no PostgreSQL???!!!???
Comment: Está gerando algum erro? Se sim, qual?
Comment: Veja que funcionou no meu postgres local também: https://i.stack.imgur.com/uc9c0.png
Comment: @DanielMendes desculpe erro meu, isso funciona bem funciona bem.
Comment: @DanielMendes sua resposta é boa, mais toda vez que precisa inserir dados em uma tabela com chaves estrangeiras vai ser sempre necessário inserir os dados da chave estrangeira manualmente.
Answer: 1 - Se você criar as contraints PK , o campo já se torna NotNull por default.
2 - Você pode criar um Check :
CHECK (column_name IS NOT NULL).
Answer: Outra maneira de fazer isso é usando o SERIAL nas chaves estrangeiras, assim elas nunca ficam nulas e implementam automaticamente sem precisar inserir os valores da chave.
CREATE TABLE endereco_fornecedor (
  cd_endereco_fornecedor SERIAL PRIMARY KEY,
  cidade VARCHAR(30),
  bairro VARCHAR(30),
  endereco VARCHAR(30),
  numero_endereco INT
)

CREATE TABLE telefone_fornecedor (
  cd_telefone_fornecedor SERIAL PRIMARY KEY,
  telefone_fornecedor VARCHAR(15)
)

CREATE TABLE fornecedor (
  cd_fornecedor SERIAL PRIMARY KEY,
  cd_endereco_fornecedor SERIAL,
  cd_telefone_fornecedor SERIAL,
  cnpj_fornecedor VARCHAR(18),
  nome_fornecedor VARCHAR(30),
  email_fornecedor VARCHAR(50),
  FOREIGN KEY (cd_endereco_fornecedor) REFERENCES endereco_fornecedor (cd_endereco_fornecedor),
  FOREIGN KEY (cd_telefone_fornecedor) REFERENCES telefone_fornecedor (cd_telefone_fornecedor)
)

Comment: Do jeito que fiz não é uma boa prática, pois a chave estrangeira sempre vai ser incrementada e pode ocorrer erros.


From the given figure, prove that $[\triangle ABC]=[\triangle CDE]$

Question: From the given figure, prove that $\triangle ABC=\triangle CDE$ in area.

In $\triangle ABC$ and $\triangle CDE$
1. $AB=CD$
2. $BC=DE$
3.??
Comment: @ÉvaristeGalois The title of the question is misleading.
Comment: The triangles can not be equal (unless $\alpha=\theta=90^\circ$ of course). Did you mean they have the same area, maybe?
Comment: It is true, even if $\alpha\neq 90\neq \theta$. The pictures are badly drawn considering $\alpha+\theta=180$
Comment: @dxiv,  why?  Any strong reason?
Comment: @dxiv No, it's true. See my answer.
Answer: Hint: The area of a triangle $ABC$ is $\frac{1}{2}ab\sin(\angle C)$. The result follows from the fact that $\sin(180^\circ - \theta) = \sin(\theta)$ for all $\theta$.
Comment: Hey..I have a question....If $\theta+\alpha=180$ then ...$\theta=180-\alpha$...taking $\sin$ on both sides gives $\sin(\theta)=\sin(\alpha)$Using your fact...applying $\arcsin$ now gives $\theta=\alpha$ ..SO every angle is of this form is $90$? or is this due to $2\pi$ periodicity?
Comment: @user35508 No. The reason this isn't true is that $f(x) = f(y)$ doesn't imply $x=y$ for every function $f$. For instance, $-2 \not= 2$ but $(-2)^2 = 4 = 2^2$. It's the same principle with $\arcsin$.
Answer: The given triangles can be "glued together" in the following way:

It was not a wise choice to use twice the $C$ letter, but in any case the original triangles are parts of a larger triangle split through a median, and
$$ \text{same base} + \text{same height}\quad \Longrightarrow\quad \text{same area}.$$
Answer: It is easy to see with a picture; simply stick the triangles together by a side that is the same length, and see (where the two green sides are the same and the two blue sides are the same):

It is now trivial that the area is equal, since their base and height are equal.


Issue on Adding Markers to Google Map From json Using Ajax and jQuery

Question: I am trying to add some Markers from a json file by using following code but it is not working!
Here you can see a running DEMO
var map;
$(document).ready(function () {
  var latlng = new google.maps.LatLng(11.818965,123.727169);
  var myOptions = {
        zoom: 0,
        center: latlng,
        disableDefaultUI: true,
        scaleControl: true,
        zoomControl: true,
        zoomControlOptions: {
          style: google.maps.ZoomControlStyle.SMALL
        },
        mapTypeControl:true,
        draggableCursor: 'move',
        mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  // map.setOptions({styles: styles}); // styles is not defined

  $.ajax({
        type: "POST",
        url: 'http://supermobile.dk/html5/webApp.GMAP/json/demo.txt',
        dataType: 'json',

        success: function(data){
          locations.length = 0;
          for (p = 0; p < data.length; p++) {
            locations.push(Array(data[markers].latitude,data[markers].longitude));
          }
        }
  });
});

Can you please let me know what i doing wrong here? thanks
Update:
<!DOCTYPE html>

Answer: If you want markers to  appear on  your map need to create them and add them to the map.
  $.ajax({
    type: "POST",
    url: 'http://supermobile.dk/html5/webApp.GMAP/json/demo.txt',
    dataType: 'json',

    success: function(data){
      locations.length = 0;
      for (p = 0; p < data.markers.length; p++) {
        locations.push(Array(data.markers[p].latitude,data.markers[p].longitude));
        // To add the marker to the map, use the 'map' property
        var marker = new google.maps.Marker({
          position: new google.maps.LatLng(data.markers[p].latitude,data.markers[p].longitude),
          map: map,
          title:"marker "+p
        });
      }
    }
  });

working fiddle (with hardcoded data)
working online example


C# Roslyn to use previously compiled class in next compilations

Question: I ask with example,
Lets say I have the following code.
fullcommand = @"public class oldTest
{
    public static void oldTestMethod(){
        Console.WriteLine(""oldTest Class"");
    }
}"

var syntaxTree = CSharpSyntaxTree.ParseText(fullCommand);
var compilation = CSharpCompilation.Create(
                assemblyName,
                new[] {syntaxTree},
                references,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,allowUnsafe:true));

var ms = new MemoryStream();
var result = compilation.Emit(ms);

And I will compile the above code with Roslyn in memory.
next i want to compile another code in memory to use the above compiled class, lets say the below.
new_fullcommand = @"public class newTest
    {
        public static void newTest(){
            oldTest.oldTestMethod();
        }
    }"

    var syntaxTree = CSharpSyntaxTree.ParseText(new_fullcommand);
    var compilation = CSharpCompilation.Create(
                    assemblyName,
                    new[] {syntaxTree},
                    references,
                    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,allowUnsafe:true));

    var ms = new MemoryStream();
    var result = compilation.Emit(ms);

how can i make the second code to use the first code as its reference? or use it?
Answer: The easiest way would probably be to pass in multiple syntaxTree objects when you create your compilation.
However, if you want to build up your compilation incrementally I believe you can use Compilation.AddSyntaxTrees to your first compilation object.
Comment: I cant do that, I need to compile SyntaxTrees separately because i am not aware of upcoming codes.
Comment: I found this pretty much relevant, but i cant get it work. https://stackoverflow.com/questions/38646619/how-to-use-a-roslyn-scripting-submission-as-an-assembly-in-other-roslyn-compilat
Any idea how to fix it?


How do I use jQuery code through a module/theme?

Question: How do I use jQuery through a Drupal 8 module?
I'm trying to get some jquery code running on a drupal 8 website. It's a div animation that randomly moves the div within a wrapper.
I've added the JS and CSS files and they're both showing in the source. I've then added the Div ID of 'container' to html.html.twig(yes I copied it into my theme folder first) -
{%
  set body_classes = [
    logged_in ? 'user-logged-in',
    not root_path ? 'path-frontpage' : 'path-' ~ root_path|clean_class,
    node_type ? 'page-node-type-' ~ node_type|clean_class,
    db_offline ? 'db-offline',
    theme.settings.navbar_position ? 'navbar-is-' ~ theme.settings.navbar_position,
    theme.has_glyphicons ? 'has-glyphicons',
    'container',
  ]
%}

Also added a div to use for this. Again in html.html.twig
<js-bottom-placeholder token="{{ placeholder_token|raw }}"> //**Not part of my code just adding it for comparison.

TEST DIV, IF I MOVE I'M WORKING!

So the files show up in the source and the source also shows that the div ID of container has been added.
Here's the jQuery code, keep in mind that I've adjusted it to work with Drupal 8 so there's probably a syntax error or two.
$.extend(Drupal.theme, {
  animateDiv: (function() {
    ($('.fly')); }),

* Almost certain the problem is here ^^^
  makeNewPosition: function ($container) {
    // Get viewport dimensions (remove the dimension of the div)
    var h = $container.height() - 10;
    var w = $container.width() - 10;

    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);

    return [nh, nw];
},

animateDiv: function ($target) {
    var newq = makeNewPosition($target.parent());
    var oldq = $target.offset();
    var speed = calcSpeed([oldq.top, oldq.left], newq);

    $target.animate({
        top: newq[0],
        left: newq[1]
    }, speed, function() {
        animateDiv($target);
    });

},

calcSpeed: function (prev, next) {

    var x = Math.abs(prev[1] - next[1]);
    var y = Math.abs(prev[0] - next[0]);

    var greatest = x > y ? x : y;

    var speedModifier = 0.23;

    var speed = Math.ceil(greatest / speedModifier);

    return speed;

}

});

Here's the CSS code
.fly {
width: 50px;
height:50px;
background-color: red;
position:fixed;

}

That's what I've tried and yet there's still no jQuery activity, I've also tried using different types for class names(.div instead of #div etc) which I think is where the problem is. The flying div shows but isn't being styled and there's no jQuery. What am I doing wrong?
Update: After playing with the code for awhile I've managed to at least get the style to show on the div. jQuery still a no show though..
This isn't the same jquery code but it's similar and it's what I would like, the div floats around the entire page, I've tried using this code as well but I still get the same problem as above. http://jsfiddle.net/Xw29r/15/
Comment: How did you add your jQuery code? Did you create a library?
Comment: I first tried to add it to a template I'm working on, but then attempted to move it to a new module folder but it doesn't want to leave the templates one(I think a cache clear should fix this). In both cases I added the JS code via the libraries.yml file. The JS code gets loaded, I can see it in the pages source when I load it, the div just isn't using it.  Thanks for your help :)
Answer: In Drupal 7 and 8 your JS code needs to be wrapped like this:
(function($) {
Drupal.behaviors.myBehavior = {
  attach: function (context, settings) {

    //custom code goes here

  }
};
})(jQuery);

To test, put the following code inside your JS file.
(function($) {
Drupal.behaviors.myBehavior = {
  attach: function (context, settings) {

    //code starts
    $("body").click(function() {
      alert("Hello World");
    });
    //code ends

  }
};
})(jQuery);

Now when you click in the body, you should get an alert saying hello world.
For more info read the JavaScript API in Drupal
 8
Comment: :D Your little test worked! Just got my code working! You just saved me hours of scratching my head wondering If my losing it. THANK YOU!
Comment: @Krypton no problem `:)` I added a link to the Drupal docs that talks about it.


Substances in WADA's monitoring program high risk?

Question: Caffeine was earlier prohibited but now in the monitoring program. As of 2016, the list contains

"Bupropion, caffeine, nicotine, phenylephrine, phenylpropanolamine, pipradol and synephrine: These substances are included in the 2016 Monitoring Program, and are not considered Prohibited Substances."

What does it mean to be under monitoring? Can I drink coffee before competitions under WADA's supervision or use synephere stimulants?
Comment: As it refers to the definition and applicability of part of a regulation covering sporting competitions, this question is not asking for health or medical advice. It is therefore *not off-topic*.
Answer: USADA does pretty good job in explaining here. There is always a risk that you need to manage and reduce the potential health problems.

High Risk List, reduce risk and Supplement411.org
USADA contact
Determine If You Need a Therapeutic Use Exemption (TUE)
Comment: This is a link-only answer. Please quote the relevant material or statements from your sources, to avoid linkrot and to ensure the answer is self-contained.


GPO, UAC, and Active X not playing nice

Question: I have a set of users that I want to be able to install ActiveX controls from websites. I have deployed a GPO that allows them to do this, however I still fail when it comes to UAC. The ActiveX controls install, but then require administrative priv to run.  Is there a way around this? currently I have those users in the local administrator group because its the only way I can see around it.
Answer: You can try pre-approving the needed ActiveX controls using ActiveX Installer Service group policy. Unfortunately, this is something that you would have to enable before you create your OS image, since it is not installed in Vista by default. As an alternative, you can enable it manually via Programs and Features -> Windows Components on each workstation, but this is not feasible for a large network.
Details here:
technet.microsoft.com/en-us/library/cc721964(WS.10).aspx
Basically you whitelist the sites that users are allowed to install ActiveX controls from, thus allowing them to install them without the UAC elevation. Maintaining a white list may seem like a tedious process, but IMHO it's much better than letting users install whatever they want. By giving them admin access you're asking for all kinds of toolbars and Bonzie Buddies to appear on your network :).

GPO, UAC, and Active X not playing nice


Curl of a vector field cross itself

Question: How we can use the property  that
$$A×(B×C) = B(A.C)- C(A.B)$$
to prove the relation:
$$a×(∇×a) = ∇ (a^2/2) -(a.∇)a.$$
When I use it, the result directly appear to be
$$∇(|a|^2 )-(a.∇)a$$ instead of the correct one. Any help please?
Comment: $\nabla$ is not a regular vector. That identity is only valid for vectors.
Comment: Have a look at my answers to [this question](http://math.stackexchange.com/questions/1381381/showing-that-nabla-times-nabla-times-veca-nabla-nabla-cdot-veca-de/1381415#1381415) and [this question](http://math.stackexchange.com/questions/1381155/how-to-differentiate-the-following-interesting-vector-product/1381226#1381226), both of which show a simple but powerful method that helps to prove vector identities, such as the one you want, in just a few lines.
Comment: Don't forget that derivative of a product requires a product rule.
Comment: Yes ok. But any other way to prove the above formula ?
Comment: From the title, and from the identity you wish to apply, it seems that where it says $\nabla a$ you meant $\nabla\times a$?
Comment: No, ∇a is the gradient of the vector a
Comment: Actually i need to prove the last formula apart from the way used
Comment: I see now that this was an error introduced through an edit by another user. I'm not sure why you supported that error when you'd originally posted the relation correctly, but never mind. You posted the curl, and the question only makes sense with the curl, so let's assume you meant the curl.
Comment: The user who introduced the mistake had tried to correct it, but whereas the erroneous edit was approved, the correction was rejected. I corrected it now; if you agree, we can now remove all these comments to reduce the clutter.
Answer: The problem arises because vector identites are usually derived for "normal" vectors, where the order of the vectors either doesn't matter or, in the case of the cross product, merely affects the sign. If one of the "vectors" is a differential operator, these vector identities still hold in a certain sense and can be very useful, but need to be applied with care, since the differential operator must always operate on what originally came after it and never on what originally came before it – this must be taken into account when the vector identity being applied changes the order of the vectors.
Naively applying the double cross product identity yields
$$
a\times(\nabla\times a)=\nabla(a\cdot a)-a(a\cdot \nabla)\;.
$$
Both terms are wrong – the first because $\nabla$ is now acting on both instances of $a$ whereas it should only be acting on one, as on the left-hand side, and the second because $\nabla$ is at the very right and is no longer acting on anything. The second problem is readily corrected by switching the order of multiplication. The first problem isn't quite as easy to correct. The actions of $\nabla$ on both instances of $a$ yield the same result, so if we only wanted one of them, we can correct for having both by dividing by two. Applying both corrections yields
$$
a\times(\nabla\times a)=\frac12\nabla(a\cdot a)-(a\cdot \nabla)a\;.
$$
If you intially find these things confusing, it often helps to write them out in coordinates – that makes them seem less mysterious, and over time you'll develop a feel for how the vector identities, applied with care, summarize what you would automatically have done correctly in coordinates.
Comment: That is an exceedingly clear explanation. Kudos!
Comment: @israasharbeh: For a scalar $y$, we have $\frac{\mathrm d}{\mathrm dx} (y\cdot y) = \frac{\mathrm dy}{\mathrm dx}y+y\frac{\mathrm dy}{\mathrm dx}=2y\frac{\mathrm dy}{\mathrm dx}$, so $y\frac{\mathrm dy}{\mathrm dx}=\frac12\frac{\mathrm d}{\mathrm dx} (y\cdot y)$. The factor $2$ arises because the differential operator acts on both instances of $y$. We're doing the same thing here, the only difference being that we can't explicitly write the equivalent of $y\frac{\mathrm dy}{\mathrm dx}$ in the vector case because our vector notation won't allow it.
Comment: If it's still not clear, I suggest that before asking further questions you write it out in components, make sure you understand it in components (where the problem with the vector notation doesn't arise), and then try to see how the vector version corresponds to what you understand in components.
Comment: It's all about how our vector notation and our notation for differential operators interact; keeping clear in your mind what aspects of the ordering relate to the requirements of the vector notation, which aspects of the ordering relate to the requirements of the differential operator notation, and writing things such that both sets of requirements are fulfilled.
Comment: Thx  now it is clear
Comment: Easily take one of that ∇(a.a) ? this idea is not obvious. Please demonstrate further.
Answer: Using tensor notation we  have
\tag
$$\begin{align}
\vec a\times \nabla \times \vec a&=(\hat x_ia_i)\times (\hat x_j\partial_j \times \hat x_ka_k)\\\\
&=(\hat x_j\delta_{ik}-\hat x_k\delta_{ij})a_i\partial_j(a_k)\tag 1 \\\\
&=\hat x_j(a_i)\partial_j(a_i)-\hat x_ka_i\partial_i(a_k)
\tag2 \\\\
&=\hat x_j\frac12\partial_j(a_i^2)-a_i\partial_i(\hat x_ka_k)
 \\\\
&=\frac12\nabla |\vec a|^2-(\vec a\cdot \nabla) \vec a
\end{align}$$
In arriving at $(1)$, we used the vector triple product rule. Note that $\delta_{ij}$ is the Kronecker Delta where $\delta_{ij}=1$ for $i=j$ and $0$ otherwise.
In going from $(1)$ to $(2)$, we used the sifting property of the Kronecker Delta.
Comment: Thx it seems to be simple
Comment: Yes, surely :) Dr.MV's
Comment: Please let me know how I can improve my answer. I really want to help and give you the best answer I can.

Curl of a vector field cross itself


Cisco Extended ACL on 2811 Not Working Properly

Question: I am almost positive this is an issue of missing a key component or not declaring/applying the ACL properly, but I am unable to figure out the fix on my own. What I am trying to do is only allow PC2 to send any traffic to PC3 and PC1. Essentially PC4 should be inaccessible to PC2.
Since I am unable to post images yet, I will try to explain the topology which is very simple. PC1 and PC2 are behind SWITCH1 and SWITCH1 is connected to ROUTER0 on port f0/1. On the left side PC3 and PC4 are behind SWITCH2 and SWITCH2 is connected to ROUTER0 on port f0/0. The IPs are as follows:

PC1 18.104.22.168/8  & Connected to f0/1 on SWITCH1
PC2 22.214.171.124/8  & Connected to f0/2 on SWITCH1
PC3 10.0.0.2/8  & Connected to f0/1 on SWITCH2
PC4 10.0.0.3/8  & Connected to f0/2 on SWITCH2
ROUTER0 f0/0 10.0.0.1/8  & Connects to f0/24 on SWITCH2
ROUTER0 f0/1 10.0.0.2/8  & Connects to f0/24 on SWITCH1

The ACL as it exists now is as follows:
ip access-list extended NSL1
 permit ip host 126.96.36.199 host 10.0.0.2

The problem is that devices on the left side of the topology (PC1 & 2) can no longer ping 22.214.171.124 or 10.0.0.1 when they were able to prior to implementing the ACL. It gives the "Destination host unreachable." error. PC1 also cannot ping anything on or to the right of the router, but I know that is because I have not put a permit statement in for it yet. Any help would be greatly appreciated. I would think it should be a simple fix, but I don't know since I don't have much experience with Cisco IOS.
Below is the full running-config of ROUTER0.
Router>
Router>en
Router#sh run
Building configuration...

Current configuration : 579 bytes
!
version 12.4
no service timestamps log datetime msec
no service timestamps debug datetime msec
no service password-encryption
!
hostname Router
!
!
!
!
!
!
!
!
!
!
!
!
!
!
spanning-tree mode pvst
!
!
!
!
interface FastEthernet0/0
  ip address 10.0.0.1 255.0.0.0
  duplex auto
  speed auto
!
interface FastEthernet0/1
  ip address 18.104.22.168 255.0.0.0
  ip access-group NSL1 in
  duplex auto
  speed auto
!
interface Vlan1
  no ip address
  shutdown
!
ip classless
!
!
ip access-list extended NSL1
 permit ip host 18.104.22.168 host 10.0.0.2
!
!
!
!
!
line con 0
 line vty 0 4
 login
!
!
!
end

Comment: What's the source IP of the ICMP unreachable response?
Comment: Both 184.108.40.206 and 18.104.22.168 cannot ping 18.104.22.168 or 10.0.0.1
Comment: I mean when you get an Unreachable message in your ping output, which should tell you what device decided it was unreachable.  What IP is shown?
Comment: Oh apologies. It shows the IP of whatever I am trying to ping. Ex. If I use PC1(22.214.171.124) to ping 188.8.131.52 it returns "Reply from 126.96.36.199: Destination host unreachable."
Answer: Your access list is specifying 22.214.171.124 as the source and 10.0.0.2 as the destination - other traffic ingressing on the f0/1 interface will not be allowed (and I think it's a little strange that they're sending ICMP Unreachables instead of just dropping the packet, but there you have it).
If you want to allow pings to the router's IPs, you'll also need to put those in the ACL:
ip access-list extended NSL1
 permit ip host 184.108.40.206 host 10.0.0.2
 ! let it communicate with the other interface's routed IP:
 permit ip host 18.104.22.168 host 10.0.0.1
 ! let's just allow it to hit anything else in its subnet; might as well, right?
 permit ip host 188.8.131.52 220.127.116.11 0.255.255.255

Note that only 188.8.131.52 will be able to ping now with this ACL; you'd need to allow 18.104.22.168 explicitly as well.
Comment: That worked perfectly! Thanks so much for explaining it. I guess I didn't quite understand how the ACLs worked.


Causal effect by back-door and front-door adjustments

Question: If we wanted to calculate the causal effect of $X$ on $Y$ in the causal graph below, we can use both the back-door adjustment and front-Door adjustment theorems, i.e.,
$$P(y | \textit{do}(X = x)) = \sum_u P(y | x, u) P(u)$$
and
$$P(y | \textit{do}(X = x)) = \sum_z P(z | x) \sum_{x'} P(y|x', z)P(x').$$
Is it an easy homework to show that the two adjustments lead to the same causal effect of $X$ on $Y$?
Comment: Hi Knarpie, it is a part of self-study and not a homework. I am currently reading "Causal Inference in Statistics" by Pearl et al. and spend about 1 hour pondering over the question I asked above, as it is a natural question to ask, but couldn't show the equality. Either I am missing something here, or the two expressions are not equal.
Comment: Is this a real homework? Then please add the self-study tag. Then people may give you hints, leaving the thinking (and learning) to you. Tell us what you tried and where you are stuck. Remember CV is not for outsourcing homework...
Comment: BTW Pearl's "The Book of Why: The New Science of Cause and Effect" has a chapter on front-door adjustment offering a more intuitive understanding of the math.
Answer: The action $do(x)$ corresponds to an intervention on variable $X$ that sets it to $x$.  When we intervene on $X$, this means the parents of $X$ do not affect its value anymore, which corresponds to removing the arrows pointing to $X$.So let's represent this intervention on a new DAG.

Let's call the original observational distribution $P$  and the post-intervention distribution $P^*$. Our goal is to express $P^*$ in terms of $P$. Notice that in $P^*$ we have that $U \perp X$. Also, the pre interventional and post interventional probabilities share these two invariances: $P^*(U) = P(U)$ and $P^*(Y|X, U) = P(Y|X, U)$ since we did not touch any arrow entering those variables in our intervention. So:
$$
\begin{aligned}
P(Y|do(X)) &:= P^*(Y|X)  \\
           &=\sum_{U}P^*(Y|X, U)P^*(U|X)\\
           &=\sum_{U}P^*(Y|X, U)P^*(U)\\
           &=\sum_{U}P(Y|X, U)P(U)
\end{aligned}
$$
The derivation of the front door is a bit more elaborate. First notice that there's no confounding between $X$ and $Z$, hence,
$$P(Z|do(X)) = P(Z|X)$$
Also, using the same logic for deriving $P(Y|do(X))$ we see that controlling for $X$ is enough for deriving the effect of $Z$ on $Y$, that is
$$P(Y|do(Z)) = \sum_{X'}P(Y|X', Z) P(X')$$
Where I'm using the prime for notation convenience for the next expression. So these two expressions are already in terms of the pre-intervention distribution, and we simply used the previous backdoor rationale to derive them.
The last piece we need is to infer the effect of $X$ on $Y$ combining the effect of $Z$ on $Y$ and $X$ on $Z$. To do that, notice in our graph $P(Y|Z, do(X)) = P(Y|do(Z), do(X)) = P(Y|do(Z))$, since the effect of $X$ on $Y$ is completely mediated by $Z$ and the backdoor path from $Z$ to $Y$ is blocked when intervening on $X$. Hence:
$$
\begin{aligned}
P(Y|do(X))  &= \sum_{Z} P(Y|Z, do(X))P(Z|do(X))\\
            &= \sum_{Z} P(Y|do(Z))P(Z|do(X))\\
            &= \sum_{Z} \sum_{X'}P(Y|X', Z) P(X')P(Z|X)\\
            &= \sum_{Z}P(Z|X) \sum_{X'}P(Y|X', Z) P(X')
\end{aligned}
$$
Where $\sum_{Z} P(Y|do(Z))P(Z|do(X))$ can be understood in the following way: when I intervene on $Z$, then the distribution of $Y$ changes to $P(Y|do(Z))$; but I'm actually intervening on $X$ so I want to know how often would $Z$ take a specific value when I change $X$, which is $P(Z|do(X))$.
Hence, the two adjustments give you the same post-interventional distribution on this graph, as we have showed.

Re-reading your question it occurred to me you might be interested in directly showing that the right hand side of the two equations are equal in the pre-interventional distribution (which they must be, given our previous derivation). That's not hard to show directly too. It suffices to show that in your DAG:
$$
\sum_{X'}P(Y|Z, X') P(X') = \sum_{U}P(Y|Z, U) P(U)
$$
Notice the DAG implies $Y \perp X|U, Z$ and $U \perp Z|X$ then:
$$
\begin{aligned}
\sum_{X'}P(Y|Z, X') P(X') &= \sum_{X'}\left(\sum_{U}P(Y|Z, X', U)P(U|Z, X') \right)P(X') \\
&= \sum_{X'}\left(\sum_{U}P(Y|Z,  U)P(U| X') \right)P(X') \\
&= \sum_{U}P(Y|Z,  U) \sum_{X'}P(U| X')P(X') \\
&= \sum_{U}P(Y|Z,  U) P(U) \\
\end{aligned}
$$
Hence:
$$
\begin{aligned}
\sum_{Z}P(Z|X) \sum_{X'}P(Y|X', Z) P(X') &= \sum_{Z}P(Z|X)\sum_{U}P(Y|Z,  U) P(U)\\
&= \sum_{U}P(U)\sum_{Z}P(Y|Z, U)P(Z|X) \\
&= \sum_{U}P(U)\sum_{Z}P(Y|Z, X, U)P(Z|X, U) \\
&= \sum_{U}P(Y| X, U) P(U)\\
\end{aligned}
$$
Comment: This is a very good and exhaustive answer. The bit where you identify the causal effect through the front-door is, however, superfluous (OP has already done it and it follows straight from the front-door theorem), and it also contains a mistake: There is no "law of total probability" for causal effects. That is, $P(Y|do(X))$ does not generally equal $\sum_{Z}P(Y|do(Z))P(Z|do(X)$, but rather $\sum_{Z}P(Y|Z, do(X))P(Z|do(X))$, which is clearly different. See the big Pearl book on pages 87--88.
Comment: @Jeevaka this is an assumption encoded in the DAG, implied by its factorization and the assumption that the system is composed of modular, autonomous pieces. Thus, changes in $P(X, U)$ do not affect $P(Y|X, U)$. One way to help thinking about this is to write down the structural equations of both models $M$ (observational model)  and $M^*$ (interventional model) and then derive the implied distributions $P$ and $P^*$. You will see that the conditional of $Y$ given $X$ and $U$ will be the same in both.
Comment: @einar that's correct, when intervening on X, there is no confounding between Z and Y.
Comment: @JulianSchuessler that's why I wrote "can be thought as", as a way to help understanding, but not literally saying it is. Regarding the front door derivation it wasn't clear the OP knew how to obtain it, that's why I put it there.
Comment: Great answer. Thanks, Carlos. The second part of your answer was exactly what I asked for. I have two follow-up questions here. 1) What search strategy did you use for algebraically manipulating the expressions in your second answer? (By squinting long enough at the expressions?) Since the search space is large, I am wondering how an algorithm can be written to be able to automatically come to the same conclusion.
Comment: 2) I was also confused with  how to interpret $\sum_z P(Y|\textit{do}(Z)P(Z|\textit{do}(X))$, as my first intuition was like Julian's suggestion. But the Pearl et al.'s book I mentioned uses your expression. I am wondering, whether, in general, when factoring a directed chain where the start node is the cause and end node is the effect, each factor has to be conditioned on  $\textit{do}(Z)$ and not on $Z$, where $Z$ is an intermediate note in the chain.
Comment: @CarlosCinelli, could you comment on why the second invariant, P*(Y|X,U) = P(Y|X,U) is true? I am puzzled because in the post intervention case X and U are independent while they are dependent in the original case. Tks.
Comment: @CarlosCinelli I have two related question. 1) in general the backdoor criterion/adjustment permit to try only one set of necessary control variables $Z$ or several sets? 2) can you give me your opinion here: https://stats.stackexchange.com/questions/406671/conditional-and-interventional-expectation ?
Comment: @markowitz 1) several sets. 2) I'm a bit busy now, but I can take a look later
Comment: @CarlosCinelli may you give me your answer/comment about this question? https://stats.stackexchange.com/questions/416607/causality-structural-causal-model-and-dag
Comment: I can't 100% get my mind around the step $P(Y | do(X), Z) = P(Y | do(X), do(Z))$, could you elaborate on why this equality holds? Otherwise a great answer, +1.
Comment: Followup: I've been reading the Primer book which doesn't incude the do-calculus. Is the trick done by Rule 2? IE. it states that if $Z'$ blocks all backdoor paths between $X', Y'$ then $P(Y'| do(X'), Z') = P(Y' | X', Z')$. Then the argument is presumably that since $do(X)$ above blocks the backdoor path by intervention it takes the place of my $Z'$ of Rule 2 and we get the desired equality?


Render an HTML element before the menu with hook preprocess menu

Question: I want to render an HTML element like This is my menu before my menu. I thought the most easy solution is to alter the menu. I already altered my menu to add a class. But how do I add an element before my menu with a hook preprocess function?
I know I can extend menu.html.twig to menu--my-mwnu.html.twig and add it there but I think it's less overhead to add it with a preprocess function.
/**
 * Implements hook_preprocess_menu().
 */
function mytheme_theme_preprocess_menu(&$variables) {

  if (isset($variables['menu_name'])) {
    if ($variables['menu_name'] === 'my-menu') {

      $variables['attributes']['class'][] = 'my-menu';
    }
  }
}

Comment: Why is it less overhead in a preprocess? Performancewise this should not really matter, as both solutions are cached after the first visit. m2c: You should always pick the "least intrusive" method first: CSS -> HTML/Twig -> PHP - If e.g. after an update CSS breaks, your site won't be that beautiful, but stil fully functional. It Twig breaks, your site usually ends up with less rendered output. If PHP breaks, your site often ends up in a WSOD.
Comment: I'm with Hudri. In this case you should edit the template. Since it's a change to the actual markup. Markup = templating.
Comment: Ok thank you. I will extend the twig file. But just to know. How do I add it within my preprocess function?
Answer: In this case you should edit the template. Changes to markup should be done in templates.

Ok thank you. I will extend the twig file. But just to know. How do I add it within my preprocess function?

Looking into the standard menu.html.twig there isn't any place left to preprocess content to have it prefixed with some markup for example. There are just menu items that must be of type URL and attributes.
So your best bet probably is to add the markup to the block instead (in case you print your menu in a block).
/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME_preprocess_block__footermenu(&$variables) {
  $variables['content']['#prefix'] = 'This is my menu';
}


Show that $\sum_{j=0}^{2J+1} (-1)^j \sum_{d|(m,n) \, \omega(d)=j} 1 \le 1_m (n)$

Question: I'd like to show that
$$\sum_{j=0}^{2J+1} (-1)^j \sum_{d|(m,n)\\ \omega(d)=j} 1 \le 1_m (n)$$ for any integer $J\ge 0$,
where $\omega(d)$ is the number of distinct prime factors of $d$ and
$1_m(n) = 1$ if and only if $(m,n)=1$, otherwise it is $0$.
All I have done is the followings:
I know that
$$\sum_{j=0}^{2J+1} {k \choose j}(-1)^j \le 1_m(n)=(1-1)^k$$
where $k$ is the number of prime factors of $(m,n)$.
and
$$\sum_{d|(m,n) \\ \omega(d)=j} 1 \le {k \choose j}$$
What I want to show is "alternating sum" of above terms,
I've tried to use above two inequalities, but I couldn't proceed it further.
And a few calculation by hands convinces me it is true.
Any hint and suggestion will be appreciated.
Edit:
I've shown that if $(m,n)={p_1}^{a_1}{p_2}^{a_2}\dots{p_l}^{a_l}$, where $p_i$ are primes and $l$ is odd, then
$$\sum_{j=0}^{l}(-1)^j \sum_{d|(m,n) \\ \omega(d)=j} 1 = A \prod_{i=1}^{j} \left(\frac{1}{a_i} -1\right), $$
where $A := \prod_{i=1}^{j} a_i$.
so if $l$ is odd, then the above sum should be non-positive.
then what I'd like to show follows.
But it left the case $2J+1 < l$.
Comment: @TravorLZH What is m? Do you mean that m came from (m,n)?
Comment: There was a typo. The correct formula should be $\sum_{0\le j\le m}\binom kj(-1)^j=(-1)^m\binom{k-1}m$
Comment: @TravorLZH How do you use it the sum over $\omega(d)=j$?
Answer: The problem is equivalent to proving
$$
\sum_{j=0}^{2J+1}(-1)^j\sum_{\substack{d|n\\\omega(d)=j}}1\le
\begin{cases}
1 & n=1 \\
0 & n>1
\end{cases}
$$
Suppose $\omega(n)=k$. Then the LHS can be written using binomial coefficients:
$$
\sum_{j=0}^{2J+1}(-1)^j\sum_{\substack{d|n\\\omega(d)=j}}1=\sum_{j=0}^{2J+1}(-1)^j\binom kj
$$
To obtain a closed form for the RHS, we define the following sequence:
$$
a_{t,k}=\sum_{0\le j\le t}(-1)^j\binom kj,
$$
so that its generating function can be written as
\begin{aligned}
\sum_{t\ge0}a_{t,k}z^t
&=\sum_{j\ge0}(-1)^j\binom kj\sum_{t\ge j}z^t=\sum_{j\ge0}(-1)^j\binom kj{z^j\over1-z} \\
&=-(1-z)^{k-1}=-\sum_{0\le t\le k-1}\binom{k-1}t(-z)^t.
\end{aligned}
Comparing coefficients gives
$$
a_{t,k}=(-1)^t\binom{k-1}t\quad k\ge1,
$$
Plugging $t=2J+1$ into this expression yields the desired inequality.


How to setup Azure traffic manger to switch between a website and a down (maintenance) page, without disruption?

Question: Once can easily setup Azure traffic manager with two endpoints:

the e-commerce site you operate normally
the site with a static maintenance page (we user Azure static web)

The requirement is to be able to put up the maintenance page any time, regardless of if the e-commerce site is up or down.
There are several routing methods in TM, but nothing which is "just use the enabled one".
We tried the "performance" routing method.
The problem is this: if we enable the site down endpoint (after a long period of not using it), and disable the e-commerce endpoint, there is a long period (about a minute) where TM is checking the endpoint status of the newly enabled endpoint, where "new" users see this message: site not found, DNS name not resolved instead of the site down page. If they keep refreshing, after around a minute they see the maintenance page.
Any suggestions on how to make the switch instant? Ideally I would want to disable all the endpoint health checks, but cant see a way to do this.  This is especially true of our non-prod envs, which cant be seen without our VPN, so Azure can't actually check the health.
This is separate to the issue that users on the e-commerce site will continue to see the e-commerce site after the switch, as their browsers are caching the DNS records (we set them to the min, which is 10 mins).
Answer: In your scenario you want to use the priority routing method, this will use the endpoint with the highest priority unless it is not available then it will use the low priority. You can then switch the priority when you need to switch to the maintenance page.
Using performance routing will result in people hitting your maintenance page when it is quicker for them to reach that than your main page.
Traffic manager is a DNS based routing tool, so there will always be some delay when you make the change, there isn't really a way around this. If you need quicker response time then you will want to look at something like Front Door, which is not DNS based.


What do careless, reckless and excessive force mean when referring to fouls?

Question: In Law 12 (Fouls and Misconduct) of the IFAB Laws of the Game, it states that:

A direct free kick is awarded if a player commits any of the following offences against an opponent in a manner considered by the referee to be careless, reckless or using excessive force:

What do careless, reckless and excessive force mean, and do these have any affect on other sanctions (eg. cards) that a player might receive?
Comment: @Don_Biglia - I find myself including all of this information in answers a lot, so to avoid repetition and very long answers, I thought I'd just post it here. That way, I can use a one-sentence summary in other answers and include a link back to here. The point of answering your own question is to contribute information to the site for the benefit of others. [It's always been encouraged on Stack Exchange sites.](https://stackoverflow.blog/2011/07/its-ok-to-ask-and-answer-your-own-questions/)
Comment: How does this work, you post a question and an answer at the same time? What is the point of that?
Comment: As an aside, I'm going to wait a tiny bit longer before marking my answer as accepted, so that if a better answer appears, I can select that instead.
Answer: In Law 12.1, the following definitions are provided for careless, reckless, and excessive force:

Careless is when a player shows a lack of attention or consideration when making a challenge or acts without precaution. No disciplinary sanction is needed.

Examples of careless fouls are shown here, here and here.

Reckless is when a player acts with disregard to the danger to, or consequences for, an opponent and must be cautioned

A player who commits a reckless foul is cautioned for unsporting behaviour.
In Law 12.3:

There are different circumstances when a player must be cautioned for unsporting behaviour including if a player:
...

commits in a reckless manner a direct free kick offence

...

Examples of reckless fouls are shown here, here1  and here.
1 - The referee appears to give the decision to the incorrect team here.

Using excessive force is when a player exceeds the necessary use of force and endangers the safety of an opponent and must be sent off

A player who commits an excessively forceful foul is sent-off for:

serious foul play, if the foul was committed whilst challenging for the ball; or
violent conduct, the foul was committed while not challenging for the ball.

In Law 12.3:

A tackle or challenge that endangers the safety of an opponent or uses excessive force or brutality must be sanctioned as serious foul play.
...
Violent conduct is when a player uses or attempts to use excessive force or brutality against an opponent when not challenging for the ball, or against a team-mate, team official, match official, spectator or any other person, regardless of whether contact is made.

Examples of excessively forceful fouls are shown here, here and here.
Answer: Usually related to intent or generally careless use of your body and lack of control to the point where you're simply irresponsible in an already dangerous sport.
Comment: Any sources to back up your answer?
Comment: The question is specifically about football, and these terms are defined. Vague descriptions based on anecdotes which may not even reflect the sport are unhelpful. This response adds nothing to the well-referenced answer written by studro 10 months ago, and interferes with a clear understanding of the definitions in LOTG. -1 and flagged as VLQ.
Comment: Sorry I guess nothing on paper just played and watched contact sports my whole life.


Image file size is too small. I want it increased

Question: Is there a way to pad or "decompress" an image (JPEG) in bash? By this I mean, if the image size is 1.5MB I want it padded up to 5MB, for example. The resultant quality of the image is not (so) important. And if this padding leaves a black rectangle by the side of the image it is acceptable.
EDIT:
THe sole answer below guessed correctly that it was an XY problem.
Comment: So you took a wild guess at what was causing your problem and asked about that, rather than telling us what the real problem is and asking how to solve that. This is a classic example of an [XY problem](http://www.perlmonks.org/?node_id=542341). Not a good approach.
Comment: It might help to know why you want to do this. It's common to want to increase the displayed size of a jpg; I can't think of a reason to want to increase the file size.
Comment: Because the system I am working on renames and slightly compresses images fed into it. But for some images the resultant image is just a black screen. SO I thought the reason was that these images are too small.
Comment: Tell us about the system you're working on and *how* it "slightly compresses images". There's probably something wrong there.
Comment: I see you've accepted my answer -- but I still have no idea what the real problem was. Future readers might benefit from more information.
Comment: You correctly guessed that it was an XY problem. My base problem has nothing to do with resizing bitmaps.
Comment: If you can describe what the actual problem was, and the solution, it would be much more helpful to future readers.
Answer: A quick experiment indicates that just appending zero bytes to a <code>*.jpg</code> image file still allows it to be viewed.
On Unix, I used a command like this:
<code>( cat original.jpg ; dd if=/dev/zero bs=1024 count=1024 ) > bigger.jpg
</code>
I suspect that any arbitrary data, not just zeros, would have the same result.
(Using an image editor to append a black rectangle to an image probably wouldn't increase the file size by much; compression works really well on blocks of solid color.)
I can hardly imagine a good reason to do this, though.
Update :
Based on your comment, this answers the question you asked, but almost certainly won't address your actual problem. You haven't given us enough information to guess how to do that.
This is a classic XY problem.


PySpark Linear Regression in dataset with more features than data points

Question: I'm using PySpark to develop a simple text mining application. The goal is to classify a specific document as Spam or Not Spam. I have approximately 1000 documents available to train the model (n).
After cleaning the data (orthographic error correction, stemming, etc) I use IDF to extract a features vector. By default the feature dimension of this vectorization method is 2^18=262144, meaning that I'll have a total of 262144 highly sparse features (p).
Considering that p >> n:
As I remember, and according to some articles and web-articles I found, it's not possible to apply a Linear Regression with this type of data without doing any feature reduction, otherwise the algorithm would fail (because with p > n the variance would be infinite).
To test this I ran a Linear Regression on my data:
...

idf_stage = IDF(inputCol=hashingTF.getOutputCol(), outputCol="features", minDocFreq=1)
lr = LinearRegression(maxIter=10, tol=1E-6)

pipeline = Pipeline(stages=[..., idf_stage, lr])

model = pipeline.fit(train)
results = model.transform(test)
results.select("label", "prediction").show()

And I got the following results:
+-----+--------------------+
|label|          prediction|
+-----+--------------------+
|  1.0|  0.8123660495998272|
|  1.0|  0.9485273761704205|
|  0.0| 0.25039087467730653|
|  1.0|  1.2359878831283708|
|  0.0|-0.14295806192187976|
|            ...           |
+-----+--------------------+

I was expecting this to fail so it surprised me when I noticed that no exception was thrown.
There are a couple of things I can't figure out:
Question 1: When it's said that a Linear Regression algorithm will fail when p > n what is the meaning of fail? Should it not work at all and throw an exception or should it run but return results that shouldn't be considered?
Question 2: If the algorithm should throw an exception why is it running? Is Spark doing any feature reduction behind the scenes?
Question 3: Since Logistic Regression can be turned into a Linear Regression problem using variable transformations does it present the same problems when p > n?
Comment: @user10465355 your answer actually answered the question- you didn't need to delete and convert to a comment. Either way it's up to you.
Comment: Cross-site almost dupe: [Why is it the case that when we try to fit an OLS model to a system with more variables than observations, that the residuals are zero?](https://stats.stackexchange.com/questions/135163/why-is-it-the-case-that-when-we-try-to-fit-an-ols-model-to-a-system-with-more-va)
Comment: By default Spark doesn't use use closed form solution if number of features exceeds `LinearRegression.MAX_FEATURES_FOR_NORMAL_SOLVER` (4096). Instead it treats it as an optimization problem, and solves it using limited memory gradient descent to optimize cost function. There is no reason for it to fail, though solution doesn't have to be precise. Same as a many packages don't use actual inverse,  and instead apply [pseudoinverese](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse), and can reach a solution, even if T(X) * X is not reversible.
Answer: A failure would be expected only if you used closed form solution:
(XT X)-1 XT y
and additional verification was applied.
However this is not the only approach that can be used, and not a one that is normally used in Spark. It is possible to use numerical methods, like

Using pseudoinverse. This is how linear is usually implemented in different packages.
Iterative optimization procedures including different variants of gradient descent.

and can be used even if  (XT X) non-invertible or ill-condition. There is of course no guarantee that these result in any sensible solution.
By default o.a.s.ml.regression.LinearRegression uses closed form solution ("normal" solver) only if the number of features is lower than 4096 (It is a default value of LinearRegression.MAX_FEATURES_FOR_NORMAL_SOLVER).


Time Series Model (SARIMA) Consistently Underpredicting

Question: I have a SARIMA model of order (0,1,1)(0,1,1)52 on a log transformed variable (originally non-negative count data, y = log(x+1), '+1' as zero is possible for x).
The series plot along with prediction (figure 1):

Residuals plots against the training data (figure 2):

Errors against the validation set (figure 3):

As is evident from figures 1 and 3, the model is consistently predicting a lower value than that which actually occurs. How can I improve my model to mitigate this?
I'm using the statsmodels package for Python. Model generated by the following code:
<code>sarima_mod = sm.tsa.statespace.SARIMAX(train, order=(0,1,1), trend = 'n', seasonal_order =(0,1,1,52))
sarima_fit = sarima_mod.fit(disp=False, outer_iter=400)
</code>
Comment: Are you correcting for bias when you are back-transforming your predictions? See the "Bias adjustments" section [here](https://otexts.com/fpp2/transformations.html). If this is indeed the culprit, it underscores why it is usually better to use established forecasting methods (`forecast::auto.arima()` will determine the correct Box-Cox transformation and take care of the bias-adjusted backtransformation), rather than creating your own tools...
Comment: Thanks Stephan, but I have not back-transformed the data. All the residual analysis (training and validation sets) are on the transformed data. Thank you for the link, this will help with back-transforming.
Answer: You should search for the best params using pmdarima or you can just grid search with statsmodels and minimize AIC or whatever cost you deem appropriate.
Assuming you did that already and that's how you got those orders or just in general it is entirely possible that your validation data is just different. Perhaps there is a level change which causes the issue and there is NO way a model would be able to perceive this.  Or there may be an exogenous factor that you can account for. Either way, if you just adjust your current parameters to something that is a less 'likely' given your training data but doesn't underforecast then you are just overfitting the validation set by using future knowledge. You could mitigate this by using time series cross validation with a rolling train/validation set but in my 'experience' your best bet is to use AIC/AICc for params in ARIMA models since I have seen arima models go crazy from overfitting on a simple holdout set.
Another possible issue is that you wanted to produce n number of one-step-ahead forecasts but you accidentally produced a n-step forecast. That would put it on the right level overall. Statsmodels api is confusing.
Comment: Thanks Tylerr, helpful to know about pmdarima (I almost recoded everything in R to take advantage of forecast::auto.arima), great this is also available in Python in the pmdarima package.


Consulta SQL Retornado Nulo, Vazio para JSON

Question: estou com problemas!! tenho duas tabelas 'usuario'e 'post' quando uso o phpmyadmin para fazer a seguinte consulta ele me retorna os valores como eu espero.

SELECT usuario.nome, usuario.foto_profile, post.titulo, post.descricao, post.local, post.latitude, post.longitude, post.data
FROM usuario
INNER JOIN post ON usuario.id = post.id_usuario

porém quando vou fazer com php não me retorna nada e pior nem se quer um erro.
segue o código utilizado.

// Instancia o objeto PDO
$pdo = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
// define para que o PDO lance exceções caso ocorra erros
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// executa a instrução SQL
$consulta = $pdo->query("SELECT usuario.id,usuario.nome, usuario.foto_profile, post.titulo, post.descricao, post.local, post.latitude, post.longitude, post.data
FROM usuario
INNER JOIN post ON usuario.id = post.id_usuario");

$results = array();
while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
    // aqui eu mostro os valores de minha consulta
  $results['feed'][] = array(
      'nome' => $linha['nome'],
      'foto_perfil' => $linha['foto_profile'],
      'timestamp' => $linha['data'],
      'titulo' => $linha['local'],
      'descricao' => $linha['descricao'],
   );
}

echo json_encode($results);

obs: estranho é quando eu tiro o titulo e a descrição ele me retorna o nome, foto_perfil e o timestamp

{
    "feed": [{
        "nome": "Alessandro Barreto",
        "foto_perfil": "https:\/\/lh3.googleusercontent.com\/-fgg69tRzubc\/AAAAAAAAAAI\/AAAAAAAAAEA\/fU9pzVn2CO8\/photo.jpg",
        "timestamp": "1448069250858"
    }, {
        "nome": "Alessandro Barreto",
        "foto_perfil": "https:\/\/lh3.googleusercontent.com\/-fgg69tRzubc\/AAAAAAAAAAI\/AAAAAAAAAEA\/fU9pzVn2CO8\/photo.jpg",
        "timestamp": "1448069704176"
    }, {
        "nome": "Alessandro Barreto",
        "foto_perfil": "https:\/\/lh3.googleusercontent.com\/-fgg69tRzubc\/AAAAAAAAAAI\/AAAAAAAAAEA\/fU9pzVn2CO8\/photo.jpg",
        "timestamp": "1448069737468"
    },
Comment: Ele não retorna nada do banco para aplicação ou retorna normal e quando vai criar o json ele cria o json em branco?
Comment: quando eu eu tento retornar normal (só printando os valores )sem usar o json ele funciona, porém o estranho é quando eu tento montar o json e colocar informações da tabela 'post' ele vem vazio
Comment: Pode ser algum valor dentro de alguma string quebrando o json, tenta pegar o valor de uma linha (where usuario.id = X) e tenta montar o Json.
Comment: continua do mesmo jeito vazio, fiz um teste fazendo o print "print_r($results);" e ele me retornou os valores mas no json_encode não vem vazio
Comment: Tenta colocar assim echo json_encode(utf8_encode($results));
Comment: Ricardo ele da erro "Warning: utf8_encode() expects parameter 1 to be string, array given in"
Comment: Então coloca ela para executar para cada string que vai entrar no array. Se não der então fico sem ideias.
Comment: Ricardo fiz o seguinte coloquei o utf8_encode($linha['titulp])  em cada linha da consulta e deu certo, valeeeeu
Comment: Coloquei o resultado da conversa na resposta para ficar mais fácil de outras pessoas resolverem o problema. Se quiser marca como resposta.
Answer: Colocando na resposta o resultado dos comentários:
Para o Json funcionar no PHP é necessário encode utf8.
No caso acima bastas colocar utf8_encode(string), nas strings vindas da consulta na hora de montar o array, com isso todo o array vai estar no formato correto e a conversão para o Json vai funcionar.
Comment: O json funciona com ou sem uso do utf8_encode(). O problema no seu caso é a falta de configuração do ambiente e, por isso precisou fazer essa "gambiarra" com utf8_encode()..


OOP Bubble sort C++ program

Question: I am getting these errors
Compiler Error C3867 (((( 'func': function call missing argument list; use '&func' to create a pointer to member ))))
nothing
#include <iostream>
using namespace std;

class Cuzmo
{
private:
    int array[1000];
    int n;

public:
    Cuzmo ()
    {
        int array[] = { 95, 45, 48, 98, 485, 65, 54, 478, 1, 2325 };
        int n = sizeof (array) / sizeof (array[0]);
    }

    void printArray (int* array, int n)
    {
        for (int i = 0; i < n; ++i)
            cout << array[i] << endl;
    }

void bubbleSort (int* array, int n)
{
    bool swapped = true;
    int j = 0;
    int temp;

    while (swapped)
    {
        swapped = false;
        j++;
        for (int i = 0; i < n - j; ++i)
        {
            if (array[i] > array[i + 1])
            {
                temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
                swapped = true;
            }
        }
    }
}
};

int main ()
{
    Cuzmo sort;

cout << "Before Bubble Sort :" << Cuzmo::printArray << endl;

cout << Cuzmo::bubbleSort << endl;

cout << "After Bubble Sort :" << Cuzmo::printArray << endl;

return (0);
}

I am getting these errors
Compiler Error C3867 (((( 'func': function call missing argument list; use '&func' to create a pointer to member ))))
Comment: Your constructor does not do what you think it does.
Comment: `array` is a local variable in the constructor. It no longer exists after the constructor exits. Same with `n` these have nothing to do with the class members of the same name.
Answer: This is not how you call a function f with no arguments:
f;

This is how you do it:
f();

Furthermore, you're trying to send the return value of bubbleSort() to cout, but there is no such value as the function has void return type.
In fact, the same is true of your printArray() function: it already does the printing, and there is no result value to send to cout.
Try:
cout << "Before Bubble Sort :";
Cuzmo::printArray();
cout << endl;

Cuzmo::bubbleSort();

cout << "After Bubble Sort :";
Cuzmo::printArray();
cout << endl;

The other problem is that you are declaring and initialising a local variable array in your constructor; this variable has nothing to do with the member.

How to initialise a member array of class in the constructor?

The same is true of your variable n. You keep redeclaring new, local variables that shadow the member variables.
Answer: Maybe you simply forgot the parentheses after your function calls?
Try Cuzmo::printArray() and Cuzmo::bubbleSort().
Also, you may want to use std::vector instead of a fixed-size int array (so that you loop over your actual entries rather than 10000 mostly uninitialised values) and look into std::swap.

OOP Bubble sort C++ program


Test a single score against a sampled distribution of scores

Question: I am currently working on a tool which calculates a score for each sample (i.e. patient biopsy) it is run on. Input samples can belong to one of two categories: case and control. Usually, a user would run the tool on a number of samples from both categories.
For each case sample, I would like to report whether the sample's score is significantly different from the scores of the provided control samples.
In other words, for each case sample I want to assess the likelyhood of seeing a score at least as extreme as the observed score if the sample came from the control distribution. My problem is that I don't know the real control distribution; I can only estimate it based on the control samples the user provides.
Currently, I am calculating the mean and standard deviation of the scores of the control group. For every case sample, I am then calculating the z-score with respect to the control group (i.e. I subtract the control group's mean score from the sample's score and divide by the control group's standard deviation). If the absolute value of the sample's zscore is > 2, I report the sample as significantly different.
However, I a worried that with this approach I do not take the sample size of the control group into account.
Other simple approaches like t-tests seem not appropriate here, since I don't want to compare means.
Could someone suggest a more appropriate test for this use-case? Any help would be greatly appreciated.
Thank you,
Peter
Comment: Trying to understand... when you say "I am currently working on a tool which calculates a score for each sample": how this score is calculated?
Comment: @Pitouille : thanks for trying to help! The tool uses a complex procedure (explaining would go too far here) to summarize epigenetic properties of the patient sample into a single, real valued number (can be positive or negative). The idea would be that cancer patients would often get a higher score than the control samples, due to their epigenetic properties. It may be reasonable to assume the scores of the controls are roughly normally distributed. Does that help?
Answer: You may want to use a prediction interval when
considering a new patient, who was not part of the previous
analysis for the control group.
Suppose the true scores for the control group are
distributed $\mathsf{Norm}(\mu =50, \sigma=7).$ In
a real application you could not know the population
mean and standard deviation, but would estimate
them using a sample mean $\bar X = 48.90$ and standard
deviation $S = 7.03$ of available data. (Computations in R:)
set.seed(1234)
x = rnorm(100, 50, 7)
mean(x);  sd(x)
[1] 48.90267
[1] 7.030837

Confidence interval. Then you might use these estimates to compute a 95%
confidence interval for $\mu,$ of the form
$$\bar X \pm t^*S\sqrt{\frac{1}{n}},$$ where $t^* = 1.98$
cuts probability 2.5% from the upper tail of Student's t
distribution with $\nu = n-1 = 99$ degrees of freedom.
qt(.975, 99)
[1] 1.984217

Based on the estimates above, the 95% CI computes to
$(47.51, 50.30).$
CI = mean(x) + qt(c(.025,.975), 99)*sd(x)/sqrt(100); CI
[1] 47.50760 50.29774

Prediction interval. Now suppose you have a new patient with score $55.7.$
Your first inclination might be to say this patient is
not contained in the 95% CI above and so would not
be consistent with the control group. However, the CI
is meant as an estimate of the unknown population mean $\mu.$
It is not intended to 'predict' scores of individual
additional cancer-free patients.
A 95% prediction interval is of the form
$$\bar X \pm t^*S\sqrt{1+\frac{1}{n}},$$
which takes into account the variability of the new
patient. This prediction interval $(34.88, 62.92)$
does include the new patient's score.
PI = mean(x) + qt(c(.025,.975), 99)*sd(x)*sqrt(1+1/100); PI
[1] 34.88238 62.92295

Notes: Without seeing your real data, I should not take
this further.
(1) In an actual situation, you would hope that prediction intervals for control and treatment groups do not overlap,
so we can be reasonably sure how to classify the new patient.
(2) If the only purpose of prediction intervals is to classify new patients, then it might be appropriate to
use one-sided prediction intervals, with an upper bound for
control group and a lower bound for treatment group.
(3) Some sort of discriminant analysis might take various kinds of information on a new patient into account in deciding on a classification.
Comment: Thank you, that was very helpful! I especially appreciate the examples. I expect that the variability in the cancer group to be quite high, but the variability for the control group should be much lower. Based on your suggestions, I would now calculate the prediction interval for the control group, and mark each cancer sample as "significantly different" if its score is outside the control-group's prediction interval. I'd rather avoid treating the cancer samples as a unified group, so I wouldn't calculate a cancer-group prediction interval.
Comment: Thanks again. I just want to point out a typo in the above command, in case someone else wants to use it: As far as I understood, the R command for calculating the pi is missing a "sqrt()", and it should be `pi = mean(x) + qt(c(.025,.975), 99)*sd(x)*sqrt(1+1/100); pi`
Comment: Seems reasonable.
Comment: @peter_pen.  Thanks much for finding typo. Fixed it // For the record, another blunder (also fixed) was to use `pi` which is a _reserved constant_ in R for 3.141593... and should **never** be used for anything else. Because R is case-sensitive, it's OK to use `PI`.


Convergence proof based on another sequence

Question: If $x_n \rightarrow \infty$ and $y_n = \frac{10}{x_n^2}$ for all $n \in N$, then $y_n \rightarrow 0$.
I'm struggling here. Clearly I have to show $|\frac{10}{x_n^2}-0|<\epsilon$ but I don't know what to choose for k such that n>k implies this is true.
I had tried k=$\sqrt{\frac{10}{\epsilon}}$ but that only proved it for $|\frac{10}{n^2}-0|<\epsilon$. Not sure how to get started with two sequences involved here.
Answer: Let $\epsilon > 0$ be arbitrary, since $x_n \to \infty, \exists N: n > N \to x_n > \sqrt{\dfrac{10}{\epsilon}} \to \left|y_n-0\right| = \left|\frac{10}{x_n^2} - 0\right| < \epsilon$


Problem in getting months in ceiling or floor in SQL Server

Question: In SQL Server, I am trying to get number of months between two dates.
I want the solution like this:

if output came to 4.2 months, then return 4 months
if output came to 4.5 months, then return 5 months
if output came to 4.9 months, then return 5 months
Comment: i have tried below functions but desired output is not coming:
DATEDIFF(dd,OPENDATE,MATURITYDATE)/30 , DATEDIFF(mm,OPENDATE,MATURITYDATE)
Comment: There are many different days in a month like Jun 31 Feb 28 or 29. What's your logic to get days of month numbe?
Comment: i am simply assuming days of month to be 30 days. my query is  that if there are 135 days then the number of months are coming 4.5 months. i want it to be 5 months(ceiling of 4.5, or in other case floor if months are 4.2 )
Comment: Ok I got it, you can try my answer:)
Comment: For example, if the dates are 2018-01-01 and 2018-01-16.  That's 15 days, so you would want 0.5 rounded up to 1 in this case.  Another way to look at it is that 15 days of passed in a month of 31 days, so that's 15/31 months, which would round down to 0.
Comment: I know you specified assuming a month to be 30 days, but I have to ask.  I've spent a lot of time educating users on the consequences of their definition of "months between two dates".  Every user has a different definition.  Consider May 1, 2018 through Aug 16, 2029.  Fewer than 136 months have passed, but if you assume 30 days per month, you'll get 138 months. Just want to make sure you know what you're asking. No disrespect intended if you've already considered this.
Answer: If you are assuming days of month to be 30 days.
DATEDIFF return integer difference days.
So, you can try to use CAST(days as decimal) let the day number be float number, then do ROUND get your result.
select ROUND(CAST(DATEDIFF(dd,OPENDATE,MATURITYDATE)as decimal) /30,0 )
from T

sqlfiddle


The height of the pdf is just a relative frequency: is that correct?

Question: I understand that the pdf function is not a probability, and the area under the curve must sum to one. I understand that the height of the pdf function is meaningless, and it is not a probability but a density of the probability.
Can I say that the height of the pdf tells us which value of the random variable can occur more likely than the other values?
For example, the following answer came from ChatGpt.
Let's consider a numerical example related to children's weight. Suppose we have a dataset of children's weights that follows a normal distribution with a mean of 50 kg and a standard deviation of 5 kg.
For Child A, let's say their weight is 55 kg. We can calculate the probability density at this weight using the dnorm() function:
dnorm(x = 55, mean = 50, sd = 5)
The resulting probability density would be a numeric value representing the likelihood of observing a weight of 55 kg within the given normal distribution.
For Child B, let's say their weight is 45 kg. Similarly, we can calculate the probability density at this weight:
dnorm(x = 45, mean = 50, sd = 5)
This will give us the probability density for a weight of 45 kg within the normal distribution.
Comparing the probability densities for Child A and Child B will provide insight into the relative concentration of weights around these values within the distribution. A higher probability density suggests that weights closer to that particular value are more likely to occur.
Comment: ... And a good pedagogical way to help it become familiar is to learn to construct and read *true* histograms, which are not mere bar charts: they use the *areas* rather than the *heights* of the bars to represent probability.  Once that is learned, the move to a density (which is a limiting version of a histogram) is natural and easy.
Comment: The height of the pdf is not meaningless at all: it is as said a probability density. It's not more mysterious or meaningless than any other kind of density, just an idea that is unfamiliar until it becomes familiar.
Comment: `Can I say that the height of the pdf tells us which value of the random variable can occur more likely than the other values?` What would "more likely" mean to you? It's not in dispute that the PDF height is the "likelihood" in the technical sense of the word as statistics nomenclature, so there is some merit to the ChatGPT response, but that technical definition need not align with a more colloquial definition that you might use.
Answer: This is really a FAQ, you are on the right track, but, the height is not really a relative frequency, as you say in the title. Values of density functions do not represent frequencies or probabilities directly. What you need is the areas below the density function, as explained beautifully at Intuitive explanation for density of transformed variable?
Then in question body Can I say that the height of the pdf tells us which value of the random variable can occur more likely than the other values?
In a way, yes (assuming sufficient continuity). Say you want to compare how likely values around $x$ is compared to values around $y$. Then you calculate the density ratio $f(x)/f(y)$. If, say, $f(x)/f(y)=2$, then the probability of values in a short interval around $x$ would be the double of values in a short (same length) interval around $y$.
Answer: You only have a probability for an interval of length >0 on the real line (for a truly continuous random variable), but as you say after seeing a value you can assess how relatively likely it is compared with other values.
On the other hand, if you measure with rounding (e.g. weight rounded to the nearest full kg), you can integrate out how much probability mass there is in the interval that gets rounded to the value you talk about. E.g. in your example, the probability of a weight that gets rounded to 55 kg is using R pnorm(q = 55.5, mean = 50, sd = 5) - pnorm(q = 54.5, mean = 50, sd = 5) $\approx 0.048$ (= 4.8%).
Answer: Yes; take for example the normal distribution. Most of the mass is in the center/middle, and fewer mass at both of the tails, meaning that if you take a random value from that distribution, you are much more likely to end up with a value that lies in the center of that distribution than in the tails.
So in that sense, the height of a PDF is related to how likely a number is. (This is also the concept of likelihood).
Comment: This isn't an issue for a Gaussian distribution, but what about a distribution like the one drawn around the `10:15` mark [here](https://www.youtube.com/watch?v=kkhdIriddSI)?


How to add value in to multiple value field (entity reference)?

Question: Im inserting into user field reference to other users. This is my code, but it is overiding all values of field, i just want too add value and keep values added before.
<?php
global $user;

$user->field_profile_access['und'][]['target_id'] = 34;
user_save($user)

?>

Answer: I think this line is wrong:
$user->field_profile_access['und'][]['target_id'] = 34;

You could try with:
$user->field_profile_access['und'][] = array('target_id' => 34);

or this:
$next = count($user->field_profile_access[LANGUAGE_NONE]);
$user->field_profile_access[LANGUAGE_NONE][$next]['target_id'] = 34;

I used LANGUAGE_NONE instead of 'und' here, but that's not the problem.
Or even better, using the Entity API. That example is for a field collection, but the idea should be useful for any entity, including nodes.
Comment: for more about the Entity API see https://www.drupal.org/node/1021556


How do you tell the inversion of a diminished 7th chord?

Question: In any chord that isn't a diminished 7th, you can tell the inversion of the chord by the intervals against the bass(eg: 6-3=1st inversion triad, 6-4=2nd inversion triad, 5-3=root position, more complicated for 7th chords, but the same idea).
But in a diminished 7th chord, such as the Adim7 below (clef is bass):

Every interval is the same(minor third). So how can you tell if a diminished 7th is in an inversion, and which inversion it is in?
Answer: Differentiating inversions
All seventh chords, when inverted, contain a second somewhere in the stack of notes. Diminished sevenths are no different. Somewhere in the inversion will be an augmented second.1
Thus, one can tell the inversion in the same way as any other seventh chord:

First inversion = 6-5-3 (the second is between 6 and 5)
Second inversion = 6-4-3 (the second is between 4 and 3)
Third inversion = 6-4-2 (the second is between 2 and 1)

Example
As an example, consider Adim7 and Cdim7. Both chords contain the same keys on the keyboard. However, the spellings are different, and therefore the inversion also clear.

Cdim7 in root position = C-Eb-Gb-Bbb (all minor thirds)
Adim7 in first inversion = C-Eb-Gb-A (augmented second between Gb and A)

Enharmonic spelling
Where there can be genuine confusion is when diminished sevenths are "misspelled". This can happen for two reasons.

The chord is being used to pivot between two keys, so it's correctly spelled only relative to one of those keys.
The chord is easier to read if written enharmonically.

For an example of (1), see How to analyze mm. 5-8 in the first movement of Beethoven Piano Sonata No. 8.
For an example of (2), see What's the Reason for Naming Major Second a Diminished Third?.

1. An implicit assumption is made, for the purposes of this post, that chords are in close position.
Comment: I learned something today. Always considered that a dim chord had all intervals as m3, but you've explained why that can't be so. To explain all to readers - here's the same dim7 chord, with all of its root positions. 1-m3-d5-d7. Spelled as such - C Eb Gb Bbb; A C Eb Gb Bbb: Eb Gb Bbb Dbb; Gb Bbb Dbb Fbb.That's without going unto their enharmonic names!Note, each note needs to be a 3rd of some kind above the last - the 'stack of thirds'.The very reason why some notes sound the same, but will need a different name, hence the reason the chord's inversion can be distinguished.If I understood it !
Comment: "All seventh chords, when inverted, contain a second somewhere in the stack of notes. Diminished sevenths are no different." So, a root position Dim. 7th chord is when every interval is spelled as a minor third, and no augmented seconds exist?
Comment: @OprenStein Correct.


Facebook Open Graph Custom Actions on the Fly

Question: I am giving a look to the Open Graph API. From what I see so far we must set up the actions and objects in the App configuration on Facebook.
What I am looking for is to set up actions and objects on the fly using PHP or Javascript.
I want to build an application that will be integrated in a Social Networking CMS. I do not want my customers to set up each action and object on the Dev App, it could take forever. I simply want to fire them with PHP based on the different actions that can be done in this CMS (such as: watch a video, listen music, chatting with friends etc.. etc.. )
Is there a way to create actions on the fly with PHP or Javascript?
Thanks
Answer: No - each action you setup has to be approved by Facebook before it can be used outside the developers of your application.
Comment: I think the way is explained here: https://developers.facebook.com/docs/opengraph/connections/ . But I do not clearly understand how it actually works...?!
Comment: Thats for creating instances of an approved action for a user. https://developers.facebook.com/docs/opengraph/tutorial/ - should clear things up for you.
Answer: I had the same question, seems to be buried in the opengraph section:
https://developers.facebook.com/docs/technical-guides/opengraph/publish-action/
Comment: nav's original answer is correct. The question is about defining an action for an application, which is not the same as publishing an action instance for a user.


How to delete 1 table if another table exists in PostgreSQL using IF-statements

Question: I'm trying to write a query that deletes/drops a table, if another table exists:
DO
$do$
BEGIN
   IF  EXISTS (SELECT FROM table1) THEN
      DELETE  FROM table2;
    END IF;
END
$do$;

but all that it is doing is deleting rows from table1 if table2 exists rather than the table itself.
Can you use a function similar to the one above or should I use drop table if exists?
Answer:
delete/drop a table if another table exists

That would be:
do $$
begin
    if exists(select 1 from information_schema.tables where table_schema = current_schema() and table_name = 'table1') then
    drop table table2;
  end if;
end; $$ language plpgsql;

Rationale:

selecting from a table is not the right way to check if it exists - if it doesn't, an error is raised. Instead, you can query the information schema.

to remove a table, you want drop table; delete, on the other hand, removes the content of the table, not the table itself.
Comment: yes! that works wonderfully, just needed to add quotation marks around  information_schema and tables! thx.                                                                                im wondering tho, what does the 1 after select mean?


Determine $x_1 + \cdots + x_n$ for Vandermonde-like system of equations

Question: Consider the system of equations
\begin{align*}
x_1^2 + x_2^2 + &\cdots + x_n^2 = q_2\\
x_1^3 + x_2^3 + &\cdots + x_n^3 = q_3\\
&\vdots\\
x_1^m + x_2^m + &\cdots + x_n^m = q_m
\end{align*}
where all the $q_i$ ($i = 2,\dots, m$) are known. Suppose that $m$ is arbitrary in the sense that for all $m > 1$, the value $q_m = x_1^m + x_2^m + \cdots + x_n^m$ is known (say, kept in a massive book somewhere). Can we determine $q_1 = x_1 + \cdots + x_n$?
Of course, we can, by taking $m = n$ and iteratively back-substituting the equations. However, I am wondering if there is a more clever way to go about this, that perhaps has a closed form.
Answer: A method I am more fond of is to relate the given system to that of a recursion. This works very well when we are given a lot of values of the Newton sums (in this case, in a massive book), or if we are given another variable in front of each of the $x_i$ terms (i.e. the system, $\sum_{i=1}^n a_i x_i^m=q_m$, for integers $m\geq 2$).
We can note that $q_m$ is in the form of an explicit formula for a recursion whose characteristic polynomial has roots $x_1,x_2,...x_n$.
This means we can express $q_m$ recursively as $q_m=\sum_{i=1}^n a_iq_{m-i}$, where $a_i$ is a sequence of length $n$. We can look up the values of $q_m$ from $q_2$ to $q_{2n+2}$ to get a system of $n$ linear equations and $n$ variables.
Once we solve for all the terms in $a_n$, we get that $q_1=\frac{q_{n+1}-\sum_{i=1}^{n-1} a_iq_{n+1-i}}{a_n}$
Comment: Thank you, I would have never thought of this!


Empty document when export string to .tex file

Question: I am trying to create a simple .tex file:
document = """
\documentclass[12pt]{article}
\begin{document}
Hello world!
$Hello world!$ %math mode
\end{document}
"""

file = open("main.tex",'w')
file.write(document)
file.close

It creates a document, named "main.tex" but this document is empty. Any ideas why and how to fix it?
Comment: Are you sure that you are looking at the correct main.tex file? If I execute your code, it works.
Comment: @Antje This is weird ! Yes, I changed the name and double-checked, but the file is empty.
Comment: @Antje I am using Jupyter Notebook and Python 3
Answer: If you run this code like this, the problem come from:
file.close

add parenthensis to:
file.close

which become:
file.close()

Comment: Thank you so much !! :)) I completely forgot to put the parentheses.
Answer: you could try this:
Sub Create_Text()

On Error Resume Next
MkDir "C:\Temporal"

On Error GoTo 0

document = "" & _
"\documentclass[12pt]{article}" & _
"\begin{document} " & _
"Hello world! " & _
"$Hello world!$ %math mode " & _
"\end{document}"

Open "C:\Temporal\main.txt" For Append As #1
    Print #1, document & ";"
    Close #1

End Sub

I hope this helps!!!
Comment: Thanks. I get: Sub Create_Text() Invalid Syntax