How to make Swift Codable types more versatile

Question: I currently am working with an API that deals with bus predictions. There is an interesting quirk with the JSON that is returned for the predictions of a certain stop. When there are multiple predictions for a stop, the JSON looks something like this:
...
"direction": {
            "prediction": [
                {
                    "affectedByLayover": "true",
                    "block": "241",
                    "dirTag": "loop",
                    "epochTime": "1571785998536",
                    "isDeparture": "false",
                    "minutes": "20",
                    "seconds": "1208",
                    "tripTag": "121",
                    "vehicle": "1698"
                },
                {
                    "affectedByLayover": "true",
                    "block": "241",
                    "dirTag": "loop",
                    "epochTime": "1571787798536",
                    "isDeparture": "false",
                    "minutes": "50",
                    "seconds": "3008",
                    "tripTag": "122",
                    "vehicle": "1698"
                },
                {
                    "affectedByLayover": "true",
                    "block": "241",
                    "dirTag": "loop",
                    "epochTime": "1571789598536",
                    "isDeparture": "false",
                    "minutes": "80",
                    "seconds": "4808",
                    "tripTag": "123",
                    "vehicle": "1698"
                }
            ],
            "title": "Loop"
        }
...

However, when there is only one prediction for a stop, the JSON looks like this instead:
...
"direction": {
            "prediction":
                {
                    "affectedByLayover": "true",
                    "block": "241",
                    "dirTag": "loop",
                    "epochTime": "1571785998536",
                    "isDeparture": "false",
                    "minutes": "20",
                    "seconds": "1208",
                    "tripTag": "121",
                    "vehicle": "1698"
                }
            "title": "Loop"
        }
...

Notice that the "prediction" is no longer inside an array -- this is where I believe things are getting complicated when using a Swift Codable type to decode the JSON. My model looks like this for the "direction" and "prediction"
struct BTDirection: Codable {
    let title: String!
    let stopTitle: String!
    let prediction: [BTPrediction]!
}

struct BTPrediction: Codable {
    let minutes: String!
    let vehicle: String!
}

Basically what is happening is prediction in BTDirection is looking for an Array of BTPrediction, however in the 2nd case above, this wouldn't be an Array so the decoding fails. How can I make my models more flexible to accommodate both an array or a single object? Ideally, in the 2nd case prediction would still be an array of a single BTDirection. Any help on this would be much appreciated.
Answer: You can try
struct BTDirection:Codable {

    let title,stopTitle: String
    let prediction: [BTPrediction]

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        title = try container.decode(String.self, forKey: .title)
        stopTitle = try container.decode(String.self, forKey: .stopTitle)
        do {
            let res = try container.decode([BTPrediction].self, forKey: .prediction)
            prediction = res
        }
        catch {
              let res = try container.decode(BTPrediction.self, forKey: .prediction)
              prediction = [res]
        }
    }
}

Answer: To add on to Sh_Khan's answer, if you have multiple places in your API responses where this sort of thing happens, you can extract this custom decoding and encoding out to a custom wrapper type so you don't have to repeat it everywhere, like:
/// Wrapper type that can be encoded/decoded to/from either
/// an array of `Element`s or a single `Element`.
struct ArrayOrSingleItem<Element> {
    private var elements: [Element]
}

extension ArrayOrSingleItem: Decodable where Element: Decodable {
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()

        do {
            // First try decoding the single value as an array of `Element`s.
            elements = try container.decode([Element].self)
        } catch {
            // If decoding as an array of `Element`s didn't work, try decoding
            // the single value as a single `Element`, and store it in an array.
            elements = try [container.decode(Element.self)]
        }
    }
}

extension ArrayOrSingleItem: Encodable where Element: Encodable {
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()

        if elements.count == 1, let element = elements.first {
            // If the wrapped array of `Element`s has exactly one `Element`
            // in it, encode this as just that one `Element`.
            try container.encode(element)
        } else {
            // Otherwise, encode the wrapped array just as it is - an array
            // of `Element`s.
            try container.encode(elements)
        }
    }
}

// This lets you treat an `ArrayOrSingleItem` like a collection of elements.
// If you need the elements as type `Array<Element>`, just instantiate a new
// `Array` from your `ArrayOrSingleItem` like:
//     let directions: ArrayOrSingleItem<BTDirection> = ...
//     let array: [BTDirection] = Array(directions)
extension ArrayOrSingleItem: MutableCollection {
    subscript(position: Int) -> Element {
        get { elements[position] }
        set { elements[position] = newValue }
    }

    var startIndex: Int { elements.startIndex }
    var endIndex: Int { elements.endIndex }

    func index(after i: Int) -> Int {
        elements.index(after: i)
    }
}

// This lets you instantiate an `ArrayOrSingleItem` from an `Array` literal.
extension ArrayOrSingleItem: ExpressibleByArrayLiteral {
    init(arrayLiteral elements: Element...) {
        self.elements = elements
    }
}

Then you can just declare your prediction (and any other property that has the potential to be either an array or a single item in your API response) like this:
struct BTDirection: Codable {
    let title: String?
    let stopTitle: String?
    let prediction: ArrayOrSingleItem<BTPrediction>?
}

Answer: Both @TylerTheCompiler & @Sh_Khan provide very good technical input into solution that provide the mechanics of a solution, but the provided code will hit some implementation issues with the given json data:

there are errors in the JSON posted that will stop codable working with it - I suspect these are just copy & paste errors, but if not you will have issues moving forwards.
Because of the initial direction key the JSON effectively has 3 (or at least 2.5!) layers of nesting.  This will either need to be flattened in init(from:) or, as in the below, need a temporary struct for ease of mapping.  Flattening in the initialiser would be more elegant, a temporary struct is far quicker :-)
CodingKeys, while obvious, isn't defined in the previous answers, so will cause errors compiling the init(from:)
there's no stopTitle field in the JSON, so that will error on decoding unless it is treated as an optional.  Here I've treated it as a concrete String and handled it in the decoding; you could just make it a String? and then the decoder would cope with it being absent.

Using "corrected" JSON (added opening braces, missing commas, etc) the following code will import both scenarios.  I've not implemented the arrayOrSingleItem as all credit for that belongs with @TylerTheCompiler, but you could easily drop it in.
struct Direction: Decodable {
   let direction: BTDirection
}

struct BTDirection: Decodable {
   enum CodingKeys: String, CodingKey {
      case title
      case stopTitle
      case prediction
   }
   let prediction: [BTPrediction]
   let title: String
   let stopTitle: String

   init(from decoder: Decoder) throws {
      let container = try decoder.container(keyedBy: CodingKeys.self)
      do {
         prediction = try container.decode([BTPrediction].self, forKey: .prediction)
      } catch {
         let singlePrediction = try container.decode(BTPrediction.self, forKey: .prediction)
         prediction = [singlePrediction]
      }
      title = try container.decode(String.self, forKey: .title)
      stopTitle = try container.decodeIfPresent(String.self, forKey: .stopTitle) ?? "unnamed stop"
   }
}

struct BTPrediction: Decodable {
   let minutes: String
   let vehicle: String
}

and then to actually decode the JSON decode the top-level Direction type
let data = json.data(using: .utf8)
if let data = data {
   do {
      let bus = try decoder.decode(Direction.self, from: data)
      // extract the BTDirection type from the temporary Direction type
      // and do something with the decoded data
   }catch {
      //handle error
   }
}

In case you're not aware, JSON Validator is very useful for validating/correcting json.


How do I mark the only Windows (10) disk as NOT bootable?

Question: How can I prevent Windows from (ever) booting to the current (and only) Windows disk?

I am doing "legacy" (not UEFI) booting.
I want something lower in the boot order to be attempted.
I want to perform this (quick?) action, and then reboot.

This is a run of the mill Windows install, with one disk and two partitions. The first partition is a "System Reserved" partition, created by the Windows installer. The other is the OS partition.
Ultimately, what I am trying to do is to get a machine to reboot and PXE boot for an OS reinstall. It's a (Gen1) Hyper-V VM, and I never get a chance to interfere in the boot sequence. It boots to the primary HDD before I can send keystrokes to it.
Comment: Disable the virtual disk of your VM or run Windows Setup (from a DVD drive) within Windows.
Comment: @paladin, won't that require me to re-enable the disk _while Windows Setup is running_?
Comment: That depends on what you want to do, but I guess yes, you might want to re-enable the disk while Windows Setup is running. Or just just install Windows from a (virtual) DVD within Windows, it's probably easier. Just download the installation image from Microsoft and "mount" it with Windows Explorer, there should be a setup.exe or install.exe or anything else like that.
Answer: If you need to change to boot order on a Windows Server HyperV Virtual Machine (VM):
<code>Launch HyperV
Click on your VM to select it
Click SETTINGS in the menu on the right
Click BIOS
Make your change and save it
</code>
https://www.urtech.ca/2016/03/solved-how-to-configure-the-boot-order-on-a-hyperv-virtual-machine/
Comment: The reason I don't want to do this is because it requires making two (setting it, then undoing it later) permanent settings changes for a setting that I really don't want to modify. I'm looking for a solution that does _just enough_ to thwart booting from the booted windows disk.
Comment: Then you need to emulate what i.e. SCCM does; boot always PXE and decide if you want to trigger a PXE reinstall/update or continue to localboot (HDD). This is not simple and there's not a ready to use minimalist approach to the problem.


How can I place $content in search results instead of $snippet?

Question: I'm using the Link module to display a field that links to an external site. $snippet places this link (along with other fields) in a paragraph. The fields have no separation and the link doesn't work. I'd like to place $content in search-result.tpl to remedy this but everything I've tried has given me an error.
Someone mentions $result here which may be what I need.
I tried <?php print $result['field_lipsum']; ?> but it gives me Notice: Undefined index: field_lipsum in include() . . .
How can I place $content in search results instead of $snippet?
Answer: Define yourtheme_preprocess_search_result in your theme's template.php file. Do something like this:
function yourtheme_preprocess_search_result(&$variables) {
  //$variables['result']['node'] should contain the entire node object
  $variables['content'] = $variables['result']['node']->body[LANGUAGE_NONE][0]['value'];
}

