How to measure uncertainty of a parameter when false positives exist?

Question: The main goal of my research is to measure the percentage of brown dwarf stars in the Pleiades star forming cluster that are actually double stars (i.e. the brown dwarf star has a companion brown dwarf orbiting it).
To do this, I need to estimate what is the faintest brown dwarf star detectable in our observations using the Hubble Space Telescope Wide Field Camera 3.
My question is this:
Say you measure a parameter $X$ 100 times ($X_1, X_2,...,X_{100}$). There are only true positive detections and false positive detections.

80 of those measurements have a 99% chance of being a true positive.
20 measurements have a 50% chance of being a true positive (50% chance of a false positive).

If there was no false positives, the uncertainty $\sigma_X$ = standard deviation of all measurements.
How do you measure $\sigma_X$ when false positives exist as shown above?
To elaborate: each measurement of parameter $(X_1,X_2,...,X_{100})$ has an assigned chance of being a false positive $(P_{f1},P_{f2},...,P_{f100})$. So how to measure $\sigma_X$ is complicated by the existence of these false positives.
Comment: Just to elaborate: each measurement of parameter ($X_1$,$X_2$,...,$X_{100}$) has an assigned chance of being a false positive ($P_{f1}$,$P_{f2}$,...,$P_{f100}$).

So how to measure $\sigma_X$ is complicated by the existence of these false positives.
Answer: In principle this is a classification problem. If you would know which observation is a true positive, you could just take these observations and estimate the mean and the variance for them. Doing so, you implicitly assume that the true value follows a normal (or more accurately T-student) distribution defined by the obtained mean $\overline{x}$ and variance $s$:
$$\mu\;\tilde{}\;N\left(\overline{x},s\right)$$
Because you are not certain about which observations are true positives, there are various scenarios which need consideration. If you would construct 100 scenarios then in 80 of them you would include an observation which you give 80% to be a true positive. The probability model gets more complicated:
$$p(\mu) = p\left(\mu\ |\, \text{subset of the }X_i\right)\times
p\left(\text{subset of the }X_i\right)$$
where the first factor on the right hand side denotes the probability for a certain value $\mu$ to be the true value given the subset of observations. The second factor denotes the probability that a certain subset of the observations contains all the true positives and no false positives.
So how to get the standard deviation? You could write a computer program that samples values for $\mu$ from the above stated probability distribution. If you afterwards have a list of $\mu_1,\mu_2,\dots,\mu_N$, you can calculate mean and variance in the usual way:
$$\mu = \frac{1}{N}\sum_{i=1}^N \mu_i \hspace{1em}\mbox{and}\hspace{1em}
\sigma^2 = \frac{1}{N}\sum_{i=1}^{N}(\mu_i-\mu)^2$$
Additionally, just to make sure that the variance (or standard deviation) is a meaningful measure for the uncertainty, you could draw a histogram for the $\mu_1,\mu_2,\dots$ and compare it to a normal distribution characterized by $\mu$ and $\sigma$.
In order to get the $\mu_i$'s you can perform the sampling in two steps. Firstly, sample a subset of the $X_i$. The following pseudo code demonstrates how this could be done:
for each observation X_i do
  P := probability for X_i to be a true positive
  R := random number in [0,1] drawn from uniform distribution
  if R smaller P
    accept X_i as true positive
  otherwise
    reject X_i for the scenario

If you work with statistical programming language $R$ you could also simply use the sample function.
For the sampled subset of the $X_i$'s calculate a mean $\overline{x}$ and variance $s$ in the usual way. Use that to draw a sample data point $\mu_i$ from the normal (better T-student) distribution characterized by mean $\overline{x}$ and variance $s$.
Repeat these two steps until you have enough data points $\mu_i$ to get convergence for $\mu$ and $\sigma$.
Comment: There is pretty clear systematic error in the $X_i$'s since (long story short) the point spread function models for HST Wide Field Camera 3 are not perfect. This may imply that the distribution is likely not a student's T.

I do know this: If the sample is all false positives, then the distribution is clearly different from a distribution with false positives and true positives as we are dealing with here.

Howabout doing the accept/reject as suggested to sample the $X_i$'s and then compute $\overline{x}$ and variance $s$ from it?  Is knowledge of the underlying distribution required?
Comment: Also - I am entirely serious - if this method works I am more than willing to mention you by name in the acknowledgement section of resulting refereed publication :)


Worst placing in the climber competition for the overall winner

Question: It is clear that the Tour de France is won in the mountains. (On a flat stage there usually are mass finishes, with the winner not able to make up much time.) Hence the overall winner should also be a reasonably good climber.
What was the lowest placing in the climber competition of any winner of the Tour de France Yellow Jersey?
Answer: The official Best Climber Competition started in 1933. There was an unofficial ranking from 1905 to 1932 by the magazine Auto, but I couln't find these data.
In 1966, Lucien Aimar (France) didn't even make it to the Best Climber Classification, scoring 0 points.
In 1987, Stephen Roche (Ireland) also scored 0 points.
 Sources: Wikipedia, bikeraceinfo.com, the-sports.org
Comment: I did find a raft of information on the tour de france site, in their history section, and I gathered data back to 1975. Unfortunately, up to 1997 only the top three point getters are listed on the site. (And of course definitions, methods and available points have been constantly adapted.)
Comment: Not that I doubt you, but do you have a specific link that says Roche scored 0 points in the KOTM in 1987? It doesn't tally with my memory of that race (I watched almost every second), but I willingly concede that I might be wrong.
Comment: @TrueDub [french](https://fr.wikipedia.org/wiki/Tour_de_France_1987), [english](https://en.wikipedia.org/wiki/1987_Tour_de_France) Wikipedias, and [bikeraceinfo](http://www.bikeraceinfo.com/tdf/tdf1987.html) agreed he wasn't in the Top 5. Then [the-sports](http://www.the-sports.org/cycling-tour-de-france-results-1987-epr15654.html) gives the full list. Reports said Roche won time on time trials, then even he had decent places in climbing stages, he didn't score points (points are only for the few first on the top, not at the end of the stage) and Roche only won one stage that year.
Comment: Thank you for your full list link. I'm well aware of how the points were awarded. I just thought he'd scored at least one point somewhere - incorrectly.


load items where relation is null in sequelize

Question: I'm new to sequelize, i'm trying to load all entries in my user table where the task relation is null. but its not working. here is what i have tried:
const express = require('express');
const app = express();

const Sequelize = require('sequelize');
const sequelize = new Sequelize('sequelize', 'mazinoukah', 'solomon1', {
  host: 'localhost',
  dialect: 'postgres',

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000,
  },
});

const Task = sequelize.define('Task', {
  name: Sequelize.STRING,
  completed: Sequelize.BOOLEAN,
  UserId: {
    type: Sequelize.INTEGER,
    references: {
      model: 'Users', // Can be both a string representing the table name, or a reference to the model
      key: 'id',
    },
  },
});

const User = sequelize.define('User', {
  firstName: Sequelize.STRING,
  lastName: Sequelize.STRING,
  email: Sequelize.STRING,
  TaskId: {
    type: Sequelize.INTEGER,
    references: {
      model: 'Tasks', // Can be both a string representing the table name, or a reference to the model
      key: 'id',
    },
  },
});

User.hasOne(Task);
Task.belongsTo(User);

app.get('/users', (req, res) => {
  User.findAll({
    where: {
      Task: {
        [Sequelize.Op.eq]: null,
      },
    },
    include: [
      {
        model: Task,
      },
    ],
  }).then(function(todo) {
    res.json(todo);
  });
});

   app.listen(2000, () => {
      console.log('server started');
   });

if i have three users, and 2 of those users have a task each, i want to load just the last user without a task. is this possible in sequelize ?
Answer: after much debugging i found the solution
app.get('/users', (req, res) => {
User.findAll({
    where: {
      '$Task$': null,
    },
    include: [
      {
        model: Task,
        required: false,
      },
    ],
  }).then(function(todo) {
    res.json(todo);
  });
});

by adding this where clause
where: {
  '$Task$': null,
},

i was able to load only users without a task


Linear Search returning array with indices value is found at

Question: I attempted a program to return an array with the indicies of the array where a specific inputed value is found, but every run results in an error, which seems to be an infinite run time. The error seems to be occuring right after printing out the last of the indicies found.
Can anyone help?
(Side note: I've seen multiple pages about deleting pointers when done with them; should I be doing that here?)
Forgot to mention - I want the first slot of the returned array to save the size of the array, so that it can be accessed easily later on in the program
#include <iostream>
#include <vector>
using namespace std;

int* linearSearch(int* n, int k, int f) {
    // Input: Index 0 Address ; Size of Array; Element to Search
    // Output: Array of Found Indicies
    vector <int> a;
    int* b;
    for(int i = 0; i < k; i++)
        if(n[i] == f)
            a.push_back(i);
    *b = a.size();
    for(int i = 0; i < a.size(); i++)
        b[i + 1] = a[i];
    return b;
}

int main() {
    int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
    int* k = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);
    for(int i = 0; i < k[0]; i++) {
        cout << "Found at index: " << k[i + 1] << endl;
    }
    return 0;
}

Comment: What do you think the line `*b = a.size();` does?
Comment: Forgot to mention - I want the first slot of the returned array to save the size of the array, so that it can be accessed easily later on in the program.
Comment: Where do you allocate space for b to point to, then?
Answer: int* b;
....
*b = a.size();

b has to be allocated. Try following:
int* b = new int[a.size() + 1];
b[0] = a.size();

I see what you meant. b will have magically length in first element. This was in Pascal/Delphi but not the case in C/C++.
Comment: What do you mean by "b has to be allocated"? I'll try this out and see if it works, but can you explain why this works and mine doesn't?
Comment: @JohnJazzer no offence, but allocation (more generally memory management) is one of the core concepts in C/C++. May be it is better to read some books or at least tutorials before jumping to coding? More specifically `int* b;` means "create a variable capable of pointing to memory location with integer(s)". Fine! But no space in memory is allocated for array and you have to do that, hence allocate.
Comment: Ah, beautiful - it worked. Now to figure out what that first line means
Comment: @John: It's because `b` starts out as a brand new pointer to absolutely nowhere (or anywhere).  Such a wild pointer isn't useful for anything.
Comment: Ah this does make sense - I'm new to C++, so I'll have try reading up on it. Thanks!
Comment: @Andrey thow that the shortest patch to fix crash, I doubt it is a good advice for novice programmer to deal with memory management, especially with returning raw pointer from function.
Comment: @Slava I agree, but I just wanted to point what is wrong on micro level. Of course the whole approach is awful.
Answer: You are writing to heap memory that you never claimed.
int* b;

This pointer, having never been initialized, points to an undefined memory address. Then when you use the indexing operator to assign your matches, you are writing to the subsequent bytes following the undefined memory address.
You need to allocate space for storing the results using the 'new[]' operator. Additionally, if you had correctly claimed the memory, you would be assigning the number of match results to the first element in the result array - something that doesn't seem to be your intention.
Take a look at dynamic memory allocation in C++ using the new [] operator.
Answer: This is not perfect but this is much closer to a correct implementation and you should be able to take it further with some work:
#include <iostream>
#include <vector>
using namespace std;

std::vector<int> linearSearch(int* n, int k, int f)
{
  vector <int> a;

  for(int i = 0; i < k; i++)
  {
      if(n[i] == f)
      {
          a.push_back(i);
      }
  }

  return a ;
}

int main() {
  int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
  std::vector<int> result = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);

  for(unsigned int i = 0; i < result.size(); i++)
  {
      cout << "Found at index: " << result[i + 1] << endl;
  }
  return 0;
}

