Should I store frequently running/needed query results to the Session scope in Coldfusion?

Question: I have a system running Coldfusion8/MySQL 5.0.88.
On some pages I need to pre-query parameters based on (logged in) users. For example, the site has a product search, which only allows to search products from sellers that authorized the user (allow-list).
Right now, when the user goes to the products page, I'm building the allow-list to display sellers. When the user triggers a product search, I invoke a search.cfc, which again builds the allow-list to perform the search. On another page, the user can lock/unlock sellers, so the list may change regularly.
Question:
Should I store the allow-list to the Session to have it available everywhere and not have to requery the database all the time? The list can be quite long (13-digit ID, comma separated). WOuld I need a Session.rebuild = true/false parameter to indicate, when to rebuild and not take the current Session value (after updating). Also will query caching make any sense if the query is identical, but on separate pages?
Thanks for some thoughts.
Comment: the query `cachedwithin` is based on a number of properties of the cfquery tag. It's possible that you'll end up with more than copy in the cache if you have more than one cfquery getting the results. (I would assume your cfquery is in a single function in the CFC, so it shouldn't be an issue.)  As for the authorization issue, that may still be protected values.  What will the sellers say if someone makes a purchase from them 5 minutes after that person was disallowed from buying from that seller?
Comment: Good point. I just ran through my user_cfc and set a Session.rebuild_user flag to all functions that alter the underlying table and `allow-list`. Haven't tesed, but buyer-wise I think this is ok. Seller-wise I'm not sure yet, because the seller frequently update their allowed-list and I don't know if how to catch this if the buyer is logged in. Any idea?
Comment: we are talking edge case here
Answer: As long as the values stored in the session are not the security that protects them, that's fine.
Query caching is based on the query being sent to the server, if it is identical it should not even touch the db, but get it from memory, effectively doing what your suggesting.
Comment: No, it's not the protection values, it's the resulting from checking for authorization (id 1 is ok, id 2 is ok...). Regarding caching, you are saying it does not matter if the query name="abc" is on page 1 and page 2. First time it is cached, it will be read from the cache on every page? I still need to figure out how to re-cache once the user changes something though
Answer: Choosing where to save the cache depends a lot on what you expect your system to do.
You don't want to cache the data for longer than is useful, so putting it in the Session scope is good because it will only be cached for as long as the user is active.  Additionally, if sellers don't care if a buyer manages to buy something from then even if they've been recently removed, then you never need to update the cache, just wait for it to expire.
Alternately you can rely on ColdFusion's built in query cache.  You can supply a timeframe that you expect most users to be active on the site and you can easily update the cache with a small bit of code.  A possible disadvantage, or advantage, depending on your point of view is that there is a fixed number of cached queries on the server which will flush your cache for you if the number of cached queries is exceeded.  This provides some level of memory management at the expense of possibly refetching queries that you would prefer stayed cached.
Here is one way to handle the query cache.  This will cache the query until the timespan has elapsed; the cached results are removed as part of a max cached queries exceeded purge, or a code based flush of the entire query cache.
<cffunction name="getData" access="public" output="false" returntype="string" hint="Returns a hash of the supplied string.">
    <cfargument name="Id" type="numeric" required="true">
    <cfargument name="ClearCache" type="boolean" default="false" required="false">

    <cfif Arguments.ClearCache EQ false>
        <cfset local.CachedWithin = CreateTimeSpan(0,0,10,0)>
    <cfelse>
        <cfset local.CachedWithin = CreateTimeSpan(0,0,0,-1)>
    </cfif>

    <cfquery name="local.qryGetData" datasource="#Variables.DSN#" cachedwithin="#local.CachedWithin#">
        ...
    </cfquery>

    <cfreturn local.qryGetData>
</cffunction>

You can then have a function that you run after an update to clear out the relevant cached queries.
<cffunction name="clearCache" access="public" output="false" returntype="void">
    <cfargument name="Id" type="numeric" required="true">

    <cfset Variables.getData(Id=Arguments.Id,  ClearCache=true)>
    <cfset Variables.getSomethingElse(Id=Arguments.Id, ClearCache=true)>

</cffunction>

Comment: Cool. That looks nice. Trying it out
Comment: The limit for cached queries is 10,000. This number is not that high be default and must be changed in the CF administrator. I'm not sure what it is in Railo. You'll definitely want to make sure that you have the number above the number of queries X the number of active sessions on the site.


Haskell: Reducing Boilerplate

Question: What is the accepted way to reduce the amount of repetition in code like this?
newtype Fahrenheit = Fahrenheit Double deriving (Eq)
newtype Celsius    = Celsius   Double deriving (Eq)
newtype Kelvin     = Kelvin    Double deriving (Eq)
newtype Rankine    = Rankine   Double deriving (Eq)
newtype Reaumure   = Reaumure  Double deriving (Eq)
newtype Romer      = Romer     Double deriving (Eq)
newtype Delisle    = Delisle   Double deriving (Eq)
newtype Newton     = Newton    Double deriving (Eq)

instance Show Fahrenheit where
  show (Fahrenheit f) = show f ++ " °F"

instance Show Celsius where
  show (Celsius c) = show c ++ " °C"

instance Show Kelvin where
  show (Kelvin k) = show k ++ " K"

instance Show Rankine where
  show (Rankine r) = show r ++ " °R"

instance Show Reaumure where
  show (Reaumure r) = show r ++ " °Ré"

instance Show Romer  where
  show (Romer  r) = show r ++ " °Rø"

instance Show Delisle where
  show (Delisle d) = show d ++ " °De"

instance Show Newton where
  show (Newton n) = show n ++ " N°"

class Temperature a where
  increaseTemp  :: a -> Double -> a
  decreaseTemp  :: a -> Double -> a
  toFahrenheit  :: a -> Fahrenheit
  toCelsius     :: a -> Celsius
  toKelvin      :: a -> Kelvin
  toRankine     :: a -> Rankine
  toReaumure    :: a -> Reaumure
  toRomer       :: a -> Romer
  toDelisle     :: a -> Delisle
  toNewton      :: a -> Newton

instance Temperature Fahrenheit where
  increaseTemp (Fahrenheit f) n = if n < 0 then error "negative val" else Fahrenheit $ f + n
  decreaseTemp (Fahrenheit f) n = if n < 0 then error "negative val" else Fahrenheit $ f - n
  toFahrenheit                  = id
  toCelsius    (Fahrenheit f)   = Celsius  $ (f - 32) * 5 / 9
  toKelvin     (Fahrenheit f)   = Kelvin   $ (f - 32) * 5 / 9 + 273.15
  toRankine    (Fahrenheit f)   = Rankine  $ f + 458.67
  toReaumure   (Fahrenheit f)   = Reaumure $ (f - 32) * 4 / 9
  toRomer      (Fahrenheit f)   = Romer    $ (f - 32) * 7 / 24 + 7.5
  toDelisle    (Fahrenheit f)   = Delisle  $ (212 - f) * 5 / 6
  toNewton     (Fahrenheit f)   = Newton   $ (f - 32) * 11 / 60

instance Temperature Celsius where
  increaseTemp (Celsius c) n = if n < 0 then error "negative val" else Celsius   $ c + n
  decreaseTemp (Celsius c) n = if n < 0 then error "negative val" else Celsius   $ c - n
  toFahrenheit  (Celsius c)   = Fahrenheit $ c * 9 / 5 + 32
  toCelsius                  = id
  toKelvin     (Celsius c)   = Kelvin    $ c + 273.15
  toRankine    (Celsius c)   = Rankine   $ c * 9/5 + 491.67
  toReaumure   (Celsius c)   = Reaumure  $ c * 4 / 5
  toRomer      (Celsius c)   = Romer     $ c * 21 / 40 + 7.5
  toDelisle    (Celsius c)   = Delisle   $ (100 - c) * 3 / 2
  toNewton     (Celsius c)   = Newton    $ c * 33 / 100

instance Temperature Kelvin where
  increaseTemp (Kelvin k) n = if n < 0 then error "negative val" else Kelvin    $ k + n
  decreaseTemp (Kelvin k) n = if n < 0 then error "negative val" else Kelvin    $ k - n
  toFahrenheit  (Kelvin k)   = Fahrenheit $ (k - 273.15)  *  9 / 5 + 32
  toCelsius    (Kelvin k)   = Celsius   $ k - 273.15
  toKelvin                  = id
  toRankine    (Kelvin k)   = Rankine   $ k * 9 / 5
  toReaumure   (Kelvin k)   = Reaumure  $ (k - 273.15) * 4 / 5
  toRomer      (Kelvin k)   = Romer     $ (k - 273.15) * 21 / 40 + 7.5
  toDelisle    (Kelvin k)   = Delisle   $ (373.15 - k) * 3 / 2
  toNewton     (Kelvin k)   = Newton    $ (k - 273.15) * 33 / 100

-- rest of the instances omitted.

also, in the Class definition is there a way of restricting the type of in the input variable to one of the Units. ie toCelsius :: a -> Celsius, is there anything that can be done about constraining what that a can be? or is that implied by the fact that it will only work on Types that have instances declared.
Comment: This is more a case for codereview IMHO
Comment: Consider a pair of types `data TemperatureScale = Fahrenheit | Celsius | ...` and `data Temperature = Temperature TemperatureScale Double`. Also, instead of defining n^2 conversion routines, define 2n routines to convert each temperature to and from a single canonical scale. For example, instead of defining `Fahrenheit <-> Celsius`, just use `Fahrenheit <-> Kelvin <-> Celsius`.
Answer: Main problem seems to be the unit conversions, which you can make significantly shorter and less boilerplat-y using DataKinds and a bunch of other scary looking language extensions (only for 3 units, but you should be able to generalise this easily enough):
{-# LANGUAGE DataKinds,
             KindSignatures,
             RankNTypes,
             ScopedTypeVariables,
             AllowAmbiguousTypes,
             TypeApplications #-}
data TemperatureUnit = Fahrenheit | Celsius | Kelvin
newtype Temperature (u :: TemperatureUnit) = Temperature Double deriving Eq

class Unit (u :: TemperatureUnit) where
  unit :: TemperatureUnit

instance Unit Fahrenheit where unit = Fahrenheit
instance Unit Celsius where unit = Celsius
instance Unit Kelvin where unit = Kelvin

instance Show TemperatureUnit where
  show Celsius = "°C"
  show Fahrenheit = "°F"
  show Kelvin = "K"

instance forall u. Unit u => Show (Temperature u) where
  show (Temperature t) = show t ++ " " ++ show (unit @u)

convertTemperature :: forall u1 u2. (Unit u1, Unit u2) => Temperature u1 -> Temperature u2
convertTemperature (Temperature t) = Temperature . fromKelvin (unit @u2) $ toKelvin (unit @u1) where
  toKelvin Celsius = t + 273.15
  toKelvin Kelvin = t
  toKelvin Fahrenheit = (t - 32) * 5/9 + 273.15
  fromKelvin Celsius k = k - 273.15
  fromKelvin Kelvin k = k
  fromKelvin Fahrenheit k = (k - 273.15) * 9/5 + 32

You can then use it like this:
-- the explicit type signatures here are only there to resolve
-- ambiguities; In more realistic code you'd not need them as often
main = do
  let (t1 :: Temperature Celsius) = Temperature 10.0
      (t2 :: Temperature Fahrenheit) = Temperature 10.0
  putStrLn $ show t1 ++ " = " ++ show (convertTemperature t1 :: Temperature Fahrenheit)
  -- => 10.0 °C = 50.0 °F
  putStrLn $ show t2 ++ " = " ++ show (convertTemperature t2 :: Temperature Celsius)
  -- => 10.0 °F = -12.222222222222221 °C

Try it online!
The trick here is that DataKinds allows us to lift regular data types to the kind level, and their data constructor to the the type level (which I understand aren't really different things in modern versions of GHC anymore? Sorry, I'm a bit shaky on the subject myself). We then just define a helper class to get the data version of the unit back so we can dispatch based on it. This allows us the sort of thing you tried to do with all your newtype wrappers, except with less newtype wrappers (and less instance declarations, and less named functions overall).
The other thing of course is that you still have a combinatorial explosion between different unit conversions - you can either suck it up and write all n^2 formulas for those by hand, or you can try to generalise it (may be possible for temperature units as per @chepner's comment, but I'm not sure it's possible for all sorts of things you could want to convert between). This approach can't solve that inherent problem, but it does remove some of the syntactic noise you incur with the newtype-per-unit approach.
Your increaseTemp and decreaseTemp functions could be implemented as a single function offsetTemperature while allowing negative numbers. Although I think it'd make more sense to have them take a temperature with the same unit as the second parameter rather than just a Double:
offsetTemperature :: Temperature u -> Temperature u -> Temperature u
offsetTemperature (Temperature t) (Temperature offset) = Temperature (t + offset)

PS: Temperature should probably not be an instance of Eq - floating point equality is notoriously wonky (predictable, but probably doesn't do what you want). I only kept it in here because it was in your example.
Comment: I've applied @chepner's suggestion to cut down the conversions from n^2 to 2n
Comment: Arguably temperature *should* be an instance of Eq, it just shouldn't use floating point. Fixed point seems fine for this: decide you don't care about anything smaller than a millionth of a degree, and use an Int scaled up by a million instead of a Float.
Comment: With this approach, you definitely should have a role annotation.
Comment: @dfeuer That role would be phantom, right? It's clearly not representational, but I don't totally get what "nominal" means.
Comment: I don't think `Eq` is inherently a problem (as long as you don't allow NaN to be converted to a temperature), and you need that to get the more-useful `Ord` instance. The big floating-point problem is `Num`, and temperatures aren't `Num` anyway. You just have to watch out for the fact that any arithmetic will be approximate.
Comment: @amalloy, no, the role should be forced to `nominal`, which means "pretend it's a GADT index". You really want `coerce (a :: Temperature 'Fahrenheit) :: Temperature 'Celsius` to barf.
Comment: @dfeuer I thought nominal was the default? (Qualified by: I only learned about roles 5 minutes ago from your comment)
Comment: @amalloy You also need to avoid exporting the `Temperature` data constructor from the "public" interface.
Comment: @Cubic, the default is the inferred role, which in this case will be `phantom`. But you can always impose a more restrictive role (`phantom < representational < nominal`) with a role annotation.
Comment: @dfeuer I'm not sure how helpful hiding the `Temperature` constructor would be considering you'll want _some_ way to do a `Double -> Temperature u` conversion. I suppose you could restrict to to `fromKelvin` or something like that, but that's not very ergonomic.
Comment: @Cubic, you can make it very ergonomic using a pattern synonym.
Comment: You can also expose an "unsafe" `coerceTemperature :: Coercion (Temperature u) Double` for coercing containers. Unlike the data constructor, that's *local* unsafety, which is easier to audit.
Comment: @amalloy Fixed point still has the same problem with 'wonky' operations like `1/3 * 3 /= 1`; If you don't care about that you might as well use floating point. If you do the only real way around it is to use rational.
Comment: Floating point probably makes more sense than fixed point, because small changes in temperature have a bigger impact when the temperature is low. Near absolute zero, tiny fractions of a degree have a huge impact. Around room temperature, a quarter of a degree this way or that probably won't matter much. At stellar temperatures, I don't imagine a few dozen degrees would matter too much. On the flip side, I would guess that `Double` can express temperatures far higher than anything that happens in the universe.
Comment: I am still trying to understand your post, hence  why I haven't closed it yet.
Answer: This is an adaptation of @Cubic's excellent answer, but: you don't need fancy data kinds to do this.
{-# LANGUAGE ScopedTypeVariables, TypeApplications #-}

import Data.Proxy

newtype Temperature u = Temperature Double deriving Eq

class TemperatureUnit u where
  label :: Proxy u -> String
  toKelvin :: Temperature u -> Double
  fromKelvin :: Double -> Temperature u

instance TemperatureUnit u => Show (Temperature u) where
  show (Temperature t) = show t ++ " " ++ label (Proxy @u)

convertTemperature :: forall u1 u2. (TemperatureUnit u1, TemperatureUnit u2) => Temperature u1 -> Temperature u2
convertTemperature = fromKelvin . toKelvin

data Fahrenheit
data Celsius
data Kelvin

instance TemperatureUnit Fahrenheit where
  label _ = "°F"
  toKelvin (Temperature t) = (t - 32) * 5/9 + 273.15
  fromKelvin k = Temperature $ (k - 273.15) * 9/5 + 32

instance TemperatureUnit Celsius where
  label _ = "°C"
  toKelvin (Temperature t) = t + 273.15
  fromKelvin k = Temperature $ k - 273.15

instance TemperatureUnit Kelvin where
  label _ = "K"
  toKelvin (Temperature t) = t
  fromKelvin k = Temperature k

Try it online!

The advantage of the data kinds approach in this case, as far as I know, is that if you need that TemperatureUnit data type for some other purpose, you can reuse it instead of also defining data Fahrenheit etc. types as I did here. It also constrains the possible temperature types to what you define in the TemperatureUnit type, which could be good or bad for you. It's good that you get the extra type checking that you can't have a TemperatureUnit Bool, for example, but that sort of mistake would very likely be caught by the compiler elsewhere, albeit perhaps with a less clear error. And if you're exporting this functionality, you may want an open world of temperature types so that downstream modules can add their own.
So if you don't already have a TemperatureUnit type in use elsewhere, IMO not using data kinds is simpler and more flexible.
Comment: (If you use `AllowAmbiguousTypes` as @Cubic did, you don't need these `Proxy` shenanigans at all, but switching off the ambiguity check is a little more scary than just enabling type application syntax, and I wanted to showcase a solution with a minimum of scary extensions.)
Comment: FWIW `AllowAmbiguousTypes` sounds scary but I don't recall ever having a case where I had an ambiguous type by accident
Comment: Your TIO link links to my answer
Comment: so with `TypeApplications` you can pass types as arguments?  and why do that by using `Proxy`? Sorry, I am trying to understand what is gong on in your post.
Comment: Look at `label`. In theory, `label` shouldn't need any arguments at all—it's a constant for each instance of `TemperatureUnit`. However, if `label` just had type `String`, then the type parameter `u` wouldn't be involved at all, which leaves ambiguous exactly which `TemperatureUnit` instance you want to pull a `label` from. `Proxy` is the standard hack to deal with this problem; a `Proxy` is basically the same as `()`, but with a type parameter that is just along for the ride, the same way `u` is in `Temperature`. So far, none of this requires `TypeApplications`. (cont.)
Comment: But now, to actually invoke `label`, you need a `Proxy` of the appropriate type. You can write `label (Proxy :: Proxy u)` if you want, and that works just fine. With the `TypeApplications` extension, you can write that as `label (Proxy @u)`. There's nothing special about `Proxy` in this respect; it's just a polymorphic value being specialized to a particular type, and type applications give you a cleaner way to do so.
Comment: @user11228628 how would you  rewrite the instances so that  `fromKelvin` also takes a `Temperature _` and `convertTemperature` still works?
Comment: @user11228628 NVM I figured it out :)

Haskell: Reducing Boilerplate


There are N fish in a pond

Question: There are N fish in a pond. You use a net to catch them, and each fish is equally likely to be caught.
One day, you catch 40 fish, tag each of them, and then throw them back. The next day, you catch 30 fish in the net and note that 6 have tags.
What is your best guess of the value of N?
I think this relates to the capture - recapture problem where N= total population; n=first sample, k=tagged items, m=second sample.
$\frac{\binom{n}{k}\binom{N-n}{m-k}}{\binom{N}{m}}$
but I honestly do not know how to proceed.
Comment: The pertinent equation for the best-guess value is simpler: It's just $6/30=40/N$. (Of course this only works because the result turns out to be an integer.)
Comment: It may depend on what counts as "best".
Comment: https://math.stackexchange.com/q/3206178/321264, https://math.stackexchange.com/q/3683583/321264, https://math.stackexchange.com/q/3210550/321264, https://math.stackexchange.com/q/3809554/321264
Answer: As you said, we need to use the capture-recapture formula, but this does not involve combinatorics. Here is the formula below: $$N=\frac{1st\:sample\:\times\:2nd\:sample}{2nd\:sample\:previously\:marked}$$ So in this case, $N =(40×30)/6=1200/6=200 $
Comment: What would you do if the calculated value was not an integer, e.g., if the first sample had been $39$ fish caught instead of $40$?
Comment: @BarryCipra Then you would round the calculated value up.
Comment: Why up and not down?
Answer: Keeping it simple:

on Day 1 proportion of fish in pond that are tagged $=\frac{40}{N}$

on Day 2 proportion of fish in sample that are tagged $=\frac{6}{30}$

so solve $\frac{40}{N}$ = $\frac{6}{30}$ to get N = 200
Comment: setting sample ratio equal to population ratio. Then that would be the "most likely" population value?  What kind of distribution therefrom, if each fish has equal likelihood (i.e. uniform confidence?)? binomial?
Answer: Define a function $f:\mathbb{Z}_{\geq 64}\rightarrow [0,1]$ by $f(n)=\frac{{40 \choose 6}{n-40 \choose 24}}{{n \choose 30}}$. Then $f(n)=P(X=6|N=n)$ where $X$ is the number of tagged fish in your second sample of $30$ fish. I would use the value of $n$ that maximizes $f(n)$ as my estimate for $N$.
Comment: That maximum likelihood estimator seems to give $199$ or $200$, similar to MathGeek's natural estimator
Comment: How can a find such value N with a simple calculator?
Comment: @Marco It may be easier to find the max of $\ln(f(x))$ on Desmos.

There are N fish in a pond


Inverse Laplace transform $\frac{s^{2}}{(s^{2} + 100)^{2}}$

Question: How to find inverse Laplace transform of $F(s) = \frac{s^{2}}{(s^{2} + 100)^{2}}$? I do not ask for a solution, I simply can not understand which method I can use to solve it. I tried partial fraction, but they do not work, I tried convolutions, but it seems impossible ...
Answer: (This should probably be a comment, but I my reputation makes me untrustworthy)
Partial fractions with complex factors ought to work, i.e. $(s^2+100)^2=(s+10i)^2(s-10i)^2$.


Make either of 2 fields required but not both?

Question: Ive added a file field to comments so users can upload a file in a comment as well as post text. How can I make it so one of the 2 fields is required to be filled in to create a comment (so you cant submit blank comments), but without making both fileds required?
Answer: Check this thread and Conditional fields module.
If you're using D7 - the principle is the same as in the thread I linked. Basically you need to attach the name of your processing function to the validation array and there perform the checks.


Correct way to use hook_menu_alter

Question: What is the correct way of using hook_menu_later.
I did this in my module to redirect (see also redirect in hook_init makes body text disappear):
function MYMODULE_menu_alter(&$items) {
  $items['node/%node']['page callback'] = 'citykidz_node_page_view';
}

function MYMODULE_node_page_view($node) {
  if ($node->type == 'event') {
    if ($items = field_get_items('node', $node, 'field_event_date')) {
      $date = date_create($items[0]['value']);
      drupal_goto('news/events/day/' . date_format($date, 'Y-m-d'));
    }
  }
  else {
    return node_page_view($node);
  }
}

And this got me wondering: what if other modules are also overriding hook_menu_alter and setting their own page callbacks on 'node/%node'? Or even on access or whatever. This is the standard Drupal way of doing things, but surely it will lead to problems (that can be hard to detect). I see there is a module Chain Menu Access API that addresses this. Has anyone had success using this module? And has this issue been addressed in Drupal 8?
Answer: If two modules are altering the same menu item / form / page etc, last one will "win".
If you need some control over this, and are expecting collisions, you need to implement hook_module_implements_alter:

This hook is invoked during module_implements(). A module may implement this hook in order to reorder the implementing modules, which are otherwise ordered by the module's system weight.

Do your best to have your hook fire last, and if menu item you are altering does not look like the one you are expecting (it was altered already), write a warning message to watchdog.
Comment: That is excellent advice, especially in the case of a page callback where I want my module's code to run. But could it be possible that correct execution of the code depends on multiple module's code to run, especially in the case of the 'access callback'?
Comment: @J.Reynolds Access callbacks are different, they are not altering single object, they are just adding "yes / no / no change" entries to access array. They are *designed* to be used by many modules simultaneously, so should not create any problems.
Comment: Other modules can also execute `hook_module_implements_alter()` and put their `hook_menu_alter` on top ;)
Comment: @milkovsky yes, they can. As usual, simple solution is not to install two conflicting software (here: modules) on one system.
Comment: After thinking about it some more I am finding myself unconvinced. What if I install Revisioning module and my own module. My module runs later than Revisioning. I implement hook_menu_alter for nodes at revision edit path (node/%/revision/%/edit I think). I only want to allow person to edit revision if he has certain userpoints. Must I now re-implement Revisioning's complex set of permissions also? I must return either TRUE or FALSE. Hence I will have to chain onto Revisioning's own access calback, and if it return TRUE then do I do my own module's check on top of that.
Comment: @J.Reynolds In that case you can call revisioning's callback from your callback if needed, you know.
Comment: The way I understand it Revisioning's own alter hooks will also run. And if I implement hook_menu_alter on the page callback, then I explicitly want to exclude Revisioning's page callback in favour of mine. But I still want Revisioning's access callback to run in addition to mine, else I will have to re-implement it and I break modularity. To me this seems like an unresolved cross cutting concern.
Comment: @Molot I agree with you, thanks for clearing that up. I will investigate Chain Menu Access API and try and use that.
Comment: @J.Reynolds This site is not a forum. If you want to ask more specific question, ask it as a question. If you want to continue discussion here, I will not participate.
Comment: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/16275/discussion-between-j-reynolds-and-molot).

Correct way to use hook_menu_alter


Cannot read properties of undefined (reading 'merge') - React RTK

Question: I am trying to have all the slices in store so my app can access it, I saw similar issues on stackoverflow, they are suggesting to use a unique "reducerPath" name. But when I changed that I got another error caller

Warning: Middleware for RTK-Query API at reducerPath "apifb" has not
been added to the store.

One more doubt, in store.js if there are more than 3 slices please suggest me a better way to concat them for middleware.
FacebookSlice
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { api } from "../../config/constants";

export const facebookSlice = createApi({
    reducerPath: 'api',
    baseQuery: fetchBaseQuery({
        baseUrl: api.FACEBOOK_GRAPH_URL,
    }),
    endpoints: (builder) => ({
        getPagePhotos: builder.query({
            query: () => ({
                url: api.FB_GET_PAGE_PHOTOS_URL,
                method: 'GET'
            }),
        })
    })
})
export const {
    useGetPagePhotosQuery
} = facebookSlice

serviceSlice.js
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { api } from "../../config/constants";

export const serviceSlice = createApi({
    reducerPath: 'api',
    baseQuery: fetchBaseQuery({
        baseUrl: api.API_BASE_URL,
    }),
    endpoints: (builder) => ({
        getServices: builder.query({
            query: () => ({
                url: '/',
                method: 'GET'
            }),
        })
    })
})

export const {
    useGetServicesQuery
} = serviceSlice

bookingSlice.js
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { api } from "../../config/constants";

export const bookingSlice = createApi({
    reducerPath: 'api',
    baseQuery: fetchBaseQuery({
        baseUrl: api.API_BASE_URL,
    }),
    endpoints: (builder) => ({
        getBookings: builder.query({
            query: () => ({
                url: '/bookings',
                method: 'GET'
            }),
        }),
        createBooking: builder.mutation({
            query: (booking) => ({
                url: '/bookings/create',
                method: 'POST',
                headers:{},
                body: booking
            }),
        })
    })
})

export const {
    useGetBookingsQuery,
    useCreateBookingMutation
} = bookingSlice

store.js
import { configureStore } from '@reduxjs/toolkit';
import { facebookSlice } from './features/api/facebookSlice';
import { bookingSlice } from './features/api/bookingSlice';
import { serviceSlice } from './features/api/serviceSlice';

const store = configureStore({
    reducer: {
        [facebookSlice.reducerPath]: facebookSlice.reducer,
        [bookingSlice.reducerPath]: bookingSlice.reducer,
        [serviceSlice.reducerPath]: serviceSlice.reducer
    },
    middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().concat([bookingSlice.middleware, serviceSlice.middleware])
});

export default store;

index.js
import React, { Children } from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import 'semantic-ui-css/semantic.min.css'
import App from './App';
import reportWebVitals from './reportWebVitals';
import 'bootstrap/dist/css/bootstrap.min.css';
import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import Layout from './layout';
import Home from './components/home/home'
import Booking from './components/booking/booking'
import { ApiProvider } from '@reduxjs/toolkit/dist/query/react';
import { serviceSlice } from './features/api/serviceSlice';
import { Hallway } from './components/hallway/hallway';
import { Provider } from 'react-redux';
import  store  from './store';

const router = createBrowserRouter([
    {
        path: '/',
        element: <Layout />,
        children: [
            {
                path: "",
                element: <Home />
            },
            {
                path: "booking",
                element: <Booking />
            },
            {
                path: "hallway",
                element: <Hallway />
            }
        ]
    }
])

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    <React.StrictMode>
        <Provider store={store}>
            <RouterProvider router={router} />
        </Provider>
    </React.StrictMode>
);
reportWebVitals();

Answer: Issues

You have multiple API slices all using the default reducerPath value, e.g. "api" and this creates a conflict.
See reducerPath.

The reducerPath is a unique key that your service will be mounted
to in your store. If you call createApi more than once in your
application, you will need to provide a unique value each time.
Defaults to 'api'.

You are passing an array to the getDefaultMiddleware function instead of just passing the middlewares directly.

const store = configureStore({
  reducer: {
    [facebookSlice.reducerPath]: facebookSlice.reducer, // api: facebookSlice.reducer
    [bookingSlice.reducerPath]: bookingSlice.reducer,   // api: bookingSlice.reducer
    [serviceSlice.reducerPath]: serviceSlice.reducer    // api: serviceSlice.reducer
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware()
      .concat([ // <-- passing array, not middleware
        bookingSlice.middleware,
        serviceSlice.middleware
      ])
});

Solution
Give each API slice a unique reducer path value.
export const facebookSlice = createApi({
  reducerPath: 'facebookApi', // <-- unique among API slices
  baseQuery: fetchBaseQuery({
    baseUrl: api.FACEBOOK_GRAPH_URL,
  }),
  endpoints: (builder) => ({
    getPagePhotos: builder.query({
      query: () => ({
        url: api.FB_GET_PAGE_PHOTOS_URL,
        method: 'GET'
      }),
    })
  })
});

export const serviceSlice = createApi({
  reducerPath: 'serviceApi', // <-- unique among API slices
  baseQuery: fetchBaseQuery({
    baseUrl: api.API_BASE_URL,
  }),
  endpoints: (builder) => ({
    getServices: builder.query({
      query: () => ({
        url: '/',
        method: 'GET'
      }),
    })
  })
});

export const bookingSlice = createApi({
  reducerPath: 'bookingApi', // <-- unique among API slices
  baseQuery: fetchBaseQuery({
    baseUrl: api.API_BASE_URL,
  }),
  endpoints: (builder) => ({
    getBookings: builder.query({
      query: () => ({
        url: '/bookings',
        method: 'GET'
      }),
    }),
    createBooking: builder.mutation({
      query: (booking) => ({
        url: '/bookings/create',
        method: 'POST',
        headers: {},
        body: booking
      }),
    })
  })
});

Pass the middlewares directly, .concat handles "merging" them.
const store = configureStore({
  reducer: {
    [facebookSlice.reducerPath]: facebookSlice.reducer,
    [bookingSlice.reducerPath]: bookingSlice.reducer,
    [serviceSlice.reducerPath]: serviceSlice.reducer
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware()
      .concat(
        facebookSlice.middleware,
        bookingSlice.middleware,
        serviceSlice.middleware
      )
});


Passing Parameters from Mod_rewrite to PHP with a front end comprising of just HTML & JS

Question: I have a web application, the front end is HTML & JavaScript/jQuery and The back is PHP.
It was using ugly URL's like so, host/app/Page.php?userid=1.... Ugly!
So with a little MOD_REWRITE it will now serve the urls as url/page/1/
RewriteRule    ^page/([0-9]+)/?$    host/app/page.php?userid=$1    [NC,L]

Next it originally did simple jQuery GET request to the PHP files to get the dynamic data.
var params = A JS function to get all paramters after the ? in URL
var userid = params.id;

$.getJSON('host/app/page_code.php',{'userid':userid}, function(data) {
    var blah = data.blah;
});

Now after using MOD_REWRITE I now have no nice way of getting the front end to the parameters, only in a page/1 format. Looping though and getting each parameter between the slashes just doesn't feel like the right way to do it.
So I hit Google and it seems that sending the parameters from MOD_REWRITE seems like the way to do it. So my questions are, how is this possible, how is it done?
How do I get the parameters from the URL with MOD_REWRITE, send those to the PHP back-end, then return a response to the front end with JSON?
edit: The problem I have is I need to pass Parameters from JS to a PHP back end then get a response. I originally got these parameters from the URL with JS, then passed them into the GET request, I would then get a response from the PHP depending on the Parameter sent. However now I have used mod_rewrite on the URL I can not see the parameters on the front end to catch in the traditional ?a=b&c=d format. it now looks like /b/d To get these in JS I can only do this by looping through and getting each parameter between the slashes. Doesn't seem right to do it like this. I need to actually some how get the parameters to the PHP code in a separate page from the front end. Then pass it back to the front end with the response from the PHP code so I have lets say the users id. I don't want any php in the front end file as to keep it all separated. There must be a way to get the parameters from the url to the PHP code in a different file, then send back a response to the front end HTML&JS with the usersid. I'm new to this whole MOD_REWRITE for clean urls, and it's confusing the hell out of me!
Comment: @imsiso Wont that just pass it to the front end page though? I need it to go from mod_rewrite, to the back-end PHP then to the front end in JSON.
Comment: Did an edit with more info, hope that makes more sense.
Answer: use a post method with http://api.jquery.com/serialize/
Example from jquery.
$( "form" ).on( "submit", function( event ) {
  event.preventDefault();
  console.log( $(this).serialize() );
});

An idea to pass the data in a PHP sense.
data = $( "form" ).serialize();
$.post("serverside.php", data, function(returnData)
{
    $("divid").html(returndata)
}

Answer: You could write a reverse router with javascript in which given a URL, it returns the parameters.
Example (really bad one, just to give you an idea) on jsfiddle
Drawbacks: you will have to copy the routings from .htacces file to Javascript. Everytime something changes in that file, you will have to change in Javascript too.
Edit:
Another approaches

Do all the routing processing in PHP, not even using .htaccess and then retrieve data for routing in JSON format. You'd access this from an Ajax request

Example:
jQuery.ajax({
    url: 'reverse_routing.php',
    type: 'json',
    data: {
        url: window.location.pathname
    },
    function(params) {
       // Do your stuff here...
    }
});


Interval censored Cox proportional hazards model in R

Question: Given interval censored survival times, how do I perform an interval censored Cox PH model in R? An rseek search turns up the package intcox, which no longer exists in the R repository. I'm almost positive the coxph function in the survival package cannot handle interval censored survival data.
Also, I don't want to impute the data and then use the coxph function. This method underestimates the standard errors of the coefficients because you are ignoring the uncertainty of the interval censoring.
Comment: The [Survival Analysis CRAN Task View](http://cran.r-project.org/web/views/Survival.html) summarizes available packages for survival analysis, including a number with support for interval censoring.
Comment: You can still install the `intcox` package even if it's not on `CRAN` using the normal `install.packages("intcox")`.
Comment: It's possible, but I don't know. I just used the Berlin CRAN to do it about 10 minutes ago (R version 2.15.1).
Comment: As of 21 Dec 2015, I was able to `install.packages("intcox")` without any particular trouble (R-devel, but any modern R should work)
Comment: Hmmm... I was not able to do that. Could the mirror selection affect the download?
Answer: As stated above, you can use the survreg function. A note though: this is not strictly a Cox PH model, but rather location-scale models. Using the default log-transformation, this is the aft model. In the case of the exponential distribution, the proportional hazards and aft model are equivalent, so if distribution is set to exponential, this is a proportional hazards model with an exponential baseline. Likewise, if a baseline Weibull distribution aft model is used, the parameter estimates are just a linear transformation of those used in the proportional hazards model
 with Weibull baseline distribution. But in general, survreg does not fit a Cox PH model.
If a semi-parametric model is desired, as found implemented in intcox, a word of caution: there are several issues with the current version of intcox (algorithm typically prematurely terminates significantly far from the MLE, fails outright with uncensored observations, no standard errors automatically presented).
A new alternative that you could use is the package "icenReg".
Admission of bias: this is the author of icenReg.
Comment: Welcome to our site!  We're glad to have you and your fine contributions.
Comment: @Munichong: interesting! Unfortunately, I don't think this method will work. In particular, `ic_sp` needs to estimate the baseline survival distribution (unlike in right censored case), which has as many parameters as unique times in your data. This creates a problem for mini-batching; with continuous times, the baseline steps will not line up batch to batch.
Comment: Something like a parametric approach, in which the baseline distribution is parameterized by 2 fixed parameters, would likely work. Unfortunately, in `icenReg`, all the derivatives are computed at the C++ level and are not readily accessible in R.
Comment: @Cliff AB What specific semi-parametric method do you use in the ic_sp function? Do you have a paper or tutorial about the method?
Comment: @Munichong: the full paper can be found [here](https://www.jstatsoft.org/article/view/v081i12/v81i12.pdf). Alternatively, the package's vignette gives a quick introduction to the models as well; see [here](https://cran.r-project.org/web/packages/icenReg/vignettes/icenReg.pdf)
Comment: @CliffAB Since my data is too large to fit in the memory, I want to modify ic_sp in a stochastic way: Feed a mini-batch to ic_sp and set maxIter = 1, get the gradients and update betas iteratively. Do you know How I can access the gradients from the ic_sp function?
Answer: To do interval censored analysis in R, you must create a Surv object, and then use survfit(). If you have more than a variable, the intcox package solves the problem.


Bayesian inference and testable implications

Question: I have one question regarding testable implications of a model and Bayesian inference. My main doubt is how to exploit testable implications to reject a model.
Here is a simple example.
Suppose my model is that I have an iid sample from two gaussians with means $\mu_1$ and $\mu_2$ (and known variance) and I impose in the model the restriction that $\mu_1 = c \mu_2$ where  $c$ is a known constant.  Note this model imposes restrictions on the observed data---the mean of the two samples can't be more than $c$ times apart, barring, of course, sampling error.
Now suppose the data is discrepant with the posited model. Is there a principled way in Bayesian inference to reject this model given discrepant data?
PS: The generic comments saying that we could use posterior predictive checks, bayes factors and what not are ok, but not very useful in practice. Please also show how you would actually solve this toy problem.
Edit for the bounty: I will give the bounty to an answer that compares the three current suggestions (or more) both theoretically and more importantly with numerical examples of the toy problem. The three suggestions are: (i) posterior predictive checks; (ii) bayes factors; (iii) credible intervals (with or without ROPE).
For people potentially interested in answering: it would be helpful to actually perform a posterior predictive check and the hypothesis test or whatever you choose in your answer. The answer that does this and compares approaches will get the bounty.
Comment: Can you clarify the sentence "I impose in the model the restriction that $\mu_1=c\mu_2$ [...] the mean of the to samples can't be more than $c$ times apart".  This seems to imply $\mu_1<c\mu_2$.  Also, is $c$ known outright or do you place a prior on it?
Comment: One does not simply reject a model. The question assumes it makes sense for Bayesians to reproduce frequentist artifacts. At best you will end up with a posterior distribution over models. The most Bayesian thing to do is hang on to that full posterior distribution, not apply some arbitrary procedure to it so that one hypothesis is 'rejected' and the other (wink, wink) 'not rejected'.
Comment: @DemetriPananos It means that int my model the restriction $\mu_1 = c\mu_2$ holds. In the real world anything can hold.
Comment: @conjectures Go ahead then and write down your answer of how you should solve this toy problem.
Comment: Unfortunately my response does not fit into the framework of the question, so undoubtedly I will just get flak for doing so. Good luck.
Comment: @conjectures your response seems exactly what Demetri proposed.
Comment: One method that comes to mind is the Bayes factor, though it is generally not recommended.
Answer: There are only two "principled" ways you can get out of your posited model that operate within the framework of the Bayesian paradigm.  Once is to initially set a broader class of models, and give some non-zero prior probability for the alternative models in that class (i.e., have a prior probability less than one for your posited model class).  The other is to observe some evidence that has zero density under all distributions in the posited model class, which then allows you to update to any belief you want (see discussion here).
If you have assigned a prior probability of one to a class of models, and you never observe evidence that is inconsistent with those models, you can never "escape" that set of models within the Bayesian paradigm.  Note that this is by design --- if you assign a prior probability of one to a set of models, you are saying that any alternative class of models has zero probability.  In short, you are choosing to stick with your posited class of models no matter how strongly the evidence turns against them, so long as it is not inconsistent with those models.  If you would like to have a principled "escape route" operating within the Bayesian paradigm, you will need to posit some broader class of alternative models and give it a non-zero prior probability.  You could certainly give the alternative models a very low prior probability, so that they only become important a posteriori when the main model class starts to be (probabilistically) falsified by the data.

Implementation in your problem: In the problem you raise, it would be usual to handle this by framing the problem as a Bayesian hypothesis test, with hypotheses:
$$H_0: \mu_1 = c \mu_2 \quad \quad \quad H_A: \mu_1 \neq c \mu_2.$$
For example, under $H_0$ you could posit an overall model like this:
$$\begin{aligned}
X_{11}, X_{12}, ... , X_{1n} | \mu_2,\sigma_1^2,\sigma_2^2 &\sim \text{N}(c \mu_2,\sigma_1^2), \\[6pt]
X_{21}, X_{22}, ... , X_{2n} | \mu_2,\sigma_1^2,\sigma_2^2 &\sim \text{N}(\mu_2,\sigma_2^2), \\[6pt]
\mu_2 &\sim \text{N}(0, \eta^2), \\[6pt]
\sigma_1^2 &\sim \text{Ga}(\alpha, \beta), \\[6pt]
\sigma_2^2 &\sim \text{Ga}(\alpha, \beta), \\[6pt]
\end{aligned}$$
and under $H_A$ you could posit an overall model like this:
$$\begin{aligned}
X_{11}, X_{12}, ... , X_{1n} | \mu_1,\mu_2,\sigma_1^2,\sigma_2^2 &\sim \text{N}(\mu_1,\sigma_1^2), \\[6pt]
X_{21}, X_{22}, ... , X_{2n} | \mu_1,\mu_2,\sigma_1^2,\sigma_2^2 &\sim \text{N}(\mu_2,\sigma_2^2), \\[6pt]
\mu_1 &\sim \text{N}(0, \eta^2), \\[6pt]
\mu_2 &\sim \text{N}(0, \eta^2), \\[6pt]
\sigma_1^2 &\sim \text{Ga}(\alpha, \beta), \\[6pt]
\sigma_2^2 &\sim \text{Ga}(\alpha, \beta). \\[6pt]
\end{aligned}$$
You can obtain the Bayes' factor for the above hypothesis test and use this to see how you update prior probabilities for the hypotheses to posterior probabilities.  If the data makes $H_0$ highly implausible, this will manifest in a lower posterior probability for $H_0$.  Given some prior probability $\lambda = \mathbb{P}(H_0)$ for your posited subclass of models, you will be able to update this to a posterior probability.
Comment: @BayesianNewbie: You are certainly free to award the bounty to whomever you wish, on whatever basis you wish.  I will just note that it is considered somewhat "bad form" to move the goalposts with edits requiring substantially more work after a question has already been asked and answered.  (Since you are new to the site, you probably didn't know that, so please don't take this as a criticism.)  In any case, numerical implementation of the model is a much larger project, that is outside the scope of my answer.  Cheers, Ben.
Comment: In a basic sense, *every* assumption restricts things, so you just need to list your assumptions and then consider if you want to test these, and if so how.  One of the difficulties of the Bayesian framework (as opposed to classical hypothesis testing) is that you have to specify your alternative as a model, rather than just leaving it as non-compliance with the null.  Nevertheless, if you can identify the assumptions you want to test, and the alternative, you can usually set up an alternative model.
Comment: Ben I gave the bounty to you since you were going to get it anyway, but this answer is too generic... I'm going to ask several more specific questions next to complement this.
Comment: Can you numerically apply your proposal in my simple two gaussians example?
Comment: I'm asking because both your and Tim's answer are very generic. It should be easy to demonstrate the two approaches in this example: the posterior predictive check versus having a hyper prior or something (is that what you are proposing, a hyper prior, one that selects the constrained model $M_1$ with X% chance versus the unconstrained model $M_2$ with 1-X% chance?).
Comment: Thanks for the update (+1), one problem I see with this approach is that I may not know what the testable implications of my model are. Here we know what it is, so we can formalize the problem clearly as a test of population means. However in a complex model one may not know whether (and which) assumptions are in fact restricting the data.
Comment: Ben, there are assumptions that do not impose any restrictions on the observed data when you are dealing with latent variable models. In this example I assumed we knew the assumptions imposed the restriction $\mu_1 = c\mu_2$ so we have a simpler easier problem to deal with, but in practice we do not know whether our model has testable implications.
Comment: One thing that is not clear to me is the difference between this approach you proposed, and the approach of estimating $c$ and checking whether the 95% credible interval includes 1, as suggested by Demetri. Why is your approach better than that approach?
Comment: By the way I have two other questions without answer, you seem very knowledgeable about bayesian stats and could maybe give them a shot: https://stats.stackexchange.com/questions/447353/bayesian-inference-on-the-maximum-of-a-function and https://stats.stackexchange.com/questions/454060/bayesian-formulation-of-best-subset-regression
Comment: Ben I would be very happy to give you the bounty if you just implemented numerically your answer in simulated data and compared it to posterior predictive checks and the credible interval approach.
Comment: https://stats.stackexchange.com/questions/468189/how-do-i-perform-an-actual-posterior-predictive-check-in-this-model
Answer: Prior predictive and posterior predictive checks may be helpful in here. In both cases you sample the predictions from the model (the "fake data"), in first case from the prior, in the second case from the posterior distribution, and then compare the distributions of the fake data, with the distribution of the observed data. Prior predictive checks are aimed to diagnosing the prior-data conflict, i.e. the model a priori does not make reasonable predictions that cover the possible range of the values observed in the data, it is ill-defined a priori. In posterior predictive checks you sample from the predictions after estimating the parameters (i.e. from posterior), so you check if the predictions that the model does fit the observed data. In both cases, there are many ways of doing this, depending on particular problem, ranging form eyeballing the histograms, density plots, scatter plots, summary statistics etc, up to defining more formal tests (data falls within the per-specified interval, hypothesis tests to compare the distributions, etc). This is a routine practice in Bayesian modeling.
If I understand you correctly, the model that you use as example assumes that your data $X$ comes from a mixture of two Gaussians, with unknown means $\mu_1, \mu_2$ and known variances $\sigma^2_1, \sigma^2_2$, and known constraint $c$, such that $\mu_2 = c\mu_1$. Simple way to test this model is to treat $c$ as free parameter, to be estimated. You know what $c$ should be, so you can come up with a strong, informative prior for it. In such case, it would surprise you if estimated $c$ differed from the true value. If I understand you correctly, that's the property of the model that you want to test. To test the validity of this assumption, you could take samples from the posterior distribution $\hat c_i$, and compare them to the true value of $c$, e.g. you would accept the model if at least in in $100\alpha\%$ cases, the predicted values for $c$ would be within the $\pm \varepsilon$ range from the truth
$$
\alpha \le 1/n \sum_{i=1}^n \mathbf{1}(|c - \hat c_i| < \varepsilon)
$$
This is not exactly a posterior predictive check, since we may argue if $c$ is data, or not, but it follows the spirit of the kind of checks you would make to test model validity.
Accidentally, Michael Betancourt has just published a lengthy Towards A Principled Bayesian Workflow tutorial, where among other things, he discusses importance of prior and posterior checks discussed above.
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/108191/discussion-on-answer-by-tim-bayesian-inference-and-testable-implications).
Comment: Please post any new comments in the dedicated chat room linked above.  All comments posted here will be deleted from now on.
Answer: I'm not a Bayesian expert and I'm happy to stand corrected, but to me the most straightforward & principled way to test this would be to define region of practical equivalence (ROPE) around c and then estimate how much posterior density falls inside this region.
For example, let's say that, based on theory and domain knowledge, you know that for all practical purposes, if c deviates from exactly 1 by less than 0.01 then it might as well be 1 (outside of simulation, c is never going to be exactly 1 anyway and so you will always reject point null hypothesis with enough data). Anyway, using the deviation of 0.01 you define a ROPE of 0.99 - 1.01. After that, you run your model, and estimate how much density falls inside the ROPE region. If the proportion of density $k$ that falls inside the rope is smaller than whatever you decide your alpha is, then you should feel comfortable rejecting your model, with $k$ confidence. See this vignette: https://easystats.github.io/bayestestR/articles/region_of_practical_equivalence.html
PS: You'll probably want a large tail effective sample (ESS) size for this kind of testing. This is because Monte Carlo samplers tend to explore the typical set & give increasingly less precise estimates towards the tails of the distribution, which is where your ROPE might be. So you'll want to run your sampler with a lot of iterations.
Comment: @innisfree you're doing a lot of commenting, which is great, but it seems you have a preferred way of answering this question.  I think you should post it to compare among the existing answers.
Comment: Adam, it doesn't ROPE doesn't matter that much here. You are just changing the constraint to $|\mu_1 - \mu_2| < c$. But the logic is the same.
Comment: Will this be equivalent to posterior odds for the hypotheses $H_0: c \in ROPE$ versus $H_1: c \not\in ROPE$ with prior for $p(H_0)=\int_{ROPE} p(c) dc$ etc
Comment: I more of less agree with the answer of @ben
Comment: @BayesianNewbie yeah I think you're right, if I understand you correctly, the only real difference between mine & Demetri's answer is that you quantify what proportion of the total density falls inside the ROPE around c, as opposed to just concluding that c falls outside of the 95% HDI. I don't think there's going to be much difference for symmetric, two-sided intervals, but I think that I can imagine a situation where c would fall inside the HDI but at the same time have < alpha proportion of density in the ROPE (marginal posterior with long tails).
Comment: E.g. if the marginal posterior for c was skewed with a fat tail, and the 95% HDI was extending from 0.2 - 3 with a mode around 0.6, you might still have < 5% of density inside ROPE. In plain words, it would mean that the value for c was most likely below 1, but it could extend to values up to 3, however, *simultaneously*, the probability that c would be exactly 1 (or close to exactly 1) would be very small.
Answer: EDIT: innisfree is right.  Bayes factors seem like a better approach than what I have provided here.  I'm leaving it up for posterity, but it isn't the right approach.
Because this problem really relies on a single assertion (namely, that $c$ has some value), we can simply estimate the following model
$$ y \sim \mathcal{N}(b_0 + b_1x, \sigma)$$
and determine the posterior probability that either $b_0/(b_0+b_1)<c$ or $b_0/(b_0+b_1)>c$.  Here is an example.  Say we had a hypothesis that $c=1$ and we know that the variance is 4 and that the intercept (or the mean of one populatio
n) is 2.  We can fit the following model in Stan
<code>stan_model = '
data{
  int n;
  vector[n] x;
  vector[n]y;
}
parameters{
  real b;
}
model{
  b~normal(0,1);
  y~normal(2+b*x, 2);
}

'
</code>
This will allow is to freely estimate the parameter $b1$ assuming we know $b_0$ and $\sigma$. After fitting the model with a standard normal prior on $b_1$, here is a histogram of the posterior

The model provides a 95% posterior credible interval for $c$ covering (0.465, 0.686).  We can be fairly certain that the value of $c$ is not 1.
Comment: Thanks, I don't think innisfree is correct, your answer is sensible, but I believe your modeling choice is not correct for this example. Here you should model both means.
Comment: Why couldn't you just estimate how much posterior density falls in region of practical equivalence (ROPE) around 1? Point null c = 1 is always going to be wrong.
Comment: @innisfree I don't understand your objection to this answer. Can you elaborate on how you would go about solving this question in my example on a separate answer?
Comment: You can't use credible regions to test a point null. The fact that $1$ lies outside a credible region tells nothing about the relative plausibility of $c=1$ to $c\neq 1$ or an error rate associated with any decision procedure associated with $c$.
Comment: On top those those two problems, the procedure depends on an ordering rule for the credible region, and worse still, isn't necessarily invariant under reparameterisations of the problem.


Figured bass: Am I on the right track?

Question:

I'm studying for the Grade 6 music theory exam with the ABRSM and am working through this exercise in preparation (2018 B, question 2)
Can anyone tell me if there are any errors I should correct? Beat 4 of bar 1 (e minor chord) seems slightly off to me.
Answer: If you're talking about part-writing errors in general, there are some parallel octaves between the tenor and alto lines happening.
In the third full measure, last beat, that a minor chord looks like it should be a C major chord, according to the exercise.
Comment: In other words, the figure _6_ has been realized with a _6/4_ chord.
Comment: I see no parallel octaves between tenor and alto
Answer: Beat 4 of m. 1 also seems odd to me, but your realization correctly follows the figures. Beat 3 to 4 has a IV chord moving to vi, which is a little unorthodox, and so much so that I wonder if there should be a "6" underneath beat 4 to make that a IV6. But let's assume it's correct!
Some other considerations:

Parallel octaves between soprano and tenor (A to B) in the opening two chords.
Parallel octaves between alto and bass (E to D) moving into the downbeat of m. 2.
Beat 2 of m. 3, with the doubled root and third, could be respelled to include a D in the alto, making the chord complete. But note that this creates a diminished fifth (F♯–C) moving to a perfect fifth (G–D) between the alto and tenor. This doesn't bother me, but some traditions don't allow the diminished fifth moving to a perfect fifth. But I might recommend moving the alto to D, because this will also smooth out the motion into beat 3 when the alto moves to E.
Beat 4 of m. 3 is in first inversion, so this should be a C-major chord (with E in the bass), not an A-minor chord.

Figured bass: Am I on the right track?


Calculation of a limit without Hopital or with Hopital

Question: I would like to calculate the following limit below.
How do you evaluate the limit?
$$\lim_{x\to\infty}{(1 + 2x)^\left(\frac{1}{2\ln x}\right)}$$
Comment: $$\large\lim_{\color{red}{n}\to\infty}{(1 + 2x)^\left(\frac{1}{2 \ln x}\right)}$$
Answer: Let $L = \lim_{x\to\infty} (1+2x)^{\frac 1{2\ln x}}$. Then,
$$\ln L = \ln\lim_{x\to\infty} (1+2x)^{\frac 1{2\ln x}} = \lim_{x\to\infty} \frac {\ln(1+2x)}{2\ln x}$$ and you can use L'Hospital.
Answer: We have $$\lim_{x \to \infty} (1+2x)^{\frac {1}{2\ln x}} = \lim_{x \to \infty} e^{\frac {\ln (1+2x)}{2 \ln x}} = e^{\frac {1}{2} \lim_{x \to \infty} \frac {\ln (1+2x)}{\ln x}} $$
Can you now continue using L'Hopital's rule? The answer is $\boxed {\sqrt {e}} $. Hope it helps.
Answer: With l'Hopital
Let $\displaystyle y=(1 + 2x)^{\frac{1}{2\ln x}}$
Then
$\displaystyle \ln y=\frac{\ln(1 + 2x)}{2\ln x}$
and
$$
\lim_{x\to\infty}\ln y=\lim_{x\to\infty}\frac{\ln(1 + 2x)}{2\ln x}=\lim_{x\to\infty}\frac{2x}{2(2x+1)}=\frac12
$$
then $y\to\sqrt{e}$.
Without l'Hopital
We know $\displaystyle\lim_{x\to\infty}\ln x=\infty$ and $\displaystyle 1+2x\sim 2x $ as $x\to\infty$, so
$$
\lim_{x\to\infty}(1 + 2x)^{\frac{1}{2\ln x}}=\lim_{x\to\infty}(2x)^{\frac{1}{2\ln x}}=\lim_{x\to\infty}y$$
$$\ln y=\frac{\ln(2x)}{2\ln x}=\frac{\ln2+\ln x}{2\ln x}=\frac12+\frac{\ln 2}{2}\lim_{x\to\infty}\frac{1}{\ln x}=\frac12
$$
and $y\to\sqrt{e}$ again.
Comment: I'm not sure you can just switch $\lim (1+2x) = \lim (2x)$ when these limits are not finite. Surely you couldn't switch, for example, $\lim(1+2x)$ and $\lim x^n$. More justification is needed, is what I'm trying to say. For example, $\lim\frac{\ln(1+2x)}{\ln(2x)} = 1$ would be useful.
Comment: @Ennar This is equivalence like $\sin x\sim x$ as $x\to0$ and we write $1+2x\sim2x$ as $x\to\infty$ because we can ignore 1 beside $\infty$, in the other words $1+\infty=\infty$ informal.
Comment: I know what it is, the question is whether OP does. It does need justification, and $\lim (1+2x) = \lim(2x)\implies \lim(1+2x)^{1/(2\ln x)} = \lim(2x)^{1/(2\ln x)}$ is not correct argument.


A square grid path problem proof

Question: Let n be a positive integer. Consider all possible ways of arranging n pairs of parentheses "(" and ")" in a row. We want to arrange them in a way that it is possible to pair up the left parentheses with the right parenthesis so that "(" is to the left of ")". Let An be the set of all such arrangements with n pairs of parentheses. There are 5 elements in A3, as shown below.
((())) (()()) (())() ()(()) ()()()
On an n × n grid, we consider paths travelling along the grid lines from the SW corner to the NE corner of the grid with the conditions that we only travel in the N or E direction, and we never travel above the x = y line. Let Bn be the set of all such paths in an n × n grid. There are 5 elements in B3
Now how do I prove this using a bijection(|An| = |Bn|)?
Comment: Hint: choose a path and write it out as a sequence of letters (N and E). How many such sequences can you make?
Comment: Are you sure ${2n \choose n}$ is correct? That seems to count the paths without the "below the diagonal" restriction. When $n=1$, for example, it says there are $2$ paths, but there should only be $1$. (I assume that by "$n \times n$ grid" you mean $n$ squares by $n$ squares, for $n+1$ grid points on each side.)
Comment: @GregoryJ.Puleo Yes you are right I was mistaken, I have edited the question. This is what I am trying to solve. Please have a look
Comment: @Théophile Thanks for the idea, how can I use this NE sequence of letters to find an bijection?
Comment: Just label any legal move on you grid using a string consists of alphabet $E$ and $N$ and then translate every $E$ to "(" and every $N$ to ")".  You should find the wiki page for [Catalan number](http://en.wikipedia.org/wiki/Catalan_number) relevant and useful.
Comment: @achillehui Yes I did that, but I am not sure how to formalize it
Comment: What you really need is a more precise/concrete definition of the predicate `"(" is to the left of ")"` for strings formed using "(" and ")". If you remove that restriction from $A_n$ and the constraint  `never travel above the x = y line` from $B_n$, there is already a bijection between the extended $A_n$ and $B_n$. If you can show under this bijection,  $A_n$ get mapped onto $B_n$, then you are done.
Answer: Let $a_0 = (0,0)$ be the SW corner and $a_{2n} = (n,n)$ the NW corner.  Suppose a path from $a_0$ to $a_{2n}$ does go above the diagonal.  Say the first time it does so is at $a_j = (k, k+1)$ (where we must have $j = 2k+1$).
Reflect the part of the path from $a_0$ to $a_j$ across the line $y=x+1$, and
you have a path (still consisting of N and E moves) from $(-1,1)$ to $(n,n)$.  Conversely, given any path from $(-1,1)$ to $(n,n)$, it must at some point hit the line $y=x+1$, and if you reflect the part of the path up to that point you get a path from $(0,0)$ to $(n,n)$ that goes above the diagonal.  We conclude that the number of paths from $a_0$ to $a_{2n}$ that go above the diagonal is the same as the number of all paths from $(-1,1)$ to $a_{2n}$, which is $2n \choose n+1$ (because there are $2n$ steps, of which $n+1$ are E and $n-1$ N).  The total number of paths from $a_0$ to $a_{2n}$ is $2n \choose n$.  So the number of paths that don't cross the diagonal is
${2n \choose n} - {2n \choose n+1}$.
Comment: Thanks for the great answer but can you tell me how to prove the required statement |An| = |Bn| using a bijection

A square grid path problem proof


Morphable field not working for Laravel DB::Select() statement

Question: I need to use DB::select() statement for a complex query I´m working on that is not returning any results. After some diggin I found out the reason was that the model_type field comparison was not working regardless wheather I use this sintaxis:
select *
from questions
where model_type = "App\Models\Product"

Or this sintaxis:
select *
from questions
where model_type = "App\\Models\\Product"

Even though the second sentence works perfectly on MySQL Workbench, it returns no results in Laravel.
The MySQL table field name is of course model_type and the values are hold as: App\Models\Product and all relationships work perfectly. It seems that there should be a way to specify to Laravel how to compare the values, but I haven´t found it.
Please note that I have to use DB::select() as oposed to Eloquent, since my actual query deals with MySQL MATCH() AGAINST() and some other complexities.
Any help would be welcome. Txs!
Comment: can you provide a [mre] with concrete example
Comment: @nbk yes, is the one I have described. Just setup any morpheable model in a table and try to select * from your_table where model_type = "App\\Models\\YourModel" using DB::select(). It works fine in MySQL, but doesn´t work on Laravel... at least not for me.
Answer: Just in case someone else runs into this predicament, I still don´t know why Eloquent is not able to resolve it´s model, but I did find this great article which solves this and other potential situations as well, by decoupling Laravel logic from the information stored in the database. https://josephsilber.com/posts/2018/07/02/eloquent-polymorphic-relations-morph-map#the-default-morph-type


How likely am I to be descended from a particular person born in the year 1300?

Question: In other words, based on the following, what is p?
In order to make this a math problem rather than anthropology or social science, and to simplify the problem, assume that mates are selected with equal probability across the population, except that siblings and first cousins never mate, and mates are always selected from the same generation.

$n_1$ -- initial population
$g$ -- the number generations.
$c$ -- the average number of children per couple. (If necessary for the answer, assume that every couple has exactly the same number of children.)
$z$ -- the percentage of people who have no children, and who are not considered part of a couple.
$n_2$ -- population at the final generation. (Either $n_2$ or $z$ should be given, and (I think) the other can be calculated.)
$p$ -- probability of someone in the final generation being a descendant of a particular person in the initial generation.

These variables can be changed, omitted, or added to, of course. I am assuming for simplicity that $c$ and $z$ do not change over time. I realize this will get a very rough estimate, but it's a starting point.
Part 2 (suggestion for further research):
How can you consider that mates are not selected with globally uniform probability? In reality, mates are more likely to be of the same geographical area, socio-economic background, race, and religious background. Without researching the actual probabilities for this, how would variables for these factors come into play? How important would this be?
Comment: To get an initial sense of the answers, consider the fraction $f$ of the population that is *not* related to a given ancestor by descent.  Initially $f = (n-1)/n$ for a population of $n$.  With random mixing, $f$ is *squared* after each generation.  In a starting population of $n=10^8$, say, this implies $f$ is almost surely $0$ after $32$ generations (about $600$ - $800$ years).
Comment: @John: Thanks for your edit. I believe the prevailing consensus (on this site and others) is that we not edit questions simply to add the `homework` tag. It is better for all involved to let the OP do that. You might be interested in [this meta thread](http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions/10812#10812) if you haven't already seen it.
Comment: I believe there is some academic research on the probability of a unique surname going extinct.  Although not identical to the problem posed, that might provide some interesting insight (but unfortunately I can't remember where it's from).  Oddly enough, I believe those studies led to some insight in the math behind the spread of infectious disease...
Comment: I am just curious. I am not a student and this is not anybody's homework. I was just joking about the extra credit, although I can see how it would imply homework.
Answer: Because this question is receiving answers that vary from astronomically small to almost 100%, I would like to offer a simulation to serve as a reference and inspiration for improved solutions.
I call these "flame plots."  Each one documents the dispersion of genetic material within a population as it reproduces in discrete generations.  The plots are arrays of thin vertical segments depicting people.  Each row represents a generation, with the starting one at the top.  The descendants of each generation are in the row immediately beneath it.
At the beginning, just one person in a population of size $n$ is marked and plots as red.  (It's hard to see, but they are always plotted at the right of the top row.)  Their direct descendants are likewise drawn in red; they will show up in completely random positions.  Other descendants are plotted as white.  Because the population sizes can vary from one generation to the next, a gray border at the right is used to fill empty space.
Here is an array of 20 independent simulation results.

The red genetic material eventually died out in nine of these simulations, leaving survivors in the remaining 11 (55%).  (In one scenario, the bottom left, it looks like the entire population eventually died out.)  Wherever there were survivors, though, almost all the population contained the red genetic material.  This provides evidence that the chance of a randomly selected individual from the last generation containing the red gene is about 50%.
The simulation works by randomly determining a survivorship and a mean birth rate at the beginning of each generation.  Survivorship is drawn from a Beta(6,2) distribution: it averages 75%.  This number reflects both mortality before adulthood and those people not having any children.  Birth rate is drawn from a Gamma(2.8, 1) distribution, so it averages 2.8.  The result is a brutal story of insufficient reproductive capacity to compensate for generally high mortality.  It represents an extremely pessimistic, worst-case model--but (as I have suggested in comments) the ability of the population to grow is not essential.  All that matters in each generation is the proportion of red within the population.
To model reproduction, the current population is thinned down to the survivors by taking a simple random sample of the desired size.  These survivors are randomly paired (any odd survivor left over after pairing doesn't get to reproduce).  Each pair produces a number of children drawn from a Poisson distribution whose mean is the generation's birth rate.  If either of the parents contains the red marker, all the children inherit it: this models the idea of direct descent through either parent.
This example starts with a population of 512 and runs the simulation for 11 generations (12 rows including the start).  Variations of this simulation starting with as few as $n=8$ and as many as $2^{14} = 16,384$ people, using different amounts of survivorship and birth rates, all exhibit similar characteristics: by the end of $\log_2(n)$ generations (nine in this case), there's about a 1/3 chance that all the red has died out, but if it hasn't, then the majority of the population is red.  Within two or three more generations, almost all the population is red and will remain red (or else the population will die out altogether).
A survivorship of 75% or less in a generation isn't fanciful, by the way.  In late 1347 rats infested with bubonic plague first made their way from Asia to Europe; during the next three years, somewhere between 10% and 50% of the European population died as a result.  The plague recurred almost once a generation for hundreds of years afterwards (but usually not with the same extreme mortality).

Code
The simulation was created with Mathematica 8:
<code>randomPairs[s_List] := Partition[s[[Ordering[RandomReal[{0, 1}, Length[s]]]]], 2];

next[s_List, survive_, nKids_] := Flatten[ConstantArray[Max[#],
   RandomVariate[PoissonDistribution[nKids]]] & /@
   randomPairs[RandomSample[s, Ceiling[survive Length[s]]]]]

Partition[Table[
   With[{n = 6}, ArrayPlot[NestList[next[#, RandomVariate[BetaDistribution[6, 2]],
        RandomVariate[GammaDistribution[3.2, 1]]] &,
        Join[ConstantArray[0, 2^n - 1], ConstantArray[1, 1]], n + 2],
     AspectRatio -> 2^(n/3)/(2 n),
     ColorRules -> {1 -> RGBColor[.6, .1, .1]},
     Background -> RGBColor[.9, .9, .9]]
    ], {i, 1, 20}
   ], 4] // TableForm
</code>
Comment: @xpda Mathematical solutions will provide insight into what matters and what doesn't.  For instance, they will show that you don't necessarily need to model huge populations.  They will also indicate the role played by variability, which is harder to handle analytically and comes to the fore in a simulation.
Comment: I think modeling like this may be the best approach. It is far simpler and more fun (for me) than the math, and it should make it much easier to introduce factors restricting mate selection. Do you have any recommendations, caveats, or other advice before I dive in on this?
Comment: @whuber Did you run the simulation in Mathematica? Would you mind posting code?
Comment: @Max The code is now up.  I apologize for the lack of comments.  If you run each of `randomPairs` and `next` on test data, their functions should become apparent.  Notice the use of `NestList` to iterate `next` in order to produce multiple generations.
Answer: What happens when you try counting ancestors?
You have 2 parents, 4 grandparents, 8 great grandparents, ... So if you go back $n$ generations then you have $2^n$ ancestors.  Let's assume an average generation length of $25$ years.  Then there have been about $28$ generations since 1300, which gives us about 268 million ancestors at that time.
This is the right ballpark, but there is something wrong with this calculation, because the population of Earth in 1300 did not mix uniformly, and we are ignoring intermarriage within your ancestral "tree", i.e. we are double counting some ancestors.
Still, I think, this can lead to a correct upper bound on the probability that randomly chosen person in 1300 is your ancestor by taking the ratio $2^{28}$ to the population in 1300
Comment: Very significant considering a lot of the population back then were rather isolated from each other, so there was far less opportunity to avoid intermarriages.
Comment: So let's assume that the OP to be from English descent and around 1300, England's population was over one million. (Let's say before the great famine). How would that change your analysis?
Comment: $2^{28}\approx 268$ *million*, not billion.  It's the right ballpark.
Comment: D'oh! Edited the answer.  The calculation still ignores intermarriage, but this might give a correct upper bound on the probability that a randomly chosen person in 1300 is your ancestor by taking the fraction: $2^{28} / 3$ or $4$ hundred million.
Answer: The further back you go, the more likely that you are related to a person that successfully passed along their genes that lived in that time. Of the 1/4 billion ancestors that you have that lived in 1300, many of them would show up hundreds (if not thousands, millions) of times in your family tree.  Genetic drift and the number of times we are directly related to someone are likely more relevant to the differences in our genetic code than who our ancestors were.
Answer: My updated short answer is:
$$ p > {(1-z)} \times {{{1} \over {n_1(1-z)}} \over {2}} = {2 \over n_1} $$
Answer explained:
Given a particular person today, it is certain that they are a descendant of at least 2 people in 1300.
When picking a particular person in 1300, there is (1-z) chance that person never reproduced, and the other term is for the number of 'parent couples', and the probability for the person to be related to this couple (1 / number of couples).
The (1-z) ends up cancelling out, leaving us with
$$ p > {2 \over n_1} $$
Now just for fun but not necessary for solving the probability question
Here is the population of any given generation k in the chain between then and today.
$$ n_{k+1} = {{n_k(1-z)\times c} \over 2} = {n_1(1-z)^kc^k \over 2^k}$$
 Lets plug in some numbers as an example. For assumptions, I use:
g = 28 (25-year generations between 1300 and 2011)
n = 360M (world population estimate in 1300 from wikipedia)
z = 0.2, c = 2.77=8 (not real data, but does end up with about 7B people in 2011)
Resulting in:
$$p > 2 / 360,000,000 = 5.56 \times 10^{-9}$$
or over one in 180M.
Thanks for reading, Erad
Comment: The answer given here holds for *each* member of the original population, no matter who they were.  Summing over all members gives an *upper bound* for the probability that we are descended today from *some* person in the year 1300 of $360,000,000 / (2.66 \times 10^{249}) \ll 1$, which is obviously way wrong (unless alien clones were introduced along the way...).
Comment: Hm, how come your probability is less than $1/n$=$1/360M\approx 10^{-9}$?
Comment: @Erad In your comment you appear to assume that all of today's population is descended from a tiny fraction of the world in 1300.  That's just not plausible.  However, suppose for the sake of argument--and to examine an extreme case--that everyone today is known to have descended solely from one couple, "Adam" and "Eve", alive in 1300.  Then the chance of descent is either 100% if Adam or Eve are the "particular person" of the question or else is 0%.  This chance, averaged over the population in 1300, is still about $10^{-8}$, far higher than you compute.
Comment: What is $c$? And what is $z$?
Comment: Based on the original question above:
c = the average number of children per couple, and
z = the percentage of people who have no children
Comment: oh sorry, as usual I did not read the question carefully :)
Comment: The reason this probability is lower than just 1 out of the population in 1300, is that 20% (z=.2) of this population did not have any offsprings. In order for someone today to be linked to a random person 710 years ago, his offsprings needed to 'survive' throughout these generations, and always be linked to the person today. This turns out to be a very small chance.
Comment: Is $n_1^g$ correct in your short answer? If you use g=2 and n=100, it seems to throw everything off and gets p=1/2770. (I didn't follow the part about the multiplication of P(x) over the different generations (g).)
Comment: I agree with Erad above. Quite an elegant solution to this problem, and your final population nearly matches the current population of Earth at ~7 billion! Very good question as well, xpda.
Answer: The probability is=1-z, every descendant in this problem is related to ancestors above. Whatever the initial rate of reproduction is (1-z) is your probability of being descendant from someone in the initial population.Only uncertain probability is what are the chances of being alive in final population.
I agree with Erad's answer, although I now think it responds to a question that was not asked - namely what is the probability that you are alive given certain known reproductive and population constraints on your fore-bearers.
Comment: @xpda That's a strange interpretation, because everybody either is, or is not, descended from any particular individual, as can be established through DNA testing.  I think the way many people may have been understanding your question is if we pick an arbitrary person "$A$" in the year 1300 and we select a *random* person alive today, what is the chance they are descended from $A$?  This is answered by estimating the proportion of today's population that is descended from $A$.  We could also take $A$ to be randomly selected.
Comment: The question is to find the probability of someone in the final generation being descended from *a particular person* in the initial generation. If $n_1$=360 million and $z$=.2, then the probability wouldn't be 1-$z$ at, for example, $g$=1.
Comment: Also, to clarify, the question is to find the probability of *a particular person* in the final generation being descended from *a particular person* in the initial generation.
Comment: @Wipa Descartes' *cogito, ergo sum* strongly suggests the probability I'm alive given *any* constraints on my forebears is 100% :-)
Comment: @whuber, you are correct. I believe we are talking about the same problem. The thing I wanted to clarify is that I am not looking for the likelihood of someone in the first generation having a descendant alive in the last generation. I was afraid that's where Wipa came up with (1-z) for the answer.
Comment: @xpda you are correct in identifying my misinterpretation. I was working from 1300 forward and assuming a single finite initial population. whuber thank you for grasping the philosophical nature of my answer, this problem is certainly worthy of contemplation from many disciplines. This equation really does not take into account ancestry in the same way your genetics simulation does since anybody can reproduce with anybody and there are no gender constraints. Nevertheless I think you simulation does illustrate the likelihood of maintaining lineage for multiple generations / iterations.
Answer: This is a very interesting question as it is asking us to mathematically solve a fractal. Such as the famous game of life.
The % of the population which each generation related to will grow over each iteration, starting at $p_1={2 \over n_1}$ and at the limit generation will approach $\lim_{k \to \infty } p_k = (1-z)$.
If we denote $p_k$ as the probability of someone in generation $k$ to be related to the initial population. And for simplicity lets relax the siblings & cousins rule (can be added later). Then:
$$p_1 = {2 \over n_1}$$
As each person in the new generation has exactly 2 ancestors in the initial population.
$$
p_2 = relatives \times {2 \over n_2} + non.relatives \times {4 \over n_2}
$$
In this case relatives could be calculated as:
$$ relatives = {\binom{c}{2} \times {n \over c} \over \binom{n}{2}} = {c-1 \over n-1}$$
Or in other words, the number of sibling combinations, times the number of siblings family, divided by the total mating combinations.
$$p_3 = immediate.relatives \times {4 \over n_3} + cousins \times {6 \over n_3} + non.relatives \times {8 \over n_3}$$
With each generation, the probability to be related to someone at the initial population will undoubtedly grow, but at a decreasing pace. This is because the probability to draw "relatives" which are coming from the same or similar tree will grow.
Lets use ethnicity as an example. Lets say we know for a fact someone is 100% Caucasian. At generation 28 he is most likely related to a significant portion of the Caucasian population in 1300 (As shown by @whuber simulation). Lets say he is marrying someone who is 100% of a different ethnicity. Their offspring will be linked to approximately double the number of people they are linked to from 1300.
Another interesting thought is that given the human (homosapien) race started from ~600 people in Africa, then we are most likely a genetic permutation of all of them who successfully mated.


Conditionally mount components based on their return value

Question: Is there a way to check if a component renders null ?
const ComponentA = ({ shouldRender }) => {
  if(!shouldRender){
     return null;
  }

  return
Foo

}

const ComponentB = () =>
Bar

// I want this component to render ComponentA
const ComponentC = () => {
  return <ComponentA shouldRender /> || <ComponentB />;
}

// I want this component to render ComponentB, as ComponentA returns null
const ComponentD = () => {
  return <ComponentA shouldRender={false} /> || <ComponentB />;
}

Renders
Foo
, as expected
<ComponentC />

I want this to render
Bar
, but ComponentA is mounted and renders null
<ComponentD />

Answer: AFAIK, this is not possible. You will have to either render conditionally on the outer component:
const ComponentD = () => {
  return shouldRender ? <ComponentA shouldRender={true} /> : <ComponentB />;
}

or render both component, but giving them the shouldRender property, as a component rendering null is actually not displayed:
const ComponentD = () => {
  return (
    <>
      <ComponentA shouldRender={shouldRender} />
      <ComponentB shouldRender={!shouldRender} />
    </>
  );
}

EDIT: If the condition for rendering or not ComponentA is inside the component, and complex enough for you don't want to duplicate it, you should probably externalize it in a hook, used by both components A and B.
Comment: Thank you. Yes I maybe did not write a good example. The condition to render the component is performed inside the component itself:

`const ComponentA = () => shouldRender ? Foo : null`

Using a shared hook was what I thought initially, but wasn't sure if there was a "simpler" solution to my problem.
Answer: const ComponentC = () => {
    return shouldRender ? <ComponentA /> : <ComponentB />;
}


Why delete old redirects?

Question: I have many, MANY redirects (thousands).  Most just redirect the system (node/12345) url to the friendly one - but we also have a lot of others.
I have Drupal set to never delete old redirects after they haven't been accessed for X amount of time, but the mere presence of the option to do so makes me think there's a legitimate reason I may want to.
Is there a lot of overhead?  Or is it simply to keep the list manageable?  Do people typically delete them, and if so, what's the time frame?  Six months?  A year?
Answer: Whether to keep redirects is an SEO question. Do you have good search results on Google which you don't want to loose. Or you have external sites or even print stuff that point to your old URLs. Then you have to keep them.
If all this is not true, you can safely delete you old redirects.
About the DB:
The redirects are stored in the url_alias table. It has two indexes for the mapping from nid to redirect url and from redirect url to nid. This means that reading is fast even if you have thousands of redirects.
If you are creating a lot of redirects this can get slow. But this will not hit you for a few thousand redirects. If you have millions you have to dive into MySql details (or whatever DB system you are using) and find what you can do about it. Since you are only adding records to the DB table, there's a lot of DB tuning stuff that can help you.
Answer: Typical you wont have a big problem with that. But it depends on your site size and your server environment. Having less records in your table is always faster than having a more. Example:
Senario 1 : You have 10 redirects in your database. A user must be redirected and drupal executes the database SELECT function. Then after a very short time the database will return the result because it searches only within 10 records.
Senario 2 : You have 9999999 redirects in your database. A user must be redirected and drupal executes the database SELECT function. Your database server must search within 9999999 records and that may take a while. Also it may crash and go out of memory.
BUT if your redirect records remains constant then you wont have any problems at all. You can keep the redirects as long as you want.

Why delete old redirects?


Services module, custom endpoint & authentication

Question: I have a have setup a custom endpoint via services and a REST server the custom end point looks like this,
https://website.local/api/v1/druaplsquare
The custom endpoint is built using the following code,
drupalsqaure.module
function drupalsquare_services_resources() {
  $resources = array();
  $resources['drupalsquare'] = array(
    'operations' => array(
         'create' => array(
             'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/drupalsquare'),
             'callback' => '_job_resource_create',
             'args' => array(
                 array(
                     'name' => 'node',
                     'optional' => FALSE,
                     'source' => 'data',
                     'description' => 'The node data to create',
                     'type' => 'array',
                 ),
             ),
             'access callback' => '_drupalsquare_resource_access'
         ),
         'delete' => array(
             'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/drupalsquare'),
             'callback' => '_job_resource_delete',
             'args' => array(
                 array(
                     'name' => 'jobref',
                     'optional' => FALSE,
                     'source' => array('path' => 0),
                     'description' => 'The job reference to delete',
                     'type' => 'string',
                 ),
             ),
             'access callback' => '_drupalsquare_resource_access'
         ),
    ),
  );
  return $resources;
}

resources/drupalsquare.inc
<?php

function _drupalsquare_resource_access() {
    return TRUE;
}

//function _drupalsquare_resource_retrieve($uid) {
//  $account = user_load($uid);
//
//  if (isset($account->data['checked_in'])) {
//      return array(
//          'uid' => $account->uid,
//          'name' => $account->name,
//          'last_checkin' => $account->data['checked_in'],
//      );
//  }
//  else {
//      return FALSE;
//  }
//}

function _drupalsquare_resource_checkin($JobReference, $date = NULL) {
    die(print_r($JobReference));
//  $account = user_load($uid);
//  $account->data['checked_in'] = is_null($date) ? REQUEST_TIME : $date;
//  user_save($account);
    return TRUE;
}

function _job_resource_create() {
    die("create");
}

function _job_resource_delete($jobref) {
    die("here");
}

I am making a cURL request this is endpoint, but I am first of all authenticating a user, and getting a token, I am then sending this token in the request header, however the endpoint above is returning,

Access denied for user anonymous

Can anyone see any reason as why this would be happening?
Comment: Have you checked Services modules permissions?
Comment: Authenticated user can do all the actions they should be able to do.
Answer: In your code
Replace 'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/drupalsquare'),
with 'file' => array('type' => 'inc', 'module' => 'NAME_OF_YOUR_MODULE', 'name' => 'resources/drupalsquare'),
It seems you're wrongly defining your INC file.


Compare $\int_0^1 f(x)\log f(x)\,dx$ and $\int_0^1f(s)\,ds\cdot\int_0^1\log f(t)\,dt$

Question:
The question: Given $f$ to be a positive, measurable function on $[0,1]$, which is larger, $\displaystyle\int_0^1 f(x)\log f(x)\,dx$ or $\displaystyle\left(\int_0^1f(s)\,ds\right)\left(\int_0^1\log f(t)\,dt\right)$?

I know from testing with $f(x)=x$ that the first integral is, indeed, larger. Of course, this isn't a rigorous "proof" (if it can even be called that). I am unsure as to go about this. At first glance, it looks similar to a Holder's Inequality problem, but appears to go the wrong direction. If anyone has a hint/suggestion as to where to start, it would be much appreciated. Thanks in advance!
EDIT:
I've now found examples which show equality and inequality the other direction. If I let $f(x)=\cases{c\text{ if }x\in[0,1/2)\newline1\text{ otherwise}}$, then the first integral is smaller if $0<c<1$, they are equal if $c=1$, and the second is smaller if $c>1$. The new question, is this a sufficient answer: The magnitude of the integral depends on the function $f$?
RE-EDIT:
I forgot about the sign change of $\log c$ when $c<1$, so I either have the integrals being equal when $c=1$ or the second integral being larger for other values of $c$. I suppose my original question still remains.
Comment: I don't follow your edit: that function gives integrals of $\frac12 c \log c $ and $\frac12 (c+1)\log(c)$ respectively; while they're both zero if $c=1$, the LHS is otherwise always always bigger (note the sign flip of log when $c<1$.)
Comment: Well, that was embarrassing. You are completely correct - I completely ignored the sign change when $c<1$. I guess the original question still holds, then...
Answer: This is a particular case of a vastly more general result. To wit, consider two functions $G$ and $H$, both increasing, such that $G(f)$ and $H(f)$ are both integrable. Then,

$$\int_0^1G(f(x))\mathrm dx\cdot\int_0^1H(f(x))\mathrm dx\leqslant\int_0^1G(f(x))H(f(x))\mathrm dx.$$

To show this, integrate on $(0,1)\times(0,1)$ with respect to the Lebesgue measure the inequality $$[G(f(x))-G(f(y))]\cdot[H(f(x))-H(f(y))]\geqslant0,$$ which holds everywhere in $(0,1)\times(0,1)$, and expand the inequality you get.
Thus, the result in the question uses only the monotonicity of the functions $t\mapsto t$ and $t\mapsto\log t$.
Comment: Yes there is something deep, which is (unfortunately) largely overlooked in many analysis curricula, and is called *coupling*.
Comment: Wow, that is nice! Is there something deep to this, or how did you come up with this solution?
Answer: Let me give you a sequence of hints:

 1. Do you know Jensen's inequality?

$ $

 2. $\frac{d^2}{dx^2} (x \cdot \ln(x)) = \frac{d}{dx} (\ln(x) + x \cdot 1/x) = 1/x > 0$ for $x>0$. Hence, the map $\Phi : (0,\infty) \to \Bbb{R}, x \mapsto x \cdot \ln(x)$ is convex.

$ $

 3. An application of Jensen's inequality yields $$\int_0^1 f(x) dx \cdot \ln \left(\int_0^1 f(x) dx\right) =\Phi \left( \int_0^1 f(x)\,dx\right)\leq \int_0^1 \Phi(f(x)) \, dx$$

$ $

 4. Finally, $\frac{d^2}{dx^2}(-\ln(x)) = \frac{d}{dx} -1/x = x^{-2} > 0$, so that $-\ln : (0,\infty) \to \Bbb{R}$ is also convex. Another application of Jensen gives $$- \ln\left(\int_0^1 f(x) \, dx \right) \leq \int_0^1 -\ln(f(x))\,dx$$. Together with step (3) and with $\int_0^1 f(x) \,dx > 0$, this proves the claim.
Comment: PhoemueX: The OP seems to like the use of technology for itself... :-)


Bounded Linear Transformation proof

Question: One paragraph in my text is to prove that $\|T\|=\sup\{|\langle Tf, g\rangle|:\|f\|<1, \|g\|<1\}$, where we have a bounded linear operator between two Hilbert spaces $T:\mathcal H_1\rightarrow \mathcal H_2$ and $f\in\mathcal H_1, f\in\mathcal H_2$. FYI, the norm of $T$ is defined as the minimum constant such that $\|Tf\|_{\mathcal H_2}\leq M\|f\|_{\mathcal H_1}$. I can following the details in my book. However, I do not understand the rationale behind the proof. Any clarification will be appreciated.
Normally, if we want to show $a=b$, we can proceed to show $a\geq b$ and $a\leq b$. However, the proof in my book proceeded as follows.

Assume $\|T\|\leq M$, then show $|\langle Tf, g\rangle|\leq M$. Update: This makes sense if we rephrase the proof as follows. If $|\langle Tf, g\rangle|\leq M$ for all $M$, then it is clear that $\sup|\langle Tf, g\rangle|\leq \inf M=\|T\|.$
Assume $\sup\{|\langle Tf, g\rangle|:\|f\|<1, \|g\|<1\}\leq M$, then show $\|Tf\|_{\mathcal H_2}\leq M\|f\|_{\mathcal H_1}$.

My question is why proving 1 and 2 above is sufficient to prove the opening lemma.
Answer: (1) proves that $|\langle Tf,g \rangle|\leq ||T||$, so $\sup|\langle Tf, g\rangle|\leq ||T||$.
(2) similarly proves that $||T||\leq \sup|\langle Tf, g\rangle|$, by taking $M=\sup|\langle Tf, g\rangle|\leq ||T||$.
What's going on here is you're trying to prove a sup of one set of numbers is the same as the sup of another set of numbers.  If you think of it in terms of least upper bounds you would arrive at trying to prove the statements with "$\leq M$".
Comment: It should be to prove the sup of a set equals the inf of another set, not sup equal sup. In addition, (1) only shows that $|\langle Tf,g \rangle|\leq M$ instead of $\|T\|$, right?
Comment: For the sup/sup vs sup/inf thing: Right, I was thinking of one of the other definitions of norm of an operator.  If (1) is true then since $||T||\leq||T||$ we may use it for M.

Bounded Linear Transformation proof