Regras de Negócio no Banco de Dados - quais as vantagens e desvantagens?

Question: Talvez eu me integre a uma equipe para desenvolver um novo projeto. O líder já tomou algumas decisões. Entre elas, que "as regras de negócio estarão no banco de dados". Em "procedures"  e "views", "triggers", etc.
Ao tomar conhecimento disto, minha primeira reação foi negativa - estou acostumado com regras de negócio na camada de aplicação, e não me parece uma boa ideia que fiquem no banco de dados.
Porém, o fato é que eu realmente não sei. Tampouco tenho experiência com essa arquitetura. Por isso, pergunto: Quais as vantagens? Quais as desvantagens? É uma boa prática? É uma má ideia?
Comment: "Os dados pertencem à Organização, não à Aplicação." É bom ter isso em mente, além de todas as vantagens e desvantagens apontadas nas respostas. Se a empresa quiser fazer uso desses dados de uma forma **diferente** daquela que foi concebida (ex.: data mining, integração com sistemas externos, etc), o sistema será ou não um empecílio para isso? Se a resposta for "sim", temos um *lock-in* em potencial - o que é "bom" pros desenvolvedores mas ruim para o cliente.
Comment: Excelentes respostas propiciadas aqui abaixo! Será interessante, se de fato se integrar à equipe, publicar um feedback aqui da situação real (em termos conceituais em cima do que foi respondido, nada que fira a empresa, claro).
Comment: Outro usuário fez hoje mesmo uma pergunta relacionada que talvez lhe seja interessante: http://pt.stackoverflow.com/questions/15702/e-realmente-necessario-definir-constraints-no-banco-de-dados#comment28468_15702
Comment: Acho que isso depende muito do que é para fazer, pode ser ou não vantajoso... Acho que fica um bocado na opinião e sensibilidade de cada um.
Comment: @mgibsonbr editei a resposta p/ refletir o que você disse. Eu não acredito que existam muitos casos em que você esteja completamente livre do *lock-in*. Quando você escolhe um modelo, você se prende a ele e nunca é fácil sair dele. Claro que se você é dono do modelo, é um pouco mais fácil, mas não acho que faça muita diferença na prática estar no DB ou não, a propriedade sim. Na teoria o Windows tem desvantagens, e tem mesmo. Na prática as empresas não ligam para isso e se preocupam com coisas mais palpáveis. Falta espaço p/ explicar melhor :) Boa sugestão do brasofilo.
Comment: O tratamento de grande volume de dados por uma Stored Procedure pode ser mais perfomático do que resgatar estes dados através da rede para processá-los no aplicativo, simplesmente porque a rotina que processa está mais próxima dos dados.

Não há **nenhuma** outra vantagem. Todas as vantagens citadas por Cigano e bigown podem ser usufruidas também ao programar a regra de negócio no aplicativo. E mesmo esta que eu citei é bastante questionável pois há muitos modelos de arquitetura para resolver problemas de performance sem precisar implementar a regra de negócio no SGBD.
Answer: Vou tentar focar esta resposta no tocante a bancos de dados relacionais, que são os que tenho mais experiência e que devem abranger melhor o escopo da pergunta.
Vantagens

A segurança e a consistência dos dados são maiores;
O permissionamento é feito a nível de dados, possibilitando a outros sistemas acessar diretamente a base, o que é bastante positivo em termos de desempenho;
Como tabelas não são acessadas diretamente em termos de escrita (apenas em termos de leitura, quando isso acontece), a higienização de dados pode ser mais eficiente;
Defaults, Checks e Rules evitam ausência de dados, definindo dados padrão e evitando inserção ou atualização de informações fora do escopo do negócio;
Corretamente normalizado, é muito mais rápido em termos de leitura que qualquer ORM de qualquer linguagem de programação, pois não há a construção da consulta nem a transliteração dinâmica dos resultados.

Desvantagens

Mudanças são onerosas: quando é preciso modificar o esquema de alguma tabela ou chave, todos os objetos impactados precisam ser mudados manualmente;
Demanda cuidado extra quanto a tuning e desempenho, pois triggers afetam a performance das leituras e escritas;
Pode causar deadlocks e outros problemas específicos de bancos de dados, como geração anormal de informações por triggers;
Sendo exageradamente programado, o comportamento do sistema pode se tornar obscuro, visto que alguns programadores e analistas usam indiscriminadamente artefatos como Procedures e Triggers para realizar programação pesada. Por exemplo, já vi um sistema que executava um cálculo inteiro de Folha de Pagamento chamando Procedures em cadeia. Funcionava bem e rápido, mas a implementação de um algoritmo dessa complexidade inteiramente em banco de dados é preocupante e bastante difícil de conferir;
Pode se tornar um gargalo, se a tecnologia não suportar distribuição de carga. O caching fica inteiro no servidor de banco de dados;
Não funciona bem com um número simultâneo de conexões muito grande (na casa dos milhares ou até dos milhões, por exemplo); Há bloqueio de E/S.

É uma má prática?
Não. O conjunto de recursos que possui hoje um banco de dados foi inventado em uma época em que sistemas eram como cascas: apenas interfaces de acesso organizadas aos dados de um banco de dados. Um bom exemplo são as linguagens MUMPS, Cobol, Natural e SQLWindows, em que basicamente quase todas as regras de manipulação massiva de dados eram feitas diretamente na base.
Entretanto, é preciso saber que esta é uma abordagem antiga e conservadora, portanto, pouco focada em produtividade. A manutenção e depuração é muito mais complexa do que numa camada de aplicação de um sistema moderno. Pode se tornar uma má prática se extrapolar o propósito do banco de dados, que é armazenar e manter responsavelmente as informações.
Answer: Vantagens

Fica tudo em um único local. Fica mais fácil manter.
Pode haver ganhos de performance. O softwares são otimizados para isso.
Serve diversas aplicações usando diversas tecnologias e linguagens sem problemas. Permite até o acesso direto.
Facilita o desenvolvimentos das aplicações por não precisar lidar com essa lógica.
Permite um controle de acesso mais efetivo e granular ao banco.

Banco de dados é ótimo em armazenar e recuperar informações de diversas formas. Quando você quer fazer muito mais que isso, está usando a ferramenta errada. Banco de dados é um mecanismo, e ele é ótimo nisso. Ele não funciona muito bem tentando fornecer abstrações. Quando o mecanismo é mais importante, você faz nele.
Existe aplicação para tudo. Quando o banco de dados é mais importante que a aplicação, é possível fazer a lógica funcionar melhor dentro do DB. Um Data Warehouse, por exemplo se beneficia disto. Se o banco de dados define bem a solução e as aplicações rodando em cima dele são apenas satélites. Se, de certa forma, o banco de dados se torna a aplicação, faz sentido usar mais recursos dele.
Regras de negócios e regras de dados são coisas diferentes. Regras de dados cabem bem dentro do banco de dados, sempre.
Eu sei de outros itens que parecem vantagens, mas que há uma solução tão boa ou melhor colocando a lógica na aplicação. Só precisa entender como. Não vou colocar como vantagem coisas que só são vantagens se for feito do jeito certo e quando fizer do jeito certo na camada da aplicação, lá também é vantajoso. Por exemplo, se você souber o que está fazendo no DB ou na aplicação, os dados podem ficar consistentes. Se não souber, você vai ter problemas fazendo em qualquer um deles. Por isso que usar aquilo que você (ou sua equipe) sabe o que está fazendo é mais importante que a maneira de fazer. A resposta do Cigano Morrison Mendez mostra isso. A primeira vantagem que ele cita só é real se não acontecerem outros problemas, especialmente, mas não só, os citados por ele no primeiro e terceiro item das desvantagens.
Desvantagens

Praticamente exige um DBA para otimizar o banco de dados.
Os desenvolvedores precisarão conhecer bem a linguagem do banco de dados. É raro encontrar profissionais com conhecimento e experiência além do trivial. É fácil achar quem ache que tem ambos.
SQL, mesmo amplificada, não é muito expressiva. Costuma ser menos produtivo programar em qualquer variação disponível. Os códigos são bem feios e desorganizados.
Programadores não entendem como fazer interfaces do banco de dados para aplicações adequadamente.
O banco de dados recebem uma carga extra de trabalho que pode precipitar a necessidade de escalonamento. E é mais difícil escalar o DB que a aplicação.
Você fica preso ao fornecedor de banco de dados ou tem trabalho para replicar a lógica para outros fornecedores, perdendo o primeiro item de vantagem.
As aplicações ficam dependentes do banco de dados para conseguir fazer diversas manutenções.
É ilusão achar que dá para colocar toda lógica no DB, então na prática acaba ficando com uma solução híbrida.
Versionamento é bem mais complicado.
Testes são mais complicados.
Depuração é mais complicada.
Documentação não é tão simples.
Manter sincronismo do desenvolvimento entre a equipe é complicado.
Não existem ferramentas tão boas para lidar com código em DB.
Dificulta o reuso de código. Cria acoplamento excessivo.
Uma alteração em um campo pode gerar uma quantidade enorme de alterações em cascata em outras partes do banco.
Os programadores odeiam isso (opinião, ok, mas isso deve ser considerado).

Existem diversas formas de colocar a lógica na aplicação. Usar um ORM é apenas uma delas. O uso do ORM muitas vezes acaba sendo justificativa para colocar a lógica no DB, afinal é comum os ORMs serem complexos e inflexíveis.
Uma aplicação pode fazer a maior parte da lógica de negócios e deixar para o banco o que ele pode fazer melhor que a aplicação. Cuidado com a otimização prematura. Em banco de dados isso pode produzir resultados contrários mais facilmente. Se escolher colocar as regras de negócio somente como otimização pode estar criando mais uma desvantagem.
Se uma aplicação apenas usa o banco de dados como um mecanismo para armazenar dados e é ela é que é importante na solução, é claro que colocar mais do que é necessário no DB é ruim.
Minha experiência
Particularmente eu costumo usar a lógica na aplicação, isto me serve bem, sai mais barato pra mim. Eu trabalho com softwares que são produtos. Então:

Eu preciso ter mais controle sobre a aplicação.
Eu preciso suportar fornecedores de banco de dados diversos.
Eu sei que minha aplicação será a única a acessar o DB. Eu forneço uma API em um servidor de aplicação para outras aplicações acessarem o DB indiretamente.
Eu preciso me livrar de todas as desvantagens que isso traz mais do que as vantagens que ela fornece.
Toda a aplicação, incluindo a modelagem do banco de dados, é minha e não do cliente, só os dados são dele.
Existe uma cultura estabelecida. Isso é importante para tomar decisões. Não é só o técnico que conta. Melhor usar o que você tem expertise do que se aventurar em algo ótimo que você não domina. A não ser que a vantagem seja brutal.