Comment: Why not just return std::vector instead of passing it by reference?
Comment: Good point, old habits from my pure C programming days I suppose, let me adjust.
Answer: If you use std::vector anyway, why not to use it where it is needed the most? Also if you not suppose to modify array by that pointer express that by const pointer:
std::vector<int> linearSearch(const int* n, int k, int f)
{
   std::vector<int> res;
   for(int i = 0; i < k; i++)
        if(n[i] == f) res.push_back(i);
   return res;
}

int main() {
    int c[10] = {4, 4, 6, 3, 7, 7, 3, 6, 2, 0};
    std::vector<int> k = linearSearch(&c[0], sizeof(c)/sizeof(int), 4);
    for(int i = 0; i < k.size(); i++) {
        cout << "Found at index: " << k[i] << endl;
    }
    return 0;
}


Let Y be a random variable with $0\le Y\le 1.$

Question: Let Y be a random variable with $$0\le Y\le 1.$$Show that $$var(Y)\le 1/4 $$ and that $$var(Y)= 1/4 $$ if and only if P(0)=1/2=P(1).
Comment: This is same as http://math.stackexchange.com/questions/412843/variance-inequality
Answer: Since $Y(1-Y)\geq0$ we conclude that
$$
\mathbb{E}Y-(\mathbb{E}Y)^2-\mathrm{var}(Y)=\mathbb{E}Y-\mathbb{E}Y^2=\mathbb{E}(Y-Y^2)\geq0
$$
Thus
$$\mathrm{var}(Y)\leq(\mathbb{E}Y)(1-\mathbb{E}Y)\leq \frac{1}{4}$$
Now equality folds if and only if $\mathbb{E}Y=1/2$ and $Y=Y^2$, $P$-a.s.,
or equivalently $P(Y\notin\{0,1\})=0$. That is if $Y$ is a Bernoulli trial with equal probability.
Answer: If $0\le Y\le 1$ with probability $1$, we have $Y^2\le Y$ with probability $1$.  Thus the variance of $Y$ is $\le \mu-\mu^2$, where $\mu$ is the mean. Note that the function $t-t^2$ attains a maximum of $\frac{1}{4}$ at $t=\frac{1}{2}$.
The maximum is attained precisely if $Y=Y^2$ with probability $1$, and the mean is $\frac{1}{2}$. Thus with probability $1$ we must have $Y=0$ or $Y=1$. The condition $\mu=\frac{1}{2}$ does the rest.  For if $Y=1$ with probability $p\ne \frac{1}{2}$, then $\mu\ne \frac{1}{2}$.


Role added to HttpContext.current.user but isInRole(rolename) is not working

Question: Here is my code about Authentication and Authorization

public class SessionContext
{
    public void setAuthenticationToken(UserAccount userAccount,bool isPersistant,string name)
    {
        string data = new JavaScriptSerializer().Serialize(userAccount.Roles);
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,name,DateTime.Now,DateTime.Now.AddYears(1),isPersistant,data);
        var encryptedCookieData = FormsAuthentication.Encrypt(ticket);
        HttpCookie httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookieData) { HttpOnly=true,Expires=ticket.Expiration };
        HttpContext.Current.Response.Cookies.Add(httpCookie);
    }
}

My Global.asax has this method:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

    if (authCookie != null)
    {
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

        string[] roles = authTicket.UserData.Split(new Char[] { ',' });

        GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(authTicket.Name), roles);

        HttpContext.Current.User = userPrincipal;

        Debug.WriteLine(roles.FirstOrDefault() +" "+ userPrincipal.IsInRole("Admin"));
    }
}

But when I using [Authorize(Roles="Admin")] authorizing not working.
I tried these solutions but didn't work:

I used [CustomAthorize(Roles="Admin")] that explained in StackOverflow
I used Thread.CurrentPrincipal=userPrincipal

Notice: As you can see I added Debug.WriteLine() in my Global.asax to show me userPrincipal role state but always returns false.
Notice: If you can , show me another way to use Authorization and Authentication that not based on OWIN
Answer: I realized the problem is in saving role name to HttpContext it was saving role name with "" in while it have to save without ""


codeigniter-external java scriptfile not working?

Question: codeigniter-external java scriptfile not working?
in my view file:

Start

getquestion.js:
function getquestion() {
    $.get("http://localhost/n/saq/saq/index.php/quizs/getquestion", function (resp) {
        if (resp != null) {
            $("#question").html(resp);
            $("#loading").html("");
        }
    })
}

when i click start button not showing any thing,
but when i navigate to url http://localhost/n/saq/saq/index.php/quizs/getquestion its working. pls help me
Comment: check your onclick event function name..your function name is  getquestion() and you're using like  getQuestion()...change tht first....
Comment: try add `alert('yay');` before `$.get ...` script. If "yay" appear, then the problem is in your `$.get` script. If even "yay" not appear, maybe you have wrong `src` of the .js file
Answer: Which folder you put your .js at?
Usually .js files are take places in assets/js folder, so you'll need:

Or you can ... (more neat)

Just for information considering this is 2017 already

HTML5 don't need us to include the type attribute in

... and you must include <!DOCTYPE html> in the first line of your pages
Answer: try this code,

    Start

js code,
function getquestion() {
            $.get(<?php echo base_url("index.php/getquestions"),function(result){
            if(result != null) {
             //alert(result);
             document.getElementById("question").innerHTML=result;
             }
      });
}


Get fields of referenced entity in view

Question: I have two content types, Project and Resource,
I have a view displaying fields from Projects, and I would like to display fields from resources referenced by projects in that view.
If I Twig debug, I can see the resource fields are available in the view, but I cannot output them using <code>{{ fields.field_project_linked_resource.content }}</code> or similar...
Any help much appreciated!
Answer: You can display information from nodes that are connected through Entity reference fields right in the Views interface.
In your case you're referencing Resources from Projects, so you need to add the Relationship for the Entity reference field, then you will be able to add Fields that you can find on the Resources Content type (they will automatically use the Relationship).
If it's a field that both Content types have (for example Title) then if you don't use the Relationship on it the title will be for the Project node, and if you do make it use the Relationship it will show the Resource title.
Comment: Thats great, thank you. But how do I output the referenced fields in my projects view  twig template? say I have   a field called 'field_resource_thumbnail_bg_col' how would I show that?

Get fields of referenced entity in view


Python Genetic Algorithm by using PyGAD library

Question: I have a function that I want to maximize its value.
But I don't know much how to use PyGad library. What I see on some sites is that they always use default functions like w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6. Where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7).
And there they write the code without write the function just like :
function_inputs = [4,-2,3.5,5,-11,-4.7]  # Function inputs.
desired_output = 44  # Function output.

And add the other parameters
So what if we want to use a polynomial function like w1^3.x1 + w2^2.x2 + w3.x3 + w4.x4 + w5.x5 + w6.x6?
Answer: It is simple. Just write your function in Python.
(w1**3)*(x1) + (w2**2)*(x2) + (w3)*(x3) + (w4)*(x4) + (w5)*(x5) + (w6)*(x6)

For the complete example, please use the next fitness function instead of the function used in this example.
def fitness_func(solution, solution_idx):
    w1, w2, w3, w4, w5, w6 = solution
    x1, x2, x3, x4, x5, x6 = function_inputs
    output = (w1**3)*(x1) + (w2**2)*(x2) + (w3)*(x3) + (w4)*(x4) + (w5)*(x5) + (w6)*(x6)
    fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
    return fitness

Thanks for using PyGAD!


Commerce hook attributes refresh alter

Question: Commerce 1.x (D7) has a hook <code>_commerce_cart_attributes_refresh_alter(&$commands, $form, $form_state)</code> that allows modules to add arbitrary AJAX commands to the array returned from the Add to Cart form attributes refresh. Is there anyway to have something similar in Commerce 2.x (D8)?. I would like to add my custom commands.
Answer: Drupal 8/9 modules often use events to take the place of alter hooks like this one found in Commerce 1.x on Drupal 7. You have to do a little digging to find the place in the code that executes it, but you can look it up with the following info:

An Add to Cart form in Commerce 2.x is an order item add form governed by the "Add to Cart" form mode (a new concept in Drupal 8).
Looking at the configuration for the default order item type, you can see that the "Purchased entity" reference field uses a field widget called "Product variation attributes."
You can find that widget's definition in the ProductVariationAttributesWidget.php plugin contained in the product module, and its formElement() function shows you that it invokes an ajaxRefresh() function on the object to process the Ajax response when attributes are updated.
That class extends ProductVariationWidgetBase where the ajaxRefresh() function is located. You can see that it builds a response array using an Ajax renderer and gives other modules the opportunity to add commands to the response array via the PRODUCT_VARIATION_AJAX_CHANGE event (using a constant defined in the ProductEvents.php class).

If you're still following, the solution then is to create your own event subscriber that subscribes to that event and appends your own commands to the event's $response array. I've created an example module showing how to do this: https://github.com/rszrama/commerce_example3
Comment: Thank you very much for your answer and your example. It's just what I was searching.

Commerce hook attributes refresh alter


SSL chain verification problems

Question: I've installed a new SSL certificate using SHA2 hashing instead of SHA1.  My chain checks out using these online verifiers:

http://www.sslshopper.com/ssl-checker.html
http://www.digicert.com/help/

However this one tells me it can't find the Root CA:

http://www.geocerts.com/ssl_checker

How do I fix this?
Edit:  Derp, here's the URL:  secure.symt.us
This is on Apache2 on CentOS.
I have followed GoDaddy's setup instructions to the letter and restarted the server.
Edit 2, apache vhost conf:
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
SSLCertificateChainFile /etc/pki/tls/certs/gd_bundle-g2.crt

==== [SOLVED] ====
The problem turned out to be that my vhost was defined as <VirtualHost *:443>.  Changing it to the server's IP address <VirtualHost xx.xx.xx.xx:443> fixed it.
There was a catch-all default <VirtualHost _default_:443> albeit without an SSLCertificateChainFile directive.  Grepping /etc/httpd for SSLCertificateChainFile returned only my directive.
Perhaps apache has its own default SSLCertificateChainFile internally and refused to send mine when configured as *:443?
Answer: You're missing the certificate chain. Follow GoDaddy's Instructions, make sure you install their Intermediate certificates (commonly called a chain, or bundle as well)
GoDaddy supports Internet Censorship. You should seriously reconsider funding them. Especially when you can get SSL Certificates for Free.

For Apache 2.2.x, you need all three of the following (for a standard configuration)
SSLCertificateFile /path/to/public/key_file
SSLCertificateKeyFile /path/to/private/key_file
SSLCertificateChainFile /path/to/chain/file