You should be able to use the variable $content inside search-result.tpl.php now.
Comment: Do I need to place function in front of that? I already have a yourtheme_preprocess_search_result. can I do yourtheme_preprocess_search_results? I tried adding it to that function but it gave me the error Notice: Undefined property: stdClass::$body in podliberty_preprocess_search_result()
Comment: Yes, function needs to be in front of the function name.
Comment: Okay I did it in a new function. (preprocess_search_resultS) then put   in search-result.tpl but it doesn't show up
Comment: What do you mean by should contain the entire node object?
Comment: In the above example, it should just be setting a variable $content with the entire node content. You should be able to use `print $content;` to output it.
Comment: print $content gives me Notice: Undefined variable: content in include() (line 98,  print render($content) gives no error but no content
Comment: It might be easier for you to ignore yourtheme_preprocess_search_result. Use `print $result['node']->body[LANGUAGE_NONE][0]['value'];` inside search-result.tpl.php
Comment: That gives Notice: Undefined property: stdClass::$body in include() is value supposed to be the field?
Comment: Hmm well you can try `print ''.print_r($result['node'], true).'';` That should give a print out of what's contained inside $result['node'].
Comment: Hmm that does work but it gives a massive list of code. The two fields I want are at the bottom though and the link does work. Any way to just get the fields with that?
Comment: Yup, you can print out something like `print $result['node']-> field_lipsum[LANGUAGE_NONE][0]['value']` if field_lipsum is the name of your custom field.

I believe the link module uses the 'url' element (instead of 'value') but I don't remember for sure.
Comment: Getting close I think! It doesn't output the title for value, field_lipsum, which is "Lipsum: null", inline. It just does "null". And for field_download, the link, it does the URL in plain text. I have the link set to static title "Download" which goes to the link.
Comment: I'll leave it to you to experiment around then. All the data you need is in $result['node'] (the node object includes all data about your content). You might find the [devel module](https://www.drupal.org/project/devel) useful for understanding the structure of a node object.
Comment: I just realized I can wrap it in an anchor and just put the title in front of the other field. Duh. I appreciate the help Shawn.


How to fix errors with Firebase storage

Question: I am trying to use Firebase storage to have people download files. I am not going to create a file upload, I am just going to upload files through Firebase console because I am the only person ever going to need to and I won't need to upload much. I have created a small static website from HTML and CSS. I deployed it to Firebase hosting and it works fine.
I uploaded a file to storage using the Firebase console and then read the documentation on creating a reference and downloading files from it. I added the necessary js libraries and then below it the config info for my project. I tried clicking the button but nothing happened. This error appears in the console:

Here is the code for the one page that uses Firebase:

    <meta name="robots" content="noindex, nofollow">
    <link rel="icon" type="icon" href="/favicon.ico">
    <link rel="stylesheet" type="text/css" href="/style.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Railsplitters Patrol</title>

        Home
        Campouts
        Full Archive

Railsplitters Patrol List 7/22/20

This is a list of everyone in the patrol and everyones patrol/troop position.

    <button onclick="download()">Download the file</button>

What do I need to do to fix this? This is my first Firebase storage project. I have used Firebase authentication before and never gotten successful results but I have used firebase hosting and dynamic links before. I am very new to JavaScript.
Comment: Please don't post screenshots of error messages, or other textual content. Instead post the actual text, and use the formatting tools of Stack Overflow to mark it up. And when you post the error message, I recommend also searching for it as I'm pretty sure that has been covered a few times before.
Answer: Like the console error says, you have to initialize Firebase, like this:
firebase.initializeApp(firebaseConfig);

More info:
https://firebase.google.com/docs/web/setup
Comment: I added that line of code below the config and libraries and above the rest of the js code. I then deployed it and pressed the button. Nothing happened. It did not initiate a download, I looked in the console and there were no errors at all. Please tell me what I need to do to fix this.
Comment: Edit your post with your full code again so I can see
Comment: None. I just get 3 Info messages about content being loaded and 2 warnings from Grammarly about some kind of default root logger. The URL is https://railsplitters-patrol.firebaseapp.com/. Open that and navigate to Full Archive > Patrol List 7/22/20 and press the button and look at the console.
Comment: The API request works, check out the Network tab. https://i.imgur.com/eXSh8RS.png

You just have to display it correctly on the page.

How to fix errors with Firebase storage


How to interpret a high sensitivity and low specificity using svm classifier?

Question: I want to have your opinion on the performance of my SVM classifier (k-fold cross validated):
Classification of class1(n=45)/class2(n=86)
Accuracy: 65.4%
Sensitivity: 88.2%
Specificity: 22.2%
AUC: 0.628
How can I interpret the high sensitivity and low specificity?!
Answer: you have high true positive rate which is 88.2% that means 88.2% time your model predicted positive classes which are actually positive,example : percentage of cancerous people who are correctly identified as having the condition.it also indicates that your recall score is high,we know that Recall = (tp/(tp+fn)) here tp = true positive and fn= false negative rates
but your specificity is low which indicates that "true negative rates" of your model is low which is 22.2%.example :  the percentage of non cancerous patients who are correctly identified as not having the condition it is also known as precision and we know precision = (tp/(tp+fp)) here tp = true positive rates and fp = false positive rates
shortly :
*your model has a precision of 22%, when it predicts a tumor is malignant, it is correct 22% of the time.
*your model has a recall of 88%, it correctly identifies 88% of all malignant tumors.
conclusion :
perhaps your model is overfitting(you need to check your cross validation score)and your model has low accuracy and high false positive(fp) rates that means your model is making many mistakes and should not do well on unseen data
Comment: Ok so it doesn't mean that my model failed or that it's useless?
Also, is it possible that i have this results because of unbalanced classes (45 vs 86)?
Comment: your model has failed and yeah unbalanced classes often lead to such problems,your model is kind of biased now,you need to improve it,thanks


changed sa password but clients get bad password

Question: I reset the password for sa for ms sql 2008 R2.
I can login using Microsoft SQL Server Management Studio with the account sa with the new password.
However, when I go to update the servers (sharepoint) that uses the account, they get bad login (bad password) for sa.
I have tried multiple different ways to reset the sa password and restart the service, but so far no luck.
Why is it still giving me bad password outside the system, but not when used locally?
Comment: Are you sure it's using the sa user account? Check the SQL server's error logs and see if it shows other users trying to log in.
Comment: @KJ-SRS The SQL logs show the user "sa" as having the failed login. This also is the case for the sharepoint logs.
Comment: @KJ-SRS I have spent hours trying to figure this out. both Sharepoint and SQL show that sharepoint is trying to use the sa account and that login fails.
Answer: This was due to a lack of experience on my end. I had to update the sa password in two locations. I had to update in the default database info, as well as the database specific (WSS_CONTENT) info.


When to *not* split up your data into training and testing

Question: So, I was thinking of a situation of when to not split up your data into training and testing and to just train on the entire dataset, at the risk of "overfitting". If my dataset has let's say 10 columns of which 9 are categorical (which have no particular order, and one is numeric). It could happen that your training set split may not capture observations that include some of the original categorical observations. When you then go and try to evaluate your model on the test set, which will have these new categorical variables that were not trained on, your model would potentially not know what to do, and in R, or wherever your model is implemented, you would get an error.
Another situation could be where you are not interested in predicting "new data", rather, just getting a feel for the relationships/patterns between the predictors and the response variable.
Would these not be situations that even though your model will potentially be overfitted, that you would want to train on the entire dataset?
Thanks!
Comment: The categorical situation would be an argument for being very careful about how the split is done rather than not spliitting at all (e.g. rather than splitting purely at random, some more sophisticated sub-sampling might be called for), unless the sample was so small - such s for a designed experiment perhaps) that no such approach could work.
Answer: I agree with your first point: if the amount of data is limited, then we can use the entire data for model building, instead of split the data into two sets.
However, I do not completely agree with your second point: even there are no new data to predict, and all we want is trying to discover patterns. Sometimes testing data is still needed.
For example, we want to use K-means to "discover patterns" in data. How many clusters to choose can be derived using a testing data set.
Always keep in mind, data can contain noise, and if we just do whatever we can to overfit one data set. The discovery can be false and not generalized well.
Answer: You don't need to do a train/validation (or test) split, if what you do cannot be evaluated on a bit of data you did not use. Clustering could be an example, the assessment of clustering is often just by human gut feeling (e.g. humans might hope that similar countries/products/whatever get clustered together). However, if the next step is to use the clusters as part of some predictive algorithm, this may again need splitting.
Regarding the example with categories that may not be present in the training part of a split, I think the comment by @Glen_b is one good perspective on this (i.e. split so that's not an issue). Additionally, you could also make sure your approach can handle this type of data situation (for which there are many approaches depending on the exact setting), which is particularly valuable, if even more categories (for which you have no data, so far) may "turn up" in the future. In that case, splitting gives you an evaluation of how well what you are doing performs when faced with a new previously unseen category.
If anything, a small data setting might be the setting, where you really need a robust evaluation approach, because it's so incredibly easy to overfit a small dataset. A single train-test split is probably not it though, cross-validation or perhaps overfitting-corrected bootstrapping might be better. E.g. Harrell mentions in Section 5 of his Regression Modelling Stratgies book as a good internal (=when we cannot test on large new external data, or will only do so later) validation strategy:

Strong internal validation using 100 repeats of 10-fold cross-validation or several hundred bootstrap resamples, repeating all
analysis steps involving Y afresh at each re-sample and the
arbitrariness of selected 'important variables' is reported (if
variable selection is used)
Answer: Would like to add my comments to the second point.
In the situation that we are using a regression model to discover pattern in the data, I would think that we do not need a training, testing and validation data set to select the model. There are other methods available to assist us in eliminating "noise" to prevent over-fitting. For instance, we can use metrics such as AIC, leave one out cross-validation (jackknife) or adjusted R-square. Using these metrics above, it will help us to eliminate the possibility of overfitting and the aim of interpolation as oppose to using a testing and validation set which is more related to extrapolation.
Feel feel to add in your views on this.
Answer: Regarding your second point, if you are referring to clustering algorithms, then you do not split the data into train and test. That is because we are not predicting or classifying anything and so we do not need the test or validation set. We train the clustering algorithm on the full dataset.


Is it possible to share 4 cores single CPU among 12 virtual machines on Xen?

Question: I have to host about 12 virtual machines on Xen server. all of them are very lightweight Linux machines. Means not resource intensive.
I have a xen server with single CPU with 4 cores. so is it possible i can share 2-3 CPUs among all of the virtual machines?
Answer: Yes, the Xen hypervisor (as well as others) will just share timeslices of the available CPUs to your virtual machines. And yes, you can overbook the amount of vCPUs. You could pin the vCPUs to physical CPU cores, but this is not required at all, in which case you let the hypervisor decide what VM gets a certain amount of timeslices of a physical CPU core.
From the perspective of a VM you'll notice that some CPU time will get stolen at some points in time. For example in top the percentage of st raises above 0 when a lot of CPU time is requested but overbooked.
Cpu(s):  0.7%us,  0.7%sy,  0.0%ni, 98.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

Answer: Yes that is not a problem, just make sure you monitor the resources of this servers closely.
In my experience disk i/o is a much bigger issue than CPU power and memory these days...
What you should do though, dedicate one cpu core and some RAM to the dom0. The dom0 will setup some buffers based on the RAM allocated, and that can lead to some strange behaviour when the RAM shrinks (because it's allocated to domUs) during operation. In addition you'll want to be sure that the dom0 is always able to execute a command, even when some domUs block all the cores. Cores pinned to the dom0 wont get allocated to the domUs. To check memory and CPU resources of both dom0 and domUs use xm top
To achieve that use the following kernel options
dom0_mem=1024M dom0_max_vcpus=1 dom0_vcpus_pin

You do not need to do any further configuration so that the CPUs are shared. Just add vcpus=number of cores to your xen domU configuration files to specify how many virtual cores a single domU can use. If you do not explicitly pin some cores to a domU (using cpus= in the configuration file), the dom0 will distribute the load accordingly over all available CPUs / Cores.
Comment: sorry,but my question has already mentioned that i have no issue with other resources. and your post does not have clear instructions that in what steps the CPU is to be shared among each VM.
Comment: Added some more explanations. Hope that helps...


excel vba - copy formula and generate new sheets

Question: if i have a sheet called 'values' with just 1 column with cells such as:
ColumnA
---------
emp_id
emp_name
dept_id

and then a 2nd sheet called 'sql' with 2 columns with cells such as:
ColumnA
--------
="select count(*) from tablex where "&a1&" is null;"
="select count(*) from tablex where length("&a1&") > 10;"

ColumnB
--------
Sheet for null
Sheet for length

What VBA macro code (note i need it in vba as i just want to click one button to generate the sheets) would i need to generate the following two output sheets (each sheet with just one column):
    Sheet for null
-------------------
select count(*) from tablex where emp_id is null;
select count(*) from tablex where emp_name is null;
select count(*) from tablex where dept_id is null;

    Sheet for length
-------------------
select count(*) from tablex where length(emp_id) > 10;
select count(*) from tablex where length(emp_name) > 10;
select count(*) from tablex where length(dept_id) > 10;

Comment: which database do you connect to? (where is `tablex`). What have you already tried?
Comment: Oracle. Just to get this simple example working tablex is hardcoded but in future i would put the tablename in a cell next to each fieldname.  i've tried it just manually copying down cells but i would really like to save time with vba.
Answer: I think this will do what you are asking.
One change I made is that on the "sql" sheet, I changed the cell contents to look like this:
="select count(*) from tablex where variable is null;"

so I used "variable" instead of "&a1&" like you originally had, and then in the code it replaces this word with the correct value of emp_id, emp_name, etc.
    Sub GenerateSheets()
    Dim formulaRange As Range
    Dim formula As String
    Dim r As Range
    Dim destloc As Range
    Dim VariableRange As Range

    Set formulaRange = Worksheets("sql").Cells(1, 1)
    ' get each formula
    Do Until formulaRange.Value = ""
        For Each r In formulaRange.Rows
            ' for each formula found, create a new sheet and move it to end
            Worksheets.Add
            ActiveSheet.Name = r.Offset(0, 1).Value
            ActiveSheet.Move after:=Sheets(ActiveWorkbook.Sheets.Count)

            Set destloc = ActiveSheet.Cells(1, 1)
            Set VariableRange = Worksheets("values").Cells(1.1)
            ' Loop through all the variables
            Do Until VariableRange.Value = ""
                destloc.Value = Replace(formulaRange.Value, "variable", VariableRange.Value)

                Set VariableRange = VariableRange.Offset(1, 0)
                Set destloc = destloc.Offset(1, 0)
            Loop
        Next

        Set formulaRange = formulaRange.Offset(1, 0)
    Loop
End Sub


Variance of the beta coefficient in linear regression

Question: I have a linear regression equation: $y=bx + a$.
Also standard error of the slope $b$ estimate is given and the sample size. Is it possible with this information to infer the variance of $b$?
Answer: The standard error of an estimator can be seen as the standard deviation of this estimator. So, standard error is the same for an estimator as standard deviation for a sample. Hence, you only need to square your standard error in your regression output in order to get the variance of the coefficients.
Go to the Wikipedia article for standard error by clicking here. The first sentence is: The standard error (SE) is the standard deviation of the sampling distribution of a statistic.
Comment: In Bayesian approach yes, but in frequentionist you $b$ is *not* a random variable but a point estimate, so it has no variance. The same as confidence intervals does not tell you about 95% of possible values (they are SE-based).


Using before and after pseudo-elements on background image

Question: I am wanting to construct navigation items from three (background) images, with the first and last a fixed width, and the central a variable width, depending on the width of the text in the nav item.  I have been led to believe that using the pseudo elements before and after would be the best method.  When I try this though, the main (central) background image for the nav item is overlapping with the before and after background images.
You can see what I mean on this page.
Here is the CSS:
.box {
   background-image: url(nav/images/nav_02.png);
   background-repeat: repeat;
   height:20px;
   position: absolute;
   padding: 10px 13px;
}

.box:before {
    left: 0;
    background-image: url(nav/images/nav_01.png);
}

.box:after {
    right: 0;
    background-image: url(nav/images/nav_03.png);
}

.box:before,
.box:after {
    content: ' ';
    width: 13px;
    height:40px;
    position: absolute;
    top: 0;
    background-repeat: no-repeat
}

And the HTML:

here is some text

Can I use background-images in this way using pseudo elements?
Thanks,
Nick
Answer: Yes, but you will have to use left and right attributes for moving the pseudo elements in the right position. padding is not correct for the main box to position. Better use margin.
.box {
    background-repeat-x: repeat;
    background-image: url(nav/images/nav_02.png);
    background-repeat: repeat;
    height: 40px;
    position: absolute;
    margin: 0 13px;
    line-height: 40px;
}

.box:before, .box:after {
    content: ' ';
    display:block;
    width: 13px;
    height: 40px;
    position: absolute;
    top: 0;
    background-repeat: no-repeat;
}

.box:before {
    left: -13px;
    background-image: url(nav/images/nav_01.png);
}

.box:after {
    right: -13px;
    background-image: url(nav/images/nav_03.png);
}


Let $f : M \to M $ - bijection of a finite set. Then $f^n = Id_M$ for some $n \in N$.

Question: Let $f : M \to M $ - bijection of a finite set. Then $f^n = Id_M$ for some $n \in N$.Give an example of a bijection of an infinite set, for which this statement is false.
Could you please provide any hints or give clarification ? I do not understand this problem.
I will try to construct my own proof

Let $f : Z \to Z$ such that $f(x) = 2*x$. Then with each $n$th iteration $f$, elemenets can be divided by $2^n$. Therefore, there will never be $n \in N$  such that $f^n = Id_M$, since elements such as 1, 3, 5 cannot be divided by $2^n$ for any $n \in N$.
Comment: If the set $Z$ in your attempt is the set of integers, then $f(x)=2x$ is not a bijection $Z\to Z$ since it isn't surjective.
Comment: non-infinite = finite?
Comment: @azifmedrano Sorry))) I forgot the word 'finite' )
Comment: Your counter example looks fine (although the argument can be made a bit simpler), but it still doesn't give a proof of the claim for finite sets - only shows that it's not true in the infinite case.
Answer: Consider the set $S_M:=\{f:M\to M|\text{ } f\text{ is a bijection }\}$ and endow it with the operation of composition. This is a finite group, namely the symmetry group on $M$, so all of its elements have finite order.
For the infinite case, take for example $M=\mathbb{Z}$ and consider the bijection $f(n)=n+1$. Obviously as many times we compose $f$ with itself we will never reach the identity.
Answer: Hint - choose some $a\in M$ and look at the sequence $a_n=(f^n(a))_{n=0}^{\infty}$. Can this sequence be non-recurring? If not, what can you conclude?
Repeat this for all $a\in M$ and try to think what you can say about $f$ after enough iterations.
Comment: I can't make an example where this sequence is non-reccuring. Does it mean that there is no such infinite set for which this statement is false ?
Comment: If you think about it (this is the challenging part of the proof), you'll see that the sequence has to be recurring - there cannot be any counter example (at least in the finite case). Then, since the sequence must be recurrent, then there is some index $n$ such that $f^n(a)=a$, as you wished to prove.
Comment: To make things simpler, you can even show that there are two distinct $m,n$ such that $a_n=a_m$.
Comment: Thanks for the clarification. Do I understand this question right ? Suppose $f:Z \to Z$. and $f(x) = -1 * x$. Then f is such a bijection that is defined in hypothesis, right ?
Comment: @RustemSadykov I think what they wanted is a counter example through an infinite set, this is not a counter example
Comment: Please have a look at the proof I have written


