Linear Extension

Question: I haven't encountered the concept of linear extensions in combinatorics before and was confused by the following questions:
How many linear extensions exist concerning a chain on n elements and a chain on m elements? Assume there are no possible comparabilities between the chains.
How many linear extensions exist concerning a chain on n elements and an antichain on m elements? Assume no comparabilities between the chain and antichain.
I'm aware of what chains and antichains mean in terms of posets, but just don't know what is meant by linear extensions. I'd appreciate any help.
Answer: You have a poset that is the disjoint union of two chains, one with $m$ and the other with $n$ elements. If $m>n$, the Hasse diagram of your poset looks something like this:
<code>      *
      |
      *
      |
      ·
      ·
      ·
      |
      *   *
      |   |
      *   *
      |   |
      ·   ·
      ·   ·
      ·   ·
      |   |
      *   *
      m   n
</code>
Technically this poset is a set $X$ together with a partial order $R$ on $X$. A linear extension of it is a pair $\langle X,L\rangle$, where $L$ us a linear order on $X$ such that $R\subseteq L$. In other words $L$ has all of the comparability relationships that $R$ has and enough more to make it a linear rather than just a partial order. If the two chains are $C_m$ and $C_n$, and the partial order is $\preceq$, one simple linear extension is the linear order $\le$ on $X=C_m\cup C_n$ that you get by defining $x\le y$ if and only if either $x,y\in C_m$ and $x\le y$, or $x,y\in C_n$ and $x\le y$, or $x\in C_m$ and $y\in C_n$. In other words, we just move $C_n$ 'above' $C_m$ in the Hasse diagram.
In this case the any linear ordering of the $m+n$ elements of $C_m\cup C_n$ will work provided that it preserves the internal orders of the changes $C_m$ and $C_n$. If $m=2$ and $n=3$, for instance, and the chains are $a<b$ and $1<2<3$, any permutation of $\{a,b,1,2,3\}$ that has $a$ somewhere before $b$, $1$ somewhere before $2$, and $2$ somewhere before $3$ yields a linear extension of the partial order.
I'll stop there to give you a chance to work it out on your own.
Comment: Thanks for the response. Hmm...it's not immediately obvious to me how to compute the total number of permutations that meets the constraints. I'm thinking of a recursive algorithm using an array....For example, let X = ${x_1,x_2, ..., x_n}$ and Y = ${y_1, y_2, ..., y_m}$, where all elements are "sorted" from least to greatest. Then I could designate some kind of array as follows: R[i][j] = the number of linear orders of $x_1$ thru $x_i$ and $y_1$ thru $y_j$. And I could recurse starting from the last elements...but I have a feeling that this is neither necessary nor intended by the question?
Comment: @user3280193: Indeed it's not; you're making it much too hard. Look at the miniature example in my answer. Any linear extension is going to have $5$ slots. If I tell you that the members of $C_2$ occupy the third and fifth slots (reading from small to large), can you tell me exactly what the whole linear order has to be?
Comment: Oh, so it just amounts to C(5, 2)? Or in the general case, C(m+n, n)?
Comment: @user3280193: You've got it. Once you know which slots are occupied by one chain, the order of those elements in those slots is fixed, as is the order of the elements of the other chain in *their* slots.
Comment: Thanks! Could you shed some light also on what this implies for the second question, regarding n-element chain and m-element antichain?
Comment: @user3280193: You're welcome! In that case you have essentially $m+1$ disjoint chains, one of $n$ elements and $m$ of $1$ element each. Getting a linear extension is then just a matter of deciding which $m$ slots will get the members of the antichain ***and*** deciding in which order to fill those slots, since the elements of the antichain can be arranged in any order.
Comment: Ah, ok...so then would we be looking at something like m! * C(m + n, n)? Since the m antichain elements can be permuted? Or am I missing something?
Comment: @user3280193: No, you're not missing anything: that's exactly right. You can write it in various ways: $m!\binom{m+n}n=m!\binom{m+n}m=\frac{(m+n)!}{n!}\;,$$ for instance.
Comment: Thanks again for your help!
Comment: @user3280193: You're welcome!

Linear Extension


nginx proxy causing error when response takes too long to reply

Question: I have an nginx configuration that redirects to a Django rest service (Through gunicorn).
Everything works correctly, but when the response is too big (takes more than 30s to respond) I'm getting a 503 service unavailable error.
I am sure it is because of this issue because it works correctly on other requests, and only on specific requests where the response is too big (and fetching the request from a third party api) takes too long.
Below is my nginx configuration :
server {
listen       www.server.com:80;
server_name www.server.com;

client_max_body_size 200M;
 keepalive_timeout 300;

location /server/ {
    proxy_pass http://127.0.0.1:8000/;
    proxy_connect_timeout 120s;
    proxy_read_timeout 300s;
    client_max_body_size 200M;
}

 location / {
    root   /var/www/html;
    index  index.html index.htm;
}
}