You're likely missing that last one.
Comment: I've followed their instructions to the letter and I still have the error.  Any ideas?
Comment: What version of IIS are you running?
Comment: Hi Chris, Apache 2 on CentOS, not IIS.
Comment: See edit. Post your SSL* configuration lies if that's not the case.
Comment: vhost conf posted.
Comment: Must have the wrong chain file: https://certs.godaddy.com/anonymous/repository.pki?streamfilename=gdig2.crt&actionMethod=anonymous%2Frepository.xhtml%3Arepository.streamFile%28%27%27%29&cid=179406  Running `sha1 /path/to/chain/file` should return "C62FE90D242CA64F1FFD82BFCAAC1AEF41BDD21D".
Comment: OK, I've updated the chain directive to point to the correct file (gdig2.crt) and I've verified the sha1 hash and I've restarted the server, and I STILL get the same error from ssllabs.com/ssltest argh.  Chrome is reporting the proper intermediate cert though, what the heck is going on?
Comment: You cleared the cache on SSL labs to be sure you're actually retesting? Chrome has many intermediate certificates built-in, for misconfigured servers. If SSL labs still reports a problem you must have another SSLCertificateChainFile directive somewhere in your configuration. The stock `httpd.conf` in many Linux distros comes with a lot of weird stuff. Running `httpd -S` will dump the whole configuration as Apache sees it; running it through a `grep -i chain` might reveal what's going on.
Comment: Yep, was clearing the cache each time.  So I finally found it:  using httpd -S showed me that there was a _default_ 443 host, although there were no SSLCertificateChainFile rules other than my one.  I remembered having trouble with another SSL server that was using *:443 for the vhost which was resolved by using the IP address.  I changed this vhost to use the server's IP, and now it's reporting OK all the way through.  Thanks for your help!
Comment: let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/9202/discussion-between-jemminger-and-chris-s)
Answer: Your server is not properly configured to send the required intermediate certificate. See this answer for a more detailed description of why this is a problem, and why you (or even some SSL verifiers) may not notice it during testing.
The missing certificate in your case is http://certificates.godaddy.com/repository/gdig2.crt.
The SSL Labs online test shows this problem as a warning (and, BTW, finds several other problems with your server configuration).
Answer: I have the same trouble, when I see the site with Android the page show an error
of the certificate.
I test the page with https://www.sslshopper.com/ssl-checker.html
and the chain is broken, after a few tests I use this certificate http://certificates.godaddy.com/repository/gdig2.crt like intermediate certificate
for this property SSLCertificateChainFile.
All my hosts are configurated with  and
And this works perfect.
Answer: The problem took me hours of work. Had a wildcard certificate from Global Sign; see www.unfallkassesachsn.de. My mistake: configured SSL ... File in vhosts.d.
After doing this in ssl-global.conf I'd got an A from Qualys.

SSL chain verification problems


How to print karate api configuration settings

Question: I am trying to print karate configuration settings(like readTimeout, connectTimeout etc.) in a test. Below is the sample request and I am configuring readTimeout and would like to print the same. Can someone please help me with this. Thanks in advance
<code>Given url 'http://dummy.restapiexample.com'
And path 'api/v1/employees'
* configure readTimeout = 100000
* print readTimeout //nothing printed
When method GET
Then status 200
And print response
</code>
Answer: This is not currently supported as most teams don't need this. It sounds like you are attempting to write some custom framework using Karate. You are welcome to contribute code as this is an open-source project - or switch to some other framework.


How do I convert SQL getdate() function into a string in dd/mm/yyyy format?

Question: How do I convert SQL getdate() function into a string in dd/mm/yyyy format?
Answer: Try:
select convert(varchar(10),getdate(),103)

There's more info on the different conversion codes here.
Comment: Thanks I was just looking at a clean way to do this.  That worked.
Comment: In case someone gets confused using a format that returns a longer string, the `varchar(10)` is going to truncate your string. You can do the following to avoid this: `CONVERT(VARCHAR, GETDATE(), 21)`.
Answer: Use conversion code 103
convert(varchar, getdate(), 103)

Answer: this worked for me
replace(convert(char(11),getdate(),113),' ','-')

Comment: Please go through the Question before answering. Your answer doesn't seem to provide the correct answer.


kernel tunables for non privileged user

Question: How can i permit non privileged user to set kernel tunables within specific limit. For instance to let user set hugepages up to 40
sysctl -w vm.nr_hugepages=40
Answer: You should be able to do it using sudo + regexp:

user    ALL=(ALL)    NOPASSWD: /sbin/sysctl -w vm.nr_hugepages=[0-4][0-9]

Comment: Thanks for reply! i've tried to put it into **sudoers** file and got the following error
`sysctl: permission denied on key 'vm.nr_hugepages'`

P.S. my distro is Centos 7
Comment: Do you have selinux activated? Try to run setenforce 0 as root then sudo and the command as user
Comment: It looks like it works perfectly with **sudo** in front, Thank you!

kernel tunables for non privileged user


How to freeze an im2txt model?

Question: I fine-tuned im2txt model and obtained the ckpt.data, ckpt.index and ckpt.meta files and a graph.pbtxt file using the procedure in the im2txt github.
The model seems to work well as it produces almost correct captions.
Now I would like to freeze this model to use it on android.
I used the freeze_graph.py script in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py.
python freeze_graph.py --input_graph=/path/to/graph.pbtxt --input_binary=false --input_checkpoint=/path/to/model.ckpt --output_graph=/path/to/output_graph.pb --output_node_names="softmax,lstm/initial_state,lstm/state"

And I have the following error : AssertionError: softmax is not in graph.
The discussion in https://github.com/tensorflow/models/issues/816 is about the same problem but it did not help me very much.
Indeed, when I look in the graph.pbtxt generated after fine-tuning, I cannot find softmax, lstm/initial_state and lstm/state.
But in the show_and_tell_model.py file of im2txt, the names of the tensors seem to be "softmax", "lstm/initial_state" and "lstm/state". So, I don't know what's happening.
I hope I was clear enough about what I've tried so far. Thanks in advance for any help.
Regards,
Stephane
Answer: Ok,
I think I finally got the solution. In case it is useful for others, here it is:
After training, you obtain ckpt.data, ckpt.index and ckpt.meta files and a graph.pbtxt file.
You then have to load this model in 'inference' mode (see InferenceWrapper in im2txt). It builds a graph with the correct names 'softmax', 'lstm/initial_state' and 'lstm/state'. You save this graph (with the same ckpt format) and then you can apply the freeze_graph script to obtain the frozen model.
Regards,
Stephane
Comment: I added
saver = tf.train.Saver()
saver.save(sess, "/tmp/im2txt.ckpt")
in inference_step and build_model of inference_wrapper.py, as well as in _restore_fn of inference_wrapper_base.py. Then I used the generated checkpoint file in python tensorflow/python/tools/freeze_graph.py  --input_graph=graph.pbtxt --input_checkpoint=/tmp/im2txt.ckpt --output_graph=/tmp/im2txt_frozen_graph.pb --output_node_names="softmax,lstm/initial_state,lstm/state" but always get the error AssertionError: softmax is not in graph. Can you post the exact place, file and code you used to fix the problem? Thanks!
Answer: Found and verified the answer: in inference_wrapper.base.py, just add something like saver.save(sess, "model/ckpt4") after saver.restore(sess, checkpoint_path) in def _restore_fn(sess):. Then rebuild and run_inference and you'll get a model that can be frozen, transformed, and optionally memmapped, to be loaded by iOS and Android apps.
For detailed commands to freeze, transform, and convert to memmapped, see my answer at Error using Model after using optimize_for_inference.py on frozen graph.
Comment: Hi,
Sorry it was not clear enough in my answer. Glad you found how to freeze the model.

I am still unable to use my model on Android. I used the commands from your answer and I have the following error when loading the model with TensorflowInferenceInterface :
"Not a valid TensorFlow Graph serialization: Shape must be rank 4 but is rank 5 for 'ResizeBilinear' (op: 'ResizeBilinear') with input shapes: [1,1,299,299,3], [2]."
Would you have an idea about what is wrong?
Regards,
Stephane
Comment: Did you use the transformed file generated with:
bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
--in_graph=/tmp/ckpt4_frozen.pb \
--out_graph=/tmp/ckpt4_frozen_transformed.pb \
--inputs="convert_image/Cast,input_feed,lstm/state_feed" \
--outputs="softmax,lstm/initial_state,lstm/state" \
--transforms='
      strip_unused_nodes(type=float, shape="1,299,299,3")
      fold_constants(ignore_errors=true)
      fold_batch_norms
      fold_old_batch_norms'
Comment: Yes, I used this one. Maybe it's a problem with my frozen model I use before transforming it? In the Java code, I tried "input_feed" and "convert_image/Cast" as input nodes but none of them is working.

Is there a specific version of tensorflow to use (>1.4 for instance)? Still trying to make it work.
Anyway, thanks for taking time to answer and trying to help !

How to freeze an im2txt model?


Solving for probability in equation involving Beta function

Question: Consider the equation
$$p^\alpha(1-p)^\beta = cB(\alpha+1,\beta+1)$$
where $c,\alpha,\beta>0$ and $B(\alpha,\beta)$ is the Beta function.
Question: What is the probability $p$ as a function of $c,\alpha,\beta$?
Comment: The Beta function in this equation is irrelevant. You simply want the inverse of the function $p^\alpha(1-p)^\beta$ which I believe can only be found for specific values of $\alpha,\beta$.
Answer: As  @Peter Foreman commented, you only need to find the zero of function
$$f(p)=p^\alpha(1-p)^\beta-k$$ and, for the most general case, you will need some numerical method.
From a practical point of view, I think that it would be better to consider
$$g(p)=\alpha \log(p)+\beta \log(1-p)-\log(k)$$ Its derivatives
$$g'(p)=\frac{\alpha }{p}-\frac{\beta }{1-p}\qquad \text{and} \qquad g''(p)=-\frac{\alpha }{p^2}-\frac{\beta }{(1-p)^2} <0 \,\,\, \forall p$$ The first derivative cancels at
$$p_*=\frac{\alpha }{\alpha +\beta }$$ which corresponds to a maximum (by the second derivative test. So, if
$$g\left(\frac{\alpha }{\alpha +\beta }\right)=\alpha  \log \left(\frac{\alpha }{\alpha +\beta }\right)+\beta  \log
   \left(\frac{\beta }{\alpha +\beta }\right)-\log (k)$$ is positive, there will be roots to the equation. One root will be $0 < p_1 <\frac{\alpha }{\alpha +\beta }$ and the second one $\frac{\alpha }{\alpha +\beta }< p_2 <1$.
Start the search at the midpoint of the interval and use Newton method.
For example, let us use $\alpha=10$, $\beta=17$ and $c=\frac 1 {15}$. For the first root, the iterates would be
$$\left(
\begin{array}{cc}
 n & p_n \\
 0 & 0.185185 \\
 1 & 0.238691 \\
 2 & 0.258600 \\
 3 & 0.261070 \\
 4 & 0.261105
\end{array}
\right)$$ and, for the second root,
$$\left(
\begin{array}{cc}
 n & p_n \\
 0 & 0.685185 \\
 1 & 0.561947 \\
 2 & 0.503743 \\
 3 & 0.490163 \\
 4 & 0.489394 \\
 5 & 0.489392
\end{array}
\right)$$


why is it that the conjugate of a+bi is a-bi?

Question: if a+bi is an element of a group, then its conjugate is a-bi,
how can we prove this by using the fact that the conjugate of an element g of a group is h if there is an x in the group such that h=xgx^(-1)?
Comment: The *complex* conjugate is defined to be that. This is not exactly the same as conjugation within a group.
Comment: Then the underlying group is abelian, so conjugation is trivial.
Comment: What group are you talking about? what are a and bi?
Comment: ring of gaussian integers
Answer: For a complex number $a+bi$, the complex number $a-bi$ is more properly called the complex conjugate.  This is a distinct use of the word "conjugate" to that in a group, where $x^{-1}gx$ is also called a conjugate of $g$.
Having said that, you could take the multiplicative group $\mathbb{C}^{\ast}$ of units of the field $\mathbb{C}$ and form the semi-direct product with the group $\langle\alpha\rangle$ of order $2$ acting by complex conjugation, and then complex conjugation in that semi-direct product would be realised by group conjugation by $\alpha$: $z^{\alpha} = \bar{z}$, for $z\in\mathbb{C}^{\ast}$.
EDIT: Oops, from your comment, I guess one should instead take the group $U$ of units in the ring $\mathbb{Z}[i]$ of Gaussian integers, rather than $\mathbb{C}^{\ast}$.  But the same idea applies.
Comment: Good reference James. You are spending your Groups in sake of complex analysis? Groups are all we have on this planet.  :D))
Answer: Given a mathematical structure $S$ and a group of automorphisms $G \subseteq \mathrm{Aut}(S)$, we often say that the elements $\sigma(x)$ for $\sigma \in G$ are the conjugates of $x$. And  if $\sigma$ is any particular automorphism, we might call $\sigma(x)$ the conjugate of $x$ by $\sigma$.
For the complex numbers, $\sigma(a+bi) = a-bi$ is an automorphism.
For your group theory, there is a standard homomorphism $G \to \mathrm{Aut}(G)$ that sends any element $g$ to the inner automorphism $\sigma_g(x) := gxg^{-1}$. This mapping from elements to inner automorphisms is so natural, that rather than talk about conjugation by $\sigma_g$, we simply call it conjugation by $g$.
More verbosely, the condition you are thinking of "$h$ is a conjugate of $g$ by an inner isomorphism". But the complex conjugation example is not an inner isomorphism of any (reasonable) group structure on the complexes, so you can't think of it that way.