Por que estou dizendo isso? Para você entender minha situação e julgar se estou sendo tendencioso. As vantagens descritas existem mesmo e é difícil quem consiga listar vantagens genuinamente diferentes das apresentadas, elas podem ser detalhadas ou especificadas melhor, mas dificilmente encontrará algo diferente.
Seu caso
Primeiro te digo para aproveitar a experiência. Tente manter a cabeça aberta e tente aprender uma forma de desenvolver que não está acostumado. Tentar mudar o rumo do que já foi decidido provavelmente só vai lhe trazer problemas e vai desperdiçar uma oportunidade de se desenvolver melhor.
Tente usar alguns dos itens que está lendo nas respostas aqui para tentar avaliar se tudo está correto no projeto. Mas perceba que cada projeto é diferente. Nem todas as vantagens e desvantagens serão encontradas em todos os projetos.
Seria terrível se você fosse o responsável pelo projeto e estivesse fazendo algo que não domina.
A maior pergunta que poderia fazer é: por que o projeto será centrado no DB? Se a decisão foi tomada com critérios adequados, se ela foi pensada, se foi levado em consideração todos os pontos que poderiam trazer vantagens ou desvantagens, provavelmente ele está no caminho certo.
O projeto é apenas um sistema interno? Qual é o background da equipe? Como será a evolução desta solução?
Boa prática é fazer o correto em cada situação, você descobrirá se é o caso neste projeto com o tempo.
Conclusão
Basicamente se você preguntar para um programador (de verdade) ele vai dizer que é melhor colocar na aplicação. Se perguntar para o DBA (mesmo aquele que também programa), ele vai dizer que é melhor colocar no DB. Na maioria das vezes a decisão é tomada com base em experiências prévias ou ideias preconcebidas do que é o ideal. Você escolhe com quais problemas você quer lidar. Pode parecer que isso é ruim, mas não necessariamente. Surpreendentemente as pessoas decidem sem sequer saber porque.
Bom senso e experiência sempre contam mais que qualquer recomendação genérica. O problema é que cada um tem seu próprio bom senso e a experiência é influenciada por diversos fatores.
Coloquei no GitHub para referência futura.
Comment: "Bom senso e experiência sempre contam mais que qualquer recomendação genérica. O problema é que cada um tem seu próprio bom senso e a experiência é influenciada por diversos fatores." Gostei dessa conclusão +1
Comment: No caso da lógica ser implementada no banco de dados, é possível implementar regras de acesso a informação, ou seja, determinar o que o utilizador pode ou não ver? Se sim, seria uma boa prática, uma vez que o propósito de um banco de dados é armazenar informação?
Comment: @FilipeMoraes é possível sim, e normalmente isto é feito.
Comment: O que seriam "regras de dados" e como se diferenciam das regras de negócio?


Bool array problems in c++

Question: Is an array of bools also "optimized", like a vector<bool> is? I want to make arrays of true or false, but I also dont want the problems that some with vector<bool> to show up in an array, such as slow access times
Comment: I think we can say one thing for sure: At the basic design stage, **do not worry** about how an array of bools is laid out. It's not even guaranteed yet that bool access will be a bigger bottleneck than your big network coding bug ;-)
Comment: what exactly do you need? memory efficient storage? fast? sparse? growable? want to access it like it were an array, etcetc..
Comment: The good way of understanding it - is by sitting in front of your computer with a debuger.
Comment: i want to access individual values in a 2d array quickly
Comment: "2d array"? What has this to do with your original question??
Comment: forget it. im going to change the question slightly
Comment: Why don't you describe the actual time-critical operation that you are performing. Then we can see which data type would be suited to that task.
Comment: im not sure of how im implementing my program yet. im still on the basic designing stage
Answer: bool[N] will occupy N times sizeof(bool) contiguous bytes in memory.
Comment: This will use 1 byte per bool element right? I would also like to know how to make it use only a bit for each but i guess this is not easily implemented right?
Comment: @GeoPapas: Such an optimization has been implemented for you in `std::vector`, written by professionals.
Comment: Thanks for your reply, I would like to implement a fast bool array for use in cuda. Would this std::vector implementation be suficient and fast? From what i ve heard this is only for minimal space management but not for optimal speed. Any suggestions? Thanks in advance!
Comment: Hmm i found out that it says: `Simultaneous access to different elements is not guaranteed to be thread-safe (as storage bytes may be shared by multiple bits).` Maybe that is not suitable for cuda.
Comment: @GeoPapas: That's a complex question with lots of details and trade-offs. `std::vector` doesn't guarantee any specific implementation, and in any case it'd be difficult to use with CUDA memory. Maybe Thrust has a packed bitset? But as you said, sharing can be a terrible, terrible performance problem.
Answer: Optimized for speed is one bool per word so it doesn't need to do masking and read-modify-write operations.  Optimized for space would be to pack 32 bools per word, so you have to be more specific about what "optimized" means.
Comment: Experienced C++ people will recognize what you mean when you say 'optimized' in scare quotes, but it's much preferable to be explicit. As best I can tell, you are asking whether any aggregation of `bool`s gets packed, or just `vector`. You should say so directly, and not hint at it.
Comment: optimized as in how `vector` fails
Comment: upvoted it, seems perfectly fine to me. calccrypto, have you downvoted this?
Answer: I think the C++ default implementation is mainly for saving the space, while the access time may be affected.
if you need quicker access time, you may have implement it by yourself and sacrifice the space.
Comment: Note, however, that in many cases the effect on access time is positive. The smaller representation lets more fit in a cache.
Comment: Err, No, Kerrek SB's answer is correct. An *array* must occupy `N * sizeof(bool)` memory which implies *zero* space optimization.

Bool array problems in c++


Source to "Strongly zero-dimensional and zero-dimensional is equivalent for separable metrizable spaces"?

Question: What is the source of the claim that "Strongly zero-dimensional and zero-dimensional is equivalent for separable metrizable spaces"? My university lecture told me that this is true, but I fail to find it anywhere in literature. Could you provide a source for this?
Definitions
A Hausdorff topological space $X$ is zero dimensional if for every point $x$ of $X$
and every neighborhood $U$ of $x$ in $X$, there exists a nonempty clopen subset $V$ of $X$ such that $x \in V \subset U$.
The clopen basis of any zero-dimensional space is a collection of clopen sets that is closed under complements and finite intersections.
A Hausdorff topological space $X$ is said to be strongly zero-dimensional whenever for every closed subset $A$ of $X$ and every open subset $U$ of $X$ such that $A \subseteq U$, there exists a clopen subset $V$ of $X$ such that $A \subseteq V \subseteq U.$
Comment: If $\text{ind}$ and $\text{Ind}$ denote the small and large inductive dimensions, then $\text{ind} = \text{Ind}$ for separable metric spaces. Zero dimensional space is zero-dimensional with respect to $\text{ind}$, while strongly zero-dimensional, with respect to $\text{Ind}$ (or equivalently Lebesgue covering dimension)
Answer: What you call "strongly zero-dimensional" is usually called ultranormal. In this common notation a Tychonoff space is called strongly zero-dimensional, if its Stone-Cech compactification is zero-dimensional. A space is ultranormal, iff it is normal and strongly zero-dimensional. In particular, for metric spaces, ultranormality is the same as strong zero-dimensionality. See, for instance here.
In the paper cited in the above link, you also find a proof for "Every Lindelof zero-dimensional space is ultraparacompact" (hence ultranormal), thus answering your question, since a separable metric space is, of course, Lindelof. See also Engelking, General topology 6.2.7.
Comment: @TerezaTizkova: According to *your* defintion, you are not asking for strong zero-dimensionality, but for ultranormality. Of course, a (regular) Lindelof space is normal, hence a Lindelof zero-dimensional space is ultranormal, or, according to your definition "strongly zero-dimensional". But for non-Lindelof, non-metric spaces, strong zero-dimensionality is strictly weaker than ultranormailty.
Comment: Thank you! But isn´t it enough to say 1) Every metric & separable space is Lindelof and 2) Every Lindelof zero-dimensional space is strongly zero dimensional and from 1) and 2) together conclude the result? I don´t see how the ultranormality etc. is needed.
Answer: It´s easy to see that every strongly zero-dimensional space is a zero-dimensional space, but conversely it is not necessarily true.
Theorem: Every zero-dimensional Lindelöf space is strongly zero-dimensional. You can see this result in Engelking, in the theorems 6.2.2 to 6.2.7.
Answering your question for metrizable spaces. I think you should ask for the space to be metrizable and separable, in order to have a Lindelöf space and use the previous result. I do not have at the moment, a counterexample on a zero-dimensional, metrizable and non-separable space that is not strongly zero-dimensional
Comment: Just curious: why did you (almost) copy my answer (or parts of it)?


opencart update cart total ajax

Question: I am trying to implement the cart in the header to update it's price / items when I add an item to the cart
My html:

01234 567890

<a href="<?php echo $shopping_cart; ?>">Basket

  <?php
  $cart_number = $this->cart->countProducts();
  if ($cart_number == 1) {
   echo "1 item";
  } else {
   echo $cart_number." items";
  }
  ?>

  <?php echo $this->currency->format($this->cart->getTotal()); ?>

It's a custom theme but with most elements copied from the default theme, but for some reason it doesn't update the cart items / total
any help would be appreciated
Comment: What does it do instead of the desired behaviour?
Comment: nothing, it displays the success box with the added to cart message but it doesn't update the cart total
Answer: You have to refresh #cart div at the end
add line
$('#cart').load('index.php?route=module/cart #cart > *');

after
$('html, body').animate({ scrollTop: 0 }, 'slow');

opencart update cart total ajax


Do the circumferences of spheres scale linearly w.r.t. their radii in normed vector spaces?

Question: Let $(V, ||\cdot ||)$ be a finite-dimensional normed vector space. Let $S(r) = \{ v \in V: ||v|| = r \}$ be the sphere of radius $r$ in $V$. Assume, if necessary*, that the length/measure of $S(1)$ is finite. In other words, that the circumference of the unit sphere is finite.

Question: Then do we have, for any choice of norm $|| \cdot ||$ on $V$, that for all $r > 0$: $$S(r) = r \cdot S(1) \,\,? $$
In other words, do the circumferences of spheres in any finite-dimensional normed space scale linearly with respect to their radii?

A pointer to a reference, or a yes/no will suffice for an answer -- I just want to know whether the result is correct before spending time actually trying to prove it.
Note: In order for the notion of circumference of a sphere to be well-defined, presumably we would need the notion of length to be well-defined first. However, I am not sure if every finite-dimensional vector space can be construed as a length space -- completeness is necessary for being a geodesic space, but not for being a length space. If necessary, assume that the ground field is $\mathbb{R}$.
My gut instinct is that the answer to this question is yes, and that this follows from the absolute homogeneity of any norm (i.e. $||rv|| = |r| ||v||$). However, I am not sure how to set up/write the integral expressing the circumference of the unit sphere in an arbitrary normed vector space. Having such an expression, it would be easier to see whether or not absolute homogeneity plus linearity of integrals automatically implies the result or not.
In particular, I am not sure how to find a continuous map $\gamma: [0,1] \to V$ such that the trace/image of $\gamma$ is the unit sphere for any given norm $|| \cdot ||$. (In order to set up an integral to calculate the length of the sphere, i.e. its circumference.) Presumably it would then be easy to modify such a map such that the trace is $S(r)$ for any $r > 0$, and this would then tell me immediately, using linearity of integrals and absolute homogeneity of norms, whether the conjecture is correct or not.
*I assume that this assumption is probably unnecessary, since all norms on finite-dimensional vector spaces are topologically equivalent, so if the circumference of one's unit sphere is finite, then perhaps this is true for all possible norms.  On the other hand, boundedness of a set is not necessarily preserved under homeomorphisms (i.e. "boundedness is not a topological property") so it seems like I would need additional assumptions for this to be correct, and I am not positive that the axioms of a normed vector space are sufficient enough additional assumptions.  Never mind, this probably all follows from the fact that the unit sphere/ball in any finite-dimensional normed vector space is compact, which is a topological property, and since all norms are topologically equivalent, if compactness for one norm implies boundedness, then presumably compactness for any norm implies boundedness. Still, this is somewhat conjectural on my part.
Answer: For every continuous curve $\gamma\colon [a,b]\to X$ with values in a metric space $X$, one can define the length of $\gamma$ as
$$L(\gamma)=\sup\sum_{j=1}^n d(\gamma(t_j), \gamma(t_{j-1}))$$
where the supremum is taken over all partitions $a=t_0<t_1 < \dots<t_n=b$ (and may be infinite). In particular, this makes sense for normed spaces over $\mathbb R$ or $\mathbb C$, whether they are complete or not.
For a sphere $S(r)$ in a normed space, it is natural to consider the quantity $\delta(x, -x)=\inf_\gamma\{L(\gamma)\}$ where the infimum is taken over all curves connecting $x\in S(r)$ to $-x$ and lying on the sphere $S(r)$. This is   the intrinsic distance from $x$ to its antipode. There are two interesting things to do with this distance: maximize or minimize it. J. J. Schäffer wrote a book (Geometry of spheres in normed spaces, Marcel Dekker, New York, 1976) which is still the primary reference for the subject. He called
$2\inf_{x\in S(r)} \delta(x,-x)$ the girth of the sphere, and $2\sup_{x\in S(r)} \delta(x,-x)$ its perimeter. The appearance of $2$ accounts for the fact that a shortest curve from $x$ to $-x$ is one half of a closed curve that comes back to $x$ after visiting $-x$.
Both of these quantities scale linearly with $r$, because the length of each curve is multiplied by $r$ under the map $x\mapsto rx$.
The Wikipedia article Girth (functional analysis) mentions a remarkable property of the girth of the unit sphere of a normed space $V$: it is the same for $V$ as for its dual $V^*$. This was proved by J. C.  Álvarez Paiva not so long ago (2006).
Comment: Wow thank you so much! I had no idea that there were whole books written about things like this. Thank you so much for sharing this!


