NSURLCache is emptied when app is closed

Question: i'm trying to implement a NSURLCache with AFHTTPRequestOperation, this is what i have done:
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:50 * 1024 * 1024 diskPath:nil];
    [NSURLCache setSharedURLCache:URLCache];

....
    }

Then to use it i do this:
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0f];
UIImage *image = [[UIImageView sharedImageCache] cachedImageForRequest:urlRequest];
if (image != nil) {
  //Cached image
} else {

AFHTTPRequestOperation *postOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
postOperation.responseSerializer = [AFImageResponseSerializer serializer];
[postOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  UIImage *image = responseObject;
  [[UIImageView sharedImageCache] cacheImage:image forRequest:urlRequest];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Image error: %@", error);
}];
[postOperation start];

}
the first time i open the view when there is the code above download the image and store it in the cache, then i close the view and i open it again and read it from the cache, instead if i close the app, and i try again the image is downloaded again, so how i can create a NSURLCache persistent?
EDIT:
I have tried what Duncan Bubbage suggest, and i have add a cache path instead of the nil, and i have do it in this way:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    cachePath = [cachePath stringByAppendingPathComponent:@"temp_img"];

    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:50 * 1024 * 1024 diskPath:cachePath];
    [NSURLCache setSharedURLCache:URLCache];

but i still cannot cache perstitently across session, what i have wrong? i have also check in the Finder the Cache Folder, and there is the temp_img folder, i have tried to create the folder manually with NSFileManager and then pass the path, but the folder temp_img remain empty...
Comment: Does anything get written to the cache directory while the app is running, which is then deleted, or is it simply that nothing is ever written?
Comment: nothing is ever written, no directory, no image nothing
Comment: Make sure path exists.
Comment: If i create it with nsfilemanager exist for sure, but the folder is always empty, Also the documentation say that if the folder doesn't exist it will be created, so i think that this is not the problem, anyone can help me?
Comment: Is it valid to set initWithMemoryCapacity:0? Try setting the memory capacity to the same size as the disk capacity. It may be that the memory capacity prevents the cache from ever being used. In that regards, your question here states "the first time i open the view when there is the code above download the image and store it in the cache". However, have you actually confirmed that the image is ever being cached, or would it be re-downloaded even during the current app session? May simply be your cache is not working at all.
Answer: Are you sure it is valid to set diskPath:nil when you are wanting to cache to disk persistently across sessions? The parameter is optional but that may only be for the use case of caching in memory only, in which case you'd expect to see what you're seeing here?
Alternatively, the Apple documentation for this method states:

NOTE
In iOS, the on-disk cache may be purged when the system runs low on disk space, but only when your app is not running.

This could just be expected behaviour?
Comment: Thanks for the answer, i have to purge the on disk cache when system runs low memory, or iOS do it automatically?
Comment: iOS will do this automatically, if the overall system (not just your cache) is short on free disk space.
Comment: i have tried to add the path, but doesn't work, i have update the question


Find a homomorphism $f : \mathbb{Z}_{15} \to\mathbb{Z}_5$, and indicate its kernel.

Question: Wondering if this is a good enough answer. Let me know if I did anything wrong. Please, if you have a simpler way let me know.
Since $\mathbb{Z}_{15}$ is cyclic, one can pick ANY generator, $k = 1, 2, 4, 7, 8, 11, 13$, or $14$ and then pick ANY element of $\mathbb{Z}_{5}$ to be the image, $f(k)$. {$\mathbb{Z}_{15}$ is determined by a generator $k$, and the condition: $15k=0$. So the only condition on "$f$" is that $0 = f(0) = f(15k) = 15f(k) = 5(3f(k))$, which is true for all possible $(3f(k))$ in $\mathbb{Z}_{5}$. Hence there are exactly five distinct group homomorphisms.}
Let's take: $f(7)=3$. That completely determines the homomorphism. For example
$f(2)= f(7+7+7+7+7+7+7+7+7+7+7) \\= f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) \\= 3+3+3+3+3+3+3+3+3+3+3 \\= 3$.
To find the kernel, one could just list $f(k)$ for all $k$ in $\mathbb{Z}_{15}$, the kernel is those "$k$" for which $f(k)=0$ For example:
$f(10) = f(7+7+7+7+7+7+7+7+7+7) \\= f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) + f(7) \\= 3+3+3+3+3+3+3+3+3+3 \\= 0$,
so 10 is in the kernel for this "$f$". It turns out that there are only two possible kernels, depending on which group homomorphism you pick.
Alternatively, the kernel must be a subgroup of $\mathbb{Z}_{15}$, for which the order of $\mathbb{Z}_{15}$ divided by the order of that subgroup produces a divisor of the order of $\mathbb{Z}_{5}$ {$1$ or $5$}. So the kernel is a subgroup of $\mathbb{Z}_{15}$ of order either $15/5=3$ or $15/1=15$, and there are just one of each. Both are kernels for some homomorphism. However, if $\mathbb{Z}_{15}$ is the kernel then $f(k)=0$ for all $k\in\mathbb{Z}_{15}$, the trivial homomorphism. So for all the non-trivial homomorphisms, the kernel is just the subgroup generated by the elements of order $3$.
Comment: Please see https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
Answer: One method is to see that you have cyclic group of order 5, whih has no proper, non trivial subgroups. Since the image of a homomorphism has to be a subgroup, you will know that image must be either identity element, or the whole group. After that it's easy to see kernel must be subgroup of order 3, for onto homomorphism. This makes things easier, when you have a group with no proper, non trivial subgroups.
Answer: Not really an answer, but when I do these kind of problems with homomorphisms, I usually start by seeing where 1 goes. What is $f(1)$ equal to?
To answer your question, your answer is good enough. You made several good conclusions and have the algebra to prove it. You should tidy it up though.
Comment: How can I find the answer. I'm only just learning about homomorphisms. I'd really appreciate your help.
Comment: What I was getting at was that you should write out your homomorphism out and start with $1$.

Pick a image for $f(1)$. Say, $f(1) = 0$. Then what is $f(2)$? And so forth down to $15$.

Also, if you're only just starting out, I think this is a great way to do so with manually cranking out a homomorphism.


Finding a minimum variance unbiased (linear) estimator