Windows 7 "Home Group" on a work PC with only one NIC

Question: I am a Domain Admin and am trying to support BYOD with computers that will also be domain connected.  The problem is that once I domain join a PC, the "zone" changes to "domain", while Homegroups require to be set to "home" in order to work correctly.
How can I get HomeGroup connected machines "talk" to each other while domain connected?
In this scenario, imagine a HomePC that is VPN'ed into work, and a work desktop both running Windows 7 Enterprise or Ultimate.  I would like the user's files to be replicated between home and work, (but off our file server).
Answer: It is by design that a domain joined PC cannot connect to a HomeGroup on the same network as that would allow the circumvention of domain-specified security policies and the like (as well as cause confusion about what sort of authentication the machine should use on that network).
This TechNet article tells you how to configure domain joined PCs for use in a homegroup when at home, which you might be able to extend the PCs you're trying to use this method on by adding another NIC... but that would surely complicate things unnecessarily for what you're trying to achieve.
I think you might need to look at a better way for the remote machine to access files - have you tried using Offline Files against the file share? Saving Windows Credentials to the Credential Manager should suffice for authentication purposes.

http://windows.microsoft.com/en-us/windows-vista/Understanding-offline-files
http://technet.microsoft.com/en-us/library/ff633429(v=ws.10).aspx


What does "U.t.c." stand for in the context of a math proof?

Question: What does "U.t.c." stand for in the context of a math proof? This abbreviation appears at the start of some (but not all) proofs in Desoer and Vidyasagar.
"U.t.c." isn't searchable online (conflicts with temps universel coordonné), and isn't on Wikipedia's list of mathematical abbreviations.
Comment: Under this/that/these condition(s)?
Answer: Coming from Google Book's preview of Feedback Systems (which I assume is the book you reference), there is a "note to the reader" section in the front of the book.  Near the bottom of that page:

For convenience the abbreviations u.t.c. for "under these conditions" and s.t. for "so that" are used throughout.
Comment: Note: I'd also accept this as a duplicate of something like "best practices for reaching math textbooks". I only started trying to learn from books recently. I checked the table of symbols (where I expected to find it), but missed "note to the reader" because I thought this just a preface.
Comment: Question: This question was downvoted; Would it be best for the community if I delete it?


MySQL Union Operator not behaving as expected

Question: I usually find the answer to my questions here. If not, I know that is probably because I haven't looked deep enough. However this time none of the answers I looked through fit my needs. So here it goes:
I want to use MySQL server to build a report from two tables:
ID              Email                  ID     Date      Spent
-----------------------              ---------------------------
A123         firstname.lastname@example.com               A123      3.3.14     2.50
B102         email@example.com               A123      7.3.14     3.50
yum          email@example.com                B102      4.4.14     7.00
(null)         (null)

I want to make a report in which for a given timestamp, eg. from 3.1.2014 to 3.31.14, I get a list of all the ID's of the system with the corresponding amount that they have sent, even if they didn't spend anything.
Therefore from the above tables, let's say we want to retrieve the month of March 2014 (from 3.1.2014 to 3.31.14), I would like to get:
ID          Spent
------------------
A123         6.00
B102         7.00
yum          NULL

I don't mind to have NULL or 0's for those users that didn't spend any money. So far I have gotten this:
SELECT ID,
   sum(Spent) AS Spent
FROM expenses
WHERE expenses.date >= '2014-03-01 00:00:00'
  AND expenses.date < '2014-03-31 00:00:00'
GROUP BY expenses.ID
UNION
SELECT users.ID,
   NULL AS Spent
FROM users
WHERE users.ID NOT IN
 (SELECT expenses.ID
   FROM expenses
   WHERE expenses.date >= '2014-03-01 00:00:00'
   AND expenses.date < '2014-03-31 00:00:00'
 GROUP BY expenses.ID)
ORDER BY ID;

You can check the above code in here.
It works as expected, but in the real case of my database, the amount of rows in the result query is greater that the amount of unique rows in users.
I have checked that all the ID's in the expenses table are in the users tables, which is the case. I have tested with:
SELECT ID FROM expenses
  WHERE expenses.date >= '2014-03-01 00:00:00'
 AND expenses.date < '2014-03-31 00:00:00'
 AND ID NOT IN (SELECT users.ID FROM users);

and it returns an empty set, which is as expected.
I must be missing something, but I have no clue of what. Could someone please give some insights? I am pretty new to MySQL, and maybe there is a better way of doing this.
Comment: The most likely explanation for the behavior you observe is that there are rows in `expenses` with `ID` values that don't match any `ID` value in `users`. The first part of your query doesn't restrict the values of `ID` returned from `expenses`. One way to identify those `ID` values from expenses: **`SELECT e.ID FROM expenses e LEFT JOIN users u ON u.ID=e.ID WHERE u.ID IS NULL GROUP BY e.ID`**. See Gordon's answer for an example of a query that returns the specified resultset.
Answer: I think you want a left outer join rather than a union:
SELECT u.ID, sum(e.Spent) AS Spent
FROM users u left outer join
     expenses e
     on u.id = e.id and
        e.date >= '2014-03-01 00:00:00' and
        e.date < '2014-03-31 00:00:00'
GROUP BY u.ID;