How do I sort the buttons generated from the following code alphabetically base on string values

Question: <string name="Manuf0">best</string>
<string name="Manuf1">Bravo</string>
<string name="Manuf2">zoo</string>
<string name="Manuf3">Skitz</string>
<string name="Manuf4">don</string>
<string name="Manuf5">animal</string>

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    scrollviewManuf = new ScrollView(this);
    LinearLayout linearlayout = new LinearLayout(this);
    linearlayout.setOrientation(LinearLayout.VERTICAL);
    scrollviewManuf.addView(linearlayout);
    for (int i = 0; i < 5; i++)

    {

        LinearLayout linearManuf = new LinearLayout(this);
        linearManuf.setOrientation(LinearLayout.HORIZONTAL);
        linearlayout.addView(linearManuf);

        Manufb = new Button(this);

        int id = getResources().getIdentifier("Manuf" + i, "string", getPackageName());
        String Manuf = getResources().getString(id);
        Manufb.setText(Manuf);
        Manufb.setId(i);
        Manufb.setTextSize(30);
        Manufb.setPadding(0, 0, 0, 0);

        // b.setTypeface(Typeface.SERIF,Typeface.ITALIC);

        Manufb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        linearManuf.addView(Manufb);

        Manufb.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                String SManuf= Manuf.replaceAll("&","").replaceAll(" ","").replaceAll("/","").replaceAll(" / ","").replaceAll("/ ","").replaceAll(" /","".replaceAll("&",""));
               //Panel= getResources().getString(id);
                Toast.makeText(getApplicationContext(), SManuf , Toast.LENGTH_SHORT).show();

                Intent passIntent = new Intent(Manufacturers.this,panels.class);
                passIntent.putExtra("SManuf",SManuf);
                startActivity(passIntent);
            }

        });

    }

    this.setContentView(scrollviewManuf);
}
}