Question: Here is a basic question that perhaps has a simple answer, but one that I was not able to find by quickly scanning the literature.
Suppose that I have a collection of $n$ unopened boxes. Each box $i$ contains either a red ball or a blue ball: write $c_i = 1$ if box $i$ contains a red ball, and $c_i = 0$ otherwise. Moreover, each box $i$ has a probability $p_i > 0$ written on it. If I attempt to open box $i$, then with probability $p_i$ I will succeed and observe whether $c_i = 1$ or $c_i = 0$. With probability $1-p_i$ I will fail, and will not learn $c_i$. I can only try once -- if the box does not open, I never learn $c_i$. The parameters for each box $(c_i, p_i)$ are drawn i.i.d from an uknown distribution $\mathcal{D}$. I wish to produce an unbiased estimate of the expected fraction of red balls:$$C \equiv E_{(c_i, p_i) \sim \mathcal{D}}[ c_i]$$ Note that while the marginal distribution on the $c_i$ is Bernoulli, I make no assumptions about how the $p_i$ are distributed -- they could be correlated with the $c_i$ in some arbitrary way.
An obvious first attempt at finding an unbiased estimator is to attempt to open each box, and set $d_i = \frac{c_i}{p_i}$ if the box opens, and $d_i = 0$ otherwise. Then take as my estimate:
$$S \equiv \frac{1}{n}\sum_{i=1}^nd_i$$
Since $E[d_i] = C$ for each $i$, this is an unbiased estimator. Can I argue that it is the minimum variance unbiased estimator?
If I was literally sampling the values $d_i$, then I could at least say that $S$ was the minimum variance linear unbiased estimator, since each $d_i$ is sampled i.i.d and I am estimating the sample mean. But can I say anything about $S$ given that I am actually sampling $(c_i, p_i)$? Is there a literature on this sort of problem?
Answer: Your setup is analogous to sampling from a finite population (the $c_i$) without replacement, with a fixed probability $p_i$ of selecting each member of the population for the sample.  Successfully opening the $i^{th}$ box corresponds to selecting the corresponding $c_i$ for inclusion in the sample.
The estimator you describe is a Horvitz-Thompson estimator, which is the only unbiased estimator in the class of estimators $\hat{S} = \sum_{i=1}^{N} \beta_i c_i$, where $\beta_i$ is a weight to be used whenever $c_i$ is selected for the sample.  Thus, within that class of estimators, it is also the optimal unbiased estimator regardless of the criterion for optimality.
It has been shown (Ramakrishnan) that the H-T estimator is admissible in the class of all unbiased estimators of a finite population total.  (Note the link is not to the original paper by Godambe and Joshi, which I can't seem to find online.)
For a review of the Horvitz-Thompson estimator and its properties, see Rao.
Comment: My script tried to fix the broken link but the new one isn't right. Would https://books.google.nl/books?id=f8NY6M-5EEwC be a suitable alternative?
Comment: Yes it would, thanks!  And that'll remind me to put a full(er) cite in with the link.


Python Scikit-Learn SVM - No Predicted Samples for a Class

Question: I am doing a classification task in Python to classify audio files of different musical instrument into their respective class, in my case there are 4 class, which are Brass, String, Percussion, and Woodwind. I used SVM algorithm as the classifier. My code looks a bit like this (I do not change any parameter for the classifier):
#X is feature matrix, y is class vector
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

#SVM Classifier
svm = SVC()
svm.fit(X_train,y_train)
svm_pred = svm.predict(X_test)
print(metrics.classification_report(y_test,svm_pred)

When I try to run this code, I got problem with the classifier. The error code looks like this:
            precision  recall   f1-score   support

Brass         1.00      0.21      0.34        72
Percussion    0.38      1.00      0.55       279
String        1.00      0.15      0.26       276
Woodwind      0.00      0.00      0.00       156

avg / total   0.58      0.43      0.32       783

C:\Users\Anaconda3\lib\site-packages\sklearn\metrics\classification.py:1135: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.

When I checked my predicted labels from SVM classifier(svm_pred), no Woodwind class are predicted by the classifier
>>> set(svm_pred)
{'Brass','String','Percussion'}

My number of samples for each class are like this: Brass = 200 samples, Woodwind = 500 samples, Percussion = 900 samples, and String = 800 samples so it is a bit imbalanced

My question is, is it possible for a SVM classifier to not predict a class at all in the output of the classifier like my case above?
Comment: Can you try scaling the inputs?  I find SVMs do better when features are scaled.
Answer: First, you write "I do not change any parameter for the classifier." This is poor practice. Different problems have different optimal hyperparameter configurations. This is because we need to find the optimal bias-variance-tradeoff for your particular data.
Second, you need a method to relate the inputs (audio signals) to the model that you're using. The absolutely generic application of an SVM is comparing vectors. Are the audio files you're using represented as single vectors, or are they represented in some more complex format (perhaps a matrix)? Also, the default SVM kernel in sklearn is the RBF kernel. Does this kernel make sense for your inputs, or would an alternative kernel return more relevant comparisons of your inputs? Could a pre-processing step help extract more useful signal? These are all research questions you'll have to consider as you pursue your project.
Third, the predict method uses a default number to decide at what level of predicted outcome the sample should be allocated to a class. What this means is that the classification is sensitive to the choice of cutoff, and choosing a different cutoff will yield a different classification.

Classification is a decision. To make an optimal decision, you need to asses a utility function, which implies that you need to account for the uncertainty in the outcome, i.e. a probability.
The costs of misclassification are not uniform across all units.
Don't use cutoffs.
Use proper scoring rules.
The problem is actually risk estimation, not classification.

In other words, it's worth considering what the consequences are for a correct or incorrect decision, and selecting classification rules which minimize your risk. This is true regardless of whether or not you think the process of audio reproduction is deterministic, because some problems may have steep costs for misclassifying specific instruments, but not others.
Comment: I would argue that this is actually a good case for binary decisions.  Frank Harrell writes in the linked blog post "When are forced choices appropriate? I think that one needs to consider whether the problem is mechanistic or stochastic/probabilistic... It may be best to apply classification techniques instead just to high signal:noise ratio situations such as those in which there there is a known gold standard and one can replicate the experiment and get almost the same result each time.".  Classifying instrument sounds is probably one of those cases.
Comment: @DemetriPananos What is special about musical instruments that makes this a good case for binary decisions?
Comment: Instrument sounds are not probabilistic; they are mechanistic.  When you play a saxophone, its going to sound similar to most all other saxophones you've heard (save any pathological examples).  Its much like image recognition. Again, referencing Frank's post "An example [of when binary decisions my be appropriate] is pattern recognition - visual, **sound**, chemical composition, etc".  If you are arguing otherwise, it seems your first link undermines your point.
Comment: What about if the features you have are only weakly informative about the outcome? If our assumption is that all saxophones sound alike, and all woodwinds sound alike, it seems like we would have a higher-quality model. But in this case, there's a lot of confusion about what woodwinds sound like. In this setting, a probabilistic interpretation seems more appropriate because the features are so weak.
Comment: I'm fairly confident that audio files of instruments are not weakly informative about the source of the sound.  But, until OP posts data, that remains to be seen.
Comment: The audio signal could be pristine. On the other hand, it could also be corrupted with feedback, signal noise, or include other sounds like vocals or background noise. The labels could also be noisy, perhaps because the person making them was bad at their job, or the segmentation was poor, or other reasons not related to the instruments themselves or recording quality itself. If you're confident that the signals are so strongly informative, then why is the model so poor?
Comment: The mode could be poor for any number of reasons not related to the data, including but not limited to: a coding error, hidden state in OP's jupyter notebook, or as you allude to, inappropriate hyperparameters for the model.  The argument we're having is moot until OP posts data and we can verify that either the data is of high quality or it is corrupted so much so as to obscure the origin.
Comment: It's interesting how your argument has pivoted from "This is actually a good case for binary decisions" to "The argument we're having is moot until OP posts data." I wonder if you'll be changing your position again soon.
Comment: I still maintain this is a good case for binary decisions. What I have pivoted on is that you are worth my effort to argue with.
Comment: Right now, that case assumes facts not in evidence. Do you have an argument in favor of binary decisions which has a premise that we can verify?


How to access a site in IIS with no DNS mapping

Question: In my IIS 7.5 hosted in a Windows Server 2008 R2 I have several websites with no DNS address assigned. Let's take for instance the site (as named in IIS) with site name mySite. I have for this site the standard binding with no host name.

Suppose that my server IP address is, for instance, 101.22.23.01. Therefore it seems logic to me that in order to access the website, I should place in the address bar of my browser:
[IP_address]/[sitename]

in this case:
101.22.23.01/mySite

but it does not work. Shall I specify something else in the bindings?
Answer: You can add host name(mySiteHostName.com) and IP address(101.22.23.01) in the binding.
You need to add entry in your client hosts file.
C:\Windows\System32\drivers\etc\hosts file
101.22.23.01     mySiteHostName.com.
Now you can test in client machine without creating dns record.
Answer: You will need to use host headers and create the appropriate DNS records to host multiple websites using the same IP Address.
edit:
For example, you will need to create bindings for each site.
Create a binding for port 80 and enter the www.mysite.com in the hostname field and create the dns entry (or setup a local hosts file for testing) to point to that IP Addres.
Create a second binding for port 80 and enter www.myothersite.com in the hostname field and create the DNS entry.
In other words, you can't access those multiple sites without dns mapping or setting up the sites to bind on another port or IP Address
Comment: Thanks for your answer. However my purpose was to test the sites without creating a DNS record. Assume that all the sites use a different port, there should be some way to access them by using the IP address and let IIS route the request tot he appropriate site...
Comment: if they are using a different port, you get to them by going to the port (http://www.mysite.com:81) if it's bound to port 81. hostheaders is the way IIS routes requests if they are bound to the same IP and port.
Comment: In theory, you could setup a default page to process any request hitting the server, parse the data after and then redirect the user to the appropriate site.. but I think that's kludgey, prone to failure, and over complicating the issue.
Comment: sorry, I edited my question mayeb now it looks more clear
Comment: edit: Based on the picture of the binding, you can just go to http://101.22.23.01 and it will go to that site.
Comment: and if I have two websites, one with the binding like the picture and one with port 88?
Comment: the second one would be http:// 101.22.23.01:88


Comparison of regression methods with MATLAB

Question: I am writing a chapter of my thesis, where I try to establish the relationship
between regressors (512 x 8) and response (512 x 1) in my data.
I have been suggested to use three regression methods:

Bayesian linear (tapas_vblm),
Gaussian process (a non-parametric probabilistic regressor; GPML
package),
Artificial Neural Network (MATLAB, Feedforward Multilayer Perceptron
Neural Network, MLPNN, with a backpropagation reinforcement learning
training algorithm—scaled conjugate gradient).

Once I trained the models with 85% of the data, for cross-validation I compared them with the remaining 15%, and determined over the testing data the:
- coefficient of determination
- root mean square error
- mean square error
- mean average error
My question is how to theoretically compare the three methods (cost function,
given priors, etc.)?
Comment: Surely you mean mean absolute error? And what kind of answer are you looking for? Also since you mention software, could you please be more precise which software? I for one am not aware of tapas_vblm or gpml packages. Is this python, R, matlab, Stata, SPSS, SAS or something else?
Comment: These are unlikely to be equally flexible in their ability to fit the data. You should take that into account somehow, eg w/ cross validation.
Answer: I'm not that familiar with those regression methods. However, I also suggest ridge regression which is fairly easy to use.
Given your data are in a 2-dim format where 1st dim are instances and 2nd dim are datapoints as well as a target 1-dim vector with target values:
ridgeModel = ridge( YTrain, XTrain(:,:), 1, 0); % parameters are necessary
for i=1:length(XTest)
   YPredict(i) = XTest(i,:)*ridgeModel(2:end) + ridgeModel(1);
end
For better comparison of regression methods, I direct you towards AUC-value analysis by receiver operating characteristics. Fawcett (2005) wrote an excellent introductory article.


`Operating-System` attribute in AD vs MAC OS

Question: There is a Operating-System attribute in Active Directory attributes defined by Active Directory
As I don't have access to AD containing MAC OS joined devices can anyone tell me what would it populate under this attribute ?
Answer: I don't see an Operating-System attribute (attributes don't usually start with an uppercase letter btw), only operatingSystem, that is set to:
Mac OS X

The relaed operatingSystemVersion is the appropriate macOS release, e.g.
10.14.2

The release number doesn't seem to update, it reflects the release number at the time of joining to AD.
Comment: Operating-System is the name of the attribute as it appears in the schema, and operatingSystem is the LDAP display name for the attribute, as per the [OPs link](https://docs.microsoft.com/en-us/windows/win32/adschema/a-operatingsystem).


Como fazer o download de multiplos urls no R?

Question: Preciso extrair vários arquivos textos que estão organizados em pastas anuais no seguinte url: http://www.rsssfbrasil.com/tablesae/
Como faço para extrair diversas pastas ao mesmo tempo? Por exemplo, se quero os arquivos de 2003, 2004 e 2005, para não precisar escrever o mesmo código três vezes.
Obrigada!
Comment: Olá, no endereço informado na pergunta, não encontrei nenhuma pasta ou diretório, apenas vários arquivos htm. Geralmente você vai baixar os arquivos somente uma vez, e depois trabalhar em cima deles. O que exatamente você quer fazer?
Answer: Há várias maneiras; uma não muito elegante:
<code>library(RCurl)
library(XML)

base = 'http://www.rsssfbrasil.com/tablesae/'

page = url(base)

download.file(base, destfile='test.html')
page = htmlTreeParse('test.html', useInternal=TRUE, asTree=TRUE)
links = xpathSApply(page, "//a", xmlGetAttr, name='href')

# pega apenas os links com 'htm'
links = links[grep('htm', links)]

for(link in links) {

  download.file(paste(base, link, sep=""), destfile=link)

}
</code>


How to dynamically group content in a views listing

Question: I've hit a massive wall in trying to group a list of nodes displayed through a view on my Drupal 7 site. Currently I have a view that lists a group of nodes that are under a specific vocab term. I then use a weight field to make sure they are in the order that I would like. This works just fine.
Here is an example on my site. In this example I have 11 videos bundled together under one taxonomy term.
What I would like to do is now create "chapters" to better organize these. Using the example above I would like to be able to have the following output:
<code>HEADING 1  <-- I want to add headers like this. And then group videos in each header.
 1. Video
 2. Video
 3. Video
HEADING 2
 4. Video
 5. Video
HEADING 3
 6. Video
 7. Video
 8. Video
 9. Video
 10. Video
 11. Video
</code>
The big caveat is that I want to be able to assign these "chapter headings" at different spots for each video series that I have. I don't want to have Chapter 1,2,3 in the same position every time.
Here is an example of how I would like to be able to group my individual videos. Take a look at the video list on the left.
One route that I tried was to create a new Vocabulary called "Video Headers" and then create terms for each header that I wanted. I then created a new term reference field in my video content type where I would choose the corresponding "header" term that I wanted. But I am having trouble grouping this in my view. It's just not grouping correctly.
I've looked into the Book Module, but that doesn't seem like the right fit. My gut tells me that it has to come down to using taxonomy terms to group these videos.
UPDATE: Thanks to Jason below I was able to almost get it working. The problem is that all of my videos show up in the view rather then just the videos that are under a term. In order for my lists to work I have to have a contextual filter:
UPDATE #2: Figured it out! With help from Jason below, I also added a contextual filter of "Has Taxonomy: Term ID". That showed only the nodes associated with a term.

So as soon as I add that contextual filter, it ruins my grouping and groups all of the videos that I have together with one header. Somehow that is breaking it.
Comment: Why are using the node ID contextual filter?
Comment: It's the only way that I can display all the nodes tied to a taxonomy term. If I don't, then all videos are shown. I only want the videos to show that are tied to a taxonomy term. Make sense?
Answer: Try this out.
Add the taxonomy term (or entity) reference field to your output, exclude it from rendering, and configure the format settings to group on this field.
Add a relationship to you view based on the term (or entity) reference field you are using to reference the taxonomy terms. With this relationship, you can now add a sort field using the taxonomy term's weight. When you click to add a sort field, filter the options to taxonomy term using the drop-down select to the right of the search field in the add sort dialog. Once filtered, you will now see the taxonomy term weight.
Adding this to your sort, will allow you to sort the view results based on the node's referenced taxonomy term's weight within the taxonomy vocabulary. You could also sort on the term itself, if your term names will include numbers like, "1. Chapter 1" and "2. Chapter 2", etc.
Jason.
Comment: Thanks Jason. I was able to almost get it working. To display all of the videos assigned to a term, I have to create a contextual filter: Contend: Nid. Doing so reads the content ID from the URL and gives me my video list. As soon as I add that contextual filter, it ruins my grouping and only shows me one group for all of them. Know of any way to make the contextual filter work with this?


Greyhound Racing Descriptions

Question: When a greyhound race is described as A4 480m, what does that mean?
I guess the 480m means that it is a 480 metre race. But what does the A4 mean? Or if it is S2 or OR?
Answer: You are correct in your guess about 480m being the length of the race.  As for A4 or other classifications, it is the grade of the race.
Specifically A4 refers to a middle grade race.  This is a UK grading system and the system is described below:

U.K. - Top Grade - (A1 - A3) equilavent to American greyhound grades (A, B) and Australian grades (1, 2). Some 'top graders' may also compete in major race events and can average speeds around a track between 38-39 mph.
U.K. - Middle Grade - (A4 - A6) equilavent to American grades (C, D) and Australian grades (3, 3/4, 4). Occasionally 'graded runners' competing at the very best "city tracks" maybe the equal of 'A1' graded dogs running at perhaps lesser tracks. These hounds average speeds between 37-38 miles per hour.
U.K. - Low Grade - (A7 - A11) equilavent to American grades (E, F and Maidens) and Aussie grades (4/5, 5 and Maidens). Most dogs will begin or finish their racing careers in the lower grades and generally record speeds between 35-37 mph.
U.K. - B Grade - (B1 - B6) This grade prefix usually indicates to either a slightly shorter or longer middle-distance.

As for the OR classification, that means "open race" meaning it is the highest level of racing in the UK.  This does not mean that it is open to any participant.
When it comes to the "S" vs "A", this distinguishes the race distance. "S" is for a hound that has qualified in a sprint distance.  "A" is for a hound that has qualified in a middle distance.  There is also "D" for long distance, "H" for hurdles, and "N" for novice.

Basically, the lower the number of next to the letter is, the faster the race is.  The letter distinguishes the race type (distance) and the distance is of course the actual distance of the race.

Here are a few references:
Race Guide
Grading Rules

Greyhound Racing Descriptions


Poisson regression for continuous variables?

Question: I understand that Poisson regression is used for count data because, among other reasons, it accommodates the count variable's mode being zero and the fact that it cannot be negative. What about continuous variables, for example C-reactive protein, in clinical contexts? It is heavily positive-skewed, cannot be negative, and can have mode of 0 in many scenarios (lower limit of detection can be, for example <3, and considered 0 for analysis). Transformations are inadequate. This situation is true for many biomarkers.
How should one approach regression with these variables, can Poisson regression be used?
Comment: "Considered 0 for analysis" is usually thought to be a fundamental error and has to be considered one of the roots of your problem (as well as being a hint about how to resolve it).  See, for instance, Helsel's book "Nondetects and Data Analysis."
Answer: The Poisson model is not just skew and non-negative; for example it has a particular variance specification (the variance is equal to the mean). This will almost never be true for variables that are not counts (what happens if you change units? You change the shape of your Poisson model! That makes no sense).
While it would in some situations be possible to consider a quasi-Poisson model (variance proportional to the mean) that would still not usually be a suitable model for non-negative measurements of physical quantities.
You would instead typically expect them to have standard deviation proportional to mean - you don't expect the distributional characteristics, aside from a known change to a scale parameter, to change when you change scale.
If you're looking for a GLM, the obvious candidate with the characteristics non-negative, right skew, standard deviation proportional to mean is the gamma. (There are other commonly used standard-deviation-proportional-to-mean models -- the lognormal, the Weibull and so forth).
However, if you have exact zeros you might consider a zero-inflated or a hurdle model (gamma with a proportion of zeros).
On the other hand, if the low end is really censored (as with an instrument than cannot detect below some threshold and just records 0 there), then you may be better off to explicitly deal with that censoring. Since censored data is standard in survival models, programs for modelling survival often have what you need built right in (and offer multiple choices for survival time distributions, which you can parlay into models for C-reactive protein levels by simply putting that where survival time would go in the left-censored survival model)
Comment: Well, not so separate; parametric survival models are basically GLM-like models with censoring (but not necessarily natural-exponential family). Sure, if you stick to thinking of the response as necessarily *time*, they're separate things, but you can use survival models to fit regression / glm-like models (with or without censoring) to responses that aren't time. Numbers are just numbers, they don't know they're not times; as long as the model is the model you want, it's just fitting censored data by maximum likelihood.. Left censoring can sometimes be a bit tricky, as can  ...ctd
Comment: I'm as big a fan of gamma regression as anyone, but I think this answer is too hard on the quasi-Poisson model.  Why is it not "suitable"?  The estimator is consistent, assuming you have the right functional form for the conditional mean, and you can use a robust VCE to get consistent estimates of the standard errors.  It seems suitable to me; it's just probably not as good in all respects (in particular, not as efficient) as a gamma-log model.
Comment: @Laconic the variance specification really doesn't make much sense for physical measurements (indeed, aside from things that are counts or multiples of counts, the variance proportional to mean specification would fairly rarely make sense), so intervals - especially prediction intervals - and tests based on them will often be misleading. Modeling is not just about means! At the least such models should be applied (outside the arena of count data) with forethought, care and a skeptical eye. If you can give some reason to anticipate var. $\propto$ mean in this sot of context, I'm keen to hear it
Comment: @Glen_b Thank you that is very helpful. School boy question: if I only wanted to run a univariate linear regression with CRP as IV (lower end censored) and a continuous DV such as weight, how would I do this? In my undergraduate mind survival models and linear regression are separate things?
Comment: ctd... a fixed censoring point.  There's an example of using a parametric survival model (to fit data without censoring) here -- https://stats.stackexchange.com/questions/91762/analysis-of-variance-with-weibull-or-gamma-distributions/91786#91786  . Also see [this](https://stats.stackexchange.com/questions/124217/inconsistency-between-r-and-sas-for-mle-on-weibull/124224#124224) and [this](https://stats.stackexchange.com/questions/161891/how-to-estimate-the-parameters-of-frechet-distribution-in-r/161901#161901) ... ctd
Comment: and [this](https://stats.stackexchange.com/questions/230937/how-to-find-initial-values-for-weibull-mle-in-r/230946#230946) for other "nonstandard" uses of survreg.


Trouble setting up chartkick with flask

Question: I would like to use the python package chartkick-0.4.0 to add graphs in my flask app. However, I am new to flask and having trouble setting it up. The readme file for the project states:
 Add chartkick to jinja_env and static_folder:

 app = Flask(__name__, static_folder=chartkick.js(),
 static_url_path='/static')
 app.jinja_env.add_extension("chartkick.ext.charts")

I have added chartstick.js to my templates/js folder. However, the line
app=Flask(__name__,static_folder=chartkick.js(), static_url_path='/static/js/')

throws a
NameError: "chartstic" is not defined.

What am I doing wrong ? How do i set up chartstick properly with flask?
This is my template:

{% bar_chart data %}

UPDATE ** and this is my views file now:
from flask import Flask, jsonify, render_template, request
import chartkick

app=Flask(__name__,static_folder=chartkick.js(), static_url_path='/static/js/')
app.jinja_env.add_extension("chartkick.ext.charts")

@app.route('/chart')
def first_graph():
    data = {'Chrome': 52.9, 'Opera': 1.6, 'Firefox': 27.7}
        return render_template('first_graph.html', data=data)

if __name__=="__main__":
    app.run(debug=True)

Comment: i tried from chartkick import * but still the same error
Comment: Not `from chartkick import *`. Just import the name chartkick with `import chartkick`.
Comment: thank you. however it only displays a page with a "Loading...." text on it. I will edit my post with my template and view file
Comment: What errors do you have in JS console?
Answer: The problem is really simple. You have invalid HTML in this statement:

Note the doubled left-side quotes. A secondary problem is that you have to specify the Javascript file without a path. The statement with these two errors fixed becomes:

And now you should get your chart.
Note: the way their documentation tells you to set up your static path folder is awful. They are basically pointing the application's static folder to their own folder, which will prevent you from having any of your own static files. A much better way to handle this is to create a blueprint for chartkick and have the static folder for the blueprint point to their Javascript file.
Comment: thank you! it worked like a charm. they do not have scatter plots for python chartkick right? only the ones provided in the readme file
Comment: Looks like they only support line, pie column, bar and area charts at this time. Highchart does support scatter though, if you are willing to code it in Javascript directly.

Trouble setting up chartkick with flask


BreezeJs/EntityFramework Order By null/empty string handling

Question: I have an application making use of BreezeJs querying a standard WebApi controller backed by an EntityFramework and SQL store. When I use an order by clause, I wish NULL and empty string values to always be sorted last (for BOTH ascending and descending order).
In SQL or even EF server side this is not a difficult task. However, I've not been able to locate the documentation for BreezeJs which shows how this can be accomplished.
The ideal solution would be some way to modify my EF context so this is the default behavior when any Order By is detected, prior to query hitting the DB. Failing that, perhaps there's a way I've missed  in Breeze itself to specify how nulls are handled in the results.
Worst case, I'll need to write two separate queries client side and merge the results, one for with and one without values in the specified column.
Suggestions?
Answer: This is an interesting question.
Breeze currently has a class called LocalQueryComparisonOptions that may be used to control whether client side ordering is case sensitive and whether string comparisons should be SQL92 compliant. The idea here is that this class should be used to match whatever ordering rules are used by whatever server implementation you have selected.
It sounds like what you want is to also have the option to completely control local ordering of a query, probably by providing your own ordering function.  If this is the case, please add this to the breeze User Voice and vote for it.  This seems very reasonable and if it gets enough votes I think it makes sense to add it to the product.
Note that you will still need to insure that EF ( or whatever backend you are using) adheres to these same ordering rules.
Also, out of curiosity, how do you make EF always order nulls this way?
Comment: When querying on the server side, one can simply do something like this:

db.MyCollection.OrderBy(n=>n.MyPotentiallyNullField == null).ThenBy(n=>n.MyPotentiallyNullField);

This is because "false" will be ordered before the actual field itself. I wish there was a way to make this ordering occur on the SERVER before the client even gets the results. If I was to do that on the client, Chrome would be very angry at the multi million rows it gets ;)
Comment: You can force this ordering to occur on the server by returning your IQueryable with the clauses you mention above included. Of course, any order by clauses coming  coming from the client will be applied after these, but if there is no client side order by clause on the same properties then you will get the results that you want.  Does this make sense?
Comment: It does, but I'd rather not have the overhead in cases where there's no sort requested, rather than querying off another end point. I would rather have that be transparent. I'm currently exploring intercepting queries at the db context level and dynamically adding my clause when an orderby is detected on a nullable string field. Failing that, I'll most likely do that which you suggest, simply make a new end point on my Api controller that already has the sort "built-in".
Comment: You know you have access to the Request within your Web API Action method (e.g, `this.Request.RequestUri.Query`) and can look for the "$orderby" OData keyword. If you see it, you know a sort is requested and can add your magic LINQ phrases ... if not, you carry on without them. It's hacky ... but not TOO hacky :)


Given $x_{0}$, let $f(x) = \|x-x_{0}\|$. Show tha $f$ has a minumum on any closed, nonempty set $A \subset \mathbb{R^{n}}$

Question: Given $x_{0}$, let $f(x) = \|x-x_{0}\|$. Show tha $f$ has a minumum on any closed, nonempty set $A \subset \mathbb{R^{n}}$.
I tried to do a test for reduction to the absurd, but it was a little difficult to get the result. Perhaps you find a simpler way to demonstrate this exercise using elements of real analysis.
Comment: @MaximilianJanisch  Thanks, edited.
Comment: Any bounded closed subset of $\mathbb{R}^n$ is compact.  Show that $f$ is continuous.
Comment: So $f$ is continuous and, as $f$ wanders off to $\infty$ in a very nice way as $\|x\|\to\infty$, you can always look for the minimum of $f$ on a bounded closed subset of $A$. [Can you take it from here?](https://math.stackexchange.com/questions/109548/x-compact-metric-space-fx-rightarrow-mathbbr-continuous-attains-max-min)
Answer: Did you tried use the following fact?

All limited sequence in a closed set has a convergent subsequence to an element of this set.

Try construct a sequence in $A$ s.t. the distances to $x_0$ are decreasing (at least non-increasing).
Answer: Examine $\ell=\inf_{x\in A} f(x)$. We know $\ell \ge 0$, because $f(x)\ge 0$ always. We need to show that $f$ attains its infimum. Here's a sketch of how: let $(x_n)$ denote a sequence of elements in $A$ so that $f(x_n)\to \ell$ (why does such a sequence exist?).  In particular, we see that $\lVert x_n-x_0\rVert \le\ell+\varepsilon$ for $n$ sufficiently large, so that the sequence is eventually contained in a compact neighborhood of $x_0$, and so up to passing to a subsequence, we may assume it converges.
So, $(x_n)\to x_\infty$, a limit point. Now, $x_\infty\in A$, because $A$ is closed. By continuity of $f$ (justify!), we see that $f(x_\infty)=\ell$ (justify!), and so $f$ attains its minimum on $A$.
Answer: Let $C$ be a closed subset and $x\in C$. We denote by $r=\|x-x_0\|$. Write $L=B(x_0,2r)\cap C$ is compact, since $f$ is continuous, the restriction of $f$ to $L$  attains its minimum at $y$. $f(y)$ is the minimum of the restriction of $f$ to $C$. Suppose that $f(z)<f(y), z\in C$ it is equivalent to saying that $\|z-x_0\|<\|y-x_0\|<r$. This implies that $z\in B(x_0,2r)\cap C$, contradiction.
Answer: Without loss of generality, $x_0=0.$ If $0\in A$, the minimum is $0.$ If not, let $N$ be the first integer such that $A\cap \overline B_N(0)\neq \emptyset.$ Now, $A\cap \overline B_N(0)$ is compact, and $f$ is continuous, so there is a $y\in A\cap \overline B_N(0)$ such that $f(x)=\|x\|\ge \|y\|$ for all $x\in A\cap \overline B_N(0).$ Now let $x\in A.$ There is a first integer $n$ such that $x\in \overline B_n(0).$ If $n\le N$, then $f(x)=\|x\|\ge \|y\|$. On the other hand, if $n>N,$ then again, $\|x\|>\|y\|$ because $x\notin \overline B_N(0).$ So, if fact, $y$ is a global minimum for $f$.
Answer: Hint Pick some $a \in A$. Let $\|x_0-a\|=R$.
Denote by $K:= \{ x \in \mathbb R^n : \| x -x_0\| \leq R$.
Show that $A \cap K$ is closed in $K$, and hence compact and non-empty.
Deduce that $f$ attains its minimum on $A \cap K$.


Bind an Array with dynamic length to an Adaptive Card

Question: I was wondering how I could bind an array which can have a dynamic length to an element in Microsofts Adaptive Cards. Specifically, I want to make a list with instructions and display those in "kind-of-a-list" with a dynamic layout. In a perfect world, ths list would be filled dynamically and adjust its length to the length of the array. Any ideas/workarounds?
Card Payload:
<code>{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.2",
"body": [
    {
        "type": "TextBlock",
        "text": "${title}",
        "size": "Medium",
        "weight": "Bolder",
        "wrap": true,
        "separator": true
    },
    {
        "type": "FactSet",
        "facts": [
            {
                "title": "1.",
                "value": "${instructions[1]}"
            },
            {
                "title": "2.",
                "value": "${instructions[2]}"
            },
            {
                "title": "3.",
                "value": "${instructions[3]}"
            },
            {
                "title": "4.",
                "value": "${instructions[4]}"
            },
            {
                "title": "5.",
                "value": "${instructions[5]}"
            }
        ],
        "isVisible": false,
        "$data": "${$root['instructions[0]']}",
        "separator": true
    }
]
}
</code>
Card Data:
<code>{
"title": "Instructions:",
"instructions" : [
    "blablablba",
    "qwerertzasdfadfds fasdf ",
    "asdfkjhasf 3 asdflkjw",
    "Lorem ipsum dolor mi",
    "hello world",
    "last instruction"
]
}
</code>
Answer: the best way to do this is if you change your JSON a litle bit:
<code>{
"title": "Instructions:",
"instructions": [{
        "id": "1",
        "text": "blablablabla"
    },
    {
        "id": "2",
        "text": "qwerertzasdfadfds fasdf "
    },
    {
        "id": "2",
        "text": "asdfkjhasf 3 asdflkjw"
    },
    {
        "id": "3",
        "text": "Lorem ipsum dolor mi"
    },
    {
        "id": "4",
        "text": "hello world"
    },
    {
        "id": "5",
        "text": "last instruction"
    }

]
}
</code>
This way you can build the card like this:
<code>{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.3",
"body": [
    {
        "type": "TextBlock",
        "text": "${title}",
        "size": "Medium",
        "weight": "Bolder",
        "wrap": true,
        "separator": true
    },
    {
        "type": "FactSet",
        "facts": [
            {
                "$data": "${instructions}",
                "title": "${id}.",
                "value": "${text}"
            }
        ],
        "separator": true
    }
]
}
</code>
Which would look like this:


How to put conditional multiple recipients in drupal 7 default contact us form

Question: I am working on a multilingual Drupal 7 site using a single contact us form. It has 4 languages. The contact us form is multilingual i.e labels of form fields changes, depending on the language you have selected. But it has a single recipient now.
Now I want it to have conditional recipients i.e if English is selected the contact us form should be emailed to email@example.com and if Danish is selected then to firstname.lastname@example.com and so on.
Any suggestions how to do it ?
website form : http://dev-salman.com/venuepoint/contact
Answer: With this module https://www.drupal.org/project/tcontact you can create different contact categories for each language so you can specific different recipients.
Answer: Refer to my answer to the question "Contact form is not being translated" related to the usage of the i18n_contact module. That's a prerequisite for the contact form to be available in multiple languages. However it will not allow you to specify an eMail ID for each language.
I'm not familiar (yet) with the tContact module, but it might help to implement what you're trying to setup. Here's an excerpt about it (from its project page):

... allows you to make the site-wide contact form fully translated on multilingual sites. Categories can be set to display only in a specific language, or they can be language neutral. Depends on the core "contact" module for the site wide contact form.

If with the tContact module you still can't get it to work, then I'd investigate if sending eMails by using the Rules module you can get it to work. E.g. by using the patch in comment #3 of issue # 1472316. For more details about this issue, visit the question about  "Rule condition to check which language is used by the visitor?".


Caesar Shift Encryption Objective C

Question: I'm learning Objective C and iOS development. I'm trying to recreate some of the projects we did in my Java class (I know they're completely different) but I'm running into trouble in one of the projects. We were doing a caesar shift in a lab one day. A string manipulation lab. It was a really basic deal in Java... a for loop through the string and change each character. I can't seem to find any way to change individual characters in Objective C. I've looked through the NSMutableString documentation and NSString documentation and I know I can do a
[NSString stringByReplacingCharactersInRange:(NSRange *) withString:(NSString *)
but that doesn't really help because I don't know what I'm going to be replacing with. I need to find a way to grab a character at a specific index and change it. Any ideas?
Answer: Sounds like you are looking for the [NSString characterAtIndex:(NSUInteger)] method
E.g.
NSString *string = @"abcde";
NSString *character = [NSString stringWithFormat:@"%C",[string characterAtIndex: 0]];
NSLog(@"%@", character);

Result: a
Using this, and an NSMutableString, you can build the string you need.
Ussing appendString you can add to the end of an NSMutableString
Comment: `-characterAtIndex:` returns a `unichar`, not a `NSString`. -1 until fixed.
Comment: I swear I looked and didn't see anything about characterAtIndex! That's exaclty what I would use for Java `string.chatAt();` that's frustrating. Thank you for the answer though.
Answer: Probably the best way to do this would be using a good old C-string, as that allows you to change the bytes without the overhead of reallocating a different string every time:
NSString *ceasarShift(NSString *input)
{
    char *UTF8Str = strdup([input UTF8String]);

    int length = [input length];
    for (int i = 0; i < length; i++)
    {
         UTF8Str[i] = changeValueOf(UTF8Str[i]); // some code here to change the value
    }

    NSString *result = [NSString stringWithUTF8String:UTF8Str];
    free(UTF8Str);

    return result;
}

This reduces overhead, and although you have to free the data you allocated when you are done, it gives you the advantage of not relying on a high level API, improving performance drastically. (The difference between an array set and a dynamic method lookup is ~5 CPU cycles, which means a lot if you are doing any major sort of encryption)
Maybe also look into NSMutableData for this kind of task, instead of NSString, as the random \0 may per chance appear in the result string.
Comment: This is asking for trouble if the input contains anything that requires more than one byte to represent in UTF8 -- parsing a UTF8 string byte-by-byte as an ordinary C string can only end in tears.
Comment: I appreciate the answer and I'm sure you have it right... but the only thing I know about regular C is the fact that you can define a function. I started with Objective-C just this week. Maybe one of these days this answer will be better for me but for now it's a bit dangerous ;)

Caesar Shift Encryption Objective C


Is it viable to use w32tm /stripchart to judge time variance of two Windows hosts from each other? A better way to discover two host's time variance?

Question: Is it viable to use w32tm.exe's option stripchart to judge the time variance of two hosts (within a very small margin of error)?  Note that w32tm.exe's stripchart function is separate than the algorithm used by the Windows Time service itself.
If not, what are some alternate methods?
Thanks,
Matt
Answer: The answer to your first question is "Yes, w32tm.exe (Windows Time) is able to measure the time variance of two network hosts within a very small margin of error (relative term.)"
The answer to your second question is "Yes, there are better ways to measure time variance between two hosts. Microsoft says so right here. They point you to NIST, which lists a whole bunch of other pieces of software (and hardware) that is probably better than w32tm.exe. I'm sure many of them are, since Microsoft clearly does not support w32tm.exe as an ultra high precision tool."
Windows Time (and w32tm.exe) are RFC 1305 (NTPv3) compliant, which includes compensating for network latency. Source and Source. Compensating for network latency is one of the very basic features of the network time protocol. (See Marzullo's algorithm, which is what NTPv3 uses.)
What frustrates me about your question is that high precision is so important to you, but you don't give any hints as to what exact precision you need. 1 second? 1 millisecond? 1 nanosecond? General purpose computers have a clock resolution that is bound to the frequency of clock interrupts received by the processor, which is usually controlled by a crystal oscillator that runs at 32.768 KHz (a power of two,) but that's sensitive to temperature, voltage, etc. The HAL on a typical Windows machine defaults to configuring the real-time clock to fire every 15.6 milliseconds, or about 64 times a second.  However, you can still crank the RTC down to 1ms, and you can also subdivide that 15.6 millisecond time slice via software into even smaller slices for high-performance applications. Regardless, the NTP timestamp itself is a 64-bit unsigned fixed point number and so has a theoretical limit of about 232 picoseconds of precision, but the Windows Time implementation does not even approach that. Windows Time displays an NTP precision of -6 and doesn't support some of the latest and greatest NTP algorithms so in reality it could probably never reliably produce a precision tighter than one hardware clock tick, or plus or minus 16 milliseconds.
General purpose operating systems are not extraordinarily great clocks, especially if the time keeping algorithms are implemented in user mode where the executing threads get preempted constantly. Highly precise clocks are expensive.

The picture above is the frequency of clock interrupts on the system. Note that even clock interrupts (IRQL 28 on 32-bit Windows and IRQL 13 on 64-bit Windows) can be preempted by higher interrupts, such as inter-processor interrupts, and can cause accurate time accounting to be deferred, if even by a nanosecond.
So back to NTP.
w32tm /stripchart /computer:10.0.1.8 is a perfectly valid way to test the time delta between one Windows machine and another. It does account for network latency, which is implied by it being NTPv3 compliant as we discussed above. But don't take my word for it. You can see the transaction for yourself in a Wireshark trace (client packet sent by w32tm.exe to a Windows NTP server):

Microsoft does not guarantee sub-second accuracies using Windows Time, because they do not need to support that in order for any of their products to work.  However, that does not mean that w32tm.exe is not still capable of sub-second accuracy.
If you truly need more accurate time than this, you might eek out an extra millisecond or 10 of precision with another implementation of NTP that uses slightly different algorithms. But if you really need more accurate time, I personally wouldn't suggest NTP at all. I'd wire a cesium clock straight up to your machine and not use a preemptive operating system.
Edit 5/2/2017: The above information is out of date and does not necessarily apply to Windows Server 2016 and beyond. Microsoft has made some serious improvements to the accuracy of Windows Time in later operating systems.
Comment: I'm editing my question so as to avoid extended discussions in comments.
Comment: Thanks Ryan.  Thanks for posting the article, which I think is focused on how the `Windows Time` service is not accurate versus that of the `w32tm.exe` `stripchart` function itself.  I am trying to figure out how viable it is to run `stripchart` on two separate PCs targeting an onsite NTP server.  It is because I am using an application that outputs time stamps from `now()` (local machine time), and I need to compare the log entries of the output on both machines for drift.  Maybe [ntp](http://www.meinbergglobal.com/english/sw/ntp.htm) has another similar tool to measure drift.
Comment: Does the `stripchart` function if `w32tm.exe` handle packet latency properly?
Comment: Where are you seeing a clear statement that says that the `w32tm.exe` tool's function `stripchart` accounts for latency?  I see that the `Windows Time` service: "[passes information] to the clock discipline algorithm, which uses the information gathered to correct the local clock of the computer, while compensating for errors due to network latency and computer clock inaccuracy."  I just ran `stripchart` and monitored API calls with rohitab's API Monitor, and it doesn't look like any latency measurements are taking place; but I don't know.
Comment: Thanks Ryan! You've answered my question cross referencing the RFC and the time data within the packets (which I will use to see if I see offset between that and the output of w32tm.exe's stripchart) [I will post back when I finish the test with these results]. Thanks for adding in additional info on the technicalities of off set and the poor time keeping capabilities of chips that aren't dedicated to time keeping. I'm not measuring atom collisions here, so we're mostly concerned with millisecond drift (possibly micro-second later). Simply, local CPU utilization causes the most drift for us.
Answer: I can not locate whether w32tm.exe's stripchart accounts (or how it accounts) for latency and jitter.  Instead, I located a thread on the ntp mailing list that directed the user(s) to three additional solutions that do measure latency and jitter:

satsignal's NTP Monitor (disqualified since poll times are no less than 60 seconds)
satsignal's NTP Plotter (plots historical loopstats files from ntpd)
meinberg's NTP Time Server Monitor (requires a local ntpd service)


Q: NGINX - Dynamically mapping multiple different domain names through 'map' to use as "server_name"

Question: Looking for some advice and peer input - I'm currently trying to configure NGINX in a way so that I can configure multiple websites at the same time without having to set up individual VHost configs or declaring multiple 'server {}' entries with duplicate config parameters in my NGINX configuration. I have partial success thus far but I'm worried that in doing this it might create security concerns or inadvertently have too broadly defined range. I'm also unsure how to properly/correctly use the "default" value in 'map' directive.
All in all, currently I have a typical NGINX configuration with 2 VHost configs for one domain name (and a 'www.*' subdomain for it) in 'nginx_root/sites-available' - one for HTTP (port 80) and one for HTTPS (port 443) which are soft-symlinked to 'nginx_root/sites-enabled'. But as previously mentioned - I'd like to add additional domain names but keep the amount and size of these configs as they currently are by using the NGINX 'map' features to set-up new domain names dynamically.
The way I've mapped it at the moment is by creating a config file: 'nginx_root/mapped_fqdn.conf' which I have then included early in my NGINX config (so that it gets mapped before it's called/referenced further in the config) and then using the map as 'server_name' parameter. If it's possible to expand this to other config parameters and safe to do so, I'd like to add the new domain names in this same way.
mapped_fqdn.conf:
### Domain name mapping
    map $host $fqdn_map {
        hostnames;
        default 0;

        domain-1.com 0;
        www.domain-1.com 0;
    }

00-http.conf:
server {
    listen 80;
    listen [::]:80;
    server_name $fqdn_map;
    root /var/www/public_html;
    index index.html;
    access_log /var/log/nginx/nginx_access.log nginx_access;;

    ### HTTP to HTTPS Redirect
    return 301 https://$server_name$request_uri;
}

For the sake of not repeating I'll skip adding the HTTPS config, which looks about the same, except it has some few minor additional parameters, along with the SSL-specific config parameters added and doesn't have the 301 redirect, which is present in the HTTP config.
The way I see it, I can add the additional domain names in the same way...
example of updated mapped_fqdn.conf:
(the way I see is that perhaps adding specific subdomains would be safer than adding them like wildcards, i.e.: "*.domain.com")
### Domain name mapping
    map $host $fqdn_map {
        hostnames;
        default 0;

        domain-1.com 10;
        www.domain-1.com 11;

        domain-2.co.uk 20;
        www.domain-2.co.uk 21;
        subdomain1.domain-2.co.uk 22;
        subdomain2.domain-2.co.uk 23;

        domain-3.com 30;
        www.domain-3.com 31;
        subdomain1.domain-3.com 32;
    }

example of updated 00-http.conf:
server {
    listen 80;
    listen [::]:80;
    server_name $fqdn_map;
    root /var/www/$fqdn_map/public_html;
    index index.html;
    access_log /var/log/nginx/$fqdn_map-nginx_access.log nginx_access;;

    ### HTTP to HTTPS Redirect
    return 301 https://$server_name$request_uri;
}

However, before I proceed, I'm concerned about how would this work with directories, i.e.: Let's Encrypt certificates for HTTPS config - is it possible to specifically select which mapped items to use? For example: $fqdn_map{10|20|30} (this way selecting only the domain names without subdomain names).
    ssl_client_certificate /etc/ssl/certs/$fqdn_map{10|20|30}/cloudflare_origin.pem;
    ssl_certificate /etc/letsencrypt/live/$fqdn_map{10|20|30}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/$fqdn_map{10|20|30}/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/$fqdn_map{10|20|30}/chain.pem;

Or perhaps I'm going about this the wrong way and I should map FQDN and subdomains separately ... or maybe just create symlinks and junctions from mapped subdomain directories to fqdn directories (which, while resulting in less/cleaner configs, leads to more symlinking kinda defeating the purpose of "doing less but achieving more")?
Comment: This look really complicated to me. I would recommend using a configuration management system to generate the configuration files. I also doubt that this approach doesn't even work at all: `server_name` would receive value `0` when the request has `Host:` header `domain-1.com`. `server_name` must always have a FQDN or regular expression in it so that it would work correctly.
Comment: Thanks for the input - this is why I'm looking for peer input on this topic. I'm not entirely familiar with the 'map' directive and how to construct a valid formula. Some sort of config manager could certainly work from the viewpoint of managing configs, but I'm afraid that this will lead to more configuration files involved and potentially pose as a threat or a complication in the future (instead of just configuring the web server, now I have to worry about configuring the software that configures the server).
Comment: From the code samples I've seen, 'map' is usually used for redirections. Also, would it work if instead of numbers I assigned FQDN's? For example, one site with different subdomains would have the assigned value of a single domain? That would be a more welcomed result - so that a mapped domain name as a server_name, with multiple subdomains would point towards the same Certbot certificate directory (certbot creates unified certificate bundles for each domain with all the subdomains included in the same directory) and the same logs (condensed logs instead of creating different standalone ones).
Comment: You should have presented your exact problem directly in your question, that would have avoided extra work here. What you need is a `map` where you map host names to certificate directories, and then use the mapped variable in the certificate directory statement.
Comment: I like what you're suggesting - it sounds exactly like what I'm trying to achieve... if you could write it up as an answer below I'd accept and mark it as the answer to my situation.
Comment: I updated my answer.
Answer: If you simply want to map hostnames to certificate directories, then you can use this approach:
map $http_host $certdir {
    hostnames;

    dom1.example.com dom1;
    dom2.example.com dom2;
    .example.com dom;
}

server {
    listen 443 ssl;

    server_name something.example.com;

    ssl_certificate /path/to/certificates/$certdir/dom.crt;
    ssl_certificate_key /path/to/certificates/$certdir/dom.key;
    ...
}

Here $certdir value will be dom, since something.example.com matches .example.com in the map definition.
--- First answer which answered a question which was not originally asked ---
Maybe you are looking for this:
server {
    listen 80 default_server;

    server_name _;

    root /var/www/$host/public_html;

    ....
}

We specify a default_server block here, which processes all requests that do not have a matching virtual host elsewhere.
Then we use the $host variable, which contains the domain name.
If you want to use more specific matching for only some domains, use regular expression:
server {
     listen 80;

     server_name ~ ^(domain-1.com|domain-2.com)$;

     root /var/www/$host/public_html;

     ....
}

You can refine the regular expression.
However, as I stated in my comment, this will place restrictions how you can customise each server block. You could use include /etc/nginx/conf.d/$host.conf to add domain specific configuration in another file.
Still, in the long run a configuration management system is the best option.
Comment: I was afraid it might not be possible, but the progress so far lead me to believe it might. I do like the second code snippet - it's simpler and I hadn't thought about doing it this way. However, reason why I was looking for a way to map it is in the interest to have the corresponding sites use the correct Certbot SSL certificates. For example - domain1.com, subdomain6.domain.com and subdomain1.domain1.com look for the certificates in '//live/domain1.com/*' instead of '//live/subdomain1.domain1.com/*' or '//live/subdomain6.domain1.com/*'.


Moving site to another domain, install appears

Question: I move the website to another domain on the same server, by copying all files to the other domain's folder.
When I visit the new domain the install.php appears. Trying to navigate to another page like /user/login redirects to the installer as well.
I don't think I need to change database settings because it's on the same server.
Any ideas why?
Answer: When install appears on a site that you expect to be fully installed, the problem is usually with the file settings.php (in the sites/default/ directory).
It may be missing in action, not visible to the web server user, or unable to establish the credentials to connect to the site's database.
Comment: Thanks, that did it. sites/default had no write permissions, so my copy failed there.
Answer: As Gisle said problem looks with settings.php either it has not been created or your web server has not permissions to read it,check under sites/default directory. If its not there then just copy the default settings.php and change database settings as your current database details.
As a gentle reminder ensure you have .htaccess file in your drupal root directory if your server is linux based , this file will be hidden.


Inverse trigonometric Problem

Question: For any $x \in [-1,0) \cup (0,1]$, how can I prove that:
$$\sin^{-1}(2x\sqrt{1-x^2})=2\cos^{-1}x$$
Also, can someone explain to me how to understand the graphs of $sin$ and $cos$ functions?
Comment: your expression is unclear. Either you type with LaTeX (very recommended!), or *at least* use parentheses and then *again* parentheses to write roots in exponential form.
Comment: I tried to rewrite the equation using LaTeX, can the OP confirm this is what he meant?
Comment: This equality isn't valid for all $x\in[-1,1]$ take for example $x=0$.
Answer: Hint
Consider $x=\cos t$ and recall that $2\sin t\cos t=\sin 2t$.
Comment: @Sami, what if $x=-1?$
Comment: @labbhattacharjee The equality is only valid for $t\in [0,\frac\pi4]$ so $x\in\left[\frac{\sqrt2}2,1\right]$.
Answer: Hint: If $\theta=2\cos^{-1}(x)$, then $x=\cos(\theta/2)$.
What is $\sqrt{1-x^2}$ ?
What is $2x\sqrt{1-x^2}$ ?
Comment: Congratulations for **100k** et bonne continuation.+1
Comment: @SamiBenRomdhane: thanks! I'll keep on keepin' on...
Answer: Sufficient care has to be taken while dealing with Inverse trigonometric functions
as the principal values of $\cos^{-1}x$ lies in $\left[0,\pi\right]$ whereas it lies in  $\left[-\frac\pi2,\frac\pi2\right]$ for $\sin^{-1}y$
$\displaystyle\iff0\le2\cos^{-1}x\le2\pi$
Case $\#1:\displaystyle2\cos^{-1}x$ will be $\displaystyle\sin^{-1}2x\sqrt{1-x^2}$
iff $\displaystyle-\frac\pi2\le2\cos^{-1}x\le\frac\pi2\iff -\frac\pi4\le\cos^{-1}x\le\frac\pi4$
$\displaystyle\implies0\le\cos^{-1}x\le\frac\pi4\iff1\ge x\ge\frac1{\sqrt2}$
Case $\#2:$
If $\displaystyle\frac\pi4<\cos^{-1}x\le\frac\pi2\iff\frac1{\sqrt2}> x\ge0$
$\displaystyle\iff\frac\pi2<2\cos^{-1}x\le\pi,\sin^{-1}2x\sqrt{1-x^2}=\pi-2\cos^{-1}x$
Similarly, Case $\#3:$
$\displaystyle\iff\frac\pi2<\cos^{-1}x\le\frac{3\pi}4,\sin^{-1}2x\sqrt{1-x^2}=\pi-2\cos^{-1}x$
and Case $\#4:$
$\displaystyle\iff\frac{3\pi}4<\cos^{-1}x\le\pi,\sin^{-1}2x\sqrt{1-x^2}=2\pi-2\cos^{-1}x$
For example, if $\displaystyle x=-1,\cos^{-1}x=\cos^{-1}(-1)=\pi$
But, $\displaystyle\sin^{-1}\{2x\sqrt{1-x^2}\}=\sin^{-1}0=0$
Comment: Related : http://math.stackexchange.com/questions/672575/proof-for-the-formula-of-sum-of-arcsine-functions-arcsin-x-arcsin-y

Inverse trigonometric Problem


Find the equation of the line parallel to $2x-y=4$ through point $(0,3)$

Question: I have used the point-slope formula and concluded this:
$$2x-y=4$$
$$-y=-2x$$
$$-y-3=-2(x-0)$$
$$-y-3=-2x+0$$
$$2x-y=3 $$
The answer in the text book says $2x-y=-3.$
Is my answer wrong? and if so how could i improve it?
Comment: Try plugging in $x=0$ into your equation. What do you get for $y$?
Comment: Right, and what's the $y$ value that you should have? Remember what they told you in the problem.
Comment: "I have used the point-slope formula"  Could you please state exactly what your text says the "point-slope formula" is.  ... I dont know if you are allowed to just throw the $4$ away... annd in any event you should isolate that *positive* $y$... not the $-y$.  If you had done $y = 2x -4$ rather than $-y = -2x + 4$ (or $-y=-2x$) I don't think you ever would have made your error.
Comment: i get y=-3 through that
Comment: Yes correct , so we plug in the x and y value at end of the equation?
Comment: As @Crosby indicates you can always check which is right by putting in the values you are given - you should get used to doing this as a check on your workings, at least until you are really confident - and even beyond that it will save you from many a slip and error of signs.
Comment: Your main error was $-y = -1*y$ so when you plug in $y-3$ you should have $-(y-3) = -y + 3$ and not $-y -3$.... but frankly you shouldn't just write down a bunch of stuff without explaining them.  If $2x-y = 4$ is a true statement, then $-y=-2x + 4$ is a true statement and $-y=2x$ is a *FALSE* statemen.  And if $-y = 2x$ is a true statement then $-(y-3) = 2(x-0)$ is not true.  Your mechanics of doing these are correct but your explanation of *what* you are doing is utterly wrong and makes no sense.
Comment: y-y1=m(x-x1)  the third column
Comment: There's a much simpler way IMO to approach this problem: all lines parallel to the original one have equations of the form $2x-y=c$. Plug in the coordinates of the point and solve for $c$.
Answer: If you plug $x=0$ into your final equation, you get $y=-3$. This means that you must have made a mistake somewhere: the question demands that the line pass through $(0,3)$, not $(0,-3)$. As Mark Bennet pointed out in the comments, it's a good habit to check your work by plugging in the values you've been given.
Regarding your work, you seem to have misapplied the point-slope formula. If we solve for $y$ in the original equation, we see that
$$y=2x-4,$$
so the parallel line has a slope of $2$. Therefore, the equation you want is
$$\color{blue}{y}-3=\color{blue}{2}(x-0).$$
You wrote
$$\color{red}{-y}-3=\color{red}{-2}(x-0).$$
Can you take it from here?
Edit: We have
$$y-3=2x.$$
From here, to get to your textbook's answer, we can subtract $y$ from both sides. That would give
$$2x-y=-3.$$
We could also add $3$ to both sides. That would give
$$y=2x+3.$$
Both equations are correct and represent the same line.
Comment: Thank you good sir! you have cleared my mind :)
Comment: The slope can't be subtracted from both sides?

so we just apply the formula as its +y-y1=+m(x-x1)?
Comment: i picked it up from y−3=2(x−0). and got this

y-3=2x-0

Here what should i do next? move 2x to y or y to 2x? this part is what really confuses me and i make the mistake cause if i move 2x to y i get y-2x=3 and vice versa i get 2x-y=-3
Comment: @Yoda The most logical thing to do would be to isolate $y$. We can do that by adding $3$ to both sides. What does that give you?
Comment: if i isolate it goes as y=2x+3
Comment: @Yoda Great! That's the correct answer and equivalent to the answer in your textbook. Are you confused as to how to get to your textbook's answer?
Comment: Yes! the book says 2x-y=-3
Comment: @Yoda Yes. I know. The equation $y=2x+3$ is equivalent to $2x-y=-3$. I'll edit my answer.
Comment: so its the same thing? both are correct? , EDIT: just tried it and got -3=2x-y
Comment: @Yoda Yes. They both represent the same line. You can rearrange one and get the other.
Answer: Be careful.  And make sure what you say actually means something.
You say $2x-y =4$.  That's not true or accurate.
What if $x = 27$ and $y = 39$?  Well, you might answer, then $(27,39)$ are not on the line described by $2x-y=4$.  But I'd counter you didn't say that $(x,y)$ was one the first line.  You just said $2x-y = 4$ with no explanation as to where $x,y$ came from.
Then you say $-y = -2x$.  Well, if $2x-y = 4$ then $-y=-2x + 4\ne -2x$.  That's contradictory.  Well, you might answer, this is about $x,y$ on a different line so this is a different $x,y$.
But you didn't say you were talking about different values of $x,y$.
....
Which is not to say you are doing anything wrong, but that you aren't explaining what you are doing.
====
Let's start over.
We have an equation for the first line. $2x-y = 4$ and that can be rewritten as $-y =-2x +4$.
In terms of the slope that means $-(\text{any change in }y) = -2(\text{any change in }x)$.  This is true for the first line $2x-y =4$ and for any line parallel to the first line.
This mean for any $(x_1,y_1)$ on a line parallel to the first line that
$-(y-y_1) = -2(x - x_2)$.
And as $(0,3)$ is a point one the second line, we have
$-(y-3) = -2(x-0)$.
And this is where you made your mistake.  You did $-y-3 = -2(x-0)$ and not $-(y-3)=-2(x0)$.
So the equation for the line is $-(y-3) =-2(x-0)$
$-y+3 = -2x$
$2x -y = -3$.
====
It'd be easier to do:
Equation of first line.
$2x - y = 4$ or equivalently
$y = 2x -4$.
So the slope of first line is $2$.
So equation of a parallel line is $y = 2x + b$.  As $(0,3)$ is in the second line we know
$3 = 2*0 + b$ so $b = 3$ and equation for second line is $y = 2x +3$
Or equivalently $2x - y = -3$.
=====
if we assume we know that if parallel lines will have the same $x,y$ coefficients, then it'd be even easier to do:
Then if line one is $2x-y = 4$ then the equation for a parallel line will be:
$2x -y = c$ for some constant $c$.
As $(0,3)$ is a point on the line with have:
$2*0 - 3 = c$
so $c = -3$ and
$2x-y = -3$ is the equation for the seccond line.
Comment: i wrote it step by step as i attempted it , i usually type out first the original equation in first step , my bad
 2x−y=4 is the original equation


Java object which takes the least memory

Question: This is a silly question, but here it goes.
I have a multithreaded program and a "global" Collection of unique elements. I rejected synchronized Set implementations due to performance, for the ConcurrentHashMap. I don't really need the Value part of Map, so I wanted to use the smallest Object in java in terms of memory usage. I solved this issue in a different way (single Boolean object referenced multiple times in the Map), but I am still curious what is the smallest object in Java. I always thought it to be Boolean, but that is not true I think (Java - boolean primitive type - size, Primitive Data Types)
Comment: If you really wanted a `Set`, you can use `Collections.setFromMap(new ConcurrentHashMap())`. This does your work for you :-)
Comment: Primitive types are different from objects. Boolean is immutable wrapper object, boolean is primitive type.
Comment: If you really wanted the smallest Object, though, I'd probably just use `Object`... since it's the base-class of every other `Object`... :-)
Answer: It doesn't really matter, actually, since the value part of each association is fixed to be a reference. You might even use null as value here, but any other (fixed) object reference should be fine (and more convenient sometimes). I'd prefer Boolean.TRUE (or a similar "well known" singleton). You can then test for membership via
if (myMap.get(someKey) != null) { ... }

in addition to
if (myMap.containsKey(someKey)) { ... }

Comment: @alien See edit. In particular, `containsKey` is always available on maps.
Comment: Ok, but then you can't really check if key exists (unless -> KeySet.iterator). Thus the need for a value to be able to perform this check. Or am I missing something?
Comment: @alien01 `Map.keySet().contains()`
Comment: @Dirk using `myMap.get(someKey)` where there is no associated value  (i.e. `get` returns `null`) will cause a `NullPointerException` when trying to auto-unbox.
Answer: If you want a Set<K> that is backed by a ConcurrentHashMap, you should use Collections.newSetFromMap, e.g.
final Set<K> set = Collections.newSetFromMap(new ConcurrentHashMap<K, Boolean>());

Now, if you really want to reinvent the wheel, and care that much about memory usage, I suggest you merely use a plain Object as your value. Since every object in Java inherits from Object (the universal base class), the size of any object in memory must be greater than or equal to the size of a plain Object. You cannot use a primitives since generic type arguments must be Objects.
EDIT: Actually, allocating a particular object to use as your value here will take more memory than using a preexisting object which will likely be allocated for anyways. You can just use a reference to an object that will more or less always be allocated during VM initialization, e.g. Object.class. I really suggest you just use the first solution, though.
Answer: An object's size consists of:

the size of the instance variables it holds
an 8 or 16 bytes header (depending on the Hotspot VM (32/64bit))
a padding: its size is always padded to be a multiple of 8 bytes.

E.g (assuming a 32bit JVM) :
public MyBoolObject {
  boolean flag;
}

will take up 16 bytes: 8bytes(header) + 1byte(instance variable) + 7bytes(padding).
Since you are not interested in the map values you can set them to null. This consumes 4 or 8 bytes memory from the stack (32/64bit).
You might also check this good list on cost/elements of well-known Java data structures:
http://code.google.com/p/memory-measurer/wiki/ElementCostInDataStructures
Answer: The primitive data types are not objects.
Since all objects in java must inherit from the super class Object. Then the smallest conceivable object in java would be a class that you define that has no members. Such a class would be pretty useless.
Answer: The Object class is instantiable and its instances are definitely the smallest objects in Java. However, many other objects have exactly the same footprint, Integer and Boolean being examples on 64-bit VMs. This is due to heap memory alignment.


Fatal error: Call to a member function diff() on a non-object

Question: I have an entityform that has a computed field.
In computed field,
I have to compute two time fields.
When I tried using this code that declares value for the two time field, it works fine
$timefrom = '06:00PM';
$timeto = '8:30PM';
$from = DateTime::createFromFormat('h:iA', $timefrom );
$to = DateTime::createFromFormat('h:iA', $timeto );

$ot_hour =  $to->diff($from);
$ot_minute =  $to->diff($from);
$hour = $ot_hour->format('%h');
$minute = $ot_minute->format('%i');
$hourtominute = $hour*60;
$total = ($hourtominute+$minute)/60;
$entity_field[0]['value'] = $total;

But when I used the variables from the entityform,
$timefrom_value = array_pop(field_get_items($entity_type, $entity, 'field_time_from'));
$timeto_value =array_pop(field_get_items($entity_type, $entity, 'field_time_to'));

$timefrom = $timefrom_value['value'];
$timeto = $timeto_value['value'];
$from1 = DateTime::createFromFormat('h:iA', $timefrom );
$to1 = DateTime::createFromFormat('h:iA', $timeto );

$ot_hour =  $to1->diff($from1);
$ot_minute =  $to1->diff($from1);
$hour = $ot_hour->format('%h');
$minute = $ot_minute->format('%i');
$hourtominute = $hour*60;
$total = ($hourtominute+$minute)/60;
$entity_field[0]['value'] = $total;

It show error "Fatal error: Call to a member function diff() on a non-object"
Why is this happening?
I need some help to solve this. thanks a lot!
Comment: Please don't edit your question in a way that invalidates the given answers. If you remove that code, the answer doesn't make sense anymore.
Answer: It seems like you're getting confused with your $to and $to1 objects. There is typo in your code. Try this on your later code:
$ot_hour =  $to1->diff($from);
$ot_minute =  $to1->diff($from);
$hour = $ot_hour->format('%h');
$minute = $ot_minute->format('%i');
$hourtominute = $hour*60;
$total = ($hourtominute+$minute)/60;
$entity_field[0]['value'] = $total;


How to organize rows of a data.frame in R

Question: I want to build a 3x3 data.frame in R by using two vectors of values: a vector of x-values (xr) and a vector of y-values (yr). xr has 3 values and yr has 9 values.
Here my example:
    xr<-c(1,2,3)
    yr<-c(4,4,4,5,5,5,6,6,6)
    db<-data.frame(x=xr,y=yr)

The resulting data.frame is as follows:
    > db
    x y
    1 4
    2 4
    3 4
    1 5
    2 5
    3 5
    1 6
    2 6
    3 6

However, I would like the data.frame to be like this:
    > db
    x y
    1 4
    1 4
    1 4
    2 5
    2 5
    2 5
    3 6
    3 6
    3 6

The trivial way to do it would be to use another xr, with the explicit sequence of values of x, like this:
    xr<-c(1,2,3,1,2,3,1,2,3)

However I am looking for a way of creating such data.frame by using the same xr as in my first example.
Any help is much appreciated!
Comment: Many thanks, guys! Both answers are quite elegant and make what I wanted. I chose Richard's one just because I learnt more.
Answer: It is just a matter of the each argument of the rep function. It does exactly what its name might suggest.
db<-data.frame(x= rep(xr, each = 3) ,y=yr)
 db
  x y
1 1 4
2 1 4
3 1 4
4 2 5
5 2 5
6 2 5
7 3 6
8 3 6
9 3 6

Answer: To capture any possible differences in run-lengths, along with rep() you might want rle()
data.frame(x = rep(xr, rle(yr)$lengths), y = yr)

To show what I mean, take zr as follows.  It has varying run-lengths (i.e. they are not all 3).
zr <- c(4, 4, 4, 5, 5, 6)
data.frame(x = rep(xr, rle(zr)$lengths), z = zr)
#   x z
# 1 1 4
# 2 1 4
# 3 1 4
# 4 2 5
# 5 2 5
# 6 3 6

Comment: Excellent and very smart base R solution: I like it.
Comment: Thanks @SabDeM - yours too.
Comment: Honestly: I wish I thought yours first :D


Problemas em implementar o recurso do jQueryUI Accordion

Question: E fiz uma pesquisa rápida sobre esse recurso e encontrei essa pagina;
https://jqueryui.com/accordion/
Então criei uma pasta com o nome de GUJ e dentro desta pasta coloquei três arquivos;
Um de JavaScript com o nome de jquery-2.2.2.min.js e o outro com o nome de jquery-ui.js
E coloquei também o arquivo index.html
com essas configurações;
<!DOCTYPE html>

<meta charset="UTF-8">
<title>Insert title here</title>

First header

First content panel

Second header

Second content panel

Era para ter esse resultado;
https://jsfiddle.net/a2w4zs3h/
Mas não aconteceu nada.
aceito sugestões.
Comment: Os caminhos do script estão devidamente corretos?
Answer: Lembre-se que em jQuery o script deve ser usado dentro da função do ready
Faça como abaixo e tudo ocorrerá bem:
          $(document).ready(function(e){ // essa é a parte que você esqueceu
            $("#accordion").accordion({
              collapsible: true
            });
          });

Se a resposta servir, dê um UP.
Comment: muito obrigado, deu certo.


Remover linhas com uma condição em um Dataframe python

Question: Gostaria de saber como excluir as linhas baseado na coluna cliente_id segue a imagem abaixo:

O Objetivo é deletar as linhas onde cliente_id aparece somente uma vez que nesse caso seria o ID 4 e o 5, deixando o restante
Comment: Outra solução seria filtrar os indices dos elementos que não se repetem e então remover linhas baseadas nesses indices. Esse exemplo é bastante específico ao seu problema: https://replit.com/@Augusto-Vasques/dropunique#main.py Para a finalidade aqui do site a [resposta do Paulo Marques](https://pt.stackoverflow.com/a/572990/137387) é mais adequada pois é flexível e permite que outros usuários a reaproveitem aplicando diferentes condições de remoção.
Comment: PS: Quando fizer uma pergunta sobre dataframes ao menos deixe na pergunta um código no formato texto que crie o df de amostra para que as pessoas possam apenas copiar e colar. Toma muito tempo copiar os dados individualmente de uma imagem para então montar o df para então analisar o problema.
Answer: Uma solução é usar o value_counts
# contando items
contagem = df["cliente_id"].value_counts()

# condição para que a contagem seja maior que 1
condicao = contagem.gt(1)

# criando novo df com clientes que apareçam mais de uma vez
novo_df = df[df["cliente_id"].isin(contagem.index[condicao])]

Veja como isto acontece, passo a passo:
In [1]: df = pd.DataFrame({"cliente_id": [1,2,3,4,5,1,2,4,5]})

In [2]: df
Out[2]:
   cliente_id
0           1
1           2
2           3
3           4
4           5
5           1
6           2
7           4
8           5

In [3]: contagem = df["cliente_id"].value_counts()

In [4]: contagem
Out[4]:
1    2
2    2
4    2
5    2
3    1
Name: cliente_id, dtype: int64

In [5]: condicao = contagem.gt(1)

In [6]: condicao
Out[6]:
1     True
2     True
4     True
5     True
3    False
Name: cliente_id, dtype: bool

In [7]: novo_df = df[df["cliente_id"].isin(contagem.index[condicao])]

In [8]: novo_df
Out[8]:
   cliente_id
0           1
1           2
3           4
4           5
5           1
6           2
7           4
8           5

In [9]:

Perceba que o cliente_id = 3 não está no novo_df.


Evaluating a limit using L'Hôpital's rule

Question: I know that it can be also evaluate using Taylor expansion, but I am intentionally want to solve it using L'Hôpital's rule:
$$ \lim\limits_{x\to 0} \frac{\sin x}{x}^{\frac{1}{1-\cos x}} =
\lim\limits_{x\to 0}\exp\left( \frac{\ln(\frac{\sin x}{x})}{1-\cos x} \right)$$
Now, from continuity and L'hopital Rule:
$$\lim\limits_{x\to 0} \frac{\ln(\frac{\sin x}{x})}{1-\cos x} =
\lim\limits_{x\to 0} \frac{\frac{x}{\sin x}\cdot\frac{x\cos x - \sin x}{x^2}}{\sin x} =
\lim\limits_{x\to 0}\frac{\frac{x\cos x - \sin x}{x\sin x}}{\sin x}$$
This is where I got stuck.
If I'm not mistaken the limit is $-\frac{1}{3}$ so the orginial one is $e^{-\frac{1}{3}}$
What should I do different (Or what's is wrong with my calculation?)
Thanks
Comment: Have you tried applying the rule again?
Answer: After rearranging the quotient a bit we can apply L'hopitals rules $2$ more times to get the answer:
$$\lim_{x\to0}\frac{x\cos(x)-\sin(x)}{x\sin^{2}(x)}\underbrace{=}_{\text{l'hopital}}\lim_{x\to0}\frac{-x\sin(x)}{\sin^{2}(x)+2x\sin(x)\cos(x)}=\lim_{x\to0}\frac{-x}{\sin(x)+2x\cos(x)}$$
$$\underbrace{=}_{\text{l'hopital}}\lim_{x\to0}\frac{-1}{3\cos(x)-2x\sin(x)}=\frac{-1}{3}$$
Comment: Thanks, I should have just continue and not give up :)
Comment: You're welcome.
Answer: You can continue it as the following :
$$\begin{align}\lim_{x\to 0}\frac{x\cos x-\sin x}{x\sin^2 x}&=\lim_{x\to 0}\frac{(x\cos x-\sin x)'}{(x\sin^2 x)'}\\&=\lim_{x\to 0}\frac{\color{red}{\cos x}-x\sin x\color{red}{-\cos x}}{\sin^2 x+2x\sin x\cos x}\\&=\lim_{x\to 0}\frac{-x}{\sin x+2x\cos x}\\&=\lim_{x\to 0}\frac{-1}{(\sin x/x)+2\cos x}\\&=\frac{-1}{1+2\cdot 1}.\end{align}$$
Comment: @Elimination: You are welcome.
Answer: Alternative approach using Taylor series:
Using $\frac{\sin x}{x} = 1 - \frac{x^2}{6} + o(x^2)$ we get $\ln(\frac{\sin x}{x}) \sim_0 \ln(1-\frac{x^2}{6})$. This expression has the Taylor expansion $-\frac{x^2}{6}+o(x^2)$.
We can approximate the denominator by $1-\cos x=\frac{x^2}{2} + o(x^2)$.
Then we have:
$\frac{\ln(\frac{\sin x}{x})}{1-\cos x} \sim_0 -\frac{x^2}{6} \frac{2}{x^2} = -\frac{1}{3}$.
This doesn't exactly answer your question, but hopefully it provides a little bit of insight. :)


slow drum beats over fast sub beats

Question: I'm looking for the definition of a drum beat where the main beat is slow, about 70 bpm, but the sub beat is about 3 times faster. What is the official name/style of that playing?
for example andalusia by joe satriani.
Comment: What is a "sub beat"? Is this a term you made up? This is not a standard term.
Comment: I made it up, i means the beats between two primary beats which are defined by beats per minute
Comment: Do you mean playing in 3/4?
Comment: I guess this specific song is in 3/4 but the style of a slow primary beat and fast sub beats is what i'm talking about, for example this https://www.youtube.com/watch?v=KL3ACYEluUw is Not what i'm talking about
Comment: I like this Q a lot & think folks may be missing the point by describing basic music notation (3/4, etc.). Are you asking about a technique where a song simultaneously employs two *drum*beats: one built from long note durations and the other using smaller note durations? The example that came to my mind is **[Knives Out](https://youtu.be/Y-c_LOMbTLc).** The bass establishes a slower beat, & then at 0:18 the drums establish a faster beat. The use of 2 beats in this fashion is a distinct musical technique. (Separately, see sub-beats [**here**](https://en.wikibooks.org/wiki/Music_Theory/Rhythm).)
Comment: Sounds like "Andalusia" is in 6/8 time, which means there are two beats per measure and each beat is divided into three parts. Is that what you're talking about?
Comment: jdjazz yeah exactly
Answer: Not sure about 3 times, but sometimes if the beat is constant and somebody is playing something that seems twice as fast we may refer to that person playing double time.  An example would be somebody switching from quarter notes to eighth notes.
On the flip side, if someone is playing half-time it has the opposite feel.  Changed quarter notes into half notes.
Here is an example of a drummer showing half-time:youtube video
Answer: As the comments suggest, you might benefit by reading up on some basics of musical notation.  THe "Beats per minute" term, while often used, is normally referring to the fundamental meter of the piece.  For example, in 3/4 time, there are three beats per measure, quarter note getting a beat.
But (duh), any musical instrument can play multiple notes per beat.  If your putative drummer is playing sixteenths, there'd be four 'hits' per beat.  If he's whacking the bass drum on the first quarter note of the measure ("downbeat") and hitting something else on every quarter note, you'd be hearing a 3-to-1 rhythm.  And so on.

slow drum beats over fast sub beats


How do I disable resampling in the SpriteKit framework?

Question: If I have a scene, let's say is 100x100. When I scale it up to the size of the phones screen, it gets all blurry, like MIP-Mapping. How can I make it so it is just pixelated when I scale up the scene?
Answer: Change each affeced texture's filteringMode to nearest filtering. Refer to https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKTexture_Ref/Reference/Reference.html#//apple_ref/occ/instp/SKTexture/filteringMode
Comment: That will be an improvement, but the best results will come from point filtering to an integral multiple of the screen res, and then "linearly" filtering from there. Is that possible with SpriteKit?


What's the difference between T(V) and ImT?

Question: Assuming I have the following Linear transformation:
$\mathbb{T}: \mathbb{V} \rightarrow \mathbb{W}$ where $\mathbb {V}$ and $\mathbb {W}$ are vector space.
My question is: What is the difference between writing $\mathbb {T(V)}$ and writing Im$\mathbb {T}$?
Answer: I believe that you should had written $T:V\to W$.
There is no difference. $${\rm Im}(T)=\{w\in W\mid \exists v\in V, T(v)=w\}\subseteq W.$$ Also, $$T(V)=\{T(v); v\in V\}\subseteq W.$$
So, both are the same subset of $W$.


GlusterFS SSL not working

Question: I have been trying for hours to get GlusterFS SSL workong on the management and I/O path to no avail, so I hope you can help me.
I have tried it the simple way:
openssl genrsa -out glusterfs.key 4096
openssl req -new -x509 -key glusterfs.key -subj "/CN=Anyone" -out glusterfs.pem

And then I concatenated the glusterfs.pem from all peers to glusterfs.ca and distributed glusterfs.ca to all nodes.
The following files exist:
/etc/ssl/glusterfs.key
/etc/ssl/glusterfs.pem
/etc/ssl/glusterfs.ca

I have created the file to enable SSL on the management path and restarted glusterd.
touch /var/lib/glusterd/secure-access
systemctl restart glusterd

My glusterd.log is now logging hundreds of entries (dozens per minute) showing an SSL error:
[2017-08-29 22:01:07.535725] E [socket.c:202:ssl_dump_error_stack] 0-socket.management:   error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number
[2017-08-29 22:01:07.535760] E [socket.c:2519:socket_poller] 0-socket.management: server setup failed

The same error occurs in the volume log when I try to enable SSL on the I/O path and the same error also occurs when I try a more complex way of creating my glusterfs.* files. Example:
openssl genrsa 4096 > glusterfs.ca.key
openssl req -sha1 -new -x509 -nodes -days 3650 -key glusterfs.ca.key > glusterfs.ca
openssl req -sha1 -newkey rsa:4096 -days 1825 -nodes -keyout glusterfs.key > glusterfs-req.pem
openssl rsa -in glusterfs.key -out glusterfs.key
openssl x509 -sha1 -req -in glusterfs-req.pem -days 1825  -CA glusterfs.ca -CAkey glusterfs.ca.key -set_serial 01 > glusterfs.pem
openssl verify -CAfile glusterfs.ca glusterfs.pem

But this results in the same error as shown above, no matter what CN etc. I set and no matter whether I set the same or different CN on the peers.
I have also tried creating RSA keys with a key length of 2048 to no avail.
I am running out of ideas and I don't know what this error means in the context of GlusterFS:
SSL3_GET_RECORD:wrong version number

Disclaimer:
I am running CentOS 7 64-bit and GlusterFS 3.11.3 (latest version as of this writing).
Answer: I have fixed the problem this way (only for I/O path SSL support) ... the cipher list shown in the GlusterFS tutorial did not work it turns out and I have now set the following:
gluster volume set mailrep-volume ssl.cipher-list 'HIGH:!SSLv2:!SSLv3:!TLSv1:!TLSv1.1:TLSv1.2:!3DES:!RC4:!aNULL:!ADH'

Also I did not concate the .pem files from the servers but instead created my own self-signed CA as shown in the more complex example above. The .ca file created this way must be the same on all peers and when it is used to sign the glusterfs.pem files no concat certs are needed.
This is my complete volume config:
Volume Name: mailrep-volume
Type: Replicate
Volume ID: 907250fb-d83e-4fe7-a63b-636d952095ff
Status: Started
Snapshot Count: 0
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: eris:/mnt/glusterfs/vmail
Brick2: orcus:/mnt/glusterfs/vmail
Options Reconfigured:
ssl.cipher-list: HIGH:!SSLv2:!SSLv3:!TLSv1:!TLSv1.1:TLSv1.2:!3DES:!RC4:!aNULL:!ADH
nfs.disable: on
transport.address-family: inet
client.ssl: on
server.ssl: on
auth.ssl-allow: eris,orcus

To verify I can do ... (1) and see ... (2)
(1) cat /var/log/glusterfs/glustershd.log | grep SSL
(2) 0-mailrep-volume-client-1: SSL support on the I/O path is ENABLED
(2) 1-mailrep-volume-client-1: SSL support on the I/O path is ENABLED

The error from above (SSL3_GET_RECORD:wrong version number) is fixed now ONLY when using SSL on the I/O path. The problem still exists when I enable SSL on the management path but I don't know how to alter the cipher-lists for that to apply the same fix.
A little side note: The GlusterFS SSL tutorial seems to be horribly outdated. It is also stated that /etc/ssl/glusterfs.* are hardcoded file locations, but at least for SSL on the I/O path you can easily apply custom locations this way (tested and worked):
gluster volume set mailrep-volume ssl.private-key /path/to/your/certkey
gluster volume set mailrep-volume ssl.own-cert /path/to/your/certfile
gluster volume set mailrep-volume ssl.ca-list /path/to/your/cafile

I found those options in the Gluster source.
PS: I have also upgraded to GlusterFS 3.12.0 today (after the fix!)
Answer: After spending a few hours trying to figure out what is wrong with the certificates, I found that, in my case, the problem was a stupid mistake: I haven't restarted glusterd on one of the two servers that were hosting the bricks. Both servers were therefore configured exactly the same, but one of them was unaware of the new SSL configuration.
If you encounter the same issue, the first thing is to run service glusterd restart on all the nodes. If it doesn't work, the other answer, as well a bug report may help.

GlusterFS SSL not working