Note in the data you have in the SQL Fiddle, this will return four rows, because of the NULL valued row in users.
Comment: @tokenizer_fsj: That query uses an "anti-join" pattern, effective (and usually the most efficient query pattern) for identifying rows that don't have a match, essentially the inverse of the rows returned by Gordon's query above. To "fix" your original query, you'd add an INNER JOIN to the `users` table in the top part of your query. The `LEFT JOIN` (as demonstrated in Gordon's answer, is a (usually) more efficient pattern to get the "non-matching" rows. Suitable indexes should markedly improve performance of the JOIN operation.
Comment: A covering index **`... ON expenses (id, date, spent)`** will likely improve performance. (Likely there's already an index `on users(id)` . It strikes me a bit odd that the date range includes only the first 30 days of March, and not the whole month. (Nothing wrong with getting the first thirty days, it's just odd.) To get the whole month, we'd typically use something like **`e.date >= '2014-03-01' AND e.date < '2014-03-01' + INTERVAL 1 MONTH`**.
Comment: +1. a `LEFT JOIN` is the way to go. Adding a **`WHERE users.ID IS NOT NULL`** would eliminate that "extra" NULL row from `users`. An easy way to replace a NULL from the SUM aggregate with a zero is to wrap it in a `IFNULL()` function...  **`IFNULL(sum(e.Spent),0)`**
Comment: Thanks for your help guys, really appreciated. @spencer7593 `SELECT e.ID FROM expenses e LEFT JOIN users u ON u.ID=e.ID WHERE u.ID IS NULL GROUP BY e.ID` worked like charm to identify Id's that where on e.ID and were not on u.ID. I thought it u.ID had all the Id's and it turned out that not....                                               @gordon-linoff Thanks a lot, I am running the query now, but I have to say it is significantly slower than my union. Keep in mind that expenses.ID has 4K records and users.id has 100K. Thank you once more!
Answer: you can do a join of two tables (Users and Expenses) to get the same result (http://sqlfiddle.com/#!2/738ea6/5). This will be much faster and an understandable query.
SELECT expenses.ID,
   sum(Spent) AS Spent
FROM expenses, users
WHERE expenses.date >= '2014-03-01 00:00:00'
  AND expenses.date < '2014-03-31 00:00:00'
  AND users.ID = expenses.ID
GROUP BY expenses.ID
ORDER BY ID;

Comment: You'd need an outer join to return rows from `users` that don't have any matching rows in `expenses`.
Answer: Group by in second query?
    SELECT ID,
   sum(Spent) AS Spent
FROM expenses
WHERE expenses.date >= '2014-03-01 00:00:00'
  AND expenses.date < '2014-03-31 00:00:00'
GROUP BY expenses.ID
UNION
SELECT users.ID,
   NULL AS Spent
FROM users
WHERE users.ID NOT IN
 (SELECT expenses.ID
   FROM expenses
   WHERE expenses.date >= '2014-03-01 00:00:00'
   AND expenses.date < '2014-03-31 00:00:00'
 GROUP BY expenses.ID)
GROUP BY ID
ORDER BY ID;


Trying to implement the Jensen-Shannon Divergence for Multivariate Gaussians

Question: Given two multivariate Gaussian distributions $P \equiv \mathcal{N}(\mu_p, \Sigma_p)$ and $Q \equiv \mathcal{N}(\mu_q, \Sigma_q)$, I am trying to calculate the Jensen-Shannon divergence between them.
I am following JSD-discussion for multivariate Gaussian in this discussion in this question. There it is suggested that one can approximate the midpoint measure $M$ using Monte Carlo sampling.
Specifically, it is pointed out that the JSD for continuous RVs (in my case Gaussian), is given by
$$
\mathrm{JSD} = \frac{1}{2} (D_{KL}(P\,\|M)+ D_{KL}(Q\|M)) = h(M) - \frac{1}{2} (h(P) + h(Q)) \>,
$$
where $h(P)$ and $h(Q)$ are just the differential entropies for the MVN. These properties are well known and we can calculate them easily, e.g.
$$
h(P) = \frac{1}{2} \log_2\big((2\pi e)^n |\Sigma_p|\big)
$$
What is causing me trouble is $M$. I believe I have misunderstood/not-implemented-correctly the Monte Carlo estimate for it.
User FrankD says that for the JSD approximation:
$$
JSD(P\|Q) = \frac{1}{2} (D_{KL}(P\|M)+ D_{KL}(Q\|M))
$$
we can use Monte Carlo estimates for the individual components. The Kullback-Leibler divergence is defined as:
$$
D_{KL}(P|M) = \int P(x) log\big(\frac{P(x)}{M(x)}\big) dx
$$
The Monte Carlo approximation of this is:
$$
D_{KL}^{approx}(P|M) = \frac{1}{n} \sum^n_i log\big(\frac{P(x_i)}{M(x_i)}\big)
$$
where the $x_i$ have been sampled from $P(x)$, which is easy as it is a Gaussian in our case. As $n \to \infty, D_{KL}^{approx}(P|M) \to KLD(P|M)$. $M(x_i)$ can be calculated as
$$
M(x_i) = \frac{1}{2}P(x_i) + \frac{1}{2}Q(x_i)
$$.
Here is my attempt:
import numpy as np
from scipy.stats import multivariate_normal as MVN

def jsd(mu_1: np.array, sigma_1: np.ndarray, mu_2: np.array, sigma_2: np.ndarray):
    """
    Monte carlo approximation to jensen shannon divergence for multivariate Gaussians.
    """
    assert mu_1.shape == mu_2.shape, "Shape mismatch."
    assert sigma_1.shape == sigma_2.shape, "Shape mismatch."

    # Monte Carlo samples
    MC_samples = 1000

    # Take MC samples
    P_samples = MVN.rvs(mean=mu_1, cov=sigma_1, size=MC_samples)
    Q_samples = MVN.rvs(mean=mu_2, cov=sigma_2, size=MC_samples)

    P = lambda x: MVN.pdf(x, mean=mu_1, cov=sigma_1)
    Q = lambda x: MVN.pdf(x, mean=mu_2, cov=sigma_2)
    M = lambda x: 0.5 * P(x) + 0.5 * Q(x)

    P_div_M = lambda x: P(x) / M(x)
    Q_div_M = lambda x: Q(x) / M(x)

    D_KL_approx_PM = lambda x: (1 / MC_samples) * sum(np.log2(P_div_M(x)))
    D_KL_approx_QM = lambda x: (1 / MC_samples) * sum(np.log2(Q_div_M(x)))

    return 0.5 * D_KL_approx_PM(P_samples) + 0.5 * D_KL_approx_QM(Q_samples)

Suffice to say, this does not quite produce what it should.
Comment: The code seems ok, although the definition of the KL-divergence uses natural log, not base 2 (but this should only mean that you're off by a factor of log(2)). You say it does not produce what it should -- how do you know?
Comment: I think problem might be that the PDF of M is not equal to 1/2(P+Q)
Answer: Actually, using the answer in https://stackoverflow.com/questions/26079881/kl-divergence-of-two-gmms (and the fact, that the author factored out the 1/2 from the logarithm, made the montecarlo approximation sample from both distributions to average the result), I would say, that the symmetrized numerical code for jensen shannon divergence using monte carlo integration, even for general scikit.stats distributions (_p and _q), should look like this:
def distributions_js(distribution_p, distribution_q, n_samples=10 ** 5):
    # jensen shannon divergence. (Jensen shannon distance is the square root of the divergence)
    # all the logarithms are defined as log2 (because of information entrophy)
    X = distribution_p.rvs(n_samples)
    p_X = distribution_p.pdf(X)
    q_X = distribution_q.pdf(X)
    log_mix_X = np.log2(p_X + q_X)

    Y = distribution_q.rvs(n_samples)
    p_Y = distribution_p.pdf(Y)
    q_Y = distribution_q.pdf(Y)
    log_mix_Y = np.log2(p_Y + q_Y)

    return (np.log2(p_X).mean() - (log_mix_X.mean() - np.log2(2))
            + np.log2(q_Y).mean() - (log_mix_Y.mean() - np.log2(2))) / 2

print("should be different:")
print(distributions_js(st.norm(loc=10000), st.norm(loc=0)))
print("should be same:")
print(distributions_js(st.norm(loc=0), st.norm(loc=0)))

For noncontinuous, change .pdf to probabilities of samples.


Transform bivariate uniform variable

Question: Let $X_1 = U(0,1)$ and $X_2 = U(0,1)$. $X_1$ and $X_2$ are independent. Then $f(x_{1}, x_{2})=1, {0}\le{x_1}\le{1}, {0}\le{x_2}\le{1}$.
Let $Y_1 = \arctan(X_{2}/X_{1})$, $Y_2 = X_2$. I need to find the density function $g(y_1)$.
Here's what I've done so far.
$X_{2}/X_{1} = \tan(Y_{1})$
$X_{1} = X_{2} / \tan(Y_{1}) = X_{2}\cot(Y_{1})$
Since $X_{2}=Y_{2}$, $X_{1}=Y_{2}\cot(Y_{1})$.
The Jacobian of the transformation is give by the matrix $J = \begin{bmatrix} \frac{\partial{y_2\cot(y_1)}}{\partial{y_1}} & \frac{\partial{y_2\cot(y_1)}}{\partial{y_2}} \\ \frac{\partial{y_2}}{y_1} & \frac{y_2}{y_2} \end{bmatrix} = \begin{bmatrix} -y_{2}\csc^{2}(y_{1}) & \cot(y_1) \\ 0 & 1 \end{bmatrix}$.
So, $\det(J)= -y_{2}\csc^{2}(y_{1})$.
At this point, I am kinda lost. I know that I need to compute $g(y_1, y_2)$, find its domain, and then integrate with respect to $y_2$. I do not understand how to find the joint density $g(y_1, y_2)$, could you please help?
Comment: The [change of variable formula](https://en.wikipedia.org/wiki/Probability_density_function#Function_of_random_variables_and_change_of_variables_in_the_probability_density_function) indicates how to get from $f(x_1,x_2)$ to $g(y_1,y_2)$:$$g(y) = f\big(h^{-1}(y)\big) \left| \frac{d}{dy} \big(h^{-1}(y)\big) \right|$$
Answer: Find the distribution function of $Y_1$ with a picture, then differentiate it.
Observe that because $(X_1,X_2)$ lies in the first quadrant and $Y_1$ is the angle subtended by this point, $0\le Y_1 \le \pi/2.$  Moreover, for any possible value $\theta$ in this interval, the event $Y_1 \le \theta$ is the region in the unit square bounded above by the line at angle $\theta,$ highlighted in this diagram:

Because $(X_1,X_2)$ is uniformly distributed on the unit square, the probability of any event equals its area.
Clearly when $0\le \theta\le \pi/4,$ where this event is a triangle, its area is half its height:
$$\Pr(Y_1 \le \theta) = \frac{1}{2}\tan\theta, \quad \theta \le \pi/4.$$
Its derivative is $\sec^2(\theta)/2.$
When $\pi/4\le\theta\le \pi/2,$ the event is the complement of a triangle, immediately giving
$$\Pr(Y_1 \le \theta) = 1 - \frac{1}{2}\cot\theta, \quad \pi/4 \le \theta \le \pi/2.$$
Its derivative is $\csc^2(\theta)/2 = \sec^2(\pi/2-\theta)/2.$
Combining these results into a common formula gives

$$g(\theta) = \frac{1}{2}\sec\left(\min\left(\theta, \frac{\pi}{2}-\theta\right)\right)^2,\quad 0\le\theta\le\frac{\pi}{2}.$$

Of course $g\equiv 0$ for all other arguments.
To illustrate $g,$ here is a histogram of a million realizations of $Y_1$ (computed with the R code n <- 1e6; y <- atan2(x2 <- runif(n), x1 <- runif(n))) over which the graph of $g$ is plotted in red.
Answer: The transformation from $(x_1,x_2)$ to $(y_1,y_2)$ is given by $h(x_1,x_2) = (h_1(x_1,x_2),h_2(x_1,x_2))^t = \left(\arctan(\frac{x_2}{x_1}), x_2\right)^t$. The jacobian matrix $J(x_1,x_2)$ is given by:
$$
J(x_1,x_2)
 =  \begin{bmatrix}
\frac{\partial h_1}{\partial x_1} & \frac{\partial h_1}{\partial x_2} \\
\frac{\partial h_2}{\partial x_1} & \frac{\partial h_1}{\partial x_2}
\end{bmatrix}
 =  \begin{bmatrix}
\frac{-x_2}{x_1^2 + x_2^2} & \frac{x_1}{x_1^2 + x_2^2} \\
0 & 1
\end{bmatrix},
$$
with the absolute value of the determinant $|J(x_1,x_2)| = \frac{x_2}{x_1^2 + x_2^2}$.
As mentioned by Xi'an in the comments, you can find the density probability function $g(y_1,y_2)$ of $(Y_1,Y_2)$ with the following formula:
$$
g(y_1,y_2) = \frac{f(x_1,x_2)}{|J(x_1,x_2)|}|_{(x_1,x_2) = h^{-1}(y_1,y_2)} = \frac{x_1^2 + x_2^2}{x_2}|_{(x_1,x_2) = h^{-1}(y_1,y_2)},
$$
$$
= \frac{y_2^2\cot(y_1)^2 + y_2^2}{y_2} = y_2(\cot(y_1)^2 + 1),
$$
where $f(x_1,x_2)$ is the probability density function of $(X_1,X_2)$.
Note that this works only for bijective transformations (mapping) $h(x_1,x_2)$.
Comment: To find the marginal of $Y_1,$ then, you would integrate out $y_2$ in $g,$ giving $(\cot(y_1)^2+1)/2.$ Clearly that's not correct for $y_1 \le \pi/4:$ it blows up near $y_1=0.$ For $\pi/2\le y_1\le \pi/2$ it can also be expressed as $\sec(\pi/2-y_1)^2/2,$ which is correct.

Transform bivariate uniform variable


ArrayBlockingQueue - How to "interrupt" a thread that is wating on .take() method

Question: I use an ArrayBlockingQueue in my code. Clients will wait until an element becomes available:
myBlockingQueue.take();

How can I "shutdown" my service in case no elements are present in the queue and the take() is waiting indefinitely for an element to become available? This method throws an InterruptedException. My question is, how can I "evoke" an Interrupted Exception so that take() will quit? (I also tought about notify(), but it seems I doesn't help here..)
I know I could insert an special "EOF/QUIT" marker Element but is this really the only solution?
UPDATE (regarding the comment, that points to another question with two solutions):
one mentioned above using a "Poisoning Pill Object" and the second one is Thread.interrupt():
The myBlockingQueue.take() is used NOT in a Thread (extending Thread) but rather implements Runnable. It seems a Runnable does not provide the .interrupt() method?
How could I interrupt the Runnable?
Comment: Very similar question: http://stackoverflow.com/questions/812342/how-to-interrupt-a-blockingqueue-which-is-blocking-on-take
Answer: You can implement the Runnable like this. (It assumes you have started the Runnable before you attempt to interrupt it)
class MyRunnable implements Runnable {
    private Thread thisThread;
    public void run() {
       thisThread = Thread.currentThread();
       try {
          // do some work
       } catch(Throwable t) {
          t.printStackTrace(); // or log the error.
       }
    }
    public void interrupt() {
       thisThread.interrupt();
    }
}

Answer: The straightforward way is to keep a reference of the thread you use to run the runnable and interrupt it on shutdown.
Alternatively you can use thread pools to organize the threads for a service. Then the threadpool takes care of keeping track of the threads and managing them, including  interrupting them when shutting down.
Yet another (hacky and not recommended way) way is to enumerate all running threads in the JVM (using the ThreadGroup's method) and try to guess which thread runs your runnable (e.g. by analyzing the stack of each thread).
Answer: Found the solution:
Either use, as described in the question an "Poisoning Object"
or
Interrupt the Thread eitehr with this.interrupt() or if it is a Runnable use Thread.currentThread().interrupt();
Comment: First part of the answer is correct, the second is nonsense. If you execute Thread.currentThread().interrupt(), you are interrupting the current thread, and if you can execute that you are clearly not blocked anywhere else.


Fourier transform problem with symmetric matrix. Related to Gaussian?

Question: Hi everyone I encountered a problem that looks simple enough but I have no idea where to start.

Find Fourier transform of $e^{-\langle Ax,x\rangle}$ when $A$ is a positive definite symmetric $n \times n$ matrix.

Since $A$ can be any symmetric positive definite matrix, I have no idea how to apply Fourier transform formula I learned in class. I does look like Gaussian, but I really need some help.
Help would be very much appreciated. Thank you!
Answer: $U^tAU=D$ where $D$ is diagonal and $U^tU=I$. Use $U$ to change variables in the multi-dimensional transform integral. Starting with
$$
            \int_{\mathbb{R}^n}e^{-(Ax,x)}e^{ix\cdot\xi}dx,
$$
Let $x=Uy$. Because $U$ is orthogonal, then $\|x\|=\|Uy\|=\|y\|$ gives a constant Jacobian equal to $1$. Therefore, the above is transformed to
$$
           \int_{\mathbb{R}^n}e^{-(U^tAUy,y)}e^{iy\cdot(U^t\xi)}dy
   = \int_{\mathbb{R}^n}e^{-(Dy,y)}e^{iy\cdot U^t\xi}dy \\
   = \left.\int_{\mathbb{R}^n}e^{-\alpha_1 y_1^2}e^{-\alpha_2 y_2^2}\cdots e^{-\alpha_n^2y_n^2}e^{iy\cdot\eta}dy\right|_{\eta=U^t y}.
$$
The $\alpha_j$ are the diagonal elements of the diagonal matrix $D$, which are the eigenvalues of $A$. By assumption $\alpha_j > 0$ for all $j$. So everything reduces to single variable Gaussian transforms, with a final evaluation at $\eta=U^t \xi$.
Answer: Hint: Since $A$ is an SPD matrix, it has a unique SPD square root.  Apply the change of variables
$$
y = A^{1/2}x
$$
Noting that
$$
\langle Ax,x \rangle =
\langle A^{1/2}x,A^{1/2}x \rangle
$$


Como redirecionar para uma url externa utilizando React useEffect?

Question: Estou tentando redirecionar para uma url externa ,utilizando a função vaiparala passando um parametro
que eu pego a partir do click, o caminho chega na funçao corretamente mas o redirecionamento não
é feito , o que eu posso estar fazendo de errado ?
React.useEffect(() => {
    const vaiparala = (caminho) => {
      console.log(caminho);
      caminho === 'Linkedin' ? (
        <Link to={"https://www.linkedin.com/feed/"} />
      ) : (
        history.push(`/${caminho}`)
      );
    };
    const handleClick = (e) => {
      const items = document.getElementsByClassName('context-menu-item');

      if (items && items.length > 0) {
        let count = 0;
        for (let i = 0; i < items.length; i++) {
          if (items[i].contains(e.target)) {
            vaiparala(rotasClick[i]);
            count++;
          }
        }
        if (count === 0) {
          setActive(false);
        }
      } else {
        setActive(false);
      }
    };
    document.addEventListener('click', handleClick);
    return () => {
      document.removeEventListener('click', handleClick);
    };
  }, []);

Answer: Você não pode redirecionar para uma URL externa usando react-router - Link, Redirect ou history.push.
Mas você pode fazer isso: window.location.href = 'https://www.linkedin.com/feed/' ou window.location.replace('https://www.linkedin.com/feed/').
React.useEffect(() => {
    const vaiparala = (caminho) => {
      console.log(caminho);
      caminho === 'Linkedin' ? (
        window.location.href = 'https://www.linkedin.com/feed/'
        // ou
        window.location.replace('https://www.linkedin.com/feed/')
      ) : (
        history.push(`/${caminho}`)
      );
    };
    // ...

Comment: Funcionou perfeitamente , muito obrigado !


What does a small slash through a line on the staff mean?

Question:
The slash mark is shown multiple times in the piece, and it's not just shown on the top line. The slash goes between notes and on any of the staff lines. I just want to know what it signifies.
Comment: Please post the name & composer so that we can compare other scores.
Answer: It's a tiny caesura, often a breath mark.  With bowing instructions penciled in, it wouldn't be a breath instruction even though the violinist apparently is considered to be able to catch enough of a breath to figure out whether they are bowing in the wrong direction: the immediately following note has an "upbow" instruction on it.
Comment: Wikipedia shows the caesura as two slashed lines. Is there anything special about this one-line version? https://en.wikipedia.org/wiki/Caesura
Comment: @YourUncleBob - the Wikipedia entry also says "It may be a comma, a tick, or two lines, either slashed (//) or upright (||)".  But there is a difference: a caesura is a pause, a temporary halt in the rhythm, and a breath mark (which is typically shaped like a comma) doesn't change the rhythm - you take a breath between the notes, but the next note comes in at the same time as it would if no mark was present.
Comment: @TomSerb Are you saying this one-line version is actually more like a breath mark, and not a caesura?
Comment: @YourUncleBob - it's really difficult to say from just a fraction of a measure here.  There's a ritardando, and they are often followed by a fermata and/or caesura, but I don't see an "a tempo" instruction.  I think it was probably handwritten - it's thicker than the stems, and about the same thickness as the bowing marks.  Tick marks for caesura are pretty rare in printed scores - so I'm guessing the same player that added the bow marks put it in as a reminder for some instruction from the conductor (which means it's would probably be a caesura).  But I'm not 100% sure.
Comment: I agree that it's most likely a breath-mark, but it'd be much easier to be sure if we could examine the full score.  Let's see if the OP gives us more info


How to change the image style for image preview in node-add/edit form?

Question: IN a related question, "How to set image style for media browser in node edit form?" one response noted that "Once you have chosen the image and clicked save and the overlay is closed, images are display in the "Media thumbnail" image style."
How can I change this automatic behavior such that the preview image is displayed in my custom image style?
Answer: If you are using the Media module, I couldn't find a way to change it so it doesn't use the "Media thumbnail" image style. But what you could do is edit the "Media thumbnail" image style by going to <code>/admin/config/media/image-styles/edit/media_thumbnail</code> and clicking on the override button which will let you edit it to your liking. (Tested on Drupal 7)

If you're not using the Media module, you can by going to your content type's Manage Fields and clicking on edit for your image field. Scroll down a bit until you see Preview image style
Comment: Thank you, @sssweat. I would like to change the thumbnail for one content type. If I override the default Media thumbnail, the preview image style will change in all content types.


What is the value of $\sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n^p}$ for different values of p?

Question: Consider this series:
$$\sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n^{p}}$$
For p=1, using the comparison test:
$$\sum_{n=2}^{\infty} \frac 1{1+n}>\sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n} > \sum_{n=2}^{\infty} \frac 1{1-n}$$
These are both harmonic and so they are divergent thus for p=1 the series diverges.
For p=2, $$\frac 12 \pi \coth \pi-1=\sum_{n=2}^{\infty} \frac 1{1+n^2}>\sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n^2} > \sum_{n=2}^{\infty} \frac 1{1-n^2}=-\frac 34 \\ \therefore \frac 12 \pi \coth \pi-1>\sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n^2} >-\frac 34$$
So for p=2 it is convergent. The exact answers follow from Parseval's theorem.
For p>1 (integer),
$$\sum_{n=2}^{\infty} \frac 1{1+n^{p}}>\sum_{n=2}^{\infty} \frac 1{1+n^{p+1}}>\sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n^{p+1}}> \sum_{n=2}^{\infty} \frac 1{1-n^{p+1}}> \sum_{n=2}^{\infty} \frac 1{1-n^{p}}$$
Thus by induction using p=2 as a basis, the series converges for all p>1. This identity also shows that:
$$\vert \sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n^{p}} \vert > \vert \sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n^{p+1}} \vert $$
My questions are: What are the values of the series for different values of p and what is the value of $\lim_{p\to \infty} \sum_{n=2}^{\infty} \frac 1{1+(-1)^n\cdot n^{p}}$?
Comment: No the limit is $\log(2)-1= -0.3068528$
Comment: Your study of the case $p=1$ is not correct. Note that you can only conclude that $$- \infty \le \sum_{n=2}^{\infty} \frac{1}{1+(-1)^nn} \le + \infty$$ which is something obvious and does not give you any information about the convergence of the series.
Comment: The limit equals zero.
Comment: Crostul I thought the comparison test said that if series 1  > series 2 and series 2 diverges, then series 1 diverges - apparently not.
Comment: HH Rugh how did you derive that limit?
Comment: The comparison test is for series of positive terms.  You can find a convergent series where the terms are larger than some negative series that diverges, right?
Answer: Partial answer here.
$$\begin{align*}
\sum_{n=2}^\infty \frac{1}{1 + (-1)^n n^p} & = \sum_{n=1}^\infty \frac{1}{1+(2n)^p} + \sum_{n=1}^\infty \frac{1}{1-(2n+1)^p} \\
& \le 2^p \sum_{n=1}^\infty \frac{1}{n^p} - \sum_{n=1}^\infty \frac{1}{(2n+1)^p-1} \\
& \le  2^{-p} \zeta(p) - \sum_{n=1}^\infty \frac{1}{(2n+1)^p} \le \frac{\zeta(p)}{2^p}.
\end{align*}$$
The right hand side clearly converges to zero.
$$\begin{align*}
\sum_{n=2}^\infty \frac{1}{1 + (-1)^n n^p} & = \sum_{n=1}^\infty \frac{1}{1+(2n)^p} + \sum_{n=1}^\infty \frac{1}{1-(2n+1)^p} \\
& \ge \sum_{n=1}^\infty \frac{1}{(3n)^p} - \sum_{n=1}^\infty \frac{1}{(2n+1)^p-1} \\
& \ge\sum_{n=1}^\infty \frac{1}{(3n)^p} - \frac{1}{3^p-1}- \sum_{n=2}^\infty \frac{1}{(1.5n)^p} \\
& = \left(\frac{1}{3^p} - \frac{2^p}{3^p}\right) \zeta(p) - \frac{1}{3^p-1}\end{align*}
$$
Maybe the estimations used here aren't very clever, but at least they work (for big enough $p$?) and you can conclude that your limit is indeed zero.
Comment: @H.H.Rugh can you check the computations?
Comment: Nice work - thanks!
Answer: As Rugh said: Case $p=1$.
$$
\sum_{n=2}^\infty \frac{1}{1+(-1)^n\;n}=
\sum_{k=1}^\infty \left(\frac{1}{1+2k}+\frac{1}{1-(2k+1)}\right)
=-\sum_{k=1}^\infty\frac{1}{2k(2k+1)} = -1+\log 2
$$
Case $p=2$:
$$
\sum_{n=2}^\infty \frac{1}{1+(-1)^n\;n^2}=\frac{\pi\coth(\pi/2)-3}{4}
$$
(series by Maple)
Maple does $p=3$ and $p=4$ in terms of the digamma function.  Higher ones will involve digamma evaluated at the zeros of a certain polynomial.  When $p>1$ the series converges absolutely, so you can do the even and odd cases separately and add.
Comment: Thanks - I was naive to hope for a nice answer devoid of special functions :D


