Show block on all 404 / 403 pages only via context

Question: Without having to install even more modules on the site for context. I know there a boat load of plugins for context, but I was wondering if this can be achieved by just using core context:
I basically want to show a view block on all the 404/403 pages, which are just basic page nodes. The problem with context is when you choose path as the condition, it will only show up on that path, which is the 404/403 node paths only. Even if you put in something random like: foo.com/adsfadsf -- the context won't fire since its not a recognized path even though it is the 404 page.
What context recipe can restrict a block to show up on the error pages only? I know i can list out all the paths i dont want it to show up on, but thats not going to work in my scenario.
Answer: A better way to do this is create a panel page for 403 and 404 page. Add whatever you want to in the panes.
Set the 403 and 404 page path under :
Admin > Configuration > System > Site Information

There is a section called ERROR PAGES. Add both the path there.
Edit :
For context use Context Error module.
Comment: im not using panels. im mainly using context as a replacement to panels.
Comment: Check the updated answer.
Comment: yea but i was wondering if there was a method where you didnt need to install another module.
Comment: I dont think so. Thats why this module is contributed.


DataFrame not considering column A due to cell A1 is blank

Question: I'm trying to import all raw data (csv files) into one DataFrame, and since the raw data file have some useless lines, I like to delete them by "drop", however for the row with first column is a blank cell. I'm unable to delete it, and the dataframe doesn't recognize that column.
Here is my code:
import pandas as pd
import numpy as np
import glob
import os

#Determine file path for index weighting files
pathwgt=r'//10.27.36.181/etf/Bill/Quant/AxJ_Weight'
filenames = glob.glob(pathwgt + "/*.csv")

#declare data frame
dfwgt=pd.DataFrame()

#consolidate all files into one data frame
for filename in filenames:
    dfwgt=dfwgt.replace('',np.NaN)
    dfwgt=dfwgt.append(pd.read_csv(filename))

dfwgt=dfwgt.drop(['Symbol','Company'])

now my cell A1 in excel is blank, and B1 has a string, where I like to delete the entire row 1. The dataFrame shape is [124544 rows x 6 columns], where it suppose to be [124544 rows x 7 columns]
Comment: Can you add data samples, 3-4 rows and desired output?
Comment: Welcome to SO. Remember to add the appropriate language tag.
Comment: Its unclear do you need to delete the column or the row? You dont need to drop the rows. Just select the rows ignoring what you dont want.
Comment: Hi Jezrael, yes I can add the heading, however since dataframe only consider it as 6 columns, I'm unable to add the first column header...
Comment: @BillSun - Do you need `pd.read_csv(filename).reset_index()` ? It is really hard answering, because data depending problem.
Comment: Also quite confusing that you use the term `excel` but you use `pd.read_csv()` this function would rename the blank column to `'Unnamed: 0'` - lastly `dfwgt` is a shit variable name.
Comment: @GiantsLoveDeathMetal, Yes, that's exactly my problem, I got 'Unnamed :0' in my output. You mentioned that I used pd.read_csv(), that's because my source files are in csv format.
Answer: You have a few solutions to your problem:
Remove row and header:
pd.read_csv('data.csv', skiprows=1, header=None)

This create a DataFrame with column names identified by a number. (e.g. from 0 -> 3)

Drop column:
pd.read_csv('data.csv')

Will result in have the unnamed column being assigned the name 'Unnamed: 0' you can drop this column by doing the following:
df = df.drop('Unnamed: 0', axis=1)

Change name:
pd.read_csv('data.csv')