How to compare predictive power of sports odds

Question: For my MSc Thesis I would like to compare the predictive power of classic bookmakers on the one hand and a betting exchange on the other. I have a lot of data on both types of betting, mostly on soccer matches but I have data on other sports as well. I want to see from the implied probability in the odds if the betting exchange performs better.
I read about ROC-curve and Area under the curve as a measure but I am not sure if I can use this method in my case? Or do you know a better way see which odds predict the outcome of matches significantly better?
Thanks in advance
Answer: The ROC is related to what you're interested in, but not quite it. We're interested in the performance of probability estimates, rather than binary classification thresholds.
In general, methods of evaluating probability estimates are called scoring rules. For every event, you can calculate the implied probability of the event that actually happened, take the log of that probability, and then sum them, ending up with a total score for each oddsmaker.
This gives you a sense of who is better at estimating probabilities overall, but is a single opaque number. For more clarity, let's investigate the two different things that we typically want to estimate: 1) calibration and 2) skill.
Calibration is easier to talk about: it's whether or not I can communicate my belief correctly to probabilities. If I think that team X is 70% likely to win a match, is it the case that across all times I predict a victory with 70% probability that team wins roughly 70% of the time?
We can measure calibration by binning the predictions into the various odds, calculating the actual rate, and then plotting. (You'll also want to do this with a confidence interval; if I only predict 80% twice, and get it right once and wrong once, that doesn't mean much, even though 50% is very different from 80%.)
Skill is how good I am at making right beliefs in the first place. If I only give predictions of "95%" and "5%", and am right 70% of the time in each case, I'm very poorly calibrated (I can't tell the difference between "only somewhat sure" and "very sure.") and would be better off reporting "70%" and "30%." But being right 70% of the time is my skill, and that may be very good (especially compared to chance, which would be right only 50% of the time).
(For further reading, you might be interested in this paper on estimating the calibration and skill of weather predictions.)


Understanding the base case for the following formula: $\varphi(n):= n \geq 3 \rightarrow (n!)^2 \gt n^n$

Question: In my book The Foundations of Mathematics by Kenneth Kunen, the following passage is stated:

To prove $\forall n \in \omega \ \varphi(n)$, it is sufficient to prove $\varphi(0)$ (the basis) and $\forall n \in \omega [\varphi(n) \rightarrow \varphi(S(n))]$ (the induction step); then, apply the Principle of Ordinary Induction to $X:=\{n \in \omega: \varphi(n)\}$

where the Principle of Ordinary Induction is the theorem:

For any set $X$: If $\emptyset \in X$ and $\forall y \in X (S(y) \in X)$, then $X$ contains all natural numbers.

I wondered how the above descriptions come into play for a $\varphi(n)$ of the following form:
$\varphi(n):= n \geq 3 \rightarrow (n!)^2 \gt n^n$...i.e. when proving the statement: $\forall n \in \omega [n \geq 3 \rightarrow (n!)^2 \gt n^n]$
In more informal settings, I often read analogies of the "domino effect", which tries to illustrate "what happens" after proving of the base case and inductive case $\forall n \in \omega [\varphi(n) \rightarrow \varphi(S(n))]$.
However in the statement $\forall n \in \omega [ n \geq 3 \rightarrow (n!)^2 \gt n^n]$, the first three natural numbers $0,1,$ and $2$ are are vacuously true.
Said differently, the first three numbers are, in essence, true for a different reason (as compared to numbers like $3, 4, 5, ...$ etc).
So my question is: What should be my base case for a statement like $\forall n \in \omega [n \geq 3 \rightarrow (n!)^2 \gt n^n]$?
Should it be $\varphi(0)$? Or should it be $\varphi(3)$?
I am tempted to think that the base case should be $\varphi(3)$. And then after my inductive step, I essentially backfill $\varphi(0), \varphi(1),$ and $\varphi(2)$ as being true because of their vacuous qualification. In this fashion, my set $\{n \in \omega : \varphi(n)\}$ would still qualify for the Principle of Ordinary Induction and thus conclude $\omega = \{n \in \omega : \varphi(n)\}$.
Is that the right idea?
Comment: The base case should be $\varphi(3)$.
Answer: To show $\forall n\ in \omega$, the base case has to be $n=0$, at least formally. However, the proof of $\phi(n)\to \phi(n+1)$ can be made in form of a case distinction, one trivial for small $n$, one explicit check for when $n+1$ is the wannabe base case, and one normal induction step for larger $n$. But in effect, this works exactly as if we consider $n=3$ the base case right away. It would therefore be overly formal to use the full method. But it is perhaps instructive to try that once, but not more than once.
Comment: Just to understand then...there are three induction cases. The first is for $0 \leq n \lt 2$, the second is for $n=2$, and the last is for $n \geq 3$. Is that correct?


How to approach missing data...ignore or not to ignore?

