In the key of F major, what would happen if someone labeled a B#?

Question: I know you could just notate it as C or B natural, depending on the intention...
But theoretically, is this possible or is that confusing?
would it transform the note into B# (until the rest of the measure)? cancel out the original key leading to B natural? Or nothing because it is just not allowed/meaningless or too confusing?
Comment: This is a slightly confused question, as B# is enharmonically the same as C natural, but not B natural!
Comment: It would simply change the key until the next measure cancels it out. There's a term for this... Let me find it...
Comment: Ok, it's unclear to me if what you mean by "B#" is to raise the Bb note by a half a step (Bnat) or to make it a B#, because you seem to reference both in your question.
Answer: Accidentals have absolute power... Over a single measure. If you threw a B# in a key signature where the B is normally flat, it would change it to a B#. No questions asked. You can even change the key signature by using accidentals. This is called transposition... I think.
So if you wanted your Bb to become a Bnat, you would write Bnat into the measure as an accidental, or, as the case may be, change the key signature. But a sharp or flat does not change the pitch of the note relative to what it was before; it is played as written. If you see a B# in any key signature, you play a B#. End of story.
Hope this helps, and please ask questions should you have them or if my explanation is stupid.
Answer: In F major, all Bs are played initially as Bb, and Cs as Cs. However,there could be a requirement to write a B#, for example, if there was a modulation using , say, E+. E augmented needs to have a B#, but not written as a C. Some folks would notate it as a C, which of course would sound the same, but technically it should be written as B#, which tells players who understand that the harmony containd an augmented 5th.
Answer: It seems like you're asking whether the sharp "sharpens" a B flat to a B natural, or actually makes a B sharp.  The answer is that, yes, a B# in F major would be the same enharmonic note as a C natural.  So for the that bar, all written B's would be played as Cs, effectively.
The important question though is would this ever happen?  And the answer is rarely if ever.  You are raising the 4th degree of the scale by 2 semitones; to give you an analogy, all of the following are equivalent:

A B♯ in F major
An F (double sharp) in C major
An E♯ in B♭ major
A C in G major

Which way we write a given note (for example B♭ or A♯) is called a "spelling".  Why would we ever use these spellings?

Borrowing from C♯ minor, minor scales can have a sharp 7. This would be very unlikely in the key of F. (this would be like borrowing from G♯ minor in the key of C major)

Borrowing from C♯ major.  Why would you do this when you could write it as D flat major, and have a C♮?