How to activate a conda virtualenv in a Jupyter Notebook terminal?

Question: In a Jupyter Notebook terminal (used from a browser) I created a new conda environment. But when I tried to activate it using:
source activate <env_name>

But I got following error:

/bin/sh: 5: source: not found

I also tried:
activate <env_name>

and
conda activate <env_name>

but both of these approaches haven't worked. I'm using Anaconda 3 on
Distributor ID: Ubuntu
Release:        16.04
Codename:       xenial

Answer: I solved this way (your conda.sh could be in a different path).
In the same jupyter cell, paste this:
%%bash
source /opt/miniconda/etc/profile.d/conda.sh
conda activate <your_environment_name>


Why can we divide by zero in limits?

Question: Before I ask, I want to tell you that I am beginner in limits, so you may find some problems in my understanding.
Let's assume a function $f(x) = 15-2x^2$.  We want to know how the function behaves at $x=1$.  Specifically, we want to know the slope of tangent line at $x=1$.
Simply, we get a good formula for that by doing this: $$m=\frac{f(1)-f(x)}{x-1}.$$ Then we get the equation $$m=\frac{2-2x^2}{x-1}.$$
Now we have to take the limit to find the slope of the tangent line, $$\lim_{x\to 1} \frac {2-2x^2}{x-1}.$$
To solve this we simplify it like this :
\begin{align}
\lim_{x\to 1} \frac {2-2x^2}{x-1}
 =& \lim_{x\to 1}\frac {-2(x-1)(x+1)}{(x-1)} \\
 =& \lim_{x\to 1}-2(x+1) \\
 =& -2(1)-2 \\
 =& -4