Question: What would be the best option when you come across missing data. Do you exclude that person completely from the data analysis or not. If not then how do you go about it?
i have data on two groups at three different time points. At each time point they had to undertake a 6 minute walking test. Now some participants they stopped at stage 4 (because physically they could not go any longer) and others managed to complete all of the stages (1-6th stage)..some managed to complete only 2 stages and so on.
Thank you in advance
Comment: This sounds like a job for [survival analysis](https://en.wikipedia.org/wiki/Survival_analysis), if only to determine whether observations are missing at random...but I doubt they will be. This could get tricky. I wonder if you could use (stage number of) inability to continue as an indicator of fitness...
Comment: Could you please add the research question to your (interesting) setting?
Comment: @MichaelMayer ...research question is: will there be a significant improvement in fitness in group A vs. group B when measuring fitness at three time point using 6 minute walking test
Comment: @NickStauner i could indeed use number of stages achieved before, after and at 3 months follow-up to indicate their fitness..thats a better way of looking at it
Comment: Doesn't solve your missing data problem for the rest of your analyses though. I linked to a web resource that might be helpful for that in a recent answer to "[Logistic regression with an independent variable that only applies to a subset](http://stats.stackexchange.com/q/97597/32036)".
Comment: I don't have time to give a full answer, but this is a job for Inverse Probability of Censoring Weights. At any rate, thank you thank you thank you for even thinking to ask this question. It's a pain to deal with but it's a fact of life.
Answer: The question posed is interesting, although it is hard to give an exact answer.
I'll go through some of the common concerns below.
When do missing data pose a problem?
In general, missing data poses a problem when it is not at least missing-at-random (MAR). Such data may lead to severely biased estimates of population quantities. In your case, the persons who did not show up for the follow-up tests may differ from those who did in important ways.
However, it is important to note that missing data (esp. in the form of "Don't know"-answers, common in attitude surveys) is not intrinsically uninformative. Schumann and Presser (1996, ch. 4) has a good walk-through of what one must consider when dealing with "don't know"-answers.
What can be done about missing data?
There are two main approaches to missing data: deletion and imputation. I'll summarize them briefly below.
Case-wise deletion is the omission of observations which are missing in one or more of the variables of interest. It is my belief that this is the standard in SPSS. Pair-wise deletion, on the other hand, computes the relevant statistics on pairs of variables. If you were interested in the correlations between $X,Y,Z$ and you had an observation missing in $Z$, but valid in $Y,X$, case-wise deletion would drop if from the analysis entirely, while pair-wise deletion would only drop it from the analysis of $corr(X,Z),corr(Y,Z)$.
Both deletion methods have their weakness: case-wise deletion often leads to a low sample size (and thus, low power) while pair-wise deletion obscures the size of the sample and - in my opinion - complicates inference about the data generating mechanism.
Imputation refers to the attempt to impute missing values. Once, the standard approach was so-called mean imputation, where one substitutes the mean of the valid observations in the missing observations. Newer development has lead to more efficient and useful techniques, which Wiki has a good summary of.
Answer: If your dependent variable is number of stages walked, the missing data problem disappears.
The analysis could then be a multilevel ordinal logistic model. Unfortunately, the two programs you list in your tags SPSS and Stata are two that I am not familiar with, even to knowing whether this type of analysis is possible in those programs. It can be done in R with the nlme package and in SAS with PROC GLIMMIX.
If your dependent variable is some score measured at each time point (such as heart rate) then you may have a big problem because the data may be severely missing not at random. It depends on whether the differences between the people who stop at an early stage and those who finish can be accounted for by other variables in your data set (that is, are they missing at random)? This is not a fully testable assumption. If you have a lot of measures on the subjects (age, weight, BMI, resting heart rate, exercise pattern etc) that are related to the number of stages completed, it may be reasonable.


prove the inequality $\left({1-x\over1+x}\right)^{1/2}\lt{\ln(1+x)\over \arcsin(x)}\lt1$ for $x\in (0,1)$

Question: $$\left({1-x\over1+x}\right)^{1/2}\lt{\ln(1+x)\over \arcsin(x)}\lt1$$
for $x\in(0,1)$
My attempt:
For the upper bound, I took the derivative of ${\ln(1+x)\over \arcsin(x)}$ and found out its a decreasing function over $(0,1)$. So I can found the upper bound less than 1 by taking the limit as $x\to0$.
$$\lim_{x\to0}{\ln(1+x)\over \arcsin(x)}=\lim_{x\to0}\left({1-x\over1+x}\right)^{1/2}=1$$
The lower bound, in this case, should be $\lim_{x\to1}{\ln(1+x)\over \arcsin(x)}={2\ln2\over\pi}$. Instead the lower bound is $\left({1-x\over1+x}\right)^{1/2}$. How could I prove that? Noted that the term $\left({1-x\over1+x}\right)^{1/2}$ does appear when I was calculating the limit for the upper bound.
Answer: The right inequality.
Let $f(x)=\arcsin{x}-\ln(1+x).$
Thus, $$f'(x)=\frac{1}{\sqrt{1-x^2}}-\frac{1}{1+x}=\frac{\sqrt{1+x}-\sqrt{1-x}}{(1+x)\sqrt{1-x}}=\frac{2x}{(1+x)\sqrt{1-x}(\sqrt{1+x}+\sqrt{1-x})}>0.$$
Thus, $$f(x)>\lim_{x\rightarrow0^+}f(x)=0.$$
The left inequality.
Let $g(x)=\ln(1+x)-\sqrt{\frac{1-x}{1+x}}\arcsin{x}.$
Thus, $$g'(x)=\frac{1}{1+x}-\left(\sqrt{\frac{1-x}{1+x}}\right)'\arcsin{x}-\sqrt{\frac{1-x}{1+x}}\cdot\frac{1}{\sqrt{1-x^2}}=$$
$$=-\left(\sqrt{\frac{2}{1+x}-1}\right)'\arcsin{x}>0.$$
Id est, $$g(x)>\lim_{x\rightarrow0^+}g(x)=0.$$
Comment: (+1) the cancellation of $\frac1{1+x}$ in the second derivation worked out nicely.
Comment: I think the fact that $\frac{1-x}{1+x}$ is decreasing is pretty obvious, just as much as $\frac2{1+x}-1$, but that's up to the beholder.
Answer: For $0\lt x\lt1$,
$$
\log(1+x)=\overbrace{\int_0^x\frac{\mathrm{d}t}{1+t}\le\int_0^x\,\mathrm{d}t}^{\normalsize t\ge0}=x\tag1
$$
and
$$
\arcsin(x)=\overbrace{\int_0^x\frac{\mathrm{d}t}{\sqrt{1-t^2}}\ge\int_0^x\,\mathrm{d}t}^{\normalsize t\ge0}=x\tag2
$$
Thus, we get
$$
\frac{\log(1+x)}{\arcsin(x)}\le\frac xx=1\tag3
$$
Furthermore, for $0\lt x\lt1$,
$$
\log(1+x)=\overbrace{\int_0^x\frac{\mathrm{d}t}{1+t}\ge\int_0^x\frac{\mathrm{d}t}{1+x}}^{\normalsize t\le x}=\frac{x}{1+x}\tag4
$$
and
$$
\arcsin(x)=\overbrace{\int_0^x\frac{\mathrm{d}t}{\sqrt{1-t^2}}\le\int_0^x\frac{\mathrm{d}t}{\sqrt{1-x^2}}}^{\normalsize t\le x}=\frac{x}{\sqrt{1-x^2}}\tag5
$$
Thus, we see that
$$
\frac{\log(1+x)}{\arcsin(x)}\ge\frac{\sqrt{1-x^2}}{1+x}=\sqrt{\frac{1-x}{1+x}}\tag6
$$
Combining $(3)$ and $(6)$ yields
$$
\sqrt{\frac{1-x}{1+x}}\le\frac{\log(1+x)}{\arcsin(x)}\le1\tag7
$$
Comment: I don't really understand the inequality in (3) and (4)
Comment: @YibeiHe: I have expanded on them. Hopefully, it's clear now.
Answer: Through the substitution $x=\sin\theta$ we just have to prove that
$$ \log(1+\sin\theta)<\theta,\qquad \theta\sqrt{\frac{1-\sin\theta}{1+\sin\theta}}<\log(1+\sin\theta)$$
hold over $(0,\pi/2)$. The former inequality is trivial as a consequence of $\log(1+\sin\theta)<\sin\theta<\theta$.
The latter is a consequence of
$$ \log(1+\sin\theta)>\tan(\theta)\sqrt{\frac{1-\sin\theta}{1+\sin\theta}} $$
which on its turn is equivalent to
$$ \log(1+\cos\theta)>\frac{1}{2}\left(1-\tan^2\frac{\theta}{2}\right) $$
or to
$$ \log\left(1+\frac{1-t^2}{1+t^2}\right) > \frac{1}{2}(1-t^2) $$
or to
$$ \log\left(\frac{2}{1+t}\right)>\frac{1-t}{2} $$
for $t\in(0,1)$, which is a consequence of the convexity of $-\log$.


Elements of a monoid generated by a single element.

Question: Let $K$ be a real quadratic field and $\epsilon$ be its fundamental unit. When we consider the monoid $\mathfrak{M}$ in $\mathcal{O}_K$ where $\mathfrak{M}$ is generated by $\epsilon$, does this mean that $\mathfrak{M}=\{ \epsilon ^n : n \in \mathbb{N}_0\}$? I just want some clarification on this. Thanks in advance.
Answer: Yes, that is exactly what it means.


Make browser interpret txt as html

Question: if I add an iframe inside a page and make the src="temp.txt" how can I make the browser interpret it as HTML instead of only displaying the content of "temp.txt"?
Comment: Why does it have the .txt extension if it is actually HTML? It's not really illegal, just as you could name your GIF file using the .pdf or .doc extension, but it's not practically sensible.
Answer: Browsers learn the MIME type of the file from what the web server tells the browser it is. Generally, web servers are configured so that .txt files are plain text files, and that the browser shouldn't try to interpret any markup in the file.
What you want is for the browser to get the mime type of "text/html". Three ways to do this;
1) Set the filename to temp.htm, this is the easiest
2) Change the web server so that txt files are now html files. In apache for example, you can add a line to httpd.conf that looks something like
AddType text/html .txt

and remove the line that looks like
AddType text/plain .txt

I wouldn't recommend this path, as this is a global change. You can try adding this to your .htaccess file in the directory your code resides in as well.
3) Use a dynamic language like PHP to set the file's mime type in transit. For example in PHP:
header('Content-type: text/html');

and then read the file to the browser.
Answer: Or, if you don't want/have-access-to/permission to manipulate Apache files
and your .txt file is at the same domain where your page is
you can access 's .textContent and
replace 's .innerHTML with it:
say you have an  in :
,
and say you have afile.txt file in same directory:

HTTP/1.1 200 OK

Date: Sat, 13 Apr 2013 16:46:15 GMT

Server: Apache

Last-Modified: Fri, 08 Mar 2013 12:57:30 GMT

Accept-Ranges: bytes

Content-Length: 216469

Connection: close

Content-Type: image/jpeg

you can replace page content by running this:
//
window.onload =
function () {
  var frame = document.getElementById("frame01");
  document.body.innerHTML =
    frame.contentWindow.document.body.textContent;
};
//
//

and output page shold look like:

HTTP/1.1 200 OK
Date: Sat, 13 Apr 2013 16:46:15 GMT
Server: Apache
Last-Modified: Fri, 08 Mar 2013 12:57:30 GMT
Accept-Ranges: bytes
Content-Length: 216469
Connection: close
Content-Type: image/jpeg

with htm tags parsed.

Make browser interpret txt as html


Building something like an affiliate shop?

Question: We want something very much like Amazon in that some providers define their products in a (site-)global catalog and customers can buy from this catalog.
Customers and providers have the standard features expected from such a system (history, various reports etc etc).
A little peculiarity is that there are no payments or shippings in this system. We are responsible for gathering the orders from customers and notify the providers. The rest is not up to us.
Which modules do you think we should use to implement such a system ? (Drupal 6)
Answer: You've tagged this with ubercart and you're using Drupal 6 so this should be fine - http://drupal.org/project/ubercart_marketplace
If you're not processing payments directly and have some 3rd party integration you're probably best off implementing a custom payment gateway and method using the hooks provided by ubercart than trying to track down a contrib module.


Porque meu jQuery parou de aceitar $ mas esta aceitado "jQuery"?

Question: Com esse código da erro

Porque esta dando erro sendo que a pouco estava funcionado perfeitamente?
Comment: Verifique no console do seu navegador o que a variável `$` contém. Algum código provavelmente está sobrescrevendo essa variável.
Comment: pode explicar melhor? o que devia fazer o código? onde está usando ele?
Comment: como o @user140828 mencionou, algum outro código deve ter sobrescrito a alias "$", vc adicionou recentemente alguma biblioteca js no seu projeto? Aliás, só pra deixar a nota aqui, muita gente não sabe, mas o "$" é só um apelido para o jQuery, poderia ser qualquer outra coisa que não esteja reservada pelo javascript ou esteja sendo usada, e é configurável tb
Comment: @GustavoRichter Richter esse código é para poder colocar html em um popover e coloca e remove uma classe conforme um slide troca de imagem
Comment: @user140828 retorna isso no console Uncaught TypeError: $ is not a function
    at (index):524
Answer: É um boa prática quando se utiliza jQuery não utilizar diretamente $. Existem algumas formas de referenciar $ justamente para se evitar os erros que você está tendo.
Usando um atalho
// Note que $jq pode ser qualquer nome de variável que você desejar.
var $jq = jQuery.noConflict();

// Veja o exemplo
$jq( '#link ').css( 'background', 'red' );

Usando uma IIFE
(function ( $ ) {
   // Dentro deste escopo $ é uma referência ao objeto jQuery
   $( '#link' ).css( 'background', 'red' );
})( jQuery );

Passando $ como argumento na chamada jQuery( document ).ready()
jQuery( document ).ready(function ( $ ) {
   // Dentro deste escopo $ é uma referência ao objeto jQuery
   $( '#link' ).css( 'background', 'red' );
});

Usando uma sintaxe mais curta que a anterior
jQuery(function ( $ ) {
    // Dentro deste escopo $ é uma referência ao objeto jQuery
    $( '#link' ).css( 'background', 'red' );
});

Mas você deve estar se perguntando em qual situação devo usar cada sintaxe?

Opção 1:  Indicado quando você possuir outras bibliotecas que também usam  $ (como prototype.js por exemplo) e como o nome da fucionalidade já diz, esta funcionalidade evitará o conflito entre as bibliotecas.
Opção 2: Usando uma IIFE você cria um escopo interno onde $ será acessível somente dentro da daquela função. Você pode ou deve utilizar esta sintaxe quando você não quer ou não deseja esperar o dom ser carregado completamente. Por exemplo, quando se tem certeza que seu script será carregado após o(s) elementos(s) que você quer manipular ou você tem certeza que seu script será a última coisa a ser carregada na página.
Opções 3 e 4: Quando a ação que você deseja realizar em seu script necessita que o DOM esteja completamente carregado.

Seu código alterado poderá parecer com trecho de código abaixo:
jQuery( document ).ready(function ( $ ) {
    var slide_old = "slide_img0";
    var slide = "efeito_slide";

    $('[data-toggle="popover"]').popover({
        html:true,
        animation: true,
        sanitize: false
    });

    $('#carouselExampleCaptions').on('slide.bs.carousel', function (ev) {
        var id = ev.to;
        switch (id) {
            case 0:

            $("." + slide_old).removeClass( slide )
            $(".slide_img0").addClass( slide );
            slide_old = "slide_img0";

            break;
            case 1:

            $("." + slide_old).removeClass( slide )
            $(".slide_img1").addClass( slide );
            slide_old = "slide_img1";

            break;
            case 2:

            $("." + slide_old).removeClass( slide )
            $(".slide_img2").addClass( slide );
            slide_old = "slide_img2";

            break;
            case 3:

            $("." + slide_old).removeClass( slide )
            $(".slide_img3").addClass( slide );
            slide_old = "slide_img3";

            break;
            default:
            break;
        }
    });
});

Referência

jQuery summary-of-ways-to-reference-the-jquery-function
Comment: Muito Obrigado, eu não sabia nem metade  disso e seu código funcionou perfeitamente


How to enable TLS 1.1 and TLS 1.0 on apache?