An E augmented chord has a raised fifth, not a flat sixth, so would be written as E G♯ B♯ (not E G# C).  Quite why you would find this in a piece in F major though, I can't see.  It would be like seeing an F♯ sharp augmented (F♯ A♯ C) in the key of G... Why?

TL;DR B♯ would be a C♮.  While it is technically possible to raise the fourth degree of a scale by 2 notes, there are few, if any circumstances where you would want to do this, and so you are unlikely to ever encounter a B♯ in F major.
Comment: 'E aug has a raised fifth' - that's the same as a sharpened fifth, perfect + a semitone,  isn't it?
Comment: yeah I meant to say not a flat 6, just a brain-typo I guess
Answer: B# is B#, in any surroundings.  It would persist until the next barline, like any accidental.
The likelihood of B# being a useful notation in an F major context is slim though.
Comment: You could certainly devise a justifiable reason for writing B# C# before a D minor chord, and that chord could obviously have a harmonic function in F major.
Comment: Oh it's extremely unlikely but great fun trying to justify. I don't think you read the latest edit of this post, missing the point perhaps?
Answer: This is a really fun if slightly useless question. Let's consider inder what REASONABLE circumstances a B# would appear in a piece starting off (because thats the ONLY thing a key signature means) in F Major. First let's look at which scales contain B# (that aren't unreasonably daft like for example D# Major):
C# Major
that's it for majors
and then non-daft Minor scales:
C# Minor Let's say Melodic
D# Minor Melodic
A# Minor ?
Now let's hunt for some (but not all) USEFUL chords properly containing B# when written correctly in the key:
(You know what, to hell with A# Minor.)

C# Major = C#Ma7 E#m7 G#7 B#m7b5 (=D#m6)

C# Minor Melodic = B# ALT, F#7b5

D# Minor Melodic = B#m7 b5

Can some of them be ruled out ? I would say so! E#m7 C#Ma7, B#m7b5, B# ALT,  and B#7 b5 are revoltingly preposterous from a publishing point of view, even IF you are Charlie Parker.
But, HELLO! that leaves G#7 and you would probably spell that properly.
There you go! Now we'll just figure out a pretty progression and ...

F Ma7 | F Ma7 b5 | A Ma7 | G# Ma7 b5 | G Sus Ma7 | F# Ma7 b5 | F Ma7 |

Yeah, ok so I changed it. And I know G# Ma7 b5 is in D# Major after all. Daft key. Play all these in Root inversion. Here's how it works. You're not going to spell the A Ma7 with an A Flat are you, so you're going to spell the next chord properly, G#, B#, D, FX. After that the B# is PEDALLED thru all the chords, SO YOU CAN'T CHANGE IT, even the final FMa7 ends up being spelled with a B#! If I ever figure out how to score it on a staff on this site, I will. Till then use your imaginations.
So, I don't know what was the question? Oh yeah IF you ever saw a B# it might be in a progression somefink like the one above.
Enjoy!


Interpretation of quasi poisson regression with offset

Question: I am trying to find out how to correctly interpret the coefficients for a Poisson regression. My main outcome of interest is the number of antibiotics prescribed (count) or the rate of antibiotics prescribed (number of antibiotics prescribed divided by the total number of prescriptions in one calendar year). My main research question is looking at whether a practice's centrality measure is associated antibiotics prescribing. Covariates in my model include a GP dummy, Charlson comorbidity score. I have used the count as my outcome variable with the total number of prescriptions as an offset, and a quasi-Poisson specification to account for the overdispersion. My regression is as follows:
<code>log(Number of of antibiotics prescribed) =  β0+ β1×centrality
       measure+ β2×GP(dummy)+ β3×Charlsonscore +
       log(# of total medications prescribed)
</code>
Right now, I'm struggling to interpret the coefficients of my model. Would 1 unit increase in centrality measure equate to e^β1 in number of antibiotics prescribed?
Answer: Interpretation of coefficients in a Poisson rate regression fitted with quasi-likelihood is identical with interpretation when using maximum likelihood. The only thing that changes is the standard errors, the estimated coefficients are the same.
For interpretation of the coefficients, tat are answered at

Offset in Poisson regression
Can we associate poisson regression coefficients with the degree of harmfulness to the outcome?
and certainly more ...


Synchronising two mysql databases with a chron script

Question: I have two mysql databases which I would like to sync every couple of hours, I'm aware that my ISP offers cron scripts for this purpose but have no experience with cron can anyone point me to a decent resource for learning cron - I haven't had much luck searching.
Comment: Why do you synchronise? Is this for backup? Have you thought about doing master-slave replication?
Answer: cron is a mechanism for executing a script (or executable) at a regular time. The script can be written in any script language, so pick one you are familiar with. Shell script (e.g. bash) is often used, but you could use perl, python, PHP etc.
The wikipedia page has information about the cron format for specifying when the script will run: http://en.wikipedia.org/wiki/Cron
Your ISP may provide easy access via a control panel, or you may have to request them to setup the cron job. You'll need to find out from them exactly how to set it up.
Answer: Here is a concise, printable and to the point text document that covers all the basics.
A simple example of what you may be looking to have in your crontab:
<code>15 2 * * * /usr/bin/mysqldump –opt –all-databases -u root -pPASSWORD > /foo/bar/db-`date +%Y-%m-%d`.sql
</code>
That job, would run a backup task daily at 2:15 AM.
On a sidenote, if you just need this for regular backing up purposes and not actual duplication, I have come to love this script for quick and dirty db backups.
Good-luck.


Methods for finding the $k$ best regression predictors out of $n$ total predictors

Question: Suppose we want to find the $k=7$ best predictors out of a total of $30$ predictors in a linear regression, and that we use some metric like mean square error. Obviously fitting ${30\choose7} = 2035800$ combinations is tedious, and I am wondering what fast algorithms or methods exist for something like this. Thanks
Comment: What for? What problem are you trying to solve? You can just use all of them, don't you?
Comment: This was a question I was asked in an interview a few years ago and its been bothering me.
Answer:
Methods for finding the k best regression predictors out of n total
predictors

This sound like the best subset selection criteria. It is explained in Elements of Statistical Learning - Hastie Tibshirani and Friedman; Springer (2008).
An efficient algorithm — the leaps and bounds procedure (Furnival and Wilson, 1974)—makes this feasible for $k$ not too large, let me says no more than $40$.
I take this image from the cited book:

"The best-subset curve (red lower boundary in Figure 3.5) is necessarily decreasing, so cannot be used to select the subset size k. The question of how to choose k involves the tradeoff between bias and variance, along with the more subjective desire for parsimony. There are a number of criteria that one may use; typically we choose the smallest model that minimizes an estimate of the expected prediction error." pag 58.
Indeed to find the model with the smallest EPE is usually the main goal of predictive learning. In order to do that several criteria exist. Most of them are explained in this book. The algorithms for stepwise selection suggested in the Tim's comment stay among them.
Comment: Newer algorithms (see [Bertsimas et al. 2016](https://projecteuclid.org/journals/annals-of-statistics/volume-44/issue-2/Best-subset-selection-via-a-modern-optimization-lens/10.1214/15-AOS1388.full)) make best subset selection for $k$ in the 100s or even 1000s feasible.
Answer: Many types of model have equations that do not necessarily use all the predictors - i.e. they have built-in feature selection. These could be useful for this problem e.g. you could build a random forest model, specifying all 30 predictors using the ranger package in R. This works by training a number of decision trees and then these trees are averaged - the final model will only include the best predictors. Furthermore, once the model is fitted you can randomly shuffle each predictor's data one at a time - the difference between the MSE (or other evaluation criterion) before and after the shuffling gives a measure of each predictor's importance. This is because important variables will be affected by this random sampling, whereas unimportant predictors will show minor differences. So you could use these importance scores to find the k best predictors.
You could also use a function like dredge from the MuMIn package on a regression model - this automates the process of building all combinations of your predictors. You can then produce a weighted average of the regression coefficients, where the weights are based on the AICc score of each model. This method could be more useful if you're only interested in using linear regression, but I imagine it might take a while for a model with 30 predictors... in my experience the random forest method mentioned above will be much quicker.
Comment: Thank you, if I were to take the best predictors from a random forest, will they still be the best if I were to actually fit all the linear regression models? In other words, if a predictor collection is optimal (in terms of MSE) for random forest, is it optimal for linear regression as well?
Comment: Not necessarily - the best predictors for a linear regression may be different to the best predictors for a random forest. But in practise, there is likely to be a strong overlap.


Python Pip Install "Egg" Error (Pandas and other modules)

Question: Having issues installing some python modules (for example pandas) and also had some issues installing ipython notebook.
This error seems to have come up before for other folks (Python pip install fails: invalid command egg_info), but they were using Python 2.7, whereas I'm using 2.6 (default with my Mac OS 10.6). Also I tried easy_install -U setuptools and pip install --upgrade setuptools but neither worked.
This is the error I get for installing pandas:
Command python setup.py egg_info failed with error code 1 in /private/var/folders/Wm/WmmbYincEnuCrAMtGBudAk+++TM/-Tmp-/pip_build_AWal/pandas
Storing debug log for failure in /Users/AWal/Library/Logs/pip.log

For installing iPython, this is the error that I get:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/commands/install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/req.py", line 1435, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/req.py", line 671, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/req.py", line 901, in move_wheel_files
    pycompile=self.pycompile,
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/wheel.py", line 341, in move_wheel_files
    generated.extend(maker.make_multiple(['%s = %s' % kv for kv in console.items()]))
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/_vendor/distlib/scripts.py", line 316, in make_multiple
    filenames.extend(self.make(specification, options))
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/_vendor/distlib/scripts.py", line 305, in make
    self._make_script(entry, filenames, options=options)
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/_vendor/distlib/scripts.py", line 209, in _make_script
    self._write_script(scriptnames, shebang, script, filenames, ext)
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/_vendor/distlib/scripts.py", line 189, in _write_script
    self._fileop.write_binary_file(outname, script_bytes)
  File "/Library/Python/2.6/site-packages/pip-1.5.4-py2.6.egg/pip/_vendor/distlib/util.py", line 384, in write_binary_file
    with open(path, 'wb') as f:
IOError: [Errno 13] Permission denied: '/usr/local/bin/sphinx-apidoc'

Storing debug log for failure in /Users/AWal/Library/Logs/pip.log

Any thoughts on what to do?
UPDATE:
When I do sudo pip install pandas (another module that I can't install), I get a long long traceback with the end error message:
RuntimeError: Broken toolchain: cannot link a simple C program

And traceback (a sample of it of the 100 or so lines) looks like this:
Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/private/tmp/pip_build_root/pandas/setup.py", line 619, in <module>

    **setuptools_kwargs)

  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/core.py", line 113, in setup

  File "build/bdist.macosx-10.6-universal/egg/setuptools/dist.py", line 266, in __init__

  File "build/bdist.macosx-10.6-universal/egg/setuptools/dist.py", line 312, in fetch_build_eggs

  File "build/bdist.macosx-10.6-universal/egg/pkg_resources/__init__.py", line 753, in resolve

  File "build/bdist.macosx-10.6-universal/egg/pkg_resources/__init__.py", line 1005, in best_match

  File "build/bdist.macosx-10.6-universal/egg/pkg_resources/__init__.py", line 1017, in obtain

  File "build/bdist.macosx-10.6-universal/egg/setuptools/dist.py", line 379, in fetch_build_egg

  File "build/bdist.macosx-10.6-universal/egg/setuptools/command/easy_install.py", line 619, in easy_install

Comment: This might be related http://stackoverflow.com/questions/22388519/problems-with-pip-install-numpy-runtimeerror-broken-toolchain-cannot-link-a
Comment: @user3736169 Yes, you can just download it from the Mac App Store. Not 100% sure, but I think installing and opening it brings all the C-based compile tools to life.
Comment: try wiht admin right
Comment: @Hackaholic Tried sudo pip install pandas (another module that is not working), and I got the same error: Command python setup.py egg_info failed with error code 1 in /private/tmp/pip_build_root/pandas
Storing debug log for failure in /Users/AWal/Library/Logs/pip.log
Comment: Do you have XCode (and the tools it brings with itself) installed on your OS X?
Comment: @sthzg I don't have xCode that I could find. Is there a way to easily install it?
Answer: A permission Denied message would suggest you to run your command as root as suggested by  Hackaholic.
try sudo pip install YOUR_PACKAGE
You might also want to have a look at virtualenv to use a more isolated environment.
Under debian based distributions, you might install python-virtualenv package with apt-get install python-virtualenv.
Then create a virtual environment names whatever by typing virtualenv whatever (this will create a folder whatever inside your current folder.
Then cd to it and type bin/activate to enter this virtual environment.
From there try again your pip install command.
Might help
UPDATE :
You might try to use the suggested solutions available here
To sumup, try to add export ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future inside ~/.bash_profile
Comment: Basically, virtualenv is a good way to go. To install it on a mac (no ``apt-get``), you can follow this [post](http://jamie.curle.io/blog/installing-pip-virtualenv-and-virtualenvwrapper-on-os-x/). However, the update in the OP suggests that there might be an issue with the C compiler, which needs to be solved as well.
Comment: @sthzg I think its definitely an issue with the compiler as well. I just tried to command export ARCHFLAGS in the bash_profile as well as with a module (numpy this time, another module that doesn't install for me): sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install --upgrade numpy. And unfortunately, I got another error: Command /usr/bin/python -c "import setuptools, tokenize;__file__='/private/tmp/pip_build_root/numpy/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" etc....
Comment: @user3736169 in the previous [link i gave you](http://stackoverflow.com/questions/22388519/problems-with-pip-install-numpy-runtimeerror-broken-toolchain-cannot-link-a), some people explain that they had to just open XCode one type and accept the aggreement as this suggested : `I simply had to open XCode and accept the agreement and let it install the tools.` might help you too.
Answer: This question comes first in Google, but the second one actually worked:
pip install -U setuptools

pip install ez_setup


Multivariate analysis - creating a classification index

Question: I have the following problem I would like to tackle, I thought it would be useful to get some help for determining the best possible workflow.
I have data for some 200 biological observations. 1 of the measured parameters gives a pretty accurate description of the property I would like to evaluate.
4 other parameters I expect to give some information on this property, but are clearly also influenced by a number of other factors.
I would like to create an index (based on the observations I have) to classify future observations, where only the last 4 parameters will be measured.
(Additionally: for one of these parameters a number of observations is below the detection limit. What is the best way to go about this? Replace these values by DL/2?)
Answer: This appears to be a regression problem: You can use 4 "other" parameters as independent variables to predict 1 "pretty accurate" one as the dependent variable. The choice of regression method will depend on what you want to do with your index. Should it be easy to calculate the index without a computer? Then linear regression might be a good place to start.


Convergence of a sum to $e^{-1/2}$?

Question: I have this sum:
$\displaystyle \sum _{k=0}^{\frac{n}{2}-m} \frac{\left(-\frac{1}{2}\right)^k}{k!} \exp \left(\frac{3 (m+k)-2 (m+k)^2}{n}\right)$.
I have some suspicion that it converges to $e ^{-1/2}$ for all $ m=1,2....$ when $n \rightarrow \infty$; but failed to prove that. Any help?
Comment: That is a pretty big suspicion. Is there anything similar to this thing that converges to anything known? alternatively the a book on summation of series might already have this in it.
Answer: The statement is true.
Let $S_{-1} = 0$ and $S_n$ be the partial sum $\displaystyle\;\;\sum_{k=0}^{n}\frac{1}{k!}(-\frac12)^k\;\;$ for $n \ge 0$.
Let $\alpha_{n,k}$ be the double sequence defined by
$$\alpha_{n,k} = \begin{cases}
\exp\left(\frac{3(m+k)-2(m+k)^2}{n}\right), & 0 \le k \le \frac{n}{2} - m\\
\\
0, & k > \frac{n}{2} - m.
\end{cases}$$
For each $n$, the $n^{th}$ entry of the sequence we are interested in can be rewritten as
$$
\sum _{k=0}^{\frac{n}{2}-m} \frac{1}{k!}(-\frac12)^k \alpha_{n,k} =
\sum_{k=0}^{\infty} (S_k - S_{k-1})\alpha_{n,k} = \sum_{k=0}^{\infty} S_k (\alpha_{n,k} - \alpha_{n,k+1})$$
As long as $m > \frac14$, it is easy to check $a_{n,k}$ is a non-negative decreasing sequence in $k$ for fixed $n$.
We have:

For fixed $n$,
$\displaystyle\quad\sum_{k=0}^{\infty} |\alpha_{n,k} - \alpha_{n,k+1}| = \alpha_{n,0} =
e^{(3m-2m^2)/n} \le \max( e^{3m-2m^2}, 1 ) < \infty.$
$\displaystyle\lim_{n\to\infty} \sum_{k=0}^{\infty} (\alpha_{n,k} - \alpha_{n,k+1})
   =\lim_{n\to\infty} a_{n,0} = \lim_{n\to\infty} e^{(3m-2m^2)/n} = 1.$
For fixed $k$, $\displaystyle\quad\lim_{n\to\infty} (\alpha_{n,k}-\alpha_{n,k+1}) = 1 - 1 = 0.$

The double sequence $\alpha_{n,k}-\alpha_{n,k+1}$ satisfies the conditions for
Silverman-Toeplitz theorem and hence
$$\lim_{n\to\infty} \sum_{k=0}^{\infty}S_k(\alpha_{n,k}-\alpha_{n,k+1}) = \lim_{k\to\infty} S_k = \sum_{k=0}^{\infty} \frac{1}{k!}(-\frac12)^k = e^{-1/2}$$
Notes
Using Silverman-Toeplitz is not the most efficient way to prove the statement. There is a theorem

Dominated convergence theorem for series
Let $\beta_{n,k}$ be a double sequence and $b_k$, $d_k$ be two sequences such that

$\displaystyle \lim_{n\to\infty} \beta_{n,k} = b_k$ exists for every $k$.
For every $n$ and $k$, $|\beta_{n,k}| < d_k$ and $\displaystyle\;\sum_{k} d_k < \infty$.

i.e. the rows of the double sequence $\beta_{n,k}$ are dominated by a single
  non-negative sequence $d_k$ which has finite sum. Then
$$\sum_{k} b_k = \sum_{k} \lim_{n\to\infty} \beta_{n,k} = \lim_{n\to\infty} \sum_{k}\beta_{n,k}$$

One can verify
$$\begin{align}
\beta_{n,k} = & \frac{1}{k!}(-\frac12)^k \alpha_{n,k}\\
b_k = & \frac{1}{k!}(-\frac12)^k\\
d_k = & \frac{1}{k! 2^k} \max( e^{3m-2m^2}, 1 )
\end{align}
$$
satisfies above condition for "DCT for series" and the statement follows immediately.
I can't find any wiki page for this "DCT for series" but here is a proof I find online.
Comment: Thanks Achille! Very neat!
Comment: two comments:(1) Concerning the use of DCT, we considered $m$ as a given number. Don't we have to worry about the case of $m$ depending on $n$. For example $m=n/2$, in that case, it is not true anymore that $\beta_{n,k} \rightarrow b_k$ since $\beta_{n,k} \rightarrow 0$ . Recall that $k$ goes from $0$ to $n/2 -m$. (2) In defining $d_k$, is it $ \frac{1}{k! 2^k} \max( e^{2m-3m^2}, 1 )$ or $  \frac{(-1/2)^k}{k!} \max( e^{2m-3m^2}, 1 )$. Thanking you!
Comment: 1) If $m$ indeed depends on $n$, then this will become a different problem. However, that isn't the case and once $m$ is given, it only affect the definition of $\alpha_{n,k}$. 2) d_k needs to be a non-negative sequence and it is $\frac{1}{k!2^k} \max(e^{2m-3m^2},1)$. The situation is similar to that in [Lebesgue dominated convergence theorem](http://en.wikipedia.org/wiki/Dominated_convergence_theorem), you need a non-negative sequence/function to dominated others so that one can exchange the order of taking limit and summation/integration.
Comment: I thought it would not affect the reasoning, so I shared half of the problem then. Here  is my full problem $P\left[X=k\right]= \frac{{{\rm (}{{\frac{{\rm 1}}{{\rm 2}}}}{\rm )}}^k}{k{\rm !}}\sum^{\left\lfloor \frac{n}{{\rm 2}}{\rm -}k\right\rfloor }_{r{\rm =0}}{\frac{{\left({\rm -}{{\frac{{\rm 1}}{{\rm 2}}}}\right)}^r}{r{\rm !}}e^{\frac{3(r+k)-2{(r+k)}^2}{n}}}$. I want to show that $X$ converges to a Poisson (1/2). . I calculated the 8 first moments of $X$ and I found that all of them converge to those of a Poisson (1/2) hence my suspicion.
Comment: Find a typo in the $2^{nd}$ part of my answer, $d_k$ should be $\frac{1}{k! 2^k}\max(e^{3m-2m^2},1)$. About the problem of $X$ converges to Poisson(1/2). What we have shown is some sort of point-wise convergence. The proof can probably be extended to expectation value of any $f(X)$ where $f(x)$ falls to zero fast enough for large $x$. However, to really prove $X$ converge to Poisson(1/2), we need a proof of uniform convergence. I don't think there is any nice theorem one can directly use.

Convergence of a sum to $e^{-1/2}$?


Show that $\Vert Tx-Tp \Vert \leq \Vert x-p \Vert $, where $p=0.$

Question: Let $X = l_\infty$ (the space of sequences of real numbers which are bounded). Let $K=\{x\in l_\infty:\Vert x \Vert_\infty\leq 1\}.$ Defined \begin{align} T:& K\to K \\&x\mapsto Tx=(0,x^2_1,x^2_2,x^2_3,\cdots)\end{align}
I want to show that

$Tp=p$ if and only if $p=0;$
$\Vert Tx-Tp \Vert \leq \Vert x-p \Vert $, where $p=0.$

MY TRIAL
1.
\begin{align} Tp=p&\iff(0,p^2_1,p^2_2,p^2_3,\cdots)=(p_1,p_2,p_3,\cdots)\\ &\iff p_1=0,\;p_2=p^2_1,\;p_3=p^2_2,\;\cdots\\ &\iff p_n=0,\;\forall n\in \Bbb{N}\\ &\iff p=0\end{align}

Let $x,p\in K$ s.t. $p=0,$ then
\begin{align} \Vert Tx-Tp \Vert=\Vert (0,x^2_1,x^2_2,x^2_3,\cdots)-(0,0,0,0,\cdots)\Vert\end{align}
Honestly, I don't know what to do from here. Any help please?
Comment: If $p=0$ in 2.) then doesn't it mean that you merely want to prove $||Tx||\le ||x||$?
Comment: @BigbearZzz: Yes, that's it!
Answer: Your answer for 1 is maybe not very well written, but it looks correct to me.
For 2 notice that $Tp=p=0$, so you just need to show $\|Tx\|_\infty\leq\|x\|_\infty$. Maybe you can even find a closed form of $\|Tx\|_\infty$ in terms of $\|x\|_\infty$?
Comment: Thanks for number one but how do I find the closed form of $\Vert Tx \Vert_\infty$ in terms of $\Vert x \Vert_\infty$? I'm new into Functional Analysis.
Comment: Maybe first try to find $\|Tx\|_\infty$ for $x=(t,0,0,...)$ in terms of $t$.
Answer: The norm on $l^\infty$ is $||x||:=\sup_{n\in\Bbb N} |x_n|$.
Hint: If $|\lambda|\le 1$, then $|\lambda|^2\le |\lambda|$.
Comment: Sorry, I don't quite get the hint. Can you please, break it down?
Comment: Your set $K$ consists of elements $x=(x_1,x_2,\dots)$ such that $|x_i|\le 1$ (see the definition of the norm on $l^\infty$).


What clustering algorithm should I use for clusters spaced on a grid?

Question: I have some data sets of clusters of points arranged more or less on a regular grid. The data sets have these properties:

The data is two, three, or maybe rarely four-dimensional.
I know in advance how many rows/columns there are in each dimension (typically 3-6), but the number can differ by dimension.
Each grid point has a population centered at it (although I would be interested to know if anything changes if I know in advance that some won't).
The grid is pretty evenly aligned with the axes, but may be slightly skewed.
Rows/columns are pretty evenly spaced within each dimension, but the spacing can be different from one dimension to the next.
The number of points per cluster is similar, maybe up to a factor of two.
Each cluster is pretty close to a multivariate gaussian distribution.
The variance of each cluster is pretty constant when looking at one dimension at a time.
The co-variance of each cluster is pretty constant for each pair of dimensions, and typically takes on one of two values in a particular data set:

Occasionally, a low value for the entire first row/column of a dimension (see example)
Otherwise, a fairly high value

There is a fair amount of overlap between adjacent clusters when looking at a single dimension, but a lot less when you consider the dimensions are correlated.
There may be some outlier populations, but these will be much smaller than the ones I am trying to find.

Here's an example of one two-dimensional "slice" of one of these data sets (5x4), where clusters in the bottom row have less covariance than the others:

An overall goal is probably to classify at least 90% of data points correctly, and it would be much better to classify a point as an outlier than as belonging to the wrong cluster. Speed is a slight factor, I might need to process a group of 500 data sets each with 100 clusters of 500-1000 points and the grids might differ slightly between sets. Preferably that wouldn't take all day.
My idea at this point is to treat the data as a gaussian mixture model and use expectation maximization. I could either use the standard model and use what I know about the structure of the data to generate a good initial guess, which will hopefully converge reliably, or I could impose some sort of prior on the cluster means and variances. Looking for other opinions through.
Answer: Use Gaussian Mixture Modeling.
You can make it much faster and reliable if you initialize it with a reasonable estimate of where the clusters are, and e.g. by providing an initial covariance matrix.
That data looks well-behaved enough so that EM should just work, so try it.
It also is an option to simply first cut your data set into the observed pieces, then model each grid cell separately.
Comment: Thanks, this is how I've been going about it. I've been getting a guess on the grid by fitting 1D GMMs on each dimension separately, then using those fits to initialize the means, variances, and weights of the clusters in the full space. I'm getting good fits on maybe 93 or 94 of 96 populations in a 6x4x4 grid. From here I think I could get better results by using more data points (requires some optimizations for speed) or initializing clusters with some positive covariance. When I figure it out I'll post the exact tweaks I had to make and mark this as the correct answer.
Comment: Haven't had a chance to optimize further, but I'm going to go ahead and mark this as correct as I'm already getting close to optimal results. People seem to be interested in this question so I'll post additional important optimizations here soon after I've done a bit more exploring.


Jsonparsing error with @JsonIgnore on Hibernate

Question: I get this exception when Jackson tries to parse my data to Json:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: packagename.Thing.Stuffs, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->packagename.Stuff["thing"]->packagename.Thing["stuffs"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: packagename.Thing.Stuffs, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->packagename.Stuff["thing"]->packagename.Thing["stuffs"])

I have the following entities(names have been replaced with Stuff and Thing):
Stuff:
@Entity
@Table(name = "stuff")
@SQLDelete(sql = "UPDATE stuff SET deleted = 1 WHERE id = ?")
@Where(clause = "deleted = 0")
public class Stuff implements Serializable {

    private Long id;
    private Thing thing;
    private String stuffName;

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue
    public Long getId() {
        return this.id;
    }

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, optional = false)
    @JoinColumn(name = "thing_id", nullable = false)
    public Thing getThing() {
        return thing;
    }

    @Transient
    public String getStuffName() {
        return stuffName;
    }

    // Setters and constructor(s) omitted

}

Thing:
@Entity
@Table(name = "thing")
public class Thing implements Serializable {

    private Long id;
    private String name;
    private List<Stuff> stuffs;

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @GeneratedValue
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name", unique = false, nullable = false, length = 45)
    public String getName() {
        return this.name;
    }

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "thing_id")
    @JsonIgnore
    public List<Stuffs> getStuffs() {
        return stuffs;
    }

    // Setters and constructor(s) omitted
}

I have been googling for this type of error. It seems that this is happening when the json-parser is trying to parse objects that aren't loaded due to lazy-load. Lazy load seems to be on by default. I'd like to avoid setting everything to eager so I put @JsonIgnore instead. Changes nothing for me. Help would be greatly appreciated, this is driving me nuts.
EDIT:
Adding @JsonIgnore and @Eager in both classes gives me another problem. An exception that is looking like this:
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exception java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

Searching for that on stackoverflow gives me this: Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed)
The link is basically saying that I should add @JsonIgnore, and I have already done that.
Comment: My guess is that you should add the `@JsonIgnore` on the `getThing` method in the `Stuff` class.
Comment: Tried it and updated question.
Comment: @why_vincent What version of Jackson are you using?
Comment: 1.9.10 , might that cause the problem?
Comment: @why_vincent Yes kind of.
Comment: Instead of the `@JsonIgnore` you can try to add the `@JsonBackReference       ` on `getThing` and `@JsonManagedReference` on `getStuffs` see [this blog](http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion) for a more detailed explanation.
Answer: From comments you said that you are using Jackson 1.9.10, which will allow you to use the new syntax of @JsonProperty annotation by adding READ_ONLY and WRITE_ONLY property for access type.
So you can use :
@JsonProperty(access = Access.WRITE_ONLY)

With your field definition.
For further details you can check Only using @JsonIgnore during serialization, but not deserialization discussion.
Note:
With older cersions you could just use @JsonIgnore on class member getter and @JsonProperty on its setter.
Answer: Maybe your problem caused by wrong mapping, you shouldn't use @JoinColumn for @OneToMany relation in Thing entity, you need to add mappedBy = "thing" as parameter to specify relationship correctly.


Set $pageName globally in pagination Laravel

Question: We can use a custom $customPageName in laravel's paginate() function's 3rd parameter.By that we can use site.com/url?$customPageName=n . Is there any way we can set the custom page name globally ? In AppServiceProvider or somewhere like that?So we don't need to define every time?
Answer: There are many possible solution. I give you two that IMO are the best:
1: Create a trait and use it in every component
I love to use traits, they avoid a lot of redundancy and can be easily invoked everywhere:
trait PaginationTrait
{
    const PAGE_NAME = 'your_global_value'

    public function paginate($query, $perPage, $columns = [''], $page_name = null)
    {
        return $query->paginate($perPage, $columns, $page_name ?: self::PAGE_NAME);
    }
}

This solution requires anyway that you pass as input a Builder instance, and may cause a logic problem. So let's move to the next solution
2: Config files
Simply set a variable in an existing config file or in a new config file:
// Example: app.php

return [
    // [...]

    'page_name' => 'your_custom_value' // or env('APP_PAGE_NAME', 'your_custom_value'),

    // [...]
];

And in your controller you can retrive the value as follows:

    public function index() {
         // [...]

         $result = MyModel::paginate($per_page, $columns, config('app.page_name'));
    }

3: App\Http\Controllers\Controller
The third solution is the easiest. Set a constant in your App\Http\Controllers\Controller class (as I wrote for the trait part) and by the OOP rules, it will be inherited to all your controllers:
class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    const PAGE_NAME = 'your_global_value';
}

And in your controller:

    public function index() {
         // [...]

         $result = MyModel::paginate($per_page, $columns, self::PAGE_NAME);
    }

In my opinion, if you simply have to set this global variable, solution 2 and 3 are the best... If you have to create a custom pagination logic, then I think that creating a specific Trait or Class is a good choice
Comment: Fot me solution 1 is the best.I'm using service repository pattern .So i just need to set a wrapper in the base repository.Thank you for your solution
Comment: Glad to hear that. If you think that my answer is correct, feel free to accept it


Convert Android Widget to an App with a Widget

Question: I have an Android widget (it is shown in the list of widgets on the phone, and is not shown in the list of apps). It has a button which launches a pop-up activity.
Now I would like to change the widget so, that it would be both an app (which will be shown in the list of apps on the phone) which will start from that pop-up activity, and a widget (the same as before) which will launch the app (which will consist of that pop-up activity, but will be displayed and treated like an app).
What changes should I make in my manifest file to make the changes described above?
Below is my manifest file:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Light.NoTitleBar" >
    <receiver android:name="myapp.myapp.MyWidgetProvider" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/demo_widget_provider" />
    </receiver>
    <receiver
        android:name="myapp.myapp.MyWidgetIntentReceiver"
        android:label="widgetBroadcastReceiver" >
        <intent-filter>
            <action android:name="GET_HELP" />
            <action android:name="CHANGE_PICTURE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/demo_widget_provider" />
    </receiver>

    <activity
        android:name="myapp.myapp.MainActivity"
        android:label="@string/title_activity_main" >
    </activity>
    <activity
        android:name="myapp.myapp.CardsChoice"
        android:label="@string/title_activity_choice" >
    </activity>
</application>

Answer: The solution was rather simple: I just needed to add a Launcher Activity to the app.
This operation consists of 2 steps:

Adding activity definition as a launcher activity to the Manifest
Example:
<activity
    android:name="package.package"
    android:label="@string/title_activity_main"
    android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>

Adding code of the activity (and the layout) to the app.
It is about an class MainActivity and a layout file activity_main.xml


O que é a Pesquisa Operacional no contexto da Computação?

Question: Na matéria de Programação Linear da minha faculdade, o professor cita um termo chamado Pesquisa Operacional no qual usa certos algoritmos para solucionar determinados tipos de problemas. No entanto, eu fiquei muito confuso a respeito do que é a Pesquisa Operacional e o que ela propõe no contexto da computação e gostaria de ter minhas dúvidas esclarecidas.
Dúvidas

O que é Pesquisa Operacional no contexto da computação?
Quais tipos de algoritmos são usados?
Quais são os problemas que esses algoritmos propõe-se a resolver?
Comment: (1) otimização (2) buscas (3) problemas de contagem/otimização ; depois eu elaboro uma resposta completa
Comment: Não sabia que tinha diferença na computação :P
Answer: Formalmente, pesquisa operacional é a busca por um ponto no domínio de uma função de modo a minimizar seu valor.

Problemas de maximizar uma função f(x) pode ser tratada como minimizar a função g(x) = -f(x)

Você mencionou Programação Linear, isso é bom. Dá um ótimo ponto de entrada. Sem falar que fica fácil explorar alguns dos conceitos mais técnicos.
Programação Linear
Para quem não conhece, a Programação Linear é a disciplina dentro da Pesquisa Operacional que procura o melhor resultado possível de um objetivo dado restrições. Mas não é qualquer função objetiva, nem qualquer restrição, que fazem parte da Programação Linear. Apenas funções lineares são permitidas.
A função objetiva é da forma F(X) = C . X, onde C é um vetor de constantes, X é o vetor de variáveis e C . X é o produto escalar entre os dois vetores.
As restrições podem ser fornecidas de maneira individual:
 A1 . X <= r1

onde A1 é um vetor e r1 é o valor escalar que a multiplicação deve satisfazer. Cada restrição tem sua própria inequação.
Mas também pode ser fornecida na forma de multiplicação de matriz por vetor:
A * X <= r

onde A é a matriz que tem a quantidade de linhas igual à quantidade de restrições (lembra do vetor A1? ele é a primeira linha da matriz A) e r é o valor máximo de cada restrição.
Quando você tem uma inequação de valor mínimo (restrição na forma Bi . X >= ri) você pode transformar em valor máximo (para Ai = - Bi  a restrição e equivalente a Ai . X <= -ri). Se você tem uma igualdade, você pode transformar em duas inequações:
B . X = r
(Idêntico à...)
B . X <= r
B . X >= r

Não lembro como funciona para inequações não inclusivas, como Ai . X < ri.
Se tivermos apenas duas variáveis, o domínio seria um subconjunto do plano. Cada restrição corta o plano no meio. O domínio da função (considerando as restrições) é a interseção entre todos os semi-planos delimitados pelas restrições.
Por exemplo, com 2 variáveis e 4 variáveis, poderíamos ter esse domínio (marcado em vermelho):

Cada restrição é uma reta no caso de um problema de Programação Linear, as retas pequenas perpendiculares representam qual o semi-plano válido.
No caso das restrições lineares, essa interseção resultante será convexa, ela não terá partes "cavadas". Devido à sua convenxidade, e devido à função objetiva ser linear, a solução vai estar em um dos vértices dessa estrutura convexa. Acontece de ter um espaço solução de mesmo valor objetivo no caso de uma das faces estar perpendicular à direção de crescimento da função objetiva, isso implica que todos os pontos dessa face são ótimos também.
Um dos algoritmos usados em Programação Linear é o Simplex de Dantzig, normalmente referido apenas como método Simplex. Em termos geométricos, ele pegaria o espaço domínio (um hiper-sólido convexo) e, a partir de um vértice, caminha na aresta que provê o melhor resultado da função objetiva (C . ri mínimo para problemas de minimização, se não me engano) e caminha até o outro vértice dessa aresta, até que chega em um vértice cujas arestas, todas elas, oferecem apenas piora da função objetiva. Isso significa que o algoritmo encontrou um ótimo local (mais sobre isso mais tarde). Como o espaço solução é convexo e a função objetiva é linear, o ótimo local também é ótimo global.
A própria Wikipedia traz um exemplo do "passeio" dos vértices sobre o hiper-sólido domínio:

A propósito, no caso geral, o método Simplex não é polinomial, existem hiper-sólidos construídos especialmente de modo que o Simplex demore um tempo exponencial de processamento. Mas o problema contém algoritmos que o resolvem em tempo polinomial, com estratégias distintas. Um deles é o algoritmo dos elipsóides, navegando pelos pontos interiores do espaço solução.

Programação Linear Inteira
Uma variação da Programação Linear é a Programação Linear Inteira (PLI). Ela é inteira porque a solução precisa estar nos inteiros. Isto é, para todo X é um vetor de inteiros.
O método Simplex trabalha naturalmente com divisões para fazer as navegações de vértice para vértice, então é de se esperar que seus resultados sejam racionais não inteiros na maior parte das vezes. Mas isso não o impede de ser usado para auxiliar em problemas de PLI.
Uma das estratégias para se encontrar a solução exata é:

rode o método Simplex e ache a melhor solução racional
fixe uma das variáveis

são feitas duas fixações, uma com o valor truncado e outro arredondado para cima
você pode começar fixando a variável cujo quociente na função objetiva seja mais significativo

ao fixar uma das variáveis, ela deve ser considerada constante
rode este mesmo algoritmo novamente, para cada valor fixado distinto determinado no segundo passo
a recursão chega ao fim quando todas as variáveis estão com valores inteiros fixados
a resposta é a recursão que chegou no fim com o melhor valor da função objetiva possível

Não vou demonstrar, mas esse algoritmo executa o(2^size(X)) recursões. A versão de decisão dos problemas tratados pela PLI se encaixa em NP, portanto este é um problema muito mais difícil que um problema de Programação Linear.

existem problemas mistos de Programação Linear é PLI, então a solução para esse problema misto é só fazer as fixações para inteiro das variáveis inteiras, da parte PLI do problema

O problema do caixeiro viajante (devidamente mencionado na resposta do @Pagotti) pode ser facilmente convertido em um problema de PLI. Sejam os custos de atravessar da cidade A para a cidade B a função objetiva. Cada aresta constitui uma variável do problema. As restrições são:

cada aresta só pode ser trafegada ou ignorada
seja x_ab a variável que representa a aresta direcionada de a-->b, se ela for ignorada, ela vale 0; se ela for trafegada, vale 1; portanto, 0 <= x_ab <= 1
só se pode passar em uma cidade uma única vez, e ela precisa ser visitada (considerando que o caixeiro viajante volte ao ponto original)
somatório de x_ib para chegar em b (a partir de uma cidade incidente i qualquer) tem de ser 1
toda cidade se precise entrar e sair (considerando que o caixeiro viajante volte ao ponto original)
somatório de x_aj para cidade de destino j qualquer igual a somatório de x_ia para cidade de origem i qualquer, para toda e qualquer cidade a

Programação não Linear
Quando se está em Programação não Linear, existem duas coisas que se devem levar em consideração:

se a função objetiva é não linear
se as restrições são não lineares

Também pode existir o caso das restrições serem lineares em um intervalo, mas serem não limitantes em outros, portanto não contínuas. Tanto restrições não lineares típicas como as não lineares por não continuidade podem deixar o espaço solução côncavo.

Restrições não lineares poderiam ocasionalmente gerar espaços soluções de faces que não são pedaços de hiper-planos, mas ainda assim um hiper-sólido convexo. Nesse caso ainda seria possível ir atrás do melhor ponto dessa face para a função objetiva em questão.

Com essas restrições novas e a possibilidade, a possibilidade de concavidade nos traz um problema adicional para ir atrás das soluções: os ótimos locais.
Em um problema de minimização, você pode acabar achando uma solução de tal modo que, ao tentar distanciar-se, você não consiga melhorar a função objetiva. Nesse caso, você está em um vale. Talvez exista algo ainda melhor, mas para isso você teria de "subir a colina" (hill climbing) para verificar.
Tome como exemplo essa função de quarto grau, tirada da Wikipedia:

Imagina que ela é sua função objetiva. Suas restrições são -10 <= x <= 10. Se você encontrou o mínimo local em torno de +2, você precisaria subir a colina em direção ao 0 para encontrar o outro mínimo local, por volta de -3.
Estratégias para resolver problemas de Programação não Linear devem levar em conta o hill climbing.
É bastante comum atacar esses problemas com heurísticas que trazem respostas boas o suficiente. Uma heurística de hill climbing é a têmpera simulada, que tem uma possibilidade de subir colunas que diminui com a queda da "temperatura" do sistema (essa temperatura por sua vez cai com o passar do tempo).
E, sim, existe a variação inteira desses problemas. E, não, eu não sei como aplicar para atender essa restrição da solução inteira.
Muitos objetivos
Pensou que tinha acabado? Pensou errado!, cara!
Além dos problemas lineares, lineares inteiros e dos não lineares, também temos problemas com múltiplas funções objetivas.
O mais interessante dessas funções com muitos objetivos é que existem soluções codominantes. Eu posso alterar minha solução de tal modo que eu aumento mais os lucros do caixeiro, porém torno a jornada mais demorada (considerando dinheiro como objetivo a maximizar e tempo objetivo a minimizar). Como o computador deveria tratar esses problemas? Qual resposta ele deveria dar? O senso comum diz que deveria ser algo que se assemelhe ao Conjunto de Pareto:

Isso significa que quantos mais pontos que não são objetivamente melhor (em todos os objetivos) entre si obtivermos, melhor será para um gestor escolher uma solução.
Aqui meta-heurísticas capazes de trabalhar com muitos objetivos são usadas para tentar obter esse conjunto resposta:

MOSA (têmpera simulada multi-objetiva)
algoritmos genéticos
formigas (existe uma variação com aranha que come as formigas que tentam fazer hill climbing sem muito sucesso)
abelhas

Nunca vi (mas isso não quer dizer que não haja) redes neurais artificiais para resolver esses problemas. Também não conheço agentes com aprendizado que retornem um conjunto de Pareto como resposta (vale a mesma observação anterior). Para ambos, só conheço estratégias que retornam uma solução.
Respostas diretas tl;dr

Pesquisa operacional trata de pesquisar qual a melhor solução para determinada operação (ou melhores soluções)
Diversos algoritmos podem ser usados, dependendo do problema

Método Simplex/Método dos elipsóides para problemas de Programação Linear
Recursão de métodos de Programação Linear com fixação de valores inteiros para Programação Linear Inteira
Heurísticas e meta-heurísticas para Programação não Linear e para problemas multi-objetivos

têmpera simulada, formigas, abelhas, algoritmos genéticos, entre outros

Ela é usada para resolver problemas de otimização, não problemas de decisão

Exemplos
Ok, a resposta está bem abstrata, fala da característica de cada problema (linear, linear inteiro e não linear), algoritmos de resolução, características algébricas dos problemas... mas mostrar um problema que é bom, Jefferson, você não fez, né?
Pois bem, a intenção dessa seção e suas divisões é fornecer exemplos até bater no limite de 30.000 caracteres (ok, exagero meu, não precisa tanto).
PUC-Rio: cadeiras e mesas

Extraído dos slides disponíveis ao público: http://www-di.inf.puc-rio.br/~laber/LP2010-2.pdf

Você ganha 45 dinheiros a cada cadeira vendida, e ganha 80 dinheiros a cada mesa vendida. Você quer maximizar seus lucros. Só botar toda a mão de obra para ficar nas mesas, correto?
Claro que não! Olhando só os lucros por item, eu não posso afirmar isso. Depende do quanto eu tenho disponível para transformar em cadeiras e do quanto eu tenho disponível para transformar em mesas.
No caso, eu tenho dois fatores limitantes:

tempo, só 450h de trabalho
tábuas, só tenho 400 no estoque

Para criar uma mesa, gastaria 20 tábuas e 15h. Já para uma cadeira, 5 tábuas e 10h.
Portanto, eu tenho de satisfazer as seguintes condições:
horas de trabalho:  10 * cadeiras + 15 * mesas <= 450
tábuas: 5 * cadeiras + 20 * mesas <= 400

E meu objetivo é maximizar o lucro:
objetivo: max 45 * cadeiras + 80 * mesas

É interessante perceber que 21 mesas está fora do espaço solução porque ultrapassa a minha restrição de tábuas (precisaria de 420 tábuas para 21 mesas). Você pode ver o espaço solução no slide 6 do PDF do material de origem:

Acreditando que a resposta no slide está correta, a resposta ótima é:
cadeiras = 24, mesas = 14

Variação do problema anterior: lenhador
Imagine que você tem exatamente a mesma situação anterior. Mas agora, agora você tem acesso a um lenhador. Ele consegue fornecer novas tábuas para você, consumindo das suas horas de trabalho. Ele também tem um custo para te entregar madeira nova.
Como se dá isso? Bem, sua fábrica de móveis é na Polônia em uma floresta de pinheiros e seus trabalhadores tem essa cara:

No caso, justificamos as horas gastas. E o dinheiro gasto? Seria com o desgaste dos machados de precisão que eles usam para cortar as árvores.
Como eu estou tentando derivar o problema aqui, não vou tentar colocar números adequados para acharmos a reposta. Digamos que cada 3h na floresta gere 50 tábuas ao custo de 10 dinheiros. Isso modificaria a função objetiva e as restrições também. Normalmente, se apresenta primeiro a função objetiva e só então as restrições, então vou apresentar dessa maneira o problema:
max: cadeiras * 45 + mesas * 80 - lenhadores * 10

Restrições:

horas de trabalho:  10 * cadeiras + 15 * mesas + 3 * lenhadores <= 450
tábuas: 5 * cadeiras + 20 * mesas <= 400 + 50 * lenhadores

Eu posso reescrever a restrição de tábuas assim:
5 * cadeiras + 20 * mesas - 50 * lenhadores <= 400

Como usar de lenhadores implica um custo, eu sinceramente não sei nem se vale a pena usá-los. Eu acho que sim, mas não estou com nenhum programa para fazer o cálculo. Note também que agora o espaço solução tem 3 dimensões, portanto seria um sólido geométrico.
Alocando funcionários a máquinas
Esse é um problema para ser atacado com programação linear. A pergunta linkada no título mostra a versão geral do problema, mais tarde eu coloco aqui uma instância dele para exemplificar.
Comment: Eu ainda tenho uma dificuldade em relação de como identificar problemas que podem ser solucionados pela programação linear, tipo como saber se um determinado problema é linear?
Comment: @gato você precisa considerar o problema em uma abordagem "orientada a restrições". Posso elaborar um caso concreto do alfaiate, em que ele tem alguns poucos metros quadrados de linho, algodão e seda e precisa fazer ternos e vestidos, sendo que terno consome 2 metros quadrados de linho e vestido 2 metros quadrados de seda, se vende terno com um lucro de 200 dinheiros, e vestido com lucro de 300 dinheiros. A pergunta é como otimizar a produção de modo que se obtenha lucro máximo.
Comment: Ah, @gato , se ficou meio confuso é porque não consegui elaborar direito no comentário, em breve virá uma atualização de resposta bem bonita ;-)
Comment: A propósito, se não tiver pelo menos um grau de liberdade nas restrições, então ou é impossível ou é resolver um sistema de inequações lineares que só permitirá uma única reposta, tornando o problema independente da função objetiva
Comment: Hum entendi, não se preocupe :) Hoje na faculdade eu dei uma pesquisada, e vi que é um pouco amplo mais estou entendendo mais. Futuramente irei fazer mais perguntas a respeito, e focada numa implementação especifica de linear e problemas.
Answer: Em resumo resumido
Pesquisa Operacional (P.O.) é uma área da matemática aplicada que basicamente tentar solucionar problemas relacionado a otimização através da busca de soluções de equações de máximo (maior lucro, maior desempenho) e mínimo (mínimo custo, menor caminho). É usada muito em problemas logísticos e financeiros e ajuda no processo de tomada de decisão nas empresas.
Um exemplo clássico é do mascate ou caixeiro-viajante: A partir de uma mapa, com vários clientes em locais distintos e várias estradas diferentes, com distâncias diferentes, ligando esses pontos, determinar qual o melhor caminho (otimização) que o mascate pode fazer de forma a ter o menor custo possível, considerando as condições das estradas, pedágios, etc., de forma a maximizar o seu lucro.
Outro exemplos seria o prolema de alocação de professores em salas de aula. Dado um conjunto de turmas, disciplinas e agenda dos professores na semana, determinar qual a melhor disposição das aulas na semana de forma a minimizar a quantidade de vezes que o professor vem para a escola. Ou a melhor disposição de forma a utilizar melhor as salas de aula.
A computação entra com a adaptação das equações e métodos matemático em forma de algoritmos para resolver esses problemas. A computação também colabora com tipos de algorítimos numéricos/genéticos que seriam impraticáveis para se resolver com lápis e papel.
Como exemplo de aplicação na computação, existe uma lib do Python chamada PuLP que pode ser utilizada na solução dos problemas. Nessa página e nessa outra podem ser encontrados exemplos de aplicação da lib.
Se quiser algo divertido eu sugiro esse post no blog do Ricardo Bittencourt que tem a ver com PO.
A Programação Linear é a disciplina que aborda as técnicas para resolver alguns problemas de P.O. que normalmente são modelados através de uma equações onde se deseja encontrar o valor máximo ou mínimo e outro conjunto de inequações que informam as restrições que devem ser consideradas. No meu curso a disciplina era chamada de P.O. mesmo, mas varia em cada instituição. O termo "Linear" se deve ao fato dessas inequações serem todas lineares, ou seja, polinomiais de grau 1.
Comment: E os algoritmos? Você exemplificou mais a pesquisa operacional em si como disciplina ao invés da computação.
Comment: A parte da computação é transformar as soluções matemáticas em programas como disse no quarto parágrafo. Você diz para eu citar o nome dos algorítimos ou colocar algum código de exemplo?
Comment: Você cita o problema do caixeiro viajante, mas não cita onde a pesquisa operacional tenta resolver isso. Exemplo seria comparar um algoritmo de dijkstra contra um algoritmo A*. No A* eu utilizo dijkstra como heurística junto a outro algoritmo.
Comment: @GabrielColetta existem diversos e incontáveis algoritmos de pesquisa operacional. A* é uma busca. Simplex é outra busca. Tem o método das elipses que também é usado em Programação Linear. Colônia de formigas, abelhas, têmpera simulada, algoritmos genéticos também
Comment: @JeffersonQuesado Estou ciente disso, mas a pergunta gostaria de exemplos de algoritmos, o que eu fiz foi exemplificar um caso de algoritmo que tenta resolver o problema do caixeiro viajante.
Comment: @GabrielColetta então interpretamos de maneira distinta o item 2 da pergunta
Comment: @GabrielColetta eu disse "resumo" porque eu entendo que o assunto é mais extenso do que a simplificação que eu coloquei. Se você puder contribuir com o que conhece, coloque numa nova resposta. Eu vou colocar depois um link pra um exemplo com PuLP e outro com F#.


Prove $\int_{\mathbb R^{d}}f(|y|)d\lambda^{d}(y)=C_{d}\int_{[0,\infty[}r^{d-1}f(r)d\lambda^{1}(r)$

Question: Let $f:[0,\infty[ \to \bar{\mathbb R}$ measurable, $d \in \mathbb N$
while $E_{d}:=\{x \in \mathbb R^d: |x| \leq 1\}$
Prove $\int_{\mathbb R^{d}}f(|y|)d\lambda^{d}(y)=C_{d}\int_{[0,\infty[}r^{d-1}f(r)d\lambda^{1}(r)$
and $C_{d}$ is constant with $C_{d}:=2\int_{E_{d-1}}\frac{1}{\sqrt{1-|x|^2}}d\lambda^{d-1}(x)$
Normally, I would write down a few of my ideas but I have no idea where to begin. I have only just acquainted myself with the $\lambda^{d}$ transformation formula, but do not see its applicability in this instance.
Any help is greatly appreciated
Comment: Write $x = \zeta u,$ with $\zeta > 0$ and $u$ a unitary vector (assume $x \neq 0$). Apply change of variables (change of measures?) bearing in mind that Lebuesge measure decompose as $\lambda \otimes \mu$ where $\lambda$ is $r^{d-1}$ times Lebuesgue measure on $\mathbf{R}_+$ and $\mu$ is the surface measure on the sphere.
Comment: It might help to go over the general construction of the [surface measure](http://www.math.ucsd.edu/~bdriver/240-00-01/Lecture_Notes/measurep.pdf) on $S^{n-1}$.
Comment: Could you explain why you decided to put a bounty on this question? Why are you not satisfied with the current answers?
Answer: It is the polar coordinate change of the Lebesgue measure. To prove the validity of the formula, we can first test it for $f$ of the form
$$
f(r) = 1_{(a,b)}(r).
$$ Then by the volume formula of $d$-dimensional ball, we can check that it's true. Since it is true for any interval $(a,b)$, the result can be extended to any measurable set $E$ by approximation, and hence to any non-negative measurable $f$. This proves the formula for general integrable $f$.
Answer: Change to polar coördinates.  If $\sigma$ is surface area measure on $S^{d - 1}$ and $s_{d-1}$ is the surface area of $S^{d-1}$, you have the following.
$$\int_{\mathbb{R}^d}f(|x|)\,dx = \int_0^\infty \int_{S^{d-1}} f(r)\,d\sigma\, dr
= s_{d-1} \int_0^\infty r^{d-1}f(r)\, dr$$


Programa nao le primeiro scanf e pula o segundo scanf do script

Question: Tento criar um algoritmo, porém quando vou ler o conteúdo do primeiro scanf, ele só imprime a primeira letra do nome. O segundo scanf que deveria ler um número é ignorado e automaticamente imprime o resultado do primeiro if
<code>#include <stdio.h>

//Compiler version gcc  6.3.0

int main()
{
  int tentativas = 0;
  int nivel;
  int numeroDaSorte = rand() % 100;
  char nome;

  printf("Bem vindo ao jogo de adivinhacao\n");
  printf("\nQual o seu nome?\n");
  scanf("%c", &nome);
  printf("\n\nSeja Bem Vindo %c", nome);
  printf("\n Qual nivel de dificuldade vc deseja?");
  printf("\n1 - facil");
  printf("\n2 - medio");
  printf("\n3 - dificil");
  scanf("%d", &tentativas);

  if(tentativas = 1)
  {

     printf("\n Nivel facil, vc tem 15 tentativas ");
     nivel = 15;
     }
      else
      if(tentativas = 2) {
        printf("\n Nivel medio, vc tem 10 tentativas");
        nivel = 10;
        }
        else {
          printf("\n nivel dificil, vc tem 5 tentativas");
          nivel = 5;
        }

  }
</code>
Comment: está certo, `char nome` é uma variável `char`, ou seja, armazena 1 caractere. Para armazenar um nome precisa ser um array
Comment: Aqui: ` if(tentativas = 1)`você não está comparando a variável tentativas com 1, está atribuindo 1 à variável tentativas. Para comparar utilize o operador `==`: ` if (tentativas == 1)`. Idem demais `if`. Quanto ao nome declare `char nome[3];`e utilize o formato `%s` no scanf: `scanf("%s", nome);` (sem &).
Comment: Erro de digitação. Declare `char nome[50];` ou o tamanho que julgar conveniente.
Answer: Tu precisa estudar um pouco mais sobre string, assim como disseram nas outras respostas. Tu declarou a variável nome como char , um char é apenas um caractere , ou seja, apenas a primeira letra do nome que tu digitou. Importe a bibioteca string.h, você também vai precisar mudar a declaração da variável nome. Fazendo isso , no printf tu vai chamar ela por %s (string) ao invés de %c (char) .


Update WSUS from MS Security Bulletin DVD

Question: Scenario
Windows Server 2003, WSUS 3.0 SP1, No Bandwidth Limited Internet Connection, Microsoft Security Bulletin DVD
Questions

How do you setup WSUS to import updates from this DVD?
Is this even possible?
How do you setup a WSUS server when you cannot sync with MS Update?

If this can be done with SCCM 2007 then please advise.
If you need any further information, please do not hesitate to ask. I have a feeling that what I would like to do is impossible without an upstream server.
Update
There is an Internet connection that is bandwidth limited. I would like to setup the master WSUS without Internet connectivity.
Comment: If there's no internet connection, are the updates really nessesary?
Comment: Updated the question to reflect there is Internet, just bandwidth controlled
Answer: I'd suggest against setting up a method of manually importing updates to your WSUS server.  In my experience (with pre 2.0 at least), there would almost always be complications and probability of getting your database corrupted (it might be better now with 3.0, but I highly doubt it).  There's a way to copy updates to a disconnected WSUS, but I believe you can ONLY import from exported data of another WSUS server).
I think you should be asking the question of how much time you're going to spend in getting the actual updates (how would you filter which ones get installed, for which software?), and how practical this method of Windows updates is really going to be.
I suggest you talk it over with your fellow admin folks and come up with an strategy of getting the server updates to your WSUS as intended by Microsoft (make sure you allow ONLY what's needed before downloading updates)... if this is a 'bandwidth tightening' + security issue (depending on the kind of control you have over your bandwidth/access), you could just enable the ports and/or sites for WSUS, as documented here: http://technet.microsoft.com/en-us/library/bb693717.aspx
...or you could play with the solution provided by this link: http://www.wsusoffline.net/ (WSUS Offline Update).  Looks like it was recently  updated, so might still be relevant.
Cheers!
Comment: Marking you as the correct answer as no other answers have been given after all this time and my searches have yielded that this cant be done. You only have to choices: From MS or from another WSUS server.


Postgresql Create Trigger Before Deleting A Row

Question: so i have these two tables:
 -- Table user
 columns: id,name,surname, password,token,earnedmoney

 -- Table addlisting
 columns: id, user_fk,price,date_added

Here is my problem: I would like to create a trigger so that when I delete a listing from the table addlisting, the price of the listing gets added to the column "earnedmoney" which is in the table user.
Could somebody help me? Thank you!
Answer: CREATE OR REPLACE FUNCTION add_money() RETURNS trigger AS
$$BEGIN
   UPDATE "user" SET earnedmoney = earnedmoney + OLD.price
      WHERE id = OLD.user_fk;
   RETURN OLD;
END;$$ LANGUAGE plpgsql;

CREATE TRIGGER add_money
   BEFORE DELETE ON addlisting FOR EACH ROW
   EXECUTE PROCEDURE add_money();

It could also be an AFTER trigger, that would make no difference.
Comment: Thanks, you are awesome!
Comment: @mazunki No; please ask a new question with complete information.
Answer: Just create regular BEFORE DELETE trigger:
test1=# create table addlisting (id serial PRIMARY KEY, user_fk integer, price float, date_entered date);
CREATE TABLE

test1=# insert into users (name, sername, password, token) values ('user1', '', '123', '123'), ('user2', '', '234', '234');
INSERT 0 2

test1=# insert into addlisting (user_fk, price, date_entered) values (1, 100, now()), (1, 34, now()), (2, 465, now());
INSERT 0 3

test1=# select * from users;
 id | name  | sername | password | token | earnedmoney
----+-------+---------+----------+-------+-------------
  1 | user1 |         | 123      | 123   |
  2 | user2 |         | 234      | 234   |
(2 rows)

test1=# select * from addlisting;
 id | user_fk | price | date_entered
----+---------+-------+--------------
  1 |       1 |   100 | 2016-07-08
  2 |       1 |    34 | 2016-07-08
  3 |       2 |   465 | 2016-07-08
(3 rows)

test1=# CREATE OR REPLACE FUNCTION update_price() RETURNS trigger AS $emp_stamp$
test1$#     BEGIN
test1$#         update users
test1$#         set earnedmoney = coalesce(earnedmoney, 0) + coalesce(OLD.price, 0)
test1$#         where id = OLD.user_fk;
test1$#
test1$#         return OLD;
test1$#     END;
test1$# $emp_stamp$ LANGUAGE plpgsql;
CREATE FUNCTION

test1=# CREATE TRIGGER on_delete_addlisting
test1-# BEFORE DELETE ON addlisting
test1-#     FOR EACH ROW EXECUTE PROCEDURE update_price();
CREATE TRIGGER

Test:
test1=# delete from addlisting where id in (1,3);
DELETE 2

test1=# select * from addlisting;

 id | user_fk | price | date_entered
----+---------+-------+--------------
  2 |       1 |    34 | 2016-07-08
(1 row)

test1=# select *from users;
 id | name  | sername | password | token | earnedmoney
----+-------+---------+----------+-------+-------------
  1 | user1 |         | 123      | 123   |         100
  2 | user2 |         | 234      | 234   |         465
(2 rows)

test1=# delete from addlisting;
DELETE 1

test1=# select *from users;
 id | name  | sername | password | token | earnedmoney
----+-------+---------+----------+-------+-------------
  2 | user2 |         | 234      | 234   |         465
  1 | user1 |         | 123      | 123   |         134
(2 rows)

Comment: That is very detailed, I appreciate that. Btw, what does the coalesce() function do?
Comment: If first argument is null then it will return second. ```coalesce(arg1, arg2) = case when arg1 is null then arg2 else arg1 end```


Generating Functions Homework Question 2

Question: This is a HW question
The question is to use generating functions to count the number of six digit (positive) integers whose digits sum to $42$. Ex. $978468$ is a  six digit integer whose digits sum to $42$
I am only looking for a hint on how to set this up.
Comment: someone had posted an answer/hint that seemed to be very helpful but they deleted it. Was there something wrong with it ?
Answer: If $a_{n,s}$ is the number of $n$-digit numbers (allowing leading $0$) with digit sum $s$, then we have the recursion
$$a_{n+1,s}=\sum_{d=0}^9 a_{n,s-d}$$
So if $f_n(z)=\sum_{s=0}^\infty a_{n,s}z^s$, you can express $f_{n+1}$ using $f_{n}$ ...
Comment: thanks for the answer Hagen would there be a difference to your answer if I told you that recursion was not covered in this assignment its part of the next assignment.
Comment: what I mean is there a way to approach this without recursion
Comment: someone else had posted an answer but they deleted it for some reason. If I follow what that person had said my function would be $f(x)=(x+x^2....x^9)(1+x+x^2+...x^9)^5$ where the first term is for the first digit and the second terms is for the remaining 5 digits. Then from here I would go about finding the coefficient of $x^{42}$. Does this seem right ?


Using the boolean "True" as an expression to evaluate in a while loop

Question: I recently was making a little script that would ask for tv series titles and logs them on the console in an array. I used a keyword to stop the loop which is 'terminate' and it should not be included, however, the following code adds the keyword to the array.
//-----------------firts case code ------------------//
let lista = [];
let series;

function collect(){
  while (series !== 'terminate'){
    series = prompt('Add a tv show');
    lista.push(series);
  }
}

collect();

console.log(lista);

More confusing still, is that in the next piece of code, I managed to make the script to leave the keyword out of the array.
//---------------------------Second case code--------------------------//
let lista2 = [];
let series;

while (true){
  series2 = prompt('add tv serie');
  if (series2 !=='terminate'){
    lista2.push(series2);
  }else{
    break;
  }
}
console.log(lista2);

My question is, how the use of the boolean as the expression to evaluate in a loop affects the result as opposed to the first case scenario with the first case code?
Comment: You might want to use `do … while`
Answer: Basically you need to have a check between input and adding the string to the array.
For looping forever, you need an expression which evaluates to a truthy value. This value is a constant value as opposed of the check with two strings series2 !== 'terminate'.
You could terminate the loop inside with an if statement and break.
Addtionaly, you could check for null and use the return value of prompt for being cancelled.

function collect() {
    while (true) {
        const series = prompt('Add a tv show');
        if (series === 'terminate' || series === null) break;
        lista.push(series);
    }
}

let lista = [];

collect();
console.log(lista);

Another approach by using prompt twice.

function collect() {
    let series = prompt('Add a tv show');

    while (series !== 'terminate' && series !== null) {
        lista.push(series);
        series = prompt('Add a tv show');
    }
}

let lista = [];

collect();
console.log(lista);
Comment: Thank you, Nina, the code you wrote really gets the job done! I still don't quite get the use of 'True' as an expression to evaluate. Would it be a trouble to ask you to explain it?
Comment: btw, you are still aware that `'True'` is a truthy string, but not `true`, which is a boolean value. the use of `true` indicates the loop should run forever. a string in this case would  be strange, even if the loop runs forever, too.
Comment: Right! Yeah, I was not entirely aware of it. Jeez! Thank you very much indeed! You are awesome!!
There are a few things that are still a bit puzzling, but I'll get it eventually. Again, thanks a million.


Prevent a thread from being unscheduled

Question: I try to implement something that prevent a thread from being interrupted by other running threads (inside of a single process), using the winapi.
My first ID was to put this thread to a higher priority, regarding documentation (from http://msdn.microsoft.com/en-us/library/windows/desktop/ms685100%28v=vs.85%29.aspx) it should prevents other from being executed (of course if my thread does not fall to any WAIT state).
But, and I can't really get why, other thread are still being scheduled after this "trick".
I can't get any other idea than using SuspendThread() on all other threads...
Is there some cleaner way to do so? Or have you got any advice regarding my issue?
Comment: It's impossible to have an uninterruptible thread. If you could do that, and your program ran on a single core processor, the computer would freeze, unable to react to hardware input. Even in old Windows 3.1 (a non-preentive cooperative system with only one 'system-wide thread'), your code would be interrupted by hardware events. If you give a bit of more information about what problem you are trying to solve, maybe someone can suggest a proper solution for yo.
Comment: XY-Problem. What are you trying to achieve?
Comment: "interrupted by other threads" which other threads?  In your own program or in the entire system? Because the latter is going to REALLY HARD, if not impossible.
Comment: Maybe you should look into using a lock/mutex/critical section instead.
Comment: A thread can enter a wait state for reasons you don't expect, in basically any system function you call. And if you're worried about data races or similar, then use proper locking mechanisms like mutexes or semaphores.
Comment: I want to prevent this particular thread to be interrupted by other thread (of the same process).
Comment: You already said that. Why do you need this?
Comment: The thing is, I'm currently working on a API on top of windows. And I have to provide such a functionality. Don't really know up to which goal for now.
Comment: Use Fibers, then you have control over scheduling...
Comment: Aside from setting the priority, set the processor affinity so that no other threads are running on the same core.
Comment: @joshpoley: How should this be done? You cannot change the affinity of some system processes...
Comment: @JochenKalmbach, The question stated that you only cared about the threads inside of a single process. But if you do want to rude, you can run as SYSTEM and then you can move all other processes' threads to another core (we've done this for performance benchmarks).
Comment: @joshpoley This was exactly my problem! I didn't think about multicore at all... Thank you because that explained a lot behavior on my side :) If you put your solution as an answer I will check it as accepted answer.
Comment: @JochenKalmbach can just add that (use `SetProcessAffinityMask` or `SetThreadAffinityMask`) to his answer.
Comment: @joshpoley: Done... hope it is correctly... also added comment about SuspendThread...
Answer: Threads are scheduled OS wide and not only for one process. You can change the prio to 31. This will be the highest possible... but if other threads also runs with prio 31, your thread will also interrupted... Also all hardware-events (IRQs) will have higher prio than 31...
If you just want to have an "Process-wide" solution, I suggest to use Fibers. With fibers, you can do your own scheduling and also "prevent" other "Fibers" to "interrupt" your task. Of course, it might still be "interrupted" by an other thread. But if you run your thread with prio 31, this should be no big problem....
With this scenario, we run very high responsive (almost) realtime systems with Profibus connections... Also SQL server is using the fiber mode for optimized and very high performance mode.
Alternatively you can modify (restrict) the process affinity mask for all other processes (SetProcessAffinityMask)! To modify all processes you need to start your process as SYSTEM (for example via a service).
If you just care about your own process you can modify (restrict) the thread affinity of all other threads (SetThreadAffinityMask) in your own process!
Be aware, that it makes no sense to restrict your "important" thread to a specific CPU! This will only restrict your thread. But you must restrict all other threads/processes in order to get your "important" thread the best performance!
Also be aware that you never should consider using SuspendThread! This might lead to dead-locks... see also: http://blog.kalmbachnet.de/?postid=6 / http://blog.kalmbachnet.de/?postid=16

Prevent a thread from being unscheduled


Which tree ensemble algorithms are the most suitable for time series forecasting (regression)?

Question: Decision tree ensemble models are very practical for building predictive ML models. They are not strict on assumptions, can work on data without too much preprocessing, train fast and typically result in high accuracy. One of the weaknesses of these algorithms is that they are not designed for extrapolation.
There are quite many variations of decision tree ensembles, but which are more suitable to predict multiple time series? For a time t there are multiple observations, i.e. price of different products?
Here is a list of some of the algorithms:

Random Forest
Regularized Greedy Forest (RGF)
Gradient Boosting
eXtreme Gradient Boosting (XGBoost)
LightGBM
Combining Tree-Boosting with Gaussian Process and Mixed Effects Models (GPBoost)
Natural Gradient Boosting (NGBoost)
Answer: The obvious answer is it depends on what you want to do.
AFAIK, all but one of the algorithms you've mentioned produce point forecasts. The one that's different is NGBoost as that produces probabilistic forecasts. If it's important to you to understand the uncertainty around your forecast, this could be a good option for you.
Even if you're only after a point forecast, NGBoost is a decent option in terms of performance but is significantly slower than the other ones you mentioned.


Manutenção e recuperação de URLs de serviços

Question: Estou trabalhando em um projeto Web Forms que possui uma DLL, e nessa DLL há referências a serviços externos. Temos 3 ambientes (desenvolvimento, homologação e produção) e para cada ambiente há uma URL específica para cada um dos serviços, em sua versão para o respectivo ambiente.
Estou tendo problemas para gerenciar as URLs desses serviços. No projeto WebForms tenho o web.config em que posso usar XML Transformations para armazenar conjuntos chave/valor, e isso resolveria meu problema. No entanto, na DLL tenho apenas um arquivo app.config e o mesmo não dá suporte a transformations. Qual a forma mais indicada de deixar minha solução adaptada para se integrar ao ambiente correto, baseada apenas na Configuration usada durante o processo de Publish?
Apenas para salientar meu modelo atual: eu crio uma entrada no Web.Config com a URL do serviço
<add key="UrlServicoCep" value="http://url_dev/servico.asmx" />

Em seguida recupero esse valor e o passo como um parâmetro na instanciação da classe em que o serviço é utilizado:
string urlServico = ConfigurationManager.AppSettings["UrlServicoCep"];
var dao = new CepDAO(urlServico);

Resumo: Quero gerar um pacote para o ambiente desejado (homologação ou produção) e que essa geração do pacote já tenha a URL correta aplicada aos web services que consumo.
Answer:
No entanto, na DLL tenho apenas um arquivo app.config e o mesmo não dá suporte a transformations.

Dá sim. Só instalar a seguinte extensão no seu Visual Studio:
http://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859

EDIT
Se não quiser usar a extensão, creio que o método já esteja bem próximo do adequado. Eu faria apenas uma pequena modificação:
string urlServico = ConfigurationManager.AppSettings["UrlServicoCep"] ?? "http://url_default/servico.asmx";
var dao = new CepDAO(urlServico);

Comment: Mas levando em consideração a limitação nativa de não ter a xml transformation, como você resolveria o problema?
Comment: @TiagoCésarOliveira Sem considerar a extensão, acredito que o seu modo já esteja adequado, com alguns opcionais para segurança. Vou editar a resposta.


Specializations of the function .Difference between pointer typename and string?

Question: template<typename T>
int compare(T a, T b) {
    return (a  b) ? -1 : 0;
}

template<typename T>
int compare(T* a, T* b) {
    return (*a < *b) ? 1 :(*a > * b) ? -1 : 0;
}
template<>
int compare<string>(string a, string b) {
    return  (a  b) ? -1 : 0;
}

 int main(){
    int a = 3;
    int b = 4;
    compare("Hello", "World"); //1
    compare(&a, &b); //2
 }

Issue: Function int compare(T* a, T* b) is calling for first and second case;
DOD: Call function int compare(string a, string b) for first case;
Comment: Are you sure this compiles? Partial specializations for functions are not allowed in C++, so the `T*`version shouldn't compile.
Comment: can you specify a compiler version you used?
Comment: This obviously isn't a complete example (`main` is missing for instance) but I suspect the real example uses a `T*` overload, not a specialization.
Comment: Yes, I did not compile. I changed the code. And add main(). But can not cat admirable result.
Answer: "Hello" is a const char[6]. When used as an argument, that's a better match for const char* than for std::string. The latter is a user-defined conversion.
Solution:
using namespace std::literals;
compare("Hello"s, "World"s);

These are std::string literals
Comment: Your solution does not call partial specialization for "Hello". It calls general specialization


Memory efficient way to load large entity set for data export

Question: I'm working on an Excel exporting feature in a Drupal 8 site.
Obviously, I can load all entites of certain type like this:
<code>$user = \Drupal::currentUser();

$entity_query = \Drupal::service('entity.query')->get('some_type');
$entity_query->sort('created', 'DESC');

$ids = $entity_query->execute();
$entites = $this->storage->loadMultiple($ids);

foreach ($entities as $entity) {
    // build some Excel row with the entity information
    // ...
}
</code>
But if you're doing this on a large entity set, you'll be running out of memory very soon. I think the reason why Drupal would run out of memory is the extensive caching involves in the entity query and loading process.
Is there a more memory-efficient way to load an indefinite size of entity set without running out of memory?
(Note: Let's pretend the Excel library is very clever and do not take up memory. That's another issue for another day.)
Answer:
Batches allow heavy processing to be spread out over several page
  requests, ensuring that the processing does not get interrupted
  because of a PHP timeout, while allowing the user to receive feedback
  on the progress of the ongoing operations. It also can reduce out of
  memory situations.
Source: Example: Batch API

You need to use the Batch API. There is no official docs for D8, but you can look at the batch_example module and the CSV Import module.
Good conceptual read, Drupal 8 Batch Processing but a bad code example.
Comment: There's this [csvimport](https://github.com/xurizaemon/csvimport) sample module I co-maintain which actually does it right. Though it adds one operation per CSV line. One operation per every 5-10 lines would probably have been fine as well.
Comment: Not sure if the linked article is such a good read. It does wrong what most articles on Batch API do wrong: Processing everything in just one single batch operation. Whereas this needs to happen in multiple operations each processing a chunk of entities.
Answer: To reduce the memory footprint you can load single entities and then use resetCache():
<code>$ids = $entity_query->execute();

foreach ($ids as $id) {
  $entity = $this->storage->load($id);
  // build some Excel row with the entity information
  // ...
  // release entity from memory
  $storage->resetCache([$id]);
}
</code>
Comment: The con of just doing this would be that you could potentially run into a PHP timeout, but good to know.
Comment: @NoSssweat, yes, I know, but the question is specifically about loading entities in a more memory-efficient way and not everyone googling this might be interested in a batch solution, which is in general the better solution though and it is good to have both answers.


Retornar valores entre horas e minutos - PostGres SQL

Question: creio que seja uma dúvida simples.
Tenho um banco de dados em postgres em que minha variável "datahora" possui valores no formato "AAAA-MM-DD HH:MM:SS", como indica a imagem abaixo.

Minha dúvida é em como filtrar valores entre determinados horários (para todos os dias), como por exemplo entre 09:00 e 09:45.
Já fiz os testes com os códigos abaixo (variando o comando date_part com o to_char também) mas todos apresentam erros.
Código 1
select * from tb_registros
where tick in ('IBOV')
and date_part(datahora, 'hh:mm:ss') = between 09:00:00 and 09:45:00
order by datahora desc

Código 2
select * from tb_registros
where tick in ('IBOV')
and date_part(datahora, 'hh:mm:ss') >= 09:00:00 and <= 09:45:00
order by datahora desc

Código 3
select * from tb_registros
where tick in ('IBOV')
and date_part(datahora, 'hh:mm:ss') >= 09:00:00 and date_part(datahora, 'hh:mm:ss') <= 09:45:00
order by datahora desc

Alguém consegue me auxiliar?
Desde já agradeço.
Comment: "mas todos apresentam erros." , quais ? Creio que a comparação deva ser com string ... 'hh:mm:ss') >= '09:00:00' ...
Answer: Aconselho utilizar extract ao invés de date_part, ambas fazem a mesma coisa, sendo que a primeira faz parte do padrão sql ansi (American National Standards Institute), sendo comum aos demais bancos de dados e a segunda sendo exclusiva do Postgresql, talvez algum outro sgbd tenha implementado, mas teria que pesquisar.
Relacionado ao problema específico, basta extrair a hora e extrair os minutos e comparar conforme consulta abaixo.
select * from tb_registros
where tick in ('IBOV')
    and extract(hour from datahora) = 9
    and extract(minute from datahora) <= 45
order by datahora desc

De qualquer forma, você está usando o date_part de forma equivocada, a maneira correta é: date_part(text, timestamp).
Segue abaixo uma solução com date_part.
select * from tb_registros
where tick in ('IBOV')
    and date_part('hour', datahora) = 9
    and date_part('minute', datahora) <= 45
order by datahora desc

Comment: E o perguntador some ...


Contradictory results while performing Z-test

Question: Hello Everyone,
I'm relatively new to statistics and this community so please bear with me and forgive me if I commit any mistake while asking my question. For starters, here are two hypotheses tests that I want to perform:
Test 1: If on an average, Girls score more than 600 in an exam?
Test 2: Mean score of girls = 600.
Given: sample mean(x) = 633.4, sample size(n) = 30, population std dev(σ) = 100
So first I performed Test 1 where (null hypothesis(H0): mean<= 600) and (alternate hypothesis(H1): mean > 600).
Z = (x – μ) / (σ / √n) = (633.4-600)/(100/√30) = 1.83.
Taking alpha = 0.05 and since this is a upper-tailed Z test, the decision rule is: Reject H0 if Z > 1.645.
Hence, we reject H0 and accept H1. Therefore, Girls score more than 600 in an exam.
Now, coming to Test 2
H0: mean score = 600 and H1: mean score != 600
Z = 1.83 and since this a Two-Tailed Z Test, the decision rule is: Reject H0 if Z < -1.960 or if Z > 1.960
However, we fail to reject H0. Therefore, mean score = 600.
Please explain why the 2 tests are giving contradictory conclusions?
Answer: The test aren't giving contradictory results. You are in a sense using contradictory significance levels.
If you want to require equivalent "evidence" (I'm putting that in quotation marks, because p-values do not exactly assess evidence) in a "particular direction" (i.e. in the "is higher" or "is lower" direction), then one uses one-sided tests with half of the significance level of two-sided tests. I.e. if you use a two-sided test for $\alpha = 0.05$, then the one-sided test would use $\alpha = 0.025$. If one does so, then the significance threshold for the test statistic is at $\pm 1.96$ in both cases.
Or, to put it in other words, in your example you suddenly say that in the one-sided case you are willing to tolerate a twice as large type I error rate in the "greater than"-direction under the null hypothesis as you were willing to tolerate in the two-sided case.
Comment: How do you mean other than 0.05 / 2 = 0.025? The 2.5th / 97.5th percentile of a standard normal distribution is then $\approx \pm 1.96$, while the 5th/95th ones are $\approx \pm 1.65$. Another way to look at it: You get the two-sided t-test for mean = 600 at the 0.05 level by doing two one-sided t-tests (one for the < 600 alternative and one for the > 600 alternative) at the 0.025 level and rejecting the null hypothesis of mean = 0 when either of the two one-sided tests has rejected its null hypothesis.
Comment: Umm do you mind showing it mathematically so that the results match? That would be a great help!!


SPSS - Automatic Linear Modeling "Importance" Numbers

Question: I have a large set of survey data. I'm looking at trying to find out which variables are the most important to impacting a DV (call it "happiness"). I'm not looking to find a beta number like a typical regression. I theorize that some of my ~14 variables will have co-linearity. Is it appropriate to do the following in SPSS, and am I interpreting this correctly?
Analyze->Regression->Automatic Linear Modeling
Predictors = all my ~14 variables
Target = my DV ("happiness)
Build options - automatically prepare data OFF
Model selection = forward stepwise
Criteria for entry/removal = F Statistics, including effects with p-values less than 0.05 and removing p-values greater than .1.
Hit Run.
Scroll down in the model to "coefficients, and expand window.
Variable 1 - coefficient .16, sig .000, importance .52
Variable 2 - coefficient .10, sig .000, importance .19
Variable 3 - coefficient .07, sig .000 importance .16
Variable 4 - coefficient .06, sig .000, importance .08
Variable 5 - coefficient .06, sig .001, importance .05
The coefficient I assume is beta - i.e. if this variable increased by 1, "happiness" should increase by the coefficient #. Now, what I'm really interested in is the importance #. Am I correct in interpreting this for variable 1 as, "Our model suggests 52% of the variance of happiness is due to this variable."
Further, am I correct that in that this modeling indeed removes colinearity from variables? Or do I need to do that in a different way. I'm hopeful SPSS is essentially doing a "relative weights analysis" here.
Thanks, and hopefully this makes sense!
Answer: SPSS Help or Algorithms document somewhere states about the "variable impotrance" in the Automatic Linear Modeling procedure:

For linear models, the importance of a predictor is the residual sum
of squares with the predictor removed from the model, normalized so
that the importance values sum to 1. This is the leave-one-out method
to compute the predictor importance, based on the residual sum of
squares SSe by removing one predictor at a time from the final full
model.

So, variable (predictor) importance (VI)
<code>VI = SSE_without_the_predictor - SSE_full_model, and the
Normalized_VI = VI / Sum(VI)_of_all_the_predictors.
</code>
But note that
<code>(SSE_without_the_predictor - SSE_full_model) / SSY
</code>
is the squared part correlation of the predictor with Y. Since SSY (Sum of squares in centered Y) is constant here, it does not influence the normalized VI.
Thus, the normalized VI of a predictor is nothing else than the relative size, among all the predictors, of its squared part correlation with Y.

See @amoeba's overview of various "importance" measures in regression setting. Check also other posts tagged [importance].
Comment: Hey, thank you for the response! Much appreciated. I apologize because I'm not super stats-y so this is a bit out of my element. Can you explain how this result generated by SPSS is different from a typical relative weights analysis (ex. https://www.statisticshowto.com/relative-weights/).

I'm confused on your last sentence. If I'm understanding you correctly (and I may not be), wouldn't importance be related to the coefficient? However, they vary at diffe levels - i.e. coefficient .16 for variable 1, importance of .52; coefficient of .10 for v2, importance of .19; but, .16 is not 2.7x .10.


Conditional joint probability with continuous variable

Question: Let $X, Y$ have the joint pdf
$f(x, y) = 2, \quad 0 < x < y < 1 \quad 0, \quad$ otherwise
Find $P(0 < X < 1/2$ | $y = 3/4)$
The solutions say
$\int_0^{1/2} f_{X|Y}(x | y = 3/4)dx$
I know that $f_{X|Y}(x | Y = y) = 2$, but how do I find $f_{X|Y}(x | y = 3/4)$?
Answer: Consider the figure, which shows the region in which $f(x,y) = 2$ and the particular value $y = 3/4$.  What is the chance that a point on the red line ($y = 3/4$) has value $x<1/2$?
Comment: I don't understand. I'm not even sure why that figure shows the region $f(x, y) = 2$.
Comment: So I figured out how to do this with an actual pdf chart. Is there a way to convert that pdf I gave in the original question into a chart-form?
Answer: $f_{X\mid Y}(x\mid y) = \dfrac{f_{X,Y}(x,y)}{\displaystyle \int_0^y f_{X,Y}(x,y)\operatorname d x} = \dfrac{1}{y}\quad\mathbf 1_{0< x< y< 1}$
That is $X$ (conditioned on $Y=y$) is uniformly distributed on the interval $(0;y)$