\end{align}
In algebra class when we had a fraction and we wanted to cancel something we always say $x \ne a$. For instance $\frac {1}{x-1}$. Here $x \ne 1$, because $x-1$ would be zero.
But here in limits I found something unbelievable: here we are dividing by zero and that's forbidden.
$$\lim_{x\to 1}\frac {-2(1-1)(1+1)}{(1-1)}.$$
We are just canceling zero in this fraction.  Can anyone explain this?
Comment: Simply put, **we don't**. These expressions are not $0$ but they approach $0$. There's a difference.
Comment: The limit is about what happens *near* $x=1$, not *at* $x=1$.  *At* $x=1$, the expression you are working with is undefined.  However, at any point *near* $x=1$, the function is perfectly well defined.
Comment: @ParasKhosla , But we plug 1 !
Comment: Yes because for $x-1$ and $x\to 1$ that's what it *approaches*.
Comment: Why we don't plug number that approach from $1$.Always $1=1$ not approaching from $1$
Answer: Intuitively, what the limit is doing is finding the behavior of the fraction $$\frac{-2(x-1)(x+1)}{(x-1)}$$ as $x$ approaches $1$.  The actual value of the fraction at $1$ is irrelevant, and in this case undefined.  Therefore you can 'cancel' out the terms $(x-1)$ because everywhere other than $x = 1$ $$\frac{-2(x-1)(x+1)}{(x-1)} \quad \text{and}\quad -2(x+1)$$ are equal, and so their limit as $x$ approaches $1$ will also be equal.
Another way to think about it is that the function $$f(x) = \frac{-2(x-1)(x+1)}{(x-1)}$$ has a hole in the graph at $x = 1$, whereas the graph of the function $$g(x) = -2(x+1)$$ looks exactly the same except that the hole has been filled in.
Comment: Yes, the two functions are the same except at $x = 1$.
Comment: i think here , you mean that we are changing the equation of the function to convert it to another function, but the two functions are equal in value, but the other function avoids division by zero , Right ????
Answer: You are actually canceling the factor $x-1$ from numerator and denominator.  This works as long as $x \ne 1$.  Keep in mind that in the limit, $x$ is approaching $1$; never actually equal to $1$.
Comment: @MohammadAlshareef : That's ONLY because after the cancellation, what remains is a function $f$ that is *continuous* at $x=1$. It is only by continuity of $f$ that you can then say that $\lim\limits_{x\to1}f(x)=f(1)$. Before you cancel, the original expression is *not* continuous at $x=1$ (it isn't even *defined* there!), so it is not allowed to plug in $x=1$.
Comment: Ok , But we at last we plug 1 in the equation !
Comment: Cancellation is forbidden , I am with you that it approach from one and not equal one, But we plug one , Specifically , We say that it approach from one then we plug one in the limit !
Comment: @MohammadAlshareef  : i think we use limits to Cancel or avoiding division by zero only. because x→1 means x approach 1 not equal 1 , so we can make some magic things like multplication or summation to the function to convert it to ( another function without divition by zero unlike the original function ) , and after that we cancel the limit because his job is finished to us , and then we put 1 to the function to get the value or the slope , i dont know if i am right or wrong but that is my brain took me to :)

Why can we divide by zero in limits?


Question about decreasing sequence.

Question: Problem: Imagine an infinite chessboard that contains a positive integer in each square. If the value of each square is equal to the average of its four neighbors to the north, south, west, and east, prove that the values in all the squares are equal.
I understood the problem can be proved by using the decreasing sequence. I think it is false when 'positive integer' changed to 'positive real number', but I can't prove it.
Question: Is it still true when 'positive integer' changed to 'positive real number'?
Thank you for your advice.
Comment: [Answered here](https://math.stackexchange.com/questions/51926/a-stronger-version-of-discrete-liouvilles-theorem). Generally the assumption strongly resembles the definition of a harmonic function, which by a theorem of Liouville must be constant if bounded from below. It seems that the same is true in the discrete case.
Comment: Hi, genuinely curious here. Would you mind illustrating how you solved the case with positive integers?
Comment: Choose some square in chessboard(called 'first square'). If  "The values in all the squares are equal" is false, there exists a square around the first square such that a number in square is smaller than a number in the first square. Using this property, we can make the decreasing sequence.
Comment: Ah, I see. I think then that you are correct - this is not true for positive real numbers. But I think this is tricky to prove. The only way I see of doing this is actually constructing a solution which could be very difficult.
Comment: Thanks so much.
Answer: Intuitively speaking:
W.L.O.G let, $f(0,0)=0$. There is no minimum at the lattice points.
And let, for $x,y \to -\infty$ $f(x,y)$ converges to $M$.
So, for some $x,y<<0$, let, $|f(x,y)-M|<\epsilon$, for some $\epsilon >0$.
The 4-tuple of differences between the points and it's neighbours will be like $(-\epsilon{_1}, -\epsilon'{_1}', -\epsilon{_2}, \epsilon'{_2})$.
Such that  $-\epsilon{_1}-\epsilon'{_1}+\epsilon{_2}+\epsilon'{_2}=0$.
And, if you start extending the structure from here. You will never be able to maintain $|f(x,y)-M|<\epsilon$ in its neighbourhood. If you try to maintain this convergence requirement in one portion , you fail to do in a opposite portion.
So, the convergence must not be true. It must go to $-\infty$.
This means $f$ can be bounded  neither from above nor from below with being it a constant function. So, $f(x,y)$ must have to be a constant function.

Question about decreasing sequence.


Time-varying coefficients in coxph

Question: I'm trying to understand time-varying coefficients in survival::coxph and the tt notation. I found this paper, which clarified some of my questions and provided the example below.
The way I understand it, we can interact a covariate $X$ with some function of time $g(t)$ and then have a constant coefficient to represent this new time-varying covariate $\tilde{X} = Xg(t)$ instead of having an actual time-varying coefficient for $X$. However, if that were true, I would expect to get the same result from these two models below.
library(survival)
fit.tt <- coxph(Surv(time, status) ~ age + sex + ph.karno + tt(ph.karno),
                data=lung,
                tt = function(x, t, ...) x * log(t + 20))
summary(fit.tt)

fit.I <- coxph(Surv(time, status) ~ age + sex + ph.karno + I(ph.karno*log(time + 20)),
               data=lung)
summary(fit.I)

Why do these two formulas yield such different results or how is my intuition wrong?
Answer: Section 4.2 of the time-dependence vignette of the R survival package discusses the problem with your second model:

The issue is that the above code does not actually create a time dependent covariate, rather it creates a time-static value for each subject based on their value for the covariate time; no differently than if we had constructed the variable outside of a coxph call. This variable most definitely breaks the rule about not looking into the future, and one would quickly find the circularity: large values of time appear to predict long survival because long survival leads to large values for time.

In your second model, the time variable used in your interaction is exactly (and only) the event or censoring time for an individual. So that doesn't generate a continuous function of time, as you seek. That interaction term is just a single, fixed value for each individual.
The first model instead defines a new tt() continuous function of time. While the software runs through all the event times in fitting the Cox model, that generates a new value of the interaction term, specific to that elapsed time t, for all individuals still at risk.
To summarize: time in the second model refers to a fixed covariate value for each individual; the t in the first model represents a value that changes in the way that you want.

Time-varying coefficients in coxph


How does any given major key contain only 1 tritone?

Question: Understanding the Fundamentals of Music (2006), by Robert Greenberg, B.A. in music (magna cum laude) from Princeton, Ph.D. in music composition from the University of California, Berkeley.  p. 54 of the Lecture Transcript.