Alternatively:
df = df.rename(columns={'Unnamed: 0': 'new_name'}

If the above doesn't solve your problem, then I am struggling to understand your question.


How to check if the problem is of clean url when migrating the database from one server to another?

Question: After uploading the my local db on server my site was not working and what I was getting was white screen of death. After few hours of struggling I found that as the server was not configured for clean url and the in the database the clean url settings was ON that was causing the problem.
Is there any way we can come to know if the problem is of the clean url in such scenario.
Also I can't use the backup and migrate module as the database size was too large.
Comment: Well yes, you should always make sure your server supports the software you're trying to run on it _before_ uploading it. To check for mod_rewrite in Apache, run `apachectl -t -D DUMP_MODULES` and scan the list for `rewrite_module`
Comment: @Clive Thanks for the method suggested by you for checking the mod_rewrite module status, actually the problem was that the site was present in the multi domain environment and the site was present in a sub folder so the "RewriteBase /" line in htaccess was causing the problem
Answer: It is advised to turn off the CLEAN URLs before migrating the database from one server to server.
Reason behind you were not able to see errors on white screen of death may be error reporting being turned off, if that is the case than you may try adding the following lines in setting.php to turn on error reporting:
<code>error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
</code>

Also check the entry's of "watchdog" table if you are getting white screen of death upon migration.
Comment: **Watchdog** table keeps the record of all site errors, so it helps a lot especially in finding the database error, you will surely get the clue for **CLEAN URL** issue from there.


Replace linebreak "\n" character in string

Question: I have a tibble structured like so:
structure(list(c("All", "Males", "Females"), `All Ages` = c("8.7\n            \n                \n                    6,315",
"8.6\n            \n                \n                    3,306",
"8.7\n            \n                \n                    1,136"
), `<18` = c("9.5\n            \n                \n                    19",
"9.5\n            \n                \n                    14",
"10.0\n            \n                \n                    3"
), `18-29` = c("8.7\n            \n                \n                    1,434",
"8.7\n            \n                \n                    1,081",
"8.7\n            \n                \n                    330"
), `30-44` = c("8.7\n            \n                \n                    1,746",
"8.7\n            \n                \n                    1,219",
"8.7\n            \n                \n                    507"
), `45+` = c("8.5\n            \n                \n                    951",
"8.4\n            \n                \n                    730",
"8.8\n            \n                \n                    212"
)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))

which looks like this:
# A tibble: 3 x 6
  ``     `All Ages`          `<18`         `18-29`         `30-44`         `45+`
  <chr>  <chr>               <chr>         <chr>           <chr>           <chr>
1 All    "8.7\n            … "9.5\n      … "8.7\n        … "8.7\n        … "8.5\n      …
2 Males  "8.6\n            … "9.5\n      … "8.7\n        … "8.7\n        … "8.4\n      …
3 Femal… "8.7\n            … "10.0\n     … "8.7\n        … "8.7\n        … "8.8\n      …

I want to remove the line break characters from each cell where it appears. So I tried something like this:
df %>% mutate(`18-29` = str_replace_all(`18-29`, "\n", ""))

to see if I could get it to work for just the one column, but i've not been able to. I've tried many variations (e.g. putting .$ in front of the column names, just a ., trying to copy and paste the gap which appears between the two data points in each cell after running View(df), putting an additional escape \ before the \n, removing the tick marks ` around the column name, replacing them with single ' or double " speech marks, etc.), but none have worked. The most common error I run into is Error in initialize(...) : attempt to use zero-length variable name.
Ultimately, I want to remove the \n characters, then split each of the columns with numeric values in them into two, prefixed by cnt_ and rating_ (so i'd end up with cnt_<18, rating_<18, cnt_18-29, rating_18-29, etc.).
Comment: I.e., by getting rid of the **\n** , you will be having `"8.7                                             1,434"` in one of the lines. What will be the logic to combine them? Will it be 8.71434 ?Or?
Comment: I would want to separate on the empty space. I can handle that with `separate` though, just want to remove the `\n` beforehand
Answer: The error is because you have a column which has empty name, tibbles don't allow that. You can use janitor::clean_names() to make the column names standard.
str_replace_all with empty replacement is same as using str_remove_all.
library(dplyr)
library(stringr)

df %>%
  janitor::clean_names() %>%
  mutate(x18_29 = str_remove_all(x18_29, "\n"))

To apply for all the columns use across.
df %>%
  janitor::clean_names() %>%
  mutate(across(.fns = ~str_remove_all(., "\n")))


Reverse proxy to internal server mapping

Question: I want to setup a reverse proxy to 5 web servers.  All the requests that the reverse proxy receives will have a unique id (UID) in the http request query string.  I have a database that contains the UID -> web server mappings.
I'm not sure if this is reasonably possible.  I'd like the reverse proxy do the following:
1. a request comes in
2. check cache for UID, if not found load it from the database
3. forward the request to the appropriate web server
FYI- I can't encode the server id into the UID, since the mapping might change but the uid will not.
Is something like this possible with perlbal or some other tool?  I don't mind if I need to do some scripting to make this happen.  I'm just not sure what my options are or what other people have done to solve this problem.
any input is greatly appreciated!!!
Comment: Are you looking to serve content directly from the database, or just to determine the server mapping?
Comment: You might want to have a look at HA-Proxy and the hdr* criteria. I have not used this myself.. The mappings from uid to server would have to be in the HA-Proxy configuration, not in a database, so this obviously does not apply if it needs to be very dynamic/would require a lot of UIDs.

http://haproxy.1wt.eu/download/1.3/doc/configuration.txt
Answer: What you are looking for is a load balancer.  You can buy a hardware load balancer from companies like Citrix and Foundry (we have both) or you can configure an Apache server to do it.
If your backend servers are tomcat I'd look into mod_ajp which I have done a proxy type configuration for.
For configuration details with a regular HTTP proxying load balancer you can get more information here: http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html
You can likely find some good tutorials for your specific operating system and/or product.  As for the session logic you may likely have to handle that in your application, storing the sessions in a central database, but it sounds like you are already heading that route.
Answer: Depending on which platform you're using, this can be done, but with one big catch.
For IIS you have ISAPI_Rewrite, and for Apache you have mod_rewrite. Both will do what you're after, HOWEVER, they, as far as I know, cannot dynamically retrieve their mappings. They read their settings from a text file.
What you can do (and this is what I've done), is create a small program to interface with the database, that every time a record is changed the text file for the rewriter gets re-compiled. When the next request comes through, the rewriting engine loads the latest version and it's all good.
There is, in general, a delay of about 0.5 of a second when updating the records. Not instant, but usually close enough for web work.
Answer: This is just a theory but it ought to work. You can have apaches mod_rewrite RewriteMap use simple application to do the rewrite. Rewrite each request to something like server1.example.com based on the UID and then use that url for mod_proxy.
Apache feels a bit heavy to use as a reverse proxy, perhaps you can do the same with Varnish or nginx?


Login: Busca no MySQL retorna apenas um objeto

Question: Em um sistema de login, pego o e-mail e senha dados pelo usuário por um formulário e vejo se há quaisquer informações idênticas no banco de dados.
 $conn = new mysqli("localhost", "root", "", "prisma");

        $result = $conn->query("SELECT * FROM tb_users ORDER BY iduser");
        $data = array();
        $x = -1;
        while($row=$result->fetch_array(MYSQLI_ASSOC)){
            array_push($data, $row);
            $x += 1;
            if($user == $data[$x]["desusername"] && $pass == $data[$x]["despassword"]){
                if($user == $adms){
                    $_SESSION['isAdmin'] = true;
                    header('Location: index.php');
                }

                else{
                    header('Location: index.php');
                }

            }
            else{
                echo "Erro. Insira seus dados corretamente!";
                echo $data[$x]["desusername"];
                echo $data[$x]["despassword"];
                echo $user;
                echo $pass;
                break;
            }
        }

Sendo $user o e-mail dado pelo usuário, $pass a senha dada pelo usuário, $data[$x]["desusername"] os usuários do banco de dados e $data[$x]["despassword"] as senhas encontradas;
Porém, ao fazer o login, o $data[$x]["desusername"] e $data[$x]["despassword"] são o primeiro item do meu banco de dados, sendo que ainda tem mais um item pra percorrer!
Ex.:
O primeiro item é "firstname.lastname@example.com", e senha "123".
O segundo é "email@example.com" e senha "321";
Se o usuário digitar o segundo item no formulário, o meu while só terá pego o primeiro item para comparar no if.
Answer: A explicação está no break;, reduzindo o seu código temos:
$conn = new mysqli("localhost", "root", "", "prisma");
    $result = $conn->query("SELECT * FROM tb_users ORDER BY iduser");
    $data = array();
    $x = -1;
    while($row=$result->fetch_array(MYSQLI_ASSOC)){
        array_push($data, $row);
        $x += 1;
        if($user == $data[$x]["desusername"] && $pass == $data[$x]["despassword"]){
           //....
        } else{
            // ...
            break;
        }
    }

O break quebrará o while na primeira tentativa, já que ele cairá no else. Isso é, você executa o while e na primeira verificação (que seja incorreta) cairá no else e este else dará o break. Por isso só executa uma única vez.

Ignorando outros problemas de otimização/performance e de segurança, você pode utilizar:
    $conn = new mysqli("localhost", "root", "", "prisma");

    $result = $conn->query("SELECT * FROM tb_users ORDER BY iduser");

    $user_found = null;
    while($row = $result->fetch_array(MYSQLI_ASSOC) && $user_found === null){
        if($user === $row["desusername"] && $pass === $row["despassword"]){
            $user_found = $row;
        }
    }

    if($user_found !== null) {
        $_SESSION['isAdmin'] = $user == $adms;
        header('Location: index.php');
        return; // early-return
    }

    echo "Erro. Insira seus dados corretamente!";
    echo $data[$x]["desusername"];
    echo $data[$x]["despassword"];
    echo $user;
    echo $pass;

A ideia desse código é verificar primeiro, todos as linhas, então depois fazer o que deve ser feito (dar erro ou redirecionar).

O $adms e $user e $pass não está definido, não uso PHP não posso testar.


Typescript Discriminated Union without type guard

Question: The code below results in the error:
TS2322: Type 'RhinoData | HippoData' is not assignable to type 'RhinoData & HippoData'.
  Type 'RhinoData' is not assignable to type 'RhinoData & HippoData'.
    Property 'bar' is missing in type 'RhinoData' but required in type 'HippoData'.

The hope was that the AnimalPayload type provides enough information to guarantee that the assignment is legal. Is there a way to accomplish this in such a way that setAnimalState does not have to worry about specific types?
enum Animal {
  RHINO = 'RHINO',
  HIPPO = 'HIPPO',
}

interface RhinoData {
  foo: string,
}

interface HippoData {
  bar: string,
}

interface AnimalState {
  [Animal.RHINO]: RhinoData,
  [Animal.HIPPO]: HippoData,
}

type AnimalPayload = {
  type: Animal.RHINO,
  data: RhinoData,
} | {
  type: Animal.HIPPO,
  data: HippoData,
}

const state: AnimalState = {
  [Animal.RHINO]: null,
  [Animal.HIPPO]: null,
};

const setAnimalState = (payload: AnimalPayload) => {
  state[payload.type] = payload.data;
};

Answer: It sounds like typescript can't infer the fact that the type of payload.type is constrained by the type of AnimalState. Using mapped types can fix your problem here by making that relationship explicit.
enum Animal { RHINO = "RHINO", HIPPO = "HIPPO" }

interface RhinoData {
  foo: string,
}

interface HippoData {
  bar: string,
}

interface AnimalState {
  [Animal.RHINO]: RhinoData | null,
  [Animal.HIPPO]: HippoData | null,
}

type AnimalPayload<T extends Animal> = {
  type: T
  data: AnimalState[T],
}

const state: AnimalState = {
  [Animal.RHINO]: null,
  [Animal.HIPPO]: null,
};

const setAnimalState = <T extends Animal>(payload: AnimalPayload<T>) => {
  state[payload.type] = payload.data;
};


Double image slider portfolio

Question: I have been searching everywhere now but can't seem to find anything.
I'm making a portfolio website for a photograph and he has this special gallery slider that he really wants so I asking for help here instead.
The website that he wants to have a image gallery look-a-like is from this one: http://www.dnamodels.com
The top one with the play and pause icon.
So if anyone know how to do something like this I would really appreciate any help!
And non-wordpress.
Comment: Welcome to [so]! Please take the [tour] and see the [help/on-topic]. Right now, Your question is not really suitable for [so], as it is not sufficiently specific.
Answer: It is fairly simple. Here is one way to go:

On your html file create a UL with LI elements containing the images that you want to present (could be in IMG tags or as background images to the LI element).
Create some kind of a counter in order to know the total number of images you have in the gallery and also in order to give a unique id to each LI element / gallery image.
With css make each of the LI elements an absolute positioned element filling the entire container but displayed none (or visible hidden / opacity 0 with a nice transition property if you want it to fade nicely like in the example)
Create an "active" css class making the LI that has it to visible and attach it to your first LI element in the gallery
Create a JS function with an interval - incrementing a counter variable every few seconds (or reseting it if it equals to the total number of elements), removing the active class from all LI elements in your gallery and adding the active class to the element with the ID matching your variable value.
Bind a click event to your gallery that is switching between calling the above function (playing the gallery) and clearing the interval that is running in the function (pausing the gallery). In order to the change the cursor icon to play or pause icons - use addClass/removeClass with classes that are using the "cursor: url(xxx/yyy.png), auto".
Run the play function on document ready.
Answer: If you know some jquery you can try flexslider. It gives you an easy way to build your own slider and gives you some functions to control it, like animation, next, prev, etc..
you can see examples here: http://flexslider.woothemes.com/

Double image slider portfolio


Use CTE to insert data into multiple tables

Question: I am writing a query in MSSQL.
I am writing a huge logic in CTE, this logic has to be used to insert data into multiple tables.
I dont want to write this CTE for every table, want it to be generic for every table.
Example:
<code>with CTE as (Select * from table)
select * into table3 from table1 join cte
select * into table4 from table2 join cte
</code>
is it possible to achieve it
I tried to write the insert statement and tried to use the same CTE in generic way
Comment: You could instead dump the results of {cte} into a {#temp table}. If you only need to insert the rows into _two_ tables, you could perhaps use `WITH cte AS () INSERT dbo.table1 OUTPUT inserted.cols INTO dbo.table2 SELECT FROM cte;`.
Answer: A CTE must be complied immediately before using it in your subsequent SQL to do the insert. Have you considered creating a VIEW instead of a CTE? That way you could re-use the logic over and over again without having to repeat the CTE each time.


Understanding a Mathematica result of an integral

Question: I was computing an integral, and in particular the integral is
$$\int_0^{+\infty} \frac{|\sin(2x)|}{x}\ \text{d}x$$
(which by the way is infinite, I forgot to say before).
I really don't know why I decided to use Mathematica to get a "fast" confirm of that but...
Besides the fact it wasn't really fast at all, the result that Mathematica gave me is the following (by the way: I'm talking about the serious pc version of Mathematica, not Wolfram Alpha online):
$$\sum _{K[1]=1}^{\infty } (\text{Si}(2 \pi  K[1]+\pi )-\text{Si}(2 \pi  K[1]))+\sum _{K[1]=1}^{\infty } (-\text{Si}(2 \pi  K[1])-\text{Si}(\pi -2 \pi  K[1]))+\text{Si}(\pi )$$
I did not really understand what $K[1]$ is, here, because for one side it seems like a pure index of a sum, like a normal $j$ from $1$ to $\infty$.
On the there side, I found a specific function called $K(n)$ in wolfram documentation, referring to
$$K(n) = 1^1\cdot 2^2\cdot 3^3 \ldots (n-1)^{n-1} = H(n-1)$$
Where $H$ is called Hyperfactorial.
I am really confused about that notation, so could anybody explain me better this ambiguous result?
If it had to be a general sum index, why not to write it as $k$ from $1$ to $\infty$ and stop?
And if it had to be a sum over the argument of $K$ function, why not writing $K(n)$ for $n = 1$ to $\infty$?
Thanks!
Comment: FWIW the integral in question is infinite... not sure what Mathematica is trying to tell you.
Comment: @Wojowu Indeed! Besides that fact, I meant. xD It's a strange result. I expected a simple result like "the integral does not converge"...
Comment: Ah, sorry, once you've said that you took the "fast way" I thought you don't know what result to expect.
Comment: @Wojowu  I expressed myself so bad! I edited it!
Comment: I assume if Mathematica needs several fresh variables for summation indices or whatever, it names them `K[1]`, `K[2]`, ...
Answer: Consider $$I=\int_0^{+\infty} \frac{|\sin(2x)|}{x}\,dx=\int_0^{+\infty} \frac{|\sin(y)|}{y}\,dy=\sum_{k=0}^\infty\int_{k \pi}^{(k+1)\pi} \frac{|\sin(y)|}{y}\,dy$$ $$I=\sum_{k=0}^\infty\int_{2k \pi}^{(2k+1)\pi} \frac{\sin(y)}{y}\,dy-\sum_{k=0}^\infty\int_{(2k+1) \pi}^{(2k+2)\pi} \frac{\sin(y)}{y}\,dy$$ $$I=\sum_{k=0}^\infty\left(\text{Si}( (2 k+1) \pi )-\text{Si}(2k \pi )\right)-\sum_{k=0}^\infty\left(\text{Si}(2 (k+1) \pi )-\text{Si}(2 (k+1)\pi  )\right)$$ $$I=\sum_{k=0}^\infty\left(2 \text{Si}((2k+1) \pi )-\text{Si}(2 k \pi )-\text{Si}(2 (k+1) \pi )\right)$$
Comment: doesn't the last sum  in the third line cancel?


Start GUI application in an RDP session from a different session in Windows

Question: Context:
All I want to do is, start a GUI application (ex: mspaint) on Windows node (spawned on cloud) using Java and when I connect using RDP, I want the GUI application to be already there.
Issue:
After connecting to the remote Windows node through RDP, I don't see the GUI application already there.
I can see that my Java program has actually started GUI application and I can see that in the tasks list. It got started under "Services" session. When I run the same Java program after connecting to RDP, it started the same application in session with name "RDP-Tcp#0"
If I am using traditional RDP client, I can simply choose the session I want to connect to but here I have no control as I am using HTML5 RDP client called Guacamole.
Java program uses Runtime.getRuntime().exec() API to run a powershell script thats starts the GUI application.
Is there a way to run my script so that it runs and shows the GUI on a different session? This is trivial in Linux where we need to simply export the DISPLAY env variable.
Please help
Comment: What exatcly do you run to remote execute your java code ?
Comment: From the post, it seems like the Java process is not started remotely, but from something on the host. Am I right? If a Windows service is actually launching your Java application, and the host OS is Windows 7 or above, there is no way to have the UI visible. There are other means, though, which I can explain if I'm on the right track...
Comment: @Poorkenny Yes, Java program resides on the host and gets called on startup. OS is Windows 2008 server R2 with SP1. Please help on the probable way to do it
Answer: You cannot start a program in an RDP session that doesn't exist yet. To get what you want to work you'd need your code to actually log in via RDP, start the program, and then disconnect the session without logging off. However, since idle sessions eat up system resources, Terminal Server admins tend to set a timeout for idle sessions after which the user is automatically logged out.


$Re(f)=Re(g)$ implies $f(z)=g(z)+ic$

Question: Let $f$ and $g$ be analystic in a region $G$. If $\Re(f)=\Re(g)$ in $G$, then prove
$$f(z)=g(z)+ic \tag{for all $z \in G$}$$
where $c$ is a real constant.
By cauchy riemann we have
$$\int \frac{\partial}{\partial y}Re(g)=-\int\frac{\partial}{\partial x}Re(g)$$
Hence, $Im(f)$ must be some constant?
Comment: Did a few minor edits.  Cheers!
Answer: Notice that $f-g$ has values in $i\mathbb{R}$ and if $f-g$ is non-constant, then its image must be open in $\mathbb{C}$, which is a contradiction. Whence the result.
Comment: what is the contradiction?
Comment: @R_Squared A non-constant holomorphic map has an open image : https://en.wikipedia.org/wiki/Open_mapping_theorem_(complex_analysis).
Answer: I would use Cauchy-Riemann to do it like this:
Set
$h(z) = h_R(z) + ih_I(z) = f(z) - g(z); \tag 1$
then
$\Re(h) = h_R(z) = 0; \tag 2$
now by Cauchy-Riemann
$h_{I, x} = -h_{R,y} = 0 \tag 3$
and
$h_{I, y} = h_{R, x} = 0; \tag 4$
it follows that
$\nabla h_I = 0 \tag 5$
in $G$; thus,
$h_I = c \in \Bbb R, \tag 6$
where $c$ is constant.  So
$f(z) - g(z) = h(z) = h_R(z) + ih_I(z) = 0 + ic = ic, \tag 7$
whence
$f(z) = g(z) + ic. \tag 8$

$Re(f)=Re(g)$ implies $f(z)=g(z)+ic$


Conventions for interpretation of risk ratios

Question: For the effect sizes there exist conventions as how big an effect size should be, for beeing considered low/medium/big - 0,2/0,5/0,8 - according to Cohen.
Is there something equivalent in respect to risk ratios?
In particular for the purpose of an evaluation of the efficiency of medical therapies. Comparing the new treatment to standard therapy, in a forest-plots of a  metaanalysis.
Answer: Although the convention exists that does not make it useful of course. Since it compares the shift in location with variability it means that two studies which achieve the same effect in absolute terms (say a reduction of 10 mm Hg in mean systolic blood pressure) would differ in their value of Cohen's d if the variability differed with the one which used a restricted group of people achieving a larger Cohen's d. Whether you think that is a good thing or not of course is a matter for debate.
You ask about relative risks and as far as I know there is no parallel set of conventional values which at least in my opinion is a good thing. If you want to know the practical effect of your therapy it is better to focus on absolute risk reduction and convert it to number needed to treat. You can then say to the clinicians that if they treat 100 people they will avert X cases of the bad outcome. A given value of relative risk can correspond to different numbers needed to treat depending on the baseline risk. So if the risk of the bad outcome is very low then a relative risk of 0.5 may be unimportant in practical terms (being struck by lightning) whereas if the risk is high (dying having contracted Ebola virus disease) then such a relative risk would be hugely important.


Roots of polynomials with integer coefficients

Question: Let $N\in\mathbb{N}$ be sufficiently large and $n$ is even. I am trying to prove (or disprove) that if $M_0,M_1,\dots,M_n\in\mathbb{N}$ such that $M_n>M_i$ for $i=0,1,2,...,n-1$, then the polynomial
$$P(z) = z^{n+1}-NM_nz^n+NM_{n-1}z^{n-1}-NM_{n-2}z^{n-2}+\cdots-NM_0$$ has a positive root greater than 1. By Descartes' rule of sign, $P$ has a positive root but I can't show that a root is more than 1. I tried using the fact that $NM_n$ is the sum of the roots but I am still stuck.
Comment: This isn't really well-defined unless you specify how sufficiently large $N$ is. Or are you looking for an $N$ that makes your claim true?
Comment: It means the claim if true for $N>N_0$ for some $N_0\in\mathbb{N}$. $N_0$ may depend on $M_0,...,M_n$ and n.
Comment: Then your quantifiers are out of order, you need to start with "if $M_i...$ then there is an $N_0$ such that...". Right now it looks like you want such a bound that works uniformly for all choices of the Ms.
Comment: Part 1: what is the value of the polynomial when $x=1 \; ? \; $ Part 2: where did you get the problem?
Comment: seems also the constant term should be $-NM_0$
Answer: If I understand the problem right (given $M_k$ there is $N_0$ st the respective polynomial has a root greater than $1$ for $N >N_0$) the only thing one needs is that $M_n >0$ since then letting
$g(z)=M_nz^n-M_{n-1}z^{n-1}+...+M_0$
there is $r>1, g(r)=a>0$ ($g(x) \to \infty, x \to \infty, x >0)$ so for $N > \frac{r^{n+1}}{a}$ we have $P_N(r)=r^{n+1}-Ng(r) <0$ so there is a positive root greater than $r>1$ as $P_N(x) \to \infty, x \to \infty, x>0$
Comment: Got it. Thanks.


What kind of residual plot does this variable have?

Question: I am doing a multiple regression analysis and my focus is finding the best set of independent variables for prediction. I am starting to know my dataset and the behavior of each variable. I am doing a residuals analysis (with a great help of R) and my question is about the meaning of the residual plot for one of these variables.

Can I say that these variable residuals are a Null Plot kind with some outliers? Moreover, what does it help for my goal of finding good variables?
Comment: I wondered if something like that might be the case. So your response variable is a *count* & you are using standard OLS (linear) regression. You need to use a Poisson (etc) GLiM instead.
Comment: FWIW, the standard variance-stabilizing transformation for count data is the square root. Old-timers like me used to do that in the days before all this GLIM stuff was available. Maybe not as good as Poisson regression, but possibly better than no transformation. From the new plot (and the explanation of the standardized residuals), I can easily see two more diagonal rows of points, I bet you have quite a few 1s and 2s in the data also. These diagonals appear because $e = y - \hat y$, so for fixed $y$, we have a line with intercept $y$ and slope $-1$.
Comment: Using decimal numbers to plot the residuals creates an unreadable and possibly biased clutter throughout key parts of this plot. It would help if you would recreate it using small, uniformly sized point symbols instead.
Comment: As noted in several answers below, your model may be mis-specified. Can you say more about what your data *are*? What do they represent & where do they come from? What are your goals for the model?
Comment: @whuber, I added a cleaner plot.
Comment: @gung, my dataset consists of several social network metrics and the dependent variable, in a more general way, is the number of word/terms used by the nodes of the network. The model plotted is a test of linear regression with only one of the metrics. My goal is to find the set of variables that best predict the number of words/terms.
Comment: Now I just want to know my dataset and I thought that linear regression would be a good option to understand the variables and have some insights about these. The next step is gonna be testing some models and techniques for prediction. I am following a book called Multivariate Data Analysis 7th Edition for this task. The problem is that the book has perfect shapes of residuals plots and my residual plot looks like neither of those shapes.
Comment: Because your data are counts, many of the regression assumptions will be unlikely to hold, even approximately - most importantly the basic ones describing the mean and the variation about the mean.
Comment: Thank you for helping. I will try all the options you have suggested me.
Answer: Maybe -- but it does also have some characteristics of the horn-shaped plot you get when a transformation might help. Are these ordinary residuals, or some kind of standardized ones?
The reason I ask is that it's not unusual to see a downward-sloping edge in a residuals-vs-predicted plot; it happens when there is a frequently-attained lower bound (e.g., zero) on the $y$ values. However, if that is the case, that lower edge should have a slope of $-1$ and the slope in the plot is more like $-0.1$. But if the residuals are standardized, that'd explain it.
You can use Tukey's nonadditivity test to see if a transformation might help. The technique is as follows:

Obtain the predicted values, $\hat y_i$
Compute the variable $N$ with values $N_i = \hat y_i^2$
Fit the same model with $N$ as an additional predictor
If the $t$ statistic for $N$ is significant (this is the test of Tukey's one d.f. for nonadditivity), it suggests that a transformation of the response might help. As a rough estimate, use $y$ raised to the $1-\hat\beta_N$ power, or $\log y$ if this is nearly zero.

Note: This is only for diagnostic purposes. Don't include $N$ in your final model, or in any steps along the way!
Another note: A similar idea is the Atkinson score test, where you use $N_i = \hat y_i\log\hat y_i$
An additional suggestion is to plot residuals against everything you can think of (time order, predictors in the model, predictors not in the model) to see if there is any kind of apparent pattern in those.
And one more comment: Sometimes, a bad residual plot is good news! A really poor-fitting model often has a nice residual plot but doesn't predict the response worth a darn. When the residual plot starts looking bad, it can mean that you've explained enough of the variations in the response that you can now see the more minor defects in the model.
Comment: +1, this is a really great answer. Here is where I think we see the wisdom that comes from years of experience & expertise.
Comment: Yes, the residuals are standardized (studentized). Sorry for not having commented that.. There are many 0 values for y as well! I didn't know about the Tukey's nonadditivity. I will try! Thank you!
Answer: A residual plot (meaning, residuals on one axis, conventionally the $y$ axis) and predicted or fitted on the other, conventionally the $x$ axis) is a kind of overall health check on a regression-type model.
Unless this plot is mislabelled it doesn't refer to a particular "independent variable", nor can it help much in determining which independent variables to use in a model.  I am with those who think that "independent variable" is an outdated and unsatisfactory term; unfortunately we can't all agree on which term is best, but "predictor" or "covariate" suits many people better.
The main message from this plot that I pick up is to note that residuals all lie above a line with negative slope. The key question to answer is whether the response is positive by definition, or at least non-negative. If so, negative predicted values don't make much sense, and a logarithmic transformation or using a logarithmic link seems strongly indicated. I would want a more symmetric and more nearly patternless scatter to declare the regression satisfactory.
On that and on other grounds I tend to disagree with @user1669710. Experienced users of statistics are typically reluctant to  declare data points as outliers without very strong reasons, particularly whenever heteroscedasticity is a more plausible explanation. In particular, the appearance of outliers is often illusive and is not maintained when using an appropriate non-linear scale.
If this is a plot from a standard regression, the mean residual will be zero, period, and is not, and cannot be, diagnostic.
EDIT: As the response variable has now been explained as non-negative, a Poisson regression is strongly indicated. Note that (raw) residuals are now expected to be heteroscedastic, but standardization can help with that.
Comment: Thank you for the answer! How can I have a more symmetric and more nearly patternless scatter?
Comment: As @gung suggests, you should think of a more appropriate model. Poisson regression sounds like the next thing to try.
Answer: Residual plots are used to identify heteroscedasticity, non-linear dependence and outliers. If the data is heteroscedastic you will see a lot of variation along the horizontal axis in the vertical spread of the errors. In your case, the difference in the vertical spread is looking more like outliers than heteroscedasticity, however, I am a bit unsure about the 100-150 range, which looks like the spread is slightly different. Non-linear dependence typically has more structured plots than a "blob" like yours. In your case, looks like the regression you are using is an "ok" fit to the data. I'd also look at the residual mean, which if not zero, indicates an inferior fit of the model to the data.
Comment: Did you notice that the response variable appears to have a floor (probably 0) under it?  That leads to obvious heteroscedasticity and bias in the residuals at low predicted values. A different regression model altogether is called for here.
Comment: @whuber, I think the residual mean is guaranteed to be zero as long as the intercept is in the model.
Comment: @rvl When a constant term is included in least squares regression, that is correct: but it still does not preclude the presence of a *local bias* in the residuals, which is quite apparent here where negative values are predicted. I think this would become more evident with a better plot of the results; this one does not show what's really going on in any detail.


Spectrum of the operator

Question: Let $T$ be an operator on Hilbert space.
Define $\sigma(T)=\lbrace \lambda\in \mathbb{C} | \lambda I - T~\textrm{is not invertible}\rbrace$.
How can I prove that $\sigma(T^n)=\lbrace \lambda^n|\lambda\in \sigma(T)\rbrace$?
Comment: You probably want $\sigma(T) = \{ \lambda \in \mathbb C \;|\; \lambda - T \text{ is not invertible}\}$.
Comment: @user8484: Your $\sigma (T)$ is the *resolvent of $T$*, not its spectrum. The spectrum of $T$ is the set $\sigma (T):=\{ \lambda \in \mathbb{C} |\ \lambda -T \text{ is not invertible}\}$.
Comment: Right. I changed it. Thanks.
Comment: Your operator must be bounded too...
Answer: Note: Given a polynomial $P(x)$, we can talk about the operator $P(A)$ which is the polynomial applied to $A$.  Since $A$ commutes with itself, and the identity commutes with everything, we can factor $P(A)=(A-c_1 I)(A-c_2 I)\cdots (A-c_m I)$ by the fundamental theorem of algebra.
Hint: The polynomial $x-\lambda$ divides $x^n-\lambda^n$.  This shows $\{\lambda^n:\ \lambda\in \sigma(A)\} \subset \sigma(A^n)$.  To show that each element $r\in\sigma(A^n)$ is of the form $\lambda^n$, $\lambda\in \sigma(A)$, consider $x^n-r$, and factor this over $\mathbb{C}$ as a polynomial.  Since the polynomial evaluated at $A$ is non-invertible, at least one factor must non-invertible.  Hence $r^{1/n}\zeta_n\in \sigma(A)$ where $\zeta_n$ is some root of unity, and the proof is finished.
Hope that helps,
Edit:  More generally, suppose we are given an operator $A$, and that $Q(x)=\frac{p(x)}{q(x)}$ is a rational function whose poles do not lie in $\sigma(A)$.  Then $Q(A)$ makes sense as an operator, and $$\sigma(Q(A))=\{Q(\lambda):\ \lambda\in\sigma(A)\}.$$  The proof of this is very similar to the proof of what you want.
Edit 2:  Since we are talking about the spectrum, I removed the earlier edit.  I'll still mention, the exact same proof works with the directions changed to show that the resolvent is preserved under polynomials.
Comment: On the other hand, the proof (and result) does depend on the scalar field having all roots of unity. The statement does not hold for a 90-degree rotation in the plane for instance: its real spectrum is empty but the real spectrum of its square is not.
Comment: @Pacciu:  This is fine for infinite dimensions.  I am not referring to the characteristic polynomial, but rather polynomials applied to $A$.  Given $P(x)$, the operator $P(A)$ makes sense, and the factoring over $\mathbb{C}$ of $P(x)=\prod_{i=1}^n(x-c_i)$ also holds for $P(A)=\prod_{i=1}^n(A-c_i I)$ since $A$ commutes with itself and the identity.  Now, the non-invertibility of a factor implies that of the whole polynomial, which is what we use in the above argument.
Comment: Doesn't your proof hold only in the finite dimensional case? I'm in trouble here, because I don't know how to define the characteristic polynomial in the infinite dimensional case.
Comment: Ok, got it! :-D Thank you.
Comment: @Eric: Be careful about what you use: in your answer, noninvertibility of the polynomial implies noninvertibility of at least one factor (right) in your comment, noninvertibility of a factor implies noninvertibility of the polynomial (only right here, in general wrong)

Spectrum of the operator


Using arbitrariness of $\varepsilon$ in proofs (metric spaces etc)

Question: Let $(X, d)$ be a metric space. Let $A \subseteq X$ and fix $x \in X$, define $\rho(x, A) = \inf \{d(x,a) : a \in A \}$.
In trying to prove $\overline{A} = \{x \in X : \rho(x, A) = 0\}$, if $x \in \overline{A}$ then for all $\varepsilon>0$, $B_{\varepsilon}(x) \cap A \neq \emptyset$. So let $y_{\varepsilon} \in B_{\varepsilon}(x) \cap A$. By the definition of infimum, we have
\begin{align*}
\rho(x, A) \le d(x, y_{\varepsilon}) < \varepsilon.
\end{align*}
Then since $\varepsilon$ was arbitrary, we have $\rho(x, A) = 0$.
What I don't understand is the bolded part, why is it that just because $\varepsilon$ is arbitrary allows us to conclude $\rho(x, A) =0$? What is the fundamental reason behind this? I've seen this kind of argument using the arbitrariness of $\varepsilon$ used elsewhere in really simple arguments, for example:
For all $\varepsilon>0$, assume that $x'+y' > u_1 + u_2 - \varepsilon$ for some real numbers $x', y', u_1, u_2$. Then since $\varepsilon$ was arbitrary, we have $x'+y' \ge u_1 + u_2$. Why? I think I'm missing something really obvious.
Answer: Assume that $\rho(x,A)\neq 0$, then $\varepsilon:=\rho(x,A)/2>0$ and by assumption, one has:
$$\rho(x,A)<\varepsilon=\rho(x,A)/2.$$
Which leads to $1<1/2$, a contradiction.
Answer: The fundamental reason is that if you can prove that some non-negative quantity is smaller than any positive number $\epsilon>0$ then it must be zero, for if it is not zero, then it is some positive number, for which you can always find some smaller $\epsilon>0$.
Answer: If you prove that $0\le x<\epsilon$ for an arbitrary $\epsilon>0$ then you prove that $0\le x<\epsilon$ for every $\epsilon>0$.
That is, $x$ is a non negative number that is lesser than any positive number. Since for any positive number there is another lesser positive number (its half, for example), the only non negative number that holds this condition is $0$.
Answer: Use the following result with $a = \rho(x,A)$ and $b=0$.

Theorem. Let $a$, $b \in \mathbb{R}$. If $a < b + \varepsilon$ for every $\varepsilon > 0$, then $a \leq b$.

(Note that you've already shown the truth of "$a < b + \varepsilon$ for every $\varepsilon > 0$" in your work: when you said that $\varepsilon$ was arbitrary, it means you've shown this inequality to be true for every $\varepsilon > 0$.)
It then follows from the theorem that $\rho(x,A) \leq 0$. But $\rho(x,A) \geq 0$ by definition of $\rho$, so $\rho(x,A) = 0$.
Proof of Theorem. Suppose $a > b$. We show that $a \geq b + \varepsilon$ for some $\varepsilon > 0$. Let $\varepsilon = (a-b)/2$, which is positive since $a > b$. Then
$$
a = \dfrac{a+a}{2} > \dfrac{a + b}{2} = b + \dfrac{a-b}{2} = b + \varepsilon.
\tag*{$\blacksquare$}
$$
Answer: Assume $$\rho(x, A)  < \varepsilon$$ for all $\epsilon>0.$ Can be $$\rho(x, A)  =a>0?$$ Since $\varepsilon$ is arbitrary we can take $\varepsilon=a/2.$ What do we get in such a case?
$$a=\rho(x, A)<\dfrac a2.$$ This gives us a contradiction. So it must be
$$\rho(x, A) =0.$$
Answer: The following answer is a bit cornball, but it is still an earnest attempt to be  instructive.

Can the use of '$\text{arbitrary-} \varepsilon$' logic in a proof be an arbitrary choice of techniques? It might even be an indiscriminate choice manifesting abstruseness when other methods are available.
In what follows you will not see any $\varepsilon$'s.

Warm up exercise:
Both the infimum and supremum can be expressed in terms of closure:
Assume the set $K \subset \Bbb R$ has NO greatest element but is bounded from above.
Proposition 1: There is a  unique number $k_b \in \overline{K}$ satisfying
$k_b \gt k$ for all $k \in K$.
Proposition 2: The number $k_b$ is the supremum of $K$.
The reader is encouraged to prove both of these statements and the two corresponding  propositions for the infimum.

Theorem 3: If $a \gt b$ then there exists a (unique) $p > 0$ so that
$\tag 1  a = b + p$
Proof
If $a \gt b$, then $a - b \gt 0$. Set $p = a - b$. We leave it to the reader to check that (1) holds with this choice of $p$. QED
Corollary 4: If $a \gt b$ then there exists a $q > 0$ so that
$\tag 2  a \ge b + q$
Proof
If you set $q = p/2$ then $a \gt b + q$. But then also $a \ge b + q$, giving (2)  . QED
Of course the contrapositive of this corollary is also true:
Theorem 5:  Let $a$, $b \in \mathbb{R}$. If $\,a \lt b + q\;$ for every $q \gt 0$, then $a \leq b$.
Observe that it is OK to use a different letter for $q$, like, say, $\text{e-p-s-i-l-o-n}$.

Proposition 6: If $x \in \overline{A}\;$ then $\;\rho(x, A) = 0$.
Proof
Assume $\rho(x, A) = k \gt 0$. It is certainly possible that for some $a \in A $ that $d(x,a) = k$, so we are going to be careful here. The open metric ball about $x$ with radius $k - 10^{-6}$ can't have a nonempty intersection with $A$ (if it did, $\rho(x, A)$ couldn't be a lower bound). So $x \notin \overline{A}$. QED
Exercise: Was it really necessary to be so careful in the above proof?


Proof for sequence

Question:
Let {$x_{n}$} be a sequence of strictly positive real numbers. Suppose that {$x_{n+1}/x_{n}$} is a convergent sequence with
$$\lim_{n\to\infty}\frac{x_{n+1}}{x_{n}} <1$$
Show that there exists an $N\in \mathbb{N}$ and constant $C>0$ and $0<r<1$ such that $0<x_{n}<Cr^n$ for an $n\geq N$

I understand that ratio between ${x_{n+1}}$ and $x_{n}$ is less than 1. $x_{n}$ has to be greater than 0 for it to be a denominator.
For any $r$ sufficiently close to $1$, and for every $n$ large enough (depending on $r$), we have $\frac{x_{n+1}}{x_{n}} < r$. I'm stuck here.
Answer: Note that because $\{x_{n + 1}/x_n\}$ is a convergent sequence satisfying.
$$\lim_{n \to \infty} \frac{x_{n + 1}}{x_n} < 1$$
Then we can conclude
$$\lim_{n \to \infty} \frac{x_{n + 1}}{x_n} = c < 1$$
More importantly, there exists $b$ satisfying $c < b < 1$ such that
$$\exists N \; \text{s.t.} \; \forall n \geq N \qquad \frac{x_{n + 1}}{x_n} < b$$
Thus we have
$$x_{N + 1} < b x_N$$
$$x_{N + 2} < b x_{N + 1} < b^2 x_N$$
And in general
$$x_{N + k} < b^k x_N$$
Let $r = b$ and let $C = x_N b^{-N}$, then we get that
$$x_{N + k} < r^{N + k} C$$
as desired
Answer: Let $\rho = \lim_n {x_{n+1} \over x_n} < 1$.
Choose $r \in (\rho,1)$, then there is some $N$
such that for $n \ge N$ you have $ {x_{n+1} \over x_n} \le r $.
Show that $x_n \le r^{n-N} x_N = r^n ( {1 \over r^N} x_N)$ for $n \ge N$.
Now figure out what to do with $x_1,...,x_{N-1}$.

Let $C = \max ({x_1\over r},...,{x_{N} \over r^{N}})+1$.
Comment: I'm still not sure what to do with $x_{1},\dots , x_{N-1}$ but thank you for your answer!
Comment: Choose $C$ large enough so that $C > {x_k \over r^k}$ for $k=1,...,N$.

Proof for sequence


How to calculate Laplace inverse of $\frac{1}{(s^{2}+1)^{2}}$?

Question: Laplace inverse of $\frac{1}{(s^{2}+1)^{2}}$.
I have beaten my head about this but have not got a clue as to how to do it! Can someone help? I tried to express this as some form of a derivative, but I am really just not sure what to do since a derivative of $\frac{1}{s^{2}+1}$ would involve a 2s in the numerator. Can someone please help?
Comment: Can you compute the forward transform of $f = \sin(t) - t \cos(t)$?
Comment: @mattos What was your rationale behind choosing that?
Comment: Experience, though you could probably deduce it by considering the Laplace transforms of $\sin(t), \cos(t)$ and $t$.
Answer: $$F(s)=\frac{1}{(s^{2}+1)^{2}}=\frac{2s}{(s^{2}+1)^{2}} \times \dfrac 1 {2s}$$
$$F(s)=- \dfrac {d}{ds}\left  (\frac{1}{(s^{2}+1)} \right) \times \dfrac 1 {2s}$$
$$F(s)=G(s) H(s)$$
And their inverse Laplace Transform are:
$$g(t)=t \sin t \,\, , h(t)=\dfrac 12$$
Then use the convolution theorem. Evaluate this integral:
$$f(t)=\int_0^t g(\tau) h(t-\tau)  d \tau$$
$$f(t)=\int_0^t  \tau \sin (\tau) \times \dfrac 12 d \tau$$
For the Convolution Theorem look here formula 33
here
You should find:
$$f(t)=\dfrac 12 (\sin t - t\cos t)$$
Comment: Could you point me to tje convoluyion theorem?
Comment: Open the link it's formula 33  @user00000000001899


Como guardar vários usuários para uma conta como o Netflix por exemplo

Question: Seguinte, tenho uma aplicação que suporta apenas 1 conta por usuário, quero atualizar o banco de dados em MySQL para que ele suporte um sistema que seja possível guardar por exemplo, contas filhos, derivadas dessa conta pai, que é a principal.
Minha aplicação suporta apenas 1 conta por usuário como já foi dito, todas as informações da conta do usuário estão guardadas em apenas uma row.
MySQL
SQL |
    \-[Table] Users |
                    \-[rows] id  | UserID | Username | Resto...
                              1  |   845  |  João    |   ...
                              2  |   846  |  Mãria   |   ...
                             ... |   ...  |    ...   |   ...

Neste exemplo, temos João, ele tem 3 filhos, eu deveria dar suporte as sub-contas, mas não sei como fazer isso. As contas filhos precisam ter um UserID único diferente das contas pais para que o sistema possa processa-la baseado no UserID e assim fazer o processamento das outras informações, assim como excluir um filho quando for necessário.
Filhos
... | UserID | Username | Resto das configurações pessoas de cada filho...
... |  888   | F.João 1 | ...
... |  777   | F.João 2 | ...
... |  222   | F.João 3 | ...
... | outros |  filhos  | de outros usuários ...

Existe alguma forma de fazer isso?

Se possível, mas não mais importante do que o SQL, gostaria de um
  exemplo em php de como pegar esse filho e exclui-lo, baseado na conta pai,  que é a principal.

Isso foi preciso pois os usuários não poderiam acessar em 2 plataformas diferentes com a mesma conta. Por isso preciso de um sistema baseado no netflix.
Comment: Formularei uma resposta quando estiver folgado. Por enquanto, tente pesquisar sobre o relacionamento, creio que te ajudará a dar mais alguns passos.
Comment: Também pode fazer criando uma coluna `userParent` e quando um usuário for criado como *dependente*, basta relacionar o *userParent* com o *userID*. Posteriormente quando determinado *vinculo* deixar de existir entre os usuários, basta apagar o valor de *userParent*.
Comment: O que você está precisando é um relacionamento "um para muitos". Crie uma nova tabela "profiles" e a relacione com sua tabela de "users".
Comment: Acho que entendi(na teoria, a pratica é sempre diferente), mas como a pergunta está aberta, é melhor esperar alguém responder. Obrigado pela dica.
Answer: Se as contas filhas fazem logon
Você pode utilizar n:m para uma tabela se relacionar com ela mesma, eu não posso afirmar que este é o melhor caminho, no entanto desta maneira você poderá ter níveis "infinitos" (filhos, netos, dependentes, sócio, esposa, etc), um exemplo com Mysql e innoDB:

CREATE TABLE IF NOT EXISTS `mydb`.`usuarios` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `nome` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mydb`.`usuarios_has_usuarios` (
  `usuarios_id` INT NOT NULL,
  `usuarios_id1` INT NOT NULL,
  PRIMARY KEY (`usuarios_id`, `usuarios_id1`),
  INDEX `fk_usuarios_has_usuarios_usuarios1_idx` (`usuarios_id1` ASC),
  INDEX `fk_usuarios_has_usuarios_usuarios_idx` (`usuarios_id` ASC),
  CONSTRAINT `fk_usuarios_has_usuarios_usuarios`
    FOREIGN KEY (`usuarios_id`)
    REFERENCES `mydb`.`usuarios` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_usuarios_has_usuarios_usuarios1`
    FOREIGN KEY (`usuarios_id1`)
    REFERENCES `mydb`.`usuarios` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

A vantagem de usar chave estrangeira, é que se houver usuários dependentes eles ficarão "amarrados" a outros usuários. Mesmo que você tente remover o usuário PAI (supondo que seja um acidente) a tabela não vai permitir isto. No entanto você pode sim usar o sem chave estrangeira, mas se você deletar ou atualizar algo errado, isto irá começar a exibir dados incoerentes.
Um exemplo sem chave estrangeira seria criar uma coluna a mais na tabela usuários:
CREATE TABLE IF NOT EXISTS `mydb`.`usuarios` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `parentId` INT NULL,
  `nome` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = myIsam;

Permiti NULL na parentId, mas você também pode usar NOT NULL (que provavelmente ficará 0), não muda muita coisa.
Se as contas filhas não fazem logon (serve apenas para salvar dados)
Conforme conversei com o autor, o objetivo não é ter logins para os filhos, apenas dados de preferencias salvos. Neste caso eu recomendaria a criar 3 tabelas, uma de "perfis", uma para as "contas" e outra para as "preferencias".
A tabela contas e perfis se relacionam 1:n e a tabela preferencias se relaciona com a tabela perfis 1:1. Não é necessário, a estrutura que descrevi funciona tanto "com" ou "sem" as chaves estrangeiras, mas como eu disse, a vantagem é amarrar os dados.

CREATE TABLE IF NOT EXISTS `mydb`.`contas` (
  `id_contas` INT NOT NULL AUTO_INCREMENT,
  `login` VARCHAR(45) NULL,
  `senha` VARCHAR(45) NULL,
  PRIMARY KEY (`id_contas`))
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mydb`.`perfis` (
  `id_perfis` INT NOT NULL,
  `nome` VARCHAR(45) NULL,
  `perfiscol` VARCHAR(45) NULL,
  `contas_id_contas` INT NOT NULL,
  `preferencias_id_preferencias` INT NOT NULL,
  PRIMARY KEY (`id_perfis`, `contas_id_contas`),
  INDEX `fk_perfis_contas_idx` (`contas_id_contas` ASC),
  INDEX `fk_perfis_preferencias1_idx` (`preferencias_id_preferencias` ASC),
  CONSTRAINT `fk_perfis_contas`
    FOREIGN KEY (`contas_id_contas`)
    REFERENCES `mydb`.`contas` (`id_contas`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_perfis_preferencias1`
    FOREIGN KEY (`preferencias_id_preferencias`)
    REFERENCES `mydb`.`preferencias` (`id_preferencias`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `mydb`.`preferencias` (
  `id_preferencias` INT NOT NULL AUTO_INCREMENT,
  `preferencia1` VARCHAR(45) NULL,
  `preferencia2` VARCHAR(45) NULL,
  PRIMARY KEY (`id_preferencias`))
ENGINE = InnoDB;

No entanto se for uma tabela sem chave estrangeira, ela terá que ter colunar para apontar as ids das outras tabelas, por exemplo:

CREATE TABLE IF NOT EXISTS `mydb`.`contas` (
  `id_contas` INT NOT NULL AUTO_INCREMENT,
  `login` VARCHAR(45) NULL,
  `senha` VARCHAR(45) NULL,
  PRIMARY KEY (`id_contas`))
ENGINE = myIsam;

CREATE TABLE IF NOT EXISTS `mydb`.`perfis` (
  `id_perfis` INT NOT NULL,
  `contas_id_contas` INT NULL,
  `nome` VARCHAR(45) NULL,
  PRIMARY KEY (`id_perfis`))
ENGINE = myIsam;

CREATE TABLE IF NOT EXISTS `mydb`.`preferencias` (
  `id_preferencias` INT NOT NULL AUTO_INCREMENT,
  `perfis_id_perfis` INT NULL,
  `preferencia1` VARCHAR(45) NULL,
  `preferencia2` VARCHAR(45) NULL,
  PRIMARY KEY (`id_preferencias`))
ENGINE = myIsam;

Como usar as queries
Para logar será necessário apenas selecionar contas, e salvar o id na sessão.
SELECT id_contas, login, senha FROM contas WHERE login=? AND senha=?

Para exibir os perfis, você pode usar algo como
SELECT id_perfis, nome, perfiscol FROM perfis WHERE contas_id_contas=? #O ? deve ser o ID que foi salvo na sessão

Para exibir os perfis e configurações:
SELECT
    P1.id_perfis AS ID_PERFIL,
    P1.nome AS NOME,
    P1.perfiscol AS PERFIL_DATA,
    P2.id_preferencias AS ID_PREFERENCIA,
    P2.preferencia1 AS PREFERENCIA_1,
    P2.preferencia2 AS PREFERENCIA_2

FROM
    perfis P1, preferencias P2

where
    P1.id_perfis = P2.perfis_id_perfis

Comment: @Florida, entendi, então soluções aqui parecem não ser uteis pra ti. Vou tentar reformular.
Comment: Estou tendo um pouco de dificuldades pra compreender, eu já estava tentando implementar o que o BlueX falou, mas sem chaves estrangeiras, apenas usando o `php` e `Join` pois já utilizei isso pra [algo parecido](http://pt.stackoverflow.com/questions/17147) não sei se aquele exemplo poderia ser aplicado aqui, ou melhorado, por isso vou esperar mais um pouco antes de marcar alguma resposta.
Comment: @Florida editei a resposta, mas eu estou com uma duvida, você quer que o usuário filho acesse pela própria conta? Que tipo de dados você quer trazer? Talvez isto me ajude a formular as queries pra ti.
Comment: Não, o sistema de login fica apenas na conta principal,(conta pai), assim como o login, senha, e informações importantes relevantes a todos os usuários, nas contas filhos, ficam por exemplo o idioma, preferencias de abas, session particular do filho. Ou seja, a conta pai é como uma conta filho, mas é a principal e traz dados de login, senha, level e quantas contas filhos ele pode ter. Todos os filhos logam através da conta pai e dai fazem a escolha do perfil(filho) a qual querem usar. _Se não tiver entendio algo, pode perguntar._
Comment: Assim deve funcionar, para evitar que fiquem _fragmentos_ quando alguma conta for deletada por exemplo, posso criar uma função no `php` e apagar os filhos dela. Os dois exemplos são bem úteis, quem preferir as chaves estrangeiras podem usa-las, gostei das duas alternativas.
Answer: Quando desejamos desenvolver uma base de dados devemos estrutura-la afim de criar relacionamentos entre as tabelas, este relacionamento parte do principio de haver algo em comum nas tabelas, por exemplo:
Tabela Pai:
id | Username | Resto..-
 1 |   João   |  ...
 2 |   Maria  |  ...

Tabela Filhos:
id | pai_id | Username | Resto...
1  |   1    |  António |  ...
2  |   2    |  Pedro   |  ...
3  |   1    |  Miguel  |  ...
4  |   1    | Joaquim  |  ...

Repara que existe um elemento em comum em ambas as tabelas, neste caso o field pai_id refere-se ao id existente na tabela pai, obviamente cabe a ti escolher o nome dado ao respetivo field, mas por norma pai_id provem da seguinte nomenclatura tabela_field, desta forma as coisas tornam-se mais organizadas e fáceis de entender.
Lembro dizer que os fields id existentes nas tabelas pai e filho são AUTO_INCREMENT, enquanto que o field pai_id não, este é preenchido apenas quando o filho é criado.
Estrutura de uma base de dados:

Esta imagem demonstra a estrutura de uma base de dados e podes ver que existem os tais elementos em comum.
Na prática:
Para obteres o respetivo filho, o query seria algo como isto:
SELECT * FROM `filhos` WHERE pai_id='valor';

Para excluir poderias utilizar o mesmo field como identificação:
DELETE FROM `filhos` WHERE pai_id='valor';

Isto tudo dito foi a base para o desenvolvimento da tua aplicação, cabe a ti estruturar à tua maneira.

Em relação ao sistema e seguindo um pouco a tua lógica (apesar de não saber ao certo como o sistema do netflix funciona) vou apresentar uma ideia de implementação deste sistema:

A conta principal encontraria-se na tabela pai e esta seria responsável pelos pagamentos.
As sub-contas estariam na tabela filhos e estas seriam responsáveis por filtrar conteúdos.

Funcionamento: O utilizador faria o login na conta principal e por sua vez apareceria uma lista com as sub-contas, o utilizador escolheria a sua sub-conta e faria o login nessa sub-conta.


DocuSign Notary API Error - NOTARY_NOT_ALLOWED

Question: I am reviewing the Docusign Notary functionalioty through the API. But i am stuck in one point.
The API Return an exception "{"errorCode":"NOTARY_NOT_ALLOWED","message":"Notary not enabled."}"
.I am using developer account to test the above fuctionality(demo.docusign.net). Is there any additional settings there to enable Docusign Notary?
<code> private static EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string signerClientId, string docPdf, string accountId)
    {

        byte[] buffer = System.IO.File.ReadAllBytes(docPdf);
        EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition();
        envelopeDefinition.EmailSubject = "Please sign this document";
        Document doc1 = new Document();
        String doc1b64 = Convert.ToBase64String(buffer);
        doc1.DocumentBase64 = doc1b64;
        doc1.Name = "Lorem Ipsum";
        doc1.FileExtension = "docx";
        doc1.DocumentId = "3";
        envelopeDefinition.Documents = new List<Document> { doc1 };
        Signer signer1 = new Signer
        {
            Email = signerEmail,
            Name = signerName,
            ClientUserId = signerClientId,
            RecipientId = "2",
            NotaryId = "1",
            RoutingOrder = "1"
        };
        NotaryRecipient notaryRecipient = new NotaryRecipient
        {
            Email = "firstname.lastname@example.com",
            Name = "xxx",
            RecipientId = "1",
            RoutingOrder = "1",
            Tabs = new Tabs
            {
                NotarySealTabs = new List<NotarySeal>() { new NotarySeal { XPosition = "50", YPosition = "150", DocumentId = "3", PageNumber = "1" } },
                SignHereTabs = new List<SignHere>() { new SignHere { XPosition = "300", YPosition = "150", DocumentId = "3", PageNumber = "1" } }
            },
            UserId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",//accountId,
            NotaryType = "remote"
        };

        Tabs signer1Tabs = new Tabs
        {
            SignHereTabs = new List<SignHere>() { new SignHere { XPosition = "150", YPosition = "150", DocumentId = "3", PageNumber = "1" } }
        };
        signer1.Tabs = signer1Tabs;
        Recipients recipients = new Recipients
        {
            Signers = new List<Signer> { signer1 },
            Notaries = new List<NotaryRecipient> { notaryRecipient }

        };
        envelopeDefinition.Recipients = recipients;
        envelopeDefinition.Status = "sent";
        return envelopeDefinition;
    }
</code>
did I missed any thing?
And I also added Notary Public in the corresponding account
Answer: The code is a bit off. You need to do this (see blog post on the topic):
<code>var notaryHost = new NotaryHost
{
    Name = "Nadia Notary",
    Email = "email@example.com",
    DeliveryMethod = "email",
    RecipientId = "2",
    Tabs = new Tabs { NotarizeTabs = notarizeTabs }
};
// InPersonSigner is used here even if the signer doesn't sign in person
var inPersonSigner = new InPersonSigner
{
    NotaryHost = notaryHost,
    Name = "Eddie End User",
    Email = "email@example.com",
    RecipientId = "1",
    InPersonSigningType = "notary",
    Tabs = new Tabs { SignHereTabs = signHereTabs }
};
var inPersonSigners = new List<InPersonSigner>();
inPersonSigners.Add(inPersonSigner);
var recipients = new Recipients{ InPersonSigners = inPersonSigners };
</code>
PS
It may be that you are trying to use the beta Remote Online Notary feature, and not the more established eNotary that is part of eSign. If that was your intention, you may not be able to do this, as it's a closed beta and not open yet to everyone.
Comment: Thanks for the update Inbar. Actually I am trying to test Beta RON feature. Is there any way to enroll me in beta testing?. I already registerd the form provided by DocuSign.
Comment: Any way to test RON feature?
Comment: Thank you for your interest in DocuSign Notary! We are working on a way to make Notary features available in your developer account, but this is not broadly available yet today. Please contact a sales representative to learn more about our Notary offering.


3 forms Show and ShowDialog not working as expected, BUG?

Question: I am using Visual Studio 2010, C# .NET 4.0. I have 3 forms: Form1, Form2, Form3.
In Form1 I have a button to open Form2:
private void button1_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    f.Show();
}

In Form2 I have a private Form3 variable always pointing to the same Form3:
private Form3 f = new Form3();

And a button to open it as a dialog:
private void button1_Click(object sender, EventArgs e)
{
    f.ShowDialog();
}

In Form3 I just have a button to hide the form:
private void button1_Click(object sender, EventArgs e)
{
    this.Hide();
}

The problem is that having the situation that Form2 is in front of Form1, and Form3 in front of Form2, when I click the button of Form3 to hide it, it not only hides itself but sends Form1 to the back of all of the other Windows.
This only happens when there is a window of another program (such as Windows Explorer) in the background of Form1. It seems like a bug. What do you think?
Comment: as I know it is a known bug. we cannot find a solution and live with it about 2 years(((
Answer: Yes, this cannot work properly by design.  A dialog disables all of the windows that your program displays.  So that it is modal.  When you hide the dialog, there are no windows left that can get the focus.  Windows is forced to find another window to give the focus to.  That will be a window owned by another application.  Your own windows will now hide behind it.
There are more side effects, the dialog will also close.  Necessary because otherwise the user can never get back to your program anymore since all windows are disabled.  This is all unsurprising behavior.  Bug would be a strong word, but it would of course work better if it first re-enabled all windows before closing the dialog.  But closing the dialog is already undesirable behavior.
Don't call Hide() for a dialog.  Just set the DialogResult property to DialogResult.Cancel to achieve the exact same effect, minus the focus problem.  You do have to reset it back to None if you want to display the dialog again.  That's a real bug.
Comment: Thanks a lot for you answer. But for me the dialog is not closing. When hidding, it returns control to the owner form (just after ShowDialog call) but I can do another ShowDialog call without problems and the dialog form is restored in the same state. So it was nos closed.
Comment: Oh, well, it seems that Closing does not involve disposing, so the form state is saved and the form is really closed but I can just open it again. My fault.
Comment: That's quite correct.  "Closing" here means that the window is closed, the form object is still alive because it didn't get disposed.  And allows recreating the window with ShowDialog.
Comment: @Hans Passant this issue exists even in SQL Management Stuido. Can you recommend any workaround?
Comment: @Sergey: I gave a good workaround in the bottom of my post.  Of course, won't work for SQL Management Studio since you don't have the source code for it.  Troubled program, I noticed before.  Not sure why you'd hide one of its dialogs though.
Comment: @Hans sorry missed text about DialogResult. as for SQL Studio - just to prove that is known behavior.
Answer: By the documentation. Form.Close method doesn't dispose forms shown by Form.ShowDialog method. Quote:

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

So, maybe there are ways to return focus to your application (e.g. via Windows API). But it is much more convenient to call Form.Close manually on dialog windows.


fetch OPTIONS return HTTP 401 while Curl Command succeeds

Question: This is what my fetch code looks like
let getSummary = (year, month) => {
        let url = baseUrl + "/rest/monthlySummaries/" +
            localStorage.getItem("paUserId") + "/" + year + "/" + month;

        let authHeaders = {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Bearer": localStorage.getItem("paToken")
        };

        console.log("summary url:", url, ",headers:", authHeaders);
        return fetch(url, {
            method: "GET",
            headers: authHeaders
        });
    };

Since this is GET request, browsers make preflight reqeusts using HTTP OPTIONS to make sure that they make indeed make HTTP GET requests. I log what call is made, I see
summary url: https://api.myapp.com/rest/monthlySummaries/userId/2017/4 ,headers: Object {Content-Type: "application/json", Accept: "application/json", Bearer: "41afa8432aaa411e48b6c1c637c77cb3:userId:84000000"}Accept: "application/json"Bearer: "41afa8432aaa411e48b6c1c637c77cb3:userId:84000000"Content-Type: "application/json"__proto__: Object
2VM50885:1 OPTIONS https://api.myapp.com/rest/monthlySummaries/cca6b151-cab4-4de2-81db-9a739a62ae88/2017/4 401 (Unauthorized)

While, when I do similar thing on curl, everything works
curl -v -X OPTIONS -H"BEARER:e3310afc4dcd68d80d56a83bddfd4a09:userId:564000000" "https://api.myapp.com/rest/monthlySummaries/userId/2017/4"
*   Trying 22.214.171.124...
* Connected to api.myapp.com (18.104.22.168) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> OPTIONS /rest/monthlySummaries/userId/2017/4 HTTP/1.1
> Host: api.myapp.com
> User-Agent: curl/7.43.0
> Accept: */*
> BEARER:e3310afc4dcd68d80d56a83bddfd4a09:userId:564000000
>
< HTTP/1.1 200 OK
< Date: Mon, 29 May 2017 23:21:11 GMT
< Server: WildFly/8
< X-Powered-By: Undertow/1
< Access-Control-Allow-Headers: origin, content-type, accept, authorization
< Allow: HEAD, GET, OPTIONS
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< Content-Type: text/plain
< Content-Length: 18
< Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEAD
< Access-Control-Max-Age: 1209600
< Vary: Accept-Encoding
<
* Connection #0 to host api.myapp.com left intact

Why the behavior is so different? What am I missing in fetch?
UPDATE
My server enables CORS support
@Provider
public class CORSFilter implements ContainerResponseFilter {
  @Override
  public void filter(ContainerRequestContext containerRequestContext,
      ContainerResponseContext containerResponseContext) throws IOException {
    containerResponseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
    containerResponseContext.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
    containerResponseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
    containerResponseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
    containerResponseContext.getHeaders().add("Access-Control-Max-Age", "1209600");
  }
}

This can be seen in the response as well
Comment: it's CORS request, so You need to serve `OPTIONS` method, is it nodejs app on serverside? if YES so just install CORS package (https://www.npmjs.com/package/cors) and do `app.use(require('cors')());`
Comment: @daydreamer simple curl will not do preflight request to get cors allowed methods, but browser fetch code will do. So again: needed to server OPTIONS method from server with 200 OK status.
Comment: Yes, the server is little itchy, but as you can see the response on `CURL` response with right headers and authenticates as well
Answer: See where you have configured
.add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");

You haven't allowed the Bearer header, nor is it the correct way to pass a JWT.
You probably meant
"Authorization": `Bearer ${localStorage.getItem("paToken")}`

Sending Bearer when it isn't in the allowed headers list will fail the pre-flight validation.
Comment: totally agree (:


Show $\sum_{n=1}^\infty \left(\frac{n}{2n-1}\right)^n$ converges

Question: I'm trying to show that $\sum_{n=1}^\infty \left(\frac{n}{2n-1}\right)^n$ converges. Using the Limit Ratio Test for Series, we want to show that $\lim_{n\to \infty} \left\lvert \frac{a_{n+1}}{a_n}\right\lvert<1$. However, I'm having trouble finding said limit (I know that it is equal to $\frac{1}{2}$, but I don't know how show it). Thanks in advance
Answer: Hint. One may use the root test, what is
$$
\lim_{n \to \infty} \sqrt[n]{a_n}\,?
$$
Comment: @Oliver Oloa Thank you for your answer. I haven't covered the Root Test, but I looked it up and I see how to get the answer. Is there another way to show this? It's because my professor will probably mark me down for using something not covered in lectures.
Answer: Note that we can write
$$\begin{align}
\left(\frac{n}{2n-1}\right)^n&=\frac{1}{2^n}\left(\frac{1}{1-\frac{1}{2n}}\right)^n\tag1\\\\
&\le \frac{1}{2^{n-1}}\tag2
\end{align}$$
where in going from $(1)$ to $(2)$ we invoked Bernoulli's Inequality.

NOTE:
The OP was pursuing a way forward that relied on the ratio test.  Proceeding, we have
$$\begin{align}
\lim_{n\to \infty}\frac{a_{n+1}}{a_n}&=\lim_{n\to \infty}\frac{\left(\frac{n+1}{2n+1}\right)^{n+1}}{\left(\frac{n}{2n-1}\right)^{n}}\\\\
&=\lim_{n\to \infty}\left(\left(\frac{n+1}{2n+1}\right)\,\left(1-\frac{1}{n(2n+1)}\right)^n\right)\\\\
&=\frac12
\end{align}$$
since from Bernoulli's Inequality we have
$$1\ge  \left(1-\frac{1}{n(2n+1)}\right)^n\ge 1-\frac{1}{2n+1}$$
whence application of the squeeze theorem reveals that the limit is $1$.
Answer: We have $n/(2n-1) \to 1/2.$ Hence for large $n$ we have $n/(2n-1) < 2/3.$ Since $\sum_n (2/3)^n < \infty,$ the given series converges by the comparison test.
Answer: You have
$$\left(\frac{n}{2n-1}\right)^n\sim \left( \frac{1}{2}\right)^n\frac{1}{\sqrt{e}}\;  (n\to +\infty)$$
$\implies $ the positive series converge.
Comment: Actually, no: $[(n/(2n-1)]^n \sim (1/\sqrt e)\cdot (1/2^n).$
Comment: @zhw. Yes .Thanks.
Answer: If $a_n = \left(\frac{n}{2n-1}\right)^n$, then
\begin{align} \frac{a_{n+1}}{a_n} &= \left(\frac{n+1}{2(n+1)-1}\right)^{n+1}/\left(\frac{n}{2n-1}\right)^n \\
&= \left(\frac{n+1}{2n+1}\right)\frac{\left(\frac{n+1}{2n+1}\right)^n}{\left(\frac{n}{2n-1}\right)^n} \\
&= \left(\frac{n+1}{2n+1}\right)\left(\frac{(n+1)(2n-1)}{n(2n+1)}\right)^n \\
&=\left(\frac{n+1}{2n+1}\right)\left(\frac{2n^2+n-1}{2n^2+n}\right)^n.
\end{align}
Now, $\frac{2n^2+n-1}{2n^2+n}<1$, so it follows that $\lim\limits_{n\rightarrow\infty}{\left(\frac{2n^2+n-1}{2n^2+n}\right)^n}\le 1$. (It is not too hard to show, in fact, that the limit is exactly $1$.) Hence
$$\lim\limits_{n\rightarrow\infty}{\left|\frac{a_{n+1}}{a_n}\right|} = \left(\lim\limits_{n\rightarrow\infty}{\frac{n+1}{2n+1}}\right)\left(\lim\limits_{n\rightarrow\infty}{\left(\frac{2n^2+n-1}{2n^2+n}\right)^n}\right)\le\frac{1}{2}\cdot 1 = \frac{1}{2} $$
i.e. $\lim\limits_{n\rightarrow\infty}{\left|\frac{a_{n+1}}{a_n}\right|} < 1$.
Comment: While you're correct that showing that the limit is $1$, that is a key to this development.  See my solution for an elementary approach.
Comment: @Dr.MV can you explain what you mean by "that is a key to this development"?
Comment: Without showing that $\lim_{n\to \infty}\left(\frac{2n^2+n-1}{2n^2+n}\right)^n=1$, then how do you propose to continue to arrive at the limit of interest?
Comment: @Dr.MV I showed that it is at most $1$; hence the final ratio is at most $1/2$. That's all we need to apply the ratio test, right?
Comment: Yes, it's fine.


Configuração do Debug do Eclipse

Question: Boa tarde.
Estou com um problema para debuggar meu projeto no Eclipse. Classes nativas de java estão aparecendo no processo de depuração quando as únicas classes que me são interessantes são as que fazem parte de meu projeto. Como opto por não visualizar classes nativas do Java?
Grato!
Comment: Isso acontece quando o fluxo chega no breakpoint? vc manda ele entrar no código?
Comment: Se eu fosse você não faria isso. Pode se confundir quando algum método lançar exceção.
Answer: Acione ou/e configure o Debug Step Filtering:
Window - Preferenes - Java - Debug - Step Filtering
A opção Use Step Filter deve estar acionada e as classes/pacotes que não quer depurar marcados (no mínimo java.* e javax.*).
Veja abaixo um exemplo:

se estiver faltando o pacote/classe em questão - java.* e javax.* - é só usar o respectivo botão da esquerda: Add Packages... ou Add Class... para selecionar pacotes ou classes; ou Add Filter... para entrar em forma de texto.
Obs: tambem existe um botão na tela (Perspective) do debugger para acionar/desligar o uso do Step Filter diretamente (Shift+F5).
Comment: No Use Step Filter havia apenas a opção java.Lang.ClassLoader, retirei ela no entanto, as classes como String.class ou Pattern.class ainda estão sendo mostradas no debug.

Configuração do Debug do Eclipse


Why is the approximation symbol used in universal algebra?

Question: Why is the approximation symbol "$\approx$" used in universal algebra instead of "is equal to" symbol?
Example: Bergman´s book "Universal Algebra: Fundamentals and Selected Topics ":

Edit: Found some explanation in the book, still not so clear to me though.
Comment: By the way, you should perhaps have mentioned that the book is *Universal Algebra: Fundamentals and Selected Topics* by Clifford Bergman, as opposed to, for example, *An Invitation to General Algebra and Universal Constructions* by George M. Bergman...
Comment: It's explained at the bottom of that page in that book!
Comment: For what it's worth, Wikipedia's ["Universal algebra" entry](https://en.wikipedia.org/wiki/Universal_algebra) uses "$=$".
Answer: I don't know about Bergman, but Burris & Sankappanavar: A Course in Universal Algebra has this to say on page 3:

The symbol "$=$" is used to express the fact that both sides name the same object, whereas "$\approx$" is used to build equations which may or may not be true of particular elements. (A careful study of $\approx$ is given in Chapter II.)
Comment: Don´t you have the symbols switched? I thought the equality symbol is tru for some variables, while the identity is just.. True, no matter what.
Comment: @TerezaTizkova No, it's not switched at all. But B&S allow the general case that the "identity for all" doesn't hold in a certain domain. But then they can still talk about the commutative law as $x \cdot y \approx y \cdot x$, even when it doesn't hold. "Laws" as objects in their own right, as it were.
Comment: @HennoBrandsma That is strange, since in my question´s screenshot, Bergman says that the $\approx$ asserts the equality for all, no the $=$.
Comment: No, that's exactly how B&S explain the symbols. (If you are in doubt, see it yourself by following the link!)
Comment: @TerezaTizkova Why not just accept that $\approx$ is about "laws" and $=$ about identities or equations?


Do earlier hidden layers learn more concepts/features than later ones, in neural networks?

Question: I am wondering whether there is a general statement of the sort "earlier layers in neural networks learn more concepts/features than later layers" or the other way around.
The output layer not being taken into account, as it should learn as many concepts as there are classes (in a classification task).
Are there any resources or papers which tackled this questions, maybe in image classification?
thanks
Comment: This question is very closely related. https://stats.stackexchange.com/questions/344498/cant-deep-learning-models-now-be-said-to-be-interpretable-are-nodes-features/344501#344501
Comment: If you consider each node in a neural network as defining a concept/feature, this will depend very much on the architecture of the network. If your earlier layers have more nodes, they will be able to detect more distinct features, but that will be flipped if your later layers have more nodes.
Comment: Define concept/feature. Twenty lvl1 Conv2D may learn an edge orientation, maybe local texture each; a single lvl 50 filter may learn a car, or a face. What's counts as learning more features?
Comment: I would not consider each node/neuron as a feature, since many neurons just capture noise and some learn the same concepts. That's a good point @jkm, I guess maybe something like (number of not noisy neurons)/(total number of neurons), and compute that for each layer? this would represent the intrinsic dimension of a layer
Answer: Lots of work have been carried out in this area, and in general I'd say your simplification holds true. A very good visual guide with discussion on how to actually visualize different nets is this website by Chris Olah and co-authors, which also includes an overview of other methods.
A nearly 10 years old paper on this is this classic by Lee et al.:
Answer: Although not fully answering your question, I recommend looking at the paper Identity Crisis: Memorization and Generalization under Extreme Overparameterization by Zhang et al., 2019. They discuss differences in behavior of fully connected layers and convnets in terms of inductive biases towards generalization and memorization, but also examine how much do different layers learn depending on the depth of the network, see e.g. Figure 15 in the supplementary material:


Principal Components of Random Walk

Question: In this blog figure 4 shows that the principal components of a random walk are sinusoidal with increasing frequency for decreasing eigenvalue. Is there an intuitive way of understanding this?
If I generate two independent random walks with a mean of zero, why is there a covariance (or correlation) between these trajectories in the first place?
Comment: Re last question, see https://stats.stackexchange.com/questions/255548.
Answer: I actually recently wrote a paper on this subject which will appear at NIPS 2018: https://arxiv.org/abs/1806.08805
My collaborator and I proved that in the limit of an infinite number of dimensions the projection of a random walk onto any PCA component is a sinusoid.  You are welcome to read the paper for the proof, but perhaps I can attempt a slightly more intuitive explanation.
The random walk process is a translation invariant process.  No matter how many steps you take and how far away you get from the origin, the process that determines the next step is exactly the same.  (This is in contrast to an Ornstein-Uhlenbeck process, for example, which is a random walk in a quadratic well.  In this case the process that determines what your next step will be depends on your distance from the origin.)
Now, the eigenfunctions of any translation invariant operator are Fourier modes.  Why is this?  Well, in order to be translation invariant, the eigenfunctions need to be periodic, and in order to be an orthogonal basis, they need to be mutually orthogonal.  The set of functions that satisfies these properties is the Fourier basis.
As for your second question about two independent random walks seeming to be correlated, this is just an illusion.  Although the projection of the trajectory of both random walks onto their first PCA component will be a cosine, the direction of the first PCA component will be completely random.  An an analogy, if you do two random walks, each of 10^6 steps, you can be quite sure that both will end up at a distance of about 1000 from the origin.  But the two walks had nothing to do with each other, and furthermore the direction that the walk happened to go will be different each time.
Comment: +1. Very nice! Congratulations on having a paper accepted to NIPS. Actually your paper is more interesting than all six papers I had to review for NIPS this year :) Regarding intuition, it's worth noting that Fourier modes appear every time one does PCA of some roughly translation-invariant smooth data. One well-known example is PCA of natural images (e.g. ImageNet). But it happens quite frequently and I recall quite some confusion about it in some fields; see e.g. https://www.nature.com/articles/ng.139 for discussion.
Comment: Sorry for the delay! The $\bf{v}_t$ in Eqs. 17 & 18 are unrelated to the eigenvectors defined previously. The $\bf{v}_t$ are simply a "velocity" term in the momentum update equation. $\bf{v}_t$ is initialized to 0 at time 0 and and then its value at later times is governed by Eq. 17.
Comment: Hi @vbip! I believe you have caught a typo. I think you are correct that it should be $2(1 + \gamma + \gamma^2)$. You'll notice in the following equation (21) that the first term in the inverse of the eigenvalue is $2(1 + \gamma + \gamma^2)$ which only makes sense if the previous equation is missing that last factor of 2 on the $\gamma^2$. I went ahead and redid the math and got $2(1 + \gamma + \gamma^2)$ like you.
Comment: As for your other questions, I don't remember explicitly deriving $\textbf{M}^{-1}$, but if you try multiplying $\textbf{M}\textbf{M}^{-1}$ for a 6x6 matrix, I think you can convince yourself that it works. Basically the fact that $\textbf{M}^{-1}$ is banded Toeplitz means that you're only ever considering the effect of neighboring elements of $\textbf{M}$ on each other. You're basically always doing $\gamma^k + \gamma^{k-1} (-\gamma) = 0$, except along the diagonal (or above it, where everything is zero).
Comment: No, I don't believe any of the rest of the paper would be affected. For Eqs. 17 & 18, you could check out p. 293 of Deep Learning by Goodfellow et al.: https://www.deeplearningbook.org/contents/optimization.html

Alternatively, eqs. 1 & 2 of Sutskever et al.: http://www.cs.toronto.edu/~hinton/absps/momentum.pdf
Comment: BTW, this has come up previously, e.g. here https://stats.stackexchange.com/questions/90484. I see that there is my answer there from several years ago, and it is very inadequate. I should edit it. Meanwhile, perhaps you'll be interested in answering that question too.
Comment: Another related thread is https://stats.stackexchange.com/questions/255548.
Comment: @amoeba is the idea that in a RW of high dimension, most dimensions will be approximately zero, and only a few dimensions will be large – thus, a linear combination of these few dimensions captures most of the variance?
Comment: @amoeba In this sense, isn't it a bit misleading to say each RW is explained by only the first few PC values (suggesting that RWs are low dimensional) since one had no way of knowing previously which of these low dimensions that the RW would take? See e.g. https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5913756/
Comment: @J.O'BrienAntognini Thank you for the clarification! I was having a hard time relating the $\mathbf{v_t}$ 's to the eigenvectors and this was helpful.
Comment: @j-obrien-antognini Nice paper! Regarding the math in your paper, I'm not clear how you derive Eq. (20), esp. the first term ($2+2\gamma+\gamma^2$). Would it be possible to elaborate? Also, (i) how do you derive that $\mathbf{M}^{−1}$ is a matrix with $1$s along the main diagonal and $- \gamma$s subdiagonal and (ii) are there some reference/citation/explanation for Eq. (17) and (18)? Thanks for your help in advance!
Comment: @j-obrien-antognini I'm asking because for Eq. (20), I am getting an additional $\gamma^2$ term ($2+2\gamma+2\gamma^2$) in some diagonal entries.
Comment: @J.O'BrienAntognini Thank you for the answer! I hope this doesn't change the rest of the proof, but it is good to know that I was not doing something stupid while trying to redo your derivations :) Also, I was wondering if you could provide some reference/citation/explanation for Eq. (17) and (18)?
Comment: @J.O'BrienAntognini What's not clear to me is whether $\mathbf{v}_t$ in Eqs. (17) and (18) are the non-normalized eigenvectors defined in Page 3? If they are, how are they related to the momentum update equations? Thank you again for the answers!

Principal Components of Random Walk


F1/Dice-Score vs IoU

Question: I was confused about the differences between the F1 score, Dice score and IoU (intersection over union). By now I found out that F1 and Dice mean the same thing (right?) and IoU has a very similar formula to the other two.

F1 / Dice: $$\frac{2TP}{2TP+FP+FN}$$
IoU / Jaccard: $$\frac{TP}{TP+FP+FN}$$

Are there any practical differences or other things worth noting except that F1 weights the true-positives higher? Is there a situation where I'd use one but not the other?
Comment: I made some contour plots for IoU and F1 for intuitive understanding inspired by one of the answers above. I posted my plots on https://tomkwok.com/posts/iou-vs-f1/.
Comment: Apparently the Jaccard coefficient is also the same as IoU
Comment: I'd be especially interested if some of these (now 4) measurements are only meant for binary data.
Comment: https://stats.stackexchange.com/a/55802/3277 answers why we need that 2 multiplier in Dice.
Comment: Here is a link to Tom Kwok's link above, which currently works: https://web.archive.org/web/20210619114818/https://tomkwok.com/posts/iou-vs-f1/
Comment: @Tom Kwok: Your link is not working
Answer: You're on the right track.
So a few things right off the bat. From the definition of the two metrics, we have that IoU and F score are always within a factor of 2 of each other:
$$ F/2 \leq IoU \leq F $$
and also that they meet at the extremes of one and zero under the conditions that you would expect (perfect match and completely disjoint).
Note also that the ratio between them can be related explicitly to the IoU:
$$ IoU/F = 1/2 + IoU/2 $$
so that the ratio approaches 1/2 as both metrics approach zero.
But there's a stronger statement that can be made for the typical application of classification a la machine learning. For any fixed "ground truth", the two metrics are always positively correlated. That is to say that if classifier A is better than B under one metric, it is also better than classifier B under the other metric.
It is tempting then to conclude that the two metrics are functionally equivalent so the choice between them is arbitrary, but not so fast! The problem comes when taking the average score over a set of inferences. Then the difference emerges when quantifying how much worse classifier B is than A for any given case.
In general, the IoU metric tends to penalize single instances of bad classification more than the F score quantitatively even when they can both agree that this one instance is bad. Similarly to how L2 can penalize the largest mistakes more than L1, the IoU metric tends to have a "squaring" effect on the errors relative to the F score. So the F score tends to measure something closer to average performance, while the IoU score measures something closer to the worst case performance.
Suppose for example that the vast majority of the inferences are moderately better with classifier A than B, but some of them of them are significantly worse using classifier A. It may be the case then that the F metric favors classifier A while the IoU metric favors classifier B.
To be sure, both of these metrics are much more alike than they are different. But both of them suffer from another disadvantage from the standpoint of taking averages of these scores over many inferences: they both overstate the importance of sets with little-to-no actual ground truth positive sets. In the common example of image segmentation, if an image only has a single pixel of some detectable class, and the classifier detects that pixel and one other pixel, its F score is a lowly 2/3 and the IoU is even worse at 1/2. Trivial mistakes like these can seriously dominate the average score taken over a set of images. In short, it weights each pixel error inversely proportionally to the size of the selected/relevant set rather than treating them equally.
There is a far simpler metric that avoids this problem. Simply use the total error: FN + FP (e.g. 5% of the image's pixels were miscategorized). In the case where one is more important than the other, a weighted average may be used: $c_0$FP + $c_1$FN.
Comment: I tried your total error approach and just wanted to add that it doesn't work well with constant imbalances between positives and negatives. Imagine an entire dataset of images where only one pixel makes up the ground truth segmentation. Neural networks might learn fairly quickly that an empty prediction is always 99.9% accurate using the total error. By going with IoU or DSC we pressure the network into finding a segmentation due to the same reasons you mentioned above. So, in the end it's very problem depend.
Comment: The former refers to a score of a single inference, and the latter refers to an average score over a set of inferences (e.g. a set of images).
Comment: Can someone help me reconcile the following two statements?: 1: `"That is to say that if classifier A is better than B under one metric, it is also better than classifier B under the other metric."` and 2: `"It may be the case then that the F metric favors classifier A while the IoU metric favors classifier B."`
Comment: willem, i couldn't have asked for a better answer. thank you very much for taking the time.
Answer: Yes, they indeed represent different things and have different meaning when looking at the formulas.
However, when you use them as a evaluation measure to compare the performance of different model, you only need to choose one of them.
The reason can be explained by the following evidence:
First, let $$ a = TP,\quad b=TP+FP+FN $$
Then, we have
$$ IoU = \frac{TP}{TP+FP+FN} = \frac{a}{b} $$
$$ Dice = \frac{TP+TP}{TP+TP+FP+FN} = \frac{2a}{a+b} $$
Hence,
$$ Dice = \frac{\frac{2a}{b}}{\frac{a+b}{b}}= \frac{2 \cdot \frac{a}{b}}{\frac{a}{b}+1} = \frac{2 \cdot IoU}{IoU + 1} $$
Considering the line plot of $y=2x/(x+1)$ in the range of [0,1], we find out that Dice has a monotonic increasing relation to IoU. Then the following situation will not happen: $Dice_1 < Dice_2$ while $IoU_1 > IoU_2$ (the subscript represents different model).
That is, Dice score is just similar representation of IoU under the numerical sense. It's enough to only using one of them for model comparison.
Answer: For Nico's answer above, I'm wondering shouldn't IoU be TP/(TP+FP+FN) instead of TP/(TP+FP+TN)? Also shouldn't the Dice score be (TP+TP)/(TP+TP+FP+FN)?
Comment: Welcome to CV. This does not really answer the question. Please submit a new question (and make a reference to this question if needed) when you are in this situation. The site works better this way and you increase your chance to get an answer. Thank you.
Comment: This does not provide an answer to the question. Once you have sufficient [reputation](https://stats.stackexchange.com/help/whats-reputation) you will be able to [comment on any post](https://stats.stackexchange.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/306960)
Answer: You can look at the ratio between DICE and IoU, it may be interesting:
$$
IoU = \frac{|A \cap B|}{|A \cup B|}
$$
$$
DICE = \frac{2|A \cap B|}{|A| + |B|}
$$
To compute the ratio, we would like to divide DICE by IoU, but need to be careful not to divide by zero, which will be the case when $|A \cap B| = 0$. In such case, by just looking at the metric definitions, we can notice that they both will be identically 0.
Recall that $|A \cup B| = |A| + |B| - |A \cap B| \Longrightarrow |A \cup B| + |A \cap B| = |A| + |B|$
So, suppose that $|A \cap B| > 0$, we get:
$$
\frac{DICE}{IoU} = \frac{2|A \cap B| |A \cup B|}{(|A| + |B|)|A \cap B|} = \frac{2|A \cup B|}{|A| + |B|} = \frac{2|A \cup B|}{|A \cup B| + |A \cap B|}
$$.
Now, we know that $|A \cap B| \le |A \cup B|$, which means that:

When the $A \cap B = \emptyset$, both metrics are identical and equal to 0 and the ratio cannot be computed.
When $A = B \Longrightarrow |A \cap B| = |A \cup B|$, the ratio will be 1, meaning that the two metrics are again identical, and both equal to 1.
In all other cases, since $|A \cap B| < |A \cup B|$, the ratio will be in $[1,2)$, meaning that DICE will be up to 2x larger that IoU.

To conclude,

DICE and IoU will be identical when $A = B$ or when $A \cap B = \emptyset$
When the intersection rate is very small but not identically 0, DICE will tend to be up to 2x larger than IoU, but never identically 2x.
The higher the intersection rate, the closer DICE and IoU will be, while DICE will remain somewhat larger than IoU, as long as the two sets aren't identical.

Here you can see the relationship between them visually. The union size is set to 1000 and the DICE/IoU ratio is plotted where the intersection size runs from 1 to 1000...

Here is the difference between them:

Here the two metrics are plotted individually, under the same conditions:

F1/Dice-Score vs IoU