How do I sort the buttons generated from the following code alphabetically base on string values.
Currently they are listed as the buttons are produced 0 through to 5.
The list is in an xml string file, want to eb alphabetical so I can just add more to the file as needs be, and the programming just sort it alphabetically which suits me.
Not been able to find anything  yet , but I am gueesing I may need to define the list in the file and sort that list can anyone point me in the right direction please.
Okay, so I can see the code is doing something but the sort order isn't changing and the String s is showing up in intellij as not used, :  new code below:-
marked the sections  with // here
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    ArrayList<String> theStrings = new ArrayList<>();//Here
    scrollviewManuf = new ScrollView(this);
    LinearLayout linearlayout = new LinearLayout(this);
    linearlayout.setOrientation(LinearLayout.VERTICAL);
    scrollviewManuf.addView(linearlayout);

    for (int i = 0; i < 28; i++) {

        LinearLayout linearManuf = new LinearLayout(this);
        linearManuf.setOrientation(LinearLayout.HORIZONTAL);
        linearlayout.addView(linearManuf);

        Manufb = new Button(this);

        int id = getResources().getIdentifier("Manuf" + i, "string", getPackageName());
        String Manuf = getResources().getString(id);
        theStrings.add(Manuf); /// Here
        Manufb.setText(Manuf);
        Manufb.setId(i);
        Manufb.setTextSize(30);
        Manufb.setPadding(0, 0, 0, 0);

        // b.setTypeface(Typeface.SERIF,Typeface.ITALIC);

        Manufb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        linearManuf.addView(Manufb);

        Manufb.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
                String PManuf =Manuf;
                // TODO Auto-generated method stub

                String SManuf= Manuf.replaceAll("&","").replaceAll(" ","").replaceAll("/","").replaceAll(" / ","").replaceAll("/ ","").replaceAll(" /","".replaceAll("&",""));
               //Panel= getResources().getString(id);
                Toast.makeText(getApplicationContext(), Manuf+" Selected" , Toast.LENGTH_SHORT).show();

                Intent passIntent = new Intent(Manufacturers.this,panels.class);
                passIntent.putExtra("SManuf",SManuf);
                passIntent.putExtra("PManuf",PManuf);
                startActivity(passIntent);
            }

        } );

    } Collections.sort(theStrings);   //here
    for (String s : theStrings) {  //here
        //...

    this.setContentView(scrollviewManuf); }//here
}

}
The following code is looping each time it loops its adding an extra repeated option.
ie. Cat,dog,mouse, donkey correct list  is the list but I am getting, Cat, dog.dog, mouse,mouse,mouse, donkey, donkey, donkey,donkey but still no sorting, still working on it but here is the code.
ArrayList<String> theStrings = new ArrayList<>();

        for (int i = 0; i < 28; i++) {
            int id = getResources().getIdentifier("Manuf" + i, "string", getPackageName());
            String Manuf = getResources().getString(id);
            theStrings.add(Manuf);
            Collections.sort(theStrings);
        for (String s : theStrings) {

            LinearLayout linearManuf = new LinearLayout(this);
            linearManuf.setOrientation(LinearLayout.HORIZONTAL);
            linearlayout.addView(linearManuf);

            Manufb = new Button(this);
            Manufb.setText(Manuf);
            Manufb.setId(i);
            Manufb.setTextSize(30);
            Manufb.setPadding(0, 0, 0, 0);

            // b.setTypeface(Typeface.SERIF,Typeface.ITALIC);

            Manufb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

            linearManuf.addView(Manufb);

            Manufb.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {
                    String PManuf = Manuf;
                    // TODO Auto-generated method stub

                    String SManuf = Manuf.replaceAll("&", "").replaceAll(" ", "").replaceAll("/", "").replaceAll(" / ", "").replaceAll("/ ", "").replaceAll(" /", "".replaceAll("&", ""));
                    //Panel= getResources().getString(id);
                    Toast.makeText(getApplicationContext(), Manuf + " Selected", Toast.LENGTH_SHORT).show();

                    Intent passIntent = new Intent(Manufacturers.this, panels.class);
                    passIntent.putExtra("SManuf", SManuf);
                    passIntent.putExtra("PManuf", PManuf);
                    startActivity(passIntent);
                }

            });

        }
    }
    this.setContentView(scrollviewManuf);
}
}

Answer: Read it into a list, sort it and loop over it:
ArrayList<String> theStrings = new ArrayList<>();
 for (int i = 0; i < 28; i++) {
    int id = getResources().getIdentifier("Manuf" + i, "string", getPackageName());
    String Manuf = getResources().getString(id);
    theStrings.add(Manuf);
}
Collections.sort(theStrings);

for (String s : theStrings) {

    LinearLayout linearManuf = new LinearLayout(this);
    linearManuf.setOrientation(LinearLayout.HORIZONTAL);
    linearlayout.addView(linearManuf);

    Manufb = new Button(this);

    Manufb.setText(s);  // <-- use the String here
    Manufb.setId(i);
    Manufb.setTextSize(30);
    Manufb.setPadding(0, 0, 0, 0);

    // b.setTypeface(Typeface.SERIF,Typeface.ITALIC);

    Manufb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    linearManuf.addView(Manufb);

...
Comment: Thanks,  not sure I am integrating this corectly no errors but no sorting either. I will play on  for a bit.
Comment: add some code?  not following  this and not sure what I am doing. I can see that we are putting a list theStrings together from the list generated for the buttons but I don't see how the list the Strings is taken from the strings take, other than  thestrings.add(Manuf);  so I am missing some code? or I just need to work out what goes where excactly?
Comment: show some code, saying it doesn't work usually isn't enough, unless you post the code you're using, there's no way to tell what's wrong
Comment: I will have another play and post the code I have entered/ how the section looks modified
Comment: cant add its too long to add here,  by 1700+ characters
Comment: worked it out doh must seem stupid at times thanks for helping its appreciated
Comment: worked it out doh must seem stupid at times thanks for helping its appreciated on posting the code that is not the solution to the sorting the list.
Comment: You need to sort it first before you create the Layouts. I updated my answer accordingly
Comment: okay still struggling updated my code on here, I am obviously missing something very simple I either get nothing crashes out, get the list but not sorted and my latest code gets duplication increasing as it goes down the list but still doesn't sort it, some of the values have / or & separating them if that is likey to inhibit the process of sorting. ie Cat & Mouse, Cat/ Mouse, Dog/Cat/Mouse
Comment: still no go,  will revisit later for now I will crack on with the rest of it, worst case I manually sort that one section


Estimate on Size of Convex Hull

Question: Let $X$ be a bounded subset of $\mathbb R^3$, and denote by $Conv(X)$ the convex hull of $X$, i.e, the smallest convex subset of $\mathbb R^3$ containing $X$. As $X \subset Conv(X)$ by definiton, the diameter-inequality $diam(X) \leq diam(Conv(X))$ certainly holds, and I wonder if a partial converse is also true:
There exists an integer $n \in \mathbb N$ and a constant $c > 0$, such that the following holds: If  $\Gamma \subset \mathbb R^3$ is a (smooth) simple closed curve, we have the inequality \begin{equation} diam(Conv(\Gamma)) \leq c*diam(\Gamma)^n \end{equation}
It can be shown that any (smooth) simple closed curve $\Gamma$ in $\mathbb R^3$ bounds a disc $D$ with $Area(D) \leq (4\pi)^{-1}*length(\Gamma)^2$. However, things get a bit more complicated when passing to the diameter - it is possible to construct a sequence $(D_k)_{k=1}^\infty$ of discs bounded by $\Gamma$ whose diameter grows arbitrarily large as $k \to \infty$, but have uniformly bounded Area. Moreover, the inequality $diam(\Gamma) \leq lenght(\Gamma)$ can easily shown to be true, but it is not possible to find simliar inverse inequality (as can be seen by considering curves inside a small ball that are of large variation). Thus, it seems highly unlikely that the Area-length inequality can be used to relate the diameter of a disc with the diameter of a corresponding boundary curve.
However, when considering a least area disc $D$, bounded by $\Gamma$, we have a particularly well-behaved disc, such that $D \subset Conv(\Gamma)$. Therefore, the inequality in question could be used to establish an analogous relation between the diameter of such a disc with its boundary curve.
Answer: Taking the convex hull does not increase the diameter:
$$\textrm{diam}\ ( Conv(X)) = \textrm{diam} (X)$$
This follows from the fact that
$$d( P, \lambda Q_1 + (1-\lambda) Q_2) \le \lambda d (P, Q_1) + (1-\lambda) d (P, Q_2)$$
as the distance is given by a norm.
Comment: Thanks. I wonder if the equality also extends to oriented riemannian manifolds. The problem there is, of course, that the metric is not induced by a Norm. I guess that one needs some assumptions on sectional curvature...
Comment: @BerniWaterman: Minor nitpick: the existence of the nowhere-vanishing volume form associated to the Riemannian metric implies that a Riemannian manifold is always oriented.

Estimate on Size of Convex Hull


Why do photos not add in iOS 9 using PHPhotoLibrary performChangesAndWait?

Question: In iOS 8.4, the following code works fine.
In iOS 9, it fails, with a useless error (-1 null NSCocoaErrorDomain or whatever).
bool success = [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
            ...
            newChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:container.filePath]];

        if( !newChangeRequest.placeholderForCreatedAsset ) {
            DDLogWarn(@"addToCameraRoll: WARNING, PLACEHOLDER COULD NOT BE CREATED: %@", container.filePath);

            if( perAddFailBlock )
                perAddFailBlock(container.filePath, true, container.isVideo, true);
            continue;

        } else {
            if( container.creationDate )
                newChangeRequest.creationDate = container.creationDate;
            [placeholders addObject:newChangeRequest.placeholderForCreatedAsset];
            bytesRequired += container.size.integerValue;
        }
    }

    PHAssetCollectionChangeRequest* albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.album];
    [albumChangeRequest addAssets:placeholders];
    ...
} error:error];

Answer: In iOS 8.x, Photos framework doesn't care about the name of the file.
In this code here:
newChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL fileURLWithPath:container.filePath]];

... filePath can be anything.
In iOS 9.0 this is no longer true.
In iOS 9.0, the change request is generated, and it appears to have a valid placeholderForCreatedAsset. But when you run it in performChanges block, it fails.
This is because it seems that iOS 9.0 cares about the filename: files must have a valid filename extension like .jpg
Here is the code I used to fix this iOS 9 regression. Note that although it's hardcoded to jpg here, I tried hardcoding it to PNG and jpgs still added fine. So it seems it just needs ANY photo extension to work. Laaaame.
        if( !container.filePath.pathExtension || [@"" isEqualToString:container.filePath.pathExtension] ) {
            NSString *newName = [container.filePath stringByAppendingPathExtension:@"jpg"]; // doesn't matter that it is hardcoded to jpg
            [[NSFileManager defaultManager] moveItemAtPath:container.filePath toPath:newName error:nil];
            container.filePath = newName;
        }


Django e Template

Question: Boa Noite!
Pessoal, estou com um problemas a 4 dias e infelizmente ainda não consegui resolver!
Estou trabalhando com tamplates no DJango, mas estou tendo problemas, pois não aparece nada nas páginas!
Essa é a estrutura básica da base.html
    <!DOCTYPE html>

    {% load staticfiles %}

        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

        <title>{% block title %}{% endblock %}</title>
        {% block head_override %}{% endblock head_override %}

                        {% block content %}

                        {% endblock content %}

{% block extra_javascript %}

{% endblock extra_javascript %}