Any given
  major key contains only one tritone. It's the interval between the
  fourth and seventh scale-degrees. In the key of C major, for example,
  the tritone occurs between an F (the fourth scale-degree) and a B (the
  seventh scale-degree). Now, because there are 12 different major
  keys and only six different tritones, the tritones are doubled up: the
  same tritone will serve two different major keys. So, what major key
  does C major share a tritone with? The answer: the key of F# major,
  the major key farthest away from C major, the major key a tritone
  away from C major! My friends, keys a tritone apart will share the
  same tritone! (For our information, in F# major, the tritone occurs
  between B, which is the fourth degree of F# major, and E#, which is
  enharmonic with F, which is the seventh degree of F# major.) Dang,
  don't we just love the chromatic collection! The metaphors it
  presents us with are endless: The end is the beginning, the beginning
  is the end; opposites unite into singularities and singularities become
  opposites; all points are connected within the continuum. It's a
  fantastic system!

Why can a given major key contain only 1 tritone?

I don't understand why the "only one tritone" must be "the interval between the
fourth and seventh scale-degrees"?
I added to Greenberg's figures.  In the left figure, why can't C-F# be another tritone for C maj?  In the right, why can't F#-C be another tritone for F# maj?
Comment: @Tim Wouldn't that count as an inversion rather than a second tritone?
Comment: I didn't answer your question directly in my answer. You can add a tritone to any scale if you want to, but the lecture is specifically talking about staying in the diatonic pattern of each scale, so you wouldn't have the F# in C major's regular notes, and you wouldn't have the C in F#major's notes normally.
Comment: @Tim yes, the tritone inverts to itself.
Comment: for more tritone fun, see tritone substitution: https://en.wikipedia.org/wiki/Tritone_substitution
Comment: @YourUncleBob - semantics! Yes, except that F>B isn't B>F. My money's on two! All other inversions have completely different names.
Comment: Why the downvotes? To me this seems like a very focused, well-researched, and clearly articulated question.
Comment: Yes, between the 4th and 7th notes of a majors scale is a tritone. There's also another (in a different place!) between the 7th and 4th notes.
Comment: @AlphonsoBalvenie - true, my point being there are *two* tritones, not one as stated.
Comment: well, we could count four tritones then, if we look at the intervals, so we have the tritone between the 4th to 7th degree and its inversion, and the tritone between the 11th and 14th degree and its inversion...
Comment: @AlphonsoBalvenie - why stop there - there's plenty more octaves - but aren't they merely copies..? The only inversion that is the same both ways *is* the tritone, but measuring from 4>7 maybe is the only one, as 7>11 goes out of the octave. Wait, though, would there be an inversion of, say, a M13th?
Comment: @Tim yes, I'm considering up to the 14th,  using  the tritones in relation to harmonic stacking, something like an Fflat5add6 etc.  Not sure the use of it, but you can see it in the pattern.  Looking further down the lecture transcript, the tritone comment was included in a sequential description of intervals, with sixths and seventh next, and then talking about the tritone inversion in Complementary Intervals. The lecturer is discussing inside the octave.
Comment: @AlphonsoBalvenie if you're considering intervals up to 14, then there are only three tritones unless you consider ascending and descending intervals separately.  The tritones are 4 to 7, 7 to 11, and 11 to 14.  The second tritone, a diminished fifth, is the inversion of the first and last tritones, which are augmented fourths. (The first and third, of course, are the same under octave equivalence.) Part of the problem is that "tritone" comes from a different system of describing intervals wherein the major third is a "ditone," as are the diminished fourth and the doubly augmented second.
Comment: @YourUncleBob it's an inversion, definitely.  The augmented fourth inverts to the diminished fifth.  But since both of those intervals are tritones, it is also another tritone.
Answer: The lecture transcript isn't particularly clear unless you already have an understanding of the topic.
What is being presented is this:
in the major scale pattern, the tritone (three whole steps between notes) only occurs between the 4th note and the 7th note of the scale, exampled by in C major the jump between F and B (f-g-a-b, three whole steps).
     1 2 34 5 6 78
     C-D-EF-G-A-BC
           w w w
He then goes on to explain that even though we can create a major scale pattern off of any of the twelve tones, because of the nature of the tritone it only occurs between six note parings (he really means tones, not notes) in the twelve possible keys.
Keys a tritone apart share the same tritone (in pitch, not note sequence, he doesn't make that clear).
For example if I am in the key of C major, the tritone above the tonic is F#, which isn't in the scale, it is the note between C Major scale's 4th and 5th degree, or a tritone higher.
     C-D-EFF#G-A-BC
     1 2 34TT5 6 78
If I move to F# Major, the then tritone in F# Major is the 4th to the 7th again, or B to E#, which is the actual pitch of F, so the B - F tritone.
Looking at the two keys, C major and F# major they share the same pitch jump of the tritone F-B, B-F, even though they aren't named the same.

     1  2  34  5  6  78
     F#-G#-A#B-C#-D#-E#F#
              w  w  w
The main confusion is probably that when considering the tritone we are talking about the specific frequency jump of three whole steps, independent of the enharmonic naming of the notes.
Answer: "Any given major key contains only one tritone. It's the interval between the fourth and seventh scale-degrees."
Perfectly correct, if we take 'any major key' to mean just the notes of a major scale.   If we have only C,D,E,F,G,A,B,C to choose from, the only tritone is between F and B.
Yes, of course we can play a tritone above C, which will be F#. Very likely it won't even imply a modulation to another key, we will still, in a very real sense, be 'in C major'.    I'm delighted that you recognise this fact!   A lot of questions in this forum are based on the misapprehension that only 'notes in the scale' are 'allowed.   But, in this case, we ARE talking JUST about the notes of the major scale
Answer: Perhaps one way to "see" the explanation is to rearrange the diatonic scale in fifths and then noting the fifths are all perfect except for one...
CDEFGABCD...
Rearrange that in fifths...
CGDAEBFCG...
...you can see that only the B to F is a diminished fifth, a tritone.
Personally, I think the observation should be applied to the diatonic scale rather than the more specific major scale, because then you include the various modes. For example, the Dorian mode contains only one tritone. The same applies for all the modes.
For what it is worth, minor key music has two characterisic tritones: the leading tone to the sub-dominant, and the supertonic to the (lowered) mediant.

EDIT
I didn't see your specific question...

In the left figure, why can't C-F# be another tritone for C maj? In the right, why can't F#-C be another tritone for F# maj?

Diatonically speaking you won't be in the key of C major if you use an F#, and you won't be in F# major if you use a C natural.
Of course you can play those tones and still be in the respective keys, but they will be chromatic. For the purpose of the discussion "in the key of..." means the diatonic tones of the key signature and not chromatic tones.
The major/minor key system is a mix of diatonic and chromatic tones. Chromaticism can take the form of chromatic embellishment or shifting modes/tonics. "Being in a key" can be thought of has applying on different levels global or local. For example, a sonata in C major might contain passage that temporarily shifts into D minor. Globally the sonata is in C major, but the passage is locally in D minor.
You in one sense your are correct. You can have the tritone C F# in C major using chromatic harmony like V43/V V V42 I6.
On the other hand, if we want to discuss only the diatonic tones of the key signature, the tritone C F# is not in C major.
Answer: Interesting question that made me think. I'm a guitar player who had someone hand me a Circle of fifths chart years ago to help me understand some things about music theory. Now I just pulled out my Circle of fifths chart and looked at it again to see that for any key chosen, I can look directly across (180 degrees) the chart and find the tri-tone for that key, and I counted them and I'm seeing twelve of them. One for each key. I don't wish to put up an  incorrect answer, but it is my understanding that the tritone is the center tone of the Octave and the only tone that stays the same interval when inverted. It is three whole tones (steps) from the tonic and three whole tones (steps) from the Octave. That's the definition I understand and I'm figuring twelve of them. Where's the fallacy in my understanding?
Comment: In the main question comments Tim and I have a discussion about this. I think the main thing is that the intent of the lecture is considering the intervals existing in one octave of the major scale. If you look at just the intervals in one octave of the major scale, then a tritone only occurs in one location, between the  4th and 7th degrees of the one octave scale.  The actual tritone from the Tonic doesn't exist in the major scale note sequence.
Comment: @Alphonso  Balvenie- I am more confused by the statement that there are only six tri-tones for the twelve major keys. My studies haven't defined the tri-tone as the interval between the fourth and the seventh. Instead, I learned the tri-tone was the interval consisting of three whole tones (steps) making it possible to apply the tri-tone starting at the tonic. Are my studies incorrect? Did I not get what I paid for? ;)
Comment: @Alphonso Balvenie- If I understand correctly, the lecture is considering the tri-tone interval to be exactly the same interval after it has been inverted. My studies consider the tritone to be reversed when inverted, therefore resulting in twelve different tri-tones. I'd like to know which way is correct, but it occurs to me I might be overthinking this a bit.
Comment: @Alphoso Balvenie- It just occurred to me that the confusion I've been having is likely one about definition. It may be that the lecturer is defining a Tri-tone  interval as three whole tones, whereas I'm thinking of the tri-tone as the individual notes at each end of the tri-tone interval. From the lecturers definition I come up with 6 tri-tones, and from my own definition I count 12. I've been thinking to hard, time for a break!
Comment: whatever you do, don't read the tritone inversion link. That's a deep rabbit hole. Yes, same terms, different meanings depending on context. There is the tritone that exists between the perfect 4th and 5th and inverts perfectly to the octave, and a tritone is defined by any jump of three whole tones (steps). The tritone is the dissonance in a Dominant7 chord, and inserting one in Minor scale's dominant chord raises the seventh degree in Harmonic Minor, etc.  There is a difference between defining the pitch of the tritone vs the enharmonic naming of the tritones.
Comment: The Confusing TriTones would be a good name for a Jazz Band.
Comment: @Alphonso Balvenie- I woke up  this morning thinking about this discussion, and all of a sudden everything became perfectly clear to me.  My education and studies explained the tri-tone in terms of the chromatic scale, but failed to point out the tritone interval and where it appears in the major, minor scales or how it moves around when applied to the different modes. I'm understanding there is more for me to study here and oddly enough, it feels pretty good.
Answer:
I added to Greenberg's figures. In the left figure, why can't C-F# be
  another tritone for C maj? In the right, why can't F#-C be another
  tritone for F# maj

You must understand the fundamentals here: In any given key, each note (musical letter) appears only once in its major scale.
F# does not occur in the key of C Major (it's not diatonic - belonging naturally to that key.). F is the perfect 4th in C Major - there is no F#.
C natural does not occur in the key of F# Major. C# is the perfect 5th in F# Major - there is no C natural.
Your guide here is the key signature: The key signature shows you exactly which notes are part of that key - diatonic - just read it. Key signatures are not some sort of secret code - they simply tell you what notes are diatonic to the key in question.
The key signature of C Major has no sharps or flats - that means that all 7 notes diatonic to the key of C Major are natural. So, no F#.
The key signature of F# Major has 6 sharps - every note is sharped except for B - that means that all 7 notes diatonic to the key of F# Major are sharp except for B. So, no C natural.
However, in key of F# Major, the note E# does occur, as indicated by its key signature -it's the major 7th in the key F# Major. (In most places E# is called F but technically speaking, it is sometimes more correctly referred to as E#, such as when spelling the F# Major scale).
Counting from B natural - the Perfect 4 in F# Major - to E#, the Major 7th, we get an Augmented 4th - one form of tritone. This is exactly the same as going from F, the Perfect 4th in C Major, to B, the Major 7th - again an augmented 4th.
Going from the Perfect 4th to the Major 7th is the only interval that results in a tritone - in this case an Augmented 4th - in any one major key.

Any given major key contains only one tritone

is entirely correct - it's simple math.
Try it now, using the key signature as explained, as you will see this clearly.
Answer: Compare all the notes in the key of C with those same notes shifted by half an octave:

    C # D # E F # G # A # B C # D # E F # G # A # B C
                C # D # E F # G # A # B C # D # E F # G # A # B C

Now notice that the only cases where two "white notes" line up are the "B/F" and the "F/B".
No matter what the key is, there will always be exactly one pair of natural notes where each one matches with the other.
Answer: The tritone embedded in a diatonic scale also carries through all of the modes, from Ionian through Locrian. In the key of C, the tritone (F-B) is located in the 4th and  major 7th degrees of the scale in C's Ionian mode, the 3rd and 6th scale degrees in Dorian, 2nd and 5th in degrees in Phrygian, and 1st and 4th in Lydian. It then flips to B-F in G Mixolydian,A Aeolean, and B Locrian. So although each key has but one tritone, that tritone is available in all 7 modes of that key.


Question regarding the algebra of norms

Question: I am new to linear algebra, as am having some doubts regarding the following question:
True or False
$u,v \in R^n$

$\left\lVert u\right\rVert=\left\lVert v\right\rVert$ if and only if u+v and u-v are orthogonal.
For every $u,v \in R^n$ and every $c \in R$: $\left\lVert cu+v\right\rVert^2=c^2\left\lVert u\right\rVert^2+2c(u·v)+\left\lVert v\right\rVert^2$

I worked out with basic algebra that 1 is true. However I am doubting re 2: algebraically it makes sense, and I have put in two sets of numbers that both came out correct, but I am doubting whether there might be a detail that I am missing (because the question states "for every u,v…"
Thank you!
Comment: To state your proof of 2. rigorously: First, write out the single algebraic sentence that "shows" that the LHS of the Equation is equal to the RHS. Second, put brackets around that sentence and precede it with $\forall u,v\in \Bbb R^n\,\forall c\in \Bbb R.$ That's all there is to it.
Answer: Just using the definition of the norm in $\mathbb{R}^n$: for $u \in \mathbb{R}^n$ $||u||:=\sqrt{(u,u)}$, where $(\cdot,\cdot)$ represents the standard inner product in $\mathbb{R}^n$.
Now $||cu+v||^2={\sqrt{(cu+v,cu+v)}}^2=(cu+v,cu+v)=(cu+v,cu)+(cu+v,v)=(cu,cu)+(v,cu)+(cu,v)+(v,v)=||cu||^2+(cu,v)+(cu,v)+||v||^2=c^2||u||^2+2c(u,v)+||v||^2$
using the bilinearity and scalar multiplication of the inner product.

Question regarding the algebra of norms


How to setup the Shopware development template without docker? (to provide a Shopware platform pull request)

Question: I understand, that in order to contribute to the Shopware platform / core, I have to setup Shopware 6 using the development template.
I followed the steps at
https://github.com/shopware/platform#quickstart--installation
But only until

./psh.phar docker:start

I was thinking about installing via bin/console system:setup and bin/console system:install but it fails with
PHP Fatal error:  Uncaught Symfony\Component\Dotenv\Exception\PathException:
 Unable to read the "/home/projects/shopware-dev-bd/bin/../.env" environment file. in
/home/projects/shopware-dev-bd/vendor/symfony/dotenv/Dotenv.php:567

Is there an easy way to create the .env? Do I have to copy it manually?
Answer: It is recommended now to use the platform repository directly for setting up development installations.

Just clone directly from shopware/platform
composer install
bin/console system:setup to create the .env file
composer run setup
optionally bin/console framework:demodata if you need demo data
Comment: We should update https://github.com/shopware/platform#shopware-6-repository-structure and https://developer.shopware.com/docs/guides/installation/overview!
Comment: how is the admin theme built in that setup?
Comment: Found it, it's composer scripts
Answer: Addition to the accepted answer:
While in the development template there was psr.phar to manage things like building the admin theme, and in production template there is bin/build-administration.sh none of those are there in the method described by @dneustadt.
Instead this is managed by composer scripts which are defined in the composer.json
For example admin JS is build using
composer run build:js:admin