I am sure the issue is from Nginx and not gunicorn, because if i do a curl from inside the machine i get a response.
Thanks,
Comment: Please post the output of your [NGINX error log](https://stackoverflow.com/q/1706111/2532070). Also post the output (if any) in `/var/log/syslog` for one of these errors and the error log for your Gunicorn application. What is the server setup and what is the browser you're running (assuming this is a web request)?
Comment: That's the problem there's n error on either side, and from the application it's actually returning the result (although late)
Comment: If the application is returning the correct result, without an error, but slowly, is the problem that the third-party API server is too slow to respond? I'm not sure what the problem to be solved is. Is there an entry in NGINX access log? You're saying that NGINX shows a 503 error in its access log but nothing in the error log?
Comment: Could it be that the django application is failing? It seems the case.
Comment: @DanyY, see if my answer helps
Answer: You do specify proxy_connect_timeout and proxy_read_timeout, but never proxy_send_timeout.  (TBH, I don't think you need to modify timeout for connect(2), as that call simply established the TCP connection, and wouldn't depend on the size or time of an individual page; but the other two seem like a fair game.)
Additionally, as per https://stackoverflow.com/a/48614613/1122270, another consideration might be proxy_http_version — your curl is probably using HTTP/1.1, whereas nginx does HTTP/1.0 by default, and your backend might behave differently.
Comment: I added both the `proxy_http_version` and the `proxy_send_timeout` but i'm still facing this issue, any idea ?
Comment: Would probably look into the issues through [tcpdump](http://mdoc.su/o/tcpdump) to see where the error occurs.  Are you doing caching of proxy_pass?  Might be helpful to disable http://nginx.org/r/proxy_buffering, which could either be done for these long-standing requests within a specific location, or through an HTTP header.  BTW, the whole idea of long-standing requests is often moot — why exactly does anyone need to wait over 30 seconds for your request to complete?!
Comment: I'll try tcpdump to see what's happening, for the 30 seconds it's simply because I'm using a travel booking api form the backend, and they take this long sometimes to respond, you think keepalive_timeout is any use in this case ?
Comment: I suspect it may have something to do with `proxy_buffering`, then.  If the external API that your backend uses is so slow to confirm the reservation, it's generally been best practice to simply send a few bytes to the client until the whole page can be loaded (which would require turning off `proxy_buffering` in nginx to work properly); or, another good approach, is make the page refresh itself every 10 seconds, back to itself (just using the HTML http-equiv refresh), until the backend would have the confirmation, which is when you show the proper page instead of the refresh placeholder.
Comment: Thank you, this seems the most reliable way. when you say `best practice to simply send a few bytes to the client until the whole page can be loaded` you mean manually store the data in the database and make another call to fetch them back ?
Comment: For the refresh suggestion, yes, just store it yourself before another repeat request comes in; for the keepalive approach, just send some small pieces of noop data to the client to keep the connection active (e.g., HTML comments etc).  In the old days, that's how the chat would be implemented — an HTML page that loads *very slowly*, and, basically, never completes.
Answer: When you run below
$ gunicorn --help | grep -A2 -i time
  --graceful-timeout INT
                        Timeout for graceful workers restart. [30]
  --do-handshake-on-connect
                        Whether to perform SSL handshake on socket connect
--
  -t INT, --timeout INT
                        Workers silent for more than this many seconds are
                        killed and restarted. [30]

So I would assume the timeout happens from gunicorn and not through nginx. So You don't just need timeout increase on nginx side but also on gunicorn
You can either add
timeout=180

to your config.py file or you can add it to the command line when launching gunicorn
gunicorn -t 180 ......

Comment: Thank you for your answer, i actually took this into account, and tried increasing the timeout (same value as nginx) and tried adding workers, but still face the same issue
Comment: Can you post your nginx access log and error logs when the error occurs?
Comment: There are no errors, that is the issue. The error.log stays empty, and there's no added line in access.log
Comment: Are you using any load balancers in front? Or some server or does the IP directly hit nginx?
Comment: No the IP directly hits nginx
Comment: Be it a 502 or 503, access log should always have the hit I assume?
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165368/discussion-between-tarun-lalwani-and-dany-y).


How can I get the monthly sales with change in prices?

Question: Please help out a newbie if you can.
I have two tables, one contains individual sales:

item id
transaction date
location

101
2023.01.20
A

102
2023.02.12
A

103
2023.05.25
B

102
2023.06.04
C

101
2023.11.24
B

and the other contains the prices of the items during certain periods.

item ID
price
valid from
valid to

101
1
2023.01.01
2023.05.31

101
3
2023.06.01
2023.12.31

102
5
2023.01.01
2023.12.31

103
4
2023.01.01
2023.12.31

How would you make a report which shows what was the total sales value in certain months/quarters?
Connecting many to many certainly did not work, I also tried to put a middle table between them with only item IDs
Answer: I would suggest you add a Calculated Column to your first table that gets the Price from the second table.
<code>Price =
  var thisId = [item id]
  var thisDate = [transaction date]
  var result =
    CALCULATE(
      MIN(Table2[Price]),
      Table2[item ID] = thisId &&
      Table2[valid from] <= thisDate && thisDate <= Table2[valid to]
    )

  return result
</code>


What does group.setdefault() do in this context?

Question: I looked at the documentation [https://docs.python.org/2/library/stdtypes.html#dict.setdefault]

setdefault(key[, default]):
If key is in the dictionary, return its value.
If not, insert key with a value of default and return default. default defaults to None

However, seeing as the output of group.setdefault() isn't being saved in a variable. I find this odd.
To be more specific, this code is included in the definition of a class of a scheduler. I am using pytorch and as I'm new, I'm having difficulties understanding the code. So any python/pytorch ninja that can figure this out?
Here's a simplified version of the class:
     class BaseLRScheduler(_LRScheduler):
         def __init__(self, optimizer, last_epoch=-1):

             self.optimizer = optimizer
             if last_epoch == -1:
                for group in optimizer.param_groups:
                    group.setdefault('initial_lr', group['lr'])

             self.base_lrs = list(
                  map(lambda group: group['initial_lr'], optimizer.param_groups))
             self.step(last_epoch + 1)
             self.last_epoch = last_epoch

This is the problematic part of code for me:
     if last_epoch == -1:
         for group in optimizer.param_groups:
              group.setdefault('initial_lr', group['lr'])

Answer: The fact that a function returns a value does not make it mandatory to use that value.
In this case,
if 'initial_lr' is in the dictionary, nothing happens, but,
If 'initial_lr' is not in the dictionary, insert it with the value of group['lr'].
For example:
d = {'a': 1}
d.setdefault('a', 2)
print(d)
d.setdefault('b', 3)
print(d)

Outputs
{'a': 1}
{'a': 1, 'b': 3}

Comment: Thank you. I shoud have tried coding it. Instead I kept on searching for docs. Lesson learned!


Preencher select retorno web service do CEP

Question: Estou utilizando o exemplo do site viacep para quando o usuário digitar o CEP seja preenchido os outros dados do endereço, porem gostaria que o Estado através de um select seja preenchido automaticamente. O exemplo mostra ele sendo preenchido num input.
https://viacep.com.br/exemplo/javascript/
Meu javascript
<code>    //Pessoa
    function limpa_formulário_cep() {
    //Limpa valores do formulário de cep.
    document.getElementById('frm:tabForm:logradouro').value=("");
    document.getElementById('frm:tabForm:complemento').value=("");
    document.getElementById('frm:tabForm:bairro').value=("");
    document.getElementById('frm:tabForm:cidade').value=("");
    document.getElementById('frm:tabForm:estadoEndereco').value=("");
    }function meu_callback(conteudo) {if (!("erro" in conteudo)) {
    //Atualiza os campos com os valores.
    document.getElementById('frm:tabForm:logradouro').value=(conteudo.logradouro);
    document.getElementById('frm:tabForm:complemento').value=(conteudo.complemento);
    document.getElementById('frm:tabForm:bairro').value=(conteudo.bairro);
    document.getElementById('frm:tabForm:cidade').value=(conteudo.localidade);
    document.getElementById('frm:tabForm:estadoEndereco').value=(conteudo.uf);

} //end if.
else {
    //CEP não Encontrado.
    limpa_formulário_cep();
    alert("CEP não encontrado.");
}
}

function pesquisacep(valor) {

//Nova variável "cep" somente com dígitos.
var cep = valor.replace(/\D/g, '');

//Verifica se campo cep possui valor informado.
if (cep != "") {

    //Expressão regular para validar o CEP.
    var validacep = /^[0-9]{8}$/;

    //Valida o formato do CEP.
    if(validacep.test(cep)) {

        //Preenche os campos com "..." enquanto consulta webservice.
        document.getElementById('frm:tabForm:logradouro').value="...";
        document.getElementById('frm:tabForm:complemento').value=("...");
        document.getElementById('frm:tabForm:bairro').value="...";
        document.getElementById('frm:tabForm:cidade').value="...";
        document.getElementById('frm:tabForm:estadoEndereco').value="...";

        //Cria um elemento javascript.
        var script = document.createElement('script');

        //Sincroniza com o callback.
        script.src = '//viacep.com.br/ws/'+ cep + '/json/?callback=meu_callback';

        //Insere script no documento e carrega o conteúdo.
        document.body.appendChild(script);

    } //end if.
    else {
        //cep é inválido.
        limpa_formulário_cep();
        alert("Formato de CEP inválido.");
    }
} //end if.
else {
    //cep sem valor, limpa formulário.
    limpa_formulário_cep();
    }
};
</code>
Meu xhtml. O select do Estado eu trago do banco de dados
<code><p:fieldset legend="Endereço">
                    <p:panelGrid styleClass="noGridBorder">
                        <p:row>
                            <p:column />

                        </p:row>
                        <p:row>
                            <p:column>
                                <p:outputLabel value="CEP" for="cep" />
                            </p:column>
                            <p:column>
                                <p:inputMask id="cep" label="CEP" size="9" maxlength="9"
                                    required="true" requiredMessage="O campo CEP é obrigatório."
                                    value="#{pessoaBean.pessoaEndereco.cep}"
                                    onblur="pesquisacep(this.value);" mask="99999-999" />
                            </p:column>
                            <p:column>
                                <p:outputLabel value="Logradouro" for="logradouro" />
                            </p:column>
                            <p:column colspan="5">
                                <p:inputText id="logradouro" onkeyup="alteraMaiusculo()"
                                    required="true" label="Logradouro" size="95"
                                    requiredMessage="O campo Logradouro é obrigatório."
                                    value="#{pessoaBean.pessoaEndereco.logradouro}" />
                            </p:column>
                        </p:row>
                        <p:row>
                            <p:column>
                                <p:outputLabel value="Número" for="numero" />
                            </p:column>
                            <p:column>
                                <p:inputMask id="numero" label="Número" size="10"
                                    maxlength="10" required="true"
                                    requiredMessage="O campo Número é obrigatório."
                                    value="#{pessoaBean.pessoaEndereco.numero}"
                                    mask="9?999999999999" />
                            </p:column>

                            <p:column>
                                <p:outputLabel value="Bairro" for="bairro" />
                            </p:column>
                            <p:column>
                                <p:inputText id="bairro" onkeyup="alteraMaiusculo()"
                                    required="true" label="bairro" size="24"
                                    requiredMessage="O campo Bairro é obrigatório."
                                    value="#{pessoaBean.pessoaEndereco.bairro}" />
                            </p:column>

                            <p:column>
                                <p:outputLabel value="Cidade" for="cidade" />
                            </p:column>
                            <p:column>
                                <p:inputText id="cidade" onkeyup="alteraMaiusculo()"
                                    required="true" label="Cidade" size="24"
                                    requiredMessage="O campo Cidade é obrigatório."
                                    value="#{pessoaBean.pessoaEndereco.cidade}" />
                            </p:column>

                            <p:column>
                                <p:outputLabel value="Estado" for="estadoEndereco" />
                            </p:column>
                            <p:column>
                                <p:selectOneMenu id="estadoEndereco" required="true"
                                    requiredMessage="O campo Endereço/Estado é obrigatório."
                                    value="#{pessoaBean.pessoaEndereco.estado.idEstado}">
                                    <f:selectItems value="#{pessoaBean.listaEstados}" />
                                </p:selectOneMenu>
                            </p:column>

                        </p:row>
                        <p:row>
                            <p:column>
                                <p:outputLabel value="Complemento" for="complemento" />
                            </p:column>
                            <p:column colspan="3">
                                <p:inputText id="complemento" onkeyup="alteraMaiusculo()"
                                    label="Complemento" maxlength="30" size="55"
                                    value="#{pessoaBean.pessoaEndereco.complemento}">
                                </p:inputText>
                            </p:column>

                            <p:column>
                                <p:outputLabel value="Ponto de Ref." for="pr" />
                            </p:column>
                            <p:column colspan="3">
                                <p:inputText id="pr" onkeyup="alteraMaiusculo()"
                                    label="Ponto de Referência" size="50"
                                    value="#{pessoaBean.pessoaEndereco.pontoReferencia}">
                                </p:inputText>
                            </p:column>
                        </p:row>
                    </p:panelGrid>
                </p:fieldset>
</code>
Answer: Eu penso que seja desnecessário ter uma tabela somente para estados, um Enum resolveria, afinal são 26 estados e um distrito federal = 27 objetos. (É só uma opinião)
Essa é uma forma de resolver sem javascript, com ajax do primefaces
<code> <p:inputMask id="cep" label="CEP" size="9" maxlength="9"
  required="true" requiredMessage="O campo CEP é obrigatório."
  value="#{pessoaBean.pessoaEndereco.cep}"
   onblur="pesquisacep(this.value);" mask="99999-999">
       <p:ajax event="blur" update="estadoEndereco" listener="{pessoaBean.buscaCep}"/>
  </p:inputMask>
</code>
Classe que faz a busca no Webservice
<code> public CepWebService(String cep) {

    try {
        URL url = new URL("http://cep.republicavirtual.com.br/web_cep.php?cep=" + cep + "&formato=xml");

        Document document = getDocumento(url);

        Element root = document.getRootElement();

        for (Iterator i = root.elementIterator(); i.hasNext();) {
            Element element = (Element) i.next();

            if (element.getQualifiedName().equals("uf")) {
           Aqui vc poderia fazer a busca do estado como quer
                setEstado(element.getText());
            }
            if (element.getQualifiedName().equals("cidade")) {
                setCidade(element.getText());
            }
            if (element.getQualifiedName().equals("bairro")) {
                setBairro(element.getText());
            }
            if (element.getQualifiedName().equals("tipo_logradouro")) {
                setTipoLogradouro(element.getText());
            }
            if (element.getQualifiedName().equals("logradouro")) {
                setLogradouro(element.getText());
            }
            if (element.getQualifiedName().equals("resultado")) {
                setResultado(Integer.parseInt(element.getText()));
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}
</code>
Eu uso em produção e nunca deu problema
https://github.com/danniel20/CepServiceWeb
Comment: isso mesmo cara,  um Enum resolve e funciona de primeira.
Comment: Desse modo que eu to fazendo já ta carregando todos os campos do endereço. Ao colocar CEP o estado aparece num input, mais eu quero que seja num select o estado apareça dependendo do CEP que o usuário digitar. Do modo que tá eu tenho que selecionar o Estado clicando nele e escolhendo o Estado, não tá vindo automático ao adicionar o CEP.


Month name order by in mysql

Question: Dear friends I need help,
This is my query,
$customer = DB::table('customer');
$customer->join('customer_month_calender',
   'customer_month_calender.customer_id', '=', 'customer.id','left');

$customer->select('customer.id','customer_month_calender.month');
//$customer->where(DATE_FORMAT(month,'%m')=date('m',strtotime('+1 month')));
$customer->orderBy('customer_month_calender.month', 'create_date');
$customer->groupBy('customer.id');

return $customer->get();

This query return values for,
April
August
December
February
January
July
June

However, I wanted to have the Month names in order by
(ie. January, February, Mar.. an so on until the last one is December).
Please help. Thanks in advance.
Comment: Is it an array return form the $customer->get(); ?
Comment: yes it is an array
Comment: `ORDER BY Field(customer_month_calender, "January", "February", "Mar" an so on until the last one is "December")`
Answer: You can write query like this using orderBy with raw query:
$customer = DB::table('customer');
$customer->join('customer_month_calender',
   'customer_month_calender.customer_id', '=', 'customer.id','left');

$customer->select('customer.id','customer_month_calender.month');
//$customer->where(DATE_FORMAT(month,'%m')=date('m',strtotime('+1 month')));
$customer->orderBy(DB::raw('month(create_date) ASC'));
$customer->groupBy('customer.id');

return $customer->get();

Comment: Unknown column 'create_date' in 'order clause' (SQL: select `customer`.`id`, `customer_month_calender`.`month` from `customer` left join `customer_month_calender` on `customer_month_calender`.`customer_id` = `customer`.`id` group by `customer`.`id` order by month(create_date) asc)
Answer: If you are using this mysqli class, You could try with
$customer->orderBy('MONTH(create_date)', 'ASC');

With raw query:
ORDER BY MONTH(create_date)

Of course, you can customize to match your needs
Answer:     $customer = DB::table('customer');
$customer->join('customer_month_calender',
   'customer_month_calender.customer_id', '=', 'customer.id','left');

$customer->select('customer.id','customer_month_calender.month');
//$customer->where(DATE_FORMAT(month,'%m')=date('m',strtotime('+1 month')));
$customer->orderBy('customer_month_calender.month ASC');
$customer->groupBy('customer.id');

return $customer->get();

Month name order by in mysql


What is the probability of drawing a good team in a small office sweepstake?

Question: At my work we often run small sweepstakes at big sporting events, e.g. the football world cup, the Olympics, the rugby world cup.
As you may know, the rugby world cup is currently on. There are 20 teams involved, and 11 of us in the office. Using a betting website's odds, we whittled down the 20 teams to the 11 most likely to win outright, and drew them out of a hat.
Of course, each person to draw a team has one fewer potential team than the person who drew previously. The first person to draw has 11 possibilites. The second person has 10. The third has nine, etc.
Of these 11 teams, only three are realistically likely to win. Is it possible to calculate the probability of drawing one of these three at each stage of the draw? If so, is it better to draw earlier or later?
Comment: It doesn't matter in which order you draw. You could also distribute the tickets face down to everyone randomly and then everyone looks at their ticket at the same time. It has the same effect.
Comment: It makes no difference. [Consider shuffling 11 cards (A,2-10,J) and then drawing from the deck. Which position in the deck is most likely to have the A? ]
Answer: Here is a small simulation in R, that might convince you that the order does not affect the probability of drawing a winning team.
<code># Small simulation for order of drawing from a hat
# Probably an overkill to do it this way...

# Number of iterations
N <- 100000

# Vector to count number of successful draws
counter <- rep(0,11)

# iterate number of times to estimate probabilities
for(i in 1:N)
{
  # Iterate over each of potential 11 draws
  for(j in 1:11)
  {
    # Create a vector to draw from, i.e. the hat
    hat <- rep(0,11)

    # It has 11 zeroes, now we will sample 3 random
    # places to put the 3 best tickets
    placement <- sample(1:11,3)

    # Put the winning tickets in their place
    hat[placement] <- rep(1,3)

    # Draw from place j
    counter[j] <- counter[j]+hat[j]
  }
}

# Probabilities for each placement of draw
probs <- counter/N
names(probs) <- as.character(1:11)

# print the probabilities
print(probs)

# make a barplot to visualize it
barplot(probs)
title(paste('Probabilities for each placement of draw,
    iterations: ',as.character(N),sep="",collapse = NULL))
</code>
Now we can take a look at the plots for N=10000, N=100000 and N=1000000:
Here are 10K iterations, as we add more iterations the probabilities converge to a constant.

You can see that the probabilities as converging to the same value. So it does not matter where in the order you draw.
This is an example of a problem that is very easy to simulate if you do not know how to work out the probabilities and get a quick and dirty answer.
Answer: It doesn't matter in which stage you draw : if you are not convinced let's look at what happens for the first and second person that draw :
the first who draw as the probability to draw one of these teams equal to 3/11.
For the second :
If the first drew a team likely to win, it remains two over ten of them. In this case, the probability to draw it is $\frac{3}{11} \times \frac{2}{10}$. If he didn't, you have 3/10 chance to draw it. So the chance to draw a  team likely to win is $\frac{8}{11} \times \frac{2}{10}$. By additionning those two probabilties, you find it equal to $\frac{3}{11}$ also.


React Native - ios Native Module not working

Question: I'm following the example posted in the official doc for native module ios. I've set up everything, build it and run the application.
//  CAL.h

#import <React/RCTBridgeModule.h>

@interface CAL : NSObject <RCTBridgeModule>

@end

// CAL.m

#import <React/RCTLog.h>
#import "CAL.h"

@implementation CAL

RCT_EXPORT_MODULE(CAL);

RCT_EXPORT_METHOD(createCalendarEvent:(NSString *)name location:(NSString *)location)
{
 RCTLogInfo(@"Pretending to create an event %@ at %@", name, location);
}
@end

But when I check NativeModules from react-native it shows an empty object - {}.
I'm not sure what I'm missing.
Comment: Try checking `NativeModules.CAL` directly
Comment: Yes that's what I had to do. Thanks
Answer: Like @chengsam mentioned, when I access CAL directly in the following manner it works.
const { CAL } = NativeModules;
or
NativeModules.CAL
CAL still held the native module while NativeModules directly displayed {}


Image partly off screen killing as3 frame rate on IOS

Question: I'm developing a game in as3 for iPhone, and I've gotten it running reasonably well (consistanty 24fps on iPhone 3G), but I've noticed that when the "character" goes partly off the screen, the frame rate drops to 10-12fps.  Does anyone know why this is and what I can do to remedy it?
Update - Been through the code pretty thoroughly, even made a new project just to test animations.  Started a image offscreen and moved it across the screen and back off.  Any time the image is offscreen, even partially, the frame rates are terrible.  Once the image is fully on the screen, things pick back up to a solid 24fps.  I'm using cacheAsBitmap, I've tried masking the stage, I've tried placing the image in a movieclip and using scrollRect.  I would keep objects from going off the screen, except that the nature of the game I'm working on has objects dropping from the top down (yes, I'm using object pooling.  No, I'm not scaling anything.  Striclt x,y translations).  And yes, I realize that Obj-C is probably the best answer, but I'd really like to avoid that if I can.  AS3 is so much nicer to write in
Comment: Odd behavior maybe something in your code is triggering a loop. Why not just keep it from going off the screen?
Comment: Agreed. It's probably your code
Comment: my guess is something about how cacheAsBitmap performs on iOS -- perhaps when your sprites are entirely on-screen, there's no offscreen buffer, but moving them partially offscreen doubles the amount of pixels the iDevice is managing per frame?  also not sure if there are any hints in this thread: http://stackoverflow.com/questions/7972967/cacheasbitmap-has-no-effect-on-a-sprite-masked-with-a-scrollrect-in-air-for-ios
Answer: Try and take a look at the 'blitmasking' technique: http://www.greensock.com/blitmask
From Doyle himself:

A BlitMask is basically a rectangular Sprite that acts as a high-performance mask for a DisplayObject by caching a bitmap version of it and blitting only the pixels that should be visible at any given time, although its bitmapMode can be turned off to restore interactivity in the DisplayObject whenever you want. When scrolling very large images or text blocks, BlitMask can greatly improve performance, especially on mobile devices that have weaker processorst


Can't read a const in a class instance?

Question: I was mildly surprised when the compiler complained about this:
public class UsefulClass
{
    public const String RatingName = @"Ratings\rating";
}

public class OtherClass
{
    public void SomeFunc()
    {
        UsefulClass useful = new UsefulClass();
        String rating = useful.RatingName;
    }
}

Compiler says, "Static member cannot be accessed with an instance reference; qualify it with a type name instead"
This isn't a problem, String rating = UsefulClass.RatingName; works fine.  I'm just curious what the thinking is behind this?  I have an instance of a public class with a public constant on it, why can't I get the data this way?
Comment: It's horrid language design that severely violates http://en.wikipedia.org/wiki/Uniform_access_principle ... there's no reason for it, and they could fix it, with no impact on existing code, in a moment.
Answer: Because constants just aren't instance members; they're statically bound to their respective types. In the same way you can't invoke static methods using instances, you can't access class constants using instances.
If you need to get a constant off an instance without knowing its type first-hand, I suppose you could do it with reflection based on its type.
If you're trying to add a member that can't be modified but pertains to instances, you probably want read-only fields or properties instead.
Comment: You could also wrap the constant in a getter.
Comment: @BoltClock, I tried to test your point with `String something = this.RatingName` inside UsefulClass and got the same error, which is consistent with your answer.  I've always assumed `String something = RatingName` is looking at the instance, but I see now it's uses the Class.Variable description of a static.
Comment: @TomDestry: That's right, just like how you call a class's own static methods using `StaticMethod()` rather than `this.StaticMethod()`.
Comment: @BoltClock, Thanks, glad I asked now.
Answer: A "variable" marked const is a compile time construct, not an instance member.  You can access it like you would a static variable:
public void SomeFunc()
{
    UsefulClass useful = new UsefulClass();
    String rating = UsefulClass.RatingName; // Access as if static
}

That being said, I would personally wrap this into a property if it's meant to be used as you described, like so:
public class UsefulClass
{
    private const string ratingName = @"Ratings\rating";

    public string RatingName { get { return ratingName; } }
}

This would make your syntax work, but also be a better design, IMO, since it doesn't expose your constants publically.
Comment: @Noon Silk: I would suggest that this is correct, and not wrong, **if the data should be associated with an instance** (RatingName).  This was how the OP showed it, in which case, it's instance level data, and should be wrapped in a property (even if it's always returning a constant).  That makes the "constant" nature an implementation detail of the type itself.
Comment: @JimBalter I do have the constant declared privately here - I'm not sure where your criticism lies, at least with regard to my answer.
Comment: I disagree with the plan of wrapping it in a getter (that's wrong), but you've provided the right answer initially, at least (so I upvoted you).
Comment: Um, if you don't want to expose your constants publicly, declare them private. Not allowing access to public constants via instances is stupid language design that, like all violations of en.wikipedia.org/wiki/Uniform_access_principle , expose implementation details. There's no semantic difference between const int foo = 3 and readonly int foo = 3, but the language requires that they be qualified differently.
Comment: "a better design, IMO, since it doesn't expose your constants publically" -- the best design for not exposing your constants publicly is to declare them `private`. If you *do* want to expose them publicly, the best design is to declare them `public`. Wrapping them in properties is silly busywork forced upon us by bad language design. I thought I made it quite clear that my criticism was of the language design.
Answer: Because const in c# are implicitly of static type. And As static members can be accessed only on class member and not on instance, const cannot too.

Can't read a const in a class instance?


2 x 2 Phase Portrait for 2 x 2 Linear System with Real Coefficients. Boyce, p395, Figure 7.5.4a

Question:

The given general solution for some linear system is
  $
x= c_{2} \mathbf{ x^{(1)} }(t) + c_{2} \mathbf{ x^{(2)} }(t) =
c_{1}\
\left(\begin{array}{l}
1\\
\sqrt{2}
\end{array}\right)\
e^{-t}+
c_{2}
\left(\begin{array}{l}
-\sqrt{2}\\
1
\end{array}\right)\
e^{-4t} \qquad (21)
$.
Graphs of the solution (21) for several values of $c_{1}$ and $c_{2}$ are shown in Figure 7. $5.4a$.
  The solution $x^{(1)}(t)$ approaches the origin along the line $x_{2}=\sqrt{2}x_{1}$, and the solution $x^{(2)}(t)$ approaches the origin along the line $x_{1}=-\sqrt{2}x_{2}$....

(1) Please explain this last sentence? I know $\color{brown}{ x_2 = \sqrt{2}x_{1} \iff }
\begin{bmatrix}
x_1\\
x_2
\end{bmatrix}
= x_1 \begin{bmatrix}
1 \\
\sqrt{2}
\end{bmatrix}$
and
$ \color{green} { x_{1}=-\sqrt{2}x_{2} } \iff
 \begin{bmatrix}
x_1\\
x_2
\end{bmatrix}
= x_2 \begin{bmatrix}
-\sqrt{2} \\
1
\end{bmatrix}$

As $ t\rightarrow\infty$, the solution $x^{(2)}(t)$ is negligible compared to $x^{(1)}(t)$ .

(2) Why 'negligible'? I know when $k > 0$, $\lim_{t \to \infty} e^{-kt} = 0 $

Thus, unless $c_{1}=0$, the solution (21) approaches the origin tangent to the line $x_{2}=\sqrt{2}x_{1}$.

(3) Please explain this last sentence? I know if $c_1 =0 $ then any solution must be
$ c_{2} \mathbf{ x^{(2)} }(t)$

... If the eigenvalues were positive rather than negative, then the trajectories would be similar but traversed in the outward direction.

(4)  Please explain this last sentence?
Comment: Is there a "$=$" missing in the first equation between $\mathbf{x}^{(2)}(t)$ and $c_1$?
Comment: @Roland Thanks. yes. fixed it. any other errors?
Comment: As pointed out in my answer, the black arrows which have $\mathbf{x}^{(1)}$ and $\mathbf{x}^{(2)}$ as a label might be pointing at the wrong spot. Does my answer provide some help to you, by the way?
Answer: The first equation tells us (implicitly) $$\mathbf{x}^{(1)}(t)= \begin{pmatrix} 1 \\ \sqrt{2}\end{pmatrix}e^{-t}, \quad \mathbf{x}^{(2)}(t)= \begin{pmatrix} -\sqrt{2}\\ 1\end{pmatrix}e^{-4t}.$$

This means that, starting with $t=0$ and using $e^0 = 1$, we have initial values
$$\mathbf{x}^{(1)}(0)= \begin{pmatrix} 1 \\ \sqrt{2}\end{pmatrix}, \quad \mathbf{x}^{(2)}(0)= \begin{pmatrix} -\sqrt{2}\\ 1\end{pmatrix}.$$
As $t$ increases from $0$ towards $\infty$, the factors $e^{-k}$ and $e^{-4k}$ decrease from $1$ to $0$, i.e. the solutions $\mathbf{x}^{(1)}(t)$ and $\mathbf{x}^{(2)}(t)$ run along the red/green line. In view of this, the black arrows are misleadingly positioned.
This argument of Boyce is a bit handwavy here, and my explanation is going to be similarly handwavy - I hope that this makes it more understandable. The idea is that $e^{-4t}$ converges faster to $0$ than $e^{-t}$ does. I would not call it negligible, but the two solutions converge to zero at different rates. One way to see this is to write $c_1 \mathbf{x}^{(1)}(t)+ c_2 \mathbf{x}^{(2)}(t)$ as
$$e^{-t}\left(c_1\begin{pmatrix} 1 \\ \sqrt{2}\end{pmatrix} + c_2\begin{pmatrix} -\sqrt{2}\\ 1\end{pmatrix}e^{-3t}\right) .$$
As $t$ increases, the second summand in the brackets goes to $0$, while the first one remains.
Because of this difference in convergence speed, a linear combination of the two solutions gets very close to $\mathbf{x}^{(1)}(t)$, as the part with $\mathbf{x}^{(2)}(t)$ 'converges away'. You can see this behavior in the blue arrows, which are the general solutions of the system. All of them approach the origin along the direction of $\pm \begin{pmatrix} 1 \\ \sqrt{2}\end{pmatrix}$ eventually - the part of $\mathbf{x}^{(2)}(t)$ dimishes away. I am aware that this explanation is similarly handwavy as the original one.
This is lacking context. How are the solutions related to eigenvalues (of which matrix)?


Questions about a proof using $\omega$-covers and subsets of $\omega$

Question: I've been reading Galvin and Miller's article entitled "$\gamma$-Sets and Other Singular Sets of Real Numbers." Within this article is a Lemma.

Supose $X \in [\omega]^\omega$ and $\mathcal{O}$ is an open $\omega$-cover of $[\omega]^{< \omega}$. Then, there exists $\langle D_n : n < \omega \rangle \in \mathcal{O}$ and $Y \in [X]^\omega$ such that $Y^*$ is in all but finitely many $D_n$'s.

Here:

$[\omega]^\omega$ - the set of infinite subsets of $\omega$
$[\omega]^{< \omega}$ - the set of finite subsets of $\omega$
$\mathcal{O}$ is an $\omega$-cover of $X$ iff every finite subset of $X$ is contained in an element of $\mathcal{O}$
$Y^* = \{X \subseteq \omega : Y \setminus X \mbox{ is finite } \}$

I'm having some trouble following the proof. In the proof they claim you can construct an increasing sequence $\langle k_n : n < \omega \rangle$ from $X$ and a sequence $\langle D_n :n < \omega \rangle$ from $\mathcal{O}$ such that for each $n$ and $A \subseteq \omega$, if $A \cap (k_n, k_{n+1}) = \emptyset$, then $A \in D_n$. By $(k_n, k_{n+1})$, they mean $\{ i \in \omega : k_n < i < k_{n+1} \}$.
To show this, they assume by induction that we already have the $\langle D_1, D_2, \ldots D_{n-1} \rangle$ and the $\langle k_1, k_2, \ldots k_n \rangle$ that satisfy the hypothesis. From here, they let $Q = \{ q_i : i < 2^{k_n + 1} \}$ be all the elements of $[\omega]^{< \omega}$ contained in $k_n + 1$. (so far  so good)
Now, by definition, since $\mathcal{O}$ is a $\omega$-cover of $[\omega]^{< \omega}$, there is a $D_n \in \mathcal{O}$ which covers $Q$. Now, since $D_n$ is open, there is some $k_{n-1} \in X$ such that for all $q \in Q$,
$$ \{A \subseteq \omega : A \cap k_{n+1} = q \cap k_{n+1} \} \subseteq D_n.$$
Can this be done because we can make $D_n$ big enough so that these types of subsets  of $\omega$ are included in it?
From here, they say this does the construction. I'm not sure why this completes the construction, or how this can imply $A \cap (k_n, k_{n+1}) = \emptyset \Rightarrow A \subseteq D_n$.
Next, they claim $Y = \{ k_n : n < \omega \}$ has the required properties. Is $Y$ the set of all sequences that satisfy the construction?
Then, to show $Y$ works, they say: suppose $Z - Y \subseteq k_n$ But then, for all $m > n$:$ Z \cap (k_m, k_{m+1}) = \emptyset$ and hence $Z \in D_m$. I'm not seeing how this shows $Y^*$ is in all but finitely many of the $D_n$'s.
Any help or insight would be greatly appreciated.
Thanks!
Comment: The condition that $D_n \in \mathcal{O}$ for $n<\omega$ seems to be missing from the statement of the lemma.
Comment: @JohnBentin Thanks for pointing that out. Fixed it.
Comment: @bof Yes, it means $D_1, D_2, \ldots$ is a sequence of $\omega$-covers of $[\omega]^{<\omega}$
Answer: To get $k_{n+1}$, we use the fact that $D_n$ is open: For each $i$, writing $c_i$ for the characteristic function of $q_i$, this means that the cone above some initial segment of $c_i$ is contained in $D_n$. Choose $k_{n+1} \in X$ larger than the lengths of all these initial segments. It is important to remember the identification of subsets of integers by points in $2^{\omega}$ to make sense of openness in this context.
The definition of $Y^{\star}$ has a typo and $X, Y$ should be reversed there. $Y^{\star}$ is supposed to be all sets of integers which are contained in $Y$ modulo finite sets.


PCA with on data with unequal length

Question: I am wondering how to perform PCA on data with unequal lengths. In the simplest case you could split up the data matrix so that one block would be $T_{1}Xn$ and the rest is $T_{2}Xn$. The second block has effectively missing data in the first $T_{1}-T_{2}Xn$ block.
Many of the applications of PCA on missing data are variants of the EM algorithm methodology (these typically will assume the distribution is multivariate Gaussian) that are more suited to applications where the data is missing at random. Oftentimes, these techniques do not particularly apply to data with uneven lengths. However, there are techniques to estimate the mean and covariance matrix in these cases and it is possible to apply PCA to the resulting covariance matrix. However, when the number of variables becomes large (absolutely or relative to the number of observations), then it may no longer be convenient to estimate the covariance matrix in this fashion.
Comment: Excuse me, what does this mean `data is T1Xn and T2Xn`? And did by "unequal length" you just mean that there are missing values?
Comment: So the data is $TXn$, but you could break it into the respective panels that would be of different length. The first $T_{2}-T_{1}Xn$ block would be missing in the second panel.
Comment: I'm sorry if this comes a bit too late... I'm currently looking for a solution to the same problem. You mention that there are techniques to estimate the mean and covariance matrix in these cases and it is possible to apply PCA to the resulting covariance matrix... would you mind kindly giving me some references? Thank you in advance!
Comment: This is a common one: https://ideas.repec.org/p/fth/pennfi/05-96.html
Answer: What I've seen is an interpolation of the data in order to match the length (and even the sampling) of the data. This was particulary used (successfully, I must say) in the classification of variable stars using PCA, where the data where the actual light curves of the stars. For more details, see the paper of Deb & Singh (2009).
I must add, however, that if you are going to do PCA interpolating data, maybe Functional Data Analysis techniques are more suitable (I've been thinking in writing a paper with this method, in fact, and compare it with the PCA approach).
Comment: It looks to me like that paper interpolates missing data within the sample. Hence, that might be a situation where one could apply the EM algorithm approach. I am thinking about the situation where there is a lot of missing data at the beginning of some series.
Comment: I'm also not that familiar with Functional Data Analysis, so I'm not sure how to go about applying that.
Comment: If there is not much data at the beggining, then extrapolating could be a solution but, as you say, there's a lot of data missing so I don't really know if this approach would work for you then.

FDA is just as PCA applied to continuous functions (hence the name). For an introduction to the subject, you should check out Ramsey & Silverman's book: http://www.springer.com/statistics/statistical+theory+and+methods/book/978-0-387-40080-8.
Comment: Extrapolating is like the technique used to estimate the mean and covariance matrix for unequal data. Basically, you would estimate the mean and covariance of the data that is longest and then regress the smaller dataset on the longer dataset. The error estimates would be calculated over the shorter period, but you could use the whole series of the longer dataset to predict the values of the shorter series at the longer lengths.
Answer: PCA is not the only dimensionality reduction that can be used, there are plenty of other methods, e.g. you may consider feature extraction to extract distinctive features from your data (with different lengths).
FFT on a sliding window; overlapped or not, might be a good option,
DCT of the results of FFT or without might be another option, it all depends on the Data used.

PCA with on data with unequal length


Why is this Drupal services RESTful API not working?

Question: I'm using the Services module on Drupal 7. I want to implement a service that allows me to submit a search term via AJAX and receive an array containing the top 5 nodes (node id and title) that loosely match the title. When connecting to http://example.com/api/ I receive this message

Services Endpoint "search" has been setup successfully.

but when attempting to navigate to something like http://example.com/api/search/Test, I get a 404.
Comment: I suggest to check out [RESTws](https://www.drupal.org/project/restws), which will be part of D8 core.
Comment: For future readers, consider using HTTP POST vs GET requests to retrieve data from the endpoint. Many services endpoints require POST requests.
Answer: Later of try and try, when read from other tutorial, i make a resource with rest, soap for me is imposible in drupal, i try but without success, you can copy and page this code and modify only the custom module name and hooks.
/**
 * Implements hook_ctools_plugin_api().
 */
function core_custom_webservice_ctools_plugin_api($owner, $api) {
  if ($owner == 'services' && $api == 'services') {
    return array(
      'version' => 3,
      'file' => 'core_custom_webservice.services.inc'
    );
  }
}

function core_custom_webservice_services_resources() {
  $resources = array(
    'webservice_resources' => array(
      'operations' => array(
        'retrieve' => array(
          'help' => t('Response of webservice'),
          'file' => array('type' => 'inc', 'module' => 'core_custom_webservice', 'name' => 'core_custom_webservice.resource',),
          'callback' => '_core_custom_webservice_get_response',
          'access callback' => '_core_custom_webservice_access',
          'access arguments' => array('view'),
          'access arguments append' => TRUE,
          'args' => array(
            array(
              'name' => 'parameters',
              'type' => 'string',
              'description' => 'The parameters that define requested data',
              'source' => array('path' => '0'), // first argument in the url
              'optional' => FALSE,
            ),
          ),
        ),
      ),
    ),
  );
  return $resources;
}

/* * *************************************************************************************************
 * Access callback
 * For now only view/retrieve is implemented and if the user is logged in, he gets access
 */

function _core_custom_webservice_access($op) {
  global $user;
  $access = TRUE;

  switch ($op) {
    case 'view':
      if ($user->uid) {
        $access = TRUE;
      }
      break;
  }

  return $access;
}

function _core_custom_webservice_get_response($arg) {
  $response = 'something';
  return 'print '.$response;
}

try again in your service path and success with
http://path.come/?q=webservice_server_rest/webservice_resources/string.json
Answer: If you created a RESTful Resource with the Services module, then I believe it will expect to respond to HTTP requests in JSON format, therefore you could try navigating to http://example.com/api/search/Test.json
Answer: Try to use this module https://www.drupal.org/project/services_views .
It has currently two features:
- Create view based resource creating Services display in a view
- Execute any view of the system via views resource call
Answer: basically 404 is not found error

Make sure that your service end point is
http://example.com/api/search/{search-term}
what is the response body to be send is in correct format

3.the content headers is set to application/json and name Content-Type
Answer: Hi First thing you have to do is
STEP 1: create a custom module and implement hook_services_resources()
EXAMPLE:
function mymodule_services_resources() {
  return array(
    'search' => array(
      'create' => array(
        'help' => 'Search for a content',
        'file' => array('file' => 'inc', 'module' => 'your module name'),
        'callback' => '_function_to_call_when_this_service_is_called',
        'access callback' => 'user_access',
        'access arguments' => array('access content'),
        'access arguments append' => FALSE,
        'args' => array(
         array(
           'name' => 'data',
           'type' => 'struct',
           'description' => 'The id of the data to get',
           'source' => 'data',
           'optional' => FALSE,
         ),
       ),
      ),
     ),
  );
}

STEP 2: go to

Structure -> services

and enable your module
STEP 3: sending data to the client
   $data -  data sent to client it should be array
  $url = $base_url.'/api/search';
            $response = drupal_http_request($url, array(
                'headers' => array('Content-Type' => 'application/json', 'Accept' => 'application/json'),
               'method' => 'POST',
               'data' => json_encode($data),
               'max_redirects' => 0,
               )
             );

print the response to check if its working fine.
CHECK LIST:

check for any log in by enabling DB logging module call your service.
check for any syntax error because that file is called only when services is call until then you wont see any error or warning.
print the response and test the data is passing correctly  or not.
check the client end to check.
Answer: I was getting this error too and then I realized I needed to include both the path of my service endpoint and the path of my View in the URL.
Originally I was trying http://my.local/events but I needed to be using http://my.local/events/events (I had named my service endpoint path the same as my View).


Convergence of characteristic functions on hypercube

Question: I have a question regarding the following partition of a hypercube $H_{R}(x)$ centered at $x$ with sides of length $R$  in $\mathbb{R}^{n}$:
Consider this hypercube $O = H_{R}(x) = \prod_{i=1}^{n}(\alpha_{i},\beta_{i}) = (\alpha_{1},\beta_{1}) \times \prod_{i=2}^{n}(\alpha_{i}, \beta_{i})$ and the following partition of it:
We divide $(\alpha_{1}, \beta_{1})$ into intervals of length $(\beta_{1}-\alpha_{1})2^{-m}$ where $m \in \mathbb{N}$. Each of these intervals is then subdivided into two parts of equal length $(\beta_{1}-\alpha_{1})2^{-(m+1)}$. The union of the first(resp. the second) subintervals is denoted by $I_{m}$(resp. $J_{m}$). We denote by $$O_{1}^{m} = I_{m} \times \prod_{i=2}^{n}(\alpha_{i}, \beta_{i})$$ $$O_{2}^{m} = J_{m} \times \prod_{i=2}^{n}(\alpha_{i}, \beta_{i})$$ then $\text{meas } O_{1}^{m} = \text{meas }O_{2}^{m} = \frac{1}{2}\text{meas } O$
Can anyone see how it would follow that, if considering the characteristic functions $\chi_{O_{1}^{m}}$ and $\chi_{O_{2}^{m}}$ on the sets $O_{1}^{m}$ and $O_{2}^{m}$ respectively, that we get the result that $\chi_{O_{1}^{m}} \rightharpoonup^{*} \frac{1}{2}$ and $\chi_{O_{2}^{m}} \rightharpoonup^{*} \frac{1}{2}$ in $L^{\infty}(O)$.
Thanks for any assistance, let me know if anything is unclear.
Comment: @LucioD No, it's the other way around: $L^\infty$ is the dual of $L^1$. The dual of $L^\infty$ is the space of absolutely continuous (w.r.t. the lebesgue measure) *finitely additive* measures and $L^1$ corresponds only to the subspace of true (=countably additive) measures. Either way: The weak* topology on $L^\infty$ is nevertheless given by the pairing with $L^1$.
Comment: By weak$*$ convergence do you mean that you pair with $L^1$ functions?
Comment: @DimitriosNtalampekos Yes since $L^{1}$ is the dual of $L^{\infty}$
Comment: @JohannesHahn Yes I made a mistake I see, I am using Brezis book of functional analysis, he doesn't go into the dual of $L^{\infty}$ with any great detail, but he does note that $L^{1}$ is a subspace of the dual of $L^{\infty}$. What text do you use to describe the dual of $L^{\infty}$? Also would it then still follow that $f_{n} \rightharpoonup^{*} f$ in $L^{\infty} \Leftrightarrow \langle g, f_{m} \rangle \rightarrow \langle g, f \rangle $? I think that's what you stated at the end of you message?
Comment: Weak-star-convergence $f_i \xrightarrow{w*} f$ in a dual space $X'$ is *defined* as $\langle g,f_i\rangle \to \langle f,g\rangle$ for all $g\in X$. We have the special case $X=L^1$ here. What do you mean by "what text do you use" ? Do you want to have a textbook reference? I think I don't have a real reference for that. I learned that fact from Elstrodt's (german) book "Maß- und Integrationstheorie" where there is a minor remark to that end and I then solved the (implied) exercise to prove that statement for myself.
Comment: @JohannesHahn Okay thanks understood. Do you have any idea how to resolve the above problem?
Answer: You have a bounded sequence $f_m$ in $L^\infty$, specifically $\|f_m\|_{L^\infty}\le 1$ for all $m$. For such a sequence, to verify that $f_m\xrightarrow{w^*} f $ it suffices to check that $$\langle g,f_m\rangle \to \langle g,f\rangle \tag{1}$$ for all $g$ in some set $A\subset L^1$ with norm-dense linear span.  Indeed, once we have (1) for $g\in A$, we have it for linear combinations of elements of $A$. And then, given any $h\in L^1$, we can find $g$ in the span of $A$ such that $\|h-g\|_{L^1}\le \epsilon$. It follows that
$$  | \langle h,f_m\rangle - \langle h,f\rangle | \le 2\epsilon + | \langle g,f_m\rangle - \langle g,f\rangle |$$
and since $\epsilon$ is arbitrarily small, $| \langle h,f_m\rangle - \langle h,f\rangle | \to 0$.
It remains to pick a convenient set $A$ with norm-dense span in $L^1$. I suggest the set of characteristic functions of dyadic rectangles. With this choice, you'll  see that  for every $g\in A$,
$$\int gf_m = \frac12 \int g$$
for all $m$ large enough. This shows (1),
Comment: Thanks for your response, interesting answer. I unfortunately have some questions. If you are willing, maybe you can send me an email at email@example.com, and we can correspond via email. Could you please give some detail about your choice of dyadic rectangles and how this would give the desired $\langle g, f_{m} \rangle \rightarrow \langle g, f \rangle$ for all $g$ in $A$ as you stated. I'm having difficulty finding a good reference for how exactly you would define dyadic rectangles simply.
Comment: Could you plz describe the 'set of characteristic functions of dyadic functions' that you are referring to?
Comment: @Fundamental Nice answer. I suppose for the last part of your proof where you state $$\int gf_{m} = \frac{1}{2}\int g$$ the reasoning is as follows: Take the case for proving just $\chi_{O_{1}^{m}} \rightharpoonup^{*} \frac{1}{2}$. Let $f_{m} := \chi_{O_{1}^{m}}$ and $g = \chi_{A_{i}}$ where $A_{i}$ is a dyadic cube. Then $\int gf_{m} = \int \chi_{A_{i}\cap O_{1}^{m}} = \text{meas}(A_{i}\cap O^{m}_{1}) = \frac{1}{2}\text{meas}(A_{i}) = \frac{1}{2}\int g$.
Comment: @Fundamental The problem is what if the dyadic cube $A_{i}$ is taken so small that $A_{i} \subset O^{m}_{1}$? Then $\text{meas}(A_{i}\cap O^{m}_{1}) = \text{meas}(A_{i})= \int g$. What do you think?


RuntimeError: running_mean should contain 256 elements not 128 pytorch

Question: I am a newbie in PyTorch, GAN, and I don't have much experience in Python (Although I am a C/C++ programmer).
I have a simple tutorial code for DCGAN for generating fake image, and it was ok when i run the code with "DATASETNAME = 'MNIST'". However, when i change the dataset to 'CIFAR10', the program produces error related to "running_mean".
The code is as below

import torch.nn as nn

def weights_init(module):

    if isinstance(module, nn.Conv2d) or isinstance(module, nn.ConvTranspose2d):
        module.weight.detach().normal_(mean=0., std=0.02)
    elif isinstance(module, nn.BatchNorm2d):
        module.weight.detach().normal_(1., 0.02)
        module.bias.detach().zero_()
    else:
        pass

class View(nn.Module):

    def __init__(self, output_shape):
        super(View, self).__init__()
        self.output_shape = output_shape

    def forward(self, x):
        return x.view(x.shape[0], *self.output_shape)

class Generator(nn.Module):

    def __init__(self, dataset_name):
        super(Generator, self).__init__()
        act = nn.ReLU(inplace=True)
        norm = nn.BatchNorm2d

        if dataset_name == 'CIFAR10':  # Output shape 3x32x32
            model = [nn.Linear(100, 512 * 4 * 4), View([512, 4, 4]), norm(512), act]  # 4x4
            model += [nn.ConvTranspose2d(512, 256, 5, stride=2, padding=2, output_padding=1), norm(256), act]  # 8x8
            model += [nn.ConvTranspose2d(256, 128, 5, stride=2, padding=2, output_padding=1), norm(128), act]  # 16x16
            model += [nn.ConvTranspose2d(128, 3, 5, stride=2, padding=2, output_padding=1), nn.Tanh()]  # 32x32

        elif dataset_name == 'LSUN':  # Output shape 3x64x64
            model = [nn.Linear(100, 1024 * 4 * 4), View([1024, 4, 4]), norm(1024), act]  # 4x4
            model += [nn.ConvTranspose2d(1024, 512, 5, stride=2, padding=2, output_padding=1), norm(512), act]  # 8x8
            model += [nn.ConvTranspose2d(512, 256, 5, stride=2, padding=2, output_padding=1), norm(256), act]  # 16x16
            model += [nn.ConvTranspose2d(256, 128, 5, stride=2, padding=2, output_padding=1), norm(128), act]  # 32x32
            model += [nn.ConvTranspose2d(128, 3, 5, stride=2, padding=2, output_padding=1), nn.Tanh()]  # 64x64

        elif dataset_name == 'MNIST':  # Output shape 1x28x28
            model = [nn.Linear(100, 256 * 4 * 4), View([256, 4, 4]), norm(256), act]  # 4x4
            model += [nn.ConvTranspose2d(256, 128, 5, stride=2, padding=2), norm(128), act]  # 7x7
            model += [nn.ConvTranspose2d(128, 64, 5, stride=2, padding=2, output_padding=1), norm(64), act]  # 14x14
            model += [nn.ConvTranspose2d(64, 1, 5, stride=2, padding=2, output_padding=1), nn.Tanh()]  # 28x28

        else:
            raise NotImplementedError

        self.model = nn.Sequential(*model)

    def forward(self, x):
        return self.model(x)

class Discriminator(nn.Module):

    def __init__(self, dataset_name):
        super(Discriminator, self).__init__()
        act = nn.LeakyReLU(inplace=True, negative_slope=0.2)
        norm = nn.BatchNorm2d

        if dataset_name == 'CIFAR10':  # Input shape 3x32x32
            model = [nn.Conv2d(3, 128, 5, stride=2, padding=2, bias=False), act]  # 16x16
            model += [nn.Conv2d(128, 256, 5, stride=2, padding=2, bias=False), norm(128), act]  # 8x8
            model += [nn.Conv2d(256, 512, 5, stride=2, padding=2, bias=False), norm(256), act]  # 4x4
            model += [nn.Conv2d(512, 1, 4, stride=2, padding=2, bias=False), nn.Sigmoid()]  # 1x1

        elif dataset_name == 'LSUN':  # Input shape 3x64x64
            model = [nn.Conv2d(3, 128, 5, stride=2, padding=2, bias=False), act]  # 128x32x32
            model += [nn.Conv2d(128, 256, 5, stride=2, padding=2, bias=False), norm(128), act]  # 256x16x16
            model += [nn.Conv2d(256, 512, 5, stride=2, padding=2, bias=False), norm(256), act]  # 512x8x8
            model += [nn.Conv2d(512, 1024, 5, stride=2, padding=2, bias=False), norm(512), act]  # 1024x4x4
            model += [nn.Conv2d(1024, 1, 4), nn.Sigmoid()]  # 1x1x1

        elif dataset_name == 'MNIST':  # Input shape 1x28x28
            model = [nn.Conv2d(1, 64, 5, stride=2, padding=2, bias=False), act]  # 14x14
            model += [nn.Conv2d(64, 128, 5, stride=2, padding=2, bias=False), norm(128), act]  # 7x7
            model += [nn.Conv2d(128, 256, 5, stride=2, padding=2, bias=False), norm(256), act]  # 4x4
            model += [nn.Conv2d(256, 1, 4, bias=False), nn.Sigmoid()]  # 1x1

        else:
            raise NotImplementedError

        self.model = nn.Sequential(*model)

    def forward(self, x):
        return self.model(x)

if __name__ == '__main__':

    import os
    from torchvision.transforms import Compose, Normalize, Resize, ToTensor
    from torch.utils.data import DataLoader
    #from models import Discriminator, Generator, weights_init
    import torch
    import torch.nn as nn
    import matplotlib.pyplot as plt
    from time import time
    from tqdm import tqdm
    from torchvision.utils import save_image
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    BETA1, BETA2 = 0.5, 0.99
    BATCH_SIZE = 16
    DATASET_NAME = 'CIFAR10'
    DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu:0')
    EPOCHS = 1
    ITER_REPORT = 10
    LATENT_DIM = 100
    LR = 2e-4
    N_D_STEP = 1
    ITER_DISPLAY = 500

    IMAGE_DIR = './GAN/checkpoints/'+DATASET_NAME+'/Image'
    MODEL_DIR = './GAN/checkpoints/'+DATASET_NAME+'/Model'

    if DATASET_NAME == 'CIFAR10':
        IMAGE_SIZE = 32
        OUT_CHANNEL = 3
        from torchvision.datasets import CIFAR10
        transforms = Compose([ToTensor(), Normalize(mean=[0.5], std=[0.5])])
        dataset = CIFAR10(root='./datasets', train=True, transform=transforms, download=True)
    elif DATASET_NAME == 'LSUN':
        IMAGE_SIZE = 64
        OUT_CHANNEL = 3
        from torchvision.datasets import LSUN
        transforms = Compose([Resize(64), ToTensor(), Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
        dataset = LSUN(root='./datasets/LSUN', classes=['bedroom_train'], transform=transforms)
    elif DATASET_NAME == 'MNIST':
        IMAGE_SIZE = 28
        OUT_CHANNEL = 1
        from torchvision.datasets import MNIST
        transforms = Compose([ToTensor(), Normalize(mean=[0.5], std=[0.5])])
        dataset = MNIST(root='./datasets', train=True, transform=transforms, download=True)
    else:
        raise NotImplementedError

    data_loader = DataLoader(dataset=dataset, batch_size=BATCH_SIZE, num_workers=0, shuffle=True)

    D = Discriminator(DATASET_NAME).apply(weights_init).to(DEVICE)
    G = Generator(DATASET_NAME).apply(weights_init).to(DEVICE)
    print(D, G)
    criterion = nn.BCELoss()

    optim_D = torch.optim.Adam(D.parameters(), lr=LR, betas=(BETA1, BETA2))
    optim_G = torch.optim.Adam(G.parameters(), lr=LR, betas=(BETA1, BETA2))

    list_D_loss = list()
    list_G_loss = list()
    total_step = 0

    st = time()
    for epoch in range(EPOCHS):
        for data in tqdm(data_loader):
            total_step += 1
            real, label = data[0].to(DEVICE), data[1].to(DEVICE)
            z = torch.randn(BATCH_SIZE, LATENT_DIM).to(DEVICE)

            fake = G(z)

            real_score = D(real)
            fake_score = D(fake.detach())

            D_loss = 0.5 * (criterion(fake_score, torch.zeros_like(fake_score).to(DEVICE))
                            + criterion(real_score, torch.ones_like(real_score).to(DEVICE)))
            optim_D.zero_grad()
            D_loss.backward()
            optim_D.step()
            list_D_loss.append(D_loss.detach().cpu().item())

            if total_step % ITER_DISPLAY == 0:
                #(BatchSize, Channel*ImageSize*ImageSize)-->(BatchSize, Channel, ImageSize, ImageSize)
                fake = fake.view(BATCH_SIZE, OUT_CHANNEL, IMAGE_SIZE, IMAGE_SIZE)
                real = real.view(BATCH_SIZE, OUT_CHANNEL, IMAGE_SIZE, IMAGE_SIZE)
                save_image(fake, IMAGE_DIR + '/{}_fake.png'.format(epoch + 1), nrow=4, normalize=True)
                save_image(real, IMAGE_DIR + '/{}_real.png'.format(epoch + 1), nrow=4, normalize=True)

            if total_step % N_D_STEP == 0:
                fake_score = D(fake)
                G_loss = criterion(fake_score, torch.ones_like(fake_score))
                optim_G.zero_grad()
                G_loss.backward()
                optim_G.step()
                list_G_loss.append(G_loss.detach().cpu().item())

                if total_step % ITER_REPORT == 0:
                    print("Epoch: {}, D_loss: {:.{prec}} G_loss: {:.{prec}}"
                          .format(epoch, D_loss.detach().cpu().item(), G_loss.detach().cpu().item(), prec=4))

    torch.save(D.state_dict(), '{}_D.pt'.format(DATASET_NAME))
    torch.save(G.state_dict(), '{}_G.pt'.format(DATASET_NAME))

    plt.figure()
    plt.plot(range(0, len(list_D_loss)), list_D_loss, linestyle='--', color='r', label='Discriminator loss')
    plt.plot(range(0, len(list_G_loss) * N_D_STEP, N_D_STEP), list_G_loss, linestyle='--', color='g',
             label='Generator loss')
    plt.xlabel('Iteration')
    plt.ylabel('Loss')
    plt.legend()
    plt.savefig('Loss.png')

    print(time() - st)

The error seems to come from Discriminator .forward as follows:
RuntimeError Traceback (most recent call last)
in
71 fake = G(z)
72
?> 73 real_score = D(real)
74 fake_score = D(fake.detach())
75

C:\Anaconda3\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
?> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

in forward(self, x)
87
88 def forward(self, x):
?> 89 return self.model(x)

C:\Anaconda3\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
?> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

C:\Anaconda3\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
?> 92 input = module(input)
93 return input
94

C:\Anaconda3\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
?> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

C:\Anaconda3\lib\site-packages\torch\nn\modules\batchnorm.py in forward(self, input)
81 input, self.running_mean, self.running_var, self.weight, self.bias,
82 self.training or not self.track_running_stats,
?> 83 exponential_average_factor, self.eps)
84
85 def extra_repr(self):

C:\Anaconda3\lib\site-packages\torch\nn\functional.py in batch_norm(input, running_mean, running_var, weight, bias, training, momentum, eps)
1695 return torch.batch_norm(
1696 input, weight, bias, running_mean, running_var,
-> 1697 training, momentum, eps, torch.backends.cudnn.enabled
1698 )
1699

RuntimeError: running_mean should contain 256 elements not 128

Can anyone tell me what is this error about? It seems to come from size setting of something in the model, but that's all I can guess.
Thank you in advance.
Answer: The line
model += [nn.Conv2d(128, 256, 5, stride=2, padding=2, bias=False), norm(128), act]  # 8x8

it is a batch normalization input error it should be 256 instead.


Calculus 3 Integrals

Question: Find the volume of the solid bounded by $z=4x^2+4y^2, z=0, x^2+y^2=1$ and $x^2+y^2=2$.
What I know:
I know that when I draw the graph I will get two paraboloids giving me a radius of $1$ and $2$,$\theta= 2\pi$, and $z= 0$ to $4r^2$.
I keep getting a large number like $30\pi$ when the answer should $6\pi$.
Can anyone help me ?
Thanks
Comment: Use cylindrical coordinates, and the radius is $\sqrt2$ not $2$
Comment: Perhaps you can tell us how you get a large number like $30\pi?$
Comment: I got 30pi by having theta to be 0 to 2pi, radius: 1 to 2 and z: 0 to 4r^2 and integrating 1*r dzdrdθ
Comment: It can also be worked as a solid of revolution of a region in the $xz$ plane bounded by graphs of $z=0$, $z=4x^2$, $x=1$ and $x=\sqrt{2}$ using the cylindrical shell method. Gives answer of $V=6\pi$.
Answer: Find the volume of the solid of revolution formed by revolving about the $x$ axis the figure in the $xz$-plane bounded by the graphs of $z=0$, $z=4x^2$, $x=1$ and $x=\sqrt{2}$.
$$ V=\int_1^{\sqrt{2}}2\pi rh\,dx$$
where $r=x$ and $h=4x^2$.
$$ V=2\pi\int_1^{\sqrt{2}}4x^3\,dx=2\pi[x^4]_1^{\sqrt{2}}=6\pi $$

Calculus 3 Integrals


Showing the Pin Information by Default

Question: I am working with MKPinAnnotationView annotations pins over a MKMapView. Into the view that I am developing, there is a map centered on a pin. When I call the view the pin drops down and if I make a click over it, it shows the annotation information (canShowCallout = YES). How I can get this callout just when I open the view? Without making a click over the annotation pin.
Thanks for reading.
Edited:
I am using this code.
- (void)viewDidLoad {

    [super viewDidLoad];

    AddressAnnotation *annotation = [[[AddressAnnotation alloc] initWithCoordinate:self.currentAnnotationCoordinate] autorelease];
    [self.mapView addAnnotation:annotation];
    [self zoomCoordinate:self.currentAnnotationCoordinate];
}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {

    // If it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    else { // Handles the other annotations.
        // Try to dequeue an existing pin view first.
        static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

        if (!pinView) {
            // If an existing pin view was not available, creates one.
            MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
            customPinView.animatesDrop = YES;
            customPinView.canShowCallout = YES;

            // Adds a detail disclosure button.
            UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
            customPinView.rightCalloutAccessoryView = rightButton;

            return customPinView;
        } else
            pinView.annotation = annotation;
    }

    return nil;
}

- (void)mapViewDidFinishLoadingMap:(MKMapView *)theMapView {

    AddressAnnotation *annotation = [[[AddressAnnotation alloc] initWithCoordinate:self.currentAnnotationCoordinate] autorelease];

    for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
        if ([currentAnnotation isEqual:annotation]) {
            [mapView selectAnnotation:currentAnnotation animated:FALSE];
        }
    }
}

Edited:
Calling didAddAnnotationViews: instead of mapViewDidFinishLoadingMap: (as aBitObvious has commented), it works fine.
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {

    id<MKAnnotation> myAnnotation = [self.mapView.annotations objectAtIndex:0];
    [self.mapView selectAnnotation:myAnnotation animated:YES];
}

Comment: I recently answered a [question](http://stackoverflow.com/questions/4151094/getting-the-title-to-show-up-when-map-loads-with-mkmap/4151381#4151381) like this.  See if it works for you.
Comment: Thanks, it works great like this. The only shadow is that the pin shows the information before touching the map (I am animating the drop down). How can I do it after?
Comment: Use performSelector:withObject:afterDelay.  See [this answer](http://stackoverflow.com/questions/4083491/mapkit-how-to-detect-annotations-have-loaded/4083553#4083553).
Comment: Is there no way of knowing when the pin touches the map?
Comment: Not that I know of unless you're drawing the animation yourself.
Comment: OK. The only problem now is that I can not give you the green tick, because you only has posted comments. Anyway, thank you so much.
Answer: Possible duplicate: How to trigger MKAnnotationView's callout view without touching the pin?
You want to call [mapView selectAnnotation:].
Comment: Sorry for the duplicate question. I am calling the selectAnnotation: method after adding the annotation, into the viewDidLoad method, but I still need make clic over the pin to show the information.
Comment: Did you try calling selectAnnotation from (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView as the first answer suggests?

Showing the Pin Information by Default


Send mail using my own SMTP Server

Question: I have windows server 2019 configured in AWS . Installed SMTP server role.
Now , i have a simple mail client which i downloaded from below github link.
https://github.com/CodeCowboyOrg/SimpleSMTPClient.
Image shows that mail sent sucessfully
The sender and receiver mail ID is my personal gmail account. I am not receiving this mail in my gmail inbox.
Any suggestion why i am not receiving mail ?
I think my understanding about smtp work flow is not good.
Please help me to understand.
Comment: What does the mail log on your AWS SMTP server tell you?  If it shoes gmail as accepting the message, have you checked if gmail has classified it as spam?
Comment: i didn't receive this in SPAM folder also.
Answer: This is a complex subject not conducive to a single StackExchange post. Please do further research before asking a specific question. There is much more to do to have your mail accepted by commercial servers, especially Gmail and even then they are notoriously opaque about their criteria for spam.

Send mail using my own SMTP Server


LSF job states for a given job

Question: Let's say my job was running for some time and it went to suspend state due to machine overloading and became running after sometime and got completed.
Now the status acquired by this job were RUNNING -> SUSPEND -> RUNNING
How to get all states acquired by a given job ?
Answer: bjobs -l   If the job hasn't been cleaned from the system yet.
bhist -l   Otherwise. You might need -n, depending on how old the job is.
Here's an example of bhist -l output when a job was suspended and later resumed because the system load temporarily exceeded the configured threshold.
<code>$ bhist -l 1168

Job <1168>, User <mclosson>, Project <default>, Command <sleep 10000>
Fri Jan 20 15:08:40: Submitted from host <hostA>, to
                 Queue <normal>, CWD <$HOME>, Specified Hosts <hostA>;
Fri Jan 20 15:08:41: Dispatched 1 Task(s) on Host(s) <hostA>, Allocated 1 Slot(
                 s) on Host(s) <hostA>, Effective RES_REQ <select[type == any] or
                 der[r15s:pg] >;
Fri Jan 20 15:08:41: Starting (Pid 30234);
Fri Jan 20 15:08:41: Running with execution home </home/mclosson>, Execution CW
                 D </home/mclosson>, Execution Pid <30234>;
Fri Jan 20 16:19:22: Suspended:  Host load exceeded threshold:  1-minute CPU ru
                 n queue length (r1m)
Fri Jan 20 16:21:43: Running;

Summary of time in seconds spent in various states by  Fri Jan 20 16:22:09
  PEND     PSUSP    RUN      USUSP    SSUSP    UNKWN    TOTAL
  1        0        4267     0        141      0        4409
</code>
At 16:19:22 the jobs was suspended because r1m exceeded the threshold.  Later at 16:21:43 the job resumes.
Comment: Thank Michael but bhist -l gives current/final state of the running/completed job. I am not interested in current or final state of the job.
I am interested to know all the state acquired by a job.
Say I launch a job which is RUNNING for some time and then due to system threshold it went to SSUSP state and then came back to RUNNING after load was normal.

Now I am interested to know the all states and the amount of time for which the state was acquired. Is there a way to do that ?
Comment: `bhist -l` is what you want. I've updated my answer with an example.
Comment: Thanks Michael for clarification !

LSF job states for a given job


O que "re:" e "im:" significam no Rust?

Question: Quero saber oque re e im significam/fazem
let mut z = Complex { re: 0.0, im: 0,0 };

Estou aprendendo Rust pelo livro Programming Rust e isso de re: e im: já deve ter aparecido antes, mas só agora me dei ao trabalho de pesquisar.
O que achei antes de chegar aqui foi que im: é uma maneira para criar estruturas imutáveis de código (tradução livre de: immutable data structures), mas nada sobre o re, que chuto que pode ser reutilizável.
Outra coisa que achei do im: também foi na documentação do Rust onde dizia que era uma crate, então não tenho certeza se é outra coisa completamente diferente (essa é a única dependência listada no Cargo.toml "num = "0.1.27"")
Comment: Já estudou números complexos antes? Ele é composto por uma parte real (Re) e uma parte imaginária (Im).
Answer: Rust permite que você inicialize um objeto com uma forma de literal. Essa forma é composta pelo nome do tipo, de chaves que indicarão o início e fim desse literal, como se fosse as aspas de uma string, e depois você colocará os membros que deseja inicializar os dados do objeto.
Rust não tem construtor como outras linguagens, ele pode ter funções simples que servem como construtores, mas a forma mais utilizada é a inicialização pura e simples desta forma de literal.
Então é parecido com uma chamada de função com argumentos nomeados, só que o que está fazendo é usar os nomes dos membros da struct e os seus valores. Então o que está antes do : é o nome do membro e o o que está depois do : é o valor que será atribuído a esse membro.
Neste exemplo que é um tipo Complex, re é a parte real do número e im é a parte imaginária. Nada tem a ver com o que especulou na pergunta, é muito mais simples que isto. Esta informação tem a ver com o tipo de dados ali, o Complex e não com regras da linguagem. A linguagem só determina a sintaxe deste inicializador de objetos.
Outro exemplo hipotético:
let joao = Funcionario { nome : "João", salario : 1000 };

Coloquei no GitHub para referência futura.
Entendeu? São apenas membros da struct, e diga-se de passagem nomes mal escolhidos, na verdade tem cara de C e não de Rust que costuma abreviar muito.
Answer: Isso não tem relação alguma com imutável ou reutilizável.
Provavelmente em algum momento do livro foi definida uma estrutura semelhante a:
struct Complex {
    re: f64,
    im: f64
}

Que representa um número complexo. re representa a parte real, enquanto im representa a parte imaginária.
Ao fazer:
let mut z = Complex { re: 0.0, im: 0.0 };

Você basicamente está criando um objeto z, que segue a estrutura Complex, possuindo o valor zero tanto na parte real quanto na parte imaginária.
Você pode ler mais sobre struct em Using Structs to Structure Related Data.
Comment: Acho que entendi, obrigado.

O que "re:" e "im:" significam no Rust?


What test shall I use to validate the use of a certain score to predict my outcome in a survival analysis?

Question: I validate usage of a clinical cardiovascular score to predict the risk of dementia using data from a longitudinal study. Therefore, my outcome is binary (dementia yes or not) and the independent variable (the score) is continuous, of course I have a whole set of covariates.
I did Cox analysis to assess an association between baseline values and the outcome over time but now I would like to validate the use of the score.
I thought about taking a random sub-sample of my cohort to split in training and test and run some sort of validation statistics (i.e. ROC curves) but I have some concerns about this for a number of reasons:

My sample is relatively small ($n=2500$), and I am afraid that taking a sub-sample would reduce the power too much.
Not sure whether the ROC (or alternatively the somerset) are the best tests in this case, as other tests (like those used in screenings evaluation) may suit better.

How shall I evaluate the use of this score?
Can you suggest tests that suit better for the problem?
For data analysis I use Stata.
Answer: So you basically want to do a Cross Validation of your dataset.
One type of CV is the Holdout method which divides the data into two parts: Dtest and Dtrain. The model will predict values from Dtest (x → y) and since we know the actual values, that is what x-value correspond to what y-value we can compare the predicted and the actual values and estimate the performance. The subsample is quite arbitrarily Dtrain/Dtest: 70/30.
But your concern about splitting up your dataset into subset is valid. Why? Because when the data is split up into subparts with a dataset that is quite small, in your case N=2500, there's a much higher chance that Dtest and Dtrain is different from one another.
We can solve this by using a k-folded CV. K-folded cross validation divides the dataset into several parts. One of the subparts will be used as test data and the rest (k-1 parts) will be used as training data. The model is iterated (sort of) through each subpart and an error rate will be obtained for each iteration. The mean error rate will be used as a performance value. This solves the problem of having subparts of the data that is not representative of the whole dataset.
The problem with using k-folded cross validation is if the subparts are divided so as to give a model that overfits the data. That is, it predicts the value of the datapoints that are given quite well but when the model is given new data the error rate will be high. This usually happens when there's little data to begin with. To prevent this we can use Repeated cross validation.
Repeated cross validation:

Do CV (which will give mean error rate (Ê)
Reorder the data so as to give different subparts
Repeat 1-2

Figure 1. General idea behind CV.
Comment: I would recommend against repeated cross-validation. See: Vanwinckelen, G., & Blockeel, H. (2011). On estimating model accuracy with repeated cross-validation. *Proceedings of BeneLearn and PMLS 2012*, 39–44. Retrieved from https://lirias.kuleuven.be/bitstream/123456789/346385/3/OnEstimatingModelAccuracy.pdf
Comment: This raises some concern. I recently did a course (2015) in machine learning and design of experiments at Uppsala University which taught this as a methodology for validating models, specifically regression models. How recent is this article, are there several of them and is it about to get 'mainstream'?
Comment: It's from 2011. How mainstream those authors' opinion is is hard for me to say; I don't really have my finger on the pulse of the machine-learning community. But my perception is that repeated cross-validation is something people do because it seems intuitive rather because of empirical evidence or strong theory. In that case, Vanwinckelen and Blockeel are among the few who have thought carefully about this issue, so we should listen to them.
Answer: Resampling is a useful approach for validating and calibrating a survival model. Bootstrapping might be preferred to cross-validation, as bootstrapping directly mimics the process of taking multiple samples from the underlying population of interest. Repeated cross-validation could be considered, but that doesn't end up saving computational time. Frank Harrell notes that "To be as good as the bootstrap, about 100 repeats of 10-fold cross-validation are required."
The area under a receiver operating characteristic curve isn't a very sensitive measure of model performance. It only evaluates the rank orders of outcomes, not the precision of predictions. See this answer, for example. A more comprehensive validation and calibration of the model based on resampling is called for. Harrell's rms package in R provides several measures of Cox model quality in its validate() and calibrate() functions.


R Flexsurv and time-dependent covariates

Question: I read that the R flexsurv package can also be used for modeling time-dependent covariates according to Christopher Jackson (2016) ["flexsurv: a platform for parametric survival modeling in R, Journal of Statistical Software, 70 (1)].
However, I was not able to figure out how, even after several adjustments and searches in online forums.
Before turning to the estimation of time-dependent covariates I tried to create a simple model with only time-independent covariates to test whether I specified the Surv object correctly. Here is a small example.
library(splitstackshape)
library(flexsurv)

## create sample data
n=50
set.seed(2)
t <- rpois(n,15)+1
x <- rnorm(n,t,5)
df <- data.frame(t,x)
df$id <- 1:n
df$rep <- df$t-1

Which looks like this:
  t         x id rep
1 12 17.696149  1  11
2 12 20.358094  2  11
3 11  2.058789  3  10
4 16 26.156213  4  15
5 13  9.484278  5  12
6 15 15.790824  6  14
...

And the long data:
long.df <- expandRows(df, "rep")

rep.vec<-c()
for(i in 1:n){
  rep.vec <- c(rep.vec,1:(df[i,"t"]-1))
}

long.df$start <- rep.vec
long.df$stop  <- rep.vec +1
long.df$censrec <- 0
long.df$censrec<-ifelse(long.df$stop==long.df$t,1,long.df$censrec)

Which looks like this:
     t         x id start stop censrec
1    12 17.69615  1     1    2       0
1.1  12 17.69615  1     2    3       0
1.2  12 17.69615  1     3    4       0
1.3  12 17.69615  1     4    5       0
1.4  12 17.69615  1     5    6       0
1.5  12 17.69615  1     6    7       0
1.6  12 17.69615  1     7    8       0
1.7  12 17.69615  1     8    9       0
1.8  12 17.69615  1     9   10       0
1.9  12 17.69615  1    10   11       0
1.10 12 17.69615  1    11   12       1
2    12 20.35809  2     1    2       0

...

Now I can estimate a simple Cox model to see whether it works:
coxph(Surv(t)~x,data=df)

This yields:
    coef exp(coef) se(coef)     z     p
x -0.0588    0.9429   0.0260 -2.26 0.024

And in the long format:
coxph(Surv(start,stop,censrec)~x,data=long.df)

I get:
     coef exp(coef) se(coef)     z     p
x -0.0588    0.9429   0.0260 -2.26 0.024

Taken together I conclude that my transformation into the long format was correct. Now, turning to the flexsurv framework:
flexsurvreg(Surv(time=t)~x,data=df, dist="weibull")

yields:
Estimates:
       data mean  est       L95%      U95%      se        exp(est)  L95%      U95%
shape        NA    5.00086   4.05569   6.16631   0.53452        NA        NA        NA
scale        NA   13.17215  11.27876  15.38338   1.04293        NA        NA        NA
x      15.13380    0.01522   0.00567   0.02477   0.00487   1.01534   1.00569   1.02508

But
flexsurvreg(Surv(start,stop,censrec) ~ x ,data=long.df, dist="weibull")

causes an error:
Error in flexsurvreg(Surv(start, stop, censrec) ~ x, data = long.df, dist = "weibull") :
  Initial value for parameter 1 out of range

Would anyone happen to know the correct syntax for the latter Surv object? If you use the correct syntax, do you get the same estimates?
Thank you very much,
best,
David
===============
EDIT AFTER FEEDBACK FROM 42
===============
library(splitstackshape)
library(flexsurv)
x<-c(8.136527,  7.626712,  9.809122, 12.125973, 12.031536, 11.238394,  4.208863,  8.809854,  9.723636)
t<-c(2, 3, 13,  5,  7, 37 ,37,  9,  4)

df <- data.frame(t,x)

#transform into long format for time-dependent covariates
df$id <- 1:length(df$t)
df$rep <- df$t-1
long.df <- expandRows(df, "rep")

rep.vec<-c()
for(i in 1:length(df$t)){
    rep.vec <- c(rep.vec,1:(df[i,"t"]-1))
}

long.df$start <- rep.vec
long.df$stop  <- rep.vec +1
long.df$censrec <- 0
long.df$censrec<-ifelse(long.df$stop==long.df$t,1,long.df$censrec)

coxph(Surv(t)~x,data=df)
coxph(Surv(start,stop,censrec)~x,data=long.df)

flexsurvreg(Surv(time=t)~x,data=df, dist="weibull")
flexsurvreg(Surv(start,stop,censrec) ~ x ,data=long.df, dist="weibull",inits=c(shape=.1, scale=1))

Which yields the same estimates for both coxph models but
Call:
flexsurvreg(formula = Surv(time = t) ~ x, data = df, dist = "weibull")

Estimates:
       data mean  est       L95%      U95%      se        exp(est)  L95%      U95%
shape        NA     1.0783    0.6608    1.7594    0.2694        NA        NA        NA
scale        NA    27.7731    3.5548  216.9901   29.1309        NA        NA        NA
x        9.3012    -0.0813   -0.2922    0.1295    0.1076    0.9219    0.7466    1.1383

N = 9,  Events: 9,  Censored: 0
Total time at risk: 117
Log-likelihood = -31.77307, df = 3
AIC = 69.54614

and
Call:
flexsurvreg(formula = Surv(start, stop, censrec) ~ x, data = long.df,
    dist = "weibull", inits = c(shape = 0.1, scale = 1))

Estimates:
       data mean  est       L95%      U95%      se        exp(est)  L95%      U95%
shape        NA     0.8660    0.4054    1.8498    0.3353        NA        NA        NA
scale        NA    24.0596    1.7628  328.3853   32.0840        NA        NA        NA
x        8.4958    -0.0912   -0.3563    0.1739    0.1353    0.9128    0.7003    1.1899

N = 108,  Events: 9,  Censored: 99
Total time at risk: 108
Log-likelihood = -30.97986, df = 3
AIC = 67.95973

Comment: I'm having difficulty understanding your intent. Some comments about what is a final event, what is a censoring event and what are the covariates that vary from time period to time period would be helpful.
Comment: My apologies. With `t` I refer to the time until final event (e.g. machine breakdown). With `censrec` I denote whether at the end of an interval a breakdown has been observed. In this simple case it is 0 for all intervals with stop time before `t`. I excluded (for now) covariates that vary over time to test whether the model specification is correct (hence the time intervals are somewhat arbitrary). Without time varying covariates I'd expect both flexsurvreg models to have the same estimates. So my intent it to setup the model correctly before adding time varying covariates in a next step.
Answer: Reading the error message:

Error in flexsurvreg(Surv(start, stop, censrec) ~ x, data = long.df, dist = "weibull",  :
    initial values must be a numeric vector

And then reading the help page, ?flexsurvreg, it seemed as though an attempt at setting values for inits to a named numeric vector should be attempted:
flexsurvreg(Surv(start,stop,censrec) ~ x ,data=long.df, dist="weibull", inits=c(shape=.1, scale=1))
Call:
flexsurvreg(formula = Surv(start, stop, censrec) ~ x, data = long.df,
    dist = "weibull", inits = c(shape = 0.1, scale = 1))

Estimates:
       data mean  est       L95%      U95%      se        exp(est)  L95%      U95%
shape        NA    5.00082   4.05560   6.16633   0.53454        NA        NA        NA
scale        NA   13.17213  11.27871  15.38341   1.04294        NA        NA        NA
x      15.66145    0.01522   0.00567   0.02477   0.00487   1.01534   1.00569   1.02508

N = 715,  Events: 50,  Censored: 665
Total time at risk: 715
Log-likelihood = -131.5721, df = 3
AIC = 269.1443

Extremely similar results. My guess was basically a stab in the dark, so I have no guidance on how to make a choice if this had not succeeded other than to "expand the search."
Comment: Thank you very much! This does help indeed. When running this example with your edit I get the same results. However, when I use data based on a real distribution, the estimates differ:

`x<-c(8.136527,  7.626712,  9.809122, 12.125973, 12.031536, 11.238394,  4.208863,  8.809854,  9.723636)`
`t<-c(2, 3, 13,  5,  7, 37 ,37,  9,  4)`

I edited my former question by adding the complete code. Do you get the same differing estimates?
Answer: I just want to mention that in flexsurv v1.1.1, running this code:
flexsurvreg(Surv(start,stop,censrec) ~ x ,data=long.df, dist="weibull")

doesn't return any errors. It also gives the same estimates as the non time-varying command
flexsurvreg(Surv(time=t)~x,data=df, dist="weibull")


sprite feature in XTK

Question: Is it possible to add sprite feature in XTK ?
I need to render large number of point data. I was able to render it as X.displayable.types.POINTS. But it would be great if i can render the points with texture. I am looking for something similar to the one available with three.js
https://github.com/mrdoob/three.js/blob/master/src/objects/Sprite.js
Comment: I didn't try the Three.js sprites. Could you elaborate on a use case?
Answer: Look at the following example which is kind of a sprites approach. The difference is that still each object gets pushed separately to the shaders so that it is rather slow. This is due to XTK's background of being optimized for rendering large objects than a lot of objects.
http://jsfiddle.net/haehn/jjzuD/

sprite feature in XTK


How to understand which bass strings are best for my Yamaha RBX 170?

Question: I need to do my first strings change on my RBX 170 and I don't know which kind of strings I have to buy.
How can I understand which one are best for it?
Comment: For *you* to understand what's best, *you* need to try out several different sets. Reason - it's *your* best that counts. Someone else may say set xyz is best, but that's *their* opinion. It's what *you* play, how *you* play, how *you* like it set up, etc., etc.
Answer: Your bass has a 34" scale length, and shipped from the factory with roundwound strings fitted. If you like the feel of the strings, especially when the bass was new, then you simply need to find another set of long-scale roundwound strings.
The gauge of the strings fitted to four-string basses is most commonly 45-100 or 45-105. The numbers here reflect the diameter of the strings in thousandths of an inch. Manufacturers will report the scale-length their strings are made for.

In the above image, Rotosound report the length and gauge of their strings as an integral element of the string packet design. The numbers '66' and '88' are their own proprietary cataloguing/branding system. Under these numbers it says "long scale", which indicates they will fit a 34'' scale length instrument. The gauges of the individual strings are listed under the name of the strings.
There are numerous string manufacturers, with many different string types available. A good idea might be to work out which famous bassist you like the tone of, and then find out what strings they use. There isn't really a wrong answer here, other than that you might intensely dislike the feel and sound of a set of strings your bass hero swears by.
Comment: I understand. What sould I chose then, If I'm looking for a deep round tone? I mean, I saw different string thickness... Should I go for something thicker?
Comment: Maybe.  Also, note: thicker strings, other things being equal, make lower notes, so tuning them up to the same notes will require pulling them a little tighter, and you'll feel the additional tension in the strings when you play them.  You may or may not like that feeling.

You might also consider "flatwound" strings (also called just "flats"), instead of the roundwound strings that your bass probably came with.


My react intellisense doesn't work when react run

Question: My react snippets don't work when I run react. For example, when I press control+space,there is loading... instead of snippets.
I removed my vs code & all of its snippets then install them again for solving problem but it doesn't work.
Unfortunately, I still have problem ,
do you have any idea about my problem. What could I do to solve it?
Comment: Could you try this? -> https://stackoverflow.com/questions/44321000/visual-studio-code-user-snippets-not-working/46574024
Answer: There is a very nice extension for writing react and redux codes on vs Code, Go to your extensions in vsCode then search and install ES7 React/Redux/GraphQL/React-Native snippets . Cheers! you are good to go.
Comment: Doesnt work well for me.trigger on every third attempt(OS Ubuntu 18.04 && VSC).