Aqui é a página html que estende "base.html". Tem mais informações na página, mas estou querendo mostrar mesmo é a Estrutura do Djando, pois acredito que o erro esteja relacionado a isso! Pois fiz o teste sem utilizar Templates do Djando e funcionou :(!!
{% extends "base.html" %}
{% load staticfiles %}
{% block title %}Google Maps{% endblock %}

{% block head_override %}

    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

{% endblock head_override %}

{% block content %}

MAPA COM PONTEIROS

{% endblock content %}

{% block extra_javascript %}

{% endblock extra_javascript %}

Esta é a View
__author__ = 'Sara Fernandes'

from django.shortcuts import render_to_response, render
from django.template import RequestContext, loader
from django.shortcuts import redirect # Funcao para executar um http-redirect

#from django.http import HttpResponseRedirect # Funcao para redirecionar o usuario
#Criar as Views aqui

# pagina inicial do projeto dweb

def homepage(request):
    return render_to_response('indexTEMPLATE.html',
        context_instance= RequestContext(request))

def googlemaps(request):
     return render(request, "maps.html") # redireciona o usuario para a p�gina de login

def redirecionalogin(request):
     return render(request, "login.html") # redireciona o usuario para a p�gina de login

settings.py
  """
Django settings for web project.

Generated by 'django-admin startproject' using Django 1.8.1.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

#PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'web',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'web.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'web.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'NAME': 'gerenciador.db',
        'USER':'', 'PASSWORD': '',
        'PORT':'', 'PORT': '',

    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'web/templates'),
)

Comment: qual sua estrutura de diretórios?
Comment: Só uma dica @sara-fernandes, nunca revele sua SECRET_KEY
Answer: Seu diretório templates tem que estar dentro do seu app web e no seu settings.py e a variável TEMPLATES na chave DIRS tem que possuir esse o path desse diretório.
TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [
                os.path.join(BASE_DIR, 'web', 'templates')
             ],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

Answer: Sara, você não precisa usar override, aparentemente esta tudo certo, só que, dentro do teu settings.py, delete a linha do template_dirs:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'web/templates'),
)

porque? bem, como tua pasta do template esta dentro do teu app web, ele por padrão e reconhecido automaticamente, dentro do teu template base.html retire os {% block body_override %}{% endblock body_override %}, e o {% block extra_javascript %} tente trabalhar normalmente com js, o block content vai pegar tudo o que estiver contido dentro do body do teu html, tente utilizar o js dentro do block content assim:
{% block content %}

MAPA COM PONTEIROS

{% endblock content %}

se essas modificações não derem certo, coloque teu projeto no github e poste o link aqui que tentarei te ajudar com o mesmo.

Django e Template


Are arbitrarily SELECTed non-aggregate columns guaranteed to come from the same row with a MySQL GROUP BY?

Question: More of a question of interest than something I'm looking to use
Say I run the following query:
  SELECT su.id, su.name, sua.line_1, sua.line_2
    FROM site_user su
    JOIN site_user_address sua
      ON sua.user_id = su.id
   WHERE su.id = 1
GROUP BY su.id /* id is the PK for site_user */

Are sua.line_1 and sua.line_2 guaranteed to come back from the same site_user_address row?
I'm aware that, at the very least, the site_user_address row is arbitrarily selected
I couldn't find anything in the docs, and can't think of a reliable way to test this
UPDATE
This is not a duplicate as far as I'm aware.. I'm wondering if the arbitrary selection of a value will come from the same arbitrarily selected row when choosing two different columns from the same table
Other questions seem to be less specific, and more directed towards the arbitrary selection of both row and value of one or more non-aggregate columns
Comment: Possible duplicate of [Error related to only\_full\_group\_by when executing a query in MySql](https://stackoverflow.com/questions/34115174/error-related-to-only-full-group-by-when-executing-a-query-in-mysql)
Comment: Nope, not guaranteed. They usually do, but it is not guaranteed, and if you did `sua.line_1, sua.line_2, min(sua.something)` the line_ values should definitely should not be assumed to come from the row the min value did.
Comment: What's the point of `GROUP BY` without aggregate???
Comment: That's why no other dbms will not even accept your query, because it doesn't make sense.  You need to have all the non-aggregated columns to be included in the `GROUP BY`.
Comment: @Eric `GROUP BY` without aggregate functions is more-or-less a cheezy way of doing `SELECT DISTINCT`.
Comment: @Eric I have found that grouping by PK alone is an easy short hand way to get a full table row when joining to multiple entries in another table. For example, the `su.name` in the query above, which is always consistent with `su.id`. It may be advised by purists, but you don't **need** to have all the non-aggregated columns included for MySQL, and it makes sense under certain conditions.
Answer: You are indeed exploiting the notorious non-standard MySQL implementation of GROUP BY. Read this for more info https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html
Your query is equivalent to using ANY_VALUE() for the values from the second table, like this.
  SELECT su.id, su.name,
         ANY_VALUE(sua.line_1) line_1,
         ANY_VALUE(sua.line_2) line_2
    FROM site_user su
    JOIN site_user_address sua
      ON sua.user_id = su.id
   WHERE su.id = 1
   GROUP BY su.id

As long as site_user.id is unique for each row, you'll get the right name because it's dependent on the id value.
But for the values from the other table, your results will be, formally speaking, non-deterministic or unpredictable.  Do they come back from the same row as each other?  Unpredictable.
Unpredictable is like random, but worse.  Random implies you'll get different values sometimes, so you can catch problems in test. Unpredictable means you'll get the same values every time, until you don't. Usually changes in the chosen values occur when tables and indexes grow larger.
You may want to use a query that gives predictable results. For example, this one will give back the rows of site_user_address that have the highest value of site_user_address.id.
  SELECT su.id, su.name,
         sua.line_1, sua.line_2
    FROM site_user su
    JOIN (
              SELECT MAX(id) id, user_id
                FROM site_user_address
               GROUP BY user_id
         ) sumax               ON su.id = sumax.user_id
    JOIN site_user_address sua ON sumax.id = sua.id
   WHERE su.id = 1

The subquery:
              SELECT MAX(id) id, user_id
                FROM site_user_address
               GROUP BY user_id

retrieves the largest site_user_address.id value for each user_id value. You can then join to the site_user_address table to pull the detail values for those id values.
Beware the temptation to infer that behaviors are designed into a DBMS, but not documented, just because you observe them. The behavior in your question is documented. The documentation says it is non-deterministic.  That means that the DBMS query planner is free to satisfy your query in whatever way seems most efficient.  As tables and indexes grow, the query planner can, and does, choose different ways to satisfy the same query. Different query plans can also come from different parts of tables being resident in caches. Etc. Etc.
This unpredictability is hard for programmers to wrap our brains around. We wouldn't tolerate unpredictability in procedural Java or php code. But SQL is declarative, and many thousands of programmer years have gone into making queries run fast. Don't try to outsmart a DBMS, even if you're Michael Stonebraker himself.
Comment: Thanks! I suppose even if those two columns always do come from the same row, it's not documented as such, so it could be implemented differently in later versions. I guess there's a use case for get me a user, and any of the addresses they may have (with the address parts consistent), but there's a myriad of different ways of doing that without relying on undocumented behaviour!
Comment: Thanks for the update, I totally agree with you.. I used a poor choice of words, '.. relying on undocumented behaviour' should read 'relying on behaviour not in line with the documentation'.


Quantization Matrices in x264

Question: I've downloaded the source code and am wading through it --- does anybody know where the QMs  for the images and the residuals are stored?
Answer: As of x264 snapshot 20100603-2245/, they're stored in:
common/set.h

Quantization Matrices in x264


Understanding Singular Value Decomposition in the context of LSI

Question: My question is generally on Singular Value Decomposition (SVD), and particularly on  Latent Semantic Indexing (LSI).
Say, I have $ A_{word \times document} $ that contains frequencies of 5 words for 7 documents.
A =  matrix(data=c(2,0,8,6,0,3,1,
                   1,6,0,1,7,0,1,
                   5,0,7,4,0,5,6,
                   7,0,8,5,0,8,5,
                   0,10,0,0,7,0,0), ncol=7, byrow=TRUE)
rownames(A) <- c('doctor','car','nurse','hospital','wheel')

I get the matrix factorization for $A$ by using SVD: $A = U \cdot D \cdot V^T $.
s = svd(A)
D = diag(s$d) # singular value matrix
S = diag(s$d^0.5 ) # diag matrix with square roots of singular values.

In 1 and 2, it is stated that:
$WordSim = U \cdot S$ gives the  word similarity matrix, where the rows of $WordSim $ represent different words.
WordSim =  s$u %*% S
$DocSim= S \cdot V^T$ gives the  document similarity matrix where the columns of $DocSim$ represent different documents.
DocSim = S %*% t(s$v)
Questions:

Algebraically, why are $WordSim$ and $DocSimS$ word/document similarity matrices? Is there an intuitive explanation?
Based on the R example given, can we make any intuitive word count / similarity observations by just looking at $WordSim$ and $DocSim$ (without using cosine similarity or correlation coefficient between rows / columns)?
Comment: Your question could be improved by editing it to correctly reflect what that document states. On p. 22 it defines $S$ as containing the *square roots* of $D$, "restricted" to the largest ones. Therefore neither $UD$ nor $DV^\prime$ are involved, nor do they have interpretations as "similarity matrices." The relevant matrices are instead $US$ and $SV^\prime$. They can be used to reconstruct an approximation of $A=UDV^\prime\approx U(S^2)V^\prime=(US)(SV^\prime).$
Comment: I assumed `D=svd(A)$d` in R returns the square roots of the non-zero eigen values, hence I used $UD$. I do not have a problem with the dimensionality reduction aspect, and I do understand a lower rank approximation of A can be formed as they describe. I found the answer on [this link](http://math.stackexchange.com/questions/89924/clustering-algorithm-to-cluster-objects-based-on-their-relation-weight/89931#89931) partially answers my question.
Comment: I know nothing about LSI (am quite familiar with SVD and PCA though), but find the exposition in your first link (section 8.2) superb and very clear. I am not sure what exactly you are asking about. I assume that you read that section, but some details remain unclear to you. What exactly? Your question 1 seems to be directly answered there. Your question 2 I don't really understand.
Comment: I know very little about LSI, but SVD of a matrix is at the core of linear dimensionality-reduction, mapping methods, such as Principal components, biplots, Correspondence analysis. The main "laws" of SVD is that $AV=UD$ = projection of rows of $A$ onto the principal axes; and $A'U=VD'$ = projection of columns of $A$ onto the principal axes. In a sense, it is "similarity" values between the points (rows or columns) and the principal axes. Whether it can be treated as similarity between the points themselves is dependent on the context, I think.
Comment: Ah.. I see in wikipedia that LSI is just **correspondence analysis** (CA). That's better. CA is the **biplot** of a specially prepared data table. The aforementioned projections or coordinates - you use them to plot row and column points in the space of the principal axes. Closeness between the row-row, col-col, and row-col points relate their similarity. However, the layout on the plot is dependent on how you spread inertia (variance) over the row and the col points.
Comment: @ttnphns. Thank you, can you give a reference on: "$AV=UD$ = projection of rows of A onto the principal axes; and $A ′ U=VD ′$ = projection of columns of A onto the principal axes"? I think that will clarify things for me. By principal axes, do you mean the eigen vectors corresponding to the top m singular values in $D$? I also came across: "For PCA,we need not compute the left singular vectors", but cannot wholly comprehend why this is the case.
Comment: This question is very clearly answered in the paragraphs preceding section 9 in your reference [1](http://www.ling.ohio-state.edu/~kbaker/pubs/Singular_Value_Decomposition_Tutorial.pdf) (at p. 23).
Comment: @whuber, in the section that you point out, what I don't understand is: "Words are represented by row vectors in $U$, and word similarity can be measured by computing row similarity in $UD$.". Why is this so? They explain how dim. reduction helps but not why $UD$ is what they say it is.
Answer: Matrix factorization using SVD decomposes the input matrix into three parts:

The left singular vectors $U$. The first column of this matrix specifies on which axis the rows of the input matrix vary the most. In your case, the first column tells you which words vary together the most.
The singular values $D$. These are scalings. These are relative to each other. If the first value of $D$ is twice as big as the second it means that the first singular vector (in $U$ and $V^T$) explain twice as much variation as the seconds singular vector.
The right singular vectors $V^T$. The first row of this matrix specifies on which axis the columns of the input matrix vary the most. In your case, the first row tells you which documents vary together the most.

When words or documents vary together it indicates that they are similar. For example, if the word doctor occurs more often in a document, the word nurse and hospital also occur more. This is shown by the first scaled left singular vector, the first column of $WordSim$.You can validate this result by looking at the input data. Notice that when nurse does occur, hospital also occurs and when it does not occur, hospital also does not occur.


AIME competition: Probability all 3 players obtain odd sum

Question: Here's a question from the AIME competition:
https://artofproblemsolving.com/wiki/index.php/1998_AIME_Problems/Problem_4

Nine tiles are numbered $1, 2, 3, \cdots, 9,$ respectively. Each of three players randomly selects and keeps three of the tiles, and sums those three values. The probability that all three players obtain an odd sum is $m/n,$ where $m$ and $n$ are relatively prime positive integers. Find $m+n.$

It's also been asked before on Math Stack Exchange before here:
Probability question and how to approach it
Here's what I did. Let's say the order in which people take tiles does matter. Then my result is$${{3 \binom{5}{3} 2 \binom{4}{2}}\over{3! \binom{9}{3,3,3}}} = {1\over{28}}.$$However, the answer at the given link, where they don't take into consider order (saying it will be the same final answer) is:$${{3 \binom{5}{3} 2 \binom{4}{2}}\over{\binom{9}{3,3,3}}} = {3\over{14}}.$$So my question is, if we take into consideration the order in which people take tiles, then why do we need an extra $3!$ in the numerator (to cancel out the $3!$ in the denominator)? I thought the $3$ I had already took care of that, since only $1$ of the $3$  players will have $3$ odds, which "fixes" the configuration. So how am I mistaken, what am I missing?
Comment: It is not clear to me what the $3!$ represents in your denominator of $3! \binom{9}{3,3,3}$.  The order $A$ then $B$ then $C$ is already in the   $\binom{9}{3,3,3}$, though the order of the cards each takes is not; if you wanted that too then it would be $9! =(3!)^3 \binom{9}{3,3,3}$  and the numerator would have to increase too.
Answer: The trinomial coefficient (note the third $3$ is not typically written since it is understood that it must be $9-3-3 = 3$, in as much as $\binom{n}{k}$ is not written $\binom{n}{k,n-k}$) $$\binom{9}{3,3} = \frac{9!}{3!3!3!} = 1680$$ represents the number of ways for the three players to select $3$ distinct tiles without replacement, in which the order of the players matters but the order of the tiles selected by each player does not.  So for instance, the selection $$(\{1,2,3\}, \{4,5,6\}, \{7,8,9\})$$ is equivalent to $$(\{3,2,1\}, \{6,5,4\}, \{9,8,7\})$$ but not equivalent to $$(\{7,8,9\}, \{4,5,6\}, \{1,2,3\}).$$
On the other hand, if the order of tiles taken by each player matters, but the order of the players does not, then the correct number of distinct outcomes is simply $9!/3! = 60480$, since there are $9!$ total permutations of the tiles, which are then partitioned in sequence among the $3$ players, and then for each such outcome, there are $3!$ orderings of the $3$ players assigned to the sequence.
Your denominator $3! \binom{9}{3,3} = 10080$ doesn't match either of these ways of counting.
If you want to count the outcomes in which neither ordering of tiles nor players matters, then clearly this is just $1680/3! = 280$.  If you want to count outcomes in which the order of everything matters, it is just $9!$.
Answer: I think that the simplest way to conceive it is to frame the answer entirely in terms of the multinomial coefficient $\binom{n}{k_1, \;k_2,\; k_3\; ...}, \Sigma(k_i) = n,$
defined as the number of ways to put $n$ distinct objects into $k$ distinct boxes with $k_1$ in box $1$, $k_2$ in box $2$, etc, not $k_1$ in any box.
For this problem, the formulation for $3$ odd, $0$ even for person $A$ would be
$\dfrac{\dbinom{5}{3,1,1}\dbinom{4}{0,2,2}}{\dbinom{9}{3,3,3}}$
But the configuration $3$ odd, $0$ even could go to any of the three people, hence multiplication by $3$


Linux locally dropping UDP packets (Receive buffer overflow)

Question: I built a simple test program that sends 100 UDP packets, each of size 1000 bytes, every 250ms. I'm finding that two separate linux boxes are dropping some of these packets.
My expectation is that when I write the 100 UDP packets, the kernel buffers are able to hold on to all of these packets. netstat -us indicates that there are RcvbufErrors for every packet that gets dropped. So it seems like the kernel receive buffer is overflowing. But I don't understand why.
/proc/sys/net/core/rmem_default shows a value of 212992, so I don't believe increasing that will help because it's already larger than the 100kB batches this test is sending.
I ran a second test where I send 50 UDP packets, each of size 1000 bytes, every 10ms. I found that no packets got dropped on this test. This indicates to me that 100kB overflows buffers, while 50kB does not. It also further indicates that I'm not CPU limited or packet limited -- this test sends packets many times faster than the first.
The two linux machines I used: virtualbox on a macbook, and an AWS m5a.xlarge.
My test program: https://github.com/theicfire/rando/tree/master/udp-fail-batch-stack-overflow
Answer: You're right on the edge of the buffer size, although it doesn't seem like it.
The value of rmem_default does not mean there are exactly that many bytes available to use. I got a hint of this when reading the man page for setting SO_RECVBUF:
          The kernel doubles this value (to allow space for bookkeeping
          overhead) when it is set using setsockopt(2), and this doubled
          value is returned by getsockopt(2).

What this means is that the kernel is taking up some part of this buffer. Perhaps a significant amount (i.e. it might be using up half of what rmem_default is set to.
Setting the SO_RCVBUF option to 125,000 stops packets from dropping. Setting it to 100,000 (a bit less than the batches of packets you're sending, due to packet headers) results in packets getting dropped. Even though the result of getsockopt is 250,000 and 200,000, respectively.
Comment: Definitely don't rely on the system's default size.  If you know up front what the size of the traffic you are going to receive is going to be, use `setsockopt()` to explicitly set the actual buffer size you will need.


How to customize the comments form?

Question: For example, How can I add some classes or hide labels and instead of it add placeholder with using of ThemeName_preprocess_comment to 'subject' and 'name' comments form elements?
I have tried to usedrupal_set_message(kint($variables)); in ThemeName_preprocess_comment section, but it cause the empty screen erroe in my site.
So how can I add some classes to default (or my own created) comments elements so I can target them for adding CSS style.
How can I target default comments form element in CSS?
Answer: if you use Drupal7, you should not use
drupal_set_message(kint($variables));

but
kint($variables)

or
dpm($variables)

An example that you can see, enter in the following link:
 https://api.drupal.org/api/drupal/modules%21comment%21comment.module/function/template_preprocess_comment/7.x
function bartik_preprocess_comment(&$variables) {

  $variables['classes_array'][] = 'myclass';
  dpm($variables);

}

You can also create a file "comment-wrapper.tpl.php" and add the classes you want and placeholder.
Example:
<?php

/**
 * @file
 * Bartik's theme implementation to provide an HTML container for comments.
 *
 * Available variables:
 * - $content: The array of content-related elements for the node. Use
 *   render($content) to print them all, or
 *   print a subset such as render($content['comment_form']).
 * - $classes: String of classes that can be used to style contextually through
 *   CSS. It can be manipulated through the variable $classes_array from
 *   preprocess functions. The default value has the following:
 *   - comment-wrapper: The current template type, i.e., "theming hook".
 * - $title_prefix (array): An array containing additional output populated by
 *   modules, intended to be displayed in front of the main title tag that
 *   appears in the template.
 * - $title_suffix (array): An array containing additional output populated by
 *   modules, intended to be displayed after the main title tag that appears in
 *   the template.
 *
 * The following variables are provided for contextual information.
 * - $node: Node object the comments are attached to.
 * The constants below the variables show the possible values and should be
 * used for comparison.
 * - $display_mode
 *   - COMMENT_MODE_FLAT
 *   - COMMENT_MODE_THREADED
 *
 * Other variables:
 * - $classes_array: Array of html class attribute values. It is flattened
 *   into a string within the variable $classes.
 *
 * @see template_preprocess_comment_wrapper()
 */
?>
<?php
    //add placeholder
    $content['comment_form']['subject']['#attributes']['placeholder'] = t('Title');
    $content['comment_form']['comment_body']['und'][0]['value']['#attributes']['placeholder'] = t('Comment Body');
  ?>
<div id="comments" class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if ($content['comments'] && $node->type != 'forum'): ?>
    <?php print render($title_prefix); ?>

<?php print t('Comments'); ?>

    <?php print render($title_suffix); ?>
  <?php endif; ?>

  <?php print render($content['comments']); ?>

  <?php if ($content['comment_form']): ?>

<?php print t('Add new comment'); ?>

    <?php print render($content['comment_form']); ?>
  <?php endif; ?>

Comment: Thank you so much for help and reply, I appreciate you greatly, But I use Drupal 8. thanks anyway.
Answer: i would do it:
1) create new file: testmodule.info.yml.
name: testmodule
description: Testmodule
type: module
core: 8.x

2) create new file: testmodule.module
<?php

function testmodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if ($form_id=='comment_comment_form') {

    //add class subject
    $form['subject']['widget']['0']['value']['#attributes']['class'][]='newclass_subject';

    //add placeholder subject
    $form['subject']['widget']['0']['value']['#attributes']['placeholder'] = t('Placeholder Subject');

    //add class body
    $form['comment_body']['widget']['0']['#attributes']['class'][]='newclass_body';

    //add placeholder body
    $form['comment_body']['widget']['0']['#attributes']['placeholder'] = t('Placeholder Body');

  }

}

How to customize the comments form?


Evaluating the integral $\int_{-\infty}^\infty x^2\frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x^2} \ dx$

Question:
How does one evaluate $$\int_{-\infty}^\infty x^2\frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x^2} \ dx ?$$

The result is $1$ and it corresponds to $E[X^2]$, where $X$ is a random variable with $X\sim\mathcal{N}(0,1)$. I have tried to do some substituions and I've tried integration by parts but didn't succeed to integrate it. With the integration by parts I ended up with a harder integral in both cases and I couldn't find a good substitution.
Comment: What about $Var(X)=E(X^2)-\left( E(X) \right) ^2$ ?
Comment: Do you have available the special value $\int_{-\infty}^{\infty} e^{-t^2} dt = \sqrt{\pi}$?
Comment: No. But if it shouldn't work otherwise you may use it. It would be nice to know how it works that way anyways.
Comment: @callculus yes you may use that fact. I see, that would make things very simple: $1=E(X^2)-0^2 \ \Longrightarrow E(X^2) = 1$. Thanks for that one!
Comment: That was my intention.
Comment: why such questions aren't closed as duplicates? this was asked dozens of times here...
Answer: Notice, integral can be usually computed using Laplace transform as follows,  $$\int_{-\infty}^{\infty}x^2\frac{1}{\sqrt{2\pi }}e^{-\frac{x^2}{2}}\ dx $$ By symmetry of even function: $f(-x)=f(x)$,  $$=2\frac{1}{\sqrt{2\pi }}\int_{0}^{\infty}x^2e^{-\frac{x^2}{2}}\ dx $$$$=\sqrt{\frac{2}{\pi }}\int_{0}^{\infty}x^2e^{-\frac{x^2}{2}}\ dx $$
let $\frac{x^2}{2}=u\implies x\ dx=du$ or $dx=\frac{1}{\sqrt{2u}}\ du=\frac{1}{\sqrt2}\frac{ du}{\sqrt u}$,
$$=\sqrt{\frac{2}{\pi }}\int_{0}^{\infty}(2u)e^{-u} \frac{1}{\sqrt2}\frac{ du}{\sqrt u}$$
$$=\frac{2}{\sqrt {\pi}}\int_{0}^{\infty}u^{1/2}e^{-u}\ du$$
using Laplace transform: $\color{blue}{\int_0^{\infty}t^ne^{-st}\ dt=\frac{\Gamma(n+1)}{s^{n+1}}}$,
$$=\frac{2}{\sqrt {\pi}}\left[\frac{\Gamma\left(1+\frac{1}{2}\right)}{s^{1+\frac{1}{2}}}\right]_{s=1}$$
$$=\frac{2}{\sqrt {\pi}}\left[\frac{\frac{1}{2}\Gamma\left(\frac{1}{2}\right)}{(1)^{3/2}}\right]$$
$$=\frac{2}{\sqrt {\pi}}\left[\frac{1}{2}\sqrt \pi\right]=\color{red}{1}$$
Comment: @ndrizza Laplace Transforms typically are covered in a differential equations course, you could take a look at that or simplicity, you can google "Laplace Transform Table"
Comment: Thanks. But I don't know how to do a Laplace transform. Maybe there is not even a way to do it without it.
Comment: There may be other methods of doing this but it is one of the easiest & simpliest methods
Comment: Alright. I'll have a look at it. Thank you!
Answer: Here is another solution
starting with
$$\int_{-\infty}^{\infty} e^{-t^2} dt = \sqrt{\pi}$$
and with simple substitution generalizing to (for $u>0$)
$$f(u) = \int_{-\infty}^{\infty} e^{-ut^2} dt = \sqrt{\frac{\pi}u}$$
Now, take the derivative w.r.t. u and evaluate at $u=1.$
$$\frac{d f(u)}{du} \biggr\rvert_{u=1} = \int_{-\infty}^{\infty} t^2e^{-t^2} dt = \frac12 \sqrt{\frac{\pi}{u^3}} = \frac{\sqrt{\pi}}2
$$
Or, evaluating at $u=\frac12$ will give
$$
\int_{-\infty}^{\infty} t^2e^{-\frac12 t^2} dt = \sqrt{2\pi}
$$
Answer: We know that, if $F'=f$, then
$$\int_a^bxf(x)dx=bF(b)-aF(a)-\int_a^b F(x)dx$$
Now, note that if $F(x)=e^{-x^2/2}$ then $F'(x)=f(x)=-xe^{-x^2/2}$.
So,
$$\int_{-\infty}^\infty x^2e^{-\frac{x^2}2}dx=-\lim_{x\to\infty}\left(2xe^{-\frac{x^2}2}-\int_{-x}^xF(t)dt\right)=\sqrt{2\pi}$$
Answer: Here are two solutions that exploit the same two tools in different orders:

It's a standard trick using elementary multivariable calculus and converting to polar coordinates (see, e.g., this answer) to show that the Gaussian integral has value
$$\require{cancel}\int_{-\infty}^{\infty} e^{-t^2} dt = \sqrt{\pi} .$$
Now, applying integration by parts with $u = e^{-t^2}, dv = dt$ to the above integral gives $du = -2 t e^{-t^2} dt, v = t$ and hence
$$\sqrt{\pi} = \cancelto{0}{\left.\left(e^{-t^2}\right)(t)\right\vert_{-\infty}^{\infty}} - \int_{-\infty}^{\infty} (t)(-2t e^{-t^2}) dt = 2 \int_{-\infty}^{\infty} t^2 e^{-t^2} dt,$$
and so
$$\int_{-\infty}^{\infty} t^2 e^{-t^2} dt = \frac{\sqrt{\pi}}{2} .$$
Now, substituting $t = \frac{x}{\sqrt{2}}, dt = \frac{dx}{\sqrt{2}}$ in the integral gives
$$\frac{1}{2 \sqrt{2}} \int_{-\infty}^{\infty} x^2 e^{-\frac{x^2}{2}} dx = \frac{\sqrt{\pi}}{2},$$
and multiplying by $\frac{2}{\sqrt{\pi}}$ gives the desired result:
$$\color{#bf0000}{\boxed{\int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} x^2 e^{-\frac{x^2}{2}} dx = 1}} .$$
We can apply the "multivariable polar" trick directly to the integral at hand rather than to the Gaussian integral: If one denotes $$I := \int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} x^2 e^{-\frac{x^2}{2}} dx ,$$ we have
$$
I^2
= \int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} x^2 e^{-\frac{x^2}{2}} dx \cdot \int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} y^2 e^{-\frac{y^2}{2}} dy
= \frac{1}{2 \pi} \int_{-\infty}^{\infty} \int_{-\infty}^{\infty} x^2 y^2 e^{-\frac{x^2 + y^2}{2}} dx \, dy .
$$
Now, converting to polar coordinates gives
\begin{align}
I^2
&= \frac{1}{2 \pi} \int_0^{2 \pi} \int_0^{\infty} (r \cos \theta)^2 (r \sin \theta)^2 e^{-\frac{r^2}{2}} \cdot r \,dr \,d\theta \\
&= \frac{1}{2 \pi} \int_0^{2 \pi} \int_0^{\infty} r^5 e^{-\frac{r^2}{2}} \sin^2 \theta \cos^2 \theta \,dr \,d\theta \\
&= \frac{1}{2 \pi} \int_0^{2 \pi} \sin^2 \theta \cos^2 \theta \,d\theta \int_0^{\infty} r^5 e^{-\frac{r^2}{2}} dr .
\end{align}
Now, the first integral is a standard exercise that can be handled with double-angle identities and has value $\frac{\pi}{4}$. The second integral can be evaluated readily with the substitution $u = -\frac{r^2}{2}, du = -r \,dr$ and has value $8$, so
$$I^2 = \frac{1}{2 \pi} \left(\frac{\pi}{4}\right) (8) = 1 .$$
On the other hand, the integrand in $I$ is everywhere nonnegative, and so again $I$ has value
$$\color{#bf0000}{\boxed{\int_{-\infty}^{\infty} \frac{1}{\sqrt{2 \pi}} x^2 e^{-\frac{x^2}{2}} dx = 1}}$$
as claimed.
Answer: Let's assume an integral $\int_{-\infty}^{\infty}x^2\frac{1}{\sqrt{2\pi }}e^{-{\alpha x^2}}\ dx$ you have $\alpha=1/2$, then
$$\int_{-\infty}^{\infty}x^2\frac{1}{\sqrt{2\pi }}e^{-{\alpha x^2}}\ dx=-\int_{-\infty}^{\infty}\frac{1}{\sqrt{2\pi }}\frac{ \partial }{ \partial \alpha}e^{-\alpha x^2}\ dx=-\frac{1}{\sqrt{2\pi }}\frac{ \partial }{ \partial \alpha}\int_{-\infty}^{\infty}e^{-\alpha x^2}\ dx.$$
The letter is the well known Euler–Poisson integral, which is equal to $\sqrt{\frac{\pi}{\alpha}}$, so:
$$-\frac{1}{\sqrt{2\pi }}\frac{ \partial }{ \partial \alpha}\int_{-\infty}^{\infty}e^{-\alpha x^2}\ dx=-\frac{1}{\sqrt{2\pi }}\frac{ \partial }{ \partial \alpha}\sqrt{\frac{\pi}{\alpha}}=\frac{1}{2\sqrt{2}}\alpha^{-3/2}.$$
After setting $\alpha=1/2$ you'll get the correct answer $\int_{-\infty}^{\infty}x^2\frac{1}{\sqrt{2\pi }}e^{-\frac{x^2}{2}}\ dx=1$.
Answer: \begin{align*}
\int_{-\infty}^\infty x^2\frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x^2} \; \mathrm{d}x&\overset{IBP}=\frac{1}{\sqrt{2 \pi}}\int_{-\infty}^{\infty} e^{-\frac{1}{2}x^2} \; \mathrm{d}x\\
&=\frac{1}{\sqrt{\pi}}\int_{-\infty}^{\infty} e^{-x^2} \; \mathrm{d}x\\
&= \frac{\sqrt{\pi}}{\sqrt{\pi}}\\
&=\boxed{1}
\end{align*}
Where $u=\frac{x}{\sqrt{2 \pi}}$ and $dv=xe^{-\frac{1}{2}x^2} \implies v=-e^{-\frac{1}{2}x^2}$


Degrees of freedom for t-test after delta method

Question: Say I have a dataset with $n$ observations of two independent variables, X and Y. From the observations of the X variable I estimate a parameter $\hat{\alpha}$ and corresponding variance $\hat{\sigma}_{alpha}^2$, and likewise from variable Y I estimate $\hat{\beta}$ and corresponding variance $\hat{\sigma}_{beta}^2$. Now I define a new parameter $\hat{\gamma} = g(\hat{\alpha}, \hat{\beta})$. The delta method will give me an approximation of $Var(\hat{\gamma})$.
I can now define a test statistic $\frac{\hat{\gamma}-\gamma_0}{\sqrt{\hat{Var}(\hat{\gamma})}}$ to test H0: $\gamma=\gamma_0$. My question is, how many degrees of freedom does this t-distribution have? Is it simply 2n-2? And how about when I need to estimate a nuisance parameter $\psi$ when estimating $\alpha$? 2n-3?
Comment: To answer this question, we need to know more about the form of $g$ and $Var(\hat{\gamma})$.
Comment: @Greenparker Really? Say g(a,b) = a/b, the variance of $\gamma$ is approximated by the delta method.
Comment: Delta method does not estimate the variance of $\hat{\gamma}$, but is used to understand the form/structure of $Var(\gamma)$. The degrees of freedom of the $t$ will be determined by the estimator of $Var(\gamma)$. So what is the estimator?
Comment: @Greenparker both $\alpha$ and $\beta$ are estimated by maximum likelihood, yielding also the estimates of their variances.
Answer: Since you haven't provided the exact quantities involved in the problem, let me answer the question through an example. Suppose $(X_i,Y_i)$ are  iid random variables with mean $(\alpha,\beta)$. We also assume that $X_i$ and $Y_i$ are independent for all $i$. We estimate $\alpha$ and $\beta$ with their maximum likelihood estimators $(\hat{\alpha},\hat{\beta})$. We assume the underlying distributions are such that asymptotic normality of the MLE holds. Then for a sample of size $n$,
$$\sqrt{n} \left( \left( \begin{array}{c} \hat{\alpha} \\ \hat{\beta} \end{array}\right) - \left( \begin{array}{c} \alpha \\ \beta \end{array}\right)  \right) \overset{d}{\to} N\left(\left( \begin{array}{c} 0 \\ 0 \end{array}\right)
 , \left( \begin{array}{c} \sigma^2_{\hat{\alpha}} &0  \\ 0 & \sigma^2_{\hat{\beta}} \end{array}\right)\right)\,,$$
where the covariance is 0 because $X$ and $Y$ are independent. Now you say in the comment that $g$ is say $g(a,b) = a/b$. Then by the Delta Method, the asymptotic variance for $\hat{\alpha}/ \hat{\beta}$ is
\begin{align*}
&\nabla g(\alpha, \beta)^T \left( \begin{array}{c} \sigma^2_{\hat{\alpha}} &0  \\ 0 & \sigma^2_{\hat{\beta}} \end{array}\right) \nabla g(\alpha, \beta)\\
&= \left(\dfrac{1}{\beta} \, \, \,\,\, \dfrac{-\alpha}{\beta^2} \right)\left( \begin{array}{c} \sigma^2_{\hat{\alpha}} &0  \\ 0 & \sigma^2_{\hat{\beta}} \end{array}\right) \left( \begin{array}{c} \dfrac{1}{\beta} \\ \dfrac{-\alpha}{\beta^2} \end{array}\right)\\
& = \dfrac{\sigma^2_{\hat{\alpha}}}{\beta^2} + \dfrac{\alpha^2 \sigma^2_{\hat{\beta}}}{\beta^4}\,.
\end{align*}
So, then by the Delta method,
$$\sqrt{n} \left(\dfrac{\hat{\alpha}}{\hat{\beta}}  - \dfrac{\alpha}{\beta} \right) \overset{d}{\to} N\left(0, \dfrac{\sigma^2_{\hat{\alpha}}}{\beta^2} + \dfrac{\alpha^2 \sigma^2_{\hat{\beta}}}{\beta^4}\right)\,.$$
Up till this point, I have been setting the notation and making sure this example aligns with your situation. If $\hat{\sigma}^2_{\hat{\alpha}}$ and $\hat{\sigma}^2_{\hat{\beta}}$ are consistent estimators of $\sigma^2_{\hat{\alpha}}$ and $\sigma^2_{\hat{\beta}}$ respectively, then a consistent estimator of the asymptotic variance is,
$$\dfrac{\hat{\sigma}^2_{\hat{\alpha}}}{\hat{\beta}^2} + \dfrac{\hat{\alpha}^2 \hat{\sigma}^2_{\hat{\beta}}}{\hat{\beta}^4}\,. $$
Your question, then indirectly alleges that, the following must have an approximate $t$ distribution:
$$\dfrac{\sqrt{n}\left(\dfrac{\hat{\alpha}}{\hat{\beta}}  - \dfrac{\alpha}{\beta} \right)}{\sqrt{\dfrac{\hat{\sigma}^2_{\hat{\alpha}}}{\hat{\beta}^2} + \dfrac{\hat{\alpha}^2 \hat{\sigma}^2_{\hat{\beta}}}{\hat{\beta}^4}}   } \,.$$
However, the above quantity is likely not approximately $t$-distributed. In the usual mean $t$-tests, this methods yield a $t$ test statistics, because the sample variance yields an approximate $\chi^2$ distribution. Each individual $\hat{\sigma}^2_{\hat{\alpha}}$ and $\hat{\sigma}^2_{\hat{\beta}}$ approximately yields a $\chi^2$ distribution, but their linear combination via random variables as here, will likely not allow such an approximation. Further, a Welch-Satterwaite approximation cannot be used to the the linear combination involving random variables.
So to answer your question, degrees of freedom in this case cannot be calculated because the test statistic will not be approxiamtely $t$-distributed. (Unless of course I am missing out on some literature about such a linear combination of sample variances).
With regard to testing, an application of Slutsky's theorem would indicate that you could use the large sample approximation of the test statistic being approximately normally distributed. In that case you could just do a $z$-test.
Comment: could this method be used to answer this question? https://stats.stackexchange.com/questions/398436/a-b-testing-ratio-of-sums How would you extend this to a test of difference of means ? i.e. a z test


Prove $\binom{n}{k} = \frac {n!}{k!(n - k)!} \text { where } 0 \le k \le n.$

Question: Proof(incomplete):

There are  $\frac {n!}{(n - k)!} k$- permutations. Each $k$-element subset can be listed in $k!$ ways. The number of ways to first choose a $k$ element subset and to list the elements of that subset is $\binom{n}{k} \cdot \frac {n!}{(n - k)!}$.

We are trying to prove what $\binom{n}{k} $ is, yet we are allowed to use it in the middle of its own proof? Please, explain what's up with that.
edit: $\binom{n}{k} \cdot \frac {n!}{(n - k)!}$ should be $\binom{n}{k} \cdot k!$.
Comment: What is your definition of $\binom{n}{k}$?
Comment: The proof looks incorrect for other reasons. I think the last formula should have been $\binom{n}{k} \cdot k!$. Where did this proof come from?
Comment: @David K, my bad. You're right.
Comment: @ Jakubussimus, isn't the equation in the OP definition of $C(n ,k)$?
Answer: It depends on how you have defined $\binom nk$. If the definition of $\binom nk$ is
"the number of $k$-element subsets of an $n$-element set"
then to evaluate "the number of ways to first choose a $k$ element subset and to list the elements of that subset," you have, by definition,
$\binom nk$ ways to perform the first step (choose a $k$-element subset),
and for each of those ways you have $k!$ choices for the second step
(order in which you list the elements of the subset),
ergo the total number of ways to perform both steps is $\binom nk \cdot k!$.
So we may not yet know how to write a formula for $\binom nk$, but we know what
it means and can use it in a larger formula.
Of course if we defined $\binom nk = \frac{n!}{(n-k)!k!}$ instead, then we wouldn't
have to prove that fact, but we could use a similar argument to show that
$\binom nk$ is the number of $k$-element subsets of an $n$-element set.
But if we defined $\binom nk$ as a binomial coefficient then we have some other
work to do to prove that it is equal to either of the other two things.
Answer: To derive the formulas for$~_n C_k=\binom{n}{k}$ using the definition that $\binom{n}{k}$ represents the number of different $k$ element subsets out of a set of $n$ distinct elements where order does not matter, you may consider this a multiplication principle question.
Step 1: pick the first element to appear ($n$ choices), Step 2: pick the second element to appear ($n-1$ choices), ... Step $k$: pick the $k$th element to appear ($n-k+1$ choices).
Noticing then that we overcounted and for each "different" possibility, we have counted the possibility a total of $k!$ number of times (since those $k$ steps could have been done in any different order), we divide by the amount $k!$ to account for the symmetry involved.
Thus, $\binom{n}{k} = \dfrac{n\cdot(n-1)\cdot(n-2)\cdots(n-k+1)}{k!} = \dfrac{n!}{(n-k)!k!}$
As for the condition that $0\leq k\leq n$, note that there is no way to choose more things from our set than there exist elements in the set in the first place.  Similarly, it doesn't make sense to take a negative amount of things from the set.  Indeed, $m!$ is not defined for any negative integer $m$, and as such if $k$ is outside of the range $[0,n]$ then one of $k!$ or $(n-k)!$ would not be defined.  In that case, we define $\binom{n}{k}$ to be $0$.


What does -1 mean in glm model specification mean?

Question: I am working through Applied Longitudinal Data Analysis: Modeling Change and Event Occurence by Singer and Willett, using the R code supplied here. I am working through Chapter 11, on fitting discrete-time hazard models, and came across this model specification.
model <-glm(event~factor(period) + pt + pas - 1, family="binomial", data=firstsex.pp)

Which yields this output:
Call:
glm(formula = event ~ factor(period) + pt + pas - 1, family = "binomial",
    data = firstsex.pp)

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-1.1787  -0.6182  -0.4338  -0.2836   2.7862

Coefficients:
                 Estimate Std. Error z value Pr(>|z|)
factor(period)7   -2.8932     0.3206  -9.024  < 2e-16 ***
factor(period)8   -3.5848     0.4230  -8.474  < 2e-16 ***
factor(period)9   -2.1502     0.2775  -7.750 9.20e-15 ***
factor(period)10  -1.6932     0.2646  -6.398 1.58e-10 ***
factor(period)11  -1.5177     0.2757  -5.504 3.71e-08 ***
factor(period)12  -1.0099     0.2811  -3.592 0.000328 ***
pt                 0.6605     0.2367   2.790 0.005266 **
pas                0.2964     0.1254   2.364 0.018089 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1139.53  on 822  degrees of freedom
Residual deviance:  629.15  on 814  degrees of freedom
AIC: 645.15

Number of Fisher Scoring iterations: 5

I am familiar with logistic regression but have never encountered the -1 in a glm model specification before. The interpretation of the regression coefficients in the book seems to be exactly the same as it usually would be.
I am finding it difficult to obtain any online examples where it is used. What does this -1 mean? Is it something unique to survival analysis?
Comment: Where is the intercept in your output summary?  Since you're using `R`'s formula notation, it's time to read its manual page on `formula`.
Comment: yes of course, the intercept is missing! The coefficient for each time period *is* that period's own intercept. Thank you @whuber. You are right. I do need to read the manual.
Answer: My guess is your variable period has only the values 7:12.  You are modeling that as a factor (i.e., categorical variable).  Literally, -1 in R is a command to supress the intercept.  As a result, you end up using level means coding, instead of reference level coding.  This doesn't actually change the model, but does cause the presentation to look different, and changes the interpretation of some of the values in the output.
Comment: You're welcome, @llewmills. It's always hard to search for something if you don't know what the search terms are supposed to be ;-).
Comment: Thank you @gung. I figured someone had probably asked a similar question somewhere on CV, but hadn't noticed the missing intercept so couldn't search under those terms.


paging and pre-paging technique used in linux kernel

Question: I want to know what's the current page swapping algorithm used in Linux kernel. Also, is there any pre-paging technique used in Linux kernel.
Answer: The short answer is that an LRU(Least Recently Used) approach is taken.
The long answer is found here with references to other sources as well..
Comment: The short answer fails to answer the second question: "is there any pre-paging technique used in Linux kernel?"