Question: I cannot enable tls 1.0 and 1.1 on my server.
I have this configuration:
SSLProtocol -all +TLSv1 +TLSv1.1 +TLSv1.2
SSLCompression off
SSLHonorCipherOrder on

SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4

What I'm doing wrong?
UPDATE:
I have 1.0.1f-1ubuntu2.21 openssl version
Comment: you forgot to add the most important thing, which openssl version are you using? and also make sure apache reports the same in it server error.log when it starts. Also generally people define `SSLProtocol all -SSLv2 -SSLv3` instead, or even `SSLProtocol +TLSv1 +TLSv1.1 +TLSv1.2`, not sure of the gain of convoluting it the way you defined it.
Comment: I think those cipher suites are either all TLS 1.2 spec key exchanges or non-applicable.
Comment: For readers coming here after 2020: Are you sure you want this? SSL/TLS protocols prior to TLS 1.2 are deprecated and considered insecure now.
Answer: The following config does the trick for me (Ubuntu 20 with Apache2 v2.4.41 and openssl 1.1.1f):
SSLProtocol             all -SSLv2 -SSLv3
SSLCipherSuite          CDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:@SECLEVEL=1

Notice the @SECLEVEL=1 at the end of the line.
Comment: Note that this is for serving TLS1.0/1.1. So If your Apache 2.4.41 need to serve TLSv1. If you need to connect to TLSv1 it will have no effect. So if you have something like `ProxyPass / https://my.old.server.local:443/` then you need to enable TLS1 in `openssl.conf`.
Comment: Thank you. @SECLEVEL=1 ended our 3+ hour debugging session!
Answer: There are too many variables and not enough information to give you an answer that will solve your problem. You are better off using the Mozilla SSL Config generator, then testing with SSL Labs to make sure you are getting at least an A-.
Comment: I'm usign SSL labs to perform my check and I see this: https://www.ssllabs.com/ssltest/analyze.html?d=tropicalseo.net. I've followed all the rules but tls 1.1 is still not enabled. I'm not sure what informations I should give you. Thanks
Comment: You don't want TLS 1.1 enabled: https://community.qualys.com/thread/16565-is-there-a-reason-for-still-having-tlsv11-enabled
Comment: As for TLS 1.0, it depends on your audience. Only older clients use it. Re-generate your config using the Mozilla tool and report back.
Answer: SSLProtocol -all +TLSv1 +TLSv1.1 +TLSv1.2
It should work
Comment: This is the same config that is listed in the question.


Amazon SES and EC2 instance in different regions

Question: We need to send transactional emails from our e-commerce website hosted on an EC2 instance. Our regular email service is hosted by Google.
The problem is that we are based in Asia-Pacific region where SES is not available. So can we change our region just for using SES? Will it impact our EC2 instance? Can't find any documentation regarding this.
Comment: As of June 2015 Amazon still doesn't support Asia-Pacific. I've encountered the same issue.
Answer: (Note: I think you mean different "regions" and not different "zones". In AWS a "zone" refers to an availability zone inside a given region. I will assume you mean regions here; SES is a region level service.)
Yes, as you say SES is not currently available in the Asia-Pacific region.
However, there is no reason you cannot connect cross-region to an SES service from your existing region. You do not need to change your app in your existing region to do this. While SES and networking best practices suggest you would want to choose an endpoint closest to your application, to reduce network latency, there is no SES requirement for your app instance to be in the same region as your SES service. Assuming you are using SMTP/S to send email, the email server can be anywhere on the internet.
You can configure your app in Asia-Pacific to send email via the SMTP/S endpoint provided by SES in any region. Again, network latency may be an issue, but depending on your mail volume, I would not expect network latency to be prohibitive. In fact I believe this setup is quite common for users in regions where SES is not supported.
I would suggest you setup the SES service in any of the available regions (say EU-Ireland), and run some basic load testing and see how the latency affects your application, if at all.
For more info, see Connecting to the Amazon SES SMTP Endpoint
Comment: Thanks a lot for the inputs. I just completed the SES formalities (domain verification, email verification, SMTP credentials generation, etc.) and successfully sent a test email. So I guess it should also work programmatically from my EC2 instance.
Comment: @Golmaal, Hi, is your application still using this SES setup?
If yes, can you provide some statistic and if no, could you clarify why and what have you switched to?
Comment: As of December 2019 Amazon supports several regions in Asia: https://docs.aws.amazon.com/general/latest/gr/ses.html?shortFooter=true#ses_region
Answer: Workaround:
Use Pinpoint which internally uses SES. Pinpoint is available in India region.
After creating a project, you can configure settings for Email/SMS. The standard procedures of Domain verification, Addition of DNS records (generated in aws console), Email address verification are present here as well.
You can proceed to fetch the SMTP details or the API, with which you can start sending emails.
The bounce configuration cannot be done via console like in SES. Instead, you can opt for streaming to Kinesis and then write custom queries to get all the stats.
Hope this helps.


Lebesgue integral of gradient

Question: I am wondering how Lebesgue integration interacts with a derivatives of the integrand.
I'll start with a simple observation. If $f$ is a univariate function (a function of one variable $x$), then the indefinite integral of its derivate is just $f$ again, i.e.,
$$
\int \frac{d}{dx} f(x) dx = f(x)
$$
which is also why the indefinite integral is called "antiderivative".
Things get a little more interesting if we introduce limits to the integral, e.g.,
$$
\int_a^b \frac{d}{dx} f(x) dx = f(x) |_a^b = f(b)-f(a)
$$
It gets even more interesting when we don't use a range [a;b] to delimit the integral, but a set $\mathcal{R}$, and if $f$ is a function of more than one variable.
Question: Let $f(\mathbf{x})$ be a continuous, smooth, real-valued function of a $d$-dimensional vector $\bf{x}$ and let $\mathcal{R}$ be a bounded, finite, continuous subset of $\mathbb{R}^d$. $f(\mathbf{x})$ is defined everywhere in $\mathcal{R}$ and $\mathcal{R}$ is not a function of $\mathbf{x}$. What is
$$
\int_{\mathcal{R}} \nabla f(\mathbf{x}) d {\mathbf{x}}?
$$
For a Riemann integral, e.g., if $\mathcal{R}$ is a rectangle with corners (0,0),(a,0)(0,b)(a,b), I guess we would get
$$
\int_0^a \int_0^b \nabla f(\mathbf{x}) d\mathbf{x} = \left(f((a,0)^T)-f((0,0)^T)\right)\times\left(f((0,b)^T)-f((0,0)^T)\right)
$$
but how does this work for a (Lebesgue) integral over set $\mathcal{R}$?
Comment: Before you start, what do $\frac{d}{d\mathbf{x}} f(\mathbf{x})$ and $\int g(x) d\mathbf{x}$ mean? Also $f(a)$ etc. don't make sense since $a$ is scalar and the domain of $x$ is vectors.
Comment: With $\frac{d}{d\mathbf{x}}f(\mathbf{x})$ I mean the derivative of $f$ with respect to vector $\mathbf{x}$. For an indefinite integral, we would thus have $\int \frac{d}{d\mathbf{x}}f(\mathbf{x})=f(\mathbf{x})$. I guess my example with the rectangle was a bit too sloppy, I will update the question accordingly
Comment: So for example, if $f((x_1,x_2)) = x_1^3 - x_1 x_2 + x_2^2$, what is the derivative of $f$?
Comment: Ah, I should have probably used the term "gradient" instead of derivate. Sorry. So the "derivative" would be the vector of partials of $f$ with respect to $x_1,x_2$
Comment: Okay. I would write that $\nabla f(\mathbf{x})$.
Answer: If $f$ is merely continuous you can't say anything even in the one-dimensional case of the fundamental theorem of calculus, as it may not be differentiable. You need $f$ to be absolutely continuous in order for the fundamental theorem to hold, in which case the conclusion is the same as for the Riemann integral, namely,
$$\int_a^b f'(x) dx = f(b)-f(a).$$
So assuming $f$ is absolutely continuous, it's sufficient to consider one partial derivative, in which case $\partial_i f = \nabla\cdot (fe_i)$ where $e_i$ is the $i$th unit vector. Then by the divergence theorem (whose proof for the Lebegsue integral is the same as long as you have the fundamental theorem of calculus in 1D)
$$\int_{R} \partial_i f(x) dx = \int_{\partial R} f(x) n_i(x) dS(x)$$
Where $n_i$ is the $i$th component of the (outwards) unit normal to the boundary of $R$, and $dS$ is the surface measure on $\partial R$. Note that integrating a derivative in higher dimensions gives you an integral over a surface of one less dimension than the region you're integrating over.
Comment: +1. The divergence theorem was the pointer I needed.

Lebesgue integral of gradient


How to turn multiple dummy vars into 1 factor variable?

Question: I have a huge data set from a survey with loads of statements that are dummy variables. Each dummy is a factor with levels "quoted" and "not quoted". Since different groups of statements belong to the same topic, I'd like to transform them into 1 bigger factor variable that takes the dummies as levels and the values remain "quoted" and "not quoted" (or 1 and 0, doesn't matter at the moment).
So what I have now looks like this from 2 dummy variables:
    pp_plan_thoughtAWhile   pp_plan_justHappen
     not quoted                  not quoted
     not quoted                  not quoted
     not quoted                  not quoted
     not quoted                  not quoted
     not quoted                  quoted
     quoted                      quoted

And I need it to look like something like this:
               #plan
      ## value     thoughtAWhile    justHappen
           0           350             550
           1           650             450

Does anyone know how to do this? Any help would be highly appreciated, I'm struggling!
Comment: On the right hand side is the frequency of 0 and 1 right?
Comment: @RanaUsman What do you mean by right hand side here?
Comment: 350, 650, 550 450.
Comment: @RanaUsman Oh yeah, sure! Sorry, didn't realise :)
Answer: We can use gather to reshape the dataset to 'long' format, then get the frequency with count and spread to 'wide' format
library(tidyverse)
gather(df1) %>%
   count(key, value) %>%
   spread(key, n)

Comment: @DanaDaskalova  Are you using the `dplyr::count` or is the function masked from some other library.  Try `gather(df1) %>% dplyr::count(key,value)`
Comment: I tried this when I get to count, it throws this error:

Error in .fun(.value[i], ...) : invalid 'type' (character) of argument
Answer: Here is one way to do this.
Data
   pp_plan_thoughtAWhile <-  sample(c("Quoted", "NotQuoted"), 10, replace = T, prob=c(0.7, 0.3))
   pp_plan_justHappen  <- sample(c("Quoted", "NotQuoted"), 10, replace = T, prob=c(0.5, 0.5))
   dv <- data.frame(pp_plan_justHappen, pp_plan_thoughtAWhile)

Some Processing
dv$pp_plan_justHappen <- as.factor (dv$pp_plan_justHappen)
dv$pp_plan_thoughtAWhile <- as.factor(dv$pp_plan_thoughtAWhile)

library(reshape2)
mdata <- melt(dv)

mdata$bin_plan_justhappen <- ifelse(mdata$pp_plan_justHappen=="Quoted", 1, 0)
mdata$bin_plan_thoughtwhile <- ifelse(mdata$pp_plan_thoughtAWhile=="Quoted", 1, 0)
library(plyr)
table(mdata$bin_plan_justhappen, mdata$bin_plan_thoughtwhile)
plyr::count(mdata, c("bin_plan_justhappen", "bin_plan_thoughtwhile"))

Result
bin_plan_justhappen bin_plan_thoughtwhile freq
               0                     1    2
               1                     0    1
               1                     1    7

Comment: I just generated random data as per your example to work through implementation. You don't need to generate data, it should already scale to your problem.
Comment: Thanks for this, but I don't know the exact probabilities, the ones in the example I gave are just for reference, so I don't know how this would work otherwise


Proving Ackermann's function is decidable through a Turing Machine

Question: I am aware that Ackermann's function is total and complete, meaning it is decidable for every input, regardless of how long it takes to compute the result. I'm doing a project in which I decided to prove that the function is decidable through a Turing Machine that I designed.
I also don't know if I designed this TM correctly.
So Ackermann's Function is defined as $A=(m,n)$, where m,n are non-negative integers, such that $(m,n)$ are defined by the following:
\begin{cases}
      n+1 & m=0 \\
      A(m-1,1) & m>0 ,n=0 \\
      A(m-1,A(m,n-1) & m>0 , n>0
   \end{cases}
So, following the instructions above I came up with a pseudocode for a TM that would tell me if the function is decidable, here's what I have:
read m
read n
if m = 0
   write n+1 into A
   halt
if m > 0 and if n = 0
   write m-1 to m in another TM
   write 1 to n in another TM
   run other TM
   copy A from other TM to A
   halt
if m>0 and n>0
   write m to m in another TM
   write n-1 to n in another TM
   run other TM
   copy A from other TM to A
   write m-1 to m in another TM
   write A to n in another TM
   run other TM
   copy A from other TM to A
   halt

can someone tell me if this looks about right? Also, how does one decide what the initial values for the 0's and 1's are on the tape? Or are they just randomly placed?
Comment: Where do your conditionals end? Indentation helps!
Comment: @celtschk Sorry I'll indent it properly!!!
Comment: Your "code" is essentially a literal transcription of the definition of the function. There is exactly one key point in implementing the Ackerman function: doing the recursive calls — and you hand-waved yourself out of that. You should read up a bit on how to do recursion in a TM.
Comment: @MarianoSuárez-Álvarez It seems that the whole notion of creating a TM is a little big ambiguous to me. I have read what I could find on it and I still don't quite understand it well. If you look here [link](http://cglab.ca/~michiel/TheoryOfComputation/TheoryOfComputation.pdf) on page 151, you will see what I eventually want to be able to do for the function in hand.
Answer: I don't know whether your code is correct or not (I've only glanced at it, and it's fairly early in the morning), but it's very high-level. "Run a Turing machine" is not a primitive operation in a Turing machine. If you're happy to express it in a Turing-equivalent language, rather than in a Turing machine itself, then your task is much easier.
Instead, you could implement a stack, and push $m, n-1$ to the stack; then set your TM up so that at the start of execution, it seeks to the top of the stack, then reads the top two numbers from the stack. On completion, the TM should write its output to the tape, replacing the top two numbers of the stack. Then the operation "run a copy of myself" is just "run myself from the beginning" (or goto start state), because the TM will only consume the top two values and replace them with the output.
You're reading $m$ and $n$ at the start, so the initial value on the tape should be $\mathrm{list}(m, n)$, where $\mathrm{list}$ is some function that expresses arbitrary lists of naturals as a single natural. (Then your TM should contain code to interpret that initial value correctly as a list.)
Do remember that you need to prove that your TM halts, in order to show that Ack is decidable. For that, you'll need structural induction on $\mathbb{N}^2$ in lexicographic order.
Comment: So let me see if i understand this correctly. In order to prove that the function is decidable I need to code up the Tm and implement it using a stack to hold m, and n-1. That much I understand but, by implementing and running the function through my TM it would tell me if it halts or not right ? So I don't understand the need to further prove, with structural induction that it does indeed halt. Could you clarify that a little for me. What does running list(m,n) initially do ?
Comment: Determining whether an arbitrary TM halts is in general impossible, so you need some specific reason why this one should always halt. "I ran it for some inputs and it halted" is not sufficient; I don't see how you're proving that it always halts. list(m,n) simply puts the values m and n onto the stack - for example, the tape might have two possible symbols 0 and 1. Then list(m,n) might write 111…10111…1, where there are m 1's then a 0 then n 1's. (That's only one way of coding up a list of integers; there are much more efficient ways.)


Why Can I Delete Open Image Files in .NET using FileInfo?

Question: I have a small utility that deletes files from folders using .NET's FileInfo.Delete() method.  Basically, I can catch an IOException if the file is currently open.  This works for all files except for image files (JPG, GIF, BMP, ect.).  When these types of files are open, the program is still able to delete them without throwing any exception.  Have you encountered this before?  How would go about detecting that these types of files are open so that I can prevent the deletion.
Thanks,
Minh
Answer: Whether or not you can delete a file while it is open depends more on the program in which the file is open than the file type. If the application for instance simply reads the file contents and then closes the file (as Notepad for instance), you will be able to delete the file while it is "open" (meaning that it is showing in Notepad). Excel on the other hand keeps the file open in a manner that you usually are not able even to copy it.
Answer: Use a tool like WhoLockMe or Unlocker to see whether the file is really "open". As others have said this has nothing to do with the type of file, merely how the program reading it opens the file.


Is it true that $f(X)$ is countable implies $f$ is measurable?

Question:
Let $(X, \mathcal{A})$ and $(Y, \mathcal{B})$ be measurable spaces, and $f:X \to Y$. We call $f$ is measurable if and only if $$\forall B \in \mathcal B: f^{-1} (B) \in \mathcal A$$

I would like to ask if $f(X)$ is countable implies $f$ is measurable. Thank you so much!
Comment: Why would it? Unless $\mathcal A=\mathcal P(X)$, take $A\in\mathcal P(X)\setminus\mathcal A$ (i.e. a non-measurable set) and define $f$ to have one value on  $A$ and a different value on $X\setminus A$...
Answer: Take a non measurable subset $A$ of $X$ and consider its indicator function $\chi_A$.
Comment: Good stroke (+1) !


how to correctly change the source ip of a ping packet?

Question: So I have a private network and there is a host with ip = 10.20.111.2
and there is a firewall for this host which has ip = 10.20.111.1
And of course i has external network , host ip =10.10.111.107
So i would like my external host to ping the private host and private host reply. After that, the external host will examine the ping packet and should find the private host has a IP address = 10.10.111.1
And i used the command on the firewall:
-iptables -F
-iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 10.10.111.1
but when i examine the echo from the external machine, the packet from the private network still has source address = 10.20.111.2 ..which means my command doesn't work.
Please help :(...
Comment: @aseq I don't think qwr is implying that it's public IP space, merely that 10.10.111.107 is external relative to the other subnet in question: 10.20.111.n
Comment: 10.10.111.107 is NOT an external IP. It's part of the IP range 10.0.0.0/8 reserved for private use. See http://tools.ietf.org/html/rfc1918 and http://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xml
Comment: Possibly, it's hard to determine considering the way it was phrased.
Comment: Sorry, 10.10.111.0/24 is the external network.
10.20.111.0/24 is the private network.
Answer: Try something like:
<code>iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE --to 10.10.111.1
</code>


Node titles are erased when I update a field with entity_metadata_wrapper

Question: I've written a script which fetches data from a REST API and uses it to update a field in a node. The website is multilingual (Canada English, Canada French, Australia English and US English) with US English as the default language. When I run the script with drush, it updates field_gj_metro_id with the correct value from the REST API, but for all nodes with languages other than US English, it deletes the node title.
What am I missing in my script that would take care of the language settings so that the titles will be unaffected?
<code>function fetch_metros() {
  return gj_api_request('GetMetroList', $arguments = array());
 }

$metros = array();
$metros = fetch_metros();
foreach ($metros as $metro) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'gj_locations')
    ->propertyCondition('status', 1)
    ->propertyCondition('title', $metro['metroName'])
    ->propertyOrderBy('created', 'DESC')
    ->fieldCondition('field_display_in_listing', 'value', TRUE);
  $result = $query->execute();

  if (!empty($result)) {
    $nids = array_keys($result['node']);
    $node = node_load($nids[0]);
    $node_wrapper = entity_metadata_wrapper('node', $node);
    $node_wrapper->field_gj_metro_id->set($metro['metroID']);
    $node_wrapper->save();
  }
}
</code>
Comment: not fully related to your question 1) set the `revision` flag may help on the node if its not set on the content type 2) you could try `$node->wrapper->title->set( $node_wrapper->title);` and then set the language flag too ...
Comment: Revision flag is already set on the content type. I tried various methods of setting the title but nothing worked. The node retains all of it's existing data, except for the title.
Comment: I ran into a similar issue with title values being blanked out (also I was using the Title module). I needed to call `entity_get_controller('node')->resetCache($nids)` before I saved the entities https://api.drupal.org/api/drupal/includes%21entity.inc/function/DrupalEntityControllerInterface%3A%3AresetCache/7.

It might not be what you need but worth a shot? Entity Translation is... somewhat chaotic.
Answer: I tried calling resetCache but the problem persisted. In the end I created a page for each language and used the PHP filter to add the script to the page. By switching to each language, setting the value of propertyCondition('language','') to the loaded language and loading the corresponding page, I was able to import the metroIDs and update the pages, one language at a time, without deleting the titles.


IIS Server 404 pages wordpress and video 404 page

Question: I Am trying to set up a WordPress blog with BuddyPress, My permalinks have example.com/index.php/pagename, I've never had "index.php" in my permalinks
also any BuddyPress page is a 404 page, and I put a video on the IIS website and that also gives a 404 error.
Any suggestions are welcomed ? I can not really set up my website with these problems.
Answer: try this. may be its work
all file should be under C:\inetpub\wwwroot folder


App para de responder quando executa onStartCommand

Question: Eu chamo o service no onPause() da minha activity e assim que saio do app, ele para de responder depois de um tempo. Pelo que pesquisei pode ser que os processos sejam muito pesados para serem executados em segundo plano. Eu preciso verificar o horário pra saber se está na hora de enviar  a notificação.
public int onStartCommand(Intent intent, int flags, int startId) {
        if (inicio != null) {
            ok = trataData(d2,d3);
            horaNotificacao.set(Calendar.MILLISECOND,this.intervalo);
            while (ok == true){
                try {
                    agora = new Date();
                    if(agora.equals(horaNotificacao.getTime())){
                        gerarNotificacao();
                        horaNotificacao.set(Calendar.MILLISECOND,this.intervalo);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                ok = trataData(d2,d3);
            }
        }
        return START_STICKY;
    }

trataData:
   public boolean trataData(Date d2, Date d3) {
        boolean ok = false;
        Date d1 = new Date();
        if (d1.before(d2) && d1.after(d3)){
            ok = true;
        }
        return ok;
    }

Acho q o problema ta nesse laço do ok, mas n vejo outra forma de fazer a verificação
Answer: O Service não é uma Thread separada, isso significa que ele roda na sua Main Thread fazendo assim com que sua aplicação pare de responder.
Para resolver o problema você precisa executar sua rotina dentro de uma nova Thread.
Ficaria desse jeito:
public int onStartCommand(Intent intent, int flags, int startId) {
    new Thread(new Runnable() {
       @Override
       public void run() {
          // Coloque sua rotina aqui dentro
       }).start();
    return Service.START_STICKY;
}

Com isto, a sua rotina demorada será executada em uma nova Thread. Do jeito que está no seu código, ela é executada no onPause() até terminar, por isso que acontece o problema.
Não esqueça também de parar o serviço após terminar a execução da rotina.

Veja mais nos links abaixo:
Android Service Example
Antipattern: freezing the UI with a Service and an IntentService
Comment: Deu certooo! Obrigada de novo :D


How to install Matplotlib in Python 3 on Windows

Question: Matplotlib tell us that the requirement is Python 2.4 or later but not Python3.
Now I'm working with Python3 and I need some plot from Matplotlib.
So how do I solve it?
Answer: I'm sorry, but at the current time it's not supported.
If you feel brave, you can try with the Py3k SVN branch which reportedly works with a simple example. Be aware that there has been no update in the last 8 months on this though.
Of course, you'd be more than welcome to contribute to the porting to Python 3 if you could.
Comment: The main matplotlib python3 branch is at https://github.com/matplotlib/matplotlib-py3, not the sourceforge svn branch (which is why the svn branch appears to be dead).  Also, see this wiki on the current status of matplotlib on python3 https://github.com/matplotlib/matplotlib-py3/wiki
Comment: For future readers, note that Python 3 support has now been merged into the development version of matplotlib, so it's available in the main repo: https://github.com/matplotlib/matplotlib
Answer: You could try the unofficial versions. Check this site:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Answer: Looking at the docs at the link it says the following is a dependency:
Python (>= 2.7 or >= 3.4)
You can download a version of Python that will work at:
https://www.python.org/downloads/windows/
I would go with the most recent release and a 64 bit version as certain libraries for data analysis do not run on the 32 bit version of Python.
The version of Python you download will come with pip which you can then use to install any libraries you need to do your work.
Make sure you have set your environment variables if you want to run your programs from the command line or bash terminal.
I installed matplotlib through the bash terminal with:
pip install matplotlib

Let me know if that helps.
Answer: In Visual Studio 2022 just use the "Solution Explorer" to install Matplotlib.

Expand the Project and then expand the "Python Environments"
Right click on the Python installation and choose "Manage Python Packages..."

Type "matplotlib" in the search field
Click on "Run command: pip install matplotlib"

Now Matplotlib should be installed on the Visual Studio Python environment!
Note that if the chosen Python environment is:

"Python 3.XX (global default)"
Then Matplotlib will be installed on your default Visual Studio
Python environment,
NOT the (global default)
Then Matplotlib will JUST be installed on your current project.
Answer: To install matplotlib on windows, first, you have to install pip first to install pip on windows go to website
https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip
Download get-pip.py, being careful to save it as a .py file rather than .txt. Then, run it from the command prompt:
python get-pip.py

if pip is already installed, install matplotlib by writing in command prompt:
python -mpip install -U pip
python -mpip install -U matplotlib


Learning about functions in swift

Question: first time here. I'm watching a video to supplement my learning on Swift (It seems to be an older video that has to name the first var when called, which I understand is no longer the case). Anyway, I'm having a hard time understanding how line 7 is going wrong. It's saying that I can't have a function return an integer. It's a function within a function that returns a function with no arguments but returns an integer type). Thanks!
func buildIncrementor() -> () -> Int {
    var count = 0
    func incrementor() -> Int {
        count += 1 //video uses ++count//
        return count
    }
    return incrementor()
}
var incrementor = buildIncrementor()

Answer: Try this
func buildIncrementor() -> () -> Int {
    var count = 0
    func incrementor() -> Int {
        count += 1 //video uses ++count//
        return count
    }
    return incrementor
}
var incrementor = buildIncrementor()

incrementor() returns the return value of the incrementor() function whereas incrementor returns the function.

Learning about functions in swift


Any advantage to using "file://" when defining dfs.datanode.data.dir?

Question: Wondering why should one use "file://" when defining the data directory on the datanodes. I know it will work even if I don't use "file://" and simply do something like /mnt/path/ but the official documentation mentions "file:///some/path".
Answer: If your question is why it has officially mentioned to use file://

then you know the delimiting character is most commonly the slash ("/"), the backslash character ("\"), or colon (":"), though some operating systems may use a different delimiter.
paths are used to represent the directory/file relationships common in modern operating systems, and are essential in the construction of Uniform Resource Locators(URLs).
resources can be represented by either absolute or relative paths.

Now when you are trying to go to the HDFS directory in Linux based OS then it will hit the target directory or file where the URL starts with file://.

You already know that website URL starts with http://, then why you have not wondered in this? I hope you can understand that it depends on the target location, target type and the protocol which have to be transfer or not.
Comment: Thanks for the reply. Although, that didn't really answer my question. "file" is used to determine file types, makes sense. However, w.r.t. HDFS datanode directory, why would we care to add the prefix "file://"; to determine file types of stuff stored on the datanodes? Wouldn't that be automatically resolved even if we don't specify the "file://" prefix? My question really is to know if there are any performance advantages to use this prefix against not using it.


Sputtery tonging on Trombone when playing music

Question: For some reason, whenever I play a piece of music (in this case, H. Vincent's 'Air for Baritone/Cornet'), I find that my tonging is very stutters and rough between notes, yet if I play one note, low or high, and tongue it, it sounds just fine.
I have a solo tomorrow (March 1st.) and it sounds pretty bad for this reason.
Are there any ways which I could improve my tonging so that it actually sounds decent + tips for my first solo? I have been playing since November of 2022.
P.S. I have to slur (legato) most of it, where most of the sputtery/stuttering sound occurs.
Answer:
The first three notes are 3rd position F, 4th position A then 1st position C. So you have only a fraction of a second to move from 4th position to 1st position. With practise you'll find that you can move almost instantaneously between those positions. But you've not been playing long enough to acquire that skill yet. You're doing well to be tackling this piece after only a few months playing, though. Well done!
The reason for the sputtery attacks is that you're articulating while the slide is moving. So it's not the actual tonguing that's the problem - it's the synchronization with the slide movement. But with practice you'll improve. Try to be patient with yourself - you hear other players who can do this sort of thing - but they've spent hours working on it.
Comment: Thanks, it's in a few hours and I have classes so I can't practice until then. But I'm glad I haven't been messing up horridly. One more question, when I'm playing, is it normal to hear air escaping the side of my lips and not going in the mouthpiece? I can't hear it on recordings but it is easy to hear when I play, for me at least.
Comment: Your embouchure shouldn't allow air to escape round the sides of the mouthpiece. There are several reasons why this might be happening. Your in-person brass teacher should be able to sort this out, or at least give you some exercises that will eventually sort it out. It's not really possible in a forum like this to say what the actual problem is.


Installing BeautifulSoup4

Question: I am running into problems installing BeautifulSoup4. This is the code I am using in a Jupiter notebook to import beautifulsoup
from selenium import webdriver
import beautifulsoup4
import pandas as pd

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In [12], line 2
      1 from selenium import webdriver
----> 2 import beautifulsoup4
      3 import pandas as pd

ModuleNotFoundError: No module named 'beautifulsoup4'

When pip installing in terminal I get following output which states that beautiful soup should be installed:
(CodingFolder) user ~ % pip install beautifulsoup4
Requirement already satisfied: beautifulsoup4 in ./opt/anaconda3/envs/CodingFolder/lib/python3.9/site-packages (4.11.1)
Requirement already satisfied: soupsieve>1.2 in ./opt/anaconda3/envs/CodingFolder/lib/python3.9/site-packages (from beautifulsoup4) (2.3.2.post1)

What am I missing ?
Comment: Have you tried `from bs4 import BeautifulSoup` ?
Comment: Can you try importing this way:

 `from bs4 import BeautifulSoup`

Found on [beautifulsoup4 doc](https://www.crummy.com/software/BeautifulSoup/bs4/doc/)
Comment: `pip install beautifulsoup4` or `pip3 install beautifulsoup4`
Answer: Install with:
$ pip install beautifulsoup4

and then you should be using this import statement:
from bs4 import BeautifulSoup

not:
import beautifulsoup4

Installing and importing BeautifulSoup.
Answer: To Install write
pip install beautifulsoup4

and then import as
from bs4 import BeautifulSoup

For more information refer https://www.crummy.com/software/BeautifulSoup/bs4/doc/

Installing BeautifulSoup4


Python import VIsual Studio Code

Question: I'm trying to import a class from a file, but I can't find the right syntax in Visual Studio Code :i
My structure:
└── src
    ├── common
    │   ├── database.py
    │
    └── models
        ├── admin
              ├──admin.py

In admin.py, I need to import a class called Database, which is located in database.py. My solution would be:
from src.common.database import Database

But then I get this error:

ModuleNotFoundError: No module named 'src'
Answer: As mentioned by @Dave in https://stackoverflow.com/a/7506029/2053706, you can use relative references to other files on import.
What you're looking for is:
from ...src.common.database import Database

Each "dot" is one path back, except the first "dot" which is "this folder".
Hope that makes sense.
Comment: I does make sense.. But when i use this syntax, i get ValueError: attempted relative import beyond top-level package
Comment: With two dots, i get ModuleNotFoundError: No module named 'models.src'
Comment: you might need to declare the "src" folder with absolute paths as per this answer https://stackoverflow.com/a/54997388/2053706

Python import VIsual Studio Code


Restriction mapping and banach algebra

Question: If I am looking at the Banach algebra $A(\mathbb{D})$ and let
$$\Phi: A(\mathbb{D}) \to C(\mathbb{T})$$
be the restriction mapping. Let $\mathcal{B}= \Phi(A(\mathbb{D})) \subseteq C(\mathbb{T})$.
Then I want to show that
(i) $\Vert  \Phi(f) \Vert_\infty = \Vert f \Vert_\infty$ for all $f \in A(\mathbb{D})$
(ii) That $\mathcal{B}$ is a Banach algebra and that $\Phi: A(\mathbb{D}) \to \mathcal{B}$ is an isometric isomorphism. Conclude that $\sigma_\mathcal{A}(f) =\sigma_\mathcal{B}(\Phi(f)) $ for all $f \in A(\mathbb{D})$
(iii) Let $f_0 \in C(\mathbb{T})$ be given by $f_0(z)=z$. Show that $f \in \mathcal{B}$ and show next that $\sigma_\mathcal{B}(f_0)=\mathbb{\overline{D}}$ and $\sigma_{C(\mathbb{T})}(f_0)= \mathbb{T}$.
However, I am not sure how to do this. In (i) it is hinted that I should use the maximum modulus principle but I am not sure how to as I have some trouble seeing what the restriction map really is.
Answer: $A(\mathbb D)$ consists of continuous functions on the closed unit disk which are analytic in the open unit disk. The map $\Phi$ simply takes any $f$ in  $A(\mathbb D)$ to its restriction to the boundary of the disk.
(i) is immediate from Maximum Modulus Principle: The maximum of $|f|$ is attained on  the boundary.
(ii) is a general fact about isometric isomorphisms; they always preserve spectra as you can see directly from the definition of spectrum.
(iii) The fist part is from (ii). For the spectrum of $f(z)=z$ in $C(\mathbb T)$ you have to observe that $f(z)-\lambda$ has an inverse in this space (which means there is a continuous function $g$ on the circle with $f(z)(z-\lambda)=1$ for all $z$ iff $|\lambda | \neq 1$.
Answer: The restriction map is defined as follows:
If $f A(\mathbb{D})$, then $\Phi(f):= f_{| \mathbb T}$, where $f_{| \mathbb T}$ is the restriction of $f$ to $ \mathbb T.$
Now you should see that $(i)$ follows from the  maximum modulus principle.
Can you proceed ?
Answer: Assume $f$ is constant on the interior of the disk. Then clearly the two norms are equal to each other (by continuity, $f$ is also constant on the boundary of the disk with the same constant value).
Next, assume $f$ is not constant on the interior of the disk. The maximum modulus theorem implies that $f$ can't attain a maximum on the interior of the disk. But a maximum must be attained somewhere, because the disk is compact. Thus, the maximum must be attained on the boundary of the disk and the two supremumnorms are evidently equal.
This shows that $\Phi$ is isometric and thus also injective. It is obvious to see that $\Phi$ is an algebra-morphism and $\Phi$ preserves the unit, which is in both cases the constant function $1$.
For such a map, assuming that $\sigma(x)$ denotes the spectrum of the element $x$, we have
$$\sigma(x)=\sigma(\Phi(x))$$
(use that $\Phi$ is an isomorphism (of algebra's) onto its image preserving $1$, the norm structure isn't important here).
The reason you probably had to make this exercise was that it gives an example of a Banach algebra inclusion $B\subseteq A$ where the reverse inclusion between the spectra becomes strict.

Restriction mapping and banach algebra


Calculate limit in use of integrals

Question: Calculate limit in use of integrals
$$ \lim_{n \rightarrow \infty} \sum_{k=1}^{n} \frac{1+n}{3k^2+n^2} $$
My attempt:
$$\sum_{k=1}^{n} \frac{1+n}{3k^2+n^2} = \frac{1}{n} \sum_{k=1}^{n} \frac{\frac{1}{n}+1}{3(k/n)^2+1} = \\
\frac{1}{n}\cdot (1/n + 1) \sum_{k=1}^{n} \frac{1}{3(k/n)^2+1}  $$
Ok, I know that when I am taking limit I should replace (from aproximation theorem)
$$ \sum_{k=1}^{n} \frac{1}{3(k/n)^2+1}$$
with
$$ \int_{0}^{1} \frac{1}{1+3x^2}$$
but I still don't know what have I do (and why) with
$$ \frac{1}{n}\cdot (1/n + 1) $$
part. In many solutions we just ignore part $\frac{1}{n}$ but I don't know why and there where I have little more 'difficult' expression like $ \frac{1}{n}\cdot (1/n + 1) $ I completely don't know what should I do...
$$  $$
Comment: The Riemann Sum formula gives you $\lim\limits_{n\to\infty}\frac{1}{n}\sum\limits_{k=1}^{n}\frac{1}{1+3\left(\frac{k}{n}\right)^2}=\int_{0}^{1}\frac{1}{1+3x^2}$. Don't forget the $\frac{1}{n}$. So it just leaves the $1+\frac{1}{n}$.  Can you notice anything about it ?
Comment: Limit of that is going to $1$
Comment: Now, You have the answer
Comment: Exactly, so you have a product of something that converges towards $\int_{0}^{1}\frac{1}{1+3x^2}dx$ and something that converges towards 1.
Comment: So the part $\frac{1}{n}$ before integral is just because $\frac{b-a}{n} $? It is just size of intervals?
Answer: $$\frac{1}{n}\left(1+\frac{1}{n}\right)\sum...=\underbrace{\frac{1}{n}\sum...}_{\to \int...}+\underbrace{\frac{1}{n}\underbrace{\left(\frac{1}{n}\sum_{}...\right)}_{\to \int...}}_{\to 0}\to \int...$$
Answer: Just ignore the $1$ in the numerator as it is obviously less than $\infty$.
And put $\frac{1}{n}=dx$ & $\frac{k}{n}=x$
So your integration becomes $$\int_0^1\frac{dx}{3x^2+1}=\frac{tan^{-1}\sqrt{3}}{\sqrt{3}}$$
Answer to your question, what to do with $\frac{1}{n}(\frac{1}{n}+1)$ as $(\frac{1}{n}+1)\to1$ as $n\to\infty$.
Remember, when factors are in multiplication, partial limit can be applied.
Hope this will be helpful!

Calculate limit in use of integrals


Saber o total de valores no array php

Question: Preciso resolver uma situação com o array, parece ser simples mas não estou conseguindo!
Tenho o seguinte array:
$total = array("6.00","7.00","9.5","10","5.5","7.75","6.5","9");

Preciso saber a quantidade de valores menores que 7.
Não quero saber quais são e sim a quantidade?
Por exemplo:
sei que tem o 6.00 o 5.5 e o 6.5.
Quero que retorne total: 3 valores.
Answer: Mais um exemplo de como fazer utilizando array_filter (como o próprio nome já deixa bem claro. Uma função para filtrar determinada informação no array) e count (utilizada para contar elementos), criei a função addLessThanSeven(float $val) que recebe um valor do tipo float, e por fim retorna os valores menores que 7 (não fiz nenhum tratamento de erro).
Exemplo:
<?php

    $total = array("6.00", "7.00", "9.5", "10", "5.5", "7.75", "6.5", "9");

    function addLessThanSeven(float $val)
    {
        return ($val < 7);
    }

    echo count(array_filter($total, "addLessThanSeven"));
    //3

Answer: Não precisa usar aspas nos números. Se usar, eles serão tratados como string.
$total = array(6.00,7.00,9.5,10,5.5,7.75,6.5,9);

$contador = 0;

foreach ($total as $item)
{
    if($item < 7)
    {
        $contador++;
    }
}

echo "Quantidade de números menores que 7: ".$contador;

Answer: Utilize array_reduce, que vai reduzir esses array em um outro tipo de dado com um valor especifico, no caso a redução é a contagem de itens que são menores que 7, criando uma função sum($a, $b) onde o valor da variável $a é o inicial e o valor de $b e o valor de cada posição desse array, verificando com um ternário se esse valor é menor do que 7 e se for satisfeito retorna o valor 1 para acrescimo ou valor 0, exemplo:
<?php

    $total = array("6.00","7.00","9.5","10","5.5","7.75","6.5","9");

    function sum($a, $b)
    {
        $a += (float)$b < 7 ? 1 : 0;
        return $a;
    }

    echo array_reduce($total, 'sum', 0);

Saber o total de valores no array php


Is this a predictable process?

Question:
Let $(\Omega, \mathcal{F},\mathbb{P})$ be a probability space. Denote by $t_n= n \Delta t$ a partition of $[0,T]$ and let $a_n \in \mathcal{F}_{t_n}$ be in $L^2(\Omega)$. Is the process $$X(t) = a_0\mathbb{1}_{[0,\Delta t)}+\sum^{N-1}_{n=1}a_{n-1}\mathbb{1}_{[n\Delta t, (n+1)\Delta t)}(t)$$ predictable?

$X(t)$ is right continuous and adapted, I don't see how to show its predictability.
Comment: This process doesn't look like it's even well-defined: both endpoints on that indicator function are closed.  What is $X(n\Delta t)$ or $X((n+1)\Delta t)$?
Comment: I have fixed it now.
Comment: @user6247850 I have edited it further, can you take a look now.
Answer: Yes, this is a predictable process.  For simplicity, we can consider the process $$X(t) = a1_{[s,T)}(t)$$ with $a \in \mathcal F_u$ for some $u < s$.  The predictable sigma algebra is generated by the continuous and adapted processes, so we will show $X$ is the pointwise limit of such processes.  Let $X^n(t) = a$ for $s \le t \le T - \frac 1n$, $X^n(t) = 0$ for $t < s - \frac 1n$ or $t \ge T$, and linearly interpolate between.  Then $X^n$ is continuous and adapted (as long as $u < s-\frac 1n$) and hence predictable, and converges pointwise to $X$, so $X$ is predictable.

Is this a predictable process?


get subclasses of a class OwlApi

Question: There is a way to find all named subclasses of a class without using a reasoner  for (OWLClass child : reasoner.getSubClasses(clazz, true).getFlattened() ) and without doing inference just by using axioms? Thank you
Answer: Using owl-api the ontology can be query to get all subClasses axioms.
Then you filter the result to retain only the named classes.
for (final OWLSubClassOfAxiom subClasse : ontology.getAxioms(AxiomType.SUBCLASS_OF))
{
    if (subClasse.getSuperClass() instanceof OWLClass
         && subClasse.getSubClass() instanceof OWLClass)
    {
        System.out.println(subClasse.getSubClass()
             + " extends " + subClasse.getSuperClass());
    }
}

Using Jena, you can list statement, add look for the "subClassOf" predicate, then as in owl-api you filter to get only non-annoymous objects.
final StmtIterator it = model.listStatements();
while (it.hasNext())
{
    final Statement s = it.next();
    if (s.getPredicate().equals(RDFS.subClassOf) && !s.getObject().isAnon())
            System.out.println(s.getSubject() + " extends " + s.getObject());
}

Comment: Thank you for the solution. however, it doesn't take into account relationships between classes and the first level class which is Thing. I understand, because in the owl.file there is no reference to show that those classes are subclasses of Thing it's done by the reasoner. There is no way to take them into account without using reasoner? I hope that ...
Comment: Make a set(A) of all sub classes seen that aren't sub classes of Thing.
Take the set(B) of all classes in signature and remove classes from(A).
The remaining of B should be an additionnal set of classes that are direcly subClasses of Thing.

get subclasses of a class OwlApi


Fill hash with checkboxlist (key) and dropdown (value) doesn't work? In ruby on rails

Question: I am implementing a checkboxlist with some items and next to each item there is a dropdown. These two things hang together and what i want to do is to create a hash, the key is the value or id from the checked item, the value is the value of the selected item in the dropdown.
This is what i have:
view:
<% @groups.each do |group| %>

        <%= check_box_tag 'group[]', group.id -%>
        <%= h group -%>
        <%= select_tag 'role_group[group.id][]', options_for_select(@roles) %>

  <% end %>

controller:
@selected_groups = Group.find(params[:group])
  @selected_roles = params[:role_group]

  @roleskeys = @selected_roles.keys

Now if i show @roleskeys , he always just say group.id. So this is not correct, this is just the string. Somebody who knows what i am doing wrong?
Answer: Try it this way:
<% @groups.each do |group| %>

        <%= check_box_tag 'group[][id]', group.id -%>
        <%= h group -%>
        <%= select_tag 'group[][group_id]', options_for_select(@roles) %>

<% end %>

You will have both together in a Hash.
Comment: Nope this doesn't work, i get the error: Couldn't find all Groups with IDs ('--- !map:HashWithIndifferentAccess \nid: \"9\"\ngroup_id: Anonymous\n','--- !map:HashWithIndifferentAccess \ngroup_id: Manager\n') (found 0 results, but was looking for 2)
Do you know what i am doing wrong now?
Comment: Pleas post `paramas.inspect` of the relevant page
Comment: My parameters are: {"time_tracking"=>"true",
 "group"=>[{"id"=>"9",
 "group_id"=>"Developer"},
 {"id"=>"10",
 "group_id"=>"Developer"}],
 "repository"=>"true",
 "commit"=>"Submit",
 "issue_tracking"=>"true"
}
Comment: Ok, i've think i found it.Now i have a hash, but if i don't check the checkbox, there is an error: because there is a value in 1 item, but not the key because it is not selected. WHat do I have to do against that? Thanks
Comment: My parameters then look like : {"group"=>[{"id"=>"9", "group_id"=>"Anonymous"}, {"group_id"=>"Non member"}] and the params[:group] is an array :s ?
Comment: I think your problem is the content of `@roles` would you please post the content of `@roles`. The Problem seems to be that you want the `group_id` but you arent setting the id as the `value` you set the displayed text as `value`.
Comment: roles is filled by models, of class Role, i fill it by @roles = Role.find :all, :order => 'builtin, position'. The output of roles = Manager
Developer
Reporter
Non member
Anonymous
Comment: Why dont you use `collection_select` when there is a relation to a model?
Comment: Does this work in my case? i will give it a try, and try to work that out.
Comment: This doesn't work? do you have other solutions? thanks a lot already!